BoundFieldTest.cs 15 KB

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