PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Web/Test/System.Web.UI.WebControls/ListControlTest.cs

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