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 using System.Collections;
40 using MonoTests.SystemWeb.Framework;
41 using MonoTests.stand_alone.WebHarness;
42 using System.Collections.Specialized;
44 namespace MonoTests.System.Web.UI.WebControls {
46 public class CheckBoxListPoker : CheckBoxList {
48 public Style CreateStyle ()
50 return CreateControlStyle ();
53 public Control FindControlPoke (string name, int offset)
55 return FindControl (name, offset);
58 public string Render ()
60 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
62 return writer.InnerWriter.ToString ();
67 public new bool HasFooter
71 return base.HasFooter;
75 public new bool HasHeader
79 return base.HasHeader;
83 public new bool HasSeparators
87 return base.HasSeparators;
91 public new int RepeatedItemCount
95 return base.RepeatedItemCount;
99 public new void RaisePostDataChangedEvent ()
101 base.RaisePostDataChangedEvent ();
104 protected override Style GetItemStyle (ListItemType itemType, int repeatIndex)
106 Style s = new Style();
107 s.BackColor = Color.Red;
108 s.BorderStyle = BorderStyle.Solid;
109 WebTest.CurrentTest.UserData = "GetItemStyle";
113 public Style DoGetItemStyle (ListItemType itemType, int repeatIndex)
115 return base.GetItemStyle (itemType, repeatIndex);
118 public new string RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo)
120 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
121 base.RenderItem(itemType,repeatIndex,repeatInfo,writer);
122 return writer.InnerWriter.ToString ();
128 public class CheckBoxListTest {
131 public void Defaults ()
133 CheckBoxListPoker c = new CheckBoxListPoker ();
135 Assert.AreEqual (c.CellPadding, -1, "A1");
136 Assert.AreEqual (c.CellSpacing, -1, "A2");
137 Assert.AreEqual (c.RepeatColumns, 0, "A3");
138 Assert.AreEqual (c.RepeatDirection,
139 RepeatDirection.Vertical, "A4");
140 Assert.AreEqual (c.RepeatLayout,
141 RepeatLayout.Table, "A5");
142 Assert.AreEqual (c.TextAlign, TextAlign.Right, "A6");
144 Assert.AreEqual (false, c.HasFooter, "HasFooter");
145 Assert.AreEqual (false, c.HasHeader, "HasHeader");
146 Assert.AreEqual (false, c.HasSeparators, "HasSeparators");
147 Assert.AreEqual (0, c.RepeatedItemCount, "RepeatedItemCount");
148 Assert.AreEqual (null, c.DoGetItemStyle (ListItemType.Item, 0), "GetItemStyle");
154 public void RaisePostDataChangedEvent ()
156 CheckBoxListPoker c = new CheckBoxListPoker ();
157 c.SelectedIndexChanged += new EventHandler (c_SelectedIndexChanged);
158 Assert.AreEqual (false, eventSelectedIndexChanged, "RaisePostDataChangedEvent#1");
159 c.RaisePostDataChangedEvent ();
160 Assert.AreEqual (true, eventSelectedIndexChanged, "RaisePostDataChangedEvent#2");
163 bool eventSelectedIndexChanged;
164 void c_SelectedIndexChanged (object sender, EventArgs e)
166 eventSelectedIndexChanged = true;
170 [Category("NunitWeb")]
171 public void GetItemStyle ()
173 WebTest t = new WebTest (PageInvoker.CreateOnLoad (GetItemStyle_Load));
174 string html = t.Run ();
175 string ctrl = HtmlDiff.GetControlFromPageHtml (html);
176 if (ctrl == string.Empty)
177 Assert.Fail ("CheckBoxList not created fail");
178 Assert.AreEqual ("GetItemStyle", (string) t.UserData, "GetItemStyle not done");
179 if ( ctrl.IndexOf("<td style=\"background-color:Red;border-style:Solid;\">") == -1)
180 Assert.Fail ("CheckBoxList style not rendered");
183 public static void GetItemStyle_Load (Page p)
185 CheckBoxListPoker c = new CheckBoxListPoker ();
186 ListItem l1 = new ListItem ("item1", "value1");
187 ListItem l2 = new ListItem ("item2", "value2");
191 p.Form.Controls.Add(new LiteralControl(HtmlDiff.BEGIN_TAG));
192 p.Form.Controls.Add (c);
193 p.Form.Controls.Add(new LiteralControl(HtmlDiff.END_TAG));
197 public void RenderItem ()
199 CheckBoxListPoker c = new CheckBoxListPoker ();
200 ListItem l1 = new ListItem ("item1", "value1");
201 ListItem l2 = new ListItem ("item2", "value2");
205 string html = c.RenderItem (ListItemType.Item, 0, null);
206 HtmlDiff.AssertAreEqual ("<input id=\"0\" type=\"checkbox\" name=\"0\" /><label for=\"0\">item1</label>", html, "RenderItem#1");
207 html = c.RenderItem (ListItemType.Item, 1, null);
208 HtmlDiff.AssertAreEqual ("<input id=\"1\" type=\"checkbox\" name=\"1\" /><label for=\"1\">item2</label>", html, "RenderItem#2");
212 public void RepeatedItemCount ()
214 CheckBoxListPoker c = new CheckBoxListPoker ();
215 ListItem l1 = new ListItem ("item1", "value1");
216 ListItem l2 = new ListItem ("item2", "value2");
217 Assert.AreEqual (0, c.RepeatedItemCount, "RepeatedItemCount#1");
220 Assert.AreEqual (2, c.RepeatedItemCount, "RepeatedItemCount#2");
227 public void CleanProperties ()
229 CheckBoxList c = new CheckBoxList ();
231 c.CellPadding = Int32.MaxValue;
232 Assert.AreEqual (c.CellPadding, Int32.MaxValue, "A1");
234 c.CellSpacing = Int32.MaxValue;
235 Assert.AreEqual (c.CellSpacing, Int32.MaxValue, "A2");
237 c.RepeatColumns = Int32.MaxValue;
238 Assert.AreEqual (c.RepeatColumns, Int32.MaxValue, "A3");
240 foreach (RepeatDirection d in
241 Enum.GetValues (typeof (RepeatDirection))) {
242 c.RepeatDirection = d;
243 Assert.AreEqual (c.RepeatDirection, d, "A4-" + d);
246 foreach (RepeatLayout l in
247 Enum.GetValues (typeof (RepeatLayout))) {
249 Assert.AreEqual (c.RepeatLayout, l, "A5-" + l);
254 [ExpectedException (typeof (ArgumentOutOfRangeException))]
255 public void CellPaddingTooLow ()
257 CheckBoxList c = new CheckBoxList ();
262 [ExpectedException (typeof (ArgumentOutOfRangeException))]
263 public void CellSpacingTooLow ()
265 CheckBoxList c = new CheckBoxList ();
270 [ExpectedException (typeof (ArgumentOutOfRangeException))]
271 public void RepeatColumsTooLow ()
273 CheckBoxList c = new CheckBoxList ();
274 c.RepeatColumns = -1;
278 [ExpectedException (typeof (ArgumentOutOfRangeException))]
279 public void RepeatDirection_Invalid ()
281 CheckBoxList c = new CheckBoxList ();
282 c.RepeatDirection = (RepeatDirection) Int32.MaxValue;
286 [ExpectedException (typeof (ArgumentOutOfRangeException))]
287 public void RepeatLayout_Invalid ()
289 CheckBoxList c = new CheckBoxList ();
290 c.RepeatLayout = (RepeatLayout) Int32.MaxValue;
294 [ExpectedException (typeof (ArgumentOutOfRangeException))]
295 public void TextAlign_Invalid ()
297 CheckBoxList c = new CheckBoxList ();
298 c.TextAlign = (TextAlign) Int32.MaxValue;
302 public void ChildCheckBoxControl ()
304 CheckBoxList c = new CheckBoxList ();
305 Assert.AreEqual (c.Controls.Count, 1, "A1");
306 Assert.AreEqual (c.Controls [0].GetType (), typeof (CheckBox), "A2");
310 public void CreateStyle ()
312 CheckBoxListPoker c = new CheckBoxListPoker ();
313 Assert.AreEqual (c.CreateStyle ().GetType (), typeof (TableStyle), "A1");
317 public void RepeatInfoProperties ()
319 IRepeatInfoUser ri = new CheckBoxList ();
321 Assert.IsFalse (ri.HasFooter, "A1");
322 Assert.IsFalse (ri.HasHeader, "A2");
323 Assert.IsFalse (ri.HasSeparators, "A3");
324 Assert.AreEqual (ri.RepeatedItemCount, 0, "A4");
328 public void RepeatInfoCount ()
330 CheckBoxList c = new CheckBoxList ();
331 IRepeatInfoUser ri = (IRepeatInfoUser) c;
333 Assert.AreEqual (ri.RepeatedItemCount, 0, "A1");
337 c.Items.Add ("three");
338 Assert.AreEqual (ri.RepeatedItemCount, 3, "A2");
342 public void RepeatInfoStyle ()
344 IRepeatInfoUser ri = new CheckBoxList ();
346 foreach (ListItemType t in Enum.GetValues (typeof (ListItemType))) {
347 Assert.AreEqual (ri.GetItemStyle (t, 0), null, "A1-" + t);
348 Assert.AreEqual (ri.GetItemStyle (t, 1), null, "A2-" + t);
349 Assert.AreEqual (ri.GetItemStyle (t, 2), null, "A3-" + t);
350 Assert.AreEqual (ri.GetItemStyle (t, 3), null, "A4-" + t);
355 [ExpectedException (typeof (ArgumentOutOfRangeException))]
356 public void RepeatInfoRenderOutOfRange ()
358 StringWriter sw = new StringWriter ();
359 HtmlTextWriter tw = new HtmlTextWriter (sw);
360 IRepeatInfoUser ri = new CheckBoxList ();
362 ri.RenderItem (ListItemType.Item, -1, new RepeatInfo (), tw);
366 public void RepeatInfoRenderItem ()
368 StringWriter sw = new StringWriter ();
369 HtmlTextWriter tw = new HtmlTextWriter (sw);
370 CheckBoxList c = new CheckBoxList ();
371 IRepeatInfoUser ri = (IRepeatInfoUser) c;
372 RepeatInfo r = new RepeatInfo ();
377 ri.RenderItem (ListItemType.Item, 0, r, tw);
378 Assert.AreEqual ("<input id=\"0\" type=\"checkbox\" name=\"0\" />" +
379 "<label for=\"0\">one</label>", sw.ToString (), "A1");
383 public void FindControl ()
385 CheckBoxListPoker p = new CheckBoxListPoker ();
390 p.Items.Add ("three");
392 // Everything seems to return this.
393 Assert.AreEqual (p.FindControlPoke (String.Empty, 0), p, "A1");
394 Assert.AreEqual (p.FindControlPoke ("id", 0), p, "A2");
395 Assert.AreEqual (p.FindControlPoke ("id_0", 0), p, "A3");
396 Assert.AreEqual (p.FindControlPoke ("id_1", 0), p, "A4");
397 Assert.AreEqual (p.FindControlPoke ("id_2", 0), p, "A5");
398 Assert.AreEqual (p.FindControlPoke ("id_3", 0), p, "A6");
399 Assert.AreEqual (p.FindControlPoke ("0", 0), p, "A7");
401 Assert.AreEqual (p.FindControlPoke (String.Empty, 10), p, "A1");
402 Assert.AreEqual (p.FindControlPoke ("id", 10), p, "A2");
403 Assert.AreEqual (p.FindControlPoke ("id_0", 10), p, "A3");
404 Assert.AreEqual (p.FindControlPoke ("id_1", 10), p, "A4");
405 Assert.AreEqual (p.FindControlPoke ("id_2", 10), p, "A5");
406 Assert.AreEqual (p.FindControlPoke ("id_3", 10), p, "A6");
407 Assert.AreEqual (p.FindControlPoke ("0", 10), p, "A7");
410 private void Render (CheckBoxList list, string expected, string test)
412 StringWriter sw = new StringWriter ();
413 HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
416 list.RenderControl (tw);
417 HtmlDiff.AssertAreEqual (expected, sw.ToString (), test);
421 public void RenderEmpty ()
423 CheckBoxList c = new CheckBoxList ();
426 Render (c, "", "A1");
428 Render (c, "<table border=\"0\">\n\n</table>", "A1");
432 Render (c, "", "A2");
434 Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A2");
437 c = new CheckBoxList ();
440 Render (c, "", "A3");
442 Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A3");
445 c = new CheckBoxList ();
446 c.TextAlign = TextAlign.Left;
448 Render (c, "", "A4");
450 Render (c, "<table border=\"0\">\n\n</table>", "A4");
456 [Category("NotDotNet")] // MS's implementation throws NRE's from these
458 public void Render ()
461 c = new CheckBoxList ();
463 Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
464 "name=\"0\" type=\"checkbox\" />" +
465 "<label for=\"0\">foo</label>" +
466 "</td>\n\t</tr>\n</table>", "A5");
468 c = new CheckBoxList ();
470 Render (c, "<table border=\"0\">\n\t<tr>\n\t\t<td><input id=\"0\" " +
471 "name=\"0\" type=\"checkbox\" />" +
472 "<label for=\"0\">foo</label>" +
473 "</td>\n\t</tr>\n</table>", "A6");
479 [Category("NotDotNet")] // MS's implementation throws NRE's from these
481 public void TestTabIndex ()
483 CheckBoxList c = new CheckBoxList ();
485 c.Items.Add ("Item1");
486 string exp = @"<table border=""0"">
488 <td><input id=""0"" name=""0"" tabindex=""5"" type=""checkbox"" /><label for=""0"">Item1</label></td>
491 Render (c, exp, "B1");
497 [Category("NotDotNet")] // MS's implementation throws NRE's from these
499 public void TestDisabled ()
501 CheckBoxList c = new CheckBoxList ();
503 c.Items.Add ("Item1");
504 string exp = @"<table border=""0"" disabled=""disabled"">
506 <td><span disabled=""disabled""><input disabled=""disabled"" id=""0"" name=""0"" type=""checkbox"" /><label for=""0"">Item1</label></span></td>
509 Render (c, exp, "C1");
512 class TestCheckBoxList : CheckBoxList
514 public new virtual void VerifyMultiSelect()
516 base.VerifyMultiSelect();
520 public void VerifyMultiSelectTest()
522 TestCheckBoxList list = new TestCheckBoxList();
523 list.VerifyMultiSelect();
525 [TestFixtureTearDown]
526 public void teardown ()