2 // Tests for System.Web.UI.WebControls.BoundFieldTest.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.
33 using System.Collections.Generic;
37 using System.Web.UI.WebControls;
40 using System.Collections;
41 using System.Collections.Specialized;
42 using Image = System.Web.UI.WebControls.Image;
43 using NUnit.Framework;
44 using System.Globalization;
\r
45 using MonoTests.SystemWeb.Framework;
\r
46 using MonoTests.stand_alone.WebHarness;
50 namespace MonoTests.System.Web.UI.WebControls
52 class PokerBoundField : BoundField
54 public Button bindbutoon;
56 public PokerBoundField () {
58 bindbutoon = new Button ();
59 bindbutoon.DataBinding += new EventHandler (OnDataBindField);
63 public StateBag StateBag {
64 get { return base.ViewState; }
67 public bool DoSupportsHtmlEncode {
69 return base.SupportsHtmlEncode;
73 public void DoCopyProperties (DataControlField newField) {
74 base.CopyProperties (newField);
77 public DataControlField DoCreateField () {
78 return base.CreateField ();
81 public string DoFormatDataValue (object dataValue, bool encode) {
82 return this.FormatDataValue (dataValue, encode);
85 public object DoGetDesignTimeValue () {
86 return base.GetDesignTimeValue ();
89 public object DoGetValue (Control controlContainer) {
90 return base.GetValue (controlContainer);
93 public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState) {
94 base.InitializeDataCell (cell, rowState);
97 public void DoOnDataBindField (object sender, EventArgs e) {
98 base.OnDataBindField (sender, e);
101 public Control GetControl {
102 get { return base.Control; }
109 public class BoundFieldTest
112 public void BoundField_DefaultProperty () {
113 PokerBoundField bf = new PokerBoundField ();
114 Assert.AreEqual ("!", PokerBoundField.ThisExpression, "StaticThisExpression");
115 Assert.AreEqual ("", bf.DataField, "DataField");
116 Assert.AreEqual ("", bf.DataFormatString, "DataFormatString");
117 Assert.AreEqual ("", bf.HeaderText, "HeaderText");
118 Assert.AreEqual (true, bf.HtmlEncode, "HtmlEncode");
119 Assert.AreEqual ("", bf.NullDisplayText, "NullDisplayText");
120 Assert.AreEqual (false, bf.ReadOnly, "ReadOnly");
123 Assert.AreEqual (true, bf.DoSupportsHtmlEncode, "SupportsHtmlEncode");
127 public void BoundField_DefaultPropertyNotWorking () {
128 PokerBoundField bf = new PokerBoundField ();
129 Assert.AreEqual (false, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
130 Assert.AreEqual (true, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
134 public void BoundField_AssignProperty () {
135 PokerBoundField bf = new PokerBoundField ();
136 bf.ConvertEmptyStringToNull = false;
137 Assert.AreEqual (false, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
138 bf.DataField = "test";
139 Assert.AreEqual ("test", bf.DataField, "DataField");
140 bf.DataFormatString = "test";
141 Assert.AreEqual ("test", bf.DataFormatString, "DataFormatString");
142 bf.HeaderText = "test";
143 Assert.AreEqual ("test", bf.HeaderText, "HeaderText");
144 bf.HtmlEncode = false;
145 Assert.AreEqual (false, bf.HtmlEncode, "HtmlEncode");
146 bf.NullDisplayText = "test";
147 Assert.AreEqual ("test", bf.NullDisplayText, "NullDisplayText");
149 Assert.AreEqual (true, bf.ReadOnly, "ReadOnly");
153 public void BoundField_AssignPropertyNotWorking () {
154 PokerBoundField bf = new PokerBoundField ();
155 bf.ApplyFormatInEditMode = true;
156 Assert.AreEqual (true, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
160 public void BoundField_ExtractValuesFromCell () {
161 PokerBoundField bf = new PokerBoundField ();
162 OrderedDictionary dictionary = new OrderedDictionary ();
163 DataControlFieldCell cell = new DataControlFieldCell (null);
165 bf.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
166 Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
167 Assert.AreEqual ("test", dictionary [0].ToString (), "ExtractValuesFromCellValue");
171 public void BoundField_Initialize () {
172 // This method initilize to private fields in a base class DataControlField
173 // Always return false
174 PokerBoundField bf = new PokerBoundField ();
175 Control control = new Control ();
177 bool res = bf.Initialize (true, control);
178 // Assert.AreEqual (false, res, "InitializeResult");
179 Assert.AreEqual ("test", bf.GetControl.ID, "InitializeControl");
183 public void BoundField_InitializeCell () {
184 PokerBoundField bf = new PokerBoundField ();
186 DataControlFieldCell cell = new DataControlFieldCell (null);
187 bf.HeaderText = "headertext";
189 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
190 Assert.AreEqual ("headertext", cell.Text, "InitializeCellHeaderText");
193 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
194 Assert.AreEqual (" ", cell.Text, "InitializeCellEmpty");
196 bf.HeaderText = "headertext";
197 // Header image url not empty
198 bf.HeaderImageUrl = "headerurl";
199 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
200 if (cell.Controls [0] is Image) {
201 Image image = (Image) cell.Controls [0];
202 Assert.AreEqual ("headerurl", image.ImageUrl, "InitializeCellHeaderImageUrl");
205 Assert.Fail ("Header Image dos not created");
209 bf.FooterText = "footertext";
210 bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
211 Assert.AreEqual ("footertext", cell.Text, "InitializeCellFooterText");
213 bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
214 Assert.AreEqual (" ", cell.Text, "InitializeCellFooterEmpty");
218 public void BoundField_ValidateSupportsCallback () {
219 //This method has been implemented as an empty method
223 public void BoundField_SortExpression () {
224 // Sorting enable = true , link button must be created
225 PokerBoundField bf = new PokerBoundField ();
227 DataControlFieldCell cell = new DataControlFieldCell (null);
229 bf.HeaderImageUrl = "";
230 bf.SortExpression = "a";
231 bf.HeaderText = "sortbutton";
232 bf.Initialize (true, new Control ()); // _base._sortingenable set to true
233 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
234 if (cell.Controls [0] is Button) { // mono
235 Button lb = (Button) cell.Controls [0];
236 Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
237 Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
238 Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
241 else if (cell.Controls [0] is LinkButton) { // .net
242 LinkButton lb = (LinkButton) cell.Controls [0];
243 Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
244 Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
245 Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
249 Assert.Fail ("Sort button does not created");
255 public void BoundField_CopyProperties () {
256 PokerBoundField bf = new PokerBoundField ();
257 BoundField copy = new BoundField ();
258 // Look not working property
259 // bf.ApplyFormatInEditMode = true;
260 bf.ConvertEmptyStringToNull = true;
261 bf.DataField = "test";
262 bf.DataFormatString = "test";
263 bf.HtmlEncode = true;
264 bf.NullDisplayText = "test";
266 bf.DoCopyProperties (copy);
267 // Look not working property
268 // Assert.AreEqual (true, copy.ApplyFormatInEditMode, "ApplyFormatInEditMode");
269 Assert.AreEqual (true, copy.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
270 Assert.AreEqual ("test", copy.DataField, "DataField");
271 Assert.AreEqual ("test", copy.DataFormatString, "DataFormatString");
272 Assert.AreEqual (true, copy.HtmlEncode, "HtmlEncode");
273 Assert.AreEqual ("test", copy.NullDisplayText, "NullDisplayText");
274 Assert.AreEqual (true, copy.ReadOnly, "ReadOnly");
278 public void BoundField_CreateField () {
279 PokerBoundField bf = new PokerBoundField ();
280 BoundField newfield = (BoundField) bf.DoCreateField ();
281 Assert.IsNotNull (newfield, "CreateField");
285 public void BoundField_FormatDataValue () {
287 PokerBoundField bf = new PokerBoundField ();
289 bf.NullDisplayText = "NullDisplayText";
290 result = bf.DoFormatDataValue (null, false);
291 Assert.AreEqual ("NullDisplayText", result, "FormatDataValueNullDataValue");
293 result = bf.DoFormatDataValue ("test", true);
294 Assert.AreEqual ("test", result, "FormatDataValueTextDataValue");
296 result = bf.DoFormatDataValue ("", true);
297 Assert.AreEqual ("NullDisplayText", result, "FormatEmptyDataValue");
299 bf.DataFormatString = "-{0,8:G}-";
300 result = bf.DoFormatDataValue (10, false);
301 Assert.AreEqual ("- 10-", result, "FormatDataValueWithFormat");
305 [Category ("NotWorking")]
306 public void BoundField_GetDesignTimeValue () {
308 PokerBoundField bf = new PokerBoundField ();
309 result = (string) bf.DoGetDesignTimeValue ();
310 Assert.AreEqual ("Databound", result, "GetDesignTimeValue");
314 [ExpectedException(typeof(HttpException), "A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.")]
315 public void BoundField_GetValueNull () {
316 PokerBoundField bf = new PokerBoundField ();
317 SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, null);
318 bf.DataField = PokerBoundField.ThisExpression;
319 string result = (string) bf.DoGetValue (ds);
323 public void BoundField_GetValue () {
324 PokerBoundField bf = new PokerBoundField ();
325 SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, "test");
326 bf.DataField = PokerBoundField.ThisExpression;
327 string result = (string) bf.DoGetValue (ds);
328 Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
332 public void BoundField_GetValueDataItem () {
333 PokerBoundField bf = new PokerBoundField ();
334 ControlWithDataItem ds = new ControlWithDataItem ("test");
335 bf.DataField = PokerBoundField.ThisExpression;
336 string result = (string) bf.DoGetValue (ds);
337 Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
341 public void BoundField_InitializeDataCell () {
342 PokerBoundField bf = new PokerBoundField ();
343 bf.HeaderText = "headertest";
344 DataControlFieldCell cell = new DataControlFieldCell (null);
345 DataControlRowState state = DataControlRowState.Edit;
346 Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
347 bf.DoInitializeDataCell (cell, state);
348 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
352 [ExpectedException (typeof (HttpException))]
353 public void BoundField_OnDataBindFieldExeption () {
354 PokerBoundField bf = new PokerBoundField ();
355 bf.bindbutoon.DataBind ();
360 [ExpectedException (typeof (HttpException))]
361 public void BoundField_GetValueExeption () {
362 PokerBoundField bf = new PokerBoundField ();
363 bf.DoGetValue (null);
367 [Category ("NunitWeb")]
\r
368 public void BoundField_NullValueRender ()
\r
370 string html = new WebTest (PageInvoker.CreateOnLoad (new PageDelegate (BasicRenderTestInit))).Run ();
\r
371 string orightml = @"<div>
\r
372 <table cellspacing=""0"" rules=""all"" border=""1"" id=""GridView1"" style=""border-collapse:collapse;"">
\r
374 <th scope=""col""> </th><th scope=""col""> </th>
\r
376 <td>Norway</td><td>Norway</td>
\r
378 <td>Sweden</td><td>Sweden</td>
\r
380 <td>EMPTY</td><td> </td>
\r
382 <td>Italy</td><td>Italy</td>
\r
386 html = HtmlDiff.GetControlFromPageHtml (html);
\r
387 HtmlDiff.AssertAreEqual (orightml, html, "NullValueRender");
\r
390 public static void BasicRenderTestInit (Page p)
\r
392 ArrayList myds = new ArrayList ();
\r
393 myds.Add (new myds_data ("Norway"));
\r
394 myds.Add (new myds_data ("Sweden"));
\r
395 myds.Add (new myds_data (""));
\r
396 myds.Add (new myds_data ("Italy"));
\r
398 BoundField bf = new BoundField ();
\r
399 bf.DataField = "Field1";
\r
400 bf.NullDisplayText = "EMPTY";
\r
402 BoundField bf2 = new BoundField ();
\r
403 bf2.DataField = "Field1";
\r
405 GridView GridView1 = new GridView();
\r
406 GridView1.AutoGenerateColumns = false;
\r
407 GridView1.Columns.Add (bf);
\r
408 GridView1.Columns.Add (bf2);
\r
409 GridView1.DataSource = myds;
\r
410 GridView1.DataBind ();
\r
412 LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
\r
413 LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
\r
415 p.Form.Controls.Add (lcb);
\r
416 p.Form.Controls.Add (GridView1);
\r
417 p.Form.Controls.Add (lce);
\r
423 public myds_data (string s)
\r
428 public string Field1
\r
435 class ControlWithDataItem : Control
437 readonly object _data;
438 public ControlWithDataItem (object data) {
442 public object DataItem {
449 public class SimpleSpreadsheetRow : TableRow, IDataItemContainer
452 private int _itemIndex;
454 public SimpleSpreadsheetRow (int itemIndex, object o) {
456 _itemIndex = itemIndex;
459 public virtual object Data {
465 object IDataItemContainer.DataItem {
471 int IDataItemContainer.DataItemIndex {
477 int IDataItemContainer.DisplayIndex {