ListControlTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. [Category ("NotWorking")]
  200. public void ViewStateContents ()
  201. {
  202. ListControlPoker p = new ListControlPoker ();
  203. p.TrackState ();
  204. // So the selected index can be set
  205. p.Items.Add ("one");
  206. p.Items.Add ("two");
  207. p.AutoPostBack = false;
  208. p.DataMember = "DataMember";
  209. p.DataSource = "DataSource";
  210. p.DataTextField = "DataTextField";
  211. p.DataTextFormatString = "DataTextFormatString";
  212. p.DataValueField = "DataValueField";
  213. p.SelectedIndex = 1;
  214. #if NET_2_0
  215. p.AppendDataBoundItems = true;
  216. p.Text = "Text";
  217. #endif
  218. Assert.AreEqual (p.ViewStateValue ("AutoPostBack"), false, "A1");
  219. Assert.AreEqual (p.ViewStateValue ("DataMember"), "DataMember", "A2");
  220. Assert.AreEqual (p.ViewStateValue ("DataSource"), null, "A3");
  221. Assert.AreEqual (p.ViewStateValue ("DataTextField"), "DataTextField", "A4");
  222. Assert.AreEqual (p.ViewStateValue ("DataTextFormatString"),
  223. "DataTextFormatString", "A5");
  224. Assert.AreEqual (p.ViewStateValue ("DataValueField"), "DataValueField", "A6");
  225. #if NET_2_0
  226. Assert.AreEqual (p.ViewStateValue ("AppendDataBoundItems"), true, "A7");
  227. #endif
  228. // None of these are saved
  229. Assert.AreEqual (p.ViewStateValue ("SelectedIndex"), null, "A8");
  230. Assert.AreEqual (p.ViewStateValue ("SelectedItem"), null, "A9");
  231. Assert.AreEqual (p.ViewStateValue ("SelectedValue"), null, "A10");
  232. #if NET_2_0
  233. Assert.AreEqual (p.ViewStateValue ("Text"), null, "A11");
  234. #endif
  235. }
  236. [Test]
  237. public void SelectedIndex ()
  238. {
  239. ListControlPoker p = new ListControlPoker ();
  240. p.Items.Add ("one");
  241. p.Items.Add ("two");
  242. p.Items.Add ("three");
  243. p.Items.Add ("four");
  244. p.Items [2].Selected = true;
  245. p.Items [1].Selected = true;
  246. Assert.AreEqual (p.SelectedIndex, 1, "A1");
  247. p.ClearSelection ();
  248. p.Items [3].Selected = true;
  249. Assert.AreEqual (p.SelectedIndex, 3, "A2");
  250. p.SelectedIndex = 1;
  251. Assert.AreEqual (p.SelectedIndex, 1, "A3");
  252. Assert.IsFalse (p.Items [3].Selected, "A4");
  253. }
  254. [Test]
  255. public void Render ()
  256. {
  257. ListControlPoker p = new ListControlPoker ();
  258. string s = p.Render ();
  259. string expected = "<select>\n\n</select>";
  260. Assert.AreEqual (s, expected, "A1");
  261. }
  262. [Test]
  263. #if !NET_2_0
  264. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  265. #endif
  266. public void ItemsTooHigh ()
  267. {
  268. ListControlPoker l = new ListControlPoker ();
  269. l.Items.Add ("foo");
  270. l.SelectedIndex = 1;
  271. #if NET_2_0
  272. Assert.AreEqual (-1, l.SelectedIndex, "#01");
  273. #endif
  274. }
  275. [Test]
  276. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  277. public void ItemsTooLow ()
  278. {
  279. ListControlPoker l = new ListControlPoker ();
  280. l.Items.Add ("foo");
  281. l.SelectedIndex = -2;
  282. }
  283. [Test]
  284. public void ItemsOk ()
  285. {
  286. ListControlPoker l = new ListControlPoker ();
  287. l.Items.Add ("foo");
  288. l.SelectedIndex = 0;
  289. l.SelectedIndex = -1;
  290. }
  291. [Test]
  292. public void DataBinding1 ()
  293. {
  294. ListControlPoker p = new ListControlPoker ();
  295. ArrayList list = new ArrayList ();
  296. list.Add (1);
  297. list.Add (2);
  298. list.Add (3);
  299. p.DataSource = list;
  300. p.SelectedIndex = 2;
  301. p.DataBind ();
  302. Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
  303. }
  304. [Test]
  305. [ExpectedException (typeof (ArgumentException))] // The SelectedIndex and SelectedValue are mutually exclusive
  306. public void DataBinding2 ()
  307. {
  308. ListControlPoker p = new ListControlPoker ();
  309. ArrayList list = new ArrayList ();
  310. list.Add (1);
  311. list.Add (2);
  312. list.Add (3);
  313. p.DataSource = list;
  314. p.SelectedIndex = 0;
  315. p.SelectedValue = "3";
  316. p.DataBind ();
  317. }
  318. [Test]
  319. public void DataBinding3 ()
  320. {
  321. ListControlPoker p = new ListControlPoker ();
  322. ArrayList list = new ArrayList ();
  323. list.Add (1);
  324. list.Add (2);
  325. list.Add (3);
  326. p.DataSource = list;
  327. // If Index and Value are used, everything's ok if they are selecting
  328. // the same thing.
  329. p.SelectedIndex = 2;
  330. p.SelectedValue = "3";
  331. p.DataBind ();
  332. Assert.AreEqual ("3", p.SelectedValue, "#01");
  333. }
  334. [Test]
  335. [ExpectedException (typeof (NullReferenceException))]
  336. public void DataBinding4 ()
  337. {
  338. ListControlPoker p = new ListControlPoker ();
  339. ArrayList list = new ArrayList ();
  340. list.Add (1);
  341. list.Add (null);
  342. list.Add (3);
  343. p.DataSource = list;
  344. p.DataBind ();
  345. }
  346. class Data {
  347. string name;
  348. object val;
  349. public Data (string name, object val)
  350. {
  351. this.name = name;
  352. this.val = val;
  353. }
  354. public string Name {
  355. get { return name; }
  356. }
  357. public object Value {
  358. get { return val; }
  359. }
  360. }
  361. [Test]
  362. public void DataBinding5 ()
  363. {
  364. ListControlPoker p = new ListControlPoker ();
  365. p.DataTextField = "Name";
  366. p.DataValueField = "Value";
  367. ArrayList list = new ArrayList ();
  368. list.Add (new Data ("uno", 1));
  369. list.Add (new Data ("dos", 2));
  370. list.Add (new Data ("tres", 3));
  371. p.DataSource = list;
  372. p.SelectedIndex = 2;
  373. p.DataBind ();
  374. Assert.AreEqual (3.ToString (), p.SelectedValue, "#01");
  375. Assert.AreEqual ("tres", p.SelectedItem.Text, "#01");
  376. }
  377. [Test]
  378. public void DataBindingFormat1 ()
  379. {
  380. ListControlPoker p = new ListControlPoker ();
  381. ArrayList list = new ArrayList ();
  382. list.Add (1);
  383. list.Add (2);
  384. list.Add (3);
  385. p.DataSource = list;
  386. p.DataTextFormatString = "{0:00}";
  387. p.SelectedIndex = 2;
  388. p.DataBind ();
  389. Assert.AreEqual ("3", p.SelectedValue, "#01");
  390. }
  391. [Test]
  392. public void DataBindingFormat2 ()
  393. {
  394. ListControlPoker p = new ListControlPoker ();
  395. ArrayList list = new ArrayList ();
  396. list.Add (1);
  397. list.Add (2);
  398. list.Add (3);
  399. p.DataSource = list;
  400. p.DataTextFormatString = "{0:00}";
  401. p.SelectedIndex = 2;
  402. p.DataBind ();
  403. Assert.AreEqual ("03", p.SelectedItem.Text, "#01");
  404. }
  405. [Test]
  406. [ExpectedException (typeof (NullReferenceException))]
  407. public void DataBindingFormat3 ()
  408. {
  409. ListControlPoker p = new ListControlPoker ();
  410. ArrayList list = new ArrayList ();
  411. list.Add (1);
  412. list.Add (null);
  413. list.Add (3);
  414. p.DataSource = list;
  415. p.DataTextFormatString = "{0:00}";
  416. p.SelectedIndex = 2;
  417. p.DataBind ();
  418. }
  419. private void BeginIndexChanged (ListControl l)
  420. {
  421. l.SelectedIndexChanged += new EventHandler (IndexChangedHandler);
  422. }
  423. private bool EndIndexChanged (ListControl l)
  424. {
  425. bool res = changed [l] != null;
  426. changed [l] = null;
  427. return res;
  428. }
  429. private void IndexChangedHandler (object sender, EventArgs e)
  430. {
  431. changed [sender] = new object ();
  432. }
  433. [Test]
  434. public void ValueFound1 ()
  435. {
  436. ListControlPoker p = new ListControlPoker ();
  437. p.Items.Add ("one");
  438. p.SelectedValue = "one";
  439. }
  440. [Test]
  441. [Category ("NotWorking")]
  442. public void ValueNotFound1 ()
  443. {
  444. ListControlPoker p = new ListControlPoker ();
  445. p.Items.Add ("one");
  446. p.SelectedValue = "dos";
  447. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  448. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  449. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  450. }
  451. [Test]
  452. public void ValueNotFound2 ()
  453. {
  454. ListControlPoker p = new ListControlPoker ();
  455. p.SelectedValue = "dos";
  456. Assert.AreEqual("", p.SelectedValue, "SelectedValue");
  457. Assert.AreEqual(null, p.SelectedItem, "SelectedItem");
  458. Assert.AreEqual(-1, p.SelectedIndex, "SelectedIndex");
  459. }
  460. }
  461. }