CheckBoxFieldTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. namespace MonoTests.System.Web.UI.WebControls
  42. {
  43. class PokerCheckBoxField : CheckBoxField
  44. {
  45. // View state Stuff
  46. public PokerCheckBoxField ()
  47. : base ()
  48. {
  49. TrackViewState ();
  50. }
  51. public object SaveState ()
  52. {
  53. return SaveViewState ();
  54. }
  55. public void LoadState (object o)
  56. {
  57. LoadViewState (o);
  58. }
  59. public StateBag StateBag
  60. {
  61. get { return base.ViewState; }
  62. }
  63. public bool GetSupportsHtmlEncode
  64. {
  65. get
  66. {
  67. return base.SupportsHtmlEncode;
  68. }
  69. }
  70. public void DoCopyProperties (DataControlField newField)
  71. {
  72. base.CopyProperties (newField);
  73. }
  74. public DataControlField DoCreateField ()
  75. {
  76. return base.CreateField ();
  77. }
  78. public object DoGetDesignTimeValue ()
  79. {
  80. return base.GetDesignTimeValue ();
  81. }
  82. public void DoInitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
  83. {
  84. this.InitializeDataCell (cell, rowState);
  85. }
  86. protected override void OnDataBindField (object sender, EventArgs e)
  87. {
  88. base.OnDataBindField (sender, e);
  89. CheckBoxFieldTest.databound += 1;
  90. }
  91. }
  92. [TestFixture]
  93. public class CheckBoxFieldTest
  94. {
  95. public const string FIELDNAME = "checkbox";
  96. public const string WRONGFIELD = "str";
  97. public static int databound;
  98. [Test]
  99. public void CheckBoxField_DefaultProperty ()
  100. {
  101. PokerCheckBoxField field = new PokerCheckBoxField ();
  102. Assert.AreEqual ("", field.DataField, "DataField");
  103. Assert.AreEqual ("", field.Text, "Text");
  104. Assert.AreEqual (false, field.GetSupportsHtmlEncode, "SupportsHtmlEncode");
  105. }
  106. [Test]
  107. public void CheckBoxField_AssignProperty ()
  108. {
  109. PokerCheckBoxField field = new PokerCheckBoxField ();
  110. field.DataField = "test";
  111. Assert.AreEqual ("test", field.DataField, "DataField");
  112. field.Text = "test";
  113. Assert.AreEqual ("test", field.Text, "Text");
  114. }
  115. public void CheckBoxField_ExtractValuesFromCell ()
  116. {
  117. PokerCheckBoxField field = new PokerCheckBoxField ();
  118. OrderedDictionary dictionary = new OrderedDictionary ();
  119. DataControlFieldCell cell = new DataControlFieldCell (null);
  120. cell.Controls.Add (new CheckBox ());
  121. field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
  122. Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#1");
  123. Assert.AreEqual ("False", dictionary[0].ToString (), "ExtractValuesFromCellValueFalse");
  124. CheckBox cb = new CheckBox ();
  125. cb.Checked = true;
  126. cell.Controls.Clear ();
  127. cell.Controls.Add (cb);
  128. field.ExtractValuesFromCell (dictionary, cell, DataControlRowState.Normal, true);
  129. Assert.AreEqual (1, dictionary.Count, "ExtractValuesFromCellCount#2");
  130. Assert.AreEqual ("True", dictionary[0].ToString (), "ExtractValuesFromCellValueTrue");
  131. }
  132. [Test]
  133. public void CheckBoxField_ValidateSupportsCallback ()
  134. {
  135. //This method has been implemented as an empty method
  136. }
  137. [Test]
  138. public void CheckBoxField_CopyProperties()
  139. {
  140. PokerCheckBoxField field = new PokerCheckBoxField ();
  141. CheckBoxField copy = new CheckBoxField();
  142. field.DataField = "test";
  143. field.Text = "test";
  144. field.DoCopyProperties (copy);
  145. Assert.AreEqual ("test", copy.Text, "Text");
  146. Assert.AreEqual ("test", copy.DataField, "DataField");
  147. }
  148. [Test]
  149. public void CheckBoxField_CreateField ()
  150. {
  151. PokerCheckBoxField field = new PokerCheckBoxField ();
  152. CheckBoxField blank = (CheckBoxField)field.DoCreateField ();
  153. Assert.IsNotNull (blank, "CreateField");
  154. }
  155. [Test]
  156. public void CheckBoxField_GetDesignTimeValue ()
  157. {
  158. PokerCheckBoxField field = new PokerCheckBoxField ();
  159. bool result = (bool)field.DoGetDesignTimeValue ();
  160. Assert.AreEqual (true, result, "GetDesignTimeValue");
  161. }
  162. [Test]
  163. public void CheckBoxField_InitializeDataCell ()
  164. {
  165. PokerCheckBoxField field = new PokerCheckBoxField ();
  166. field.HeaderText = "headertest";
  167. DataControlFieldCell cell = new DataControlFieldCell (null);
  168. DataControlRowState state = DataControlRowState.Edit;
  169. Assert.AreEqual (0, cell.Controls.Count, "InitializeDataCellControlsBeforeInit");
  170. field.DoInitializeDataCell (cell, state);
  171. Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
  172. Assert.AreEqual ("headertest", ((CheckBox)cell.Controls[0]).ToolTip, "InitializeDataCellControlsData");
  173. cell.Controls.Clear ();
  174. field.DataField = "fake";
  175. field.Text = "celltext";
  176. state = DataControlRowState.Normal;
  177. field.DoInitializeDataCell (cell, state);
  178. Assert.AreEqual (1, cell.Controls.Count, "InitializeDataCellControlsAfterInit");
  179. Assert.AreEqual ("celltext", ((CheckBox) cell.Controls[0]).Text, "InitializeDataCellControlsData");
  180. }
  181. [Test]
  182. public void CheckBoxField_OnDataBindField ()
  183. {
  184. Page page = new Page ();
  185. GridView grid = new GridView ();
  186. page.Controls.Add (grid);
  187. grid.DataSource = this.CreateDataSource ();
  188. grid.AutoGenerateColumns = false;
  189. PokerCheckBoxField field = new PokerCheckBoxField ();
  190. field.HeaderText = "field_header";
  191. field.FooterText = "field_footer";
  192. field.DataField = FIELDNAME;
  193. grid.Columns.Add (field);
  194. grid.DataBind ();
  195. Assert.AreEqual (2, databound, "DataBindField");
  196. Assert.AreEqual (4, ((Control) grid.Controls[0]).Controls.Count, "DataBindFieldRowCountr");
  197. }
  198. [Test]
  199. [ExpectedException (typeof (HttpException))]
  200. public void CheckBoxField_OnDataBindFieldException ()
  201. {
  202. Page page = new Page ();
  203. GridView grid = new GridView ();
  204. page.Controls.Add (grid);
  205. grid.DataSource = this.CreateDataSource ();
  206. grid.AutoGenerateColumns = false;
  207. PokerCheckBoxField field = new PokerCheckBoxField ();
  208. field.HeaderText = "field_header";
  209. field.FooterText = "field_footer";
  210. field.DataField = WRONGFIELD;
  211. grid.Columns.Add (field);
  212. grid.DataBind ();
  213. }
  214. [Test]
  215. [ExpectedException(typeof(NotSupportedException))]
  216. public void CheckBoxField_GetApplyFormatInEditModeExeption ()
  217. {
  218. PokerCheckBoxField field = new PokerCheckBoxField ();
  219. bool stab = field.ApplyFormatInEditMode;
  220. }
  221. [Test]
  222. [ExpectedException (typeof (NotSupportedException))]
  223. public void CheckBoxField_SetApplyFormatInEditModeExeption ()
  224. {
  225. PokerCheckBoxField field = new PokerCheckBoxField ();
  226. field.ApplyFormatInEditMode = true;
  227. }
  228. [Test]
  229. [ExpectedException (typeof (NotSupportedException))]
  230. public void CheckBoxField_GetConvertEmptyStringToNull ()
  231. {
  232. PokerCheckBoxField field = new PokerCheckBoxField ();
  233. bool stab = field.ConvertEmptyStringToNull;
  234. }
  235. [Test]
  236. [ExpectedException (typeof (NotSupportedException))]
  237. public void CheckBoxField_SetConvertEmptyStringToNull ()
  238. {
  239. PokerCheckBoxField field = new PokerCheckBoxField ();
  240. field.ConvertEmptyStringToNull = true;
  241. }
  242. [Test]
  243. [ExpectedException (typeof (NotSupportedException))]
  244. public void CheckBoxField_SetDataFormatString ()
  245. {
  246. PokerCheckBoxField field = new PokerCheckBoxField ();
  247. field.DataFormatString = "";
  248. }
  249. [Test]
  250. [ExpectedException (typeof (NotSupportedException))]
  251. public void CheckBoxField_GetDataFormatString ()
  252. {
  253. PokerCheckBoxField field = new PokerCheckBoxField ();
  254. string res = field.DataFormatString;
  255. }
  256. [Test]
  257. [ExpectedException (typeof (NotSupportedException))]
  258. public void CheckBoxField_SetHtmlEncode ()
  259. {
  260. PokerCheckBoxField field = new PokerCheckBoxField ();
  261. field.HtmlEncode = true;
  262. }
  263. [Test]
  264. [ExpectedException (typeof (NotSupportedException))]
  265. public void CheckBoxField_GetHtmlEncode ()
  266. {
  267. PokerCheckBoxField field = new PokerCheckBoxField ();
  268. bool res = field.HtmlEncode;
  269. }
  270. [Test]
  271. [ExpectedException (typeof (NotSupportedException))]
  272. public void CheckBoxField_SetNullDisplayText ()
  273. {
  274. PokerCheckBoxField field = new PokerCheckBoxField ();
  275. field.NullDisplayText = "";
  276. }
  277. [Test]
  278. [ExpectedException (typeof (NotSupportedException))]
  279. public void CheckBoxField_GetNullDisplayText ()
  280. {
  281. PokerCheckBoxField field = new PokerCheckBoxField ();
  282. string res = field.NullDisplayText;
  283. }
  284. public DataTable CreateDataSource ()
  285. {
  286. DataTable aTable = new DataTable ("A");
  287. DataColumn dtCol;
  288. DataRow dtRow;
  289. // Create ID column and add to the DataTable.
  290. dtCol = new DataColumn ();
  291. dtCol.DataType = Type.GetType ("System.Boolean");
  292. dtCol.ColumnName = FIELDNAME;
  293. dtCol.Caption = FIELDNAME;
  294. dtCol.ReadOnly = true;
  295. // Add the column to the DataColumnCollection.
  296. aTable.Columns.Add (dtCol);
  297. dtCol = new DataColumn ();
  298. dtCol.DataType = Type.GetType ("System.String");
  299. dtCol.ColumnName = WRONGFIELD;
  300. dtCol.Caption = WRONGFIELD;
  301. dtCol.ReadOnly = true;
  302. // Add the column to the DataColumnCollection.
  303. aTable.Columns.Add (dtCol);
  304. // Create 2 rows to the table
  305. dtRow = aTable.NewRow ();
  306. dtRow[FIELDNAME] = true;
  307. dtRow[WRONGFIELD] = "1";
  308. aTable.Rows.Add (dtRow);
  309. dtRow = aTable.NewRow ();
  310. dtRow[FIELDNAME] = false;
  311. dtRow[WRONGFIELD] = "1";
  312. aTable.Rows.Add (dtRow);
  313. return aTable;
  314. }
  315. }
  316. }
  317. #endif