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. #if TARGET_JVM // Bug #6861
  150. [Category("NotWorking")]
  151. #endif
  152. public void GetItemStyle ()
  153. {
  154. WebTest t = new WebTest (PageInvoker.CreateOnLoad (GetItemStyle_Load));
  155. string html = t.Run ();
  156. string ctrl = HtmlDiff.GetControlFromPageHtml (html);
  157. if (ctrl == string.Empty)
  158. Assert.Fail ("CheckBoxList not created fail");
  159. Assert.AreEqual ("GetItemStyle", (string) t.UserData, "GetItemStyle not done");
  160. if ( ctrl.IndexOf("<td style=\"background-color:Red;border-style:Solid;\">") == -1)
  161. Assert.Fail ("CheckBoxList style not rendered");
  162. }
  163. public static void GetItemStyle_Load (Page p)
  164. {
  165. CheckBoxListPoker c = new CheckBoxListPoker ();
  166. ListItem l1 = new ListItem ("item1", "value1");
  167. ListItem l2 = new ListItem ("item2", "value2");
  168. c.Items.Add (l1);
  169. c.Items.Add (l2);
  170. p.Form.Controls.Add(new LiteralControl(HtmlDiff.BEGIN_TAG));
  171. p.Form.Controls.Add (c);
  172. p.Form.Controls.Add(new LiteralControl(HtmlDiff.END_TAG));
  173. }
  174. [Test]
  175. public void RenderItem ()
  176. {
  177. CheckBoxListPoker c = new CheckBoxListPoker ();
  178. ListItem l1 = new ListItem ("item1", "value1");
  179. ListItem l2 = new ListItem ("item2", "value2");
  180. c.Items.Add (l1);
  181. c.Items.Add (l2);
  182. string html = c.RenderItem (ListItemType.Item, 0, null);
  183. HtmlDiff.AssertAreEqual ("<input id=\"0\" type=\"checkbox\" name=\"0\" /><label for=\"0\">item1</label>", html, "RenderItem#1");
  184. html = c.RenderItem (ListItemType.Item, 1, null);
  185. HtmlDiff.AssertAreEqual ("<input id=\"1\" type=\"checkbox\" name=\"1\" /><label for=\"1\">item2</label>", html, "RenderItem#2");
  186. }
  187. [Test]
  188. public void RepeatedItemCount ()
  189. {
  190. CheckBoxListPoker c = new CheckBoxListPoker ();
  191. ListItem l1 = new ListItem ("item1", "value1");
  192. ListItem l2 = new ListItem ("item2", "value2");
  193. Assert.AreEqual (0, c.RepeatedItemCount, "RepeatedItemCount#1");
  194. c.Items.Add (l1);
  195. c.Items.Add (l2);
  196. Assert.AreEqual (2, c.RepeatedItemCount, "RepeatedItemCount#2");
  197. }
  198. #endif
  199. [Test]
  200. public void CleanProperties ()
  201. {
  202. CheckBoxList c = new CheckBoxList ();
  203. c.CellPadding = Int32.MaxValue;
  204. Assert.AreEqual (c.CellPadding, Int32.MaxValue, "A1");
  205. c.CellSpacing = Int32.MaxValue;
  206. Assert.AreEqual (c.CellSpacing, Int32.MaxValue, "A2");
  207. c.RepeatColumns = Int32.MaxValue;
  208. Assert.AreEqual (c.RepeatColumns, Int32.MaxValue, "A3");
  209. foreach (RepeatDirection d in
  210. Enum.GetValues (typeof (RepeatDirection))) {
  211. c.RepeatDirection = d;
  212. Assert.AreEqual (c.RepeatDirection, d, "A4-" + d);
  213. }
  214. foreach (RepeatLayout l in
  215. Enum.GetValues (typeof (RepeatLayout))) {
  216. c.RepeatLayout = l;
  217. Assert.AreEqual (c.RepeatLayout, l, "A5-" + l);
  218. }
  219. }
  220. [Test]
  221. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  222. public void CellPaddingTooLow ()
  223. {
  224. CheckBoxList c = new CheckBoxList ();
  225. c.CellPadding = -2;
  226. }
  227. [Test]
  228. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  229. public void CellSpacingTooLow ()
  230. {
  231. CheckBoxList c = new CheckBoxList ();
  232. c.CellSpacing = -2;
  233. }
  234. [Test]
  235. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  236. public void RepeatColumsTooLow ()
  237. {
  238. CheckBoxList c = new CheckBoxList ();
  239. c.RepeatColumns = -1;
  240. }
  241. [Test]
  242. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  243. public void RepeatDirection_Invalid ()
  244. {
  245. CheckBoxList c = new CheckBoxList ();
  246. c.RepeatDirection = (RepeatDirection) Int32.MaxValue;
  247. }
  248. [Test]
  249. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  250. public void RepeatLayout_Invalid ()
  251. {
  252. CheckBoxList c = new CheckBoxList ();
  253. c.RepeatLayout = (RepeatLayout) Int32.MaxValue;
  254. }
  255. [Test]
  256. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  257. public void TextAlign_Invalid ()
  258. {
  259. CheckBoxList c = new CheckBoxList ();
  260. c.TextAlign = (TextAlign) Int32.MaxValue;
  261. }
  262. [Test]
  263. public void ChildCheckBoxControl ()
  264. {
  265. CheckBoxList c = new CheckBoxList ();
  266. Assert.AreEqual (c.Controls.Count, 1, "A1");
  267. Assert.AreEqual (c.Controls [0].GetType (), typeof (CheckBox), "A2");
  268. }
  269. [Test]
  270. public void CreateStyle ()
  271. {
  272. CheckBoxListPoker c = new CheckBoxListPoker ();
  273. Assert.AreEqual (c.CreateStyle ().GetType (), typeof (TableStyle), "A1");
  274. }
  275. [Test]
  276. public void RepeatInfoProperties ()
  277. {
  278. IRepeatInfoUser ri = new CheckBoxList ();
  279. Assert.IsFalse (ri.HasFooter, "A1");
  280. Assert.IsFalse (ri.HasHeader, "A2");
  281. Assert.IsFalse (ri.HasSeparators, "A3");
  282. Assert.AreEqual (ri.RepeatedItemCount, 0, "A4");
  283. }
  284. [Test]
  285. public void RepeatInfoCount ()
  286. {
  287. CheckBoxList c = new CheckBoxList ();
  288. IRepeatInfoUser ri = (IRepeatInfoUser) c;
  289. Assert.AreEqual (ri.RepeatedItemCount, 0, "A1");
  290. c.Items.Add ("one");
  291. c.Items.Add ("two");
  292. c.Items.Add ("three");
  293. Assert.AreEqual (ri.RepeatedItemCount, 3, "A2");
  294. }
  295. [Test]
  296. public void RepeatInfoStyle ()
  297. {
  298. IRepeatInfoUser ri = new CheckBoxList ();
  299. foreach (ListItemType t in Enum.GetValues (typeof (ListItemType))) {
  300. Assert.AreEqual (ri.GetItemStyle (t, 0), null, "A1-" + t);
  301. Assert.AreEqual (ri.GetItemStyle (t, 1), null, "A2-" + t);
  302. Assert.AreEqual (ri.GetItemStyle (t, 2), null, "A3-" + t);
  303. Assert.AreEqual (ri.GetItemStyle (t, 3), null, "A4-" + t);
  304. }
  305. }
  306. [Test]
  307. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  308. public void RepeatInfoRenderOutOfRange ()
  309. {
  310. StringWriter sw = new StringWriter ();
  311. HtmlTextWriter tw = new HtmlTextWriter (sw);
  312. IRepeatInfoUser ri = new CheckBoxList ();
  313. ri.RenderItem (ListItemType.Item, -1, new RepeatInfo (), tw);
  314. }
  315. [Test]
  316. public void RepeatInfoRenderItem ()
  317. {
  318. StringWriter sw = new StringWriter ();
  319. HtmlTextWriter tw = new HtmlTextWriter (sw);
  320. CheckBoxList c = new CheckBoxList ();
  321. IRepeatInfoUser ri = (IRepeatInfoUser) c;
  322. RepeatInfo r = new RepeatInfo ();
  323. c.Items.Add ("one");
  324. c.Items.Add ("two");
  325. ri.RenderItem (ListItemType.Item, 0, r, tw);
  326. Assert.AreEqual ("<input id=\"0\" type=\"checkbox\" name=\"0\" />" +
  327. "<label for=\"0\">one</label>", sw.ToString (), "A1");
  328. }
  329. [Test]
  330. public void FindControl ()
  331. {
  332. CheckBoxListPoker p = new CheckBoxListPoker ();
  333. p.ID = "id";
  334. p.Items.Add ("one");
  335. p.Items.Add ("two");
  336. p.Items.Add ("three");
  337. // Everything seems to return this.
  338. Assert.AreEqual (p.FindControlPoke (String.Empty, 0), p, "A1");
  339. Assert.AreEqual (p.FindControlPoke ("id", 0), p, "A2");
  340. Assert.AreEqual (p.FindControlPoke ("id_0", 0), p, "A3");
  341. Assert.AreEqual (p.FindControlPoke ("id_1", 0), p, "A4");
  342. Assert.AreEqual (p.FindControlPoke ("id_2", 0), p, "A5");
  343. Assert.AreEqual (p.FindControlPoke ("id_3", 0), p, "A6");
  344. Assert.AreEqual (p.FindControlPoke ("0", 0), p, "A7");
  345. Assert.AreEqual (p.FindControlPoke (String.Empty, 10), p, "A1");
  346. Assert.AreEqual (p.FindControlPoke ("id", 10), p, "A2");
  347. Assert.AreEqual (p.FindControlPoke ("id_0", 10), p, "A3");
  348. Assert.AreEqual (p.FindControlPoke ("id_1", 10), p, "A4");
  349. Assert.AreEqual (p.FindControlPoke ("id_2", 10), p, "A5");
  350. Assert.AreEqual (p.FindControlPoke ("id_3", 10), p, "A6");
  351. Assert.AreEqual (p.FindControlPoke ("0", 10), p, "A7");
  352. }
  353. private void Render (CheckBoxList list, string expected, string test)
  354. {
  355. StringWriter sw = new StringWriter ();
  356. HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
  357. sw.NewLine = "\n";
  358. list.RenderControl (tw);
  359. HtmlDiff.AssertAreEqual (expected, sw.ToString (), test);
  360. }
  361. [Test]
  362. public void RenderEmpty ()
  363. {
  364. CheckBoxList c = new CheckBoxList ();
  365. #if NET_2_0
  366. Render (c, "", "A1");
  367. #else
  368. Render (c, "<table border=\"0\">\n\n</table>", "A1");
  369. #endif
  370. c.CellPadding = 1;
  371. #if NET_2_0
  372. Render (c, "", "A2");
  373. #else
  374. Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A2");
  375. #endif
  376. c = new CheckBoxList ();
  377. c.CellPadding = 1;
  378. #if NET_2_0
  379. Render (c, "", "A3");
  380. #else
  381. Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A3");
  382. #endif
  383. c = new CheckBoxList ();
  384. c.TextAlign = TextAlign.Left;
  385. #if NET_2_0
  386. Render (c, "", "A4");
  387. #else
  388. Render (c, "<table border=\"0\">\n\n</table>", "A4");
  389. #endif
  390. }
  391. [Test]
  392. #if NET_2_0
  393. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  394. #endif
  395. public void Render ()
  396. {
  397. CheckBoxList c;
  398. c = new CheckBoxList ();
  399. c.Items.Add ("foo");
  400. Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
  401. "name=\"0\" type=\"checkbox\" />" +
  402. "<label for=\"0\">foo</label>" +
  403. "</td>\n\t</tr>\n</table>", "A5");
  404. c = new CheckBoxList ();
  405. c.Items.Add ("foo");
  406. Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
  407. "name=\"0\" type=\"checkbox\" />" +
  408. "<label for=\"0\">foo</label>" +
  409. "</td>\n\t</tr>\n</table>", "A6");
  410. }
  411. // bug 51648
  412. [Test]
  413. #if NET_2_0
  414. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  415. #endif
  416. public void TestTabIndex ()
  417. {
  418. CheckBoxList c = new CheckBoxList ();
  419. c.TabIndex = 5;
  420. c.Items.Add ("Item1");
  421. string exp = @"<table border=""0"">
  422. <tr>
  423. <td><input id=""0"" name=""0"" tabindex=""5"" type=""checkbox"" /><label for=""0"">Item1</label></td>
  424. </tr>
  425. </table>";
  426. Render (c, exp, "B1");
  427. }
  428. // bug 48802
  429. [Test]
  430. #if NET_2_0
  431. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  432. #endif
  433. public void TestDisabled ()
  434. {
  435. CheckBoxList c = new CheckBoxList ();
  436. c.Enabled = false;
  437. c.Items.Add ("Item1");
  438. string exp = @"<table border=""0"" disabled=""disabled"">
  439. <tr>
  440. <td><span disabled=""disabled""><input disabled=""disabled"" id=""0"" name=""0"" type=""checkbox"" /><label for=""0"">Item1</label></span></td>
  441. </tr>
  442. </table>";
  443. Render (c, exp, "C1");
  444. }
  445. #if NET_2_0
  446. class TestCheckBoxList : CheckBoxList
  447. {
  448. public new virtual void VerifyMultiSelect()
  449. {
  450. base.VerifyMultiSelect();
  451. }
  452. }
  453. [Test]
  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. }