ListControlTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. [Category ("NotWorking")]
  119. public void NullProperties ()
  120. {
  121. ListControlPoker p = new ListControlPoker ();
  122. p.DataMember = null;
  123. Assert.AreEqual (p.DataMember, String.Empty, "A1");
  124. p.DataSource = null;
  125. Assert.AreEqual (p.DataSource, null, "A2");
  126. p.DataTextField = null;
  127. Assert.AreEqual (p.DataTextField, String.Empty, "A3");
  128. p.DataTextFormatString = null;
  129. Assert.AreEqual (p.DataTextFormatString, String.Empty, "A4");
  130. p.DataValueField = null;
  131. Assert.AreEqual (p.DataValueField, String.Empty, "A5");
  132. p.SelectedValue = null;
  133. Assert.AreEqual (p.SelectedValue, String.Empty, "A6");
  134. }
  135. [Test]
  136. public void ClearSelection ()
  137. {
  138. ListControlPoker p = new ListControlPoker ();
  139. ListItem foo = new ListItem ("foo");
  140. ListItem bar = new ListItem ("bar");
  141. BeginIndexChanged (p);
  142. p.Items.Add (foo);
  143. p.Items.Add (bar);
  144. p.SelectedIndex = 1;
  145. // sanity for the real test
  146. Assert.AreEqual (p.Items.Count, 2, "A1");
  147. Assert.AreEqual (p.SelectedIndex, 1, "A2");
  148. Assert.AreEqual (p.SelectedItem, bar, "A3");
  149. Assert.AreEqual (p.SelectedValue, bar.Value, "A4");
  150. p.ClearSelection ();
  151. Assert.AreEqual (p.SelectedIndex, -1, "A5");
  152. Assert.AreEqual (p.SelectedItem, null, "A6");
  153. Assert.AreEqual (p.SelectedValue, String.Empty, "A7");
  154. Assert.IsFalse (EndIndexChanged (p), "A8");
  155. // make sure we are still sane
  156. Assert.AreEqual (p.Items.Count, 2, "A9");
  157. }
  158. #if NET_2_0
  159. [Test]
  160. // Tests Save/Load ControlState
  161. public void ControlState ()
  162. {
  163. ListControlPoker a = new ListControlPoker ();
  164. ListControlPoker b = new ListControlPoker ();
  165. a.TrackState ();
  166. a.Items.Add ("a");
  167. a.Items.Add ("b");
  168. a.Items.Add ("c");
  169. a.SelectedIndex = 2;
  170. b.Items.Add ("a");
  171. b.Items.Add ("b");
  172. b.Items.Add ("c");
  173. Assert.AreEqual (-1, b.SelectedIndex, "A1");
  174. }
  175. #endif
  176. [Test]
  177. // Tests Save/Load/Track ViewState
  178. public void ViewState ()
  179. {
  180. ListControlPoker a = new ListControlPoker ();
  181. ListControlPoker b = new ListControlPoker ();
  182. a.TrackState ();
  183. BeginIndexChanged (a);
  184. BeginIndexChanged (b);
  185. a.Items.Add ("a");
  186. a.Items.Add ("b");
  187. a.Items.Add ("c");
  188. a.SelectedIndex = 2;
  189. object state = a.SaveState ();
  190. b.LoadState (state);
  191. Assert.AreEqual (2, b.SelectedIndex, "A1");
  192. Assert.AreEqual (b.Items.Count, 3, "A2");
  193. Assert.AreEqual (b.Items [0].Value, "a", "A3");
  194. Assert.AreEqual (b.Items [1].Value, "b", "A4");
  195. Assert.AreEqual (b.Items [2].Value, "c", "A5");
  196. Assert.IsFalse (EndIndexChanged (a), "A6");
  197. Assert.IsFalse (EndIndexChanged (b), "A7");
  198. }
  199. [Test]
  200. [Category ("NotWorking")]
  201. public void ViewStateContents ()
  202. {
  203. ListControlPoker p = new ListControlPoker ();
  204. p.TrackState ();
  205. // So the selected index can be set
  206. p.Items.Add ("one");
  207. p.Items.Add ("two");
  208. p.AutoPostBack = false;
  209. p.DataMember = "DataMember";
  210. p.DataSource = "DataSource";
  211. p.DataTextField = "DataTextField";
  212. p.DataTextFormatString = "DataTextFormatString";
  213. p.DataValueField = "DataValueField";
  214. p.SelectedIndex = 1;
  215. #if NET_2_0
  216. p.AppendDataBoundItems = true;
  217. p.Text = "Text";
  218. #endif
  219. Assert.AreEqual (p.ViewStateValue ("AutoPostBack"), false, "A1");
  220. Assert.AreEqual (p.ViewStateValue ("DataMember"), "DataMember", "A2");
  221. Assert.AreEqual (p.ViewStateValue ("DataSource"), null, "A3");
  222. Assert.AreEqual (p.ViewStateValue ("DataTextField"), "DataTextField", "A4");
  223. Assert.AreEqual (p.ViewStateValue ("DataTextFormatString"),
  224. "DataTextFormatString", "A5");
  225. Assert.AreEqual (p.ViewStateValue ("DataValueField"), "DataValueField", "A6");
  226. #if NET_2_0
  227. Assert.AreEqual (p.ViewStateValue ("AppendDataBoundItems"), true, "A7");
  228. #endif
  229. // None of these are saved
  230. Assert.AreEqual (p.ViewStateValue ("SelectedIndex"), null, "A8");
  231. Assert.AreEqual (p.ViewStateValue ("SelectedItem"), null, "A9");
  232. Assert.AreEqual (p.ViewStateValue ("SelectedValue"), null, "A10");
  233. #if NET_2_0
  234. Assert.AreEqual (p.ViewStateValue ("Text"), null, "A11");
  235. #endif
  236. }
  237. [Test]
  238. public void SelectedIndex ()
  239. {
  240. ListControlPoker p = new ListControlPoker ();
  241. p.Items.Add ("one");
  242. p.Items.Add ("two");
  243. p.Items.Add ("three");
  244. p.Items.Add ("four");
  245. p.Items [2].Selected = true;
  246. p.Items [1].Selected = true;
  247. Assert.AreEqual (p.SelectedIndex, 1, "A1");
  248. p.ClearSelection ();
  249. p.Items [3].Selected = true;
  250. Assert.AreEqual (p.SelectedIndex, 3, "A2");
  251. p.SelectedIndex = 1;
  252. Assert.AreEqual (p.SelectedIndex, 1, "A3");
  253. Assert.IsFalse (p.Items [3].Selected, "A4");
  254. }
  255. [Test]
  256. public void Render ()
  257. {
  258. ListControlPoker p = new ListControlPoker ();
  259. string s = p.Render ();
  260. string expected = "<select>\n\n</select>";
  261. Assert.AreEqual (s, expected, "A1");
  262. }
  263. [Test]
  264. [Category ("NotWorking")]
  265. #if !NET_2_0
  266. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  267. #endif
  268. public void ItemsTooHigh ()
  269. {
  270. ListControlPoker l = new ListControlPoker ();
  271. l.Items.Add ("foo");
  272. l.SelectedIndex = 1;
  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. class Data {
  346. string name;
  347. object val;
  348. public Data (string name, object val)
  349. {
  350. this.name = name;
  351. this.val = val;
  352. }
  353. public string Name {
  354. get { return name; }
  355. }
  356. public object Value {
  357. get { return val; }
  358. }
  359. }
  360. [Test]
  361. public void DataBinding5 ()
  362. {
  363. ListControlPoker p = new ListControlPoker ();
  364. p.DataTextField = "Name";
  365. p.DataValueField = "Value";
  366. ArrayList list = new ArrayList ();
  367. list.Add (new Data ("uno", 1));
  368. list.Add (new Data ("dos", 2));
  369. list.Add (new Data ("tres", 3));
  370. p.DataSource = list;
  371. p.SelectedIndex = 2;
  372. p.DataBind ();
  373. Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
  374. Assert.AreEqual ("tres", p.SelectedItem.Text, "#01");
  375. }
  376. [Test]
  377. public void DataBindingFormat1 ()
  378. {
  379. ListControlPoker p = new ListControlPoker ();
  380. ArrayList list = new ArrayList ();
  381. list.Add (1);
  382. list.Add (2);
  383. list.Add (3);
  384. p.DataSource = list;
  385. p.DataTextFormatString = "{0:00}";
  386. p.SelectedIndex = 2;
  387. p.DataBind ();
  388. Assert.AreEqual ("3", p.SelectedValue, "#01");
  389. }
  390. [Test]
  391. public void DataBindingFormat2 ()
  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 ("03", p.SelectedItem.Text, "#01");
  403. }
  404. [Test]
  405. [ExpectedException (typeof (NullReferenceException))]
  406. public void DataBindingFormat3 ()
  407. {
  408. ListControlPoker p = new ListControlPoker ();
  409. ArrayList list = new ArrayList ();
  410. list.Add (1);
  411. list.Add (null);
  412. list.Add (3);
  413. p.DataSource = list;
  414. p.DataTextFormatString = "{0:00}";
  415. p.SelectedIndex = 2;
  416. p.DataBind ();
  417. }
  418. private void BeginIndexChanged (ListControl l)
  419. {
  420. l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
  421. }
  422. private bool EndIndexChanged (ListControl l)
  423. {
  424. bool res = changed [l] != null;
  425. changed [l] = null;
  426. return res;
  427. }
  428. private void IndexChangedHandler (object sender, EventArgs e)
  429. {
  430. changed [sender] = new object ();
  431. }
  432. [Test]
  433. public void ValueFound1 ()
  434. {
  435. ListControlPoker p = new ListControlPoker ();
  436. p.Items.Add ("one");
  437. p.SelectedValue = "one";
  438. }
  439. [Test]
  440. [Category ("NotWorking")]
  441. public void ValueNotFound1 ()
  442. {
  443. ListControlPoker p = new ListControlPoker ();
  444. p.Items.Add ("one");
  445. p.SelectedValue = "dos";
  446. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  447. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  448. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  449. }
  450. [Test]
  451. public void ValueNotFound2 ()
  452. {
  453. ListControlPoker p = new ListControlPoker ();
  454. p.SelectedValue = "dos";
  455. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  456. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  457. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  458. }
  459. }
  460. }