2 // Tests for System.Web.UI.WebControls.CheckBoxFieldTest.cs
5 // Yoni Klein (yonik@mainsoft.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections.Generic;
37 using System.Web.UI.WebControls;
40 using System.Collections;
41 using System.Collections.Specialized;
42 using NUnit.Framework;
45 using MonoTests.SystemWeb.Framework;
46 using MonoTests.stand_alone.WebHarness;
48 namespace MonoTests.System.Web.UI.WebControls
50 class PokerCheckBoxField : CheckBoxField
53 public PokerCheckBoxField ()
59 public object SaveState ()
61 return SaveViewState ();
64 public void LoadState (object o)
69 public StateBag StateBag
71 get { return base.ViewState; }
74 public bool GetSupportsHtmlEncode
78 return base.SupportsHtmlEncode;
82 public void DoCopyProperties (DataControlField newField)
84 base.CopyProperties (newField);
87 public DataControlField DoCreateField ()
89 return base.CreateField ();
92 public object DoGetDesignTimeValue ()
94 return base.GetDesignTimeValue ();
97 public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
99 this.InitializeDataCell (cell, rowState);
102 protected override void OnDataBindField (object sender, EventArgs e)
104 base.OnDataBindField (sender, e);
105 CheckBoxFieldTest.databound += 1;
110 public class CheckBoxFieldTest
112 public const string FIELDNAME = "checkbox";
113 public const string WRONGFIELD = "str";
114 public static int databound;
119 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_0.aspx", "CheckBoxField_Bug595568_0.aspx");
120 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_1.aspx", "CheckBoxField_Bug595568_1.aspx");
121 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_2.aspx", "CheckBoxField_Bug595568_2.aspx");
122 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_5.aspx", "CheckBoxField_Bug595568_5.aspx");
123 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_6.aspx", "CheckBoxField_Bug595568_6.aspx");
124 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_7.aspx", "CheckBoxField_Bug595568_7.aspx");
127 [Test (Description="Bug 595568 #0")]
128 public void CheckBoxField_Bug595568_0 ()
130 string originalHtml = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"gridView\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\"> </th>\r\n\t\t</tr><tr>\r\n\t\t\t<td><span title=\"Dummy\"><input id=\"gridView_ctl00_0\" type=\"checkbox\" name=\"gridView$ctl02$ctl00\" checked=\"checked\" /></span></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><span title=\"Dummy\"><input id=\"gridView_ctl00_1\" type=\"checkbox\" name=\"gridView$ctl03$ctl00\" /></span></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><span title=\"Dummy\"><input id=\"gridView_ctl00_2\" type=\"checkbox\" name=\"gridView$ctl04$ctl00\" /></span></td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
131 WebTest t = new WebTest ("CheckBoxField_Bug595568_0.aspx");
132 string pageHtml = t.Run ();
133 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
135 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
138 [Test (Description="Bug 595568 #1")]
139 public void CheckBoxField_Bug595568_1 ()
141 string originalHtml = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"gridView\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\"> </th>\r\n\t\t</tr><tr>\r\n\t\t\t<td><select size=\"4\" name=\"gridView$ctl02$ctl00\">\r\n\r\n\t\t\t</select><span title=\"Dummy\"><input id=\"gridView_ctl01_0\" type=\"checkbox\" name=\"gridView$ctl02$ctl01\" checked=\"checked\" /></span></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><select size=\"4\" name=\"gridView$ctl03$ctl00\">\r\n\r\n\t\t\t</select><span title=\"Dummy\"><input id=\"gridView_ctl01_1\" type=\"checkbox\" name=\"gridView$ctl03$ctl01\" /></span></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><select size=\"4\" name=\"gridView$ctl04$ctl00\">\r\n\r\n\t\t\t</select><span title=\"Dummy\"><input id=\"gridView_ctl01_2\" type=\"checkbox\" name=\"gridView$ctl04$ctl01\" /></span></td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
142 WebTest t = new WebTest ("CheckBoxField_Bug595568_1.aspx");
143 string pageHtml = t.Run ();
144 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
146 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
149 [Test (Description="Bug 595568 #2")]
150 public void CheckBoxField_Bug595568_2 ()
152 string originalHtml = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"gridView\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\"> </th>\r\n\t\t</tr><tr>\r\n\t\t\t<td><input id=\"gridView_ctl00_0\" type=\"checkbox\" name=\"gridView$ctl02$ctl00\" /><span title=\"Dummy\"><input id=\"gridView_ctl01_0\" type=\"checkbox\" name=\"gridView$ctl02$ctl01\" checked=\"checked\" /></span><input id=\"gridView_ctl02_0\" type=\"checkbox\" name=\"gridView$ctl02$ctl02\" /></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><input id=\"gridView_ctl00_1\" type=\"checkbox\" name=\"gridView$ctl03$ctl00\" /><span title=\"Dummy\"><input id=\"gridView_ctl01_1\" type=\"checkbox\" name=\"gridView$ctl03$ctl01\" /></span><input id=\"gridView_ctl02_1\" type=\"checkbox\" name=\"gridView$ctl03$ctl02\" /></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><input id=\"gridView_ctl00_2\" type=\"checkbox\" name=\"gridView$ctl04$ctl00\" /><span title=\"Dummy\"><input id=\"gridView_ctl01_2\" type=\"checkbox\" name=\"gridView$ctl04$ctl01\" /></span><input id=\"gridView_ctl02_2\" type=\"checkbox\" name=\"gridView$ctl04$ctl02\" /></td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
154 WebTest t = new WebTest ("CheckBoxField_Bug595568_2.aspx");
155 string pageHtml = t.Run ();
156 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
158 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
161 [Test (Description="Bug 595568 #5")]
162 public void CheckBoxField_Bug595568_5 ()
164 string originalHtml = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"gridView\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\"> </th>\r\n\t\t</tr><tr>\r\n\t\t\t<td><select size=\"4\" name=\"gridView$ctl02$ctl00\">\r\n\r\n\t\t\t</select></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><select size=\"4\" name=\"gridView$ctl03$ctl00\">\r\n\r\n\t\t\t</select></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><select size=\"4\" name=\"gridView$ctl04$ctl00\">\r\n\r\n\t\t\t</select></td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
166 WebTest t = new WebTest ("CheckBoxField_Bug595568_5.aspx");
167 string pageHtml = t.Run ();
168 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
170 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
173 [Test (Description="Bug 595568 #6")]
174 public void CheckBoxField_Bug595568_6 ()
176 string originalHtml = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"gridView\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\"> </th>\r\n\t\t</tr><tr>\r\n\t\t\t<td><select size=\"4\" name=\"gridView$ctl02$ctl00\">\r\n\r\n\t\t\t</select><input id=\"gridView_ctl01_0\" type=\"checkbox\" name=\"gridView$ctl02$ctl01\" /></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><select size=\"4\" name=\"gridView$ctl03$ctl00\">\r\n\r\n\t\t\t</select><input id=\"gridView_ctl01_1\" type=\"checkbox\" name=\"gridView$ctl03$ctl01\" /></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><select size=\"4\" name=\"gridView$ctl04$ctl00\">\r\n\r\n\t\t\t</select><input id=\"gridView_ctl01_2\" type=\"checkbox\" name=\"gridView$ctl04$ctl01\" /></td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
178 WebTest t = new WebTest ("CheckBoxField_Bug595568_6.aspx");
179 string pageHtml = t.Run ();
180 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
182 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
185 [Test (Description="Bug 595568 #7")]
186 public void CheckBoxField_Bug595568_7 ()
188 string originalHtml = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"gridView\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\"> </th>\r\n\t\t</tr><tr>\r\n\t\t\t<td><input id=\"gridView_ctl00_0\" type=\"checkbox\" name=\"gridView$ctl02$ctl00\" /><select size=\"4\" name=\"gridView$ctl02$ctl01\">\r\n\r\n\t\t\t</select></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><input id=\"gridView_ctl00_1\" type=\"checkbox\" name=\"gridView$ctl03$ctl00\" /><select size=\"4\" name=\"gridView$ctl03$ctl01\">\r\n\r\n\t\t\t</select></td>\r\n\t\t</tr><tr>\r\n\t\t\t<td><input id=\"gridView_ctl00_2\" type=\"checkbox\" name=\"gridView$ctl04$ctl00\" /><select size=\"4\" name=\"gridView$ctl04$ctl01\">\r\n\r\n\t\t\t</select></td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
189 WebTest t = new WebTest ("CheckBoxField_Bug595568_7.aspx");
190 string pageHtml = t.Run ();
191 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
193 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
197 public void CheckBoxField_DefaultProperty ()
199 PokerCheckBoxField field = new PokerCheckBoxField ();
200 Assert.AreEqual ("", field.DataField, "DataField");
201 Assert.AreEqual ("", field.Text, "Text");
202 Assert.AreEqual (false, field.GetSupportsHtmlEncode, "SupportsHtmlEncode");
206 public void CheckBoxField_AssignProperty ()
208 PokerCheckBoxField field = new PokerCheckBoxField ();
209 field.DataField = "test";
210 Assert.AreEqual ("test", field.DataField, "DataField");
212 Assert.AreEqual ("test", field.Text, "Text");
215 public void CheckBoxField_ExtractValuesFromCell ()
217 PokerCheckBoxField field = new PokerCheckBoxField ();
218 OrderedDictionary dictionary = new OrderedDictionary ();
219 DataControlFieldCell cell = new DataControlFieldCell (null);
220 cell.Controls.Add (new CheckBox ());
221 field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
222 Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#1");
223 Assert.AreEqual ("False", dictionary[0].ToString (), "ExtractValuesFromCellValueFalse");
224 CheckBox cb = new CheckBox ();
226 cell.Controls.Clear ();
227 cell.Controls.Add (cb);
228 field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
229 Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#2");
230 Assert.AreEqual ("True", dictionary[0].ToString (), "ExtractValuesFromCellValueTrue");
234 public void CheckBoxField_ValidateSupportsCallback ()
236 PokerCheckBoxField field = new PokerCheckBoxField ();
237 field.ValidateSupportsCallback ();
241 public void CheckBoxField_CopyProperties()
243 PokerCheckBoxField field = new PokerCheckBoxField ();
244 CheckBoxField copy = new CheckBoxField();
245 field.DataField = "test";
247 field.DoCopyProperties (copy);
248 Assert.AreEqual ("test", copy.Text, "Text");
249 Assert.AreEqual ("test", copy.DataField, "DataField");
253 public void CheckBoxField_CreateField ()
255 PokerCheckBoxField field = new PokerCheckBoxField ();
256 CheckBoxField blank = (CheckBoxField)field.DoCreateField ();
257 Assert.IsNotNull (blank, "CreateField");
261 public void CheckBoxField_GetDesignTimeValue ()
263 PokerCheckBoxField field = new PokerCheckBoxField ();
264 bool result = (bool)field.DoGetDesignTimeValue ();
265 Assert.AreEqual (true, result, "GetDesignTimeValue");
269 public void CheckBoxField_InitializeDataCell ()
271 PokerCheckBoxField field = new PokerCheckBoxField ();
272 field.HeaderText = "headertest";
273 DataControlFieldCell cell = new DataControlFieldCell (null);
274 DataControlRowState state = DataControlRowState.Edit;
275 Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
276 field.DoInitializeDataCell (cell, state);
277 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
278 Assert.AreEqual ("headertest", ((CheckBox)cell.Controls[0]).ToolTip, "InitializeDataCellControlsData");
280 cell.Controls.Clear ();
281 field.DataField = "fake";
282 field.Text = "celltext";
283 state = DataControlRowState.Normal;
284 field.DoInitializeDataCell (cell, state);
285 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
286 Assert.AreEqual ("celltext", ((CheckBox) cell.Controls[0]).Text, "InitializeDataCellControlsData");
290 public void CheckBoxField_OnDataBindField ()
292 Page page = new Page ();
293 GridView grid = new GridView ();
294 page.Controls.Add (grid);
295 grid.DataSource = this.CreateDataSource ();
296 grid.AutoGenerateColumns = false;
297 PokerCheckBoxField field = new PokerCheckBoxField ();
298 field.HeaderText = "field_header";
299 field.FooterText = "field_footer";
300 field.DataField = FIELDNAME;
301 grid.Columns.Add (field);
303 Assert.AreEqual (2, databound, "DataBindField");
304 Assert.AreEqual (4, ((Control) grid.Controls[0]).Controls.Count, "DataBindFieldRowCountr");
308 [ExpectedException (typeof (HttpException))]
309 public void CheckBoxField_OnDataBindFieldException ()
311 Page page = new Page ();
312 GridView grid = new GridView ();
313 page.Controls.Add (grid);
314 grid.DataSource = this.CreateDataSource ();
315 grid.AutoGenerateColumns = false;
316 PokerCheckBoxField field = new PokerCheckBoxField ();
317 field.HeaderText = "field_header";
318 field.FooterText = "field_footer";
319 field.DataField = WRONGFIELD;
320 grid.Columns.Add (field);
325 [ExpectedException(typeof(NotSupportedException))]
326 public void CheckBoxField_GetApplyFormatInEditModeExeption ()
328 PokerCheckBoxField field = new PokerCheckBoxField ();
329 bool stab = field.ApplyFormatInEditMode;
333 [ExpectedException (typeof (NotSupportedException))]
334 public void CheckBoxField_SetApplyFormatInEditModeExeption ()
336 PokerCheckBoxField field = new PokerCheckBoxField ();
337 field.ApplyFormatInEditMode = true;
341 [ExpectedException (typeof (NotSupportedException))]
342 public void CheckBoxField_GetConvertEmptyStringToNull ()
344 PokerCheckBoxField field = new PokerCheckBoxField ();
345 bool stab = field.ConvertEmptyStringToNull;
349 [ExpectedException (typeof (NotSupportedException))]
350 public void CheckBoxField_SetConvertEmptyStringToNull ()
352 PokerCheckBoxField field = new PokerCheckBoxField ();
353 field.ConvertEmptyStringToNull = true;
357 [ExpectedException (typeof (NotSupportedException))]
358 public void CheckBoxField_SetDataFormatString ()
360 PokerCheckBoxField field = new PokerCheckBoxField ();
361 field.DataFormatString = "";
365 [ExpectedException (typeof (NotSupportedException))]
366 public void CheckBoxField_GetDataFormatString ()
368 PokerCheckBoxField field = new PokerCheckBoxField ();
369 string res = field.DataFormatString;
373 [ExpectedException (typeof (NotSupportedException))]
374 public void CheckBoxField_SetHtmlEncode ()
376 PokerCheckBoxField field = new PokerCheckBoxField ();
377 field.HtmlEncode = true;
381 [ExpectedException (typeof (NotSupportedException))]
382 public void CheckBoxField_GetHtmlEncode ()
384 PokerCheckBoxField field = new PokerCheckBoxField ();
385 bool res = field.HtmlEncode;
389 [ExpectedException (typeof (NotSupportedException))]
390 public void CheckBoxField_SetNullDisplayText ()
392 PokerCheckBoxField field = new PokerCheckBoxField ();
393 field.NullDisplayText = "";
397 [ExpectedException (typeof (NotSupportedException))]
398 public void CheckBoxField_GetNullDisplayText ()
400 PokerCheckBoxField field = new PokerCheckBoxField ();
401 string res = field.NullDisplayText;
404 public DataTable CreateDataSource ()
406 DataTable aTable = new DataTable ("A");
409 // Create ID column and add to the DataTable.
410 dtCol = new DataColumn ();
411 dtCol.DataType = Type.GetType ("System.Boolean");
412 dtCol.ColumnName = FIELDNAME;
413 dtCol.Caption = FIELDNAME;
414 dtCol.ReadOnly = true;
416 // Add the column to the DataColumnCollection.
417 aTable.Columns.Add (dtCol);
420 dtCol = new DataColumn ();
421 dtCol.DataType = Type.GetType ("System.String");
422 dtCol.ColumnName = WRONGFIELD;
423 dtCol.Caption = WRONGFIELD;
424 dtCol.ReadOnly = true;
427 // Add the column to the DataColumnCollection.
428 aTable.Columns.Add (dtCol);
430 // Create 2 rows to the table
431 dtRow = aTable.NewRow ();
432 dtRow[FIELDNAME] = true;
433 dtRow[WRONGFIELD] = "1";
434 aTable.Rows.Add (dtRow);
436 dtRow = aTable.NewRow ();
437 dtRow[FIELDNAME] = false;
438 dtRow[WRONGFIELD] = "1";
439 aTable.Rows.Add (dtRow);