CheckBoxListTest.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. public new bool HasFooter
  58. {
  59. get
  60. {
  61. return base.HasFooter;
  62. }
  63. }
  64. public new bool HasHeader
  65. {
  66. get
  67. {
  68. return base.HasHeader;
  69. }
  70. }
  71. public new bool HasSeparators
  72. {
  73. get
  74. {
  75. return base.HasSeparators;
  76. }
  77. }
  78. public new int RepeatedItemCount
  79. {
  80. get
  81. {
  82. return base.RepeatedItemCount;
  83. }
  84. }
  85. public new void RaisePostDataChangedEvent ()
  86. {
  87. base.RaisePostDataChangedEvent ();
  88. }
  89. protected override Style GetItemStyle (ListItemType itemType, int repeatIndex)
  90. {
  91. Style s = new Style();
  92. s.BackColor = Color.Red;
  93. s.BorderStyle = BorderStyle.Solid;
  94. WebTest.CurrentTest.UserData = "GetItemStyle";
  95. return s;
  96. }
  97. public Style DoGetItemStyle (ListItemType itemType, int repeatIndex)
  98. {
  99. return base.GetItemStyle (itemType, repeatIndex);
  100. }
  101. public new string RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo)
  102. {
  103. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  104. base.RenderItem(itemType,repeatIndex,repeatInfo,writer);
  105. return writer.InnerWriter.ToString ();
  106. }
  107. }
  108. [TestFixture]
  109. public class CheckBoxListTest
  110. {
  111. [TestFixtureSetUp]
  112. public void SetUp ()
  113. {
  114. Type t = GetType ();
  115. WebTest.CopyResource (t, "CheckBoxList_Bug377703_1.aspx", "CheckBoxList_Bug377703_1.aspx");
  116. WebTest.CopyResource (t, "CheckBoxList_Bug377703_2.aspx", "CheckBoxList_Bug377703_2.aspx");
  117. WebTest.CopyResource (t, "CheckBoxList_Bug578770.aspx", "CheckBoxList_Bug578770.aspx");
  118. WebTest.CopyResource (t, "CheckBoxList_Bug600415.aspx", "CheckBoxList_Bug600415.aspx");
  119. }
  120. [Test]
  121. public void Defaults ()
  122. {
  123. CheckBoxListPoker c = new CheckBoxListPoker ();
  124. Assert.AreEqual (c.CellPadding, -1, "A1");
  125. Assert.AreEqual (c.CellSpacing, -1, "A2");
  126. Assert.AreEqual (c.RepeatColumns, 0, "A3");
  127. Assert.AreEqual (c.RepeatDirection,
  128. RepeatDirection.Vertical, "A4");
  129. Assert.AreEqual (c.RepeatLayout,
  130. RepeatLayout.Table, "A5");
  131. Assert.AreEqual (c.TextAlign, TextAlign.Right, "A6");
  132. Assert.AreEqual (false, c.HasFooter, "HasFooter");
  133. Assert.AreEqual (false, c.HasHeader, "HasHeader");
  134. Assert.AreEqual (false, c.HasSeparators, "HasSeparators");
  135. Assert.AreEqual (0, c.RepeatedItemCount, "RepeatedItemCount");
  136. Assert.AreEqual (null, c.DoGetItemStyle (ListItemType.Item, 0), "GetItemStyle");
  137. }
  138. [Test]
  139. public void CheckBoxList_Bug377703_1 ()
  140. {
  141. WebTest t = new WebTest ("CheckBoxList_Bug377703_1.aspx");
  142. t.Invoker = PageInvoker.CreateOnInit (CheckBoxList_Bug377703_1_OnInit);
  143. string origHtmlFirst = "<table id=\"cbxl1\">\r\n\t<tr>\r\n\t\t<td><input id=\"cbxl1_0\" type=\"checkbox\" name=\"cbxl1$0\" value=\"x\" /><label for=\"cbxl1_0\">x</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"cbxl1_1\" type=\"checkbox\" name=\"cbxl1$1\" value=\"y\" /><label for=\"cbxl1_1\">y</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"cbxl1_2\" type=\"checkbox\" name=\"cbxl1$2\" value=\"z\" /><label for=\"cbxl1_2\">z</label></td>\r\n\t</tr>\r\n</table>";
  144. string origHtmlSecond = "<table id=\"cbxl1\">\r\n\t<tr>\r\n\t\t<td><input id=\"cbxl1_0\" type=\"checkbox\" name=\"cbxl1$0\" checked=\"checked\" value=\"x\" /><label for=\"cbxl1_0\">x</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"cbxl1_1\" type=\"checkbox\" name=\"cbxl1$1\" value=\"y\" /><label for=\"cbxl1_1\">y</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"cbxl1_2\" type=\"checkbox\" name=\"cbxl1$2\" value=\"z\" /><label for=\"cbxl1_2\">z</label></td>\r\n\t</tr>\r\n</table>";
  145. string html = t.Run ();
  146. string listHtml = HtmlDiff.GetControlFromPageHtml (html);
  147. HtmlDiff.AssertAreEqual (origHtmlFirst, listHtml, "#A1");
  148. FormRequest fr = new FormRequest (t.Response, "form1");
  149. fr.Controls.Add ("cbxl1$0");
  150. fr.Controls ["cbxl1$0"].Value = "on";
  151. fr.Controls.Add ("ctl01");
  152. fr.Controls ["ctl01"].Value = "Click me twice to have the first Item become empty";
  153. t.Request = fr;
  154. html = t.Run ();
  155. fr = new FormRequest (t.Response, "form1");
  156. fr.Controls.Add ("cbxl1$0");
  157. fr.Controls ["cbxl1$0"].Value = "on";
  158. fr.Controls.Add ("ctl01");
  159. fr.Controls ["ctl01"].Value = "Click me twice to have the first Item become empty";
  160. t.Request = fr;
  161. html = t.Run ();
  162. listHtml = HtmlDiff.GetControlFromPageHtml (html);
  163. HtmlDiff.AssertAreEqual (origHtmlSecond, listHtml, "#A2");
  164. }
  165. public static void CheckBoxList_Bug377703_1_OnInit (Page p)
  166. {
  167. CheckBoxList cbxl1 = p.FindControl ("cbxl1") as CheckBoxList;
  168. cbxl1.DataSource = new[] {
  169. new { ID = "x", Text = "X" },
  170. new { ID = "y", Text = "Y" },
  171. new { ID = "z", Text = "Z" },
  172. };
  173. cbxl1.DataValueField = "ID";
  174. cbxl1.DataTextField = "ID";
  175. cbxl1.DataBind();
  176. }
  177. [Test]
  178. public void CheckBoxList_Bug377703_2 ()
  179. {
  180. WebTest t = new WebTest ("CheckBoxList_Bug377703_2.aspx");
  181. t.Invoker = PageInvoker.CreateOnInit (CheckBoxList_Bug377703_2_OnInit);
  182. string origHtmlFirst = "<table id=\"cbxl2\">\r\n\t<tr>\r\n\t\t<td><input id=\"cbxl2_0\" type=\"checkbox\" name=\"cbxl2$0\" value=\"x\" /><label for=\"cbxl2_0\">x</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"cbxl2_1\" type=\"checkbox\" name=\"cbxl2$1\" value=\"y\" /><label for=\"cbxl2_1\">y</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"cbxl2_2\" type=\"checkbox\" name=\"cbxl2$2\" value=\"z\" /><label for=\"cbxl2_2\">z</label></td>\r\n\t</tr>\r\n</table>";
  183. string origHtmlSecond = "<table id=\"cbxl2\" class=\"aspNetDisabled\">\r\n\t<tr>\r\n\t\t<td><span class=\"aspNetDisabled\"><input id=\"cbxl2_0\" type=\"checkbox\" name=\"cbxl2$0\" checked=\"checked\" disabled=\"disabled\" value=\"x\" /><label for=\"cbxl2_0\">x</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span class=\"aspNetDisabled\"><input id=\"cbxl2_1\" type=\"checkbox\" name=\"cbxl2$1\" disabled=\"disabled\" value=\"y\" /><label for=\"cbxl2_1\">y</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span class=\"aspNetDisabled\"><input id=\"cbxl2_2\" type=\"checkbox\" name=\"cbxl2$2\" checked=\"checked\" disabled=\"disabled\" value=\"z\" /><label for=\"cbxl2_2\">z</label></span></td>\r\n\t</tr>\r\n</table>";
  184. string origHtmlThird = "<table id=\"cbxl2\" class=\"aspNetDisabled\">\r\n\t<tr>\r\n\t\t<td><span class=\"aspNetDisabled\"><input id=\"cbxl2_0\" type=\"checkbox\" name=\"cbxl2$0\" checked=\"checked\" disabled=\"disabled\" value=\"x\" /><label for=\"cbxl2_0\">x</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span class=\"aspNetDisabled\"><input id=\"cbxl2_1\" type=\"checkbox\" name=\"cbxl2$1\" disabled=\"disabled\" value=\"y\" /><label for=\"cbxl2_1\">y</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span class=\"aspNetDisabled\"><input id=\"cbxl2_2\" type=\"checkbox\" name=\"cbxl2$2\" checked=\"checked\" disabled=\"disabled\" value=\"z\" /><label for=\"cbxl2_2\">z</label></span></td>\r\n\t</tr>\r\n</table>";
  185. string html = t.Run ();
  186. string listHtml = HtmlDiff.GetControlFromPageHtml (html);
  187. HtmlDiff.AssertAreEqual (origHtmlFirst, listHtml, "#A1");
  188. FormRequest fr = new FormRequest (t.Response, "form1");
  189. fr.Controls.Add ("cbxl2$0");
  190. fr.Controls ["cbxl2$0"].Value = "on";
  191. fr.Controls.Add ("cbxl2$2");
  192. fr.Controls ["cbxl2$2"].Value = "on";
  193. fr.Controls.Add ("ctl01");
  194. fr.Controls ["ctl01"].Value = "Click to toggle enable status above";
  195. t.Request = fr;
  196. html = t.Run ();
  197. listHtml = HtmlDiff.GetControlFromPageHtml (html);
  198. HtmlDiff.AssertAreEqual (origHtmlSecond, listHtml, "#A2");
  199. fr = new FormRequest (t.Response, "form1");
  200. fr.Controls.Add ("ctl02");
  201. fr.Controls ["ctl02"].Value = "Click to refresh page";
  202. t.Request = fr;
  203. html = t.Run ();
  204. listHtml = HtmlDiff.GetControlFromPageHtml (html);
  205. HtmlDiff.AssertAreEqual (origHtmlThird, listHtml, "#A3");
  206. }
  207. public static void CheckBoxList_Bug377703_2_OnInit (Page p)
  208. {
  209. CheckBoxList cbxl2 = p.FindControl ("cbxl2") as CheckBoxList;
  210. cbxl2.DataSource = new[] {
  211. new { ID = "x", Text = "X" },
  212. new { ID = "y", Text = "Y" },
  213. new { ID = "z", Text = "Z" },
  214. };
  215. cbxl2.DataValueField = "ID";
  216. cbxl2.DataTextField = "ID";
  217. cbxl2.DataBind();
  218. }
  219. [Test]
  220. public void CheckBoxList_Bug578770 ()
  221. {
  222. WebTest t = new WebTest ("CheckBoxList_Bug578770.aspx");
  223. t.Invoker = PageInvoker.CreateOnInit (CheckBoxList_Bug578770_OnInit);
  224. string origHtml = "<table id=\"test\">\r\n\t<tr>\r\n\t\t<td><span class=\"aspNetDisabled\"><input id=\"test_0\" type=\"checkbox\" name=\"test$0\" disabled=\"disabled\" value=\"Sun\" /><label for=\"test_0\">Sun</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span><input id=\"test_1\" type=\"checkbox\" name=\"test$1\" value=\"Mon\" /><label for=\"test_1\">Mon</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span><input id=\"test_2\" type=\"checkbox\" name=\"test$2\" value=\"Tue\" /><label for=\"test_2\">Tue</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span><input id=\"test_3\" type=\"checkbox\" name=\"test$3\" value=\"Wed\" /><label for=\"test_3\">Wed</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span><input id=\"test_4\" type=\"checkbox\" name=\"test$4\" value=\"Thu\" /><label for=\"test_4\">Thu</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span><input id=\"test_5\" type=\"checkbox\" name=\"test$5\" value=\"Fri\" /><label for=\"test_5\">Fri</label></span></td>\r\n\t</tr><tr>\r\n\t\t<td><span><input id=\"test_6\" type=\"checkbox\" name=\"test$6\" value=\"Sat\" /><label for=\"test_6\">Sat</label></span></td>\r\n\t</tr>\r\n</table>";
  225. string html = t.Run ();
  226. string listHtml = HtmlDiff.GetControlFromPageHtml (html);
  227. HtmlDiff.AssertAreEqual (origHtml, listHtml, "#A1");
  228. }
  229. public static void CheckBoxList_Bug578770_OnInit (Page p)
  230. {
  231. CheckBoxList test = p.FindControl ("test") as CheckBoxList;
  232. string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  233. test.DataSource = weekDays;
  234. test.DataBind();
  235. test.Items[0].Enabled = false;
  236. }
  237. [Test]
  238. public void CheckBoxList_Bug600415 ()
  239. {
  240. WebTest t = new WebTest ("CheckBoxList_Bug600415.aspx");
  241. string origHtmlFirst = "<table id=\"checkBoxList\">\r\n\t<tr>\r\n\t\t<td><input id=\"checkBoxList_0\" type=\"checkbox\" name=\"checkBoxList$0\" checked=\"checked\" value=\"Item 1\" /><label for=\"checkBoxList_0\">Item 1</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"checkBoxList_1\" type=\"checkbox\" name=\"checkBoxList$1\" value=\"Item 2\" /><label for=\"checkBoxList_1\">Item 2</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"checkBoxList_2\" type=\"checkbox\" name=\"checkBoxList$2\" checked=\"checked\" value=\"Item 3\" /><label for=\"checkBoxList_2\">Item 3</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"checkBoxList_3\" type=\"checkbox\" name=\"checkBoxList$3\" value=\"Item 4\" /><label for=\"checkBoxList_3\">Item 4</label></td>\r\n\t</tr>\r\n</table>";
  242. string origHtmlSecond = "<table id=\"checkBoxList\">\r\n\t<tr>\r\n\t\t<td><input id=\"checkBoxList_0\" type=\"checkbox\" name=\"checkBoxList$0\" value=\"Item 1\" /><label for=\"checkBoxList_0\">Item 1</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"checkBoxList_1\" type=\"checkbox\" name=\"checkBoxList$1\" value=\"Item 2\" /><label for=\"checkBoxList_1\">Item 2</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"checkBoxList_2\" type=\"checkbox\" name=\"checkBoxList$2\" value=\"Item 3\" /><label for=\"checkBoxList_2\">Item 3</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"checkBoxList_3\" type=\"checkbox\" name=\"checkBoxList$3\" value=\"Item 4\" /><label for=\"checkBoxList_3\">Item 4</label></td>\r\n\t</tr>\r\n</table>";
  243. string origHtmlThird = "<table id=\"checkBoxList\">\r\n\t<tr>\r\n\t\t<td><input id=\"checkBoxList_0\" type=\"checkbox\" name=\"checkBoxList$0\" checked=\"checked\" value=\"Item 1\" /><label for=\"checkBoxList_0\">Item 1</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"checkBoxList_1\" type=\"checkbox\" name=\"checkBoxList$1\" checked=\"checked\" value=\"Item 2\" /><label for=\"checkBoxList_1\">Item 2</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"checkBoxList_2\" type=\"checkbox\" name=\"checkBoxList$2\" checked=\"checked\" value=\"Item 3\" /><label for=\"checkBoxList_2\">Item 3</label></td>\r\n\t</tr><tr>\r\n\t\t<td><input id=\"checkBoxList_3\" type=\"checkbox\" name=\"checkBoxList$3\" checked=\"checked\" value=\"Item 4\" /><label for=\"checkBoxList_3\">Item 4</label></td>\r\n\t</tr>\r\n</table>";
  244. string html = t.Run ();
  245. string listHtml = HtmlDiff.GetControlFromPageHtml (html);
  246. HtmlDiff.AssertAreEqual (origHtmlFirst, listHtml, "#A1");
  247. FormRequest fr = new FormRequest (t.Response, "form1");
  248. fr.Controls.Add ("cmdClick");
  249. fr.Controls ["cmdClick"].Value = "Ok";
  250. t.Request = fr;
  251. html = t.Run ();
  252. listHtml = HtmlDiff.GetControlFromPageHtml (html);
  253. HtmlDiff.AssertAreEqual (origHtmlSecond, listHtml, "#A2");
  254. fr = new FormRequest (t.Response, "form1");
  255. fr.Controls.Add ("checkBoxList$0");
  256. fr.Controls ["checkBoxList$0"].Value = "on";
  257. fr.Controls.Add ("checkBoxList$1");
  258. fr.Controls ["checkBoxList$1"].Value = "on";
  259. fr.Controls.Add ("checkBoxList$2");
  260. fr.Controls ["checkBoxList$2"].Value = "on";
  261. fr.Controls.Add ("checkBoxList$3");
  262. fr.Controls ["checkBoxList$3"].Value = "on";
  263. t.Request = fr;
  264. html = t.Run ();
  265. listHtml = HtmlDiff.GetControlFromPageHtml (html);
  266. HtmlDiff.AssertAreEqual (origHtmlThird, listHtml, "#A3");
  267. }
  268. [Test]
  269. public void RaisePostDataChangedEvent ()
  270. {
  271. CheckBoxListPoker c = new CheckBoxListPoker ();
  272. c.SelectedIndexChanged += new EventHandler (c_SelectedIndexChanged);
  273. Assert.AreEqual (false, eventSelectedIndexChanged, "RaisePostDataChangedEvent#1");
  274. c.RaisePostDataChangedEvent ();
  275. Assert.AreEqual (true, eventSelectedIndexChanged, "RaisePostDataChangedEvent#2");
  276. }
  277. bool eventSelectedIndexChanged;
  278. void c_SelectedIndexChanged (object sender, EventArgs e)
  279. {
  280. eventSelectedIndexChanged = true;
  281. }
  282. [Test]
  283. [Category("NunitWeb")]
  284. public void GetItemStyle ()
  285. {
  286. WebTest t = new WebTest (PageInvoker.CreateOnLoad (GetItemStyle_Load));
  287. string html = t.Run ();
  288. string ctrl = HtmlDiff.GetControlFromPageHtml (html);
  289. if (ctrl == string.Empty)
  290. Assert.Fail ("CheckBoxList not created fail");
  291. Assert.AreEqual ("GetItemStyle", (string) t.UserData, "GetItemStyle not done");
  292. if ( ctrl.IndexOf("<td style=\"background-color:Red;border-style:Solid;\">") == -1)
  293. Assert.Fail ("CheckBoxList style not rendered");
  294. }
  295. public static void GetItemStyle_Load (Page p)
  296. {
  297. CheckBoxListPoker c = new CheckBoxListPoker ();
  298. ListItem l1 = new ListItem ("item1", "value1");
  299. ListItem l2 = new ListItem ("item2", "value2");
  300. c.Items.Add (l1);
  301. c.Items.Add (l2);
  302. p.Form.Controls.Add(new LiteralControl(HtmlDiff.BEGIN_TAG));
  303. p.Form.Controls.Add (c);
  304. p.Form.Controls.Add(new LiteralControl(HtmlDiff.END_TAG));
  305. }
  306. [Test]
  307. public void RenderItem ()
  308. {
  309. string origHtml1 = "<input id=\"0\" type=\"checkbox\" name=\"0\" value=\"value1\" /><label for=\"0\">item1</label>";
  310. string origHtml2 = "<input id=\"1\" type=\"checkbox\" name=\"1\" value=\"value2\" /><label for=\"1\">item2</label>";
  311. CheckBoxListPoker c = new CheckBoxListPoker ();
  312. ListItem l1 = new ListItem ("item1", "value1");
  313. ListItem l2 = new ListItem ("item2", "value2");
  314. c.Items.Add (l1);
  315. c.Items.Add (l2);
  316. string html = c.RenderItem (ListItemType.Item, 0, null);
  317. HtmlDiff.AssertAreEqual (origHtml1, html, "RenderItem#1");
  318. html = c.RenderItem (ListItemType.Item, 1, null);
  319. HtmlDiff.AssertAreEqual (origHtml2, html, "RenderItem#2");
  320. }
  321. [Test]
  322. public void RepeatedItemCount ()
  323. {
  324. CheckBoxListPoker c = new CheckBoxListPoker ();
  325. ListItem l1 = new ListItem ("item1", "value1");
  326. ListItem l2 = new ListItem ("item2", "value2");
  327. Assert.AreEqual (0, c.RepeatedItemCount, "RepeatedItemCount#1");
  328. c.Items.Add (l1);
  329. c.Items.Add (l2);
  330. Assert.AreEqual (2, c.RepeatedItemCount, "RepeatedItemCount#2");
  331. }
  332. [Test]
  333. public void CleanProperties ()
  334. {
  335. CheckBoxList c = new CheckBoxList ();
  336. c.CellPadding = Int32.MaxValue;
  337. Assert.AreEqual (c.CellPadding, Int32.MaxValue, "A1");
  338. c.CellSpacing = Int32.MaxValue;
  339. Assert.AreEqual (c.CellSpacing, Int32.MaxValue, "A2");
  340. c.RepeatColumns = Int32.MaxValue;
  341. Assert.AreEqual (c.RepeatColumns, Int32.MaxValue, "A3");
  342. foreach (RepeatDirection d in
  343. Enum.GetValues (typeof (RepeatDirection))) {
  344. c.RepeatDirection = d;
  345. Assert.AreEqual (c.RepeatDirection, d, "A4-" + d);
  346. }
  347. foreach (RepeatLayout l in
  348. Enum.GetValues (typeof (RepeatLayout))) {
  349. c.RepeatLayout = l;
  350. Assert.AreEqual (c.RepeatLayout, l, "A5-" + l);
  351. }
  352. }
  353. [Test]
  354. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  355. public void CellPaddingTooLow ()
  356. {
  357. CheckBoxList c = new CheckBoxList ();
  358. c.CellPadding = -2;
  359. }
  360. [Test]
  361. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  362. public void CellSpacingTooLow ()
  363. {
  364. CheckBoxList c = new CheckBoxList ();
  365. c.CellSpacing = -2;
  366. }
  367. [Test]
  368. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  369. public void RepeatColumsTooLow ()
  370. {
  371. CheckBoxList c = new CheckBoxList ();
  372. c.RepeatColumns = -1;
  373. }
  374. [Test]
  375. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  376. public void RepeatDirection_Invalid ()
  377. {
  378. CheckBoxList c = new CheckBoxList ();
  379. c.RepeatDirection = (RepeatDirection) Int32.MaxValue;
  380. }
  381. [Test]
  382. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  383. public void RepeatLayout_Invalid ()
  384. {
  385. CheckBoxList c = new CheckBoxList ();
  386. c.RepeatLayout = (RepeatLayout) Int32.MaxValue;
  387. }
  388. [Test]
  389. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  390. public void TextAlign_Invalid ()
  391. {
  392. CheckBoxList c = new CheckBoxList ();
  393. c.TextAlign = (TextAlign) Int32.MaxValue;
  394. }
  395. [Test]
  396. public void ChildCheckBoxControl ()
  397. {
  398. CheckBoxList c = new CheckBoxList ();
  399. Assert.AreEqual (c.Controls.Count, 1, "A1");
  400. Assert.AreEqual (c.Controls [0].GetType (), typeof (CheckBox), "A2");
  401. }
  402. [Test]
  403. public void CreateStyle ()
  404. {
  405. CheckBoxListPoker c = new CheckBoxListPoker ();
  406. Assert.AreEqual (c.CreateStyle ().GetType (), typeof (TableStyle), "A1");
  407. }
  408. [Test]
  409. public void RepeatInfoProperties ()
  410. {
  411. IRepeatInfoUser ri = new CheckBoxList ();
  412. Assert.IsFalse (ri.HasFooter, "A1");
  413. Assert.IsFalse (ri.HasHeader, "A2");
  414. Assert.IsFalse (ri.HasSeparators, "A3");
  415. Assert.AreEqual (ri.RepeatedItemCount, 0, "A4");
  416. }
  417. [Test]
  418. public void RepeatInfoCount ()
  419. {
  420. CheckBoxList c = new CheckBoxList ();
  421. IRepeatInfoUser ri = (IRepeatInfoUser) c;
  422. Assert.AreEqual (ri.RepeatedItemCount, 0, "A1");
  423. c.Items.Add ("one");
  424. c.Items.Add ("two");
  425. c.Items.Add ("three");
  426. Assert.AreEqual (ri.RepeatedItemCount, 3, "A2");
  427. }
  428. [Test]
  429. public void RepeatInfoStyle ()
  430. {
  431. IRepeatInfoUser ri = new CheckBoxList ();
  432. foreach (ListItemType t in Enum.GetValues (typeof (ListItemType))) {
  433. Assert.AreEqual (ri.GetItemStyle (t, 0), null, "A1-" + t);
  434. Assert.AreEqual (ri.GetItemStyle (t, 1), null, "A2-" + t);
  435. Assert.AreEqual (ri.GetItemStyle (t, 2), null, "A3-" + t);
  436. Assert.AreEqual (ri.GetItemStyle (t, 3), null, "A4-" + t);
  437. }
  438. }
  439. [Test]
  440. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  441. public void RepeatInfoRenderOutOfRange ()
  442. {
  443. StringWriter sw = new StringWriter ();
  444. HtmlTextWriter tw = new HtmlTextWriter (sw);
  445. IRepeatInfoUser ri = new CheckBoxList ();
  446. ri.RenderItem (ListItemType.Item, -1, new RepeatInfo (), tw);
  447. }
  448. [Test]
  449. public void RepeatInfoRenderItem ()
  450. {
  451. string origHtml = "<input id=\"0\" type=\"checkbox\" name=\"0\" value=\"one\" /><label for=\"0\">one</label>";
  452. StringWriter sw = new StringWriter ();
  453. HtmlTextWriter tw = new HtmlTextWriter (sw);
  454. CheckBoxList c = new CheckBoxList ();
  455. IRepeatInfoUser ri = (IRepeatInfoUser) c;
  456. RepeatInfo r = new RepeatInfo ();
  457. c.Items.Add ("one");
  458. c.Items.Add ("two");
  459. ri.RenderItem (ListItemType.Item, 0, r, tw);
  460. string html = sw.ToString ();
  461. Assert.AreEqual (origHtml, html, "A1");
  462. }
  463. [Test]
  464. public void FindControl ()
  465. {
  466. CheckBoxListPoker p = new CheckBoxListPoker ();
  467. p.ID = "id";
  468. p.Items.Add ("one");
  469. p.Items.Add ("two");
  470. p.Items.Add ("three");
  471. // Everything seems to return this.
  472. Assert.AreEqual (p.FindControlPoke (String.Empty, 0), p, "A1");
  473. Assert.AreEqual (p.FindControlPoke ("id", 0), p, "A2");
  474. Assert.AreEqual (p.FindControlPoke ("id_0", 0), p, "A3");
  475. Assert.AreEqual (p.FindControlPoke ("id_1", 0), p, "A4");
  476. Assert.AreEqual (p.FindControlPoke ("id_2", 0), p, "A5");
  477. Assert.AreEqual (p.FindControlPoke ("id_3", 0), p, "A6");
  478. Assert.AreEqual (p.FindControlPoke ("0", 0), p, "A7");
  479. Assert.AreEqual (p.FindControlPoke (String.Empty, 10), p, "A1");
  480. Assert.AreEqual (p.FindControlPoke ("id", 10), p, "A2");
  481. Assert.AreEqual (p.FindControlPoke ("id_0", 10), p, "A3");
  482. Assert.AreEqual (p.FindControlPoke ("id_1", 10), p, "A4");
  483. Assert.AreEqual (p.FindControlPoke ("id_2", 10), p, "A5");
  484. Assert.AreEqual (p.FindControlPoke ("id_3", 10), p, "A6");
  485. Assert.AreEqual (p.FindControlPoke ("0", 10), p, "A7");
  486. }
  487. private void Render (CheckBoxList list, string expected, string test)
  488. {
  489. StringWriter sw = new StringWriter ();
  490. HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
  491. sw.NewLine = "\n";
  492. list.RenderControl (tw);
  493. HtmlDiff.AssertAreEqual (expected, sw.ToString (), test);
  494. }
  495. [Test]
  496. public void RenderEmpty ()
  497. {
  498. CheckBoxList c = new CheckBoxList ();
  499. Render (c, "", "A1");
  500. c.CellPadding = 1;
  501. Render (c, "", "A2");
  502. c = new CheckBoxList ();
  503. c.CellPadding = 1;
  504. Render (c, "", "A3");
  505. c = new CheckBoxList ();
  506. c.TextAlign = TextAlign.Left;
  507. Render (c, "", "A4");
  508. }
  509. [Test]
  510. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  511. public void Render ()
  512. {
  513. string origHtml1 = "<table>\n\t<tr>\n\t\t<td><input id=\"0\" name=\"0\" type=\"checkbox\" value=\"foo\"/><label for=\"0\">foo</label></td>\n\t</tr>\n</table>";
  514. string origHtml2 = "<table>\n\t<tr>\n\t\t<td><input id=\"0\" name=\"0\" type=\"checkbox\" value=\"foo\"/><label for=\"0\">foo</label></td>\n\t</tr>\n</table>";
  515. CheckBoxList c;
  516. c = new CheckBoxList ();
  517. c.Items.Add ("foo");
  518. Render (c, origHtml1, "A5");
  519. c = new CheckBoxList ();
  520. c.Items.Add ("foo");
  521. Render (c, origHtml2, "A6");
  522. }
  523. // bug 51648
  524. [Test]
  525. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  526. public void TestTabIndex ()
  527. {
  528. CheckBoxList c = new CheckBoxList ();
  529. c.TabIndex = 5;
  530. c.Items.Add ("Item1");
  531. string exp = @"<table>
  532. <tr>
  533. <td><input id=""0"" name=""0"" tabindex=""5"" type=""checkbox"" value=""Item1""/><label for=""0"">Item1</label></td>
  534. </tr>
  535. </table>";
  536. Render (c, exp, "B1");
  537. }
  538. // bug 48802
  539. [Test]
  540. [Category("NotDotNet")] // MS's implementation throws NRE's from these
  541. public void TestDisabled ()
  542. {
  543. CheckBoxList c = new CheckBoxList ();
  544. c.Enabled = false;
  545. c.Items.Add ("Item1");
  546. string exp = @"<table class=""aspNetDisabled"">
  547. <tr>
  548. <td><span class=""aspNetDisabled""><input disabled=""disabled"" id=""0"" name=""0"" type=""checkbox"" value=""Item1""/><label for=""0"">Item1</label></span></td>
  549. </tr>
  550. </table>";
  551. Render (c, exp, "C1");
  552. }
  553. class TestCheckBoxList : CheckBoxList
  554. {
  555. public void CallVerifyMultiSelect()
  556. {
  557. base.VerifyMultiSelect();
  558. }
  559. }
  560. [Test]
  561. public void VerifyMultiSelectTest()
  562. {
  563. TestCheckBoxList list = new TestCheckBoxList();
  564. list.CallVerifyMultiSelect();
  565. }
  566. [TestFixtureTearDown]
  567. public void teardown ()
  568. {
  569. WebTest.Unload ();
  570. }
  571. }
  572. }