BoundFieldTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. //
  2. // Tests for System.Web.UI.WebControls.BoundFieldTest.cs
  3. //
  4. // Author:
  5. // Yoni Klein ([email protected])
  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. #if NET_2_0
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.Web;
  33. using System.Web.UI;
  34. using System.Web.UI.WebControls;
  35. using System.IO;
  36. using System.Drawing;
  37. using System.Collections;
  38. using System.Collections.Specialized;
  39. using Image = System.Web.UI.WebControls.Image;
  40. using NUnit.Framework;
  41. using System.Globalization;
  42. using MonoTests.SystemWeb.Framework;
  43. using MonoTests.stand_alone.WebHarness;
  44. namespace MonoTests.System.Web.UI.WebControls
  45. {
  46. class EncodingTest
  47. {
  48. public override string ToString ()
  49. {
  50. return "<EncodingTest>&";
  51. }
  52. }
  53. class PokerBoundField : BoundField
  54. {
  55. public Button bindbutoon;
  56. public PokerBoundField () {
  57. TrackViewState ();
  58. bindbutoon = new Button ();
  59. bindbutoon.DataBinding += new EventHandler (OnDataBindField);
  60. }
  61. public StateBag StateBag {
  62. get { return base.ViewState; }
  63. }
  64. public bool DoSupportsHtmlEncode {
  65. get {
  66. return base.SupportsHtmlEncode;
  67. }
  68. }
  69. public void DoCopyProperties (DataControlField newField) {
  70. base.CopyProperties (newField);
  71. }
  72. public DataControlField DoCreateField () {
  73. return base.CreateField ();
  74. }
  75. public string DoFormatDataValue (object dataValue, bool encode) {
  76. return this.FormatDataValue (dataValue, encode);
  77. }
  78. public object DoGetDesignTimeValue () {
  79. return base.GetDesignTimeValue ();
  80. }
  81. public object DoGetValue (Control controlContainer) {
  82. return base.GetValue (controlContainer);
  83. }
  84. public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState) {
  85. base.InitializeDataCell (cell, rowState);
  86. }
  87. public void DoOnDataBindField (object sender, EventArgs e) {
  88. base.OnDataBindField (sender, e);
  89. }
  90. public Control GetControl {
  91. get { return base.Control; }
  92. }
  93. public object DoSaveViewState ()
  94. {
  95. return SaveViewState ();
  96. }
  97. }
  98. [Serializable]
  99. [TestFixture]
  100. public class BoundFieldTest
  101. {
  102. [Test]
  103. public void BoundField_DefaultProperty () {
  104. PokerBoundField bf = new PokerBoundField ();
  105. Assert.AreEqual ("!", PokerBoundField.ThisExpression, "StaticThisExpression");
  106. Assert.AreEqual ("", bf.DataField, "DataField");
  107. Assert.AreEqual ("", bf.DataFormatString, "DataFormatString");
  108. Assert.AreEqual ("", bf.HeaderText, "HeaderText");
  109. Assert.AreEqual (true, bf.HtmlEncode, "HtmlEncode");
  110. Assert.AreEqual ("", bf.NullDisplayText, "NullDisplayText");
  111. Assert.AreEqual (false, bf.ReadOnly, "ReadOnly");
  112. //Protected
  113. Assert.AreEqual (true, bf.DoSupportsHtmlEncode, "SupportsHtmlEncode");
  114. }
  115. [Test]
  116. public void BoundField_DefaultPropertyNotWorking () {
  117. PokerBoundField bf = new PokerBoundField ();
  118. Assert.AreEqual (false, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
  119. Assert.AreEqual (true, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
  120. }
  121. [Test]
  122. public void BoundField_AssignProperty () {
  123. PokerBoundField bf = new PokerBoundField ();
  124. bf.ConvertEmptyStringToNull = false;
  125. Assert.AreEqual (false, bf.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
  126. bf.DataField = "test";
  127. Assert.AreEqual ("test", bf.DataField, "DataField");
  128. bf.DataFormatString = "test";
  129. Assert.AreEqual ("test", bf.DataFormatString, "DataFormatString");
  130. bf.HeaderText = "test";
  131. Assert.AreEqual ("test", bf.HeaderText, "HeaderText");
  132. bf.HtmlEncode = false;
  133. Assert.AreEqual (false, bf.HtmlEncode, "HtmlEncode");
  134. bf.NullDisplayText = "test";
  135. Assert.AreEqual ("test", bf.NullDisplayText, "NullDisplayText");
  136. bf.ReadOnly = true;
  137. Assert.AreEqual (true, bf.ReadOnly, "ReadOnly");
  138. }
  139. [Test]
  140. public void BoundField_AssignPropertyNotWorking () {
  141. PokerBoundField bf = new PokerBoundField ();
  142. bf.ApplyFormatInEditMode = true;
  143. Assert.AreEqual (true, bf.ApplyFormatInEditMode, "ApplyFormatInEditMode");
  144. }
  145. [Test]
  146. public void BoundField_ExtractValuesFromCell () {
  147. PokerBoundField bf = new PokerBoundField ();
  148. OrderedDictionary dictionary = new OrderedDictionary ();
  149. DataControlFieldCell cell = new DataControlFieldCell (null);
  150. cell.Text = "test";
  151. bf.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
  152. Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount");
  153. Assert.AreEqual ("test", dictionary [0].ToString (), "ExtractValuesFromCellValue");
  154. }
  155. [Test]
  156. public void BoundField_Initialize () {
  157. // This method initilize to private fields in a base class DataControlField
  158. // Always return false
  159. PokerBoundField bf = new PokerBoundField ();
  160. Control control = new Control ();
  161. control.ID = "test";
  162. bool res = bf.Initialize (true, control);
  163. // Assert.AreEqual (false, res, "InitializeResult");
  164. Assert.AreEqual ("test", bf.GetControl.ID, "InitializeControl");
  165. }
  166. [Test]
  167. public void BoundField_InitializeCell () {
  168. PokerBoundField bf = new PokerBoundField ();
  169. // Header text
  170. DataControlFieldCell cell = new DataControlFieldCell (null);
  171. bf.HeaderText = "headertext";
  172. bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
  173. Assert.AreEqual ("headertext", cell.Text, "InitializeCellHeaderText");
  174. // Empty header text
  175. bf.HeaderText = "";
  176. bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
  177. Assert.AreEqual ("&nbsp;", cell.Text, "InitializeCellEmpty");
  178. bf.HeaderText = "headertext";
  179. // Header image url not empty
  180. bf.HeaderImageUrl = "headerurl";
  181. bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
  182. if (cell.Controls [0] is Image) {
  183. Image image = (Image) cell.Controls [0];
  184. Assert.AreEqual ("headerurl", image.ImageUrl, "InitializeCellHeaderImageUrl");
  185. }
  186. else {
  187. Assert.Fail ("Header Image dos not created");
  188. }
  189. // Footer empty
  190. bf.FooterText = "footertext";
  191. bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
  192. Assert.AreEqual ("footertext", cell.Text, "InitializeCellFooterText");
  193. bf.FooterText = "";
  194. bf.InitializeCell (cell, DataControlCellType.Footer, DataControlRowState.Edit, 1);
  195. Assert.AreEqual ("&nbsp;", cell.Text, "InitializeCellFooterEmpty");
  196. }
  197. [Test]
  198. public void BoundField_ValidateSupportsCallback () {
  199. //This method has been implemented as an empty method
  200. }
  201. [Test]
  202. public void BoundField_SortExpression () {
  203. // Sorting enable = true , link button must be created
  204. PokerBoundField bf = new PokerBoundField ();
  205. // Header text
  206. DataControlFieldCell cell = new DataControlFieldCell (null);
  207. bf.HeaderImageUrl = "";
  208. bf.SortExpression = "a";
  209. bf.HeaderText = "sortbutton";
  210. bf.Initialize (true, new Control ()); // _base._sortingenable set to true
  211. bf.InitializeCell (cell, DataControlCellType.Header, DataControlRowState.Edit, 1);
  212. if (cell.Controls [0] is Button) { // mono
  213. Button lb = (Button) cell.Controls [0];
  214. Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
  215. Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
  216. Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
  217. }
  218. else if (cell.Controls [0] is LinkButton) { // .net
  219. LinkButton lb = (LinkButton) cell.Controls [0];
  220. Assert.AreEqual ("Sort", lb.CommandName, "InitializeCellHeaderSortButtonCommand");
  221. Assert.AreEqual ("a", lb.CommandArgument, "InitializeCellHeaderSortButtonArgument");
  222. Assert.AreEqual ("sortbutton", lb.Text, "InitializeCellHeaderSortButtonText");
  223. }
  224. else {
  225. Assert.Fail ("Sort button does not created");
  226. }
  227. }
  228. [Test]
  229. public void BoundField_CopyProperties () {
  230. PokerBoundField bf = new PokerBoundField ();
  231. BoundField copy = new BoundField ();
  232. // Look not working property
  233. // bf.ApplyFormatInEditMode = true;
  234. bf.ConvertEmptyStringToNull = true;
  235. bf.DataField = "test";
  236. bf.DataFormatString = "test";
  237. bf.HtmlEncode = true;
  238. bf.NullDisplayText = "test";
  239. bf.ReadOnly = true;
  240. bf.DoCopyProperties (copy);
  241. // Look not working property
  242. // Assert.AreEqual (true, copy.ApplyFormatInEditMode, "ApplyFormatInEditMode");
  243. Assert.AreEqual (true, copy.ConvertEmptyStringToNull, "ConvertEmptyStringToNull");
  244. Assert.AreEqual ("test", copy.DataField, "DataField");
  245. Assert.AreEqual ("test", copy.DataFormatString, "DataFormatString");
  246. Assert.AreEqual (true, copy.HtmlEncode, "HtmlEncode");
  247. Assert.AreEqual ("test", copy.NullDisplayText, "NullDisplayText");
  248. Assert.AreEqual (true, copy.ReadOnly, "ReadOnly");
  249. }
  250. [Test]
  251. public void BoundField_CreateField () {
  252. PokerBoundField bf = new PokerBoundField ();
  253. BoundField newfield = (BoundField) bf.DoCreateField ();
  254. Assert.IsNotNull (newfield, "CreateField");
  255. }
  256. [Test]
  257. public void BoundField_FormatDataValue () {
  258. string result;
  259. PokerBoundField bf = new PokerBoundField ();
  260. bf.NullDisplayText = "NullDisplayText";
  261. result = bf.DoFormatDataValue (null, false);
  262. Assert.AreEqual ("NullDisplayText", result, "FormatDataValueNullDataValue");
  263. result = bf.DoFormatDataValue ("test", true);
  264. Assert.AreEqual ("test", result, "FormatDataValueTextDataValue");
  265. result = bf.DoFormatDataValue ("", true);
  266. Assert.AreEqual ("NullDisplayText", result, "FormatEmptyDataValue");
  267. bf.DataFormatString = "-{0,8:G}-";
  268. result = bf.DoFormatDataValue (10, false);
  269. Assert.AreEqual ("- 10-", result, "FormatDataValueWithFormat");
  270. bf.DataFormatString = "-{0:X}-";
  271. result = bf.DoFormatDataValue (10, true);
  272. Assert.AreEqual ("-A-", result, "FormatDataValueWithFormatAndHtmlEncode");
  273. bf.DataFormatString = "-{0:X}-";
  274. result = bf.DoFormatDataValue (10, false);
  275. Assert.AreEqual ("-A-", result, "FormatDataValueWithFormatAndNoHtmlEncode");
  276. bf.HtmlEncodeFormatString = false;
  277. bf.DataFormatString = "-{0:X}-";
  278. result = bf.DoFormatDataValue (10, true);
  279. Assert.AreEqual ("-10-", result, "NoHtmlEncodeFormatString_HtmlEncode");
  280. bf.DataFormatString = "-{0:X}-";
  281. result = bf.DoFormatDataValue (10, false);
  282. Assert.AreEqual ("-A-", result, "NoHtmlEncodeFormatString_NoHtmlEncode");
  283. }
  284. [Test]
  285. public void HtmlEncodeFormatString ()
  286. {
  287. string formatString = "<script>alert ('{0}');</script>";
  288. var bf = new PokerBoundField ();
  289. Assert.IsTrue (bf.HtmlEncodeFormatString, "#A1-2");
  290. Assert.IsTrue (bf.HtmlEncode, "#A1-2");
  291. Assert.IsTrue (bf.DoSupportsHtmlEncode, "#A1-3");
  292. bf.DataFormatString = formatString;
  293. #if NET_4_0
  294. Assert.AreEqual ("&lt;script&gt;alert (&#39;&lt;test&gt;&#39;);&lt;/script&gt;", bf.DoFormatDataValue ("<test>", true), "#A2");
  295. #else
  296. Assert.AreEqual ("&lt;script&gt;alert ('&lt;test&gt;');&lt;/script&gt;", bf.DoFormatDataValue ("<test>", true), "#A2");
  297. #endif
  298. Assert.AreEqual (String.Format (formatString, "<test>"), bf.DoFormatDataValue ("<test>", false), "#A3");
  299. bf.HtmlEncodeFormatString = false;
  300. Assert.AreEqual ("<script>alert ('&lt;test&gt;');</script>", bf.DoFormatDataValue ("<test>", true), "#A4");
  301. var ec = new EncodingTest ();
  302. bf.HtmlEncodeFormatString = true;
  303. #if NET_4_0
  304. Assert.AreEqual ("&lt;script&gt;alert (&#39;&lt;EncodingTest&gt;&amp;&#39;);&lt;/script&gt;", bf.DoFormatDataValue (ec, true), "#A4");
  305. #else
  306. Assert.AreEqual ("&lt;script&gt;alert ('&lt;EncodingTest&gt;&amp;');&lt;/script&gt;", bf.DoFormatDataValue (ec, true), "#A4");
  307. #endif
  308. }
  309. [Test]
  310. public void BoundField_GetDesignTimeValue () {
  311. string result;
  312. PokerBoundField bf = new PokerBoundField ();
  313. result = (string) bf.DoGetDesignTimeValue ();
  314. Assert.AreEqual ("Databound", result, "GetDesignTimeValue");
  315. }
  316. [Test]
  317. [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.")]
  318. public void BoundField_GetValueNull () {
  319. PokerBoundField bf = new PokerBoundField ();
  320. SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, null);
  321. bf.DataField = PokerBoundField.ThisExpression;
  322. string result = (string) bf.DoGetValue (ds);
  323. }
  324. [Test]
  325. public void BoundField_GetValue () {
  326. PokerBoundField bf = new PokerBoundField ();
  327. SimpleSpreadsheetRow ds = new SimpleSpreadsheetRow (0, "test");
  328. bf.DataField = PokerBoundField.ThisExpression;
  329. string result = (string) bf.DoGetValue (ds);
  330. Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
  331. }
  332. [Test]
  333. public void BoundField_GetValueDataItem () {
  334. PokerBoundField bf = new PokerBoundField ();
  335. ControlWithDataItem ds = new ControlWithDataItem ("test");
  336. bf.DataField = PokerBoundField.ThisExpression;
  337. string result = (string) bf.DoGetValue (ds);
  338. Assert.AreEqual ("test", result, "GetValueFromIDataItemContainer");
  339. }
  340. [Test]
  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");
  349. }
  350. [Test]
  351. [ExpectedException (typeof (HttpException))]
  352. public void BoundField_OnDataBindFieldExeption () {
  353. PokerBoundField bf = new PokerBoundField ();
  354. bf.bindbutoon.DataBind ();
  355. }
  356. [Test]
  357. [ExpectedException (typeof (HttpException))]
  358. public void BoundField_GetValueExeption () {
  359. PokerBoundField bf = new PokerBoundField ();
  360. bf.DoGetValue (null);
  361. }
  362. [Test]
  363. [Category ("NunitWeb")]
  364. public void BoundField_NullValueRender ()
  365. {
  366. string html = new WebTest (PageInvoker.CreateOnLoad (new PageDelegate (BasicRenderTestInit))).Run ();
  367. 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\">&nbsp;</th><th scope=\"col\">&nbsp;</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>&nbsp;</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>";
  368. html = HtmlDiff.GetControlFromPageHtml (html);
  369. HtmlDiff.AssertAreEqual (orightml, html, "NullValueRender");
  370. }
  371. public static void BasicRenderTestInit (Page p)
  372. {
  373. ArrayList myds = new ArrayList ();
  374. myds.Add (new myds_data ("Norway"));
  375. myds.Add (new myds_data ("Sweden"));
  376. myds.Add (new myds_data (""));
  377. myds.Add (new myds_data ("Italy"));
  378. BoundField bf = new BoundField ();
  379. bf.DataField = "Field1";
  380. bf.NullDisplayText = "EMPTY";
  381. BoundField bf2 = new BoundField ();
  382. bf2.DataField = "Field1";
  383. GridView GridView1 = new GridView();
  384. GridView1.AutoGenerateColumns = false;
  385. GridView1.Columns.Add (bf);
  386. GridView1.Columns.Add (bf2);
  387. GridView1.DataSource = myds;
  388. GridView1.DataBind ();
  389. LiteralControl lcb = new LiteralControl (HtmlDiff.BEGIN_TAG);
  390. LiteralControl lce = new LiteralControl (HtmlDiff.END_TAG);
  391. p.Form.Controls.Add (lcb);
  392. p.Form.Controls.Add (GridView1);
  393. p.Form.Controls.Add (lce);
  394. }
  395. class myds_data
  396. {
  397. string _s = "";
  398. public myds_data (string s)
  399. {
  400. _s = s;
  401. }
  402. public string Field1
  403. {
  404. get { return _s; }
  405. }
  406. }
  407. class ControlWithDataItem : Control
  408. {
  409. readonly object _data;
  410. public ControlWithDataItem (object data) {
  411. _data = data;
  412. }
  413. public object DataItem {
  414. get {
  415. return _data;
  416. }
  417. }
  418. }
  419. public class SimpleSpreadsheetRow : TableRow, IDataItemContainer
  420. {
  421. private object data;
  422. private int _itemIndex;
  423. public SimpleSpreadsheetRow (int itemIndex, object o) {
  424. data = o;
  425. _itemIndex = itemIndex;
  426. }
  427. public virtual object Data {
  428. get {
  429. return data;
  430. }
  431. }
  432. object IDataItemContainer.DataItem {
  433. get {
  434. return Data;
  435. }
  436. }
  437. int IDataItemContainer.DataItemIndex {
  438. get {
  439. return _itemIndex;
  440. }
  441. }
  442. int IDataItemContainer.DisplayIndex {
  443. get {
  444. return _itemIndex;
  445. }
  446. }
  447. }
  448. }
  449. }
  450. #endif