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 [Category ("NotWorking")]
190 public void CheckBoxField_InitializeDataCell ()
192 PokerCheckBoxField field = new PokerCheckBoxField ();
193 field.HeaderText = "headertest";
194 DataControlFieldCell cell = new DataControlFieldCell (null);
195 DataControlRowState state = DataControlRowState.Edit;
196 Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
197 field.DoInitializeDataCell (cell, state);
198 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
199 Assert.AreEqual ("headertest", ((CheckBox)cell.Controls[0]).ToolTip, "InitializeDataCellControlsData");
201 cell.Controls.Clear ();
202 field.DataField = "fake";
203 field.Text = "celltext";
204 state = DataControlRowState.Normal;
205 field.DoInitializeDataCell (cell, state);
206 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
207 Assert.AreEqual ("celltext", ((CheckBox) cell.Controls[0]).Text, "InitializeDataCellControlsData");
211 [Category ("NotWorking")]
212 public void CheckBoxField_OnDataBindField ()
214 Page page = new Page ();
215 GridView grid = new GridView ();
216 page.Controls.Add (grid);
217 grid.DataSource = this.CreateDataSource ();
218 grid.AutoGenerateColumns = false;
219 PokerCheckBoxField field = new PokerCheckBoxField ();
220 field.HeaderText = "field_header";
221 field.FooterText = "field_footer";
222 field.DataField = FIELDNAME;
223 grid.Columns.Add (field);
225 Assert.AreEqual (2, databound, "DataBindField");
226 Assert.AreEqual (4, ((Control) grid.Controls[0]).Controls.Count, "DataBindFieldRowCountr");
230 [Category ("NotWorking")]
231 [ExpectedException (typeof (HttpException))]
232 public void CheckBoxField_OnDataBindFieldException ()
234 Page page = new Page ();
235 GridView grid = new GridView ();
236 page.Controls.Add (grid);
237 grid.DataSource = this.CreateDataSource ();
238 grid.AutoGenerateColumns = false;
239 PokerCheckBoxField field = new PokerCheckBoxField ();
240 field.HeaderText = "field_header";
241 field.FooterText = "field_footer";
242 field.DataField = WRONGFIELD;
243 grid.Columns.Add (field);
248 [Category ("NotWorking")]
249 [ExpectedException(typeof(NotSupportedException))]
250 public void CheckBoxField_GetApplyFormatInEditModeExeption ()
252 PokerCheckBoxField field = new PokerCheckBoxField ();
253 bool stab = field.ApplyFormatInEditMode;
257 [Category ("NotWorking")]
258 [ExpectedException (typeof (NotSupportedException))]
259 public void CheckBoxField_SetApplyFormatInEditModeExeption ()
261 PokerCheckBoxField field = new PokerCheckBoxField ();
262 field.ApplyFormatInEditMode = true;
266 [ExpectedException (typeof (NotSupportedException))]
267 public void CheckBoxField_GetConvertEmptyStringToNull ()
269 PokerCheckBoxField field = new PokerCheckBoxField ();
270 bool stab = field.ConvertEmptyStringToNull;
274 [ExpectedException (typeof (NotSupportedException))]
275 public void CheckBoxField_SetConvertEmptyStringToNull ()
277 PokerCheckBoxField field = new PokerCheckBoxField ();
278 field.ConvertEmptyStringToNull = true;
282 [ExpectedException (typeof (NotSupportedException))]
283 public void CheckBoxField_SetDataFormatString ()
285 PokerCheckBoxField field = new PokerCheckBoxField ();
286 field.DataFormatString = "";
290 [ExpectedException (typeof (NotSupportedException))]
291 public void CheckBoxField_GetDataFormatString ()
293 PokerCheckBoxField field = new PokerCheckBoxField ();
294 string res = field.DataFormatString;
298 [ExpectedException (typeof (NotSupportedException))]
299 public void CheckBoxField_SetHtmlEncode ()
301 PokerCheckBoxField field = new PokerCheckBoxField ();
302 field.HtmlEncode = true;
306 [ExpectedException (typeof (NotSupportedException))]
307 public void CheckBoxField_GetHtmlEncode ()
309 PokerCheckBoxField field = new PokerCheckBoxField ();
310 bool res = field.HtmlEncode;
314 [ExpectedException (typeof (NotSupportedException))]
315 public void CheckBoxField_SetNullDisplayText ()
317 PokerCheckBoxField field = new PokerCheckBoxField ();
318 field.NullDisplayText = "";
322 [ExpectedException (typeof (NotSupportedException))]
323 public void CheckBoxField_GetNullDisplayText ()
325 PokerCheckBoxField field = new PokerCheckBoxField ();
326 string res = field.NullDisplayText;
329 public DataTable CreateDataSource ()
331 DataTable aTable = new DataTable ("A");
334 // Create ID column and add to the DataTable.
335 dtCol = new DataColumn ();
336 dtCol.DataType = Type.GetType ("System.Boolean");
337 dtCol.ColumnName = FIELDNAME;
338 dtCol.Caption = FIELDNAME;
339 dtCol.ReadOnly = true;
341 // Add the column to the DataColumnCollection.
342 aTable.Columns.Add (dtCol);
345 dtCol = new DataColumn ();
346 dtCol.DataType = Type.GetType ("System.String");
347 dtCol.ColumnName = WRONGFIELD;
348 dtCol.Caption = WRONGFIELD;
349 dtCol.ReadOnly = true;
352 // Add the column to the DataColumnCollection.
353 aTable.Columns.Add (dtCol);
355 // Create 2 rows to the table
356 dtRow = aTable.NewRow ();
357 dtRow[FIELDNAME] = true;
358 dtRow[WRONGFIELD] = "1";
359 aTable.Rows.Add (dtRow);
361 dtRow = aTable.NewRow ();
362 dtRow[FIELDNAME] = false;
363 dtRow[WRONGFIELD] = "1";
364 aTable.Rows.Add (dtRow);