ListBoxTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //
  2. // Tests for System.Web.UI.WebControls.ListBoxTest.cs
  3. //
  4. // Author:
  5. // Jackson Harper ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. //
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using NUnit.Framework;
  31. using System;
  32. using System.IO;
  33. using System.Drawing;
  34. using System.Collections.Specialized;
  35. using System.Globalization;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. using System.Data;
  40. using MonoTests.stand_alone.WebHarness;
  41. namespace MonoTests.System.Web.UI.WebControls
  42. {
  43. class ListBoxPoker : ListBox {
  44. public ListBoxPoker ()
  45. {
  46. TrackViewState ();
  47. }
  48. public bool LoadPD (string key, NameValueCollection values)
  49. {
  50. return ((IPostBackDataHandler) this).LoadPostData (key, values);
  51. }
  52. public object SaveState ()
  53. {
  54. return SaveViewState ();
  55. }
  56. public void LoadState (object o)
  57. {
  58. LoadViewState (o);
  59. }
  60. public StateBag _ViewState {
  61. get { return ViewState; }
  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 new virtual void VerifyMultiSelect()
  73. {
  74. base.VerifyMultiSelect();
  75. }
  76. #endif
  77. }
  78. [TestFixture]
  79. public class ListBoxTest {
  80. [Test]
  81. public void Defaults ()
  82. {
  83. ListBox lb = new ListBox ();
  84. Assert.AreEqual (lb.BorderColor, Color.Empty, "A1");
  85. Assert.AreEqual (lb.BorderStyle, BorderStyle.NotSet, "A2");
  86. Assert.AreEqual (lb.BorderWidth, Unit.Empty, "A3");
  87. Assert.AreEqual (lb.Rows, 4, "A4");
  88. Assert.AreEqual (lb.SelectionMode, ListSelectionMode.Single, "A5");
  89. Assert.AreEqual (lb.ToolTip, String.Empty, "A6");
  90. }
  91. [Test]
  92. public void SetProps ()
  93. {
  94. ListBox lb = new ListBox ();
  95. lb.BorderColor = Color.Black;
  96. Assert.AreEqual (lb.BorderColor, Color.Black, "A1");
  97. lb.BorderStyle = BorderStyle.Dashed;
  98. Assert.AreEqual (lb.BorderStyle, BorderStyle.Dashed, "A2");
  99. lb.BorderWidth = 0;
  100. Assert.AreEqual (lb.BorderWidth, (Unit) 0, "A3");
  101. lb.BorderWidth = 15;
  102. Assert.AreEqual (lb.BorderWidth, (Unit) 15, "A3");
  103. lb.Rows = 1;
  104. Assert.AreEqual (lb.Rows, 1, "A4");
  105. lb.SelectionMode = ListSelectionMode.Multiple;
  106. Assert.AreEqual (lb.SelectionMode, ListSelectionMode.Multiple, "A6");
  107. lb.ToolTip = "foo";
  108. #if NET_2_0
  109. Assert.AreEqual (lb.ToolTip, "foo", "A7");
  110. #else
  111. Assert.AreEqual (lb.ToolTip, String.Empty, "A7"); // Always empty in 1.x
  112. #endif
  113. }
  114. [Test]
  115. [Category ("NotWorking")]
  116. #if !NET_2_0
  117. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  118. #endif
  119. public void RowsTooHigh ()
  120. {
  121. ListBox lb = new ListBox ();
  122. lb.Rows = 2001;
  123. }
  124. [Test]
  125. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  126. public void RowsTooLow ()
  127. {
  128. ListBox lb = new ListBox ();
  129. lb.Rows = 0;
  130. }
  131. [Test]
  132. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  133. public void BadSelectionMode ()
  134. {
  135. ListBox lb = new ListBox ();
  136. lb.SelectionMode = (ListSelectionMode) 500;
  137. }
  138. [Test]
  139. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  140. public void BadBorderStyle ()
  141. {
  142. ListBox lb = new ListBox ();
  143. lb.BorderStyle = (BorderStyle) 500;
  144. }
  145. [Test]
  146. public void ViewState ()
  147. {
  148. ListBoxPoker p = new ListBoxPoker ();
  149. p.BorderColor = Color.Red;
  150. Assert.AreEqual (p._ViewState ["BorderColor"],
  151. Color.Red, "A1");
  152. p.BorderStyle = BorderStyle.Double;
  153. Assert.AreEqual (p._ViewState ["BorderStyle"],
  154. BorderStyle.Double, "A2");
  155. p.BorderWidth = 25;
  156. Assert.AreEqual (p._ViewState ["BorderWidth"],
  157. (Unit) 25, "A3");
  158. p.SelectionMode = ListSelectionMode.Multiple;
  159. Assert.AreEqual (p._ViewState ["SelectionMode"],
  160. ListSelectionMode.Multiple, "A4");
  161. }
  162. [Test]
  163. public void Render1 ()
  164. {
  165. ListBoxPoker l = new ListBoxPoker ();
  166. for (int i = 0; i < 3; i ++)
  167. l.Items.Add (i.ToString ());
  168. l.SelectedIndex = l.Items.Count - 1;
  169. #if NET_2_0
  170. string exp = @"<select size=""4"">
  171. <option value=""0"">0</option>
  172. <option value=""1"">1</option>
  173. <option selected=""selected"" value=""2"">2</option>
  174. </select>";
  175. #else
  176. string exp = @"<select name size=""4"">
  177. <option value=""0"">0</option>
  178. <option value=""1"">1</option>
  179. <option selected=""selected"" value=""2"">2</option>
  180. </select>";
  181. #endif
  182. HtmlDiff.AssertAreEqual (exp, l.Render (), "Render1");
  183. }
  184. DataSet GetExampleData ()
  185. {
  186. DataSet ds = new DataSet ();
  187. ds.ReadXml (new StringReader (@"
  188. <DataSet>
  189. <Stocks Company='Novell Inc.' Symbol='NOVL' Price='6.14' />
  190. <Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92' />
  191. <Stocks Company='Google' Symbol='GOOG' Price='291.60' />
  192. </DataSet>
  193. "));
  194. return ds;
  195. }
  196. [Test]
  197. public void DoubleDataBind ()
  198. {
  199. ListBoxPoker l = new ListBoxPoker ();
  200. l.DataSource = GetExampleData ();
  201. l.DataTextField = "Company";
  202. l.DataBind ();
  203. l.DataBind ();
  204. #if NET_2_0
  205. string exp = @"<select size=""4"">
  206. <option value=""Novell Inc."">Novell Inc.</option>
  207. <option value=""Microsoft Corp."">Microsoft Corp.</option>
  208. <option value=""Google"">Google</option>
  209. </select>";
  210. #else
  211. string exp = @"<select name size=""4"">
  212. <option value=""Novell Inc."">Novell Inc.</option>
  213. <option value=""Microsoft Corp."">Microsoft Corp.</option>
  214. <option value=""Google"">Google</option>
  215. </select>";
  216. #endif
  217. HtmlDiff.AssertAreEqual (exp, l.Render (), "DoubleDataBind");
  218. }
  219. class MyNC : Control, INamingContainer {
  220. }
  221. [Test]
  222. [Category ("NotDotNet")]
  223. [Category ("NotWorking")]
  224. public void NameIsUniqueID ()
  225. {
  226. ListBoxPoker list = new ListBoxPoker ();
  227. Page page = new Page ();
  228. page.ID = "pg";
  229. Control ctrl = new MyNC ();
  230. ctrl.ID = "ctrl";
  231. page.Controls.Add (ctrl);
  232. ctrl.Controls.Add (list);
  233. string str = list.Render();
  234. Assert.IsTrue (-1 != list.Render ().IndexOf (':'), "unique");
  235. }
  236. [Test]
  237. public void HtmlEncodedText ()
  238. {
  239. ListBoxPoker list = new ListBoxPoker ();
  240. // The att. value is encoded by the writer, but the text is encoded in ListBox.
  241. list.Items.Add (new ListItem ("\"hola", "\"adios"));
  242. string output = list.Render ();
  243. Assert.IsTrue (-1 != output.IndexOf ("&quot;hola"), "#01");
  244. Assert.IsTrue (-1 != output.IndexOf ("&quot;adios"), "#02");
  245. }
  246. [Test]
  247. public void SelectSingle1 ()
  248. {
  249. ListBoxPoker list = new ListBoxPoker ();
  250. list.Items.Add (new ListItem ("1", "first"));
  251. list.Items.Add (new ListItem ("2", "second"));
  252. list.SelectedIndex = 0;
  253. NameValueCollection coll = new NameValueCollection ();
  254. coll.Add ("2", "second");
  255. Assert.IsTrue (list.LoadPD ("2", coll), "#00");
  256. Assert.IsFalse (list.Items [0].Selected, "#01");
  257. Assert.IsTrue (list.Items [1].Selected, "#02");
  258. Assert.AreEqual (1, list.SelectedIndex, "#03");
  259. }
  260. [Test]
  261. public void SelectSingle2 ()
  262. {
  263. ListBoxPoker list = new ListBoxPoker ();
  264. list.Items.Add (new ListItem ("1", "first"));
  265. list.Items.Add (new ListItem ("2", "second"));
  266. list.SelectedIndex = 0;
  267. NameValueCollection coll = new NameValueCollection ();
  268. coll.Add ("willnotbefound", "second");
  269. Assert.IsTrue (list.LoadPD ("2", coll), "#00");
  270. Assert.IsFalse (list.Items [0].Selected, "#01");
  271. Assert.IsFalse (list.Items [1].Selected, "#02");
  272. Assert.AreEqual (-1, list.SelectedIndex, "#03");
  273. }
  274. [Test]
  275. public void SelectMultiple1 ()
  276. {
  277. ListBoxPoker list = new ListBoxPoker ();
  278. list.SelectionMode = ListSelectionMode.Multiple;
  279. list.Items.Add (new ListItem ("1", "first"));
  280. list.Items.Add (new ListItem ("2", "second"));
  281. list.SelectedIndex = 0;
  282. NameValueCollection coll = new NameValueCollection ();
  283. coll.Add ("2", "second");
  284. Assert.IsTrue (list.LoadPD ("2", coll), "#00");
  285. Assert.IsFalse (list.Items [0].Selected, "#01");
  286. Assert.IsTrue (list.Items [1].Selected, "#02");
  287. Assert.AreEqual (1, list.SelectedIndex, "#03");
  288. }
  289. [Test]
  290. public void SelectMultiple2 ()
  291. {
  292. ListBoxPoker list = new ListBoxPoker ();
  293. list.SelectionMode = ListSelectionMode.Multiple;
  294. list.Items.Add (new ListItem ("1", "first"));
  295. list.Items.Add (new ListItem ("2", "second"));
  296. list.Items.Add (new ListItem ("3", "third"));
  297. list.Items.Add (new ListItem ("4", "forth"));
  298. NameValueCollection coll = new NameValueCollection ();
  299. coll.Add ("key", "second");
  300. coll.Add ("key", "forth");
  301. Assert.IsTrue (list.LoadPD ("key", coll), "#00");
  302. Assert.IsFalse (list.Items [0].Selected, "#01");
  303. Assert.IsTrue (list.Items [1].Selected, "#02");
  304. Assert.IsFalse (list.Items [2].Selected, "#03");
  305. Assert.IsTrue (list.Items [3].Selected, "#04");
  306. Assert.IsFalse (list.LoadPD ("key", coll), "#05");
  307. Assert.IsFalse (list.Items [0].Selected, "#06");
  308. Assert.IsTrue (list.Items [1].Selected, "#07");
  309. Assert.IsFalse (list.Items [2].Selected, "#08");
  310. Assert.IsTrue (list.Items [3].Selected, "#09");
  311. coll.Clear ();
  312. coll.Add ("key", "first");
  313. coll.Add ("key", "third");
  314. Assert.IsTrue (list.LoadPD ("key", coll), "#10");
  315. Assert.IsTrue (list.Items [0].Selected, "#11");
  316. Assert.IsFalse (list.Items [1].Selected, "#12");
  317. Assert.IsTrue (list.Items [2].Selected, "#13");
  318. Assert.IsFalse (list.Items [3].Selected, "#14");
  319. }
  320. #if NET_2_0
  321. [Test]
  322. public void VerifyMultiSelectPositive()
  323. {
  324. ListBoxPoker list = new ListBoxPoker();
  325. list.SelectionMode = ListSelectionMode.Multiple;
  326. list.VerifyMultiSelect();
  327. }
  328. [Test]
  329. [ExpectedException(typeof(HttpException))]
  330. public void VerifyMultiSelectNegative()
  331. {
  332. ListBoxPoker list = new ListBoxPoker();
  333. list.SelectionMode = ListSelectionMode.Single;
  334. list.VerifyMultiSelect();
  335. }
  336. #endif
  337. }
  338. }