New test.
[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
47
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                 [Test]
118                 public void CheckBoxField_DefaultProperty ()
119                 {
120                         PokerCheckBoxField field = new PokerCheckBoxField ();
121                         Assert.AreEqual ("", field.DataField, "DataField");
122                         Assert.AreEqual ("", field.Text, "Text");
123                         Assert.AreEqual (false, field.GetSupportsHtmlEncode, "SupportsHtmlEncode"); 
124                 }
125
126                 [Test]
127                 public void CheckBoxField_AssignProperty ()
128                 {
129                         PokerCheckBoxField field = new PokerCheckBoxField ();
130                         field.DataField = "test";
131                         Assert.AreEqual ("test", field.DataField, "DataField");
132                         field.Text = "test";
133                         Assert.AreEqual ("test", field.Text, "Text");
134                 }
135
136                 public void CheckBoxField_ExtractValuesFromCell ()
137                 {
138                         PokerCheckBoxField field = new PokerCheckBoxField ();
139                         OrderedDictionary dictionary = new OrderedDictionary ();
140                         DataControlFieldCell cell = new DataControlFieldCell (null);
141                         cell.Controls.Add (new CheckBox ());
142                         field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
143                         Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#1");
144                         Assert.AreEqual ("False", dictionary[0].ToString (), "ExtractValuesFromCellValueFalse");
145                         CheckBox cb = new CheckBox ();
146                         cb.Checked = true;
147                         cell.Controls.Clear ();
148                         cell.Controls.Add (cb);
149                         field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
150                         Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#2");
151                         Assert.AreEqual ("True", dictionary[0].ToString (), "ExtractValuesFromCellValueTrue");
152                 }
153
154                 [Test]
155                 public void CheckBoxField_ValidateSupportsCallback ()
156                 {
157                         //This method has been implemented as an empty method           
158                 }
159
160                 [Test]
161                 public void CheckBoxField_CopyProperties()
162                 {
163                         PokerCheckBoxField field = new PokerCheckBoxField ();
164                         CheckBoxField copy = new CheckBoxField();
165                         field.DataField = "test";
166                         field.Text = "test";
167                         field.DoCopyProperties (copy);
168                         Assert.AreEqual ("test", copy.Text, "Text");
169                         Assert.AreEqual ("test", copy.DataField, "DataField");
170                 }
171
172                 [Test]
173                 public void CheckBoxField_CreateField ()
174                 {
175                         PokerCheckBoxField field = new PokerCheckBoxField ();
176                         CheckBoxField blank = (CheckBoxField)field.DoCreateField ();
177                         Assert.IsNotNull (blank, "CreateField");
178                 }
179
180                 [Test]
181                 public void CheckBoxField_GetDesignTimeValue ()
182                 {
183                         PokerCheckBoxField field = new PokerCheckBoxField ();
184                         bool result = (bool)field.DoGetDesignTimeValue ();
185                         Assert.AreEqual (true, result, "GetDesignTimeValue");
186                 }
187
188                 [Test]
189                 public void CheckBoxField_InitializeDataCell ()
190                 {
191                         PokerCheckBoxField field = new PokerCheckBoxField ();
192                         field.HeaderText = "headertest";
193                         DataControlFieldCell cell = new DataControlFieldCell (null);
194                         DataControlRowState state = DataControlRowState.Edit;
195                         Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
196                         field.DoInitializeDataCell (cell, state);
197                         Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
198                         Assert.AreEqual ("headertest", ((CheckBox)cell.Controls[0]).ToolTip, "InitializeDataCellControlsData");
199
200                         cell.Controls.Clear ();
201                         field.DataField = "fake";
202                         field.Text = "celltext";
203                         state = DataControlRowState.Normal;
204                         field.DoInitializeDataCell (cell, state);
205                         Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
206                         Assert.AreEqual ("celltext", ((CheckBox) cell.Controls[0]).Text, "InitializeDataCellControlsData");
207                 }
208
209                 [Test]
210                 public void CheckBoxField_OnDataBindField ()
211                 {
212                         Page page = new Page ();
213                         GridView grid = new GridView ();
214                         page.Controls.Add (grid);
215                         grid.DataSource = this.CreateDataSource ();
216                         grid.AutoGenerateColumns = false;
217                         PokerCheckBoxField field = new PokerCheckBoxField ();
218                         field.HeaderText = "field_header";
219                         field.FooterText = "field_footer";
220                         field.DataField = FIELDNAME;
221                         grid.Columns.Add (field);
222                         grid.DataBind ();
223                         Assert.AreEqual (2, databound, "DataBindField");
224                         Assert.AreEqual (4, ((Control) grid.Controls[0]).Controls.Count, "DataBindFieldRowCountr");
225                 }
226
227                 [Test]
228                 [ExpectedException (typeof (HttpException))]
229                 public void CheckBoxField_OnDataBindFieldException ()
230                 {
231                         Page page = new Page ();
232                         GridView grid = new GridView ();
233                         page.Controls.Add (grid);
234                         grid.DataSource = this.CreateDataSource ();
235                         grid.AutoGenerateColumns = false;
236                         PokerCheckBoxField field = new PokerCheckBoxField ();
237                         field.HeaderText = "field_header";
238                         field.FooterText = "field_footer";
239                         field.DataField = WRONGFIELD;
240                         grid.Columns.Add (field);
241                         grid.DataBind ();
242                 }
243
244                 [Test]
245                 [ExpectedException(typeof(NotSupportedException))]
246                 public void CheckBoxField_GetApplyFormatInEditModeExeption ()
247                 {
248                         PokerCheckBoxField field = new PokerCheckBoxField ();
249                         bool stab = field.ApplyFormatInEditMode;
250                 }
251
252                 [Test]
253                 [ExpectedException (typeof (NotSupportedException))]
254                 public void CheckBoxField_SetApplyFormatInEditModeExeption ()
255                 {
256                         PokerCheckBoxField field = new PokerCheckBoxField ();
257                         field.ApplyFormatInEditMode = true;
258                 }
259
260                 [Test]
261                 [ExpectedException (typeof (NotSupportedException))]
262                 public void CheckBoxField_GetConvertEmptyStringToNull ()
263                 {
264                         PokerCheckBoxField field = new PokerCheckBoxField ();
265                         bool stab = field.ConvertEmptyStringToNull;
266                 }
267
268                 [Test]
269                 [ExpectedException (typeof (NotSupportedException))]
270                 public void CheckBoxField_SetConvertEmptyStringToNull ()
271                 {
272                         PokerCheckBoxField field = new PokerCheckBoxField ();
273                         field.ConvertEmptyStringToNull = true;
274                 }
275
276                 [Test]
277                 [ExpectedException (typeof (NotSupportedException))]
278                 public void CheckBoxField_SetDataFormatString ()
279                 {
280                         PokerCheckBoxField field = new PokerCheckBoxField ();
281                         field.DataFormatString = "";
282                 }
283
284                 [Test]
285                 [ExpectedException (typeof (NotSupportedException))]
286                 public void CheckBoxField_GetDataFormatString ()
287                 {
288                         PokerCheckBoxField field = new PokerCheckBoxField ();
289                         string res = field.DataFormatString;
290                 }
291
292                 [Test]
293                 [ExpectedException (typeof (NotSupportedException))]
294                 public void CheckBoxField_SetHtmlEncode ()
295                 {
296                         PokerCheckBoxField field = new PokerCheckBoxField ();
297                         field.HtmlEncode = true;
298                 }
299
300                 [Test]
301                 [ExpectedException (typeof (NotSupportedException))]
302                 public void CheckBoxField_GetHtmlEncode ()
303                 {
304                         PokerCheckBoxField field = new PokerCheckBoxField ();
305                         bool res = field.HtmlEncode;
306                 }
307
308                 [Test]
309                 [ExpectedException (typeof (NotSupportedException))]
310                 public void CheckBoxField_SetNullDisplayText ()
311                 {
312                         PokerCheckBoxField field = new PokerCheckBoxField ();
313                         field.NullDisplayText = "";
314                 }
315
316                 [Test]
317                 [ExpectedException (typeof (NotSupportedException))]
318                 public void CheckBoxField_GetNullDisplayText ()
319                 {
320                         PokerCheckBoxField field = new PokerCheckBoxField ();
321                         string res = field.NullDisplayText;
322                 }
323
324                 public  DataTable CreateDataSource ()
325                 {
326                         DataTable aTable = new DataTable ("A");
327                         DataColumn dtCol;
328                         DataRow dtRow;
329                         // Create ID column and add to the DataTable.
330                         dtCol = new DataColumn ();
331                         dtCol.DataType = Type.GetType ("System.Boolean");
332                         dtCol.ColumnName = FIELDNAME;
333                         dtCol.Caption = FIELDNAME;
334                         dtCol.ReadOnly = true;
335
336                         // Add the column to the DataColumnCollection.
337                         aTable.Columns.Add (dtCol);
338
339
340                         dtCol = new DataColumn ();
341                         dtCol.DataType = Type.GetType ("System.String");
342                         dtCol.ColumnName = WRONGFIELD;
343                         dtCol.Caption = WRONGFIELD;
344                         dtCol.ReadOnly = true;
345                         
346
347                         // Add the column to the DataColumnCollection.
348                         aTable.Columns.Add (dtCol);
349
350                         // Create 2 rows to the table
351                         dtRow = aTable.NewRow ();
352                         dtRow[FIELDNAME] = true;
353                         dtRow[WRONGFIELD] = "1";
354                         aTable.Rows.Add (dtRow);
355
356                         dtRow = aTable.NewRow ();
357                         dtRow[FIELDNAME] = false;
358                         dtRow[WRONGFIELD] = "1";
359                         aTable.Rows.Add (dtRow);
360                         return aTable;
361                 }
362         }
363 }
364 #endif