ListControlTest.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. //
  2. // Tests for System.Web.UI.WebControls.ListBoxTest.cs
  3. //
  4. // Author:
  5. // Jackson Harper ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  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 NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Collections;
  33. using System.Globalization;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.Web.UI.WebControls;
  37. #if NET_2_0
  38. using MonoTests.stand_alone.WebHarness;
  39. using MonoTests.SystemWeb.Framework;
  40. #endif
  41. namespace MonoTests.System.Web.UI.WebControls
  42. {
  43. public class ListControlPoker : ListControl {
  44. public ListControlPoker ()
  45. {
  46. }
  47. public void TrackState ()
  48. {
  49. TrackViewState ();
  50. }
  51. public object SaveState ()
  52. {
  53. return SaveViewState ();
  54. }
  55. public void LoadState (object state)
  56. {
  57. LoadViewState (state);
  58. }
  59. public object ViewStateValue (string name)
  60. {
  61. return ViewState [name];
  62. }
  63. public string Render ()
  64. {
  65. StringWriter sw = new StringWriter ();
  66. sw.NewLine = "\n";
  67. HtmlTextWriter writer = new HtmlTextWriter (sw);
  68. base.Render (writer);
  69. return writer.InnerWriter.ToString ();
  70. }
  71. #if NET_2_0
  72. public HtmlTextWriterTag GetTagKey ()
  73. {
  74. return TagKey;
  75. }
  76. public new void OnSelectedIndexChanged (EventArgs e)
  77. {
  78. base.OnSelectedIndexChanged (e);
  79. }
  80. public new void OnTextChanged (EventArgs e)
  81. {
  82. base.OnTextChanged (e);
  83. }
  84. public new void VerifyMultiSelect ()
  85. {
  86. base.VerifyMultiSelect ();
  87. }
  88. #endif
  89. }
  90. [TestFixture]
  91. public class ListControlTest
  92. {
  93. #region help_class_for_addattributetorender
  94. class Poker : ListControl
  95. {
  96. public void TrackState ()
  97. {
  98. TrackViewState ();
  99. }
  100. public object SaveState ()
  101. {
  102. return SaveViewState ();
  103. }
  104. public void LoadState (object state)
  105. {
  106. LoadViewState (state);
  107. }
  108. public object ViewStateValue (string name)
  109. {
  110. return ViewState[name];
  111. }
  112. public string Render ()
  113. {
  114. StringWriter sw = new StringWriter ();
  115. sw.NewLine = "\n";
  116. HtmlTextWriter writer = new HtmlTextWriter (sw);
  117. base.Render (writer);
  118. return writer.InnerWriter.ToString ();
  119. }
  120. #if NET_2_0
  121. ArrayList Databound ()
  122. {
  123. ArrayList list = new ArrayList ();
  124. list.Add (1);
  125. list.Add (2);
  126. list.Add (3);
  127. return list;
  128. }
  129. public HtmlTextWriterTag GetTagKey ()
  130. {
  131. return TagKey;
  132. }
  133. protected override void AddAttributesToRender (HtmlTextWriter writer)
  134. {
  135. writer.AddAttribute (HtmlTextWriterAttribute.Type, "MyType");
  136. writer.AddAttribute (HtmlTextWriterAttribute.Name, "MyName");
  137. writer.AddAttribute (HtmlTextWriterAttribute.Value, "MyValue");
  138. base.AddAttributesToRender (writer);
  139. }
  140. protected override void PerformDataBinding (IEnumerable dataSource)
  141. {
  142. base.PerformDataBinding (Databound());
  143. }
  144. protected override void PerformSelect ()
  145. {
  146. base.PerformSelect ();
  147. SelectedIndex = 0;
  148. }
  149. public new void SetPostDataSelection(int selectedItem)
  150. {
  151. base.SetPostDataSelection(selectedItem);
  152. }
  153. #endif
  154. }
  155. #endregion
  156. private Hashtable changed = new Hashtable ();
  157. #if NET_2_0
  158. [TestFixtureSetUp]
  159. public void SetUp ()
  160. {
  161. #if VISUAL_STUDIO
  162. WebTest.CopyResource (GetType (), "MonoTests.System.Web.UI.WebControls.Resources.ListControlPage.aspx", "ListControlPage.aspx");
  163. #else
  164. WebTest.CopyResource (GetType (), "ListControlPage.aspx", "ListControlPage.aspx");
  165. #endif
  166. }
  167. #endif
  168. [Test]
  169. public void DefaultProperties ()
  170. {
  171. ListControlPoker p = new ListControlPoker ();
  172. Assert.AreEqual (p.AutoPostBack, false, "A1");
  173. Assert.AreEqual (p.DataMember, String.Empty, "A2");
  174. Assert.AreEqual (p.DataSource, null, "A3");
  175. Assert.AreEqual (p.DataTextField, String.Empty, "A4");
  176. Assert.AreEqual (p.DataTextFormatString, String.Empty, "A5");
  177. Assert.AreEqual (p.DataValueField, String.Empty, "A6");
  178. Assert.AreEqual (p.Items.Count, 0, "A7");
  179. Assert.AreEqual (p.SelectedIndex, -1,"A8");
  180. Assert.AreEqual (p.SelectedItem, null, "A9");
  181. Assert.AreEqual (p.SelectedValue, String.Empty, "A10");
  182. #if NET_2_0
  183. Assert.IsFalse (p.AppendDataBoundItems, "A11");
  184. Assert.AreEqual (p.Text, "", "A12");
  185. Assert.AreEqual (p.GetTagKey(), HtmlTextWriterTag.Select, "A13");
  186. Assert.AreEqual (false, p.AppendDataBoundItems, "A14");
  187. Assert.AreEqual (false, p.CausesValidation, "A15");
  188. #endif
  189. }
  190. #if NET_2_0
  191. [Test]
  192. public void AddAttributesToRender ()
  193. {
  194. Poker p = new Poker ();
  195. ListItem foo = new ListItem ("foo");
  196. ListItem bar = new ListItem ("bar");
  197. p.Items.Add (foo);
  198. p.Items.Add (bar);
  199. #region orig
  200. string orig = "<select type=\"MyType\" name=\"MyName\" value=\"MyValue\"><option value=\"foo\">foo</option><option value=\"bar\">bar</option></select>";
  201. #endregion
  202. string html = p.Render ();
  203. HtmlDiff.AssertAreEqual(orig,html,"AddAttributesToRender failed");
  204. }
  205. [Test]
  206. public void AppendDataBoundItems ()
  207. {
  208. ListControlPoker p = new ListControlPoker ();
  209. ListItem foo = new ListItem ("foo");
  210. ListItem bar = new ListItem ("bar");
  211. p.Items.Add (foo);
  212. p.Items.Add (bar);
  213. Assert.AreEqual (2, p.Items.Count, "Before databound");
  214. p.AppendDataBoundItems = true; // Not to clear list before databind
  215. p.DataSource = Databound ();
  216. p.DataBind ();
  217. Assert.AreEqual (5, p.Items.Count, "After databound");
  218. p.AppendDataBoundItems = false; // To clear list before databind
  219. p.DataBind ();
  220. Assert.AreEqual (3, p.Items.Count, "Clear list and databound");
  221. }
  222. ArrayList Databound ()
  223. {
  224. ArrayList list = new ArrayList ();
  225. list.Add (1);
  226. list.Add (2);
  227. list.Add (3);
  228. return list;
  229. }
  230. [Test]
  231. [Category ("NunitWeb")]
  232. public void CausesValidation_ValidationGroup ()
  233. {
  234. WebTest t = new WebTest ("ListControlPage.aspx");
  235. string str = t.Run ();
  236. FormRequest fr = new FormRequest (t.Response, "form1");
  237. fr.Controls.Add ("__EVENTTARGET");
  238. fr.Controls.Add ("__EVENTARGUMENT");
  239. fr.Controls.Add ("ListBox1");
  240. fr.Controls["__EVENTTARGET"].Value = "ListBox1";
  241. fr.Controls["__EVENTARGUMENT"].Value = "";
  242. fr.Controls["ListBox1"].Value = "2";
  243. t.Request = fr;
  244. string html = t.Run ();
  245. if (html.IndexOf ("Validate_validation_group") == -1)
  246. Assert.Fail ("Validate not created");
  247. if (html.IndexOf ("MyValidationGroup") == -1)
  248. Assert.Fail ("Wrong validation group");
  249. }
  250. [Test]
  251. public void OnTextChanged ()
  252. {
  253. ListControlPoker p = new ListControlPoker ();
  254. p.TextChanged += new EventHandler (p_TextChanged);
  255. Assert.AreEqual (false, eventTextChanged, "Before");
  256. p.OnTextChanged (new EventArgs ());
  257. Assert.AreEqual (true, eventTextChanged, "After");
  258. }
  259. bool eventTextChanged;
  260. void p_TextChanged (object sender, EventArgs e)
  261. {
  262. eventTextChanged = true;
  263. }
  264. [Test]
  265. public void PerformDataBind_PerformSelect ()
  266. {
  267. Poker p = new Poker ();
  268. p.DataBind ();
  269. string html = p.Render ();
  270. #region #1
  271. string orig = @"<select type=""MyType"" name=""MyName"" value=""MyValue"">
  272. <option selected=""selected"" value=""1"">1</option>
  273. <option value=""2"">2</option>
  274. <option value=""3"">3</option>
  275. </select>";
  276. #endregion
  277. HtmlDiff.AssertAreEqual (orig, html, "PerformDataBind");
  278. }
  279. [Test]
  280. [Category("NotWorking")] //Not implemented
  281. public void SetPostDataSelection ()
  282. {
  283. Poker p = new Poker ();
  284. ListItem foo = new ListItem ("foo");
  285. ListItem bar = new ListItem ("bar");
  286. p.Items.Add (foo);
  287. p.Items.Add (bar);
  288. p.SetPostDataSelection (1);
  289. Assert.AreEqual (1, p.SelectedIndex, "SetPostDataSelection");
  290. }
  291. [Test]
  292. public void Text ()
  293. {
  294. Poker p = new Poker ();
  295. ListItem foo = new ListItem ("foo");
  296. ListItem bar = new ListItem ("bar");
  297. p.Items.Add (foo);
  298. p.Items.Add (bar);
  299. Assert.AreEqual (string.Empty, p.Text, "Text#1");
  300. p.SelectedIndex = 1;
  301. Assert.AreEqual ("bar", p.Text, "Text#2");
  302. }
  303. #region HelpListForMultiple
  304. class PokerL : ListBox
  305. {
  306. public PokerL ()
  307. {
  308. InitializeMyListBox ();
  309. }
  310. private void InitializeMyListBox ()
  311. {
  312. // Add items to the ListBox.
  313. Items.Add ("A");
  314. Items.Add ("C");
  315. Items.Add ("E");
  316. Items.Add ("F");
  317. Items.Add ("G");
  318. Items.Add ("D");
  319. Items.Add ("B");
  320. // Set the SelectionMode to select multiple items.
  321. SelectionMode = ListSelectionMode.Multiple;
  322. Items[0].Selected = true;
  323. Items[2].Selected = true;
  324. }
  325. public void TrackState ()
  326. {
  327. TrackViewState ();
  328. }
  329. public object SaveState ()
  330. {
  331. return SaveViewState ();
  332. }
  333. public void LoadState (object state)
  334. {
  335. LoadViewState (state);
  336. }
  337. public object ViewStateValue (string name)
  338. {
  339. return ViewState[name];
  340. }
  341. public string Render ()
  342. {
  343. StringWriter sw = new StringWriter ();
  344. sw.NewLine = "\n";
  345. HtmlTextWriter writer = new HtmlTextWriter (sw);
  346. base.Render (writer);
  347. return writer.InnerWriter.ToString ();
  348. }
  349. }
  350. #endregion
  351. [Test]
  352. public void Multiple ()
  353. {
  354. PokerL p = new PokerL ();
  355. Assert.AreEqual (true, p.Items[0].Selected, "MultipleSelect#1");
  356. Assert.AreEqual (true, p.Items[2].Selected, "MultipleSelect#2");
  357. string html = p.Render ();
  358. #region origin
  359. string orig = @"<select size=""4"" multiple=""multiple"">
  360. <option selected=""selected"" value=""A"">A</option>
  361. <option value=""C"">C</option>
  362. <option selected=""selected"" value=""E"">E</option>
  363. <option value=""F"">F</option>
  364. <option value=""G"">G</option>
  365. <option value=""D"">D</option>
  366. <option value=""B"">B</option>
  367. </select>";
  368. #endregion
  369. HtmlDiff.AssertAreEqual (orig, html, "MultipleSelect#3");
  370. }
  371. #endif
  372. [Test]
  373. public void CleanProperties ()
  374. {
  375. ListControlPoker p = new ListControlPoker ();
  376. p.AutoPostBack = true;
  377. Assert.AreEqual (p.AutoPostBack, true, "A2");
  378. p.DataMember = "DataMember";
  379. Assert.AreEqual (p.DataMember, "DataMember", "A3");
  380. p.DataSource = "DataSource";
  381. Assert.AreEqual (p.DataSource, "DataSource", "A4");
  382. p.DataTextField = "DataTextField";
  383. Assert.AreEqual (p.DataTextField, "DataTextField", "A5");
  384. p.DataTextFormatString = "DataTextFormatString";
  385. Assert.AreEqual (p.DataTextFormatString, "DataTextFormatString", "A6");
  386. p.DataValueField = "DataValueField";
  387. Assert.AreEqual (p.DataValueField, "DataValueField", "A7");
  388. p.SelectedIndex = 10;
  389. Assert.AreEqual (p.SelectedIndex, -1, "A8");
  390. p.SelectedValue = "SelectedValue";
  391. Assert.AreEqual (p.SelectedValue, String.Empty, "A9");
  392. }
  393. [Test]
  394. public void NullProperties ()
  395. {
  396. ListControlPoker p = new ListControlPoker ();
  397. p.DataMember = null;
  398. Assert.AreEqual (p.DataMember, String.Empty, "A1");
  399. p.DataSource = null;
  400. Assert.AreEqual (p.DataSource, null, "A2");
  401. p.DataTextField = null;
  402. Assert.AreEqual (p.DataTextField, String.Empty, "A3");
  403. p.DataTextFormatString = null;
  404. Assert.AreEqual (p.DataTextFormatString, String.Empty, "A4");
  405. p.DataValueField = null;
  406. Assert.AreEqual (p.DataValueField, String.Empty, "A5");
  407. p.SelectedValue = null;
  408. Assert.AreEqual (p.SelectedValue, String.Empty, "A6");
  409. }
  410. [Test]
  411. public void ClearSelection ()
  412. {
  413. ListControlPoker p = new ListControlPoker ();
  414. ListItem foo = new ListItem ("foo");
  415. ListItem bar = new ListItem ("bar");
  416. BeginIndexChanged (p);
  417. p.Items.Add (foo);
  418. p.Items.Add (bar);
  419. p.SelectedIndex = 1;
  420. // sanity for the real test
  421. Assert.AreEqual (p.Items.Count, 2, "A1");
  422. Assert.AreEqual (p.SelectedIndex, 1, "A2");
  423. Assert.AreEqual (p.SelectedItem, bar, "A3");
  424. Assert.AreEqual (p.SelectedValue, bar.Value, "A4");
  425. p.ClearSelection ();
  426. Assert.AreEqual (p.SelectedIndex, -1, "A5");
  427. Assert.AreEqual (p.SelectedItem, null, "A6");
  428. Assert.AreEqual (p.SelectedValue, String.Empty, "A7");
  429. Assert.IsFalse (EndIndexChanged (p), "A8");
  430. // make sure we are still sane
  431. Assert.AreEqual (p.Items.Count, 2, "A9");
  432. }
  433. #if NET_2_0
  434. [Test]
  435. // Tests Save/Load ControlState
  436. public void ControlState ()
  437. {
  438. ListControlPoker a = new ListControlPoker ();
  439. ListControlPoker b = new ListControlPoker ();
  440. a.TrackState ();
  441. a.Items.Add ("a");
  442. a.Items.Add ("b");
  443. a.Items.Add ("c");
  444. a.SelectedIndex = 2;
  445. b.Items.Add ("a");
  446. b.Items.Add ("b");
  447. b.Items.Add ("c");
  448. Assert.AreEqual (-1, b.SelectedIndex, "A1");
  449. }
  450. #endif
  451. [Test]
  452. // Tests Save/Load/Track ViewState
  453. public void ViewState ()
  454. {
  455. ListControlPoker a = new ListControlPoker ();
  456. ListControlPoker b = new ListControlPoker ();
  457. a.TrackState ();
  458. BeginIndexChanged (a);
  459. BeginIndexChanged (b);
  460. a.Items.Add ("a");
  461. a.Items.Add ("b");
  462. a.Items.Add ("c");
  463. a.SelectedIndex = 2;
  464. object state = a.SaveState ();
  465. b.LoadState (state);
  466. Assert.AreEqual (2, b.SelectedIndex, "A1");
  467. Assert.AreEqual (b.Items.Count, 3, "A2");
  468. Assert.AreEqual (b.Items [0].Value, "a", "A3");
  469. Assert.AreEqual (b.Items [1].Value, "b", "A4");
  470. Assert.AreEqual (b.Items [2].Value, "c", "A5");
  471. Assert.IsFalse (EndIndexChanged (a), "A6");
  472. Assert.IsFalse (EndIndexChanged (b), "A7");
  473. }
  474. [Test]
  475. // Tests Save/Load/Track ViewState
  476. public void ViewStateNotNeeded ()
  477. {
  478. ListControlPoker a = new ListControlPoker ();
  479. a.Items.Add ("a");
  480. a.Items.Add ("b");
  481. a.Items.Add ("c");
  482. object state = a.SaveState ();
  483. Assert.AreEqual (null, state, "A1");
  484. }
  485. [Test]
  486. public void ViewStateContents ()
  487. {
  488. ListControlPoker p = new ListControlPoker ();
  489. p.TrackState ();
  490. // So the selected index can be set
  491. p.Items.Add ("one");
  492. p.Items.Add ("two");
  493. p.AutoPostBack = false;
  494. p.DataMember = "DataMember";
  495. p.DataSource = "DataSource";
  496. p.DataTextField = "DataTextField";
  497. p.DataTextFormatString = "DataTextFormatString";
  498. p.DataValueField = "DataValueField";
  499. p.SelectedIndex = 1;
  500. #if NET_2_0
  501. p.AppendDataBoundItems = true;
  502. p.Text = "Text";
  503. #endif
  504. Assert.AreEqual (p.ViewStateValue ("AutoPostBack"), false, "A1");
  505. Assert.AreEqual (p.ViewStateValue ("DataMember"), "DataMember", "A2");
  506. Assert.AreEqual (p.ViewStateValue ("DataSource"), null, "A3");
  507. Assert.AreEqual (p.ViewStateValue ("DataTextField"), "DataTextField", "A4");
  508. Assert.AreEqual (p.ViewStateValue ("DataTextFormatString"),
  509. "DataTextFormatString", "A5");
  510. Assert.AreEqual (p.ViewStateValue ("DataValueField"), "DataValueField", "A6");
  511. #if NET_2_0
  512. Assert.AreEqual (p.ViewStateValue ("AppendDataBoundItems"), true, "A7");
  513. #endif
  514. // None of these are saved
  515. Assert.AreEqual (p.ViewStateValue ("SelectedIndex"), null, "A8");
  516. Assert.AreEqual (p.ViewStateValue ("SelectedItem"), null, "A9");
  517. Assert.AreEqual (p.ViewStateValue ("SelectedValue"), null, "A10");
  518. #if NET_2_0
  519. Assert.AreEqual (p.ViewStateValue ("Text"), null, "A11");
  520. #endif
  521. }
  522. [Test]
  523. public void SelectedIndex ()
  524. {
  525. ListControlPoker p = new ListControlPoker ();
  526. p.Items.Add ("one");
  527. p.Items.Add ("two");
  528. p.Items.Add ("three");
  529. p.Items.Add ("four");
  530. p.Items [2].Selected = true;
  531. p.Items [1].Selected = true;
  532. Assert.AreEqual (p.SelectedIndex, 1, "A1");
  533. p.ClearSelection ();
  534. p.Items [3].Selected = true;
  535. Assert.AreEqual (p.SelectedIndex, 3, "A2");
  536. p.SelectedIndex = 1;
  537. Assert.AreEqual (p.SelectedIndex, 1, "A3");
  538. Assert.IsFalse (p.Items [3].Selected, "A4");
  539. }
  540. [Test]
  541. public void Render ()
  542. {
  543. ListControlPoker p = new ListControlPoker ();
  544. string s = p.Render ();
  545. string expected = "<select>\n\n</select>";
  546. Assert.AreEqual (s, expected, "A1");
  547. }
  548. [Test]
  549. #if !NET_2_0
  550. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  551. #endif
  552. public void ItemsTooHigh ()
  553. {
  554. ListControlPoker l = new ListControlPoker ();
  555. l.Items.Add ("foo");
  556. l.SelectedIndex = 1;
  557. #if NET_2_0
  558. Assert.AreEqual (-1, l.SelectedIndex, "#01");
  559. #endif
  560. }
  561. [Test]
  562. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  563. public void ItemsTooLow ()
  564. {
  565. ListControlPoker l = new ListControlPoker ();
  566. l.Items.Add ("foo");
  567. l.SelectedIndex = -2;
  568. }
  569. [Test]
  570. public void ItemsOk ()
  571. {
  572. ListControlPoker l = new ListControlPoker ();
  573. l.Items.Add ("foo");
  574. l.SelectedIndex = 0;
  575. l.SelectedIndex = -1;
  576. }
  577. [Test]
  578. public void DataBinding1 ()
  579. {
  580. ListControlPoker p = new ListControlPoker ();
  581. ArrayList list = new ArrayList ();
  582. list.Add (1);
  583. list.Add (2);
  584. list.Add (3);
  585. p.DataSource = list;
  586. p.SelectedIndex = 2;
  587. Assert.AreEqual (-1, p.SelectedIndex, "#01");
  588. p.DataBind ();
  589. Assert.AreEqual (2, p.SelectedIndex, "#02");
  590. Assert.AreEqual (3.ToString (), p.SelectedValue, "#03");
  591. }
  592. [Test]
  593. [ExpectedException (typeof (ArgumentException))] // The SelectedIndex and SelectedValue are mutually exclusive
  594. public void DataBinding2 ()
  595. {
  596. ListControlPoker p = new ListControlPoker ();
  597. ArrayList list = new ArrayList ();
  598. list.Add (1);
  599. list.Add (2);
  600. list.Add (3);
  601. p.DataSource = list;
  602. p.SelectedIndex = 0;
  603. p.SelectedValue = "3";
  604. p.DataBind ();
  605. }
  606. [Test]
  607. public void DataBinding3 ()
  608. {
  609. ListControlPoker p = new ListControlPoker ();
  610. ArrayList list = new ArrayList ();
  611. list.Add (1);
  612. list.Add (2);
  613. list.Add (3);
  614. p.DataSource = list;
  615. // If Index and Value are used, everything's ok if they are selecting
  616. // the same thing.
  617. p.SelectedIndex = 2;
  618. p.SelectedValue = "3";
  619. Assert.AreEqual ("", p.SelectedValue, "#01");
  620. p.DataBind ();
  621. Assert.AreEqual ("3", p.SelectedValue, "#02");
  622. }
  623. [Test]
  624. [ExpectedException (typeof (NullReferenceException))]
  625. public void DataBinding4 ()
  626. {
  627. ListControlPoker p = new ListControlPoker ();
  628. ArrayList list = new ArrayList ();
  629. list.Add (1);
  630. list.Add (null);
  631. list.Add (3);
  632. p.DataSource = list;
  633. p.DataBind ();
  634. }
  635. [Test]
  636. public void DataBinding6 () {
  637. ListControlPoker p = new ListControlPoker ();
  638. ArrayList list = new ArrayList ();
  639. list.Add (1);
  640. list.Add (2);
  641. list.Add (3);
  642. p.DataSource = list;
  643. p.DataBind ();
  644. Assert.AreEqual (3, p.Items.Count, "#01");
  645. p.DataSource = null;
  646. p.DataBind ();
  647. Assert.AreEqual (3, p.Items.Count, "#01");
  648. }
  649. #if NET_2_0
  650. [Test]
  651. public void DataBinding7 () {
  652. ListControlPoker p = new ListControlPoker ();
  653. ArrayList list = new ArrayList ();
  654. list.Add (1);
  655. list.Add (2);
  656. list.Add (3);
  657. p.DataSource = list;
  658. p.DataBind ();
  659. p.SelectedValue = "3";
  660. Assert.AreEqual (2, p.SelectedIndex, "#01");
  661. p.DataBind ();
  662. Assert.AreEqual ("3", p.SelectedValue, "#02");
  663. Assert.AreEqual (2, p.SelectedIndex, "#03");
  664. }
  665. [Test]
  666. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  667. public void DataBinding8 () {
  668. ListControlPoker p = new ListControlPoker ();
  669. ArrayList list = new ArrayList ();
  670. list.Add (1);
  671. list.Add (2);
  672. list.Add (3);
  673. p.DataSource = list;
  674. p.DataBind ();
  675. p.SelectedValue = "3";
  676. Assert.AreEqual (2, p.SelectedIndex, "#01");
  677. list = new ArrayList ();
  678. list.Add (4);
  679. list.Add (5);
  680. list.Add (6);
  681. p.DataSource = list;
  682. p.DataBind ();
  683. Assert.AreEqual ("3", p.SelectedValue, "#01");
  684. Assert.AreEqual (2, p.SelectedIndex, "#01");
  685. }
  686. [Test]
  687. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  688. public void DataBinding9 () {
  689. ListControlPoker p = new ListControlPoker ();
  690. ArrayList list = new ArrayList ();
  691. list.Add (1);
  692. list.Add (2);
  693. list.Add (3);
  694. p.DataSource = list;
  695. p.SelectedValue = "5";
  696. p.DataBind ();
  697. }
  698. [Test]
  699. public void DataBinding1a () {
  700. ListControlPoker p = new ListControlPoker ();
  701. ArrayList list = new ArrayList ();
  702. list.Add (1);
  703. list.Add (2);
  704. list.Add (3);
  705. p.DataSource = list;
  706. p.SelectedIndex = 4;
  707. Assert.AreEqual (-1, p.SelectedIndex, "#01");
  708. p.DataBind ();
  709. Assert.AreEqual (-1, p.SelectedIndex, "#02");
  710. Assert.AreEqual ("", p.SelectedValue, "#03");
  711. }
  712. [Test]
  713. public void DataBinding10 () {
  714. ListControlPoker p = new ListControlPoker ();
  715. ArrayList list = new ArrayList ();
  716. list.Add (1);
  717. list.Add (2);
  718. list.Add (3);
  719. p.DataSource = list;
  720. p.DataBind ();
  721. p.SelectedValue = "5";
  722. Assert.AreEqual ("", p.SelectedValue, "#01");
  723. p.SelectedIndex = 4;
  724. Assert.AreEqual (-1, p.SelectedIndex, "#02");
  725. }
  726. [Test]
  727. public void DataBinding11 () {
  728. ListControlPoker p = new ListControlPoker ();
  729. ArrayList list = new ArrayList ();
  730. list.Add (1);
  731. list.Add (2);
  732. list.Add (3);
  733. p.DataSource = list;
  734. p.SelectedValue = "3";
  735. p.DataBind ();
  736. p.SelectedValue = "5";
  737. Assert.AreEqual ("3", p.SelectedValue, "#01");
  738. p.SelectedIndex = 4;
  739. Assert.AreEqual (2, p.SelectedIndex, "#02");
  740. p.Items.Clear ();
  741. Assert.AreEqual ("", p.SelectedValue, "#03");
  742. Assert.AreEqual (-1, p.SelectedIndex, "#04");
  743. p.Items.Add (new ListItem ("1"));
  744. p.Items.Add (new ListItem ("2"));
  745. p.Items.Add (new ListItem ("3"));
  746. p.Items.Add (new ListItem ("4"));
  747. p.Items.Add (new ListItem ("5"));
  748. Assert.AreEqual ("", p.SelectedValue, "#05");
  749. Assert.AreEqual (-1, p.SelectedIndex, "#06");
  750. list = new ArrayList ();
  751. list.Add (1);
  752. list.Add (2);
  753. list.Add (3);
  754. list.Add (4);
  755. list.Add (5);
  756. p.DataSource = list;
  757. p.DataBind ();
  758. Assert.AreEqual ("5", p.SelectedValue, "#07");
  759. Assert.AreEqual (4, p.SelectedIndex, "#08");
  760. }
  761. #endif
  762. class Data
  763. {
  764. string name;
  765. object val;
  766. public Data (string name, object val)
  767. {
  768. this.name = name;
  769. this.val = val;
  770. }
  771. public string Name {
  772. get { return name; }
  773. }
  774. public object Value {
  775. get { return val; }
  776. }
  777. }
  778. [Test]
  779. public void DataBinding5 ()
  780. {
  781. ListControlPoker p = new ListControlPoker ();
  782. p.DataTextField = "Name";
  783. p.DataValueField = "Value";
  784. ArrayList list = new ArrayList ();
  785. list.Add (new Data ("uno", 1));
  786. list.Add (new Data ("dos", 2));
  787. list.Add (new Data ("tres", 3));
  788. p.DataSource = list;
  789. p.SelectedIndex = 2;
  790. p.DataBind ();
  791. Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
  792. Assert.AreEqual ("tres", p.SelectedItem.Text, "#01");
  793. }
  794. [Test]
  795. public void DataBindingFormat1 ()
  796. {
  797. ListControlPoker p = new ListControlPoker ();
  798. ArrayList list = new ArrayList ();
  799. list.Add (1);
  800. list.Add (2);
  801. list.Add (3);
  802. p.DataSource = list;
  803. p.DataTextFormatString = "{0:00}";
  804. p.SelectedIndex = 2;
  805. p.DataBind ();
  806. Assert.AreEqual ("3", p.SelectedValue, "#01");
  807. }
  808. [Test]
  809. public void DataBindingFormat2 ()
  810. {
  811. ListControlPoker p = new ListControlPoker ();
  812. ArrayList list = new ArrayList ();
  813. list.Add (1);
  814. list.Add (2);
  815. list.Add (3);
  816. p.DataSource = list;
  817. p.DataTextFormatString = "{0:00}";
  818. p.SelectedIndex = 2;
  819. p.DataBind ();
  820. Assert.IsNotNull (p.SelectedItem, "#00");
  821. Assert.AreEqual ("03", p.SelectedItem.Text, "#01");
  822. }
  823. [Test]
  824. [ExpectedException (typeof (NullReferenceException))]
  825. public void DataBindingFormat3 ()
  826. {
  827. ListControlPoker p = new ListControlPoker ();
  828. ArrayList list = new ArrayList ();
  829. list.Add (1);
  830. list.Add (null);
  831. list.Add (3);
  832. p.DataSource = list;
  833. p.DataTextFormatString = "{0:00}";
  834. p.SelectedIndex = 2;
  835. p.DataBind ();
  836. }
  837. private void BeginIndexChanged (ListControl l)
  838. {
  839. l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
  840. }
  841. private bool EndIndexChanged (ListControl l)
  842. {
  843. bool res = changed [l] != null;
  844. changed [l] = null;
  845. return res;
  846. }
  847. private void IndexChangedHandler (object sender, EventArgs e)
  848. {
  849. changed [sender] = new object ();
  850. }
  851. [Test]
  852. public void ValueFound1 ()
  853. {
  854. ListControlPoker p = new ListControlPoker ();
  855. p.Items.Add ("one");
  856. p.SelectedValue = "one";
  857. }
  858. [Test]
  859. #if !NET_2_0
  860. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  861. #endif
  862. public void ValueNotFound1 ()
  863. {
  864. ListControlPoker p = new ListControlPoker ();
  865. p.Items.Add ("one");
  866. p.SelectedValue = "dos";
  867. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  868. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  869. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  870. }
  871. [Test]
  872. public void ValueNotFound2 ()
  873. {
  874. ListControlPoker p = new ListControlPoker ();
  875. p.SelectedValue = "dos";
  876. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  877. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  878. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  879. }
  880. #if NET_2_0
  881. [Test]
  882. [ExpectedException (typeof (HttpException))]
  883. public void VerifyMultiSelect_Exception ()
  884. {
  885. ListControlPoker p = new ListControlPoker ();
  886. p.VerifyMultiSelect ();
  887. }
  888. [Test]
  889. public void DataBinding_SelectedValue () {
  890. ListControlPoker p = new ListControlPoker ();
  891. p.SelectedValue = "b";
  892. p.Items.Add (new ListItem ("a", "a"));
  893. p.Items.Add (new ListItem ("b", "b"));
  894. p.Items.Add (new ListItem ("c", "c"));
  895. Assert.IsFalse (p.Items [1].Selected, "SelectedIndex");
  896. p.DataBind ();
  897. Assert.IsTrue (p.Items [1].Selected, "SelectedIndex");
  898. }
  899. [Test]
  900. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  901. public void DataBinding_SelectedValue_Exception () {
  902. ListControlPoker p = new ListControlPoker ();
  903. p.SelectedValue = "AAA";
  904. p.Items.Add (new ListItem ("a", "a"));
  905. p.Items.Add (new ListItem ("b", "b"));
  906. p.Items.Add (new ListItem ("c", "c"));
  907. Assert.IsFalse (p.Items [1].Selected, "SelectedIndex");
  908. p.DataBind ();
  909. Assert.IsTrue (p.Items [1].Selected, "SelectedIndex");
  910. }
  911. [TestFixtureTearDown]
  912. public void TearDown ()
  913. {
  914. WebTest.Unload ();
  915. }
  916. #endif
  917. }
  918. }