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 ()
133 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";
134 WebTest t = new WebTest ("BoundField_Bug646505.aspx");
137 FormRequest fr = new FormRequest (t.Response, "form1");
138 fr.Controls.Add ("__EVENTTARGET");
139 fr.Controls.Add ("__EVENTARGUMENT");
140 fr.Controls ["__EVENTTARGET"].Value = "gridView";
141 fr.Controls ["__EVENTARGUMENT"].Value = "Edit$0";
143 string pageHtml = t.Run ();
144 string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
146 HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
150 public void BoundField_DefaultProperty () {
151 PokerBoundField bf = new PokerBoundField ();
152 Assert.AreEqual ("!", PokerBoundField.ThisExpression, "StaticThisExpression");
153 Assert.AreEqual ("", bf.DataField, "DataField");
154 Assert.AreEqual ("", bf.DataFormatString, "DataFormatString");
155 Assert.AreEqual ("", bf.HeaderText, "HeaderText");
156 Assert.AreEqual (true, bf.HtmlEncode, "HtmlEncode");
157 Assert.AreEqual ("", bf.NullDisplayText, "NullDisplayText");
158 Assert.AreEqual (false, bf.ReadOnly, "ReadOnly");
161 Assert.AreEqual (true, bf.DoSupportsHtmlEncode, "SupportsHtmlEncode");
165 public void BoundField_DefaultPropertyNotWorking () {
166 PokerBoundField bf = new PokerBoundField ();
167 Assert.AreEqual (false, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
168 Assert.AreEqual (true, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
172 public void BoundField_AssignProperty () {
173 PokerBoundField bf = new PokerBoundField ();
174 bf.ConvertEmptyStringToNull = false;
175 Assert.AreEqual (false, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
176 bf.DataField = "test";
177 Assert.AreEqual ("test", bf.DataField, "DataField");
178 bf.DataFormatString = "test";
179 Assert.AreEqual ("test", bf.DataFormatString, "DataFormatString");
180 bf.HeaderText = "test";
181 Assert.AreEqual ("test", bf.HeaderText, "HeaderText");
182 bf.HtmlEncode = false;
183 Assert.AreEqual (false, bf.HtmlEncode, "HtmlEncode");
184 bf.NullDisplayText = "test";
185 Assert.AreEqual ("test", bf.NullDisplayText, "NullDisplayText");
187 Assert.AreEqual (true, bf.ReadOnly, "ReadOnly");
191 public void BoundField_AssignPropertyNotWorking () {
192 PokerBoundField bf = new PokerBoundField ();
193 bf.ApplyFormatInEditMode = true;
194 Assert.AreEqual (true, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
198 public void BoundField_ExtractValuesFromCell () {
199 PokerBoundField bf = new PokerBoundField ();
200 OrderedDictionary dictionary = new OrderedDictionary ();
201 DataControlFieldCell cell = new DataControlFieldCell (null);
203 bf.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
204 Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
205 Assert.AreEqual ("test", dictionary [0].ToString (), "ExtractValuesFromCellValue");
209 public void BoundField_Initialize () {
210 // This method initilize to private fields in a base class DataControlField
211 // Always return false
212 PokerBoundField bf = new PokerBoundField ();
213 Control control = new Control ();
215 bool res = bf.Initialize (true, control);
216 // Assert.AreEqual (false, res, "InitializeResult");
217 Assert.AreEqual ("test", bf.GetControl.ID, "InitializeControl");
221 public void BoundField_InitializeCell () {
222 PokerBoundField bf = new PokerBoundField ();
224 DataControlFieldCell cell = new DataControlFieldCell (null);
225 bf.HeaderText = "headertext";
227 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
228 Assert.AreEqual ("headertext", cell.Text, "InitializeCellHeaderText");
231 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
232 Assert.AreEqual (" ", cell.Text, "InitializeCellEmpty");
234 bf.HeaderText = "headertext";
235 // Header image url not empty
236 bf.HeaderImageUrl = "headerurl";
237 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
238 if (cell.Controls [0] is Image) {
239 Image image = (Image) cell.Controls [0];
240 Assert.AreEqual ("headerurl", image.ImageUrl, "InitializeCellHeaderImageUrl");
243 Assert.Fail ("Header Image dos not created");
247 bf.FooterText = "footertext";
248 bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
249 Assert.AreEqual ("footertext", cell.Text, "InitializeCellFooterText");
251 bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
252 Assert.AreEqual (" ", cell.Text, "InitializeCellFooterEmpty");
256 public void BoundField_ValidateSupportsCallback () {
257 //This method has been implemented as an empty method
261 public void BoundField_SortExpression () {
262 // Sorting enable = true , link button must be created
263 PokerBoundField bf = new PokerBoundField ();
265 DataControlFieldCell cell = new DataControlFieldCell (null);
267 bf.HeaderImageUrl = "";
268 bf.SortExpression = "a";
269 bf.HeaderText = "sortbutton";
270 bf.Initialize (true, new Control ()); // _base._sortingenable set to true
271 bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
272 if (cell.Controls [0] is Button) { // mono
273 Button lb = (Button) cell.Controls [0];
274 Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
275 Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
276 Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
279 else if (cell.Controls [0] is LinkButton) { // .net
280 LinkButton lb = (LinkButton) cell.Controls [0];
281 Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
282 Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
283 Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
287 Assert.Fail ("Sort button does not created");
293 public void BoundField_CopyProperties () {
294 PokerBoundField bf = new PokerBoundField ();
295 BoundField copy = new BoundField ();
296 // Look not working property
297 // bf.ApplyFormatInEditMode = true;
298 bf.ConvertEmptyStringToNull = true;
299 bf.DataField = "test";
300 bf.DataFormatString = "test";
301 bf.HtmlEncode = true;
302 bf.NullDisplayText = "test";
304 bf.DoCopyProperties (copy);
305 // Look not working property
306 // Assert.AreEqual (true, copy.ApplyFormatInEditMode, "ApplyFormatInEditMode");
307 Assert.AreEqual (true, copy.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
308 Assert.AreEqual ("test", copy.DataField, "DataField");
309 Assert.AreEqual ("test", copy.DataFormatString, "DataFormatString");
310 Assert.AreEqual (true, copy.HtmlEncode, "HtmlEncode");
311 Assert.AreEqual ("test", copy.NullDisplayText, "NullDisplayText");
312 Assert.AreEqual (true, copy.ReadOnly, "ReadOnly");
316 public void BoundField_CreateField () {
317 PokerBoundField bf = new PokerBoundField ();
318 BoundField newfield = (BoundField) bf.DoCreateField ();
319 Assert.IsNotNull (newfield, "CreateField");
323 public void BoundField_FormatDataValue () {
325 PokerBoundField bf = new PokerBoundField ();
327 bf.NullDisplayText = "NullDisplayText";
328 result = bf.DoFormatDataValue (null, false);
329 Assert.AreEqual ("NullDisplayText", result, "FormatDataValueNullDataValue");
331 result = bf.DoFormatDataValue ("test", true);
332 Assert.AreEqual ("test", result, "FormatDataValueTextDataValue");
334 result = bf.DoFormatDataValue ("", true);
335 Assert.AreEqual ("NullDisplayText", result, "FormatEmptyDataValue");
337 bf.DataFormatString = "-{0,8:G}-";
338 result = bf.DoFormatDataValue (10, false);
339 Assert.AreEqual ("- 10-", result, "FormatDataValueWithFormat");
341 bf.DataFormatString = "-{0:X}-";
342 result = bf.DoFormatDataValue (10, true);
343 Assert.AreEqual ("-A-", result, "FormatDataValueWithFormatAndHtmlEncode");
345 bf.DataFormatString = "-{0:X}-";
346 result = bf.DoFormatDataValue (10, false);
347 Assert.AreEqual ("-A-", result, "FormatDataValueWithFormatAndNoHtmlEncode");
349 bf.HtmlEncodeFormatString = false;
350 bf.DataFormatString = "-{0:X}-";
351 result = bf.DoFormatDataValue (10, true);
352 Assert.AreEqual ("-10-", result, "NoHtmlEncodeFormatString_HtmlEncode");
354 bf.DataFormatString = "-{0:X}-";
355 result = bf.DoFormatDataValue (10, false);
356 Assert.AreEqual ("-A-", result, "NoHtmlEncodeFormatString_NoHtmlEncode");
360 public void HtmlEncodeFormatString ()
362 string formatString = "<script>alert ('{0}');</script>";
363 var bf = new PokerBoundField ();
365 Assert.IsTrue (bf.HtmlEncodeFormatString, "#A1-2");
366 Assert.IsTrue (bf.HtmlEncode, "#A1-2");
367 Assert.IsTrue (bf.DoSupportsHtmlEncode, "#A1-3");
369 bf.DataFormatString = formatString;
370 Assert.AreEqual ("<script>alert ('<test>');</script>", bf.DoFormatDataValue ("<test>", true), "#A2");
371 Assert.AreEqual (String.Format (formatString, "<test>"), bf.DoFormatDataValue ("<test>", false), "#A3");
373 bf.HtmlEncodeFormatString = false;
374 Assert.AreEqual ("<script>alert ('<test>');</script>", bf.DoFormatDataValue ("<test>", true), "#A4");
376 var ec = new EncodingTest ();
377 bf.HtmlEncodeFormatString = true;
378 Assert.AreEqual ("<script>alert ('<EncodingTest>&');</script>", bf.DoFormatDataValue (ec, true), "#A4");
382 public void BoundField_GetDesignTimeValue () {
384 PokerBoundField bf = new PokerBoundField ();
385 result = (string) bf.DoGetDesignTimeValue ();
386 Assert.AreEqual ("Databound", result, "GetDesignTimeValue");
390 [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.")]
391 public void BoundField_GetValueNull () {
392 PokerBoundField bf = new PokerBoundField ();
393 SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, null);
394 bf.DataField = PokerBoundField.ThisExpression;
395 string result = (string) bf.DoGetValue (ds);
399 public void BoundField_GetValue () {
400 PokerBoundField bf = new PokerBoundField ();
401 SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, "test");
402 bf.DataField = PokerBoundField.ThisExpression;
403 string result = (string) bf.DoGetValue (ds);
404 Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
408 public void BoundField_GetValueDataItem () {
409 PokerBoundField bf = new PokerBoundField ();
410 ControlWithDataItem ds = new ControlWithDataItem ("test");
411 bf.DataField = PokerBoundField.ThisExpression;
412 string result = (string) bf.DoGetValue (ds);
413 Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
417 public void BoundField_InitializeDataCell () {
418 PokerBoundField bf = new PokerBoundField ();
419 bf.HeaderText = "headertest";
420 DataControlFieldCell cell = new DataControlFieldCell (null);
421 DataControlRowState state = DataControlRowState.Edit;
422 Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
423 bf.DoInitializeDataCell (cell, state);
424 Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
428 [ExpectedException (typeof (HttpException))]
429 public void BoundField_OnDataBindFieldExeption () {
430 PokerBoundField bf = new PokerBoundField ();
431 bf.bindbutoon.DataBind ();
436 [ExpectedException (typeof (HttpException))]
437 public void BoundField_GetValueExeption () {
438 PokerBoundField bf = new PokerBoundField ();
439 bf.DoGetValue (null);
443 [Category ("NunitWeb")]
444 public void BoundField_NullValueRender ()
446 string html = new WebTest (PageInvoker.CreateOnLoad (new PageDelegate (BasicRenderTestInit))).Run ();
447 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>";
448 html = HtmlDiff.GetControlFromPageHtml (html);
449 HtmlDiff.AssertAreEqual (orightml, html, "NullValueRender");
452 public static void BasicRenderTestInit (Page p)
454 ArrayList myds = new ArrayList ();
455 myds.Add (new myds_data ("Norway"));
456 myds.Add (new myds_data ("Sweden"));
457 myds.Add (new myds_data (""));
458 myds.Add (new myds_data ("Italy"));
460 BoundField bf = new BoundField ();
461 bf.DataField = "Field1";
462 bf.NullDisplayText = "EMPTY";
464 BoundField bf2 = new BoundField ();
465 bf2.DataField = "Field1";
467 GridView GridView1 = new GridView();
468 GridView1.AutoGenerateColumns = false;
469 GridView1.Columns.Add (bf);
470 GridView1.Columns.Add (bf2);
471 GridView1.DataSource = myds;
472 GridView1.DataBind ();
474 LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
475 LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
477 p.Form.Controls.Add (lcb);
478 p.Form.Controls.Add (GridView1);
479 p.Form.Controls.Add (lce);
485 public myds_data (string s)
497 class ControlWithDataItem : Control
499 readonly object _data;
500 public ControlWithDataItem (object data) {
504 public object DataItem {
511 public class SimpleSpreadsheetRow : TableRow, IDataItemContainer
514 private int _itemIndex;
516 public SimpleSpreadsheetRow (int itemIndex, object o) {
518 _itemIndex = itemIndex;
521 public virtual object Data {
527 object IDataItemContainer.DataItem {
533 int IDataItemContainer.DataItemIndex {
539 int IDataItemContainer.DisplayIndex {