2 // Tests for System.Web.UI.WebControls.CheckBoxList.cs
5 // Jackson Harper (jackson@ximian.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
34 using System.Globalization;
37 using System.Web.UI.WebControls;
39 namespace MonoTests.System.Web.UI.WebControls {
41 public class CheckBoxListPoker : CheckBoxList {
43 public Style CreateStyle ()
45 return CreateControlStyle ();
48 public Control FindControlPoke (string name, int offset)
50 return FindControl (name, offset);
55 public class CheckBoxListTest {
58 public void Defaults ()
60 CheckBoxList c = new CheckBoxList ();
62 Assert.AreEqual (c.CellPadding, -1, "A1");
63 Assert.AreEqual (c.CellSpacing, -1, "A2");
64 Assert.AreEqual (c.RepeatColumns, 0, "A3");
65 Assert.AreEqual (c.RepeatDirection,
66 RepeatDirection.Vertical, "A4");
67 Assert.AreEqual (c.RepeatLayout,
68 RepeatLayout.Table, "A5");
69 Assert.AreEqual (c.TextAlign, TextAlign.Right, "A6");
73 public void CleanProperties ()
75 CheckBoxList c = new CheckBoxList ();
77 c.CellPadding = Int32.MaxValue;
78 Assert.AreEqual (c.CellPadding, Int32.MaxValue, "A1");
80 c.CellSpacing = Int32.MaxValue;
81 Assert.AreEqual (c.CellSpacing, Int32.MaxValue, "A2");
83 c.RepeatColumns = Int32.MaxValue;
84 Assert.AreEqual (c.RepeatColumns, Int32.MaxValue, "A3");
86 foreach (RepeatDirection d in
87 Enum.GetValues (typeof (RepeatDirection))) {
88 c.RepeatDirection = d;
89 Assert.AreEqual (c.RepeatDirection, d, "A4-" + d);
92 foreach (RepeatLayout l in
93 Enum.GetValues (typeof (RepeatLayout))) {
95 Assert.AreEqual (c.RepeatLayout, l, "A5-" + l);
100 [ExpectedException (typeof (ArgumentOutOfRangeException))]
101 public void CellPaddingTooLow ()
103 CheckBoxList c = new CheckBoxList ();
108 [ExpectedException (typeof (ArgumentOutOfRangeException))]
109 public void CellSpacingTooLow ()
111 CheckBoxList c = new CheckBoxList ();
116 [ExpectedException (typeof (ArgumentOutOfRangeException))]
117 public void RepeatColumsTooLow ()
119 CheckBoxList c = new CheckBoxList ();
120 c.RepeatColumns = -1;
124 [ExpectedException (typeof (ArgumentOutOfRangeException))]
125 public void RepeatDirection_Invalid ()
127 CheckBoxList c = new CheckBoxList ();
128 c.RepeatDirection = (RepeatDirection) Int32.MaxValue;
132 [ExpectedException (typeof (ArgumentOutOfRangeException))]
133 public void RepeatLayout_Invalid ()
135 CheckBoxList c = new CheckBoxList ();
136 c.RepeatLayout = (RepeatLayout) Int32.MaxValue;
140 [ExpectedException (typeof (ArgumentOutOfRangeException))]
141 public void TextAlign_Invalid ()
143 CheckBoxList c = new CheckBoxList ();
144 c.TextAlign = (TextAlign) Int32.MaxValue;
148 public void ChildCheckBoxControl ()
150 CheckBoxList c = new CheckBoxList ();
151 Assert.AreEqual (c.Controls.Count, 1, "A1");
152 Assert.AreEqual (c.Controls [0].GetType (), typeof (CheckBox), "A2");
156 public void CreateStyle ()
158 CheckBoxListPoker c = new CheckBoxListPoker ();
159 Assert.AreEqual (c.CreateStyle ().GetType (), typeof (TableStyle), "A1");
163 public void RepeatInfoProperties ()
165 IRepeatInfoUser ri = new CheckBoxList ();
167 Assert.IsFalse (ri.HasFooter, "A1");
168 Assert.IsFalse (ri.HasHeader, "A2");
169 Assert.IsFalse (ri.HasSeparators, "A3");
170 Assert.AreEqual (ri.RepeatedItemCount, 0, "A4");
174 public void RepeatInfoCount ()
176 CheckBoxList c = new CheckBoxList ();
177 IRepeatInfoUser ri = (IRepeatInfoUser) c;
179 Assert.AreEqual (ri.RepeatedItemCount, 0, "A1");
183 c.Items.Add ("three");
184 Assert.AreEqual (ri.RepeatedItemCount, 3, "A2");
188 public void RepeatInfoStyle ()
190 IRepeatInfoUser ri = new CheckBoxList ();
192 foreach (ListItemType t in Enum.GetValues (typeof (ListItemType))) {
193 Assert.AreEqual (ri.GetItemStyle (t, 0), null, "A1-" + t);
194 Assert.AreEqual (ri.GetItemStyle (t, 1), null, "A2-" + t);
195 Assert.AreEqual (ri.GetItemStyle (t, 2), null, "A3-" + t);
196 Assert.AreEqual (ri.GetItemStyle (t, 3), null, "A4-" + t);
201 [ExpectedException (typeof (ArgumentOutOfRangeException))]
202 public void RepeatInfoRenderOutOfRange ()
204 StringWriter sw = new StringWriter ();
205 HtmlTextWriter tw = new HtmlTextWriter (sw);
206 IRepeatInfoUser ri = new CheckBoxList ();
208 ri.RenderItem (ListItemType.Item, -1, new RepeatInfo (), tw);
212 public void RepeatInfoRenderItem ()
214 StringWriter sw = new StringWriter ();
215 HtmlTextWriter tw = new HtmlTextWriter (sw);
216 CheckBoxList c = new CheckBoxList ();
217 IRepeatInfoUser ri = (IRepeatInfoUser) c;
218 RepeatInfo r = new RepeatInfo ();
223 ri.RenderItem (ListItemType.Item, 0, r, tw);
224 Assert.AreEqual ("<input id=\"0\" type=\"checkbox\" name=\"0\" />" +
225 "<label for=\"0\">one</label>", sw.ToString (), "A1");
229 public void FindControl ()
231 CheckBoxListPoker p = new CheckBoxListPoker ();
236 p.Items.Add ("three");
238 // Everything seems to return this.
239 Assert.AreEqual (p.FindControlPoke (String.Empty, 0), p, "A1");
240 Assert.AreEqual (p.FindControlPoke ("id", 0), p, "A2");
241 Assert.AreEqual (p.FindControlPoke ("id_0", 0), p, "A3");
242 Assert.AreEqual (p.FindControlPoke ("id_1", 0), p, "A4");
243 Assert.AreEqual (p.FindControlPoke ("id_2", 0), p, "A5");
244 Assert.AreEqual (p.FindControlPoke ("id_3", 0), p, "A6");
245 Assert.AreEqual (p.FindControlPoke ("0", 0), p, "A7");
247 Assert.AreEqual (p.FindControlPoke (String.Empty, 10), p, "A1");
248 Assert.AreEqual (p.FindControlPoke ("id", 10), p, "A2");
249 Assert.AreEqual (p.FindControlPoke ("id_0", 10), p, "A3");
250 Assert.AreEqual (p.FindControlPoke ("id_1", 10), p, "A4");
251 Assert.AreEqual (p.FindControlPoke ("id_2", 10), p, "A5");
252 Assert.AreEqual (p.FindControlPoke ("id_3", 10), p, "A6");
253 Assert.AreEqual (p.FindControlPoke ("0", 10), p, "A7");
256 private void Render (CheckBoxList list, string expected, string test)
258 StringWriter sw = new StringWriter ();
259 HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
262 list.RenderControl (tw);
263 Assert.AreEqual (expected, sw.ToString (), test);
267 public void RenderEmpty ()
269 CheckBoxList c = new CheckBoxList ();
272 Render (c, "", "A1");
274 Render (c, "<table border=\"0\">\n\n</table>", "A1");
278 Render (c, "", "A2");
280 Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A2");
283 c = new CheckBoxList ();
286 Render (c, "", "A3");
288 Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A3");
291 c = new CheckBoxList ();
292 c.TextAlign = TextAlign.Left;
294 Render (c, "", "A4");
296 Render (c, "<table border=\"0\">\n\n</table>", "A4");
302 [Category("NotDotNet")] // MS's implementation throws NRE's from these
304 public void Render ()
307 c = new CheckBoxList ();
309 Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
310 "name=\"0\" type=\"checkbox\" />" +
311 "<label for=\"0\">foo</label>" +
312 "</td>\n\t</tr>\n</table>", "A5");
314 c = new CheckBoxList ();
316 Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
317 "name=\"0\" type=\"checkbox\" />" +
318 "<label for=\"0\">foo</label>" +
319 "</td>\n\t</tr>\n</table>", "A6");
325 [Category("NotDotNet")] // MS's implementation throws NRE's from these
327 public void TestTabIndex ()
329 CheckBoxList c = new CheckBoxList ();
331 c.Items.Add ("Item1");
332 string exp = @"<table border=""0"">
334 <td><input id=""0"" name=""0"" tabindex=""5"" type=""checkbox"" /><label for=""0"">Item1</label></td>
337 Render (c, exp, "B1");
343 [Category("NotDotNet")] // MS's implementation throws NRE's from these
345 public void TestDisabled ()
347 CheckBoxList c = new CheckBoxList ();
349 c.Items.Add ("Item1");
350 string exp = @"<table border=""0"" disabled=""disabled"">
352 <td><span disabled=""disabled""><input disabled=""disabled"" id=""0"" name=""0"" type=""checkbox"" /><label for=""0"">Item1</label></span></td>
355 Render (c, exp, "C1");