CheckBoxListTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. using System.Drawing;
  37. using System.Collections;
  38. using MonoTests.SystemWeb.Framework;
  39. using MonoTests.stand_alone.WebHarness;
  40. using System.Collections.Specialized;
  41. namespace MonoTests.System.Web.UI.WebControls {
  42. public class CheckBoxListPoker : CheckBoxList {
  43. public Style CreateStyle ()
  44. {
  45. return CreateControlStyle ();
  46. }
  47. public Control FindControlPoke (string name, int offset)
  48. {
  49. return FindControl (name, offset);
  50. }
  51. public string Render ()
  52. {
  53. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  54. base.Render (writer);
  55. return writer.InnerWriter.ToString ();
  56. }
  57. #if NET_2_0
  58. public new bool HasFooter
  59. {
  60. get
  61. {
  62. return base.HasFooter;
  63. }
  64. }
  65. public new bool HasHeader
  66. {
  67. get
  68. {
  69. return base.HasHeader;
  70. }
  71. }
  72. public new bool HasSeparators
  73. {
  74. get
  75. {
  76. return base.HasSeparators;
  77. }
  78. }
  79. public new int RepeatedItemCount
  80. {
  81. get
  82. {
  83. return base.RepeatedItemCount;
  84. }
  85. }
  86. public new void RaisePostDataChangedEvent ()
  87. {
  88. base.RaisePostDataChangedEvent ();
  89. }
  90. protected override Style GetItemStyle (ListItemType itemType, int repeatIndex)
  91. {
  92. Style s = new Style();
  93. s.BackColor = Color.Red;
  94. s.BorderStyle = BorderStyle.Solid;
  95. WebTest.CurrentTest.UserData = "GetItemStyle";
  96. return s;
  97. }
  98. public Style DoGetItemStyle (ListItemType itemType, int repeatIndex)
  99. {
  100. return base.GetItemStyle (itemType, repeatIndex);
  101. }
  102. public new string RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo)
  103. {
  104. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  105. base.RenderItem(itemType,repeatIndex,repeatInfo,writer);
  106. return writer.InnerWriter.ToString ();
  107. }
  108. #endif
  109. }
  110. [TestFixture]
  111. public class CheckBoxListTest {
  112. [Test]
  113. public void Defaults ()
  114. {
  115. CheckBoxListPoker c = new CheckBoxListPoker ();
  116. Assert.AreEqual (c.CellPadding, -1, "A1");
  117. Assert.AreEqual (c.CellSpacing, -1, "A2");
  118. Assert.AreEqual (c.RepeatColumns, 0, "A3");
  119. Assert.AreEqual (c.RepeatDirection,
  120. RepeatDirection.Vertical, "A4");
  121. Assert.AreEqual (c.RepeatLayout,
  122. RepeatLayout.Table, "A5");
  123. Assert.AreEqual (c.TextAlign, TextAlign.Right, "A6");
  124. #if NET_2_0
  125. Assert.AreEqual (false, c.HasFooter, "HasFooter");
  126. Assert.AreEqual (false, c.HasHeader, "HasHeader");
  127. Assert.AreEqual (false, c.HasSeparators, "HasSeparators");
  128. Assert.AreEqual (0, c.RepeatedItemCount, "RepeatedItemCount");
  129. Assert.AreEqual (null, c.DoGetItemStyle (ListItemType.Item, 0), "GetItemStyle");
  130. #endif
  131. }
  132. #if NET_2_0
  133. [Test]
  134. public void RaisePostDataChangedEvent ()
  135. {
  136. CheckBoxListPoker c = new CheckBoxListPoker ();
  137. c.SelectedIndexChanged += new EventHandler (c_SelectedIndexChanged);
  138. Assert.AreEqual (false, eventSelectedIndexChanged, "RaisePostDataChangedEvent#1");
  139. c.RaisePostDataChangedEvent ();
  140. Assert.AreEqual (true, eventSelectedIndexChanged, "RaisePostDataChangedEvent#2");
  141. }
  142. bool eventSelectedIndexChanged;
  143. void c_SelectedIndexChanged (object sender, EventArgs e)
  144. {
  145. eventSelectedIndexChanged = true;
  146. }
  147. [Test]
  148. [Category("NunitWeb")]
  149. public void GetItemStyle ()
  150. {
  151. WebTest t = new WebTest (PageInvoker.CreateOnLoad (GetItemStyle_Load));
  152. string html = t.Run ();
  153. string ctrl = HtmlDiff.GetControlFromPageHtml (html);
  154. if (ctrl == string.Empty)
  155. Assert.Fail ("CheckBoxList not created fail");
  156. Assert.AreEqual ("GetItemStyle", (string) t.UserData, "GetItemStyle not done");
  157. if ( ctrl.IndexOf("<td style=\"background-color:Red;border-style:Solid;\">") == -1)
  158. Assert.Fail ("CheckBoxList style not rendered");
  159. }
  160. public static void GetItemStyle_Load (Page p)
  161. {
  162. CheckBoxListPoker c = new CheckBoxListPoker ();
  163. ListItem l1 = new ListItem ("item1", "value1");
  164. ListItem l2 = new ListItem ("item2", "value2");
  165. c.Items.Add (l1);
  166. c.Items.Add (l2);
  167. p.Form.Controls.Add(new LiteralControl(HtmlDiff.BEGIN_TAG));
  168. p.Form.Controls.Add (c);
  169. p.Form.Controls.Add(new LiteralControl(HtmlDiff.END_TAG));
  170. }
  171. [Test]
  172. public void RenderItem ()
  173. {
  174. CheckBoxListPoker c = new CheckBoxListPoker ();
  175. ListItem l1 = new ListItem ("item1", "value1");
  176. ListItem l2 = new ListItem ("item2", "value2");
  177. c.Items.Add (l1);
  178. c.Items.Add (l2);
  179. string html = c.RenderItem (ListItemType.Item, 0, null);
  180. HtmlDiff.AssertAreEqual ("<input id=\"0\" type=\"checkbox\" name=\"0\" /><label for=\"0\">item1</label>", html, "RenderItem#1");
  181. html = c.RenderItem (ListItemType.Item, 1, null);
  182. HtmlDiff.AssertAreEqual ("<input id=\"1\" type=\"checkbox\" name=\"1\" /><label for=\"1\">item2</label>", html, "RenderItem#2");
  183. }
  184. [Test]
  185. public void RepeatedItemCount ()
  186. {
  187. CheckBoxListPoker c = new CheckBoxListPoker ();
  188. ListItem l1 = new ListItem ("item1", "value1");
  189. ListItem l2 = new ListItem ("item2", "value2");
  190. Assert.AreEqual (0, c.RepeatedItemCount, "RepeatedItemCount#1");
  191. c.Items.Add (l1);
  192. c.Items.Add (l2);
  193. Assert.AreEqual (2, c.RepeatedItemCount, "RepeatedItemCount#2");
  194. }
  195. #endif
  196. [Test]
  197. public void CleanProperties ()
  198. {
  199. CheckBoxList c = new CheckBoxList ();
  200. c.CellPadding = Int32.MaxValue;
  201. Assert.AreEqual (c.CellPadding, Int32.MaxValue, "A1");
  202. c.CellSpacing = Int32.MaxValue;
  203. Assert.AreEqual (c.CellSpacing, Int32.MaxValue, "A2");
  204. c.RepeatColumns = Int32.MaxValue;
  205. Assert.AreEqual (c.RepeatColumns, Int32.MaxValue, "A3");
  206. foreach (RepeatDirection d in
  207. Enum.GetValues (typeof (RepeatDirection))) {
  208. c.RepeatDirection = d;
  209. Assert.AreEqual (c.RepeatDirection, d, "A4-" + d);
  210. }
  211. foreach (RepeatLayout l in
  212. Enum.GetValues (typeof (RepeatLayout))) {
  213. c.RepeatLayout = l;
  214. Assert.AreEqual (c.RepeatLayout, l, "A5-" + l);
  215. }
  216. }
  217. [Test]
  218. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  219. public void CellPaddingTooLow ()
  220. {
  221. CheckBoxList c = new CheckBoxList ();
  222. c.CellPadding = -2;
  223. }
  224. [Test]
  225. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  226. public void CellSpacingTooLow ()
  227. {
  228. CheckBoxList c = new CheckBoxList ();
  229. c.CellSpacing = -2;
  230. }
  231. [Test]
  232. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  233. public void RepeatColumsTooLow ()
  234. {
  235. CheckBoxList c = new CheckBoxList ();
  236. c.RepeatColumns = -1;
  237. }
  238. [Test]
  239. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  240. public void RepeatDirection_Invalid ()
  241. {
  242. CheckBoxList c = new CheckBoxList ();
  243. c.RepeatDirection = (RepeatDirection) Int32.MaxValue;
  244. }
  245. [Test]
  246. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  247. public void RepeatLayout_Invalid ()
  248. {
  249. CheckBoxList c = new CheckBoxList ();
  250. c.RepeatLayout = (RepeatLayout) Int32.MaxValue;
  251. }
  252. [Test]
  253. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  254. public void TextAlign_Invalid ()
  255. {
  256. CheckBoxList c = new CheckBoxList ();
  257. c.TextAlign = (TextAlign) Int32.MaxValue;
  258. }
  259. [Test]
  260. public void ChildCheckBoxControl ()
  261. {
  262. CheckBoxList c = new CheckBoxList ();
  263. Assert.AreEqual (c.Controls.Count, 1, "A1");
  264. Assert.AreEqual (c.Controls [0].GetType (), typeof (CheckBox), "A2");
  265. }
  266. [Test]
  267. public void CreateStyle ()
  268. {
  269. CheckBoxListPoker c = new CheckBoxListPoker ();
  270. Assert.AreEqual (c.CreateStyle ().GetType (), typeof (TableStyle), "A1");
  271. }
  272. [Test]
  273. public void RepeatInfoProperties ()
  274. {
  275. IRepeatInfoUser ri = new CheckBoxList ();
  276. Assert.IsFalse (ri.HasFooter, "A1");
  277. Assert.IsFalse (ri.HasHeader, "A2");
  278. Assert.IsFalse (ri.HasSeparators, "A3");
  279. Assert.AreEqual (ri.RepeatedItemCount, 0, "A4");
  280. }
  281. [Test]
  282. public void RepeatInfoCount ()
  283. {
  284. CheckBoxList c = new CheckBoxList ();
  285. IRepeatInfoUser ri = (IRepeatInfoUser) c;
  286. Assert.AreEqual (ri.RepeatedItemCount, 0, "A1");
  287. c.Items.Add ("one");
  288. c.Items.Add ("two");
  289. c.Items.Add ("three");
  290. Assert.AreEqual (ri.RepeatedItemCount, 3, "A2");
  291. }
  292. [Test]
  293. public void RepeatInfoStyle ()
  294. {
  295. IRepeatInfoUser ri = new CheckBoxList ();
  296. foreach (ListItemType t in Enum.GetValues (typeof (ListItemType))) {
  297. Assert.AreEqual (ri.GetItemStyle (t, 0), null, "A1-" + t);
  298. Assert.AreEqual (ri.GetItemStyle (t, 1), null, "A2-" + t);
  299. Assert.AreEqual (ri.GetItemStyle (t, 2), null, "A3-" + t);
  300. Assert.AreEqual (ri.GetItemStyle (t, 3), null, "A4-" + t);
  301. }
  302. }
  303. [Test]
  304. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  305. public void RepeatInfoRenderOutOfRange ()
  306. {
  307. StringWriter sw = new StringWriter ();
  308. HtmlTextWriter tw = new HtmlTextWriter (sw);
  309. IRepeatInfoUser ri = new CheckBoxList ();
  310. ri.RenderItem (ListItemType.Item, -1, new RepeatInfo (), tw);
  311. }
  312. [Test]
  313. public void RepeatInfoRenderItem ()
  314. {
  315. StringWriter sw = new StringWriter ();
  316. HtmlTextWriter tw = new HtmlTextWriter (sw);
  317. CheckBoxList c = new CheckBoxList ();
  318. IRepeatInfoUser ri = (IRepeatInfoUser) c;
  319. RepeatInfo r = new RepeatInfo ();
  320. c.Items.Add ("one");
  321. c.Items.Add ("two");
  322. ri.RenderItem (ListItemType.Item, 0, r, tw);
  323. Assert.AreEqual ("<input id=\"0\" type=\"checkbox\" name=\"0\" />" +
  324. "<label for=\"0\">one</label>", sw.ToString (), "A1");
  325. }
  326. [Test]
  327. public void FindControl ()
  328. {
  329. CheckBoxListPoker p = new CheckBoxListPoker ();
  330. p.ID = "id";
  331. p.Items.Add ("one");
  332. p.Items.Add ("two");
  333. p.Items.Add ("three");
  334. // Everything seems to return this.
  335. Assert.AreEqual (p.FindControlPoke (String.Empty, 0), p, "A1");
  336. Assert.AreEqual (p.FindControlPoke ("id", 0), p, "A2");
  337. Assert.AreEqual (p.FindControlPoke ("id_0", 0), p, "A3");
  338. Assert.AreEqual (p.FindControlPoke ("id_1", 0), p, "A4");
  339. Assert.AreEqual (p.FindControlPoke ("id_2", 0), p, "A5");
  340. Assert.AreEqual (p.FindControlPoke ("id_3", 0), p, "A6");
  341. Assert.AreEqual (p.FindControlPoke ("0", 0), p, "A7");
  342. Assert.AreEqual (p.FindControlPoke (String.Empty, 10), p, "A1");
  343. Assert.AreEqual (p.FindControlPoke ("id", 10), p, "A2");
  344. Assert.AreEqual (p.FindControlPoke ("id_0", 10), p, "A3");
  345. Assert.AreEqual (p.FindControlPoke ("id_1", 10), p, "A4");
  346. Assert.AreEqual (p.FindControlPoke ("id_2", 10), p, "A5");
  347. Assert.AreEqual (p.FindControlPoke ("id_3", 10), p, "A6");
  348. Assert.AreEqual (p.FindControlPoke ("0", 10), p, "A7");
  349. }
  350. private void Render (CheckBoxList list, string expected, string test)
  351. {
  352. StringWriter sw = new StringWriter ();
  353. HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
  354. sw.NewLine = "\n";
  355. list.RenderControl (tw);
  356. HtmlDiff.AssertAreEqual (expected, sw.ToString (), test);
  357. }
  358. [Test]
  359. public void RenderEmpty ()
  360. {
  361. CheckBoxList c = new CheckBoxList ();
  362. #if NET_2_0
  363. Render (c, "", "A1");
  364. #else
  365. Render (c, "<table border=\"0\">\n\n</table>", "A1");
  366. #endif
  367. c.CellPadding = 1;
  368. #if NET_2_0
  369. Render (c, "", "A2");
  370. #else
  371. Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A2");
  372. #endif
  373. c = new CheckBoxList ();
  374. c.CellPadding = 1;
  375. #if NET_2_0
  376. Render (c, "", "A3");
  377. #else
  378. Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A3");
  379. #endif
  380. c = new CheckBoxList ();
  381. c.TextAlign = TextAlign.Left;
  382. #if NET_2_0
  383. Render (c, "", "A4");
  384. #else
  385. Render (c, "<table border=\"0\">\n\n</table>", "A4");
  386. #endif
  387. }
  388. [Test]
  389. #if NET_2_0
  390. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  391. #endif
  392. public void Render ()
  393. {
  394. CheckBoxList c;
  395. c = new CheckBoxList ();
  396. c.Items.Add ("foo");
  397. Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
  398. "name=\"0\" type=\"checkbox\" />" +
  399. "<label for=\"0\">foo</label>" +
  400. "</td>\n\t</tr>\n</table>", "A5");
  401. c = new CheckBoxList ();
  402. c.Items.Add ("foo");
  403. Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
  404. "name=\"0\" type=\"checkbox\" />" +
  405. "<label for=\"0\">foo</label>" +
  406. "</td>\n\t</tr>\n</table>", "A6");
  407. }
  408. // bug 51648
  409. [Test]
  410. #if NET_2_0
  411. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  412. #endif
  413. public void TestTabIndex ()
  414. {
  415. CheckBoxList c = new CheckBoxList ();
  416. c.TabIndex = 5;
  417. c.Items.Add ("Item1");
  418. string exp = @"<table border=""0"">
  419. <tr>
  420. <td><input id=""0"" name=""0"" tabindex=""5"" type=""checkbox"" /><label for=""0"">Item1</label></td>
  421. </tr>
  422. </table>";
  423. Render (c, exp, "B1");
  424. }
  425. // bug 48802
  426. [Test]
  427. #if NET_2_0
  428. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  429. #endif
  430. public void TestDisabled ()
  431. {
  432. CheckBoxList c = new CheckBoxList ();
  433. c.Enabled = false;
  434. c.Items.Add ("Item1");
  435. string exp = @"<table border=""0"" disabled=""disabled"">
  436. <tr>
  437. <td><span disabled=""disabled""><input disabled=""disabled"" id=""0"" name=""0"" type=""checkbox"" /><label for=""0"">Item1</label></span></td>
  438. </tr>
  439. </table>";
  440. Render (c, exp, "C1");
  441. }
  442. #if NET_2_0
  443. class TestCheckBoxList : CheckBoxList
  444. {
  445. public new virtual void VerifyMultiSelect()
  446. {
  447. base.VerifyMultiSelect();
  448. }
  449. }
  450. [Test]
  451. #if TARGET_JVM
  452. [Ignore ("TD #7164")]
  453. #endif
  454. public void VerifyMultiSelectTest()
  455. {
  456. TestCheckBoxList list = new TestCheckBoxList();
  457. list.VerifyMultiSelect();
  458. }
  459. [TestFixtureTearDown]
  460. public void teardown ()
  461. {
  462. WebTest.Unload ();
  463. }
  464. #endif
  465. }
  466. }