GHTControlBase.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. //
  2. // Authors:
  3. // Rafael Mizrahi <[email protected]>
  4. // Erez Lotan <[email protected]>
  5. // Vladimir Krasnov <[email protected]>
  6. //
  7. //
  8. // Copyright (c) 2002-2005 Mainsoft Corporation.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Web.UI;
  31. using System.Web.UI.HtmlControls;
  32. using System.Web.UI.WebControls;
  33. using System.Collections;
  34. using System.Data;
  35. namespace GHTTests
  36. {
  37. /// <summary>
  38. /// Provides basic functionalities for testing System.Web.UI.Control derived classes.
  39. /// </summary>
  40. public class GHTControlBase : GHTBaseWeb
  41. {
  42. #region "Data members"
  43. protected Control m_cToTest; //The control that is currently being tested.
  44. protected TextBox m_tbToValidate; //will be used by validation controls as the control to validate.
  45. protected Item[] m_aDataSource; //Array data source to use in data bound objects.
  46. protected DataTable m_dtDataSource;//DataTable data source to use in data bound objects.
  47. protected ArrayList m_derivedTypes; //The array that wil contain all types that are derived from Control, and need to be tested.
  48. protected long m_controlsCounter; //Used to generate a unique id for each of the controls created using GHTActiveSubTestControlClone
  49. #endregion
  50. #region "Construction"
  51. /// <summary>
  52. /// Default c'tor.
  53. /// handles basic initialization of the page, and contents.
  54. /// </summary>
  55. public GHTControlBase()
  56. {
  57. InitTypes();
  58. InitDataSource();
  59. InitTbToValidate();
  60. m_controlsCounter = 0;
  61. }
  62. #endregion
  63. #region "Properties"
  64. protected Control TestedControl
  65. {
  66. get
  67. {
  68. return m_cToTest;
  69. }
  70. }
  71. public Type[] TypesToTest
  72. {
  73. get
  74. {
  75. return (System.Type[])(m_derivedTypes.ToArray(typeof(System.Type)));
  76. }
  77. }
  78. #endregion
  79. #region "Methods"
  80. /// <summary>
  81. /// Initializes all the derived types that need to be tested.
  82. /// </summary>
  83. protected virtual void InitTypes()
  84. {
  85. m_derivedTypes = new ArrayList();
  86. //System.Web.UI:
  87. // typeof(System.Web.UI.PartialCachingControl), Excluded from test.
  88. // typeof(System.Web.UI.StaticPartialCachingControl), Excluded from test.
  89. // typeof(System.Web.UI.DataBoundLiteralControl), Excluded from test.
  90. m_derivedTypes.Add(typeof(System.Web.UI.LiteralControl));
  91. m_derivedTypes.Add(typeof(System.Web.UI.Page));
  92. m_derivedTypes.Add(typeof(System.Web.UI.UserControl));
  93. //System.Web.UI.HtmlControls:
  94. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlAnchor));
  95. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlButton));
  96. // typeof(System.Web.UI.HtmlControls.HtmlForm), Excluded from test.
  97. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlGenericControl));
  98. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlImage));
  99. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputButton));
  100. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputCheckBox));
  101. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputFile));
  102. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputHidden));
  103. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputImage));
  104. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputRadioButton));
  105. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlInputText));
  106. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlSelect));
  107. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlTable));
  108. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlTableCell));
  109. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlTableRow));
  110. m_derivedTypes.Add(typeof(System.Web.UI.HtmlControls.HtmlTextArea));
  111. //System.Web.UI.WebControls basic:
  112. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Button));
  113. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.CheckBox));
  114. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.HyperLink));
  115. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Image));
  116. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.ImageButton));
  117. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Label));
  118. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.LinkButton));
  119. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Literal));
  120. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Panel));
  121. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.PlaceHolder));
  122. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RadioButton));
  123. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.TextBox));
  124. //System.Web.UI.WebControls basic list controls:
  125. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DropDownList));
  126. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.ListBox));
  127. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RadioButtonList));
  128. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.CheckBoxList));
  129. //System.Web.UI.WebControls validation controls:
  130. #if KNOWN_BUG //BUG_NUM:935
  131. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.CompareValidator));
  132. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.CustomValidator));
  133. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RangeValidator));
  134. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RegularExpressionValidator));
  135. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RequiredFieldValidator));
  136. #endif
  137. #if KNOWN_BUG //BUG_NUM:1195
  138. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.ValidationSummary));
  139. #endif
  140. //System.Web.UI.WebControls rich controls (currently not supported):
  141. // m_derivedTypes.Add(typeof(System.Web.UI.WebControls.AdRotator));
  142. // m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Calendar));
  143. //System.Web.UI.WebControls advanced list controls:
  144. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DataGrid));
  145. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DataGridItem));
  146. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DataList));
  147. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.DataListItem));
  148. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Repeater));
  149. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.RepeaterItem));
  150. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.Table));
  151. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.TableCell));
  152. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.TableHeaderCell));
  153. m_derivedTypes.Add(typeof(System.Web.UI.WebControls.TableRow));
  154. //m_derivedTypes.Add(typeof( System.Web.UI.WebControls.Xml));
  155. }
  156. /// <summary>
  157. /// Adds a control to page.
  158. /// If the control is in the context of other control (Have a parent)
  159. /// (e.g.) TableCell, then the parent control is added to the page.
  160. /// </summary>
  161. /// <param name="a_toAdd">The control to add to the page.</param>
  162. protected void GHTAddToActiveForm(Control a_toAdd)
  163. {
  164. if (a_toAdd.Parent == null)
  165. {
  166. GHTActiveForm.Controls.Add(a_toAdd);
  167. }
  168. else
  169. {
  170. GHTAddToActiveForm(a_toAdd.Parent);
  171. }
  172. }
  173. /// <summary>
  174. /// Creates a control to test, and adds it to a new subtest.
  175. /// </summary>
  176. /// <param name="ctrlType">Type of control to test.</param>
  177. /// <param name="description">description to add to subtest.</param>
  178. protected void GHTSubTestBegin(Type ctrlType, string description)
  179. {
  180. GHTSubTestBegin(ctrlType, description, true);
  181. }
  182. /// <summary>
  183. /// Creates a new subtest, and a control to test.
  184. /// </summary>
  185. /// <param name="ctrlType">Type of control to test.</param>
  186. /// <param name="description">description to add to subtest.</param>
  187. /// <param name="a_AddToPage">Whether to add the control to the subtest continer.</param>
  188. protected void GHTSubTestBegin(Type a_ctrlType, string description, bool a_addToPage)
  189. {
  190. base.GHTSubTestBegin(a_ctrlType.ToString() + ": " + description);
  191. GHTActiveSubtestControlClone(a_ctrlType, a_addToPage);
  192. }
  193. /// <summary>
  194. /// Clones a control within its context, and adds it to the active subtest.
  195. /// e.g. if the control is a table cell, then a table will be created to wrap the table cell.
  196. /// The table will be added to the active subtest, and the cell will be referened by m_m_cToTest.
  197. /// </summary>
  198. /// <param name="a_ctrlType">Type of control to test.</param>
  199. /// <param name="a_addToPage">Whether to add the control to the subtest continer.</param>
  200. protected void GHTActiveSubtestControlClone(Type a_ctrlType, bool a_addToPage)
  201. {
  202. if (IsListControlDerived(a_ctrlType))
  203. {
  204. m_cToTest = GetListControlDerived(a_ctrlType, a_addToPage);
  205. }
  206. else if (IsIterativeControlControl(a_ctrlType))
  207. {
  208. m_cToTest = GetIterativeControl(a_ctrlType, a_addToPage);
  209. }
  210. else if (IsIterativeControlControlItem(a_ctrlType))
  211. {
  212. m_cToTest = GetIterativeControlItem(a_ctrlType, a_addToPage);
  213. }
  214. else if (IsTableRelated(a_ctrlType))
  215. {
  216. m_cToTest = GetTableRelated(a_ctrlType, a_addToPage);
  217. }
  218. else if (IsHTMLTableRelated(a_ctrlType))
  219. {
  220. m_cToTest = GetHtmlTableRelated(a_ctrlType, a_addToPage);
  221. }
  222. else
  223. {
  224. m_cToTest = (Control)GHTElementClone(a_ctrlType);
  225. if (a_addToPage)
  226. {
  227. GHTActiveSubTest.Controls.Add(m_cToTest);
  228. }
  229. }
  230. HandleValidationControls();
  231. HandleCausesValidation();
  232. SetId(); //Set a unique id to the control.
  233. }
  234. /// <summary>
  235. /// checks if a given type is derived from ListControl, and thus should be bound to data in order to display content.
  236. /// </summary>
  237. /// <param name="ctrlType">The type to test.</param>
  238. /// <returns>True if the ctrlType is derived from ListControl, otherwise false.</returns>
  239. private bool IsListControlDerived(Type ctrlType)
  240. {
  241. return ctrlType.IsSubclassOf(typeof(ListControl));
  242. }
  243. /// <summary>
  244. /// Creates a control of the specified type, if it is a ListControl derived, then bounds it to data source.
  245. /// </summary>
  246. /// <param name="ctrlType">Type of control to create.</param>
  247. private Control GetListControlDerived(Type ctrlType, bool a_AddToPage)
  248. {
  249. ListControl l_listcontrol = GHTElementClone(ctrlType) as ListControl;
  250. if (l_listcontrol != null)
  251. {
  252. l_listcontrol.DataSource = m_aDataSource;
  253. l_listcontrol.DataTextField = "Description";
  254. l_listcontrol.DataValueField = "Id";
  255. l_listcontrol.DataBind();
  256. if (a_AddToPage)
  257. {
  258. GHTActiveSubTest.Controls.Add(l_listcontrol);
  259. }
  260. return l_listcontrol;
  261. }
  262. else
  263. {
  264. throw new ArgumentOutOfRangeException("ctrlType", ctrlType, "Must be a ListControl derived type.");
  265. }
  266. }
  267. /// <summary>
  268. /// checks if a given type is an item of an iterative control, and thus should be checked in tis controls context.
  269. /// </summary>
  270. /// <param name="ctrlType">The type to test.</param>
  271. /// <returns>True if the ctrlType is an iteative control item, otherwise false.</returns>
  272. private bool IsIterativeControlControlItem(Type ctrlType)
  273. {
  274. if ( ctrlType.Equals(typeof(RepeaterItem)))
  275. {
  276. return true;
  277. }
  278. else if (ctrlType.Equals(typeof(DataListItem)))
  279. {
  280. return true;
  281. }
  282. else if (ctrlType.Equals(typeof(DataGridItem)))
  283. {
  284. return true;
  285. }
  286. else
  287. {
  288. return false;
  289. }
  290. }
  291. /// <summary>
  292. /// Creates an iterative control Item, included inside an iterative control..
  293. /// </summary>
  294. /// <param name="ctrlType">Type of control to create.</param>
  295. private Control GetIterativeControlItem(Type ctrlType, bool a_AddToPage)
  296. {
  297. if (ctrlType.Equals(typeof(RepeaterItem)))
  298. {
  299. Repeater l_rep = GetIterativeControl(typeof(Repeater), a_AddToPage) as Repeater;
  300. SetId(l_rep);
  301. return l_rep.Items[0];
  302. }
  303. if (ctrlType.Equals(typeof(DataListItem)))
  304. {
  305. DataList l_datalist = GetIterativeControl(typeof(DataList), a_AddToPage) as DataList;
  306. SetId(l_datalist);
  307. return l_datalist.Items[0];
  308. }
  309. if (ctrlType.Equals(typeof(DataGridItem)))
  310. {
  311. DataGrid l_datagrid = GetIterativeControl(typeof(DataGrid), a_AddToPage) as DataGrid;
  312. SetId(l_datagrid);
  313. return l_datagrid.Items[0];
  314. }
  315. else
  316. {
  317. throw new ArgumentOutOfRangeException("ctrlType", ctrlType, "Allowed types are RepeaterItem, DataListItem or DataGridItem");
  318. }
  319. }
  320. /// <summary>
  321. /// Checks if a given type is an iterative control, and thus should be boud to data.
  322. /// </summary>
  323. /// <param name="ctrlType">The type to test.</param>
  324. /// <returns>True if the ctrlType is an iteative control, otherwise false.</returns>
  325. private bool IsIterativeControlControl(Type ctrlType)
  326. {
  327. if ( ctrlType.Equals(typeof(Repeater)))
  328. {
  329. return true;
  330. }
  331. else if (ctrlType.Equals(typeof(DataList)))
  332. {
  333. return true;
  334. }
  335. else if (ctrlType.Equals(typeof(DataGrid)))
  336. {
  337. return true;
  338. }
  339. else
  340. {
  341. return false;
  342. }
  343. }
  344. /// <summary>
  345. /// Creates an iterative control.
  346. /// </summary>
  347. /// <param name="ctrlType">Type of control to create.</param>
  348. protected Control GetIterativeControl(Type ctrlType, bool a_AddToPage)
  349. {
  350. if (ctrlType.Equals(typeof(Repeater)))
  351. {
  352. Repeater l_rep = new Repeater();
  353. l_rep.ItemTemplate = new RepeaterTemplate();
  354. l_rep.DataSource = m_aDataSource;
  355. l_rep.DataBind();
  356. if (a_AddToPage)
  357. {
  358. GHTActiveSubTest.Controls.Add(l_rep);
  359. }
  360. return l_rep;
  361. }
  362. else if (ctrlType.Equals(typeof(DataList)))
  363. {
  364. DataList l_dataList = new DataList();
  365. l_dataList.ItemTemplate = new DataListTemplate();
  366. l_dataList.DataSource = m_aDataSource;
  367. l_dataList.DataBind();
  368. if (a_AddToPage)
  369. {
  370. GHTActiveSubTest.Controls.Add(l_dataList);
  371. }
  372. return l_dataList;
  373. }
  374. else if (ctrlType.Equals(typeof(DataGrid)))
  375. {
  376. DataGrid l_dataGrid = new DataGrid();
  377. l_dataGrid.DataSource = m_dtDataSource;
  378. l_dataGrid.DataBind();
  379. if (a_AddToPage)
  380. {
  381. GHTActiveSubTest.Controls.Add(l_dataGrid);
  382. }
  383. return l_dataGrid;
  384. }
  385. else
  386. {
  387. throw new ArgumentOutOfRangeException("ctrlType", ctrlType, "Allowed types are Repeater, DataList or DataGrid");
  388. }
  389. }
  390. /// <summary>
  391. /// checks if a given type is a control that should be tested in the context of a HtmlTable.
  392. /// </summary>
  393. /// <param name="ctrlType">The type to test.</param>
  394. /// <returns>True if the ctrlType is a HtmlTable related type, otherwise false.</returns>
  395. private bool IsHTMLTableRelated(Type ctrlType)
  396. {
  397. if ( ctrlType.Equals(typeof(HtmlTable)))
  398. {
  399. return true;
  400. }
  401. else if (ctrlType.Equals(typeof(HtmlTableRow)))
  402. {
  403. return true;
  404. }
  405. else if (ctrlType.Equals(typeof(HtmlTableCell)))
  406. {
  407. return true;
  408. }
  409. else
  410. {
  411. return false;
  412. }
  413. }
  414. /// <summary>
  415. /// Creates a new table, and adds it to the GHTActivesubTest.
  416. /// </summary>
  417. /// <param name="ctrlType">The type of the ctrl to test. must be one of </param>
  418. /// <param name="description"></param>
  419. /// <returns></returns>
  420. protected Control GetHtmlTableRelated(Type ctrlType, bool a_AddToPage)
  421. {
  422. HtmlTable l_table = new HtmlTable();
  423. HtmlTableRow l_row = new HtmlTableRow();
  424. HtmlTableCell l_cell = new HtmlTableCell();
  425. if (a_AddToPage)
  426. {
  427. GHTActiveSubTest.Controls.Add(l_table);
  428. }
  429. l_table.Rows.Add(l_row);
  430. l_row.Cells.Add(l_cell);
  431. l_cell.InnerText = "Cell";
  432. if ( l_table.GetType() == ctrlType)
  433. {
  434. return l_table;
  435. }
  436. else if (l_row.GetType() == ctrlType)
  437. {
  438. return l_row;
  439. }
  440. else if (l_cell.GetType() == ctrlType)
  441. {
  442. return l_cell;
  443. }
  444. else
  445. {
  446. throw new ArgumentException("Should be HtmlTable related type.", "ctrlType = " + ctrlType.ToString() );
  447. }
  448. }
  449. /// <summary>
  450. /// checks if a given type is a control that should be tested in the context of a table.
  451. /// </summary>
  452. /// <param name="ctrlType">The type to test.</param>
  453. /// <returns>True if the ctrlType is a table related type, otherwise false.</returns>
  454. private bool IsTableRelated(Type ctrlType)
  455. {
  456. if ( ctrlType.Equals(typeof(Table)))
  457. {
  458. return true;
  459. }
  460. else if (ctrlType.Equals(typeof(TableRow)))
  461. {
  462. return true;
  463. }
  464. else if (ctrlType.Equals(typeof(TableCell)))
  465. {
  466. return true;
  467. }
  468. else if(ctrlType.Equals(typeof(TableHeaderCell)))
  469. {
  470. return true;
  471. }
  472. else
  473. {
  474. return false;
  475. }
  476. }
  477. /// <summary>
  478. /// Creates a new table, and adds it to the GHTActivesubTest.
  479. /// </summary>
  480. /// <param name="ctrlType">The type of the ctrl to test. must be one of </param>
  481. /// <param name="description"></param>
  482. /// <returns></returns>
  483. protected Control GetTableRelated(Type ctrlType, bool a_AddToPage)
  484. {
  485. Table l_table = new Table();
  486. TableRow l_headerRow = new TableRow();
  487. TableRow l_row = new TableRow();
  488. TableCell l_cell = new TableCell();
  489. TableHeaderCell l_headerCell = new TableHeaderCell();
  490. if (a_AddToPage)
  491. {
  492. GHTActiveSubTest.Controls.Add(l_table);
  493. }
  494. l_table.Rows.Add(l_headerRow);
  495. l_table.Rows.Add(l_row);
  496. l_headerRow.Cells.Add(l_headerCell);
  497. l_row.Cells.Add(l_cell);
  498. l_headerCell.Text = "Header cell";
  499. l_cell.Text = "Table cell";
  500. if ( l_table.GetType() == ctrlType)
  501. {
  502. return l_table;
  503. }
  504. else if (l_row.GetType() == ctrlType)
  505. {
  506. return l_row;
  507. }
  508. else if (l_cell.GetType() == ctrlType)
  509. {
  510. return l_cell;
  511. }
  512. else if(l_headerCell.GetType() == ctrlType)
  513. {
  514. return l_headerCell;
  515. }
  516. else
  517. {
  518. throw new ArgumentException("Should be table related type.", "ctrlType = " + ctrlType.ToString() );
  519. }
  520. }
  521. /// <summary>
  522. /// All validation controls must be assigned a control to validate before the page is loaded:
  523. /// </summary>
  524. protected void HandleValidationControls(Control l_toTest)
  525. {
  526. BaseValidator l_validator = l_toTest as BaseValidator;
  527. if (l_validator == null)
  528. {
  529. return;
  530. }
  531. l_validator.ControlToValidate = "m_tbToValidate";
  532. HandleCompareValidator(l_toTest);
  533. }
  534. /// <summary>
  535. /// All validation controls must be assigned a control to validate before the page is loaded:
  536. /// </summary>
  537. protected void HandleValidationControls()
  538. {
  539. HandleValidationControls(m_cToTest);
  540. }
  541. /// <summary>
  542. /// Compare validator must have either ValueToCompare or ControlToCompare set, before the page is loaded.
  543. /// </summary>
  544. private void HandleCompareValidator(Control l_cToTest)
  545. {
  546. CompareValidator l_compareValidator = l_cToTest as CompareValidator;
  547. if (l_compareValidator == null)
  548. {
  549. return;
  550. }
  551. l_compareValidator.ValueToCompare = "value to compare";
  552. }
  553. /// <summary>
  554. /// Compare validator must have either ValueToCompare or ControlToCompare set, before the page is loaded.
  555. /// </summary>
  556. private void HandleCompareValidator()
  557. {
  558. HandleCompareValidator(m_cToTest);
  559. }
  560. /// <summary>
  561. /// Sets the causes validation property to false, if such a property exists in the m_cToTest.
  562. /// This is needed because for controls that cause validation, the ValidationControls on the page
  563. /// generate a client side script.
  564. /// That feature is not working properly in GrassHopper, and is not the main issue of this test.
  565. /// It will be testes specificly in the Validation control tests.
  566. /// </summary>
  567. private void HandleCausesValidation()
  568. {
  569. //HtmlButton:
  570. HtmlButton hButton = m_cToTest as HtmlButton;
  571. if (hButton != null)
  572. {
  573. hButton.CausesValidation = false;
  574. }
  575. //HtmlInputButton
  576. HtmlInputButton hInputButton = m_cToTest as HtmlInputButton;
  577. if (hInputButton != null)
  578. {
  579. hInputButton.CausesValidation = false;
  580. }
  581. //HtmlInputImage
  582. HtmlInputImage hInputImage = m_cToTest as HtmlInputImage;
  583. if (hInputImage != null)
  584. {
  585. hInputImage.CausesValidation = false;
  586. }
  587. //Button:
  588. Button button = m_cToTest as Button;
  589. if (button != null)
  590. {
  591. button.CausesValidation = false;
  592. }
  593. //ImageButton
  594. ImageButton imageButton = m_cToTest as ImageButton;
  595. if (imageButton != null)
  596. {
  597. imageButton.CausesValidation = false;
  598. }
  599. //LinkButton
  600. LinkButton linkButton = m_cToTest as LinkButton;
  601. if (linkButton != null)
  602. {
  603. linkButton.CausesValidation = false;
  604. }
  605. }
  606. /// <summary>
  607. /// Creates a textbox, and adds it to the page as not visable.
  608. /// Creates a Required Field validator, and adds it to the page as not visible.
  609. /// </summary>
  610. private void InitTbToValidate()
  611. {
  612. m_tbToValidate = new TextBox();
  613. m_tbToValidate.Visible = false;
  614. m_tbToValidate.ID = "m_tbToValidate";
  615. this.Controls.Add(m_tbToValidate);
  616. }
  617. /// <summary>
  618. /// Creates the data sources to bind to.
  619. /// </summary>
  620. private void InitDataSource()
  621. {
  622. InitDataArray();
  623. InitDataTable();
  624. }
  625. /// <summary>
  626. /// Creates an array to be used as a data source.
  627. /// </summary>
  628. private void InitDataArray()
  629. {
  630. m_aDataSource = new Item[] { new Item(1,"aaaa"),
  631. new Item(2,"bbbb"),
  632. new Item(3,"cccc"),
  633. new Item(4,"dddd")};
  634. }
  635. /// <summary>
  636. /// creates a data table to be used as a data source.
  637. /// </summary>
  638. private void InitDataTable()
  639. {
  640. m_dtDataSource = new DataTable("SourceTable");
  641. DataColumn l_dcId = new DataColumn("Id", typeof(int));
  642. DataColumn l_dcDescription = new DataColumn("Description", typeof(string));
  643. m_dtDataSource.Columns.Add(l_dcId);
  644. m_dtDataSource.Columns.Add(l_dcDescription);
  645. for (int i=0; i<m_aDataSource.Length; i++)
  646. {
  647. DataRow l_drCurrent = m_dtDataSource.NewRow();
  648. l_drCurrent["Id"] = m_aDataSource[i].Id;
  649. l_drCurrent["Description"] = m_aDataSource[i].Description;
  650. m_dtDataSource.Rows.Add(l_drCurrent);
  651. }
  652. }
  653. /// <summary>
  654. /// Sets a unique unified id to the tested control based on the count of controls produced by this base.
  655. /// </summary>
  656. private void SetId()
  657. {
  658. SetId(m_cToTest);
  659. }
  660. /// <summary>
  661. /// Sets a unique unified id to a control based on the count of controls produced by this base.
  662. /// </summary>
  663. private void SetId(Control a_toSet)
  664. {
  665. a_toSet.ID = "ctrl_" + m_controlsCounter.ToString();
  666. m_controlsCounter++;
  667. }
  668. #endregion
  669. /// <summary>
  670. /// Used as an array item, for the data source of data bound controls in this test.
  671. /// </summary>
  672. public class Item
  673. {
  674. #region "Construction"
  675. public Item() : this(0, String.Empty)
  676. {
  677. }
  678. public Item(int a_id, string a_description)
  679. {
  680. m_id = a_id;
  681. m_description = a_description;
  682. }
  683. #endregion
  684. #region "Data Members"
  685. private int m_id;
  686. private string m_description;
  687. #endregion
  688. #region "Properties"
  689. public int Id
  690. {
  691. get
  692. {
  693. return m_id;
  694. }
  695. set
  696. {
  697. m_id = value;
  698. }
  699. }
  700. public string Description
  701. {
  702. get
  703. {
  704. return m_description;
  705. }
  706. set
  707. {
  708. m_description = value;
  709. }
  710. }
  711. #endregion
  712. #region "Overrides"
  713. public override string ToString()
  714. {
  715. return this.Id + " " + this.Description;
  716. }
  717. #endregion
  718. }
  719. /// <summary>
  720. /// The templte used by repeater to render its items.
  721. /// </summary>
  722. public class RepeaterTemplate : ITemplate
  723. {
  724. #region ITemplate Members
  725. /// <summary>
  726. /// Implements ITemplate.instantiateIn(..)
  727. /// Adds an item to the repeater.
  728. /// </summary>
  729. /// <param name="a_container">The controls to add the templated item to.</param>
  730. public void InstantiateIn(Control a_container)
  731. {
  732. //Header labels:
  733. //~~~~~~~~~~~
  734. Label l_lIdHeader = new Label();
  735. l_lIdHeader.Text = "ID: ";
  736. l_lIdHeader.Font.Bold = true;
  737. Label l_lDescrptionHeader = new Label();
  738. l_lDescrptionHeader.Text = " Description: ";
  739. l_lDescrptionHeader.Font.Bold = true;
  740. //Data labels:
  741. //~~~~~~~~~
  742. Label l_lIdData = new Label();
  743. l_lIdData.DataBinding += new EventHandler(this.BindId);
  744. Label l_lDescriptionData = new Label();
  745. l_lDescriptionData.DataBinding += new EventHandler(this.BindDescription);
  746. //The complete panel.
  747. Panel l_pItemPanel = new Panel();
  748. l_pItemPanel.Controls.Add(l_lIdHeader);
  749. l_pItemPanel.Controls.Add(l_lIdData);
  750. l_pItemPanel.Controls.Add(l_lDescrptionHeader);
  751. l_pItemPanel.Controls.Add(l_lDescriptionData);
  752. a_container.Controls.Add(l_pItemPanel);
  753. }
  754. #endregion
  755. /// <summary>
  756. /// Handles the data binding event of the Id label in the templated item.
  757. /// </summary>
  758. /// <param name="sender">The source of the event.</param>
  759. /// <param name="args">Additional information about the event.</param>
  760. private void BindId(Object sender, EventArgs args)
  761. {
  762. Label l_lIddata = (Label)sender;
  763. RepeaterItem l_riContainer = (RepeaterItem)l_lIddata.NamingContainer;
  764. l_lIddata.Text = (DataBinder.Eval(l_riContainer.DataItem, "Id")).ToString();
  765. }
  766. /// <summary>
  767. /// Handles the data binding event of the Description label in the templated item.
  768. /// </summary>
  769. /// <param name="sender">The source of the event.</param>
  770. /// <param name="args">Additional information about the event.</param>
  771. private void BindDescription(Object sender, EventArgs args)
  772. {
  773. Label l_lDescriptionData = (Label)sender;
  774. RepeaterItem l_riContainer = (RepeaterItem)l_lDescriptionData.NamingContainer;
  775. l_lDescriptionData.Text = (DataBinder.Eval(l_riContainer.DataItem, "Description")).ToString();
  776. }
  777. }
  778. /// <summary>
  779. /// The templte used by DataList to render its items.
  780. /// </summary>
  781. public class DataListTemplate : ITemplate
  782. {
  783. #region ITemplate Members
  784. /// <summary>
  785. /// Implements ITemplate.instantiateIn(..)
  786. /// Adds an item to the data list.
  787. /// </summary>
  788. /// <param name="a_container">The controls to add the templated item to.</param>
  789. public void InstantiateIn(Control a_container)
  790. {
  791. //Header labels:
  792. //~~~~~~~~~~~
  793. Label l_lIdHeader = new Label();
  794. l_lIdHeader.Text = "ID: ";
  795. l_lIdHeader.Font.Bold = true;
  796. Label l_lDescrptionHeader = new Label();
  797. l_lDescrptionHeader.Text = " Description: ";
  798. l_lDescrptionHeader.Font.Bold = true;
  799. //Data labels:
  800. //~~~~~~~~~
  801. Label l_lIdData = new Label();
  802. l_lIdData.DataBinding += new EventHandler(this.BindId);
  803. Label l_lDescriptionData = new Label();
  804. l_lDescriptionData.DataBinding += new EventHandler(this.BindDescription);
  805. //The complete panel.
  806. Panel l_pItemPanel = new Panel();
  807. l_pItemPanel.Controls.Add(l_lIdHeader);
  808. l_pItemPanel.Controls.Add(l_lIdData);
  809. l_pItemPanel.Controls.Add(l_lDescrptionHeader);
  810. l_pItemPanel.Controls.Add(l_lDescriptionData);
  811. a_container.Controls.Add(l_pItemPanel);
  812. }
  813. #endregion
  814. /// <summary>
  815. /// Handles the data binding event of the Id label in the templated item.
  816. /// </summary>
  817. /// <param name="sender">The source of the event.</param>
  818. /// <param name="args">Additional information about the event.</param>
  819. private void BindId(Object sender, EventArgs args)
  820. {
  821. Label l_lIddata = (Label)sender;
  822. DataListItem l_dliContainer = (DataListItem)l_lIddata.NamingContainer;
  823. l_lIddata.Text = (DataBinder.Eval(l_dliContainer.DataItem, "Id")).ToString();
  824. }
  825. /// <summary>
  826. /// Handles the data binding event of the Description label in the templated item.
  827. /// </summary>
  828. /// <param name="sender">The source of the event.</param>
  829. /// <param name="args">Additional information about the event.</param>
  830. private void BindDescription(Object sender, EventArgs args)
  831. {
  832. Label l_lDescriptionData = (Label)sender;
  833. DataListItem l_dliContainer = (DataListItem)l_lDescriptionData.NamingContainer;
  834. l_lDescriptionData.Text = (DataBinder.Eval(l_dliContainer.DataItem, "Description")).ToString();
  835. }
  836. }
  837. }
  838. }