GHTListControlBase.cs 23 KB

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