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.
34 using System.Collections.Generic;
38 using System.Web.UI.WebControls;
41 using System.Collections;
42 using System.Collections.Specialized;
43 using NUnit.Framework;
46 using MonoTests.SystemWeb.Framework;
47 using MonoTests.stand_alone.WebHarness;
49 namespace MonoTests.System.Web.UI.WebControls
51 class PokerCheckBoxField : CheckBoxField
54 public PokerCheckBoxField ()
60 public object SaveState ()
62 return SaveViewState ();
65 public void LoadState (object o)
70 public StateBag StateBag
72 get { return base.ViewState; }
75 public bool GetSupportsHtmlEncode
79 return base.SupportsHtmlEncode;
83 public void DoCopyProperties (DataControlField newField)
85 base.CopyProperties (newField);
88 public DataControlField DoCreateField ()
90 return base.CreateField ();
93 public object DoGetDesignTimeValue ()
95 return base.GetDesignTimeValue ();
98 public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
100 this.InitializeDataCell (cell, rowState);
103 protected override void OnDataBindField (object sender, EventArgs e)
105 base.OnDataBindField (sender, e);
106 CheckBoxFieldTest.databound += 1;
111 public class CheckBoxFieldTest
113 public const string FIELDNAME = "checkbox";
114 public const string WRONGFIELD = "str";
115 public static int databound;
120 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_0.aspx", "CheckBoxField_Bug595568_0.aspx");
121 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_1.aspx", "CheckBoxField_Bug595568_1.aspx");
122 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_2.aspx", "CheckBoxField_Bug595568_2.aspx");
123 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_5.aspx", "CheckBoxField_Bug595568_5.aspx");
124 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_6.aspx", "CheckBoxField_Bug595568_6.aspx");
125 WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_7.aspx", "CheckBoxField_Bug595568_7.aspx");
128 [Test (Description="Bug 595568 #0")]
129 public void CheckBoxField_Bug595568_0 ()
131 string originalHtml = @"<div>
132 <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
134 <th scope=""col""> </th>
136 <td><span title=""Dummy""><input id=""gridView_ctl02_ctl00"" type=""checkbox"" name=""gridView$ctl02$ctl00"" checked=""checked"" /></span></td>
138 <td><span title=""Dummy""><input id=""gridView_ctl03_ctl00"" type=""checkbox"" name=""gridView$ctl03$ctl00"" /></span></td>
140 <td><span title=""Dummy""><input id=""gridView_ctl04_ctl00"" type=""checkbox"" name=""gridView$ctl04$ctl00"" /></span></td>
144 WebTest t = new WebTest ("CheckBoxField_Bug595568_0.aspx");
145 string pageHtml = t.Run ();
146 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
148 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
151 [Test (Description="Bug 595568 #1")]
152 public void CheckBoxField_Bug595568_1 ()
154 string originalHtml = @"<div>
155 <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
157 <th scope=""col""> </th>
159 <td><select size=""4"">
161 </select><span title=""Dummy""><input id=""gridView_ctl02_ctl01"" type=""checkbox"" name=""gridView$ctl02$ctl01"" checked=""checked"" /></span></td>
163 <td><select size=""4"">
165 </select><span title=""Dummy""><input id=""gridView_ctl03_ctl01"" type=""checkbox"" name=""gridView$ctl03$ctl01"" /></span></td>
167 <td><select size=""4"">
169 </select><span title=""Dummy""><input id=""gridView_ctl04_ctl01"" type=""checkbox"" name=""gridView$ctl04$ctl01"" /></span></td>
173 WebTest t = new WebTest ("CheckBoxField_Bug595568_1.aspx");
174 string pageHtml = t.Run ();
175 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
177 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
180 [Test (Description="Bug 595568 #2")]
181 public void CheckBoxField_Bug595568_2 ()
183 string originalHtml = @"<div>
184 <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
186 <th scope=""col""> </th>
188 <td><input id=""gridView_ctl02_ctl00"" type=""checkbox"" name=""gridView$ctl02$ctl00"" /><span title=""Dummy""><input id=""gridView_ctl02_ctl01"" type=""checkbox"" name=""gridView$ctl02$ctl01"" checked=""checked"" /></span><input id=""gridView_ctl02_ctl02"" type=""checkbox"" name=""gridView$ctl02$ctl02"" /></td>
190 <td><input id=""gridView_ctl03_ctl00"" type=""checkbox"" name=""gridView$ctl03$ctl00"" /><span title=""Dummy""><input id=""gridView_ctl03_ctl01"" type=""checkbox"" name=""gridView$ctl03$ctl01"" /></span><input id=""gridView_ctl03_ctl02"" type=""checkbox"" name=""gridView$ctl03$ctl02"" /></td>
192 <td><input id=""gridView_ctl04_ctl00"" type=""checkbox"" name=""gridView$ctl04$ctl00"" /><span title=""Dummy""><input id=""gridView_ctl04_ctl01"" type=""checkbox"" name=""gridView$ctl04$ctl01"" /></span><input id=""gridView_ctl04_ctl02"" type=""checkbox"" name=""gridView$ctl04$ctl02"" /></td>
197 WebTest t = new WebTest ("CheckBoxField_Bug595568_2.aspx");
198 string pageHtml = t.Run ();
199 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
201 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
204 [Test (Description="Bug 595568 #5")]
205 public void CheckBoxField_Bug595568_5 ()
207 string originalHtml = @"<div>
208 <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
210 <th scope=""col""> </th>
212 <td><select size=""4"">
216 <td><select size=""4"">
220 <td><select size=""4"">
227 WebTest t = new WebTest ("CheckBoxField_Bug595568_5.aspx");
228 string pageHtml = t.Run ();
229 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
231 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
234 [Test (Description="Bug 595568 #6")]
235 public void CheckBoxField_Bug595568_6 ()
237 string originalHtml = @"<div>
238 <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
240 <th scope=""col""> </th>
242 <td><select size=""4"">
244 </select><input id=""gridView_ctl02_ctl01"" type=""checkbox"" name=""gridView$ctl02$ctl01"" /></td>
246 <td><select size=""4"">
248 </select><input id=""gridView_ctl03_ctl01"" type=""checkbox"" name=""gridView$ctl03$ctl01"" /></td>
250 <td><select size=""4"">
252 </select><input id=""gridView_ctl04_ctl01"" type=""checkbox"" name=""gridView$ctl04$ctl01"" /></td>
257 WebTest t = new WebTest ("CheckBoxField_Bug595568_6.aspx");
258 string pageHtml = t.Run ();
259 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
261 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
264 [Test (Description="Bug 595568 #7")]
265 public void CheckBoxField_Bug595568_7 ()
267 string originalHtml = @"<div>
268 <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
270 <th scope=""col""> </th>
272 <td><input id=""gridView_ctl02_ctl00"" type=""checkbox"" name=""gridView$ctl02$ctl00"" /><select size=""4"">
276 <td><input id=""gridView_ctl03_ctl00"" type=""checkbox"" name=""gridView$ctl03$ctl00"" /><select size=""4"">
280 <td><input id=""gridView_ctl04_ctl00"" type=""checkbox"" name=""gridView$ctl04$ctl00"" /><select size=""4"">
287 WebTest t = new WebTest ("CheckBoxField_Bug595568_7.aspx");
288 string pageHtml = t.Run ();
289 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
291 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
295 public void CheckBoxField_DefaultProperty ()
297 PokerCheckBoxField field = new PokerCheckBoxField ();
298 Assert.AreEqual ("", field.DataField, "DataField");
299 Assert.AreEqual ("", field.Text, "Text");
300 Assert.AreEqual (false, field.GetSupportsHtmlEncode, "SupportsHtmlEncode");
304 public void CheckBoxField_AssignProperty ()
306 PokerCheckBoxField field = new PokerCheckBoxField ();
307 field.DataField = "test";
308 Assert.AreEqual ("test", field.DataField, "DataField");
310 Assert.AreEqual ("test", field.Text, "Text");
313 public void CheckBoxField_ExtractValuesFromCell ()
315 PokerCheckBoxField field = new PokerCheckBoxField ();
316 OrderedDictionary dictionary = new OrderedDictionary ();
317 DataControlFieldCell cell = new DataControlFieldCell (null);
318 cell.Controls.Add (new CheckBox ());
319 field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
320 Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#1");
321 Assert.AreEqual ("False", dictionary[0].ToString (), "ExtractValuesFromCellValueFalse");
322 CheckBox cb = new CheckBox ();
324 cell.Controls.Clear ();
325 cell.Controls.Add (cb);
326 field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
327 Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#2");
328 Assert.AreEqual ("True", dictionary[0].ToString (), "ExtractValuesFromCellValueTrue");
332 public void CheckBoxField_ValidateSupportsCallback ()
334 //This method has been implemented as an empty method
338 public void CheckBoxField_CopyProperties()
340 PokerCheckBoxField field = new PokerCheckBoxField ();
341 CheckBoxField copy = new CheckBoxField();
342 field.DataField = "test";
344 field.DoCopyProperties (copy);
345 Assert.AreEqual ("test", copy.Text, "Text");
346 Assert.AreEqual ("test", copy.DataField, "DataField");
350 public void CheckBoxField_CreateField ()
352 PokerCheckBoxField field = new PokerCheckBoxField ();
353 CheckBoxField blank = (CheckBoxField)field.DoCreateField ();
354 Assert.IsNotNull (blank, "CreateField");
358 public void CheckBoxField_GetDesignTimeValue ()
360 PokerCheckBoxField field = new PokerCheckBoxField ();
361 bool result = (bool)field.DoGetDesignTimeValue ();
362 Assert.AreEqual (true, result, "GetDesignTimeValue");
366 public void CheckBoxField_InitializeDataCell ()
368 PokerCheckBoxField field = new PokerCheckBoxField ();
369 field.HeaderText = "headertest";
370 DataControlFieldCell cell = new DataControlFieldCell (null);
371 DataControlRowState state = DataControlRowState.Edit;
372 Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
373 field.DoInitializeDataCell (cell, state);
374 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
375 Assert.AreEqual ("headertest", ((CheckBox)cell.Controls[0]).ToolTip, "InitializeDataCellControlsData");
377 cell.Controls.Clear ();
378 field.DataField = "fake";
379 field.Text = "celltext";
380 state = DataControlRowState.Normal;
381 field.DoInitializeDataCell (cell, state);
382 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
383 Assert.AreEqual ("celltext", ((CheckBox) cell.Controls[0]).Text, "InitializeDataCellControlsData");
387 public void CheckBoxField_OnDataBindField ()
389 Page page = new Page ();
390 GridView grid = new GridView ();
391 page.Controls.Add (grid);
392 grid.DataSource = this.CreateDataSource ();
393 grid.AutoGenerateColumns = false;
394 PokerCheckBoxField field = new PokerCheckBoxField ();
395 field.HeaderText = "field_header";
396 field.FooterText = "field_footer";
397 field.DataField = FIELDNAME;
398 grid.Columns.Add (field);
400 Assert.AreEqual (2, databound, "DataBindField");
401 Assert.AreEqual (4, ((Control) grid.Controls[0]).Controls.Count, "DataBindFieldRowCountr");
405 [ExpectedException (typeof (HttpException))]
406 public void CheckBoxField_OnDataBindFieldException ()
408 Page page = new Page ();
409 GridView grid = new GridView ();
410 page.Controls.Add (grid);
411 grid.DataSource = this.CreateDataSource ();
412 grid.AutoGenerateColumns = false;
413 PokerCheckBoxField field = new PokerCheckBoxField ();
414 field.HeaderText = "field_header";
415 field.FooterText = "field_footer";
416 field.DataField = WRONGFIELD;
417 grid.Columns.Add (field);
422 [ExpectedException(typeof(NotSupportedException))]
423 public void CheckBoxField_GetApplyFormatInEditModeExeption ()
425 PokerCheckBoxField field = new PokerCheckBoxField ();
426 bool stab = field.ApplyFormatInEditMode;
430 [ExpectedException (typeof (NotSupportedException))]
431 public void CheckBoxField_SetApplyFormatInEditModeExeption ()
433 PokerCheckBoxField field = new PokerCheckBoxField ();
434 field.ApplyFormatInEditMode = true;
438 [ExpectedException (typeof (NotSupportedException))]
439 public void CheckBoxField_GetConvertEmptyStringToNull ()
441 PokerCheckBoxField field = new PokerCheckBoxField ();
442 bool stab = field.ConvertEmptyStringToNull;
446 [ExpectedException (typeof (NotSupportedException))]
447 public void CheckBoxField_SetConvertEmptyStringToNull ()
449 PokerCheckBoxField field = new PokerCheckBoxField ();
450 field.ConvertEmptyStringToNull = true;
454 [ExpectedException (typeof (NotSupportedException))]
455 public void CheckBoxField_SetDataFormatString ()
457 PokerCheckBoxField field = new PokerCheckBoxField ();
458 field.DataFormatString = "";
462 [ExpectedException (typeof (NotSupportedException))]
463 public void CheckBoxField_GetDataFormatString ()
465 PokerCheckBoxField field = new PokerCheckBoxField ();
466 string res = field.DataFormatString;
470 [ExpectedException (typeof (NotSupportedException))]
471 public void CheckBoxField_SetHtmlEncode ()
473 PokerCheckBoxField field = new PokerCheckBoxField ();
474 field.HtmlEncode = true;
478 [ExpectedException (typeof (NotSupportedException))]
479 public void CheckBoxField_GetHtmlEncode ()
481 PokerCheckBoxField field = new PokerCheckBoxField ();
482 bool res = field.HtmlEncode;
486 [ExpectedException (typeof (NotSupportedException))]
487 public void CheckBoxField_SetNullDisplayText ()
489 PokerCheckBoxField field = new PokerCheckBoxField ();
490 field.NullDisplayText = "";
494 [ExpectedException (typeof (NotSupportedException))]
495 public void CheckBoxField_GetNullDisplayText ()
497 PokerCheckBoxField field = new PokerCheckBoxField ();
498 string res = field.NullDisplayText;
501 public DataTable CreateDataSource ()
503 DataTable aTable = new DataTable ("A");
506 // Create ID column and add to the DataTable.
507 dtCol = new DataColumn ();
508 dtCol.DataType = Type.GetType ("System.Boolean");
509 dtCol.ColumnName = FIELDNAME;
510 dtCol.Caption = FIELDNAME;
511 dtCol.ReadOnly = true;
513 // Add the column to the DataColumnCollection.
514 aTable.Columns.Add (dtCol);
517 dtCol = new DataColumn ();
518 dtCol.DataType = Type.GetType ("System.String");
519 dtCol.ColumnName = WRONGFIELD;
520 dtCol.Caption = WRONGFIELD;
521 dtCol.ReadOnly = true;
524 // Add the column to the DataColumnCollection.
525 aTable.Columns.Add (dtCol);
527 // Create 2 rows to the table
528 dtRow = aTable.NewRow ();
529 dtRow[FIELDNAME] = true;
530 dtRow[WRONGFIELD] = "1";
531 aTable.Rows.Add (dtRow);
533 dtRow = aTable.NewRow ();
534 dtRow[FIELDNAME] = false;
535 dtRow[WRONGFIELD] = "1";
536 aTable.Rows.Add (dtRow);