CheckBoxFieldTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. //
  2. // Tests for System.Web.UI.WebControls.CheckBoxFieldTest.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 NUnit.Framework;
  40. using System.Data;
  41. using MonoTests.SystemWeb.Framework;
  42. using MonoTests.stand_alone.WebHarness;
  43. namespace MonoTests.System.Web.UI.WebControls
  44. {
  45. class PokerCheckBoxField : CheckBoxField
  46. {
  47. // View state Stuff
  48. public PokerCheckBoxField ()
  49. : base ()
  50. {
  51. TrackViewState ();
  52. }
  53. public object SaveState ()
  54. {
  55. return SaveViewState ();
  56. }
  57. public void LoadState (object o)
  58. {
  59. LoadViewState (o);
  60. }
  61. public StateBag StateBag
  62. {
  63. get { return base.ViewState; }
  64. }
  65. public bool GetSupportsHtmlEncode
  66. {
  67. get
  68. {
  69. return base.SupportsHtmlEncode;
  70. }
  71. }
  72. public void DoCopyProperties (DataControlField newField)
  73. {
  74. base.CopyProperties (newField);
  75. }
  76. public DataControlField DoCreateField ()
  77. {
  78. return base.CreateField ();
  79. }
  80. public object DoGetDesignTimeValue ()
  81. {
  82. return base.GetDesignTimeValue ();
  83. }
  84. public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  85. {
  86. this.InitializeDataCell (cell, rowState);
  87. }
  88. protected override void OnDataBindField (object sender, EventArgs e)
  89. {
  90. base.OnDataBindField (sender, e);
  91. CheckBoxFieldTest.databound += 1;
  92. }
  93. }
  94. [TestFixture]
  95. public class CheckBoxFieldTest
  96. {
  97. public const string FIELDNAME = "checkbox";
  98. public const string WRONGFIELD = "str";
  99. public static int databound;
  100. [TestFixtureSetUp]
  101. public void SetUp ()
  102. {
  103. WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_0.aspx", "CheckBoxField_Bug595568_0.aspx");
  104. WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_1.aspx", "CheckBoxField_Bug595568_1.aspx");
  105. WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_2.aspx", "CheckBoxField_Bug595568_2.aspx");
  106. WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_5.aspx", "CheckBoxField_Bug595568_5.aspx");
  107. WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_6.aspx", "CheckBoxField_Bug595568_6.aspx");
  108. WebTest.CopyResource (GetType (), "CheckBoxField_Bug595568_7.aspx", "CheckBoxField_Bug595568_7.aspx");
  109. }
  110. [Test (Description="Bug 595568 #0")]
  111. public void CheckBoxField_Bug595568_0 ()
  112. {
  113. string originalHtml = @"<div>
  114. <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
  115. <tr>
  116. <th scope=""col"">&nbsp;</th>
  117. </tr><tr>
  118. <td><span title=""Dummy""><input id=""gridView_ctl02_ctl00"" type=""checkbox"" name=""gridView$ctl02$ctl00"" checked=""checked"" /></span></td>
  119. </tr><tr>
  120. <td><span title=""Dummy""><input id=""gridView_ctl03_ctl00"" type=""checkbox"" name=""gridView$ctl03$ctl00"" /></span></td>
  121. </tr><tr>
  122. <td><span title=""Dummy""><input id=""gridView_ctl04_ctl00"" type=""checkbox"" name=""gridView$ctl04$ctl00"" /></span></td>
  123. </tr>
  124. </table>
  125. </div>";
  126. WebTest t = new WebTest ("CheckBoxField_Bug595568_0.aspx");
  127. string pageHtml = t.Run ();
  128. string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
  129. HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
  130. }
  131. [Test (Description="Bug 595568 #1")]
  132. public void CheckBoxField_Bug595568_1 ()
  133. {
  134. string originalHtml = @"<div>
  135. <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
  136. <tr>
  137. <th scope=""col"">&nbsp;</th>
  138. </tr><tr>
  139. <td><select size=""4"">
  140. </select><span title=""Dummy""><input id=""gridView_ctl02_ctl01"" type=""checkbox"" name=""gridView$ctl02$ctl01"" checked=""checked"" /></span></td>
  141. </tr><tr>
  142. <td><select size=""4"">
  143. </select><span title=""Dummy""><input id=""gridView_ctl03_ctl01"" type=""checkbox"" name=""gridView$ctl03$ctl01"" /></span></td>
  144. </tr><tr>
  145. <td><select size=""4"">
  146. </select><span title=""Dummy""><input id=""gridView_ctl04_ctl01"" type=""checkbox"" name=""gridView$ctl04$ctl01"" /></span></td>
  147. </tr>
  148. </table>
  149. </div>";
  150. WebTest t = new WebTest ("CheckBoxField_Bug595568_1.aspx");
  151. string pageHtml = t.Run ();
  152. string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
  153. HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
  154. }
  155. [Test (Description="Bug 595568 #2")]
  156. public void CheckBoxField_Bug595568_2 ()
  157. {
  158. string originalHtml = @"<div>
  159. <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
  160. <tr>
  161. <th scope=""col"">&nbsp;</th>
  162. </tr><tr>
  163. <td><input id=""gridView_ctl02_ctl00"" type=""checkbox"" name=""gridView$ctl02$ctl00"" /><span title=""Dummy""><input id=""gridView_ctl02_ctl01"" type=""checkbox"" name=""gridView$ctl02$ctl01"" checked=""checked"" /></span><input id=""gridView_ctl02_ctl02"" type=""checkbox"" name=""gridView$ctl02$ctl02"" /></td>
  164. </tr><tr>
  165. <td><input id=""gridView_ctl03_ctl00"" type=""checkbox"" name=""gridView$ctl03$ctl00"" /><span title=""Dummy""><input id=""gridView_ctl03_ctl01"" type=""checkbox"" name=""gridView$ctl03$ctl01"" /></span><input id=""gridView_ctl03_ctl02"" type=""checkbox"" name=""gridView$ctl03$ctl02"" /></td>
  166. </tr><tr>
  167. <td><input id=""gridView_ctl04_ctl00"" type=""checkbox"" name=""gridView$ctl04$ctl00"" /><span title=""Dummy""><input id=""gridView_ctl04_ctl01"" type=""checkbox"" name=""gridView$ctl04$ctl01"" /></span><input id=""gridView_ctl04_ctl02"" type=""checkbox"" name=""gridView$ctl04$ctl02"" /></td>
  168. </tr>
  169. </table>
  170. </div>";
  171. WebTest t = new WebTest ("CheckBoxField_Bug595568_2.aspx");
  172. string pageHtml = t.Run ();
  173. string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
  174. HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
  175. }
  176. [Test (Description="Bug 595568 #5")]
  177. public void CheckBoxField_Bug595568_5 ()
  178. {
  179. string originalHtml = @"<div>
  180. <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
  181. <tr>
  182. <th scope=""col"">&nbsp;</th>
  183. </tr><tr>
  184. <td><select size=""4"">
  185. </select></td>
  186. </tr><tr>
  187. <td><select size=""4"">
  188. </select></td>
  189. </tr><tr>
  190. <td><select size=""4"">
  191. </select></td>
  192. </tr>
  193. </table>
  194. </div>";
  195. WebTest t = new WebTest ("CheckBoxField_Bug595568_5.aspx");
  196. string pageHtml = t.Run ();
  197. string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
  198. HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
  199. }
  200. [Test (Description="Bug 595568 #6")]
  201. public void CheckBoxField_Bug595568_6 ()
  202. {
  203. string originalHtml = @"<div>
  204. <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
  205. <tr>
  206. <th scope=""col"">&nbsp;</th>
  207. </tr><tr>
  208. <td><select size=""4"">
  209. </select><input id=""gridView_ctl02_ctl01"" type=""checkbox"" name=""gridView$ctl02$ctl01"" /></td>
  210. </tr><tr>
  211. <td><select size=""4"">
  212. </select><input id=""gridView_ctl03_ctl01"" type=""checkbox"" name=""gridView$ctl03$ctl01"" /></td>
  213. </tr><tr>
  214. <td><select size=""4"">
  215. </select><input id=""gridView_ctl04_ctl01"" type=""checkbox"" name=""gridView$ctl04$ctl01"" /></td>
  216. </tr>
  217. </table>
  218. </div>";
  219. WebTest t = new WebTest ("CheckBoxField_Bug595568_6.aspx");
  220. string pageHtml = t.Run ();
  221. string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
  222. HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
  223. }
  224. [Test (Description="Bug 595568 #7")]
  225. public void CheckBoxField_Bug595568_7 ()
  226. {
  227. string originalHtml = @"<div>
  228. <table id=""gridView"" cellspacing=""0"" rules=""all"" border=""1"" style=""border-collapse:collapse;"">
  229. <tr>
  230. <th scope=""col"">&nbsp;</th>
  231. </tr><tr>
  232. <td><input id=""gridView_ctl02_ctl00"" type=""checkbox"" name=""gridView$ctl02$ctl00"" /><select size=""4"">
  233. </select></td>
  234. </tr><tr>
  235. <td><input id=""gridView_ctl03_ctl00"" type=""checkbox"" name=""gridView$ctl03$ctl00"" /><select size=""4"">
  236. </select></td>
  237. </tr><tr>
  238. <td><input id=""gridView_ctl04_ctl00"" type=""checkbox"" name=""gridView$ctl04$ctl00"" /><select size=""4"">
  239. </select></td>
  240. </tr>
  241. </table>
  242. </div>";
  243. WebTest t = new WebTest ("CheckBoxField_Bug595568_7.aspx");
  244. string pageHtml = t.Run ();
  245. string renderedHtml = HtmlDiff.GetControlFromPageHtml (pageHtml);
  246. HtmlDiff.AssertAreEqual (originalHtml, renderedHtml, "#A1");
  247. }
  248. [Test]
  249. public void CheckBoxField_DefaultProperty ()
  250. {
  251. PokerCheckBoxField field = new PokerCheckBoxField ();
  252. Assert.AreEqual ("", field.DataField, "DataField");
  253. Assert.AreEqual ("", field.Text, "Text");
  254. Assert.AreEqual (false, field.GetSupportsHtmlEncode, "SupportsHtmlEncode");
  255. }
  256. [Test]
  257. public void CheckBoxField_AssignProperty ()
  258. {
  259. PokerCheckBoxField field = new PokerCheckBoxField ();
  260. field.DataField = "test";
  261. Assert.AreEqual ("test", field.DataField, "DataField");
  262. field.Text = "test";
  263. Assert.AreEqual ("test", field.Text, "Text");
  264. }
  265. public void CheckBoxField_ExtractValuesFromCell ()
  266. {
  267. PokerCheckBoxField field = new PokerCheckBoxField ();
  268. OrderedDictionary dictionary = new OrderedDictionary ();
  269. DataControlFieldCell cell = new DataControlFieldCell (null);
  270. cell.Controls.Add (new CheckBox ());
  271. field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
  272. Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#1");
  273. Assert.AreEqual ("False", dictionary[0].ToString (), "ExtractValuesFromCellValueFalse");
  274. CheckBox cb = new CheckBox ();
  275. cb.Checked = true;
  276. cell.Controls.Clear ();
  277. cell.Controls.Add (cb);
  278. field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
  279. Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#2");
  280. Assert.AreEqual ("True", dictionary[0].ToString (), "ExtractValuesFromCellValueTrue");
  281. }
  282. [Test]
  283. public void CheckBoxField_ValidateSupportsCallback ()
  284. {
  285. //This method has been implemented as an empty method
  286. }
  287. [Test]
  288. public void CheckBoxField_CopyProperties()
  289. {
  290. PokerCheckBoxField field = new PokerCheckBoxField ();
  291. CheckBoxField copy = new CheckBoxField();
  292. field.DataField = "test";
  293. field.Text = "test";
  294. field.DoCopyProperties (copy);
  295. Assert.AreEqual ("test", copy.Text, "Text");
  296. Assert.AreEqual ("test", copy.DataField, "DataField");
  297. }
  298. [Test]
  299. public void CheckBoxField_CreateField ()
  300. {
  301. PokerCheckBoxField field = new PokerCheckBoxField ();
  302. CheckBoxField blank = (CheckBoxField)field.DoCreateField ();
  303. Assert.IsNotNull (blank, "CreateField");
  304. }
  305. [Test]
  306. public void CheckBoxField_GetDesignTimeValue ()
  307. {
  308. PokerCheckBoxField field = new PokerCheckBoxField ();
  309. bool result = (bool)field.DoGetDesignTimeValue ();
  310. Assert.AreEqual (true, result, "GetDesignTimeValue");
  311. }
  312. [Test]
  313. public void CheckBoxField_InitializeDataCell ()
  314. {
  315. PokerCheckBoxField field = new PokerCheckBoxField ();
  316. field.HeaderText = "headertest";
  317. DataControlFieldCell cell = new DataControlFieldCell (null);
  318. DataControlRowState state = DataControlRowState.Edit;
  319. Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
  320. field.DoInitializeDataCell (cell, state);
  321. Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
  322. Assert.AreEqual ("headertest", ((CheckBox)cell.Controls[0]).ToolTip, "InitializeDataCellControlsData");
  323. cell.Controls.Clear ();
  324. field.DataField = "fake";
  325. field.Text = "celltext";
  326. state = DataControlRowState.Normal;
  327. field.DoInitializeDataCell (cell, state);
  328. Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
  329. Assert.AreEqual ("celltext", ((CheckBox) cell.Controls[0]).Text, "InitializeDataCellControlsData");
  330. }
  331. [Test]
  332. public void CheckBoxField_OnDataBindField ()
  333. {
  334. Page page = new Page ();
  335. GridView grid = new GridView ();
  336. page.Controls.Add (grid);
  337. grid.DataSource = this.CreateDataSource ();
  338. grid.AutoGenerateColumns = false;
  339. PokerCheckBoxField field = new PokerCheckBoxField ();
  340. field.HeaderText = "field_header";
  341. field.FooterText = "field_footer";
  342. field.DataField = FIELDNAME;
  343. grid.Columns.Add (field);
  344. grid.DataBind ();
  345. Assert.AreEqual (2, databound, "DataBindField");
  346. Assert.AreEqual (4, ((Control) grid.Controls[0]).Controls.Count, "DataBindFieldRowCountr");
  347. }
  348. [Test]
  349. [ExpectedException (typeof (HttpException))]
  350. public void CheckBoxField_OnDataBindFieldException ()
  351. {
  352. Page page = new Page ();
  353. GridView grid = new GridView ();
  354. page.Controls.Add (grid);
  355. grid.DataSource = this.CreateDataSource ();
  356. grid.AutoGenerateColumns = false;
  357. PokerCheckBoxField field = new PokerCheckBoxField ();
  358. field.HeaderText = "field_header";
  359. field.FooterText = "field_footer";
  360. field.DataField = WRONGFIELD;
  361. grid.Columns.Add (field);
  362. grid.DataBind ();
  363. }
  364. [Test]
  365. [ExpectedException(typeof(NotSupportedException))]
  366. public void CheckBoxField_GetApplyFormatInEditModeExeption ()
  367. {
  368. PokerCheckBoxField field = new PokerCheckBoxField ();
  369. bool stab = field.ApplyFormatInEditMode;
  370. }
  371. [Test]
  372. [ExpectedException (typeof (NotSupportedException))]
  373. public void CheckBoxField_SetApplyFormatInEditModeExeption ()
  374. {
  375. PokerCheckBoxField field = new PokerCheckBoxField ();
  376. field.ApplyFormatInEditMode = true;
  377. }
  378. [Test]
  379. [ExpectedException (typeof (NotSupportedException))]
  380. public void CheckBoxField_GetConvertEmptyStringToNull ()
  381. {
  382. PokerCheckBoxField field = new PokerCheckBoxField ();
  383. bool stab = field.ConvertEmptyStringToNull;
  384. }
  385. [Test]
  386. [ExpectedException (typeof (NotSupportedException))]
  387. public void CheckBoxField_SetConvertEmptyStringToNull ()
  388. {
  389. PokerCheckBoxField field = new PokerCheckBoxField ();
  390. field.ConvertEmptyStringToNull = true;
  391. }
  392. [Test]
  393. [ExpectedException (typeof (NotSupportedException))]
  394. public void CheckBoxField_SetDataFormatString ()
  395. {
  396. PokerCheckBoxField field = new PokerCheckBoxField ();
  397. field.DataFormatString = "";
  398. }
  399. [Test]
  400. [ExpectedException (typeof (NotSupportedException))]
  401. public void CheckBoxField_GetDataFormatString ()
  402. {
  403. PokerCheckBoxField field = new PokerCheckBoxField ();
  404. string res = field.DataFormatString;
  405. }
  406. [Test]
  407. [ExpectedException (typeof (NotSupportedException))]
  408. public void CheckBoxField_SetHtmlEncode ()
  409. {
  410. PokerCheckBoxField field = new PokerCheckBoxField ();
  411. field.HtmlEncode = true;
  412. }
  413. [Test]
  414. [ExpectedException (typeof (NotSupportedException))]
  415. public void CheckBoxField_GetHtmlEncode ()
  416. {
  417. PokerCheckBoxField field = new PokerCheckBoxField ();
  418. bool res = field.HtmlEncode;
  419. }
  420. [Test]
  421. [ExpectedException (typeof (NotSupportedException))]
  422. public void CheckBoxField_SetNullDisplayText ()
  423. {
  424. PokerCheckBoxField field = new PokerCheckBoxField ();
  425. field.NullDisplayText = "";
  426. }
  427. [Test]
  428. [ExpectedException (typeof (NotSupportedException))]
  429. public void CheckBoxField_GetNullDisplayText ()
  430. {
  431. PokerCheckBoxField field = new PokerCheckBoxField ();
  432. string res = field.NullDisplayText;
  433. }
  434. public DataTable CreateDataSource ()
  435. {
  436. DataTable aTable = new DataTable ("A");
  437. DataColumn dtCol;
  438. DataRow dtRow;
  439. // Create ID column and add to the DataTable.
  440. dtCol = new DataColumn ();
  441. dtCol.DataType = Type.GetType ("System.Boolean");
  442. dtCol.ColumnName = FIELDNAME;
  443. dtCol.Caption = FIELDNAME;
  444. dtCol.ReadOnly = true;
  445. // Add the column to the DataColumnCollection.
  446. aTable.Columns.Add (dtCol);
  447. dtCol = new DataColumn ();
  448. dtCol.DataType = Type.GetType ("System.String");
  449. dtCol.ColumnName = WRONGFIELD;
  450. dtCol.Caption = WRONGFIELD;
  451. dtCol.ReadOnly = true;
  452. // Add the column to the DataColumnCollection.
  453. aTable.Columns.Add (dtCol);
  454. // Create 2 rows to the table
  455. dtRow = aTable.NewRow ();
  456. dtRow[FIELDNAME] = true;
  457. dtRow[WRONGFIELD] = "1";
  458. aTable.Rows.Add (dtRow);
  459. dtRow = aTable.NewRow ();
  460. dtRow[FIELDNAME] = false;
  461. dtRow[WRONGFIELD] = "1";
  462. aTable.Rows.Add (dtRow);
  463. return aTable;
  464. }
  465. }
  466. }
  467. #endif