New tests.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / CheckBoxFieldTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.CheckBoxFieldTest.cs
3 //
4 // Author:
5 //      Yoni Klein (yonik@mainsoft.com)
6 //
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
28
29
30 #if NET_2_0
31
32
33 using System;
34 using System.Collections.Generic;
35 using System.Text;
36 using System.Web;
37 using System.Web.UI;
38 using System.Web.UI.WebControls;
39 using System.IO;
40 using System.Drawing;
41 using System.Collections;
42 using System.Collections.Specialized;
43 using NUnit.Framework;
44 using System.Data;
45
46 using MonoTests.SystemWeb.Framework;
47 using MonoTests.stand_alone.WebHarness;
48
49 namespace MonoTests.System.Web.UI.WebControls
50 {
51         class PokerCheckBoxField : CheckBoxField
52         {
53                 // View state Stuff
54                 public PokerCheckBoxField ()
55                         : base ()
56                 {
57                         TrackViewState ();
58                 }
59
60                 public object SaveState ()
61                 {
62                         return SaveViewState ();
63                 }
64
65                 public void LoadState (object o)
66                 {
67                         LoadViewState (o);
68                 }
69
70                 public StateBag StateBag
71                 {
72                         get { return base.ViewState; }
73                 }
74
75                 public bool GetSupportsHtmlEncode
76                 {
77                         get
78                         {
79                                 return base.SupportsHtmlEncode;
80                         }
81                 }
82
83                 public void DoCopyProperties (DataControlField newField)
84                 {
85                         base.CopyProperties (newField);
86                 }
87
88                 public DataControlField DoCreateField ()
89                 {
90                         return base.CreateField ();
91                 }
92
93                 public object DoGetDesignTimeValue ()
94                 {
95                         return base.GetDesignTimeValue ();
96                 }
97
98                 public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
99                 {
100                         this.InitializeDataCell (cell, rowState);
101                 }
102
103                 protected override void OnDataBindField (object sender, EventArgs e)
104                 {
105                         base.OnDataBindField (sender, e);
106                         CheckBoxFieldTest.databound += 1;
107                 }
108         }
109         
110         [TestFixture]
111         public class CheckBoxFieldTest
112         {
113                 public const string FIELDNAME  = "checkbox";
114                 public const string WRONGFIELD = "str";
115                 public static int databound;
116
117                 [TestFixtureSetUp]
118                 public void SetUp ()
119                 {
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");
126                 }
127
128                 [Test (Description="Bug 595568 #0")]
129                 public void CheckBoxField_Bug595568_0 ()
130                 {
131                         string originalHtml = @"<div> 
132         <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;""> 
133                         <tr> 
134                                 <th scope=""col"">&nbsp;</th> 
135                         </tr><tr> 
136                                 <td><span title=""Dummy""><input id=""gridView_ctl02_ctl00"" type=""checkbox"" name=""gridView$ctl02$ctl00"" checked=""checked"" /></span></td> 
137                         </tr><tr> 
138                                 <td><span title=""Dummy""><input id=""gridView_ctl03_ctl00"" type=""checkbox"" name=""gridView$ctl03$ctl00"" /></span></td> 
139                         </tr><tr> 
140                                 <td><span title=""Dummy""><input id=""gridView_ctl04_ctl00"" type=""checkbox"" name=""gridView$ctl04$ctl00"" /></span></td> 
141                         </tr> 
142                 </table>
143         </div>";
144                         WebTest t = new WebTest ("CheckBoxField_Bug595568_0.aspx");
145                         string pageHtml = t.Run ();
146                         string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
147
148                         HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
149                 }
150
151                 [Test (Description="Bug 595568 #1")]
152                 public void CheckBoxField_Bug595568_1 ()
153                 {
154                         string originalHtml = @"<div> 
155         <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;""> 
156                         <tr> 
157                                 <th scope=""col"">&nbsp;</th> 
158                         </tr><tr> 
159                                 <td><select size=""4""> 
160  
161                                 </select><span title=""Dummy""><input id=""gridView_ctl02_ctl01"" type=""checkbox"" name=""gridView$ctl02$ctl01"" checked=""checked"" /></span></td> 
162                         </tr><tr> 
163                                 <td><select size=""4""> 
164  
165                                 </select><span title=""Dummy""><input id=""gridView_ctl03_ctl01"" type=""checkbox"" name=""gridView$ctl03$ctl01"" /></span></td> 
166                         </tr><tr> 
167                                 <td><select size=""4""> 
168  
169                                 </select><span title=""Dummy""><input id=""gridView_ctl04_ctl01"" type=""checkbox"" name=""gridView$ctl04$ctl01"" /></span></td> 
170                         </tr> 
171                 </table> 
172         </div>";
173                         WebTest t = new WebTest ("CheckBoxField_Bug595568_1.aspx");
174                         string pageHtml = t.Run ();
175                         string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
176
177                         HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
178                 }
179
180                 [Test (Description="Bug 595568 #2")]
181                 public void CheckBoxField_Bug595568_2 ()
182                 {
183                         string originalHtml = @"<div> 
184         <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;""> 
185                         <tr> 
186                                 <th scope=""col"">&nbsp;</th> 
187                         </tr><tr> 
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> 
189                         </tr><tr> 
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> 
191                         </tr><tr> 
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> 
193                         </tr> 
194                 </table> 
195         </div>";
196                         
197                         WebTest t = new WebTest ("CheckBoxField_Bug595568_2.aspx");
198                         string pageHtml = t.Run ();
199                         string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
200
201                         HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
202                 }
203
204                 [Test (Description="Bug 595568 #5")]
205                 public void CheckBoxField_Bug595568_5 ()
206                 {
207                         string originalHtml = @"<div> 
208         <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;""> 
209                         <tr> 
210                                 <th scope=""col"">&nbsp;</th> 
211                         </tr><tr> 
212                                 <td><select size=""4""> 
213  
214                                 </select></td> 
215                         </tr><tr> 
216                                 <td><select size=""4""> 
217  
218                                 </select></td> 
219                         </tr><tr> 
220                                 <td><select size=""4""> 
221  
222                                 </select></td> 
223                         </tr> 
224                 </table> 
225         </div>";
226
227                         WebTest t = new WebTest ("CheckBoxField_Bug595568_5.aspx");
228                         string pageHtml = t.Run ();
229                         string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
230
231                         HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
232                 }
233                 
234                 [Test (Description="Bug 595568 #6")]
235                 public void CheckBoxField_Bug595568_6 ()
236                 {       
237                         string originalHtml = @"<div> 
238         <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;""> 
239                         <tr> 
240                                 <th scope=""col"">&nbsp;</th> 
241                         </tr><tr> 
242                                 <td><select size=""4""> 
243  
244                                 </select><input id=""gridView_ctl02_ctl01"" type=""checkbox"" name=""gridView$ctl02$ctl01"" /></td> 
245                         </tr><tr> 
246                                 <td><select size=""4""> 
247  
248                                 </select><input id=""gridView_ctl03_ctl01"" type=""checkbox"" name=""gridView$ctl03$ctl01"" /></td> 
249                         </tr><tr> 
250                                 <td><select size=""4""> 
251  
252                                 </select><input id=""gridView_ctl04_ctl01"" type=""checkbox"" name=""gridView$ctl04$ctl01"" /></td> 
253                         </tr> 
254                 </table> 
255         </div>";
256
257                         WebTest t = new WebTest ("CheckBoxField_Bug595568_6.aspx");
258                         string pageHtml = t.Run ();
259                         string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
260
261                         HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
262                 }
263
264                 [Test (Description="Bug 595568 #7")]
265                 public void CheckBoxField_Bug595568_7 ()
266                 {
267                         string originalHtml = @"<div> 
268         <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;""> 
269                         <tr> 
270                                 <th scope=""col"">&nbsp;</th> 
271                         </tr><tr> 
272                                 <td><input id=""gridView_ctl02_ctl00"" type=""checkbox"" name=""gridView$ctl02$ctl00"" /><select size=""4""> 
273  
274                                 </select></td> 
275                         </tr><tr> 
276                                 <td><input id=""gridView_ctl03_ctl00"" type=""checkbox"" name=""gridView$ctl03$ctl00"" /><select size=""4""> 
277  
278                                 </select></td> 
279                         </tr><tr> 
280                                 <td><input id=""gridView_ctl04_ctl00"" type=""checkbox"" name=""gridView$ctl04$ctl00"" /><select size=""4""> 
281  
282                                 </select></td> 
283                         </tr> 
284                 </table> 
285         </div>";
286
287                         WebTest t = new WebTest ("CheckBoxField_Bug595568_7.aspx");             
288                         string pageHtml = t.Run ();
289                         string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
290
291                         HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
292                 }
293                 
294                 [Test]
295                 public void CheckBoxField_DefaultProperty ()
296                 {
297                         PokerCheckBoxField field = new PokerCheckBoxField ();
298                         Assert.AreEqual ("", field.DataField, "DataField");
299                         Assert.AreEqual ("", field.Text, "Text");
300                         Assert.AreEqual (false, field.GetSupportsHtmlEncode, "SupportsHtmlEncode"); 
301                 }
302
303                 [Test]
304                 public void CheckBoxField_AssignProperty ()
305                 {
306                         PokerCheckBoxField field = new PokerCheckBoxField ();
307                         field.DataField = "test";
308                         Assert.AreEqual ("test", field.DataField, "DataField");
309                         field.Text = "test";
310                         Assert.AreEqual ("test", field.Text, "Text");
311                 }
312
313                 public void CheckBoxField_ExtractValuesFromCell ()
314                 {
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 ();
323                         cb.Checked = true;
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");
329                 }
330
331                 [Test]
332                 public void CheckBoxField_ValidateSupportsCallback ()
333                 {
334                         //This method has been implemented as an empty method           
335                 }
336
337                 [Test]
338                 public void CheckBoxField_CopyProperties()
339                 {
340                         PokerCheckBoxField field = new PokerCheckBoxField ();
341                         CheckBoxField copy = new CheckBoxField();
342                         field.DataField = "test";
343                         field.Text = "test";
344                         field.DoCopyProperties (copy);
345                         Assert.AreEqual ("test", copy.Text, "Text");
346                         Assert.AreEqual ("test", copy.DataField, "DataField");
347                 }
348
349                 [Test]
350                 public void CheckBoxField_CreateField ()
351                 {
352                         PokerCheckBoxField field = new PokerCheckBoxField ();
353                         CheckBoxField blank = (CheckBoxField)field.DoCreateField ();
354                         Assert.IsNotNull (blank, "CreateField");
355                 }
356
357                 [Test]
358                 public void CheckBoxField_GetDesignTimeValue ()
359                 {
360                         PokerCheckBoxField field = new PokerCheckBoxField ();
361                         bool result = (bool)field.DoGetDesignTimeValue ();
362                         Assert.AreEqual (true, result, "GetDesignTimeValue");
363                 }
364
365                 [Test]
366                 public void CheckBoxField_InitializeDataCell ()
367                 {
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");
376
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");
384                 }
385
386                 [Test]
387                 public void CheckBoxField_OnDataBindField ()
388                 {
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);
399                         grid.DataBind ();
400                         Assert.AreEqual (2, databound, "DataBindField");
401                         Assert.AreEqual (4, ((Control) grid.Controls[0]).Controls.Count, "DataBindFieldRowCountr");
402                 }
403
404                 [Test]
405                 [ExpectedException (typeof (HttpException))]
406                 public void CheckBoxField_OnDataBindFieldException ()
407                 {
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);
418                         grid.DataBind ();
419                 }
420
421                 [Test]
422                 [ExpectedException(typeof(NotSupportedException))]
423                 public void CheckBoxField_GetApplyFormatInEditModeExeption ()
424                 {
425                         PokerCheckBoxField field = new PokerCheckBoxField ();
426                         bool stab = field.ApplyFormatInEditMode;
427                 }
428
429                 [Test]
430                 [ExpectedException (typeof (NotSupportedException))]
431                 public void CheckBoxField_SetApplyFormatInEditModeExeption ()
432                 {
433                         PokerCheckBoxField field = new PokerCheckBoxField ();
434                         field.ApplyFormatInEditMode = true;
435                 }
436
437                 [Test]
438                 [ExpectedException (typeof (NotSupportedException))]
439                 public void CheckBoxField_GetConvertEmptyStringToNull ()
440                 {
441                         PokerCheckBoxField field = new PokerCheckBoxField ();
442                         bool stab = field.ConvertEmptyStringToNull;
443                 }
444
445                 [Test]
446                 [ExpectedException (typeof (NotSupportedException))]
447                 public void CheckBoxField_SetConvertEmptyStringToNull ()
448                 {
449                         PokerCheckBoxField field = new PokerCheckBoxField ();
450                         field.ConvertEmptyStringToNull = true;
451                 }
452
453                 [Test]
454                 [ExpectedException (typeof (NotSupportedException))]
455                 public void CheckBoxField_SetDataFormatString ()
456                 {
457                         PokerCheckBoxField field = new PokerCheckBoxField ();
458                         field.DataFormatString = "";
459                 }
460
461                 [Test]
462                 [ExpectedException (typeof (NotSupportedException))]
463                 public void CheckBoxField_GetDataFormatString ()
464                 {
465                         PokerCheckBoxField field = new PokerCheckBoxField ();
466                         string res = field.DataFormatString;
467                 }
468
469                 [Test]
470                 [ExpectedException (typeof (NotSupportedException))]
471                 public void CheckBoxField_SetHtmlEncode ()
472                 {
473                         PokerCheckBoxField field = new PokerCheckBoxField ();
474                         field.HtmlEncode = true;
475                 }
476
477                 [Test]
478                 [ExpectedException (typeof (NotSupportedException))]
479                 public void CheckBoxField_GetHtmlEncode ()
480                 {
481                         PokerCheckBoxField field = new PokerCheckBoxField ();
482                         bool res = field.HtmlEncode;
483                 }
484
485                 [Test]
486                 [ExpectedException (typeof (NotSupportedException))]
487                 public void CheckBoxField_SetNullDisplayText ()
488                 {
489                         PokerCheckBoxField field = new PokerCheckBoxField ();
490                         field.NullDisplayText = "";
491                 }
492
493                 [Test]
494                 [ExpectedException (typeof (NotSupportedException))]
495                 public void CheckBoxField_GetNullDisplayText ()
496                 {
497                         PokerCheckBoxField field = new PokerCheckBoxField ();
498                         string res = field.NullDisplayText;
499                 }
500
501                 public  DataTable CreateDataSource ()
502                 {
503                         DataTable aTable = new DataTable ("A");
504                         DataColumn dtCol;
505                         DataRow dtRow;
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;
512
513                         // Add the column to the DataColumnCollection.
514                         aTable.Columns.Add (dtCol);
515
516
517                         dtCol = new DataColumn ();
518                         dtCol.DataType = Type.GetType ("System.String");
519                         dtCol.ColumnName = WRONGFIELD;
520                         dtCol.Caption = WRONGFIELD;
521                         dtCol.ReadOnly = true;
522                         
523
524                         // Add the column to the DataColumnCollection.
525                         aTable.Columns.Add (dtCol);
526
527                         // Create 2 rows to the table
528                         dtRow = aTable.NewRow ();
529                         dtRow[FIELDNAME] = true;
530                         dtRow[WRONGFIELD] = "1";
531                         aTable.Rows.Add (dtRow);
532
533                         dtRow = aTable.NewRow ();
534                         dtRow[FIELDNAME] = false;
535                         dtRow[WRONGFIELD] = "1";
536                         aTable.Rows.Add (dtRow);
537                         return aTable;
538                 }
539         }
540 }
541 #endif