ListControlTest.cs 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. public void ViewStateContents ()
  476. {
  477. ListControlPoker p = new ListControlPoker ();
  478. p.TrackState ();
  479. // So the selected index can be set
  480. p.Items.Add ("one");
  481. p.Items.Add ("two");
  482. p.AutoPostBack = false;
  483. p.DataMember = "DataMember";
  484. p.DataSource = "DataSource";
  485. p.DataTextField = "DataTextField";
  486. p.DataTextFormatString = "DataTextFormatString";
  487. p.DataValueField = "DataValueField";
  488. p.SelectedIndex = 1;
  489. #if NET_2_0
  490. p.AppendDataBoundItems = true;
  491. p.Text = "Text";
  492. #endif
  493. Assert.AreEqual (p.ViewStateValue ("AutoPostBack"), false, "A1");
  494. Assert.AreEqual (p.ViewStateValue ("DataMember"), "DataMember", "A2");
  495. Assert.AreEqual (p.ViewStateValue ("DataSource"), null, "A3");
  496. Assert.AreEqual (p.ViewStateValue ("DataTextField"), "DataTextField", "A4");
  497. Assert.AreEqual (p.ViewStateValue ("DataTextFormatString"),
  498. "DataTextFormatString", "A5");
  499. Assert.AreEqual (p.ViewStateValue ("DataValueField"), "DataValueField", "A6");
  500. #if NET_2_0
  501. Assert.AreEqual (p.ViewStateValue ("AppendDataBoundItems"), true, "A7");
  502. #endif
  503. // None of these are saved
  504. Assert.AreEqual (p.ViewStateValue ("SelectedIndex"), null, "A8");
  505. Assert.AreEqual (p.ViewStateValue ("SelectedItem"), null, "A9");
  506. Assert.AreEqual (p.ViewStateValue ("SelectedValue"), null, "A10");
  507. #if NET_2_0
  508. Assert.AreEqual (p.ViewStateValue ("Text"), null, "A11");
  509. #endif
  510. }
  511. [Test]
  512. public void SelectedIndex ()
  513. {
  514. ListControlPoker p = new ListControlPoker ();
  515. p.Items.Add ("one");
  516. p.Items.Add ("two");
  517. p.Items.Add ("three");
  518. p.Items.Add ("four");
  519. p.Items [2].Selected = true;
  520. p.Items [1].Selected = true;
  521. Assert.AreEqual (p.SelectedIndex, 1, "A1");
  522. p.ClearSelection ();
  523. p.Items [3].Selected = true;
  524. Assert.AreEqual (p.SelectedIndex, 3, "A2");
  525. p.SelectedIndex = 1;
  526. Assert.AreEqual (p.SelectedIndex, 1, "A3");
  527. Assert.IsFalse (p.Items [3].Selected, "A4");
  528. }
  529. [Test]
  530. public void Render ()
  531. {
  532. ListControlPoker p = new ListControlPoker ();
  533. string s = p.Render ();
  534. string expected = "<select>\n\n</select>";
  535. Assert.AreEqual (s, expected, "A1");
  536. }
  537. [Test]
  538. #if !NET_2_0
  539. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  540. #endif
  541. public void ItemsTooHigh ()
  542. {
  543. ListControlPoker l = new ListControlPoker ();
  544. l.Items.Add ("foo");
  545. l.SelectedIndex = 1;
  546. #if NET_2_0
  547. Assert.AreEqual (-1, l.SelectedIndex, "#01");
  548. #endif
  549. }
  550. [Test]
  551. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  552. public void ItemsTooLow ()
  553. {
  554. ListControlPoker l = new ListControlPoker ();
  555. l.Items.Add ("foo");
  556. l.SelectedIndex = -2;
  557. }
  558. [Test]
  559. public void ItemsOk ()
  560. {
  561. ListControlPoker l = new ListControlPoker ();
  562. l.Items.Add ("foo");
  563. l.SelectedIndex = 0;
  564. l.SelectedIndex = -1;
  565. }
  566. [Test]
  567. public void DataBinding1 ()
  568. {
  569. ListControlPoker p = new ListControlPoker ();
  570. ArrayList list = new ArrayList ();
  571. list.Add (1);
  572. list.Add (2);
  573. list.Add (3);
  574. p.DataSource = list;
  575. p.SelectedIndex = 2;
  576. Assert.AreEqual (-1, p.SelectedIndex, "#01");
  577. p.DataBind ();
  578. Assert.AreEqual (2, p.SelectedIndex, "#02");
  579. Assert.AreEqual (3.ToString (), p.SelectedValue, "#03");
  580. }
  581. [Test]
  582. [ExpectedException (typeof (ArgumentException))] // The SelectedIndex and SelectedValue are mutually exclusive
  583. public void DataBinding2 ()
  584. {
  585. ListControlPoker p = new ListControlPoker ();
  586. ArrayList list = new ArrayList ();
  587. list.Add (1);
  588. list.Add (2);
  589. list.Add (3);
  590. p.DataSource = list;
  591. p.SelectedIndex = 0;
  592. p.SelectedValue = "3";
  593. p.DataBind ();
  594. }
  595. [Test]
  596. public void DataBinding3 ()
  597. {
  598. ListControlPoker p = new ListControlPoker ();
  599. ArrayList list = new ArrayList ();
  600. list.Add (1);
  601. list.Add (2);
  602. list.Add (3);
  603. p.DataSource = list;
  604. // If Index and Value are used, everything's ok if they are selecting
  605. // the same thing.
  606. p.SelectedIndex = 2;
  607. p.SelectedValue = "3";
  608. Assert.AreEqual ("", p.SelectedValue, "#01");
  609. p.DataBind ();
  610. Assert.AreEqual ("3", p.SelectedValue, "#02");
  611. }
  612. [Test]
  613. [ExpectedException (typeof (NullReferenceException))]
  614. public void DataBinding4 ()
  615. {
  616. ListControlPoker p = new ListControlPoker ();
  617. ArrayList list = new ArrayList ();
  618. list.Add (1);
  619. list.Add (null);
  620. list.Add (3);
  621. p.DataSource = list;
  622. p.DataBind ();
  623. }
  624. [Test]
  625. public void DataBinding6 () {
  626. ListControlPoker p = new ListControlPoker ();
  627. ArrayList list = new ArrayList ();
  628. list.Add (1);
  629. list.Add (2);
  630. list.Add (3);
  631. p.DataSource = list;
  632. p.DataBind ();
  633. Assert.AreEqual (3, p.Items.Count, "#01");
  634. p.DataSource = null;
  635. p.DataBind ();
  636. Assert.AreEqual (3, p.Items.Count, "#01");
  637. }
  638. #if NET_2_0
  639. [Test]
  640. public void DataBinding7 () {
  641. ListControlPoker p = new ListControlPoker ();
  642. ArrayList list = new ArrayList ();
  643. list.Add (1);
  644. list.Add (2);
  645. list.Add (3);
  646. p.DataSource = list;
  647. p.DataBind ();
  648. p.SelectedValue = "3";
  649. Assert.AreEqual (2, p.SelectedIndex, "#01");
  650. p.DataBind ();
  651. Assert.AreEqual ("3", p.SelectedValue, "#02");
  652. Assert.AreEqual (2, p.SelectedIndex, "#03");
  653. }
  654. [Test]
  655. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  656. public void DataBinding8 () {
  657. ListControlPoker p = new ListControlPoker ();
  658. ArrayList list = new ArrayList ();
  659. list.Add (1);
  660. list.Add (2);
  661. list.Add (3);
  662. p.DataSource = list;
  663. p.DataBind ();
  664. p.SelectedValue = "3";
  665. Assert.AreEqual (2, p.SelectedIndex, "#01");
  666. list = new ArrayList ();
  667. list.Add (4);
  668. list.Add (5);
  669. list.Add (6);
  670. p.DataSource = list;
  671. p.DataBind ();
  672. Assert.AreEqual ("3", p.SelectedValue, "#01");
  673. Assert.AreEqual (2, p.SelectedIndex, "#01");
  674. }
  675. [Test]
  676. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  677. public void DataBinding9 () {
  678. ListControlPoker p = new ListControlPoker ();
  679. ArrayList list = new ArrayList ();
  680. list.Add (1);
  681. list.Add (2);
  682. list.Add (3);
  683. p.DataSource = list;
  684. p.SelectedValue = "5";
  685. p.DataBind ();
  686. }
  687. [Test]
  688. public void DataBinding1a () {
  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.SelectedIndex = 4;
  696. Assert.AreEqual (-1, p.SelectedIndex, "#01");
  697. p.DataBind ();
  698. Assert.AreEqual (-1, p.SelectedIndex, "#02");
  699. Assert.AreEqual ("", p.SelectedValue, "#03");
  700. }
  701. [Test]
  702. public void DataBinding10 () {
  703. ListControlPoker p = new ListControlPoker ();
  704. ArrayList list = new ArrayList ();
  705. list.Add (1);
  706. list.Add (2);
  707. list.Add (3);
  708. p.DataSource = list;
  709. p.DataBind ();
  710. p.SelectedValue = "5";
  711. Assert.AreEqual ("", p.SelectedValue, "#01");
  712. p.SelectedIndex = 4;
  713. Assert.AreEqual (-1, p.SelectedIndex, "#02");
  714. }
  715. [Test]
  716. public void DataBinding11 () {
  717. ListControlPoker p = new ListControlPoker ();
  718. ArrayList list = new ArrayList ();
  719. list.Add (1);
  720. list.Add (2);
  721. list.Add (3);
  722. p.DataSource = list;
  723. p.SelectedValue = "3";
  724. p.DataBind ();
  725. p.SelectedValue = "5";
  726. Assert.AreEqual ("3", p.SelectedValue, "#01");
  727. p.SelectedIndex = 4;
  728. Assert.AreEqual (2, p.SelectedIndex, "#02");
  729. p.Items.Clear ();
  730. Assert.AreEqual ("", p.SelectedValue, "#03");
  731. Assert.AreEqual (-1, p.SelectedIndex, "#04");
  732. p.Items.Add (new ListItem ("1"));
  733. p.Items.Add (new ListItem ("2"));
  734. p.Items.Add (new ListItem ("3"));
  735. p.Items.Add (new ListItem ("4"));
  736. p.Items.Add (new ListItem ("5"));
  737. Assert.AreEqual ("", p.SelectedValue, "#05");
  738. Assert.AreEqual (-1, p.SelectedIndex, "#06");
  739. list = new ArrayList ();
  740. list.Add (1);
  741. list.Add (2);
  742. list.Add (3);
  743. list.Add (4);
  744. list.Add (5);
  745. p.DataSource = list;
  746. p.DataBind ();
  747. Assert.AreEqual ("5", p.SelectedValue, "#07");
  748. Assert.AreEqual (4, p.SelectedIndex, "#08");
  749. }
  750. #endif
  751. class Data
  752. {
  753. string name;
  754. object val;
  755. public Data (string name, object val)
  756. {
  757. this.name = name;
  758. this.val = val;
  759. }
  760. public string Name {
  761. get { return name; }
  762. }
  763. public object Value {
  764. get { return val; }
  765. }
  766. }
  767. [Test]
  768. public void DataBinding5 ()
  769. {
  770. ListControlPoker p = new ListControlPoker ();
  771. p.DataTextField = "Name";
  772. p.DataValueField = "Value";
  773. ArrayList list = new ArrayList ();
  774. list.Add (new Data ("uno", 1));
  775. list.Add (new Data ("dos", 2));
  776. list.Add (new Data ("tres", 3));
  777. p.DataSource = list;
  778. p.SelectedIndex = 2;
  779. p.DataBind ();
  780. Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
  781. Assert.AreEqual ("tres", p.SelectedItem.Text, "#01");
  782. }
  783. [Test]
  784. public void DataBindingFormat1 ()
  785. {
  786. ListControlPoker p = new ListControlPoker ();
  787. ArrayList list = new ArrayList ();
  788. list.Add (1);
  789. list.Add (2);
  790. list.Add (3);
  791. p.DataSource = list;
  792. p.DataTextFormatString = "{0:00}";
  793. p.SelectedIndex = 2;
  794. p.DataBind ();
  795. Assert.AreEqual ("3", p.SelectedValue, "#01");
  796. }
  797. [Test]
  798. public void DataBindingFormat2 ()
  799. {
  800. ListControlPoker p = new ListControlPoker ();
  801. ArrayList list = new ArrayList ();
  802. list.Add (1);
  803. list.Add (2);
  804. list.Add (3);
  805. p.DataSource = list;
  806. p.DataTextFormatString = "{0:00}";
  807. p.SelectedIndex = 2;
  808. p.DataBind ();
  809. Assert.IsNotNull (p.SelectedItem, "#00");
  810. Assert.AreEqual ("03", p.SelectedItem.Text, "#01");
  811. }
  812. [Test]
  813. [ExpectedException (typeof (NullReferenceException))]
  814. public void DataBindingFormat3 ()
  815. {
  816. ListControlPoker p = new ListControlPoker ();
  817. ArrayList list = new ArrayList ();
  818. list.Add (1);
  819. list.Add (null);
  820. list.Add (3);
  821. p.DataSource = list;
  822. p.DataTextFormatString = "{0:00}";
  823. p.SelectedIndex = 2;
  824. p.DataBind ();
  825. }
  826. private void BeginIndexChanged (ListControl l)
  827. {
  828. l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
  829. }
  830. private bool EndIndexChanged (ListControl l)
  831. {
  832. bool res = changed [l] != null;
  833. changed [l] = null;
  834. return res;
  835. }
  836. private void IndexChangedHandler (object sender, EventArgs e)
  837. {
  838. changed [sender] = new object ();
  839. }
  840. [Test]
  841. public void ValueFound1 ()
  842. {
  843. ListControlPoker p = new ListControlPoker ();
  844. p.Items.Add ("one");
  845. p.SelectedValue = "one";
  846. }
  847. [Test]
  848. #if !NET_2_0
  849. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  850. #endif
  851. public void ValueNotFound1 ()
  852. {
  853. ListControlPoker p = new ListControlPoker ();
  854. p.Items.Add ("one");
  855. p.SelectedValue = "dos";
  856. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  857. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  858. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  859. }
  860. [Test]
  861. public void ValueNotFound2 ()
  862. {
  863. ListControlPoker p = new ListControlPoker ();
  864. p.SelectedValue = "dos";
  865. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  866. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  867. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  868. }
  869. #if NET_2_0
  870. [Test]
  871. [ExpectedException (typeof (HttpException))]
  872. public void VerifyMultiSelect_Exception ()
  873. {
  874. ListControlPoker p = new ListControlPoker ();
  875. p.VerifyMultiSelect ();
  876. }
  877. [TestFixtureTearDown]
  878. public void TearDown ()
  879. {
  880. WebTest.Unload ();
  881. }
  882. #endif
  883. }
  884. }