GHTListControlBase.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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.Collections;
  31. using System.ComponentModel;
  32. using System.Web;
  33. using System.Web.SessionState;
  34. using System.Web.UI;
  35. using System.Web.UI.WebControls;
  36. using System.Web.UI.HtmlControls;
  37. using GHTWebControls;
  38. using System.Drawing;
  39. using System.Data;
  40. namespace GHTTests
  41. {
  42. /// <summary>
  43. /// Summary description for GHTListControlBase.
  44. /// </summary>
  45. public class GHTListControlBase : GHTBaseWeb
  46. {
  47. #region "Tests"
  48. protected void ListControl_AutoPostBack(Type ctrlType)
  49. {
  50. #region "Setting to true"
  51. GHTListContorlSubTestBegin(ctrlType, "AutoPostBack = True");
  52. try
  53. {
  54. m_lcToTest.AutoPostBack = true;
  55. }
  56. catch (Exception ex)
  57. {
  58. GHTSubTestUnexpectedExceptionCaught(ex);
  59. }
  60. GHTSubTestEnd();
  61. #endregion
  62. #region "Getting true:"
  63. GHTListContorlSubTestBegin(ctrlType, "Get AutoPostBack true");
  64. try
  65. {
  66. m_lcToTest.AutoPostBack = true;
  67. Compare(m_lcToTest.AutoPostBack, true);
  68. }
  69. catch (Exception ex)
  70. {
  71. GHTSubTestUnexpectedExceptionCaught(ex);
  72. }
  73. #endregion
  74. #region "Setting to false"
  75. GHTListContorlSubTestBegin(ctrlType, "AutoPostBack = false");
  76. try
  77. {
  78. m_lcToTest.AutoPostBack = false;
  79. }
  80. catch (Exception ex)
  81. {
  82. GHTSubTestUnexpectedExceptionCaught(ex);
  83. }
  84. GHTSubTestEnd();
  85. #endregion
  86. #region "Getting false:"
  87. GHTListContorlSubTestBegin(ctrlType, "Get AutoPostBack false");
  88. try
  89. {
  90. m_lcToTest.AutoPostBack = false;
  91. Compare(m_lcToTest.AutoPostBack, false);
  92. }
  93. catch (Exception ex)
  94. {
  95. GHTSubTestUnexpectedExceptionCaught(ex);
  96. }
  97. #endregion
  98. }
  99. protected void ListControl_ClearSelection(Type ctrlType)
  100. {
  101. #region "No item selected"
  102. GHTListContorlSubTestBegin(ctrlType, "No ite, selected");
  103. try
  104. {
  105. m_lcToTest.Items.Add("A");
  106. m_lcToTest.Items.Add("B");
  107. m_lcToTest.Items.Add("C");
  108. m_lcToTest.ClearSelection();
  109. }
  110. catch (Exception ex)
  111. {
  112. GHTSubTestUnexpectedExceptionCaught(ex);
  113. }
  114. GHTSubTestEnd();
  115. #endregion
  116. #region "One item selected"
  117. GHTListContorlSubTestBegin(ctrlType, "One item selected");
  118. try
  119. {
  120. m_lcToTest.Items.Add("A");
  121. m_lcToTest.Items.Add("B");
  122. m_lcToTest.Items.Add("C");
  123. m_lcToTest.SelectedIndex = 1;
  124. m_lcToTest.ClearSelection();
  125. }
  126. catch (Exception ex)
  127. {
  128. GHTSubTestUnexpectedExceptionCaught(ex);
  129. }
  130. #endregion
  131. }
  132. protected void ListControl_DataMember(Type ctrlType)
  133. {
  134. InitDataSet();
  135. #region "Existing Table"
  136. GHTListContorlSubTestBegin(ctrlType, "Existing table");
  137. try
  138. {
  139. m_lcToTest.DataSource = m_dsData;
  140. m_lcToTest.DataMember = "Second";
  141. m_lcToTest.DataTextField = "double Column";
  142. m_lcToTest.DataBind();
  143. Compare(m_lcToTest.DataMember, "Second");
  144. }
  145. catch (Exception ex)
  146. {
  147. GHTSubTestUnexpectedExceptionCaught(ex);
  148. }
  149. GHTSubTestEnd();
  150. #endregion
  151. #region "Null"
  152. GHTListContorlSubTestBegin(ctrlType, "Null");
  153. try
  154. {
  155. m_lcToTest.DataSource = m_dsData;
  156. m_lcToTest.DataMember = null;
  157. m_lcToTest.DataTextField = "char Column";
  158. m_lcToTest.DataBind();
  159. }
  160. catch (Exception ex)
  161. {
  162. GHTSubTestUnexpectedExceptionCaught(ex);
  163. }
  164. #endregion
  165. #region "Non existing table in a dataset"
  166. #if !NET_2_0
  167. GHTListContorlSubTestBegin(ctrlType, "Non existing table in a dataset");
  168. try
  169. {
  170. m_lcToTest.DataSource = m_dsData;
  171. m_lcToTest.DataMember = "not a table name";
  172. m_lcToTest.DataBind();
  173. GHTSubTestExpectedExceptionNotCaught("HttpException");
  174. }
  175. catch (HttpException ex)
  176. {
  177. GHTSubTestExpectedExceptionCaught(ex);
  178. }
  179. catch (Exception ex)
  180. {
  181. GHTSubTestUnexpectedExceptionCaught(ex);
  182. }
  183. #endif
  184. #endregion
  185. #region "DataSource is not a dataset - set"
  186. GHTListContorlSubTestBegin(ctrlType, "DataSource is not a dataset");
  187. try
  188. {
  189. InitArray();
  190. m_lcToTest.DataSource = m_items;
  191. m_lcToTest.DataMember = "not a table name";
  192. m_lcToTest.DataBind();
  193. }
  194. catch (Exception ex)
  195. {
  196. GHTSubTestUnexpectedExceptionCaught(ex);
  197. }
  198. #endregion
  199. }
  200. protected void ListControl_DataSource(Type ctrlType)
  201. {
  202. InitDataSet();
  203. InitArray();
  204. #region "DataSource that implements IEnumerable"
  205. GHTListContorlSubTestBegin(ctrlType, "DataSource that implements IEnumerable");
  206. try
  207. {
  208. IEnumerable dataSource = m_items;
  209. m_lcToTest.DataSource = dataSource;
  210. m_lcToTest.DataBind();
  211. }
  212. catch (Exception ex)
  213. {
  214. GHTSubTestUnexpectedExceptionCaught(ex);
  215. }
  216. GHTSubTestEnd();
  217. #endregion
  218. #region "DataSource that implements IListSource"
  219. GHTListContorlSubTestBegin(ctrlType, "DataSource that implements IListSource");
  220. try
  221. {
  222. IListSource dataSource = m_dsData;
  223. m_lcToTest.DataSource = dataSource;
  224. m_lcToTest.DataTextField = "char Column";
  225. m_lcToTest.DataBind();
  226. }
  227. catch (Exception ex)
  228. {
  229. GHTSubTestUnexpectedExceptionCaught(ex);
  230. }
  231. GHTSubTestEnd();
  232. #endregion
  233. #region "DataSource that does not implement IListSource or IEnumerable"
  234. GHTListContorlSubTestBegin(ctrlType, "DataSource that does not implement IListSource or IEnumerable");
  235. try
  236. {
  237. DataItem dataSource = new DataItem(1, "aaa");
  238. m_lcToTest.DataSource = dataSource;
  239. m_lcToTest.DataBind();
  240. GHTSubTestExpectedExceptionNotCaught("ArgumentException");
  241. }
  242. #if NET_2_0
  243. catch (InvalidOperationException ex)
  244. {
  245. GHTSubTestExpectedExceptionCaught(ex);
  246. }
  247. #endif
  248. catch (ArgumentException ex)
  249. {
  250. GHTSubTestExpectedExceptionCaught(ex);
  251. }
  252. catch (Exception ex)
  253. {
  254. GHTSubTestUnexpectedExceptionCaught(ex);
  255. }
  256. GHTSubTestEnd();
  257. #endregion
  258. }
  259. protected void ListControl_DataTextField(Type ctrlType)
  260. {
  261. InitArray();
  262. InitDataSet();
  263. #region "string.empty - user defined items"
  264. GHTListContorlSubTestBegin(ctrlType, "string.empty");
  265. try
  266. {
  267. m_lcToTest.DataSource = m_items;
  268. m_lcToTest.DataTextField = string.Empty;
  269. m_lcToTest.DataBind();
  270. }
  271. catch (Exception ex)
  272. {
  273. GHTSubTestUnexpectedExceptionCaught(ex);
  274. }
  275. GHTSubTestEnd();
  276. #endregion
  277. //The result is ToString of DataRow which is default Object.ToString()
  278. //In Java we get @addres at the end.
  279. // #region "string.empty - bound to a table"
  280. // GHTListContorlSubTestBegin(ctrlType, "string.empty - bound to a table");
  281. //
  282. // try
  283. // {
  284. // m_lcToTest.DataSource = m_dsData;
  285. // m_lcToTest.DataTextField = string.Empty;
  286. // m_lcToTest.DataBind();
  287. // }
  288. // catch (Exception ex)
  289. // {
  290. // GHTSubTestUnexpectedExceptionCaught(ex);
  291. // }
  292. //
  293. // GHTSubTestEnd();
  294. // #endregion
  295. #region "Name of an items property"
  296. GHTListContorlSubTestBegin(ctrlType, "Name of an items property");
  297. try
  298. {
  299. m_lcToTest.DataSource = m_items;
  300. m_lcToTest.DataTextField = "Name";
  301. m_lcToTest.DataBind();
  302. }
  303. catch (Exception ex)
  304. {
  305. GHTSubTestUnexpectedExceptionCaught(ex);
  306. }
  307. GHTSubTestEnd();
  308. #endregion
  309. #region "Name of an item non-existing property"
  310. GHTListContorlSubTestBegin(ctrlType, "Name of an item non-existing property");
  311. try
  312. {
  313. m_lcToTest.DataSource = m_items;
  314. m_lcToTest.DataTextField = "non-existing property";
  315. m_lcToTest.DataBind();
  316. GHTSubTestExpectedExceptionNotCaught("HttpException");
  317. }
  318. catch (HttpException ex)
  319. {
  320. GHTSubTestExpectedExceptionCaught(ex);
  321. }
  322. catch (Exception ex)
  323. {
  324. GHTSubTestUnexpectedExceptionCaught(ex);
  325. }
  326. GHTSubTestEnd();
  327. #endregion
  328. #region "Name of a column"
  329. GHTListContorlSubTestBegin(ctrlType, "Name of a column");
  330. try
  331. {
  332. m_lcToTest.DataSource = m_dsData;
  333. m_lcToTest.DataTextField = "int Column";
  334. m_lcToTest.DataBind();
  335. }
  336. catch (Exception ex)
  337. {
  338. GHTSubTestUnexpectedExceptionCaught(ex);
  339. }
  340. GHTSubTestEnd();
  341. #endregion
  342. #region "Name of a non-existing column"
  343. GHTListContorlSubTestBegin(ctrlType, "Name of a non-existing column");
  344. try
  345. {
  346. m_lcToTest.DataSource = m_dsData;
  347. m_lcToTest.DataTextField = "non-existing column";
  348. m_lcToTest.DataBind();
  349. GHTSubTestExpectedExceptionNotCaught("HttpException");
  350. }
  351. catch (HttpException ex)
  352. {
  353. GHTSubTestExpectedExceptionCaught(ex);
  354. }
  355. catch (Exception ex)
  356. {
  357. GHTSubTestUnexpectedExceptionCaught(ex);
  358. }
  359. GHTSubTestEnd();
  360. #endregion
  361. }
  362. protected void ListControl_DataTextFormatString(Type ctrlType)
  363. {
  364. InitArray();
  365. #region "string.empty"
  366. GHTListContorlSubTestBegin(ctrlType, "string.empty");
  367. try
  368. {
  369. m_lcToTest.DataSource = m_items;
  370. m_lcToTest.DataTextField = "Name";
  371. m_lcToTest.DataTextFormatString = string.Empty;
  372. m_lcToTest.DataBind();
  373. }
  374. catch (Exception ex)
  375. {
  376. GHTSubTestUnexpectedExceptionCaught(ex);
  377. }
  378. GHTSubTestEnd();
  379. #endregion
  380. #region "Valid format"
  381. GHTListContorlSubTestBegin(ctrlType, "Valid format");
  382. try
  383. {
  384. m_lcToTest.DataSource = m_items;
  385. m_lcToTest.DataTextField = "Name";
  386. m_lcToTest.DataTextFormatString = "format {0} format";
  387. m_lcToTest.DataBind();
  388. }
  389. catch (Exception ex)
  390. {
  391. GHTSubTestUnexpectedExceptionCaught(ex);
  392. }
  393. GHTSubTestEnd();
  394. #endregion
  395. #region "Invalid format"
  396. GHTListContorlSubTestBegin(ctrlType, "Invalid format");
  397. try
  398. {
  399. m_lcToTest.DataSource = m_items;
  400. m_lcToTest.DataTextField = "Name";
  401. m_lcToTest.DataTextFormatString = "{invalid format}";
  402. m_lcToTest.DataBind();
  403. GHTSubTestExpectedExceptionNotCaught("FormatException");
  404. }
  405. catch (FormatException ex)
  406. {
  407. GHTSubTestExpectedExceptionCaught(ex);
  408. }
  409. catch (Exception ex)
  410. {
  411. GHTSubTestUnexpectedExceptionCaught(ex);
  412. }
  413. GHTSubTestEnd();
  414. #endregion
  415. }
  416. protected void ListControl_Items(Type ctrlType)
  417. {
  418. InitArray();
  419. GHTListContorlSubTestBegin(ctrlType, "Type & contents");
  420. try
  421. {
  422. m_lcToTest.DataSource = m_items;
  423. m_lcToTest.DataTextField = "Name";
  424. m_lcToTest.DataValueField = "Id";
  425. m_lcToTest.DataBind();
  426. //Check the type:
  427. Compare(m_lcToTest.Items.GetType().ToString(), typeof(ListItemCollection).ToString());
  428. //Check all the items.
  429. for (int i=0; i<7; i++)
  430. {
  431. Compare(m_lcToTest.Items[i].Text, m_items[i].Name);
  432. Compare(m_lcToTest.Items[i].Value.ToString(), m_items[i].Id.ToString());
  433. }
  434. }
  435. catch (Exception ex)
  436. {
  437. GHTSubTestUnexpectedExceptionCaught(ex);
  438. }
  439. GHTSubTestEnd();
  440. }
  441. protected void ListControl_DataValueField(Type ctrlType)
  442. {
  443. InitArray();
  444. InitDataSet();
  445. #region "string.empty - user defined items"
  446. GHTListContorlSubTestBegin(ctrlType, "string.empty");
  447. try
  448. {
  449. m_lcToTest.DataSource = m_items;
  450. m_lcToTest.DataValueField = string.Empty;
  451. m_lcToTest.DataBind();
  452. }
  453. catch (Exception ex)
  454. {
  455. GHTSubTestUnexpectedExceptionCaught(ex);
  456. }
  457. GHTSubTestEnd();
  458. #endregion
  459. #region "string.empty - bound to a table"
  460. GHTListContorlSubTestBegin(ctrlType, "string.empty - bound to a table");
  461. try
  462. {
  463. m_lcToTest.DataSource = m_dsData;
  464. m_lcToTest.DataValueField= string.Empty;
  465. m_lcToTest.DataTextField = "char Column";
  466. m_lcToTest.DataBind();
  467. }
  468. catch (Exception ex)
  469. {
  470. GHTSubTestUnexpectedExceptionCaught(ex);
  471. }
  472. GHTSubTestEnd();
  473. #endregion
  474. #region "Name of an items property"
  475. GHTListContorlSubTestBegin(ctrlType, "Name of an items property");
  476. try
  477. {
  478. m_lcToTest.DataSource = m_items;
  479. m_lcToTest.DataValueField = "Id";
  480. m_lcToTest.DataBind();
  481. }
  482. catch (Exception ex)
  483. {
  484. GHTSubTestUnexpectedExceptionCaught(ex);
  485. }
  486. GHTSubTestEnd();
  487. #endregion
  488. #region "Name of an item non-existing property"
  489. GHTListContorlSubTestBegin(ctrlType, "Name of an item non-existing property");
  490. try
  491. {
  492. m_lcToTest.DataSource = m_items;
  493. m_lcToTest.DataValueField = "non-existing property";
  494. m_lcToTest.DataBind();
  495. GHTSubTestExpectedExceptionNotCaught("HttpException");
  496. }
  497. catch (HttpException ex)
  498. {
  499. GHTSubTestExpectedExceptionCaught(ex);
  500. }
  501. catch (Exception ex)
  502. {
  503. GHTSubTestUnexpectedExceptionCaught(ex);
  504. }
  505. GHTSubTestEnd();
  506. #endregion
  507. #region "Name of a column"
  508. GHTListContorlSubTestBegin(ctrlType, "Name of a column");
  509. try
  510. {
  511. m_lcToTest.DataSource = m_dsData;
  512. m_lcToTest.DataValueField = "int Column";
  513. m_lcToTest.DataBind();
  514. }
  515. catch (Exception ex)
  516. {
  517. GHTSubTestUnexpectedExceptionCaught(ex);
  518. }
  519. GHTSubTestEnd();
  520. #endregion
  521. #region "Name of a non-existing column"
  522. GHTListContorlSubTestBegin(ctrlType, "Name of a non-existing column");
  523. try
  524. {
  525. m_lcToTest.DataSource = m_dsData;
  526. m_lcToTest.DataValueField = "non-existing column";
  527. m_lcToTest.DataBind();
  528. GHTSubTestExpectedExceptionNotCaught("HttpException");
  529. }
  530. catch (HttpException ex)
  531. {
  532. GHTSubTestExpectedExceptionCaught(ex);
  533. }
  534. catch (Exception ex)
  535. {
  536. GHTSubTestUnexpectedExceptionCaught(ex);
  537. }
  538. GHTSubTestEnd();
  539. #endregion
  540. }
  541. protected void ListControl_SelectedIndex(Type ctrlType)
  542. {
  543. InitArray();
  544. #region "None selected"
  545. GHTListContorlSubTestBegin(ctrlType, "None selected");
  546. try
  547. {
  548. m_lcToTest.DataSource = m_items;
  549. m_lcToTest.DataBind();
  550. m_lcToTest.SelectedIndex = -1;
  551. GHTSubTestAddResult(m_lcToTest.SelectedIndex.ToString());
  552. }
  553. catch (Exception ex)
  554. {
  555. GHTSubTestUnexpectedExceptionCaught(ex);
  556. }
  557. #endregion
  558. #region "valid value"
  559. GHTListContorlSubTestBegin(ctrlType, "valid value");
  560. try
  561. {
  562. m_lcToTest.DataSource = m_items;
  563. m_lcToTest.DataBind();
  564. m_lcToTest.SelectedIndex = 5;
  565. GHTSubTestAddResult(m_lcToTest.SelectedIndex.ToString());
  566. }
  567. catch (Exception ex)
  568. {
  569. GHTSubTestUnexpectedExceptionCaught(ex);
  570. }
  571. #endregion
  572. #region "Invalid value - above length of items."
  573. GHTListContorlSubTestBegin(ctrlType, "Invalid value - above length of items.");
  574. try
  575. {
  576. m_lcToTest.DataSource = m_items;
  577. m_lcToTest.DataBind();
  578. m_lcToTest.SelectedIndex = 10;
  579. GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
  580. }
  581. catch (ArgumentOutOfRangeException ex)
  582. {
  583. GHTSubTestExpectedExceptionCaught(ex);
  584. }
  585. catch (Exception ex)
  586. {
  587. GHTSubTestUnexpectedExceptionCaught(ex);
  588. }
  589. #endregion
  590. #region "Invalid value - below -1."
  591. GHTListContorlSubTestBegin(ctrlType, "Invalid value - below -1.");
  592. try
  593. {
  594. m_lcToTest.DataSource = m_items;
  595. m_lcToTest.DataBind();
  596. m_lcToTest.SelectedIndex = -2;
  597. GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
  598. }
  599. catch (ArgumentOutOfRangeException ex)
  600. {
  601. GHTSubTestExpectedExceptionCaught(ex);
  602. }
  603. catch (Exception ex)
  604. {
  605. GHTSubTestUnexpectedExceptionCaught(ex);
  606. }
  607. #endregion
  608. }
  609. protected void ListControl_SelectedItem(Type ctrlType)
  610. {
  611. InitArray();
  612. #region "None selected"
  613. GHTListContorlSubTestBegin(ctrlType, "None selected");
  614. try
  615. {
  616. m_lcToTest.DataSource = m_items;
  617. m_lcToTest.DataTextField = "Name";
  618. m_lcToTest.DataBind();
  619. m_lcToTest.SelectedIndex = -1;
  620. if (m_lcToTest.SelectedItem == null)
  621. {
  622. GHTSubTestAddResult("Test passed: SelectedItem is null");
  623. }
  624. else
  625. {
  626. GHTSubTestAddResult("Test failede: SelectedItem is not null");
  627. }
  628. }
  629. catch (Exception ex)
  630. {
  631. GHTSubTestUnexpectedExceptionCaught(ex);
  632. }
  633. #endregion
  634. #region "Single Item selected"
  635. GHTListContorlSubTestBegin(ctrlType, "valid value");
  636. try
  637. {
  638. m_lcToTest.DataSource = m_items;
  639. m_lcToTest.DataTextField = "Name";
  640. m_lcToTest.DataBind();
  641. m_lcToTest.SelectedIndex = 5;
  642. Compare(m_lcToTest.SelectedItem.Text, m_items[5].Name );
  643. }
  644. catch (Exception ex)
  645. {
  646. GHTSubTestUnexpectedExceptionCaught(ex);
  647. }
  648. #endregion
  649. #region "multiple Items selected"
  650. GHTListContorlSubTestBegin(ctrlType, "multiple Items selected");
  651. if (m_lcToTest is DropDownList)
  652. {
  653. return;
  654. }
  655. if (m_lcToTest is ListBox)
  656. {
  657. ((ListBox)m_lcToTest).SelectionMode = ListSelectionMode.Multiple;
  658. }
  659. try
  660. {
  661. m_lcToTest.DataSource = m_items;
  662. m_lcToTest.DataTextField = "Name";
  663. m_lcToTest.DataBind();
  664. m_lcToTest.Items[2].Selected = true;
  665. m_lcToTest.Items[4].Selected = true;
  666. m_lcToTest.Items[6].Selected = true;
  667. Compare(m_lcToTest.SelectedItem.Text, m_items[2].Name );
  668. }
  669. catch (Exception ex)
  670. {
  671. GHTSubTestUnexpectedExceptionCaught(ex);
  672. }
  673. #endregion
  674. }
  675. protected void ListControl_SelectedValue(Type ctrlType)
  676. {
  677. InitArray();
  678. #region "None selected - get"
  679. GHTListContorlSubTestBegin(ctrlType, "None selected - get");
  680. try
  681. {
  682. m_lcToTest.DataSource = m_items;
  683. m_lcToTest.DataValueField = "Id";
  684. m_lcToTest.DataBind();
  685. m_lcToTest.SelectedIndex = -1;
  686. Compare(m_lcToTest.SelectedValue, string.Empty);
  687. }
  688. catch (Exception ex)
  689. {
  690. GHTSubTestUnexpectedExceptionCaught(ex);
  691. }
  692. #endregion
  693. #region "valid value - get"
  694. GHTListContorlSubTestBegin(ctrlType, "valid value - get");
  695. try
  696. {
  697. m_lcToTest.DataSource = m_items;
  698. m_lcToTest.DataValueField = "Id";
  699. m_lcToTest.DataBind();
  700. m_lcToTest.SelectedIndex = 5;
  701. Compare(m_lcToTest.SelectedValue, m_items[5].Id.ToString());
  702. }
  703. catch (Exception ex)
  704. {
  705. GHTSubTestUnexpectedExceptionCaught(ex);
  706. }
  707. #endregion
  708. #region "valid value - set"
  709. GHTListContorlSubTestBegin(ctrlType, "valid value - set");
  710. try
  711. {
  712. m_lcToTest.DataSource = m_items;
  713. m_lcToTest.DataValueField = "Id";
  714. m_lcToTest.DataBind();
  715. m_lcToTest.SelectedValue = "5";
  716. }
  717. catch (Exception ex)
  718. {
  719. GHTSubTestUnexpectedExceptionCaught(ex);
  720. }
  721. #endregion
  722. #region "Invalid value - set"
  723. GHTListContorlSubTestBegin(ctrlType, "Invalid value - set");
  724. try
  725. {
  726. m_lcToTest.DataSource = m_items;
  727. m_lcToTest.DataBind();
  728. m_lcToTest.DataValueField = "Id";
  729. m_lcToTest.SelectedValue = "10";
  730. GHTSubTestExpectedExceptionNotCaught("ArgumentOutOfRangeException");
  731. }
  732. catch (ArgumentOutOfRangeException ex)
  733. {
  734. GHTSubTestExpectedExceptionCaught(ex);
  735. }
  736. catch (Exception ex)
  737. {
  738. GHTSubTestUnexpectedExceptionCaught(ex);
  739. }
  740. #endregion
  741. }
  742. #endregion
  743. #region "Construction"
  744. /// <summary>
  745. /// Default constructor.
  746. /// </summary>
  747. public GHTListControlBase()
  748. {
  749. }
  750. /// <summary>
  751. /// Static constructor.
  752. /// Initializes the static field m_types[].
  753. /// </summary>
  754. static GHTListControlBase()
  755. {
  756. initTypes();
  757. }
  758. #endregion
  759. #region "properties"
  760. public static Type[] TestedTypes
  761. {
  762. get
  763. {
  764. return (Type[])(m_types.ToArray(typeof(Type)));
  765. }
  766. }
  767. #endregion
  768. #region "members"
  769. /// <summary>
  770. /// Holds the ListControl that is tested in the current subtest.
  771. /// </summary>
  772. private ListControl m_lcToTest;
  773. private int m_controlsCounter = 0;
  774. /// <summary>
  775. /// Holds all the types that are derived from ListControl, and should be tested.
  776. /// </summary>
  777. private static readonly ArrayList m_types = new ArrayList();
  778. /// <summary>
  779. /// Two data set to use while testing.
  780. /// Initialize this dataset using InitDataSet()
  781. /// </summary>
  782. private DataSet m_dsData;
  783. private DataTable m_dtFirst;
  784. private DataTable m_dtSecond;
  785. /// <summary>
  786. /// An array of Item objects that can be used as a datasource.
  787. /// initialize the array using InitArray()
  788. /// </summary>
  789. private DataItem[] m_items;
  790. #endregion
  791. #region "Private methods"
  792. /// <summary>
  793. /// Initializes the collection of types derived from ListControl.
  794. /// </summary>
  795. private static void initTypes()
  796. {
  797. m_types.Add(typeof(ListBox));
  798. m_types.Add(typeof(DropDownList));
  799. m_types.Add(typeof(RadioButtonList));
  800. m_types.Add(typeof(CheckBoxList));
  801. }
  802. /// <summary>
  803. /// Creates new sub test and adds a new ListControl to it.
  804. /// </summary>
  805. /// <param name="ctrlType">Actual type of the tested control</param>
  806. /// <param name="description">subtests description</param>
  807. private void GHTListContorlSubTestBegin(Type ctrlType, string description)
  808. {
  809. m_lcToTest = (ListControl)GHTElementClone(ctrlType);
  810. m_lcToTest.ID = "_ctrl" + m_controlsCounter;
  811. m_controlsCounter++;
  812. GHTSubTestBegin(description);
  813. GHTActiveSubTest.Controls.Add(m_lcToTest);
  814. }
  815. /// <summary>
  816. /// Initializes both m_dtFirst, and m_dtSecond with names ("First", "Second"), columns, and data.
  817. /// </summary>
  818. private void InitDataSet()
  819. {
  820. m_dtFirst = new DataTable("First");
  821. m_dtFirst.Columns.Add("int Column", typeof(int));
  822. m_dtFirst.Columns.Add("bool Column", typeof(bool));
  823. m_dtFirst.Columns.Add("char Column", typeof(char));
  824. m_dtSecond = new DataTable("Second");
  825. m_dtSecond.Columns.Add("double Column", typeof(int));
  826. m_dtSecond.Columns.Add("byte Column", typeof(bool));
  827. m_dtSecond.Columns.Add("time Column", typeof(DateTime));
  828. for (int i=0; i<10; i++)
  829. {
  830. DataRow dr1 = m_dtFirst.NewRow();
  831. dr1["int Column"] = i;
  832. dr1["bool Column"] = ( i % 2 == 0 ) ? true : false;
  833. dr1["char Column"] = (char)(i + 'a');
  834. m_dtFirst.Rows.Add(dr1);
  835. DataRow dr2 = m_dtSecond.NewRow();
  836. dr2["double Column"] = double.Epsilon * i;
  837. dr2["byte Column"] = (byte)( i % 10);
  838. dr2["time Column"] = DateTime.Now;
  839. m_dtSecond.Rows.Add(dr2);
  840. }
  841. m_dsData = new DataSet("Test dataset");
  842. m_dsData.Tables.Add(m_dtFirst);
  843. m_dsData.Tables.Add(m_dtSecond);
  844. }
  845. private void InitArray()
  846. {
  847. m_items = new DataItem[] { new DataItem(1, "aaa"),
  848. new DataItem(2, "bbb"),
  849. new DataItem(3, "ccc"),
  850. new DataItem(4, "ddd"),
  851. new DataItem(5, "eee"),
  852. new DataItem(6, "fff"),
  853. new DataItem(7, "ggg")};
  854. }
  855. /// <summary>
  856. /// Nested class, to use as the items of the m_items array.
  857. /// </summary>
  858. private class DataItem
  859. {
  860. public DataItem(int a_id, string a_name)
  861. {
  862. id = a_id;
  863. name = a_name;
  864. }
  865. private int id;
  866. private string name;
  867. public int Id
  868. {
  869. get
  870. {
  871. return id;
  872. }
  873. set
  874. {
  875. id = value;
  876. }
  877. }
  878. public string Name
  879. {
  880. get
  881. {
  882. return name;
  883. }
  884. set
  885. {
  886. name = value;
  887. }
  888. }
  889. public override string ToString()
  890. {
  891. return id.ToString() + name;
  892. }
  893. }
  894. }
  895. #endregion
  896. }