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.
32 using System.Collections.Generic;
36 using System.Web.UI.WebControls;
39 using System.Collections;
40 using System.Collections.Specialized;
41 using Image = System.Web.UI.WebControls.Image;
42 using NUnit.Framework;
43 using System.Globalization;
44 using MonoTests.SystemWeb.Framework;
45 using MonoTests.stand_alone.WebHarness;
49 namespace MonoTests.System.Web.UI.WebControls
53 public override string ToString ()
55 return "<EncodingTest>&";
59 class PokerBoundField : BoundField
61 public Button bindbutoon;
63 public PokerBoundField () {
65 bindbutoon = new Button ();
66 bindbutoon.DataBinding += new EventHandler (OnDataBindField);
70 public StateBag StateBag {
71 get { return base.ViewState; }
74 public bool DoSupportsHtmlEncode {
76 return base.SupportsHtmlEncode;
80 public void DoCopyProperties (DataControlField newField) {
81 base.CopyProperties (newField);
84 public DataControlField DoCreateField () {
85 return base.CreateField ();
88 public string DoFormatDataValue (object dataValue, bool encode) {
89 return this.FormatDataValue (dataValue, encode);
92 public object DoGetDesignTimeValue () {
93 return base.GetDesignTimeValue ();
96 public object DoGetValue (Control controlContainer) {
97 return base.GetValue (controlContainer);
100 public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState) {
101 base.InitializeDataCell (cell, rowState);
104 public void DoOnDataBindField (object sender, EventArgs e) {
105 base.OnDataBindField (sender, e);
108 public Control GetControl {
109 get { return base.Control; }
112 public object DoSaveViewState ()
114 return SaveViewState ();
121 public class BoundFieldTest
126 WebTest.CopyResource (GetType (), "BoundField_Bug646505.aspx", "BoundField_Bug646505.aspx");
127 WebTest.CopyResource (GetType (), "BoundField_Bug646505.aspx.cs", "BoundField_Bug646505.aspx.cs");
130 [Test (Description="Bug 646505")]
131 public void BoundField_Bug646505 ()
134 string originalHtml = "<div>\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"gridView\" style=\"border-collapse:collapse;\">\n\t\t<tr>\n\t\t\t<th scope=\"col\"> </th><th scope=\"col\"> </th>\n\t\t</tr><tr>\n\t\t\t<td><a href=\"javascript:__doPostBack('gridView$ctl02$ctl00','')\">Update</a> <a href=\"javascript:__doPostBack('gridView','Cancel$0')\">Cancel</a></td><td><input name=\"gridView$ctl02$ctl02\" type=\"text\" value=\"False\" /></td>\n\t\t</tr><tr>\n\t\t\t<td><a href=\"javascript:__doPostBack('gridView','Edit$1')\">Edit</a></td><td>False</td>\n\t\t</tr>\n\t</table>\n</div>\n";
136 string originalHtml = "<div>\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" id=\"gridView\" style=\"border-collapse:collapse;\">\n\t\t<tr>\n\t\t\t<th scope=\"col\"> </th><th scope=\"col\"> </th>\n\t\t</tr><tr>\n\t\t\t<td><a href=\"javascript:__doPostBack('gridView$ctl02$ctl00','')\">Update</a> <a href=\"javascript:__doPostBack('gridView','Cancel$0')\">Cancel</a></td><td><input name=\"gridView$ctl02$ctl02\" type=\"text\" value=\"False\" /></td>\n\t\t</tr><tr>\n\t\t\t<td><a href=\"javascript:__doPostBack('gridView','Edit$1')\">Edit</a></td><td>False</td>\n\t\t</tr>\n\t</table>\n</div>\n";
138 WebTest t = new WebTest ("BoundField_Bug646505.aspx");
141 FormRequest fr = new FormRequest (t.Response, "form1");
142 fr.Controls.Add ("__EVENTTARGET");
143 fr.Controls.Add ("__EVENTARGUMENT");
144 fr.Controls ["__EVENTTARGET"].Value = "gridView";
145 fr.Controls ["__EVENTARGUMENT"].Value = "Edit$0";
147 string pageHtml = t.Run ();
148 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
150 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
154 public void BoundField_DefaultProperty () {
155 PokerBoundField bf = new PokerBoundField ();
156 Assert.AreEqual ("!", PokerBoundField.ThisExpression, "StaticThisExpression");
157 Assert.AreEqual ("", bf.DataField, "DataField");
158 Assert.AreEqual ("", bf.DataFormatString, "DataFormatString");
159 Assert.AreEqual ("", bf.HeaderText, "HeaderText");
160 Assert.AreEqual (true, bf.HtmlEncode, "HtmlEncode");
161 Assert.AreEqual ("", bf.NullDisplayText, "NullDisplayText");
162 Assert.AreEqual (false, bf.ReadOnly, "ReadOnly");
165 Assert.AreEqual (true, bf.DoSupportsHtmlEncode, "SupportsHtmlEncode");
169 public void BoundField_DefaultPropertyNotWorking () {
170 PokerBoundField bf = new PokerBoundField ();
171 Assert.AreEqual (false, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
172 Assert.AreEqual (true, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
176 public void BoundField_AssignProperty () {
177 PokerBoundField bf = new PokerBoundField ();
178 bf.ConvertEmptyStringToNull = false;
179 Assert.AreEqual (false, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
180 bf.DataField = "test";
181 Assert.AreEqual ("test", bf.DataField, "DataField");
182 bf.DataFormatString = "test";
183 Assert.AreEqual ("test", bf.DataFormatString, "DataFormatString");
184 bf.HeaderText = "test";
185 Assert.AreEqual ("test", bf.HeaderText, "HeaderText");
186 bf.HtmlEncode = false;
187 Assert.AreEqual (false, bf.HtmlEncode, "HtmlEncode");
188 bf.NullDisplayText = "test";
189 Assert.AreEqual ("test", bf.NullDisplayText, "NullDisplayText");
191 Assert.AreEqual (true, bf.ReadOnly, "ReadOnly");
195 public void BoundField_AssignPropertyNotWorking () {
196 PokerBoundField bf = new PokerBoundField ();
197 bf.ApplyFormatInEditMode = true;
198 Assert.AreEqual (true, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
202 public void BoundField_ExtractValuesFromCell () {
203 PokerBoundField bf = new PokerBoundField ();
204 OrderedDictionary dictionary = new OrderedDictionary ();
205 DataControlFieldCell cell = new DataControlFieldCell (null);
207 bf.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
208 Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
209 Assert.AreEqual ("test", dictionary [0].ToString (), "ExtractValuesFromCellValue");
213 public void BoundField_Initialize () {
214 // This method initilize to private fields in a base class DataControlField
215 // Always return false
216 PokerBoundField bf = new PokerBoundField ();
217 Control control = new Control ();
219 bool res = bf.Initialize (true, control);
220 // Assert.AreEqual (false, res, "InitializeResult");
221 Assert.AreEqual ("test", bf.GetControl.ID, "InitializeControl");
225 public void BoundField_InitializeCell () {
226 PokerBoundField bf = new PokerBoundField ();
228 DataControlFieldCell cell = new DataControlFieldCell (null);
229 bf.HeaderText = "headertext";
231 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
232 Assert.AreEqual ("headertext", cell.Text, "InitializeCellHeaderText");
235 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
236 Assert.AreEqual (" ", cell.Text, "InitializeCellEmpty");
238 bf.HeaderText = "headertext";
239 // Header image url not empty
240 bf.HeaderImageUrl = "headerurl";
241 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
242 if (cell.Controls [0] is Image) {
243 Image image = (Image) cell.Controls [0];
244 Assert.AreEqual ("headerurl", image.ImageUrl, "InitializeCellHeaderImageUrl");
247 Assert.Fail ("Header Image dos not created");
251 bf.FooterText = "footertext";
252 bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
253 Assert.AreEqual ("footertext", cell.Text, "InitializeCellFooterText");
255 bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
256 Assert.AreEqual (" ", cell.Text, "InitializeCellFooterEmpty");
260 public void BoundField_ValidateSupportsCallback () {
261 //This method has been implemented as an empty method
265 public void BoundField_SortExpression () {
266 // Sorting enable = true , link button must be created
267 PokerBoundField bf = new PokerBoundField ();
269 DataControlFieldCell cell = new DataControlFieldCell (null);
271 bf.HeaderImageUrl = "";
272 bf.SortExpression = "a";
273 bf.HeaderText = "sortbutton";
274 bf.Initialize (true, new Control ()); // _base._sortingenable set to true
275 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
276 if (cell.Controls [0] is Button) { // mono
277 Button lb = (Button) cell.Controls [0];
278 Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
279 Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
280 Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
283 else if (cell.Controls [0] is LinkButton) { // .net
284 LinkButton lb = (LinkButton) cell.Controls [0];
285 Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
286 Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
287 Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
291 Assert.Fail ("Sort button does not created");
297 public void BoundField_CopyProperties () {
298 PokerBoundField bf = new PokerBoundField ();
299 BoundField copy = new BoundField ();
300 // Look not working property
301 // bf.ApplyFormatInEditMode = true;
302 bf.ConvertEmptyStringToNull = true;
303 bf.DataField = "test";
304 bf.DataFormatString = "test";
305 bf.HtmlEncode = true;
306 bf.NullDisplayText = "test";
308 bf.DoCopyProperties (copy);
309 // Look not working property
310 // Assert.AreEqual (true, copy.ApplyFormatInEditMode, "ApplyFormatInEditMode");
311 Assert.AreEqual (true, copy.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
312 Assert.AreEqual ("test", copy.DataField, "DataField");
313 Assert.AreEqual ("test", copy.DataFormatString, "DataFormatString");
314 Assert.AreEqual (true, copy.HtmlEncode, "HtmlEncode");
315 Assert.AreEqual ("test", copy.NullDisplayText, "NullDisplayText");
316 Assert.AreEqual (true, copy.ReadOnly, "ReadOnly");
320 public void BoundField_CreateField () {
321 PokerBoundField bf = new PokerBoundField ();
322 BoundField newfield = (BoundField) bf.DoCreateField ();
323 Assert.IsNotNull (newfield, "CreateField");
327 public void BoundField_FormatDataValue () {
329 PokerBoundField bf = new PokerBoundField ();
331 bf.NullDisplayText = "NullDisplayText";
332 result = bf.DoFormatDataValue (null, false);
333 Assert.AreEqual ("NullDisplayText", result, "FormatDataValueNullDataValue");
335 result = bf.DoFormatDataValue ("test", true);
336 Assert.AreEqual ("test", result, "FormatDataValueTextDataValue");
338 result = bf.DoFormatDataValue ("", true);
339 Assert.AreEqual ("NullDisplayText", result, "FormatEmptyDataValue");
341 bf.DataFormatString = "-{0,8:G}-";
342 result = bf.DoFormatDataValue (10, false);
343 Assert.AreEqual ("- 10-", result, "FormatDataValueWithFormat");
345 bf.DataFormatString = "-{0:X}-";
346 result = bf.DoFormatDataValue (10, true);
347 Assert.AreEqual ("-A-", result, "FormatDataValueWithFormatAndHtmlEncode");
349 bf.DataFormatString = "-{0:X}-";
350 result = bf.DoFormatDataValue (10, false);
351 Assert.AreEqual ("-A-", result, "FormatDataValueWithFormatAndNoHtmlEncode");
353 bf.HtmlEncodeFormatString = false;
354 bf.DataFormatString = "-{0:X}-";
355 result = bf.DoFormatDataValue (10, true);
356 Assert.AreEqual ("-10-", result, "NoHtmlEncodeFormatString_HtmlEncode");
358 bf.DataFormatString = "-{0:X}-";
359 result = bf.DoFormatDataValue (10, false);
360 Assert.AreEqual ("-A-", result, "NoHtmlEncodeFormatString_NoHtmlEncode");
364 public void HtmlEncodeFormatString ()
366 string formatString = "<script>alert ('{0}');</script>";
367 var bf = new PokerBoundField ();
369 Assert.IsTrue (bf.HtmlEncodeFormatString, "#A1-2");
370 Assert.IsTrue (bf.HtmlEncode, "#A1-2");
371 Assert.IsTrue (bf.DoSupportsHtmlEncode, "#A1-3");
373 bf.DataFormatString = formatString;
375 Assert.AreEqual ("<script>alert ('<test>');</script>", bf.DoFormatDataValue ("<test>", true), "#A2");
377 Assert.AreEqual ("<script>alert ('<test>');</script>", bf.DoFormatDataValue ("<test>", true), "#A2");
379 Assert.AreEqual (String.Format (formatString, "<test>"), bf.DoFormatDataValue ("<test>", false), "#A3");
381 bf.HtmlEncodeFormatString = false;
382 Assert.AreEqual ("<script>alert ('<test>');</script>", bf.DoFormatDataValue ("<test>", true), "#A4");
384 var ec = new EncodingTest ();
385 bf.HtmlEncodeFormatString = true;
387 Assert.AreEqual ("<script>alert ('<EncodingTest>&');</script>", bf.DoFormatDataValue (ec, true), "#A4");
389 Assert.AreEqual ("<script>alert ('<EncodingTest>&');</script>", bf.DoFormatDataValue (ec, true), "#A4");
394 public void BoundField_GetDesignTimeValue () {
396 PokerBoundField bf = new PokerBoundField ();
397 result = (string) bf.DoGetDesignTimeValue ();
398 Assert.AreEqual ("Databound", result, "GetDesignTimeValue");
402 [ExpectedException(typeof(HttpException), ExpectedMessage="A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem.")]
403 public void BoundField_GetValueNull () {
404 PokerBoundField bf = new PokerBoundField ();
405 SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, null);
406 bf.DataField = PokerBoundField.ThisExpression;
407 string result = (string) bf.DoGetValue (ds);
411 public void BoundField_GetValue () {
412 PokerBoundField bf = new PokerBoundField ();
413 SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, "test");
414 bf.DataField = PokerBoundField.ThisExpression;
415 string result = (string) bf.DoGetValue (ds);
416 Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
420 public void BoundField_GetValueDataItem () {
421 PokerBoundField bf = new PokerBoundField ();
422 ControlWithDataItem ds = new ControlWithDataItem ("test");
423 bf.DataField = PokerBoundField.ThisExpression;
424 string result = (string) bf.DoGetValue (ds);
425 Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
429 public void BoundField_InitializeDataCell () {
430 PokerBoundField bf = new PokerBoundField ();
431 bf.HeaderText = "headertest";
432 DataControlFieldCell cell = new DataControlFieldCell (null);
433 DataControlRowState state = DataControlRowState.Edit;
434 Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
435 bf.DoInitializeDataCell (cell, state);
436 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
440 [ExpectedException (typeof (HttpException))]
441 public void BoundField_OnDataBindFieldExeption () {
442 PokerBoundField bf = new PokerBoundField ();
443 bf.bindbutoon.DataBind ();
448 [ExpectedException (typeof (HttpException))]
449 public void BoundField_GetValueExeption () {
450 PokerBoundField bf = new PokerBoundField ();
451 bf.DoGetValue (null);
455 [Category ("NunitWeb")]
456 public void BoundField_NullValueRender ()
458 string html = new WebTest (PageInvoker.CreateOnLoad (new PageDelegate (BasicRenderTestInit))).Run ();
459 string orightml = "<div>\r\n\t<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">\r\n\t\t<tr>\r\n\t\t\t<th scope=\"col\"> </th><th scope=\"col\"> </th>\r\n\t\t</tr><tr>\r\n\t\t\t<td>Norway</td><td>Norway</td>\r\n\t\t</tr><tr>\r\n\t\t\t<td>Sweden</td><td>Sweden</td>\r\n\t\t</tr><tr>\r\n\t\t\t<td>EMPTY</td><td> </td>\r\n\t\t</tr><tr>\r\n\t\t\t<td>Italy</td><td>Italy</td>\r\n\t\t</tr>\r\n\t</table>\r\n</div>";
460 html = HtmlDiff.GetControlFromPageHtml (html);
461 HtmlDiff.AssertAreEqual (orightml, html, "NullValueRender");
464 public static void BasicRenderTestInit (Page p)
466 ArrayList myds = new ArrayList ();
467 myds.Add (new myds_data ("Norway"));
468 myds.Add (new myds_data ("Sweden"));
469 myds.Add (new myds_data (""));
470 myds.Add (new myds_data ("Italy"));
472 BoundField bf = new BoundField ();
473 bf.DataField = "Field1";
474 bf.NullDisplayText = "EMPTY";
476 BoundField bf2 = new BoundField ();
477 bf2.DataField = "Field1";
479 GridView GridView1 = new GridView();
480 GridView1.AutoGenerateColumns = false;
481 GridView1.Columns.Add (bf);
482 GridView1.Columns.Add (bf2);
483 GridView1.DataSource = myds;
484 GridView1.DataBind ();
486 LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
487 LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
489 p.Form.Controls.Add (lcb);
490 p.Form.Controls.Add (GridView1);
491 p.Form.Controls.Add (lce);
497 public myds_data (string s)
509 class ControlWithDataItem : Control
511 readonly object _data;
512 public ControlWithDataItem (object data) {
516 public object DataItem {
523 public class SimpleSpreadsheetRow : TableRow, IDataItemContainer
526 private int _itemIndex;
528 public SimpleSpreadsheetRow (int itemIndex, object o) {
530 _itemIndex = itemIndex;
533 public virtual object Data {
539 object IDataItemContainer.DataItem {
545 int IDataItemContainer.DataItemIndex {
551 int IDataItemContainer.DisplayIndex {