ListBoxTest.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. namespace MonoTests.System.Web.UI.WebControls
  41. {
  42. class ListBoxPoker : ListBox {
  43. public ListBoxPoker ()
  44. {
  45. TrackViewState ();
  46. }
  47. public bool LoadPD (string key, NameValueCollection values)
  48. {
  49. return ((IPostBackDataHandler) this).LoadPostData (key, values);
  50. }
  51. public object SaveState ()
  52. {
  53. return SaveViewState ();
  54. }
  55. public void LoadState (object o)
  56. {
  57. LoadViewState (o);
  58. }
  59. public StateBag _ViewState {
  60. get { return ViewState; }
  61. }
  62. public string Render ()
  63. {
  64. StringWriter sw = new StringWriter ();
  65. sw.NewLine = "\n";
  66. HtmlTextWriter writer = new HtmlTextWriter (sw);
  67. base.Render (writer);
  68. return writer.InnerWriter.ToString ();
  69. }
  70. }
  71. [TestFixture]
  72. public class ListBoxTest {
  73. [Test]
  74. public void Defaults ()
  75. {
  76. ListBox lb = new ListBox ();
  77. Assert.AreEqual (lb.BorderColor, Color.Empty, "A1");
  78. Assert.AreEqual (lb.BorderStyle, BorderStyle.NotSet, "A2");
  79. Assert.AreEqual (lb.BorderWidth, Unit.Empty, "A3");
  80. Assert.AreEqual (lb.Rows, 4, "A4");
  81. Assert.AreEqual (lb.SelectionMode, ListSelectionMode.Single, "A5");
  82. Assert.AreEqual (lb.ToolTip, String.Empty, "A6");
  83. }
  84. [Test]
  85. public void SetProps ()
  86. {
  87. ListBox lb = new ListBox ();
  88. lb.BorderColor = Color.Black;
  89. Assert.AreEqual (lb.BorderColor, Color.Black, "A1");
  90. lb.BorderStyle = BorderStyle.Dashed;
  91. Assert.AreEqual (lb.BorderStyle, BorderStyle.Dashed, "A2");
  92. lb.BorderWidth = 0;
  93. Assert.AreEqual (lb.BorderWidth, (Unit) 0, "A3");
  94. lb.BorderWidth = 15;
  95. Assert.AreEqual (lb.BorderWidth, (Unit) 15, "A3");
  96. lb.Rows = 1;
  97. Assert.AreEqual (lb.Rows, 1, "A4");
  98. lb.SelectionMode = ListSelectionMode.Multiple;
  99. Assert.AreEqual (lb.SelectionMode, ListSelectionMode.Multiple, "A6");
  100. lb.ToolTip = "foo";
  101. #if NET_2_0
  102. Assert.AreEqual (lb.ToolTip, "foo", "A7");
  103. #else
  104. Assert.AreEqual (lb.ToolTip, String.Empty, "A7"); // Always empty in 1.x
  105. #endif
  106. }
  107. [Test]
  108. #if !NET_2_0
  109. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  110. #endif
  111. public void RowsTooHigh ()
  112. {
  113. ListBox lb = new ListBox ();
  114. lb.Rows = 2001;
  115. }
  116. [Test]
  117. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  118. public void RowsTooLow ()
  119. {
  120. ListBox lb = new ListBox ();
  121. lb.Rows = 0;
  122. }
  123. [Test]
  124. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  125. public void BadSelectionMode ()
  126. {
  127. ListBox lb = new ListBox ();
  128. lb.SelectionMode = (ListSelectionMode) 500;
  129. }
  130. [Test]
  131. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  132. public void BadBorderStyle ()
  133. {
  134. ListBox lb = new ListBox ();
  135. lb.BorderStyle = (BorderStyle) 500;
  136. }
  137. [Test]
  138. public void ViewState ()
  139. {
  140. ListBoxPoker p = new ListBoxPoker ();
  141. p.BorderColor = Color.Red;
  142. Assert.AreEqual (p._ViewState ["BorderColor"],
  143. Color.Red, "A1");
  144. p.BorderStyle = BorderStyle.Double;
  145. Assert.AreEqual (p._ViewState ["BorderStyle"],
  146. BorderStyle.Double, "A2");
  147. p.BorderWidth = 25;
  148. Assert.AreEqual (p._ViewState ["BorderWidth"],
  149. (Unit) 25, "A3");
  150. p.SelectionMode = ListSelectionMode.Multiple;
  151. Assert.AreEqual (p._ViewState ["SelectionMode"],
  152. ListSelectionMode.Multiple, "A4");
  153. }
  154. [Test]
  155. public void Render1 ()
  156. {
  157. ListBoxPoker l = new ListBoxPoker ();
  158. for (int i = 0; i < 3; i ++)
  159. l.Items.Add (i.ToString ());
  160. l.SelectedIndex = l.Items.Count - 1;
  161. #if NET_2_0
  162. string exp = @"<select size=""4"">
  163. <option value=""0"">0</option>
  164. <option value=""1"">1</option>
  165. <option selected=""selected"" value=""2"">2</option>
  166. </select>";
  167. #else
  168. string exp = @"<select name size=""4"">
  169. <option value=""0"">0</option>
  170. <option value=""1"">1</option>
  171. <option selected=""selected"" value=""2"">2</option>
  172. </select>";
  173. #endif
  174. Assert.AreEqual (exp, l.Render ());
  175. }
  176. DataSet GetExampleData ()
  177. {
  178. DataSet ds = new DataSet ();
  179. ds.ReadXml (new StringReader (@"
  180. <DataSet>
  181. <Stocks Company='Novell Inc.' Symbol='NOVL' Price='6.14' />
  182. <Stocks Company='Microsoft Corp.' Symbol='MSFT' Price='25.92' />
  183. <Stocks Company='Google' Symbol='GOOG' Price='291.60' />
  184. </DataSet>
  185. "));
  186. return ds;
  187. }
  188. [Test]
  189. public void DoubleDataBind ()
  190. {
  191. ListBoxPoker l = new ListBoxPoker ();
  192. l.DataSource = GetExampleData ();
  193. l.DataTextField = "Company";
  194. l.DataBind ();
  195. l.DataBind ();
  196. #if NET_2_0
  197. string exp = @"<select size=""4"">
  198. <option value=""Novell Inc."">Novell Inc.</option>
  199. <option value=""Microsoft Corp."">Microsoft Corp.</option>
  200. <option value=""Google"">Google</option>
  201. </select>";
  202. #else
  203. string exp = @"<select name size=""4"">
  204. <option value=""Novell Inc."">Novell Inc.</option>
  205. <option value=""Microsoft Corp."">Microsoft Corp.</option>
  206. <option value=""Google"">Google</option>
  207. </select>";
  208. #endif
  209. Assert.AreEqual (exp, l.Render ());
  210. }
  211. class MyNC : Control, INamingContainer {
  212. }
  213. [Test]
  214. public void NameIsUniqueID ()
  215. {
  216. ListBoxPoker list = new ListBoxPoker ();
  217. Page page = new Page ();
  218. page.ID = "pg";
  219. Control ctrl = new MyNC ();
  220. ctrl.ID = "ctrl";
  221. page.Controls.Add (ctrl);
  222. ctrl.Controls.Add (list);
  223. Assert.IsTrue (-1 != list.Render ().IndexOf (':'), "unique");
  224. }
  225. [Test]
  226. public void HtmlEncodedText ()
  227. {
  228. ListBoxPoker list = new ListBoxPoker ();
  229. // The att. value is encoded by the writer, but the text is encoded in ListBox.
  230. list.Items.Add (new ListItem ("\"hola", "\"adios"));
  231. string output = list.Render ();
  232. Assert.IsTrue (-1 != output.IndexOf ("&quot;hola"), "#01");
  233. Assert.IsTrue (-1 != output.IndexOf ("&quot;adios"), "#02");
  234. }
  235. [Test]
  236. public void SelectSingle1 ()
  237. {
  238. ListBoxPoker list = new ListBoxPoker ();
  239. list.Items.Add (new ListItem ("1", "first"));
  240. list.Items.Add (new ListItem ("2", "second"));
  241. list.SelectedIndex = 0;
  242. NameValueCollection coll = new NameValueCollection ();
  243. coll.Add ("2", "second");
  244. Assert.IsTrue (list.LoadPD ("2", coll), "#00");
  245. Assert.IsFalse (list.Items [0].Selected, "#01");
  246. Assert.IsTrue (list.Items [1].Selected, "#02");
  247. Assert.AreEqual (1, list.SelectedIndex, "#03");
  248. }
  249. [Test]
  250. public void SelectSingle2 ()
  251. {
  252. ListBoxPoker list = new ListBoxPoker ();
  253. list.Items.Add (new ListItem ("1", "first"));
  254. list.Items.Add (new ListItem ("2", "second"));
  255. list.SelectedIndex = 0;
  256. NameValueCollection coll = new NameValueCollection ();
  257. coll.Add ("willnotbefound", "second");
  258. Assert.IsTrue (list.LoadPD ("2", coll), "#00");
  259. Assert.IsFalse (list.Items [0].Selected, "#01");
  260. Assert.IsFalse (list.Items [1].Selected, "#02");
  261. Assert.AreEqual (-1, list.SelectedIndex, "#03");
  262. }
  263. [Test]
  264. public void SelectMultiple1 ()
  265. {
  266. ListBoxPoker list = new ListBoxPoker ();
  267. list.SelectionMode = ListSelectionMode.Multiple;
  268. list.Items.Add (new ListItem ("1", "first"));
  269. list.Items.Add (new ListItem ("2", "second"));
  270. list.SelectedIndex = 0;
  271. NameValueCollection coll = new NameValueCollection ();
  272. coll.Add ("2", "second");
  273. Assert.IsTrue (list.LoadPD ("2", coll), "#00");
  274. Assert.IsFalse (list.Items [0].Selected, "#01");
  275. Assert.IsTrue (list.Items [1].Selected, "#02");
  276. Assert.AreEqual (1, list.SelectedIndex, "#03");
  277. }
  278. [Test]
  279. public void SelectMultiple2 ()
  280. {
  281. ListBoxPoker list = new ListBoxPoker ();
  282. list.SelectionMode = ListSelectionMode.Multiple;
  283. list.Items.Add (new ListItem ("1", "first"));
  284. list.Items.Add (new ListItem ("2", "second"));
  285. list.Items.Add (new ListItem ("3", "third"));
  286. list.Items.Add (new ListItem ("4", "forth"));
  287. NameValueCollection coll = new NameValueCollection ();
  288. coll.Add ("key", "second");
  289. coll.Add ("key", "forth");
  290. Assert.IsTrue (list.LoadPD ("key", coll), "#00");
  291. Assert.IsFalse (list.Items [0].Selected, "#01");
  292. Assert.IsTrue (list.Items [1].Selected, "#02");
  293. Assert.IsFalse (list.Items [2].Selected, "#03");
  294. Assert.IsTrue (list.Items [3].Selected, "#04");
  295. Assert.IsFalse (list.LoadPD ("key", coll), "#05");
  296. Assert.IsFalse (list.Items [0].Selected, "#06");
  297. Assert.IsTrue (list.Items [1].Selected, "#07");
  298. Assert.IsFalse (list.Items [2].Selected, "#08");
  299. Assert.IsTrue (list.Items [3].Selected, "#09");
  300. coll.Clear ();
  301. coll.Add ("key", "first");
  302. coll.Add ("key", "third");
  303. Assert.IsTrue (list.LoadPD ("key", coll), "#10");
  304. Assert.IsTrue (list.Items [0].Selected, "#11");
  305. Assert.IsFalse (list.Items [1].Selected, "#12");
  306. Assert.IsTrue (list.Items [2].Selected, "#13");
  307. Assert.IsFalse (list.Items [3].Selected, "#14");
  308. }
  309. }
  310. }