ListControlTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. }
  271. [Test]
  272. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  273. public void ItemsTooLow ()
  274. {
  275. ListControlPoker l = new ListControlPoker ();
  276. l.Items.Add ("foo");
  277. l.SelectedIndex = -2;
  278. }
  279. [Test]
  280. public void ItemsOk ()
  281. {
  282. ListControlPoker l = new ListControlPoker ();
  283. l.Items.Add ("foo");
  284. l.SelectedIndex = 0;
  285. l.SelectedIndex = -1;
  286. }
  287. [Test]
  288. public void DataBinding1 ()
  289. {
  290. ListControlPoker p = new ListControlPoker ();
  291. ArrayList list = new ArrayList ();
  292. list.Add (1);
  293. list.Add (2);
  294. list.Add (3);
  295. p.DataSource = list;
  296. p.SelectedIndex = 2;
  297. p.DataBind ();
  298. Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
  299. }
  300. [Test]
  301. [ExpectedException (typeof (ArgumentException))] // The SelectedIndex and SelectedValue are mutually exclusive
  302. public void DataBinding2 ()
  303. {
  304. ListControlPoker p = new ListControlPoker ();
  305. ArrayList list = new ArrayList ();
  306. list.Add (1);
  307. list.Add (2);
  308. list.Add (3);
  309. p.DataSource = list;
  310. p.SelectedIndex = 0;
  311. p.SelectedValue = "3";
  312. p.DataBind ();
  313. }
  314. [Test]
  315. public void DataBinding3 ()
  316. {
  317. ListControlPoker p = new ListControlPoker ();
  318. ArrayList list = new ArrayList ();
  319. list.Add (1);
  320. list.Add (2);
  321. list.Add (3);
  322. p.DataSource = list;
  323. // If Index and Value are used, everything's ok if they are selecting
  324. // the same thing.
  325. p.SelectedIndex = 2;
  326. p.SelectedValue = "3";
  327. p.DataBind ();
  328. Assert.AreEqual ("3", p.SelectedValue, "#01");
  329. }
  330. [Test]
  331. [ExpectedException (typeof (NullReferenceException))]
  332. public void DataBinding4 ()
  333. {
  334. ListControlPoker p = new ListControlPoker ();
  335. ArrayList list = new ArrayList ();
  336. list.Add (1);
  337. list.Add (null);
  338. list.Add (3);
  339. p.DataSource = list;
  340. p.DataBind ();
  341. }
  342. class Data {
  343. string name;
  344. object val;
  345. public Data (string name, object val)
  346. {
  347. this.name = name;
  348. this.val = val;
  349. }
  350. public string Name {
  351. get { return name; }
  352. }
  353. public object Value {
  354. get { return val; }
  355. }
  356. }
  357. [Test]
  358. public void DataBinding5 ()
  359. {
  360. ListControlPoker p = new ListControlPoker ();
  361. p.DataTextField = "Name";
  362. p.DataValueField = "Value";
  363. ArrayList list = new ArrayList ();
  364. list.Add (new Data ("uno", 1));
  365. list.Add (new Data ("dos", 2));
  366. list.Add (new Data ("tres", 3));
  367. p.DataSource = list;
  368. p.SelectedIndex = 2;
  369. p.DataBind ();
  370. Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
  371. Assert.AreEqual ("tres", p.SelectedItem.Text, "#01");
  372. }
  373. [Test]
  374. public void DataBindingFormat1 ()
  375. {
  376. ListControlPoker p = new ListControlPoker ();
  377. ArrayList list = new ArrayList ();
  378. list.Add (1);
  379. list.Add (2);
  380. list.Add (3);
  381. p.DataSource = list;
  382. p.DataTextFormatString = "{0:00}";
  383. p.SelectedIndex = 2;
  384. p.DataBind ();
  385. Assert.AreEqual ("3", p.SelectedValue, "#01");
  386. }
  387. [Test]
  388. public void DataBindingFormat2 ()
  389. {
  390. ListControlPoker p = new ListControlPoker ();
  391. ArrayList list = new ArrayList ();
  392. list.Add (1);
  393. list.Add (2);
  394. list.Add (3);
  395. p.DataSource = list;
  396. p.DataTextFormatString = "{0:00}";
  397. p.SelectedIndex = 2;
  398. p.DataBind ();
  399. Assert.AreEqual ("03", p.SelectedItem.Text, "#01");
  400. }
  401. [Test]
  402. [ExpectedException (typeof (NullReferenceException))]
  403. public void DataBindingFormat3 ()
  404. {
  405. ListControlPoker p = new ListControlPoker ();
  406. ArrayList list = new ArrayList ();
  407. list.Add (1);
  408. list.Add (null);
  409. list.Add (3);
  410. p.DataSource = list;
  411. p.DataTextFormatString = "{0:00}";
  412. p.SelectedIndex = 2;
  413. p.DataBind ();
  414. }
  415. private void BeginIndexChanged (ListControl l)
  416. {
  417. l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
  418. }
  419. private bool EndIndexChanged (ListControl l)
  420. {
  421. bool res = changed [l] != null;
  422. changed [l] = null;
  423. return res;
  424. }
  425. private void IndexChangedHandler (object sender, EventArgs e)
  426. {
  427. changed [sender] = new object ();
  428. }
  429. [Test]
  430. public void ValueFound1 ()
  431. {
  432. ListControlPoker p = new ListControlPoker ();
  433. p.Items.Add ("one");
  434. p.SelectedValue = "one";
  435. }
  436. [Test]
  437. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  438. public void ValueNotFound1 ()
  439. {
  440. ListControlPoker p = new ListControlPoker ();
  441. p.Items.Add ("one");
  442. p.SelectedValue = "dos";
  443. }
  444. [Test]
  445. public void ValueNotFound2 ()
  446. {
  447. ListControlPoker p = new ListControlPoker ();
  448. p.SelectedValue = "dos";
  449. }
  450. }
  451. }