2010-01-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / CheckBoxListTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.CheckBoxList.cs 
3 //
4 // Author:
5 //      Jackson Harper (jackson@ximian.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.IO;
34 using System.Globalization;
35 using System.Web;
36 using System.Web.UI;
37 using System.Web.UI.WebControls;
38 using System.Drawing;
39 using System.Collections;
40 using MonoTests.SystemWeb.Framework;
41 using MonoTests.stand_alone.WebHarness;
42 using System.Collections.Specialized;
43
44 namespace MonoTests.System.Web.UI.WebControls {
45
46         public class CheckBoxListPoker : CheckBoxList {
47
48                 public Style CreateStyle ()
49                 {
50                         return CreateControlStyle ();
51                 }
52
53                 public Control FindControlPoke (string name, int offset)
54                 {
55                         return FindControl (name, offset);
56                 }
57
58                 public string Render ()
59                 {
60                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
61                         base.Render (writer);
62                         return writer.InnerWriter.ToString ();
63                 }
64
65                 
66 #if NET_2_0
67                 public new bool HasFooter
68                 {
69                         get
70                         {
71                                 return base.HasFooter;
72                         }
73                 }
74
75                 public new bool HasHeader
76                 {
77                         get
78                         {
79                                 return base.HasHeader;
80                         }
81                 }
82
83                 public new bool HasSeparators
84                 {
85                         get
86                         {
87                                 return base.HasSeparators;
88                         }
89                 }
90
91                 public new int RepeatedItemCount
92                 {
93                         get
94                         {
95                                 return base.RepeatedItemCount;
96                         }
97                 }
98
99                 public new void RaisePostDataChangedEvent ()
100                 {
101                         base.RaisePostDataChangedEvent ();
102                 }
103
104                 protected override Style GetItemStyle (ListItemType itemType, int repeatIndex)
105                 {
106                         Style s = new Style();
107                         s.BackColor = Color.Red;
108                         s.BorderStyle = BorderStyle.Solid;
109                         WebTest.CurrentTest.UserData = "GetItemStyle";
110                         return s;
111                 }
112
113                 public Style DoGetItemStyle (ListItemType itemType, int repeatIndex)
114                 {
115                         return base.GetItemStyle (itemType, repeatIndex);
116                 }
117
118                 public new string RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo)
119                 {
120                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
121                         base.RenderItem(itemType,repeatIndex,repeatInfo,writer);
122                         return writer.InnerWriter.ToString ();
123                 }
124 #endif
125         }
126
127         [TestFixture]
128         public class CheckBoxListTest {
129
130                 [Test]
131                 public void Defaults ()
132                 {
133                         CheckBoxListPoker c = new CheckBoxListPoker ();
134
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");
143 #if NET_2_0
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");
149 #endif
150                 }
151
152 #if NET_2_0
153                 [Test]
154                 public void RaisePostDataChangedEvent ()
155                 {
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");
161                 }
162
163                 bool eventSelectedIndexChanged;
164                 void c_SelectedIndexChanged (object sender, EventArgs e)
165                 {
166                         eventSelectedIndexChanged = true;
167                 }
168
169                 [Test]
170                 [Category("NunitWeb")]
171                 public void GetItemStyle ()
172                 {
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");
181                 }
182
183                 public static void GetItemStyle_Load (Page p)
184                 {
185                         CheckBoxListPoker c = new CheckBoxListPoker ();
186                         ListItem l1 = new ListItem ("item1", "value1");
187                         ListItem l2 = new ListItem ("item2", "value2");
188
189                         c.Items.Add (l1);
190                         c.Items.Add (l2);
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));
194                 }
195
196                 [Test]
197                 public void RenderItem ()
198                 {
199                         CheckBoxListPoker c = new CheckBoxListPoker ();
200                         ListItem l1 = new ListItem ("item1", "value1");
201                         ListItem l2 = new ListItem ("item2", "value2");
202
203                         c.Items.Add (l1);
204                         c.Items.Add (l2);
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");
209                 }
210
211                 [Test]
212                 public void RepeatedItemCount ()
213                 {
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");
218                         c.Items.Add (l1);
219                         c.Items.Add (l2);
220                         Assert.AreEqual (2, c.RepeatedItemCount, "RepeatedItemCount#2");
221                 }
222
223 #endif
224
225
226                 [Test]
227                 public void CleanProperties ()
228                 {
229                         CheckBoxList c = new CheckBoxList ();
230
231                         c.CellPadding = Int32.MaxValue;
232                         Assert.AreEqual (c.CellPadding, Int32.MaxValue, "A1");
233
234                         c.CellSpacing = Int32.MaxValue;
235                         Assert.AreEqual (c.CellSpacing, Int32.MaxValue, "A2");
236
237                         c.RepeatColumns = Int32.MaxValue;
238                         Assert.AreEqual (c.RepeatColumns, Int32.MaxValue, "A3");
239
240                         foreach (RepeatDirection d in
241                                         Enum.GetValues (typeof (RepeatDirection))) {
242                                 c.RepeatDirection = d;
243                                 Assert.AreEqual (c.RepeatDirection, d, "A4-" + d);
244                         }
245
246                         foreach (RepeatLayout l in
247                                         Enum.GetValues (typeof (RepeatLayout))) {
248                                 c.RepeatLayout = l;
249                                 Assert.AreEqual (c.RepeatLayout, l, "A5-" + l);
250                         }
251                 }
252
253                 [Test]
254                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
255                 public void CellPaddingTooLow ()
256                 {
257                         CheckBoxList c = new CheckBoxList ();
258                         c.CellPadding = -2;
259                 }
260
261                 [Test]
262                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
263                 public void CellSpacingTooLow ()
264                 {
265                         CheckBoxList c = new CheckBoxList ();
266                         c.CellSpacing = -2;
267                 }
268
269                 [Test]
270                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
271                 public void RepeatColumsTooLow ()
272                 {
273                         CheckBoxList c = new CheckBoxList ();
274                         c.RepeatColumns = -1;
275                 }
276
277                 [Test]
278                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
279                 public void RepeatDirection_Invalid ()
280                 {
281                         CheckBoxList c = new CheckBoxList ();
282                         c.RepeatDirection = (RepeatDirection) Int32.MaxValue;
283                 }
284
285                 [Test]
286                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
287                 public void RepeatLayout_Invalid ()
288                 {
289                         CheckBoxList c = new CheckBoxList ();
290                         c.RepeatLayout = (RepeatLayout) Int32.MaxValue;
291                 }
292
293                 [Test]
294                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
295                 public void TextAlign_Invalid ()
296                 {
297                         CheckBoxList c = new CheckBoxList ();
298                         c.TextAlign = (TextAlign) Int32.MaxValue;
299                 }
300
301                 [Test]
302                 public void ChildCheckBoxControl ()
303                 {
304                         CheckBoxList c = new CheckBoxList ();
305                         Assert.AreEqual (c.Controls.Count, 1, "A1");
306                         Assert.AreEqual (c.Controls [0].GetType (), typeof (CheckBox), "A2");
307                 }
308
309                 [Test]
310                 public void CreateStyle ()
311                 {
312                         CheckBoxListPoker c = new CheckBoxListPoker ();
313                         Assert.AreEqual (c.CreateStyle ().GetType (), typeof (TableStyle), "A1");
314                 }
315
316                 [Test]
317                 public void RepeatInfoProperties ()
318                 {
319                         IRepeatInfoUser ri = new CheckBoxList ();
320
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");
325                 }
326
327                 [Test]
328                 public void RepeatInfoCount ()
329                 {
330                         CheckBoxList c = new CheckBoxList ();
331                         IRepeatInfoUser ri = (IRepeatInfoUser) c;
332
333                         Assert.AreEqual (ri.RepeatedItemCount, 0, "A1");
334
335                         c.Items.Add ("one");
336                         c.Items.Add ("two");
337                         c.Items.Add ("three");
338                         Assert.AreEqual (ri.RepeatedItemCount, 3, "A2");
339                 }
340
341                 [Test]
342                 public void RepeatInfoStyle ()
343                 {
344                         IRepeatInfoUser ri = new CheckBoxList ();
345
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);
351                         }
352                 }
353
354                 [Test]
355                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
356                 public void RepeatInfoRenderOutOfRange ()
357                 {
358                         StringWriter sw = new StringWriter ();
359                         HtmlTextWriter tw = new HtmlTextWriter (sw);
360                         IRepeatInfoUser ri = new CheckBoxList ();
361
362                         ri.RenderItem (ListItemType.Item, -1, new RepeatInfo (), tw); 
363                 }
364
365                 [Test]
366                 public void RepeatInfoRenderItem ()
367                 {
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 ();
373
374                         c.Items.Add ("one");
375                         c.Items.Add ("two");
376
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");
380                 }
381
382                 [Test]
383                 public void FindControl ()
384                 {
385                         CheckBoxListPoker p = new CheckBoxListPoker ();
386
387                         p.ID = "id";
388                         p.Items.Add ("one");
389                         p.Items.Add ("two");
390                         p.Items.Add ("three");
391
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");
400
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");
408                 }
409
410                 private void Render (CheckBoxList list, string expected, string test)
411                 {
412                         StringWriter sw = new StringWriter ();
413                         HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
414                         sw.NewLine = "\n";
415
416                         list.RenderControl (tw);
417                         HtmlDiff.AssertAreEqual (expected, sw.ToString (), test);
418                 }
419
420                 [Test]
421                 public void RenderEmpty ()
422                 {
423                         CheckBoxList c = new CheckBoxList ();
424
425 #if NET_2_0
426                         Render (c, "", "A1");
427 #else
428                         Render (c, "<table border=\"0\">\n\n</table>", "A1");
429 #endif
430                         c.CellPadding = 1;
431 #if NET_2_0
432                         Render (c, "", "A2");
433 #else
434                         Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A2");
435 #endif
436
437                         c = new CheckBoxList ();
438                         c.CellPadding = 1;
439 #if NET_2_0
440                         Render (c, "", "A3");
441 #else
442                         Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A3");
443 #endif
444
445                         c = new CheckBoxList ();
446                         c.TextAlign = TextAlign.Left;
447 #if NET_2_0
448                         Render (c, "", "A4");
449 #else
450                         Render (c, "<table border=\"0\">\n\n</table>", "A4");
451 #endif
452                 }
453
454                 [Test]
455 #if NET_2_0
456                 [Category("NotDotNet")] // MS's implementation throws NRE's from these
457 #endif
458                 public void Render ()
459                 {
460                         CheckBoxList c;
461                         c = new CheckBoxList ();
462                         c.Items.Add ("foo");
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");
467
468                         c = new CheckBoxList ();
469                         c.Items.Add ("foo");
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");
474                 }
475
476                 // bug 51648
477                 [Test]
478 #if NET_2_0
479                 [Category("NotDotNet")] // MS's implementation throws NRE's from these
480 #endif
481                 public void TestTabIndex ()
482                 {
483                         CheckBoxList c = new CheckBoxList ();
484                         c.TabIndex = 5;
485                         c.Items.Add ("Item1");
486                         string exp = @"<table border=""0"">
487         <tr>
488                 <td><input id=""0"" name=""0"" tabindex=""5"" type=""checkbox"" /><label for=""0"">Item1</label></td>
489         </tr>
490 </table>";
491                         Render (c, exp, "B1");
492                 }
493
494                 // bug 48802
495                 [Test]
496 #if NET_2_0
497                 [Category("NotDotNet")] // MS's implementation throws NRE's from these
498 #endif
499                 public void TestDisabled ()
500                 {
501                         CheckBoxList c = new CheckBoxList ();
502                         c.Enabled = false;
503                         c.Items.Add ("Item1");
504                         string exp = @"<table border=""0"" disabled=""disabled"">
505         <tr>
506                 <td><span disabled=""disabled""><input disabled=""disabled"" id=""0"" name=""0"" type=""checkbox"" /><label for=""0"">Item1</label></span></td>
507         </tr>
508 </table>";
509                         Render (c, exp, "C1");
510                 }       
511 #if NET_2_0
512         class TestCheckBoxList : CheckBoxList
513         {
514             public new virtual void VerifyMultiSelect()
515             {
516                 base.VerifyMultiSelect();
517             }
518         }
519         [Test]
520         public void VerifyMultiSelectTest()
521         {
522             TestCheckBoxList list = new TestCheckBoxList();
523             list.VerifyMultiSelect();
524         }
525         [TestFixtureTearDown]
526                 public void teardown ()
527                 {
528                         WebTest.Unload ();
529                 }
530 #endif
531         }
532 }
533
534