ListControlTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. namespace MonoTests.System.Web.UI.WebControls {
  38. public class ListControlPoker : ListControl {
  39. public ListControlPoker ()
  40. {
  41. }
  42. public void TrackState ()
  43. {
  44. TrackViewState ();
  45. }
  46. public object SaveState ()
  47. {
  48. return SaveViewState ();
  49. }
  50. public void LoadState (object state)
  51. {
  52. LoadViewState (state);
  53. }
  54. public object ViewStateValue (string name)
  55. {
  56. return ViewState [name];
  57. }
  58. public string Render ()
  59. {
  60. StringWriter sw = new StringWriter ();
  61. sw.NewLine = "\n";
  62. HtmlTextWriter writer = new HtmlTextWriter (sw);
  63. base.Render (writer);
  64. return writer.InnerWriter.ToString ();
  65. }
  66. #if NET_2_0
  67. public HtmlTextWriterTag GetTagKey ()
  68. {
  69. return TagKey;
  70. }
  71. #endif
  72. }
  73. [TestFixture]
  74. public class ListControlTest {
  75. private Hashtable changed = new Hashtable ();
  76. [Test]
  77. public void DefaultProperties ()
  78. {
  79. ListControlPoker p = new ListControlPoker ();
  80. Assert.AreEqual (p.AutoPostBack, false, "A1");
  81. Assert.AreEqual (p.DataMember, String.Empty, "A2");
  82. Assert.AreEqual (p.DataSource, null, "A3");
  83. Assert.AreEqual (p.DataTextField, String.Empty, "A4");
  84. Assert.AreEqual (p.DataTextFormatString, String.Empty, "A5");
  85. Assert.AreEqual (p.DataValueField, String.Empty, "A6");
  86. Assert.AreEqual (p.Items.Count, 0, "A7");
  87. Assert.AreEqual (p.SelectedIndex, -1,"A8");
  88. Assert.AreEqual (p.SelectedItem, null, "A9");
  89. Assert.AreEqual (p.SelectedValue, String.Empty, "A10");
  90. #if NET_2_0
  91. Assert.IsFalse (p.AppendDataBoundItems, "A11");
  92. Assert.AreEqual (p.Text, "", "A12");
  93. Assert.AreEqual (p.GetTagKey(), HtmlTextWriterTag.Select, "A13");
  94. #endif
  95. }
  96. [Test]
  97. public void CleanProperties ()
  98. {
  99. ListControlPoker p = new ListControlPoker ();
  100. p.AutoPostBack = true;
  101. Assert.AreEqual (p.AutoPostBack, true, "A2");
  102. p.DataMember = "DataMember";
  103. Assert.AreEqual (p.DataMember, "DataMember", "A3");
  104. p.DataSource = "DataSource";
  105. Assert.AreEqual (p.DataSource, "DataSource", "A4");
  106. p.DataTextField = "DataTextField";
  107. Assert.AreEqual (p.DataTextField, "DataTextField", "A5");
  108. p.DataTextFormatString = "DataTextFormatString";
  109. Assert.AreEqual (p.DataTextFormatString, "DataTextFormatString", "A6");
  110. p.DataValueField = "DataValueField";
  111. Assert.AreEqual (p.DataValueField, "DataValueField", "A7");
  112. p.SelectedIndex = 10;
  113. Assert.AreEqual (p.SelectedIndex, -1, "A8");
  114. p.SelectedValue = "SelectedValue";
  115. Assert.AreEqual (p.SelectedValue, String.Empty, "A9");
  116. }
  117. [Test]
  118. public void NullProperties ()
  119. {
  120. ListControlPoker p = new ListControlPoker ();
  121. p.DataMember = null;
  122. Assert.AreEqual (p.DataMember, String.Empty, "A1");
  123. p.DataSource = null;
  124. Assert.AreEqual (p.DataSource, null, "A2");
  125. p.DataTextField = null;
  126. Assert.AreEqual (p.DataTextField, String.Empty, "A3");
  127. p.DataTextFormatString = null;
  128. Assert.AreEqual (p.DataTextFormatString, String.Empty, "A4");
  129. p.DataValueField = null;
  130. Assert.AreEqual (p.DataValueField, String.Empty, "A5");
  131. p.SelectedValue = null;
  132. Assert.AreEqual (p.SelectedValue, String.Empty, "A6");
  133. }
  134. [Test]
  135. public void ClearSelection ()
  136. {
  137. ListControlPoker p = new ListControlPoker ();
  138. ListItem foo = new ListItem ("foo");
  139. ListItem bar = new ListItem ("bar");
  140. BeginIndexChanged (p);
  141. p.Items.Add (foo);
  142. p.Items.Add (bar);
  143. p.SelectedIndex = 1;
  144. // sanity for the real test
  145. Assert.AreEqual (p.Items.Count, 2, "A1");
  146. Assert.AreEqual (p.SelectedIndex, 1, "A2");
  147. Assert.AreEqual (p.SelectedItem, bar, "A3");
  148. Assert.AreEqual (p.SelectedValue, bar.Value, "A4");
  149. p.ClearSelection ();
  150. Assert.AreEqual (p.SelectedIndex, -1, "A5");
  151. Assert.AreEqual (p.SelectedItem, null, "A6");
  152. Assert.AreEqual (p.SelectedValue, String.Empty, "A7");
  153. Assert.IsFalse (EndIndexChanged (p), "A8");
  154. // make sure we are still sane
  155. Assert.AreEqual (p.Items.Count, 2, "A9");
  156. }
  157. #if NET_2_0
  158. [Test]
  159. // Tests Save/Load ControlState
  160. public void ControlState ()
  161. {
  162. ListControlPoker a = new ListControlPoker ();
  163. ListControlPoker b = new ListControlPoker ();
  164. a.TrackState ();
  165. a.Items.Add ("a");
  166. a.Items.Add ("b");
  167. a.Items.Add ("c");
  168. a.SelectedIndex = 2;
  169. b.Items.Add ("a");
  170. b.Items.Add ("b");
  171. b.Items.Add ("c");
  172. Assert.AreEqual (-1, b.SelectedIndex, "A1");
  173. }
  174. #endif
  175. [Test]
  176. // Tests Save/Load/Track ViewState
  177. public void ViewState ()
  178. {
  179. ListControlPoker a = new ListControlPoker ();
  180. ListControlPoker b = new ListControlPoker ();
  181. a.TrackState ();
  182. BeginIndexChanged (a);
  183. BeginIndexChanged (b);
  184. a.Items.Add ("a");
  185. a.Items.Add ("b");
  186. a.Items.Add ("c");
  187. a.SelectedIndex = 2;
  188. object state = a.SaveState ();
  189. b.LoadState (state);
  190. Assert.AreEqual (2, b.SelectedIndex, "A1");
  191. Assert.AreEqual (b.Items.Count, 3, "A2");
  192. Assert.AreEqual (b.Items [0].Value, "a", "A3");
  193. Assert.AreEqual (b.Items [1].Value, "b", "A4");
  194. Assert.AreEqual (b.Items [2].Value, "c", "A5");
  195. Assert.IsFalse (EndIndexChanged (a), "A6");
  196. Assert.IsFalse (EndIndexChanged (b), "A7");
  197. }
  198. [Test]
  199. public void ViewStateContents ()
  200. {
  201. ListControlPoker p = new ListControlPoker ();
  202. p.TrackState ();
  203. // So the selected index can be set
  204. p.Items.Add ("one");
  205. p.Items.Add ("two");
  206. p.AutoPostBack = false;
  207. p.DataMember = "DataMember";
  208. p.DataSource = "DataSource";
  209. p.DataTextField = "DataTextField";
  210. p.DataTextFormatString = "DataTextFormatString";
  211. p.DataValueField = "DataValueField";
  212. p.SelectedIndex = 1;
  213. #if NET_2_0
  214. p.AppendDataBoundItems = true;
  215. p.Text = "Text";
  216. #endif
  217. Assert.AreEqual (p.ViewStateValue ("AutoPostBack"), false, "A1");
  218. Assert.AreEqual (p.ViewStateValue ("DataMember"), "DataMember", "A2");
  219. Assert.AreEqual (p.ViewStateValue ("DataSource"), null, "A3");
  220. Assert.AreEqual (p.ViewStateValue ("DataTextField"), "DataTextField", "A4");
  221. Assert.AreEqual (p.ViewStateValue ("DataTextFormatString"),
  222. "DataTextFormatString", "A5");
  223. Assert.AreEqual (p.ViewStateValue ("DataValueField"), "DataValueField", "A6");
  224. #if NET_2_0
  225. Assert.AreEqual (p.ViewStateValue ("AppendDataBoundItems"), true, "A7");
  226. #endif
  227. // None of these are saved
  228. Assert.AreEqual (p.ViewStateValue ("SelectedIndex"), null, "A8");
  229. Assert.AreEqual (p.ViewStateValue ("SelectedItem"), null, "A9");
  230. Assert.AreEqual (p.ViewStateValue ("SelectedValue"), null, "A10");
  231. #if NET_2_0
  232. Assert.AreEqual (p.ViewStateValue ("Text"), null, "A11");
  233. #endif
  234. }
  235. [Test]
  236. public void SelectedIndex ()
  237. {
  238. ListControlPoker p = new ListControlPoker ();
  239. p.Items.Add ("one");
  240. p.Items.Add ("two");
  241. p.Items.Add ("three");
  242. p.Items.Add ("four");
  243. p.Items [2].Selected = true;
  244. p.Items [1].Selected = true;
  245. Assert.AreEqual (p.SelectedIndex, 1, "A1");
  246. p.ClearSelection ();
  247. p.Items [3].Selected = true;
  248. Assert.AreEqual (p.SelectedIndex, 3, "A2");
  249. p.SelectedIndex = 1;
  250. Assert.AreEqual (p.SelectedIndex, 1, "A3");
  251. Assert.IsFalse (p.Items [3].Selected, "A4");
  252. }
  253. [Test]
  254. public void Render ()
  255. {
  256. ListControlPoker p = new ListControlPoker ();
  257. string s = p.Render ();
  258. string expected = "<select>\n\n</select>";
  259. Assert.AreEqual (s, expected, "A1");
  260. }
  261. [Test]
  262. #if !NET_2_0
  263. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  264. #endif
  265. public void ItemsTooHigh ()
  266. {
  267. ListControlPoker l = new ListControlPoker ();
  268. l.Items.Add ("foo");
  269. l.SelectedIndex = 1;
  270. #if NET_2_0
  271. Assert.AreEqual (-1, l.SelectedIndex, "#01");
  272. #endif
  273. }
  274. [Test]
  275. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  276. public void ItemsTooLow ()
  277. {
  278. ListControlPoker l = new ListControlPoker ();
  279. l.Items.Add ("foo");
  280. l.SelectedIndex = -2;
  281. }
  282. [Test]
  283. public void ItemsOk ()
  284. {
  285. ListControlPoker l = new ListControlPoker ();
  286. l.Items.Add ("foo");
  287. l.SelectedIndex = 0;
  288. l.SelectedIndex = -1;
  289. }
  290. [Test]
  291. public void DataBinding1 ()
  292. {
  293. ListControlPoker p = new ListControlPoker ();
  294. ArrayList list = new ArrayList ();
  295. list.Add (1);
  296. list.Add (2);
  297. list.Add (3);
  298. p.DataSource = list;
  299. p.SelectedIndex = 2;
  300. p.DataBind ();
  301. Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
  302. }
  303. [Test]
  304. [ExpectedException (typeof (ArgumentException))] // The SelectedIndex and SelectedValue are mutually exclusive
  305. public void DataBinding2 ()
  306. {
  307. ListControlPoker p = new ListControlPoker ();
  308. ArrayList list = new ArrayList ();
  309. list.Add (1);
  310. list.Add (2);
  311. list.Add (3);
  312. p.DataSource = list;
  313. p.SelectedIndex = 0;
  314. p.SelectedValue = "3";
  315. p.DataBind ();
  316. }
  317. [Test]
  318. public void DataBinding3 ()
  319. {
  320. ListControlPoker p = new ListControlPoker ();
  321. ArrayList list = new ArrayList ();
  322. list.Add (1);
  323. list.Add (2);
  324. list.Add (3);
  325. p.DataSource = list;
  326. // If Index and Value are used, everything's ok if they are selecting
  327. // the same thing.
  328. p.SelectedIndex = 2;
  329. p.SelectedValue = "3";
  330. p.DataBind ();
  331. Assert.AreEqual ("3", p.SelectedValue, "#01");
  332. }
  333. [Test]
  334. [ExpectedException (typeof (NullReferenceException))]
  335. public void DataBinding4 ()
  336. {
  337. ListControlPoker p = new ListControlPoker ();
  338. ArrayList list = new ArrayList ();
  339. list.Add (1);
  340. list.Add (null);
  341. list.Add (3);
  342. p.DataSource = list;
  343. p.DataBind ();
  344. }
  345. [Test]
  346. public void DataBinding6 () {
  347. ListControlPoker p = new ListControlPoker ();
  348. ArrayList list = new ArrayList ();
  349. list.Add (1);
  350. list.Add (2);
  351. list.Add (3);
  352. p.DataSource = list;
  353. p.DataBind ();
  354. Assert.AreEqual (3, p.Items.Count, "#01");
  355. p.DataSource = null;
  356. p.DataBind ();
  357. Assert.AreEqual (3, p.Items.Count, "#01");
  358. }
  359. class Data {
  360. string name;
  361. object val;
  362. public Data (string name, object val)
  363. {
  364. this.name = name;
  365. this.val = val;
  366. }
  367. public string Name {
  368. get { return name; }
  369. }
  370. public object Value {
  371. get { return val; }
  372. }
  373. }
  374. [Test]
  375. public void DataBinding5 ()
  376. {
  377. ListControlPoker p = new ListControlPoker ();
  378. p.DataTextField = "Name";
  379. p.DataValueField = "Value";
  380. ArrayList list = new ArrayList ();
  381. list.Add (new Data ("uno", 1));
  382. list.Add (new Data ("dos", 2));
  383. list.Add (new Data ("tres", 3));
  384. p.DataSource = list;
  385. p.SelectedIndex = 2;
  386. p.DataBind ();
  387. Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
  388. Assert.AreEqual ("tres", p.SelectedItem.Text, "#01");
  389. }
  390. [Test]
  391. public void DataBindingFormat1 ()
  392. {
  393. ListControlPoker p = new ListControlPoker ();
  394. ArrayList list = new ArrayList ();
  395. list.Add (1);
  396. list.Add (2);
  397. list.Add (3);
  398. p.DataSource = list;
  399. p.DataTextFormatString = "{0:00}";
  400. p.SelectedIndex = 2;
  401. p.DataBind ();
  402. Assert.AreEqual ("3", p.SelectedValue, "#01");
  403. }
  404. [Test]
  405. public void DataBindingFormat2 ()
  406. {
  407. ListControlPoker p = new ListControlPoker ();
  408. ArrayList list = new ArrayList ();
  409. list.Add (1);
  410. list.Add (2);
  411. list.Add (3);
  412. p.DataSource = list;
  413. p.DataTextFormatString = "{0:00}";
  414. p.SelectedIndex = 2;
  415. p.DataBind ();
  416. Assert.IsNotNull (p.SelectedItem, "#00");
  417. Assert.AreEqual ("03", p.SelectedItem.Text, "#01");
  418. }
  419. [Test]
  420. [ExpectedException (typeof (NullReferenceException))]
  421. public void DataBindingFormat3 ()
  422. {
  423. ListControlPoker p = new ListControlPoker ();
  424. ArrayList list = new ArrayList ();
  425. list.Add (1);
  426. list.Add (null);
  427. list.Add (3);
  428. p.DataSource = list;
  429. p.DataTextFormatString = "{0:00}";
  430. p.SelectedIndex = 2;
  431. p.DataBind ();
  432. }
  433. private void BeginIndexChanged (ListControl l)
  434. {
  435. l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
  436. }
  437. private bool EndIndexChanged (ListControl l)
  438. {
  439. bool res = changed [l] != null;
  440. changed [l] = null;
  441. return res;
  442. }
  443. private void IndexChangedHandler (object sender, EventArgs e)
  444. {
  445. changed [sender] = new object ();
  446. }
  447. [Test]
  448. public void ValueFound1 ()
  449. {
  450. ListControlPoker p = new ListControlPoker ();
  451. p.Items.Add ("one");
  452. p.SelectedValue = "one";
  453. }
  454. [Test]
  455. #if !NET_2_0
  456. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  457. #endif
  458. public void ValueNotFound1 ()
  459. {
  460. ListControlPoker p = new ListControlPoker ();
  461. p.Items.Add ("one");
  462. p.SelectedValue = "dos";
  463. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  464. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  465. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  466. }
  467. [Test]
  468. public void ValueNotFound2 ()
  469. {
  470. ListControlPoker p = new ListControlPoker ();
  471. p.SelectedValue = "dos";
  472. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  473. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  474. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  475. }
  476. }
  477. }