Tests for GridView and GridView subclasses
[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                 [Category ("NotWorking")]
190                 public void CheckBoxField_InitializeDataCell ()
191                 {
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");
200
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");
208                 }
209
210                 [Test]
211                 [Category ("NotWorking")]
212                 public void CheckBoxField_OnDataBindField ()
213                 {
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);
224                         grid.DataBind ();
225                         Assert.AreEqual (2, databound, "DataBindField");
226                         Assert.AreEqual (4, ((Control) grid.Controls[0]).Controls.Count, "DataBindFieldRowCountr");
227                 }
228
229                 [Test]
230                 [Category ("NotWorking")]
231                 [ExpectedException (typeof (HttpException))]
232                 public void CheckBoxField_OnDataBindFieldException ()
233                 {
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);
244                         grid.DataBind ();
245                 }
246
247                 [Test]
248                 [Category ("NotWorking")]
249                 [ExpectedException(typeof(NotSupportedException))]
250                 public void CheckBoxField_GetApplyFormatInEditModeExeption ()
251                 {
252                         PokerCheckBoxField field = new PokerCheckBoxField ();
253                         bool stab = field.ApplyFormatInEditMode;
254                 }
255
256                 [Test]
257                 [Category ("NotWorking")]
258                 [ExpectedException (typeof (NotSupportedException))]
259                 public void CheckBoxField_SetApplyFormatInEditModeExeption ()
260                 {
261                         PokerCheckBoxField field = new PokerCheckBoxField ();
262                         field.ApplyFormatInEditMode = true;
263                 }
264
265                 [Test]
266                 [ExpectedException (typeof (NotSupportedException))]
267                 public void CheckBoxField_GetConvertEmptyStringToNull ()
268                 {
269                         PokerCheckBoxField field = new PokerCheckBoxField ();
270                         bool stab = field.ConvertEmptyStringToNull;
271                 }
272
273                 [Test]
274                 [ExpectedException (typeof (NotSupportedException))]
275                 public void CheckBoxField_SetConvertEmptyStringToNull ()
276                 {
277                         PokerCheckBoxField field = new PokerCheckBoxField ();
278                         field.ConvertEmptyStringToNull = true;
279                 }
280
281                 [Test]
282                 [ExpectedException (typeof (NotSupportedException))]
283                 public void CheckBoxField_SetDataFormatString ()
284                 {
285                         PokerCheckBoxField field = new PokerCheckBoxField ();
286                         field.DataFormatString = "";
287                 }
288
289                 [Test]
290                 [ExpectedException (typeof (NotSupportedException))]
291                 public void CheckBoxField_GetDataFormatString ()
292                 {
293                         PokerCheckBoxField field = new PokerCheckBoxField ();
294                         string res = field.DataFormatString;
295                 }
296
297                 [Test]
298                 [ExpectedException (typeof (NotSupportedException))]
299                 public void CheckBoxField_SetHtmlEncode ()
300                 {
301                         PokerCheckBoxField field = new PokerCheckBoxField ();
302                         field.HtmlEncode = true;
303                 }
304
305                 [Test]
306                 [ExpectedException (typeof (NotSupportedException))]
307                 public void CheckBoxField_GetHtmlEncode ()
308                 {
309                         PokerCheckBoxField field = new PokerCheckBoxField ();
310                         bool res = field.HtmlEncode;
311                 }
312
313                 [Test]
314                 [ExpectedException (typeof (NotSupportedException))]
315                 public void CheckBoxField_SetNullDisplayText ()
316                 {
317                         PokerCheckBoxField field = new PokerCheckBoxField ();
318                         field.NullDisplayText = "";
319                 }
320
321                 [Test]
322                 [ExpectedException (typeof (NotSupportedException))]
323                 public void CheckBoxField_GetNullDisplayText ()
324                 {
325                         PokerCheckBoxField field = new PokerCheckBoxField ();
326                         string res = field.NullDisplayText;
327                 }
328
329                 public  DataTable CreateDataSource ()
330                 {
331                         DataTable aTable = new DataTable ("A");
332                         DataColumn dtCol;
333                         DataRow dtRow;
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;
340
341                         // Add the column to the DataColumnCollection.
342                         aTable.Columns.Add (dtCol);
343
344
345                         dtCol = new DataColumn ();
346                         dtCol.DataType = Type.GetType ("System.String");
347                         dtCol.ColumnName = WRONGFIELD;
348                         dtCol.Caption = WRONGFIELD;
349                         dtCol.ReadOnly = true;
350                         
351
352                         // Add the column to the DataColumnCollection.
353                         aTable.Columns.Add (dtCol);
354
355                         // Create 2 rows to the table
356                         dtRow = aTable.NewRow ();
357                         dtRow[FIELDNAME] = true;
358                         dtRow[WRONGFIELD] = "1";
359                         aTable.Rows.Add (dtRow);
360
361                         dtRow = aTable.NewRow ();
362                         dtRow[FIELDNAME] = false;
363                         dtRow[WRONGFIELD] = "1";
364                         aTable.Rows.Add (dtRow);
365                         return aTable;
366                 }
367         }
368 }
369 #endif