CheckBoxListTest.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // Tests for System.Web.UI.WebControls.CheckBoxList.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.Globalization;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Web.UI.WebControls;
  36. namespace MonoTests.System.Web.UI.WebControls {
  37. public class CheckBoxListPoker : CheckBoxList {
  38. public Style CreateStyle ()
  39. {
  40. return CreateControlStyle ();
  41. }
  42. public Control FindControlPoke (string name, int offset)
  43. {
  44. return FindControl (name, offset);
  45. }
  46. }
  47. [TestFixture]
  48. public class CheckBoxListTest {
  49. [Test]
  50. public void Defaults ()
  51. {
  52. CheckBoxList c = new CheckBoxList ();
  53. Assert.AreEqual (c.CellPadding, -1, "A1");
  54. Assert.AreEqual (c.CellSpacing, -1, "A2");
  55. Assert.AreEqual (c.RepeatColumns, 0, "A3");
  56. Assert.AreEqual (c.RepeatDirection,
  57. RepeatDirection.Vertical, "A4");
  58. Assert.AreEqual (c.RepeatLayout,
  59. RepeatLayout.Table, "A5");
  60. Assert.AreEqual (c.TextAlign, TextAlign.Right, "A6");
  61. }
  62. [Test]
  63. public void CleanProperties ()
  64. {
  65. CheckBoxList c = new CheckBoxList ();
  66. c.CellPadding = Int32.MaxValue;
  67. Assert.AreEqual (c.CellPadding, Int32.MaxValue, "A1");
  68. c.CellSpacing = Int32.MaxValue;
  69. Assert.AreEqual (c.CellSpacing, Int32.MaxValue, "A2");
  70. c.RepeatColumns = Int32.MaxValue;
  71. Assert.AreEqual (c.RepeatColumns, Int32.MaxValue, "A3");
  72. foreach (RepeatDirection d in
  73. Enum.GetValues (typeof (RepeatDirection))) {
  74. c.RepeatDirection = d;
  75. Assert.AreEqual (c.RepeatDirection, d, "A4-" + d);
  76. }
  77. foreach (RepeatLayout l in
  78. Enum.GetValues (typeof (RepeatLayout))) {
  79. c.RepeatLayout = l;
  80. Assert.AreEqual (c.RepeatLayout, l, "A5-" + l);
  81. }
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  85. public void CellPaddingTooLow ()
  86. {
  87. CheckBoxList c = new CheckBoxList ();
  88. c.CellPadding = -2;
  89. }
  90. [Test]
  91. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  92. public void CellSpacingTooLow ()
  93. {
  94. CheckBoxList c = new CheckBoxList ();
  95. c.CellSpacing = -2;
  96. }
  97. [Test]
  98. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  99. public void RepeatColumsTooLow ()
  100. {
  101. CheckBoxList c = new CheckBoxList ();
  102. c.RepeatColumns = -1;
  103. }
  104. [Test]
  105. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  106. public void RepeatDirection_Invalid ()
  107. {
  108. CheckBoxList c = new CheckBoxList ();
  109. c.RepeatDirection = (RepeatDirection) Int32.MaxValue;
  110. }
  111. [Test]
  112. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  113. public void RepeatLayout_Invalid ()
  114. {
  115. CheckBoxList c = new CheckBoxList ();
  116. c.RepeatLayout = (RepeatLayout) Int32.MaxValue;
  117. }
  118. [Test]
  119. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  120. public void TextAlign_Invalid ()
  121. {
  122. CheckBoxList c = new CheckBoxList ();
  123. c.TextAlign = (TextAlign) Int32.MaxValue;
  124. }
  125. [Test]
  126. public void ChildCheckBoxControl ()
  127. {
  128. CheckBoxList c = new CheckBoxList ();
  129. Assert.AreEqual (c.Controls.Count, 1, "A1");
  130. Assert.AreEqual (c.Controls [0].GetType (), typeof (CheckBox), "A2");
  131. }
  132. [Test]
  133. public void CreateStyle ()
  134. {
  135. CheckBoxListPoker c = new CheckBoxListPoker ();
  136. Assert.AreEqual (c.CreateStyle ().GetType (), typeof (TableStyle), "A1");
  137. }
  138. [Test]
  139. public void RepeatInfoProperties ()
  140. {
  141. IRepeatInfoUser ri = new CheckBoxList ();
  142. Assert.IsFalse (ri.HasFooter, "A1");
  143. Assert.IsFalse (ri.HasHeader, "A2");
  144. Assert.IsFalse (ri.HasSeparators, "A3");
  145. Assert.AreEqual (ri.RepeatedItemCount, 0, "A4");
  146. }
  147. [Test]
  148. public void RepeatInfoCount ()
  149. {
  150. CheckBoxList c = new CheckBoxList ();
  151. IRepeatInfoUser ri = (IRepeatInfoUser) c;
  152. Assert.AreEqual (ri.RepeatedItemCount, 0, "A1");
  153. c.Items.Add ("one");
  154. c.Items.Add ("two");
  155. c.Items.Add ("three");
  156. Assert.AreEqual (ri.RepeatedItemCount, 3, "A2");
  157. }
  158. [Test]
  159. public void RepeatInfoStyle ()
  160. {
  161. IRepeatInfoUser ri = new CheckBoxList ();
  162. foreach (ListItemType t in Enum.GetValues (typeof (ListItemType))) {
  163. Assert.AreEqual (ri.GetItemStyle (t, 0), null, "A1-" + t);
  164. Assert.AreEqual (ri.GetItemStyle (t, 1), null, "A2-" + t);
  165. Assert.AreEqual (ri.GetItemStyle (t, 2), null, "A3-" + t);
  166. Assert.AreEqual (ri.GetItemStyle (t, 3), null, "A4-" + t);
  167. }
  168. }
  169. [Test]
  170. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  171. public void RepeatInfoRenderOutOfRange ()
  172. {
  173. StringWriter sw = new StringWriter ();
  174. HtmlTextWriter tw = new HtmlTextWriter (sw);
  175. IRepeatInfoUser ri = new CheckBoxList ();
  176. ri.RenderItem (ListItemType.Item, -1, new RepeatInfo (), tw);
  177. }
  178. [Test]
  179. public void RepeatInfoRenderItem ()
  180. {
  181. StringWriter sw = new StringWriter ();
  182. HtmlTextWriter tw = new HtmlTextWriter (sw);
  183. CheckBoxList c = new CheckBoxList ();
  184. IRepeatInfoUser ri = (IRepeatInfoUser) c;
  185. RepeatInfo r = new RepeatInfo ();
  186. c.Items.Add ("one");
  187. c.Items.Add ("two");
  188. ri.RenderItem (ListItemType.Item, 0, r, tw);
  189. Assert.AreEqual ("<input id=\"0\" type=\"checkbox\" name=\"0\" />" +
  190. "<label for=\"0\">one</label>", sw.ToString (), "A1");
  191. }
  192. [Test]
  193. public void FindControl ()
  194. {
  195. CheckBoxListPoker p = new CheckBoxListPoker ();
  196. p.ID = "id";
  197. p.Items.Add ("one");
  198. p.Items.Add ("two");
  199. p.Items.Add ("three");
  200. // Everything seems to return this.
  201. Assert.AreEqual (p.FindControlPoke (String.Empty, 0), p, "A1");
  202. Assert.AreEqual (p.FindControlPoke ("id", 0), p, "A2");
  203. Assert.AreEqual (p.FindControlPoke ("id_0", 0), p, "A3");
  204. Assert.AreEqual (p.FindControlPoke ("id_1", 0), p, "A4");
  205. Assert.AreEqual (p.FindControlPoke ("id_2", 0), p, "A5");
  206. Assert.AreEqual (p.FindControlPoke ("id_3", 0), p, "A6");
  207. Assert.AreEqual (p.FindControlPoke ("0", 0), p, "A7");
  208. Assert.AreEqual (p.FindControlPoke (String.Empty, 10), p, "A1");
  209. Assert.AreEqual (p.FindControlPoke ("id", 10), p, "A2");
  210. Assert.AreEqual (p.FindControlPoke ("id_0", 10), p, "A3");
  211. Assert.AreEqual (p.FindControlPoke ("id_1", 10), p, "A4");
  212. Assert.AreEqual (p.FindControlPoke ("id_2", 10), p, "A5");
  213. Assert.AreEqual (p.FindControlPoke ("id_3", 10), p, "A6");
  214. Assert.AreEqual (p.FindControlPoke ("0", 10), p, "A7");
  215. }
  216. private void Render (CheckBoxList list, string expected, string test)
  217. {
  218. StringWriter sw = new StringWriter ();
  219. HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
  220. sw.NewLine = "\n";
  221. list.RenderControl (tw);
  222. Assert.AreEqual (expected, sw.ToString (), test);
  223. }
  224. [Test]
  225. public void RenderEmpty ()
  226. {
  227. CheckBoxList c = new CheckBoxList ();
  228. #if NET_2_0
  229. Render (c, "", "A1");
  230. #else
  231. Render (c, "<table border=\"0\">\n\n</table>", "A1");
  232. #endif
  233. c.CellPadding = 1;
  234. #if NET_2_0
  235. Render (c, "", "A2");
  236. #else
  237. Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A2");
  238. #endif
  239. c = new CheckBoxList ();
  240. c.CellPadding = 1;
  241. #if NET_2_0
  242. Render (c, "", "A3");
  243. #else
  244. Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A3");
  245. #endif
  246. c = new CheckBoxList ();
  247. c.TextAlign = TextAlign.Left;
  248. #if NET_2_0
  249. Render (c, "", "A4");
  250. #else
  251. Render (c, "<table border=\"0\">\n\n</table>", "A4");
  252. #endif
  253. }
  254. [Test]
  255. #if NET_2_0
  256. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  257. #endif
  258. public void Render ()
  259. {
  260. CheckBoxList c;
  261. c = new CheckBoxList ();
  262. c.Items.Add ("foo");
  263. Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
  264. "name=\"0\" type=\"checkbox\" />" +
  265. "<label for=\"0\">foo</label>" +
  266. "</td>\n\t</tr>\n</table>", "A5");
  267. c = new CheckBoxList ();
  268. c.Items.Add ("foo");
  269. Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
  270. "name=\"0\" type=\"checkbox\" />" +
  271. "<label for=\"0\">foo</label>" +
  272. "</td>\n\t</tr>\n</table>", "A6");
  273. }
  274. // bug 51648
  275. [Test]
  276. #if NET_2_0
  277. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  278. #endif
  279. public void TestTabIndex ()
  280. {
  281. CheckBoxList c = new CheckBoxList ();
  282. c.TabIndex = 5;
  283. c.Items.Add ("Item1");
  284. string exp = @"<table border=""0"">
  285. <tr>
  286. <td><input id=""0"" name=""0"" tabindex=""5"" type=""checkbox"" /><label for=""0"">Item1</label></td>
  287. </tr>
  288. </table>";
  289. Render (c, exp, "B1");
  290. }
  291. // bug 48802
  292. [Test]
  293. #if NET_2_0
  294. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  295. #endif
  296. public void TestDisabled ()
  297. {
  298. CheckBoxList c = new CheckBoxList ();
  299. c.Enabled = false;
  300. c.Items.Add ("Item1");
  301. string exp = @"<table border=""0"" disabled=""disabled"">
  302. <tr>
  303. <td><span disabled=""disabled""><input disabled=""disabled"" id=""0"" name=""0"" type=""checkbox"" /><label for=""0"">Item1</label></span></td>
  304. </tr>
  305. </table>";
  306. Render (c, exp, "C1");
  307. }
  308. }
  309. }