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;
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;
118 public void CheckBoxField_DefaultProperty ()
120 PokerCheckBoxField field = new PokerCheckBoxField ();
121 Assert.AreEqual ("", field.DataField, "DataField");
122 Assert.AreEqual ("", field.Text, "Text");
123 Assert.AreEqual (false, field.GetSupportsHtmlEncode, "SupportsHtmlEncode");
127 public void CheckBoxField_AssignProperty ()
129 PokerCheckBoxField field = new PokerCheckBoxField ();
130 field.DataField = "test";
131 Assert.AreEqual ("test", field.DataField, "DataField");
133 Assert.AreEqual ("test", field.Text, "Text");
136 public void CheckBoxField_ExtractValuesFromCell ()
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 ();
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");
155 public void CheckBoxField_ValidateSupportsCallback ()
157 //This method has been implemented as an empty method
161 public void CheckBoxField_CopyProperties()
163 PokerCheckBoxField field = new PokerCheckBoxField ();
164 CheckBoxField copy = new CheckBoxField();
165 field.DataField = "test";
167 field.DoCopyProperties (copy);
168 Assert.AreEqual ("test", copy.Text, "Text");
169 Assert.AreEqual ("test", copy.DataField, "DataField");
173 public void CheckBoxField_CreateField ()
175 PokerCheckBoxField field = new PokerCheckBoxField ();
176 CheckBoxField blank = (CheckBoxField)field.DoCreateField ();
177 Assert.IsNotNull (blank, "CreateField");
181 public void CheckBoxField_GetDesignTimeValue ()
183 PokerCheckBoxField field = new PokerCheckBoxField ();
184 bool result = (bool)field.DoGetDesignTimeValue ();
185 Assert.AreEqual (true, result, "GetDesignTimeValue");
189 public void CheckBoxField_InitializeDataCell ()
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");
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");
210 public void CheckBoxField_OnDataBindField ()
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);
223 Assert.AreEqual (2, databound, "DataBindField");
224 Assert.AreEqual (4, ((Control) grid.Controls[0]).Controls.Count, "DataBindFieldRowCountr");
228 [ExpectedException (typeof (HttpException))]
229 public void CheckBoxField_OnDataBindFieldException ()
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);
245 [ExpectedException(typeof(NotSupportedException))]
246 public void CheckBoxField_GetApplyFormatInEditModeExeption ()
248 PokerCheckBoxField field = new PokerCheckBoxField ();
249 bool stab = field.ApplyFormatInEditMode;
253 [ExpectedException (typeof (NotSupportedException))]
254 public void CheckBoxField_SetApplyFormatInEditModeExeption ()
256 PokerCheckBoxField field = new PokerCheckBoxField ();
257 field.ApplyFormatInEditMode = true;
261 [ExpectedException (typeof (NotSupportedException))]
262 public void CheckBoxField_GetConvertEmptyStringToNull ()
264 PokerCheckBoxField field = new PokerCheckBoxField ();
265 bool stab = field.ConvertEmptyStringToNull;
269 [ExpectedException (typeof (NotSupportedException))]
270 public void CheckBoxField_SetConvertEmptyStringToNull ()
272 PokerCheckBoxField field = new PokerCheckBoxField ();
273 field.ConvertEmptyStringToNull = true;
277 [ExpectedException (typeof (NotSupportedException))]
278 public void CheckBoxField_SetDataFormatString ()
280 PokerCheckBoxField field = new PokerCheckBoxField ();
281 field.DataFormatString = "";
285 [ExpectedException (typeof (NotSupportedException))]
286 public void CheckBoxField_GetDataFormatString ()
288 PokerCheckBoxField field = new PokerCheckBoxField ();
289 string res = field.DataFormatString;
293 [ExpectedException (typeof (NotSupportedException))]
294 public void CheckBoxField_SetHtmlEncode ()
296 PokerCheckBoxField field = new PokerCheckBoxField ();
297 field.HtmlEncode = true;
301 [ExpectedException (typeof (NotSupportedException))]
302 public void CheckBoxField_GetHtmlEncode ()
304 PokerCheckBoxField field = new PokerCheckBoxField ();
305 bool res = field.HtmlEncode;
309 [ExpectedException (typeof (NotSupportedException))]
310 public void CheckBoxField_SetNullDisplayText ()
312 PokerCheckBoxField field = new PokerCheckBoxField ();
313 field.NullDisplayText = "";
317 [ExpectedException (typeof (NotSupportedException))]
318 public void CheckBoxField_GetNullDisplayText ()
320 PokerCheckBoxField field = new PokerCheckBoxField ();
321 string res = field.NullDisplayText;
324 public DataTable CreateDataSource ()
326 DataTable aTable = new DataTable ("A");
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;
336 // Add the column to the DataColumnCollection.
337 aTable.Columns.Add (dtCol);
340 dtCol = new DataColumn ();
341 dtCol.DataType = Type.GetType ("System.String");
342 dtCol.ColumnName = WRONGFIELD;
343 dtCol.Caption = WRONGFIELD;
344 dtCol.ReadOnly = true;
347 // Add the column to the DataColumnCollection.
348 aTable.Columns.Add (dtCol);
350 // Create 2 rows to the table
351 dtRow = aTable.NewRow ();
352 dtRow[FIELDNAME] = true;
353 dtRow[WRONGFIELD] = "1";
354 aTable.Rows.Add (dtRow);
356 dtRow = aTable.NewRow ();
357 dtRow[FIELDNAME] = false;
358 dtRow[WRONGFIELD] = "1";
359 aTable.Rows.Add (dtRow);