New test.
[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
39 namespace MonoTests.System.Web.UI.WebControls {
40
41         public class CheckBoxListPoker : CheckBoxList {
42
43                 public Style CreateStyle ()
44                 {
45                         return CreateControlStyle ();
46                 }
47
48                 public Control FindControlPoke (string name, int offset)
49                 {
50                         return FindControl (name, offset);
51                 }
52         }
53
54         [TestFixture]
55         public class CheckBoxListTest {
56
57                 [Test]
58                 public void Defaults ()
59                 {
60                         CheckBoxList c = new CheckBoxList ();
61
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");
70                 }
71
72                 [Test]
73                 public void CleanProperties ()
74                 {
75                         CheckBoxList c = new CheckBoxList ();
76
77                         c.CellPadding = Int32.MaxValue;
78                         Assert.AreEqual (c.CellPadding, Int32.MaxValue, "A1");
79
80                         c.CellSpacing = Int32.MaxValue;
81                         Assert.AreEqual (c.CellSpacing, Int32.MaxValue, "A2");
82
83                         c.RepeatColumns = Int32.MaxValue;
84                         Assert.AreEqual (c.RepeatColumns, Int32.MaxValue, "A3");
85
86                         foreach (RepeatDirection d in
87                                         Enum.GetValues (typeof (RepeatDirection))) {
88                                 c.RepeatDirection = d;
89                                 Assert.AreEqual (c.RepeatDirection, d, "A4-" + d);
90                         }
91
92                         foreach (RepeatLayout l in
93                                         Enum.GetValues (typeof (RepeatLayout))) {
94                                 c.RepeatLayout = l;
95                                 Assert.AreEqual (c.RepeatLayout, l, "A5-" + l);
96                         }
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
101                 public void CellPaddingTooLow ()
102                 {
103                         CheckBoxList c = new CheckBoxList ();
104                         c.CellPadding = -2;
105                 }
106
107                 [Test]
108                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
109                 public void CellSpacingTooLow ()
110                 {
111                         CheckBoxList c = new CheckBoxList ();
112                         c.CellSpacing = -2;
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
117                 public void RepeatColumsTooLow ()
118                 {
119                         CheckBoxList c = new CheckBoxList ();
120                         c.RepeatColumns = -1;
121                 }
122
123                 [Test]
124                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
125                 public void RepeatDirection_Invalid ()
126                 {
127                         CheckBoxList c = new CheckBoxList ();
128                         c.RepeatDirection = (RepeatDirection) Int32.MaxValue;
129                 }
130
131                 [Test]
132                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
133                 public void RepeatLayout_Invalid ()
134                 {
135                         CheckBoxList c = new CheckBoxList ();
136                         c.RepeatLayout = (RepeatLayout) Int32.MaxValue;
137                 }
138
139                 [Test]
140                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
141                 public void TextAlign_Invalid ()
142                 {
143                         CheckBoxList c = new CheckBoxList ();
144                         c.TextAlign = (TextAlign) Int32.MaxValue;
145                 }
146
147                 [Test]
148                 public void ChildCheckBoxControl ()
149                 {
150                         CheckBoxList c = new CheckBoxList ();
151                         Assert.AreEqual (c.Controls.Count, 1, "A1");
152                         Assert.AreEqual (c.Controls [0].GetType (), typeof (CheckBox), "A2");
153                 }
154
155                 [Test]
156                 public void CreateStyle ()
157                 {
158                         CheckBoxListPoker c = new CheckBoxListPoker ();
159                         Assert.AreEqual (c.CreateStyle ().GetType (), typeof (TableStyle), "A1");
160                 }
161
162                 [Test]
163                 public void RepeatInfoProperties ()
164                 {
165                         IRepeatInfoUser ri = new CheckBoxList ();
166
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");
171                 }
172
173                 [Test]
174                 public void RepeatInfoCount ()
175                 {
176                         CheckBoxList c = new CheckBoxList ();
177                         IRepeatInfoUser ri = (IRepeatInfoUser) c;
178
179                         Assert.AreEqual (ri.RepeatedItemCount, 0, "A1");
180
181                         c.Items.Add ("one");
182                         c.Items.Add ("two");
183                         c.Items.Add ("three");
184                         Assert.AreEqual (ri.RepeatedItemCount, 3, "A2");
185                 }
186
187                 [Test]
188                 public void RepeatInfoStyle ()
189                 {
190                         IRepeatInfoUser ri = new CheckBoxList ();
191
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);
197                         }
198                 }
199
200                 [Test]
201                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
202                 public void RepeatInfoRenderOutOfRange ()
203                 {
204                         StringWriter sw = new StringWriter ();
205                         HtmlTextWriter tw = new HtmlTextWriter (sw);
206                         IRepeatInfoUser ri = new CheckBoxList ();
207
208                         ri.RenderItem (ListItemType.Item, -1, new RepeatInfo (), tw); 
209                 }
210
211                 [Test]
212                 public void RepeatInfoRenderItem ()
213                 {
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 ();
219
220                         c.Items.Add ("one");
221                         c.Items.Add ("two");
222
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");
226                 }
227
228                 [Test]
229                 public void FindControl ()
230                 {
231                         CheckBoxListPoker p = new CheckBoxListPoker ();
232
233                         p.ID = "id";
234                         p.Items.Add ("one");
235                         p.Items.Add ("two");
236                         p.Items.Add ("three");
237
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");
246
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");
254                 }
255
256                 private void Render (CheckBoxList list, string expected, string test)
257                 {
258                         StringWriter sw = new StringWriter ();
259                         HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
260                         sw.NewLine = "\n";
261
262                         list.RenderControl (tw);
263                         Assert.AreEqual (expected, sw.ToString (), test);
264                 }
265
266                 [Test]
267                 public void RenderEmpty ()
268                 {
269                         CheckBoxList c = new CheckBoxList ();
270
271 #if NET_2_0
272                         Render (c, "", "A1");
273 #else
274                         Render (c, "<table border=\"0\">\n\n</table>", "A1");
275 #endif
276                         c.CellPadding = 1;
277 #if NET_2_0
278                         Render (c, "", "A2");
279 #else
280                         Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A2");
281 #endif
282
283                         c = new CheckBoxList ();
284                         c.CellPadding = 1;
285 #if NET_2_0
286                         Render (c, "", "A3");
287 #else
288                         Render (c, "<table border=\"0\" cellpadding=\"1\">\n\n</table>", "A3");
289 #endif
290
291                         c = new CheckBoxList ();
292                         c.TextAlign = TextAlign.Left;
293 #if NET_2_0
294                         Render (c, "", "A4");
295 #else
296                         Render (c, "<table border=\"0\">\n\n</table>", "A4");
297 #endif
298                 }
299
300                 [Test]
301 #if NET_2_0
302                 [Category("NotDotNet")] // MS's implementation throws NRE's from these
303 #endif
304                 public void Render ()
305                 {
306                         CheckBoxList c;
307                         c = new CheckBoxList ();
308                         c.Items.Add ("foo");
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");
313
314                         c = new CheckBoxList ();
315                         c.Items.Add ("foo");
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");
320                 }
321
322                 // bug 51648
323                 [Test]
324 #if NET_2_0
325                 [Category("NotDotNet")] // MS's implementation throws NRE's from these
326 #endif
327                 public void TestTabIndex ()
328                 {
329                         CheckBoxList c = new CheckBoxList ();
330                         c.TabIndex = 5;
331                         c.Items.Add ("Item1");
332                         string exp = @"<table border=""0"">
333         <tr>
334                 <td><input id=""0"" name=""0"" tabindex=""5"" type=""checkbox"" /><label for=""0"">Item1</label></td>
335         </tr>
336 </table>";
337                         Render (c, exp, "B1");
338                 }
339
340                 // bug 48802
341                 [Test]
342 #if NET_2_0
343                 [Category("NotDotNet")] // MS's implementation throws NRE's from these
344 #endif
345                 public void TestDisabled ()
346                 {
347                         CheckBoxList c = new CheckBoxList ();
348                         c.Enabled = false;
349                         c.Items.Add ("Item1");
350                         string exp = @"<table border=""0"" disabled=""disabled"">
351         <tr>
352                 <td><span disabled=""disabled""><input disabled=""disabled"" id=""0"" name=""0"" type=""checkbox"" /><label for=""0"">Item1</label></span></td>
353         </tr>
354 </table>";
355                         Render (c, exp, "C1");
356                 }               
357                 
358         }
359 }
360