PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Web/Test/System.Web.UI.WebControls/CheckBoxTest.cs

https://bitbucket.org/danipen/mono
C# | 454 lines | 354 code | 54 blank | 46 comment | 21 complexity | 8607b8d293d24d7df527650089722152 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // CheckBoxTest.cs
  3. // - Unit tests for System.Web.UI.WebControls.CheckBox
  4. //
  5. // Author:
  6. // Dick Porter <dick@ximian.com>
  7. // Yoni Klain <Yonik@mainsoft.com>
  8. //
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.IO;
  32. using System.Web;
  33. using System.Web.UI;
  34. using System.Web.UI.WebControls;
  35. using System.Drawing;
  36. using System.Collections.Specialized;
  37. using NUnit.Framework;
  38. using MonoTests.stand_alone.WebHarness;
  39. using MonoTests.SystemWeb.Framework;
  40. using System.Collections;
  41. namespace MonoTests.System.Web.UI.WebControls {
  42. public class TestCheckBox : CheckBox {
  43. public StateBag StateBag {
  44. get { return base.ViewState; }
  45. }
  46. public string Render ()
  47. {
  48. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  49. base.Render (writer);
  50. return writer.InnerWriter.ToString ();
  51. }
  52. public void TrackState ()
  53. {
  54. TrackViewState ();
  55. }
  56. public object SaveState ()
  57. {
  58. return SaveViewState ();
  59. }
  60. public void LoadState (object o)
  61. {
  62. LoadViewState (o);
  63. }
  64. public void LoadPostbackData (string value)
  65. {
  66. NameValueCollection nvc = new NameValueCollection ();
  67. if (value != null)
  68. nvc.Add ("mykey", value);
  69. ((IPostBackDataHandler) this).LoadPostData ("mykey", nvc);
  70. }
  71. }
  72. public class PokerCheckBox : CheckBox
  73. {
  74. private bool checker;
  75. public bool Checker
  76. {
  77. get { return checker; }
  78. }
  79. public void Reset ()
  80. {
  81. checker = false;
  82. }
  83. public string Render ()
  84. {
  85. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  86. base.Render (writer);
  87. return writer.InnerWriter.ToString ();
  88. }
  89. override protected void AddAttributesToRender (HtmlTextWriter writer)
  90. {
  91. Reset ();
  92. base.AddAttributesToRender (writer);
  93. writer.AddAttribute ("Attribute", "AttributeValue");
  94. writer.AddStyleAttribute ("StyleAttribute", "StyleAttValue");
  95. checker = true;
  96. }
  97. #if NET_2_0
  98. public new void RaisePostDataChangedEvent ()
  99. {
  100. base.RaisePostDataChangedEvent ();
  101. }
  102. protected override bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  103. {
  104. if (WebTest.CurrentTest.UserData == null) {
  105. ArrayList list = new ArrayList ();
  106. list.Add ("LoadPostData");
  107. WebTest.CurrentTest.UserData = list;
  108. }
  109. else {
  110. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  111. if (list == null)
  112. throw new NullReferenceException ();
  113. list.Add ("LoadPostData");
  114. WebTest.CurrentTest.UserData = list;
  115. }
  116. return base.LoadPostData (postDataKey, postCollection);
  117. }
  118. protected internal override void OnLoad (EventArgs e)
  119. {
  120. if (this.Page.IsPostBack) {
  121. if (WebTest.CurrentTest.UserData == null) {
  122. ArrayList list = new ArrayList ();
  123. list.Add ("ControlLoad");
  124. WebTest.CurrentTest.UserData = list;
  125. }
  126. else {
  127. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  128. if (list == null)
  129. throw new NullReferenceException ();
  130. list.Add ("ControlLoad");
  131. WebTest.CurrentTest.UserData = list;
  132. }
  133. }
  134. base.OnLoad (e);
  135. }
  136. #endif
  137. }
  138. [TestFixture]
  139. public class CheckBoxTest {
  140. [Test]
  141. public void DefaultProperties ()
  142. {
  143. TestCheckBox c = new TestCheckBox ();
  144. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
  145. Assert.IsFalse (c.AutoPostBack, "AutoPostBack");
  146. Assert.IsFalse (c.Checked, "Checked");
  147. Assert.AreEqual (String.Empty, c.Text, "Text");
  148. Assert.AreEqual (TextAlign.Right, c.TextAlign, "TextAlign");
  149. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count-2");
  150. #if NET_2_0
  151. Assert.IsFalse (c.CausesValidation, "CausesValidation");
  152. Assert.AreEqual (String.Empty, c.ValidationGroup, "ValidationGroup");
  153. #endif
  154. }
  155. #if NET_2_0
  156. [Test]
  157. public void InputAttributesTest ()
  158. {
  159. TestCheckBox c = new TestCheckBox ();
  160. c.InputAttributes.Add ("Atribute", "Test");
  161. string html = c.Render ();
  162. HtmlDiff.AssertAreEqual ("<input type=\"checkbox\" Atribute=\"Test\" />", html, "Input Attribute fail");
  163. }
  164. [Test]
  165. public void InputAttributesTest2 ()
  166. {
  167. TestCheckBox c = new TestCheckBox ();
  168. c.InputAttributes.Add ("value", "value1");
  169. c.Text = "Title";
  170. c.Checked = true;
  171. #if NET_4_0
  172. string origHtml = "<input id=\"\" type=\"checkbox\" checked=\"checked\" value=\"value1\" /><label for=\"\">Title</label>";
  173. #else
  174. string origHtml = "<input type=\"checkbox\" checked=\"checked\" value=\"value1\" /><label for>Title</label>";
  175. #endif
  176. string html = c.Render ();
  177. HtmlDiff.AssertAreEqual (origHtml, html, "#A1");
  178. }
  179. [Test]
  180. public void LabelAttributesTest_1 ()
  181. {
  182. TestCheckBox c = new TestCheckBox ();
  183. c.ID = "Check";
  184. c.Text = "CheckBoxText";
  185. c.LabelAttributes.Add ("Atribute", "Test");
  186. string html = c.Render ();
  187. HtmlDiff.AssertAreEqual ("<input id=\"Check\" type=\"checkbox\" name=\"Check\" /><label for=\"Check\" Atribute=\"Test\">CheckBoxText</label>", html, "Label Attributes fail#1");
  188. }
  189. [Test]
  190. public void LabelAttributesTest_2 ()
  191. {
  192. TestCheckBox c = new TestCheckBox ();
  193. c.LabelAttributes.Add ("Atribute", "Test");
  194. string html = c.Render ();
  195. HtmlDiff.AssertAreEqual ("<input type=\"checkbox\" />", html, "Label Attributes fail#2");
  196. }
  197. [Test]
  198. public void AddAttributesToRenderTest_1 ()
  199. {
  200. PokerCheckBox c = new PokerCheckBox ();
  201. c.Text = "CheckBoxText";
  202. string html = c.Render ();
  203. Assert.AreEqual (true, c.Checker, "AddAttributesToRender dosn't called fail");
  204. HtmlDiff.AssertAreEqual ("<input Attribute=\"AttributeValue\" type=\"checkbox\" style=\"StyleAttribute:StyleAttValue;\" /><label for>CheckBoxText</label>", html, "Add Attributes To Render fail#1");
  205. }
  206. [Test]
  207. public void AddAttributesToRenderTest_2 ()
  208. {
  209. PokerCheckBox c = new PokerCheckBox ();
  210. string html = c.Render ();
  211. Assert.AreEqual (true, c.Checker, "AddAttributesToRender dosn't called fail");
  212. HtmlDiff.AssertAreEqual ("<input Attribute=\"AttributeValue\" type=\"checkbox\" style=\"StyleAttribute:StyleAttValue;\" />", html, "Add Attributes To Render fail#2");
  213. }
  214. #endif
  215. [Test]
  216. public void NullProperties ()
  217. {
  218. TestCheckBox c = new TestCheckBox ();
  219. c.Text = null;
  220. Assert.AreEqual (String.Empty, c.Text, "Text");
  221. c.TextAlign = TextAlign.Right;
  222. Assert.AreEqual (TextAlign.Right, c.TextAlign, "TextAlign");
  223. c.AutoPostBack = true;
  224. Assert.IsTrue (c.AutoPostBack, "AutoPostBack");
  225. c.Checked = true;
  226. Assert.IsTrue (c.Checked, "Checked");
  227. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
  228. Assert.AreEqual (3, c.StateBag.Count, "ViewState.Count-1");
  229. }
  230. [Test]
  231. public void CleanProperties ()
  232. {
  233. TestCheckBox c = new TestCheckBox ();
  234. c.Text = "text";
  235. Assert.AreEqual ("text", c.Text, "Text");
  236. c.AutoPostBack = true;
  237. c.TextAlign = TextAlign.Left;
  238. c.Checked = true;
  239. Assert.AreEqual (4, c.StateBag.Count, "ViewState.Count");
  240. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
  241. c.Text = null;
  242. c.AutoPostBack = false;
  243. c.TextAlign = TextAlign.Right;
  244. c.Checked = false;
  245. // If Text is null it is removed from the ViewState
  246. Assert.AreEqual (3, c.StateBag.Count, "ViewState.Count-2");
  247. // This was failing on mono, because the
  248. // viewstate is holding an int not an enum.
  249. // (it passes on ms)
  250. Assert.AreEqual (TextAlign.Right, c.StateBag["TextAlign"], "TextAlign");
  251. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count-2");
  252. }
  253. [Test]
  254. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  255. public void TextAlign_Invalid ()
  256. {
  257. CheckBox c = new CheckBox ();
  258. c.TextAlign = (TextAlign)Int32.MinValue;
  259. }
  260. [Test]
  261. public void TextAlign_Values ()
  262. {
  263. CheckBox c = new CheckBox ();
  264. foreach (TextAlign ta in Enum.GetValues (typeof (TextAlign))) {
  265. c.TextAlign = ta;
  266. }
  267. }
  268. [Test]
  269. public void Render ()
  270. {
  271. TestCheckBox c = new TestCheckBox ();
  272. c.ID = "ID";
  273. c.Text = "Text";
  274. c.TextAlign = TextAlign.Left;
  275. Assert.AreEqual (@"<label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" />", c.Render (), "R#1");
  276. c.TextAlign = TextAlign.Right;
  277. Assert.AreEqual (@"<input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label>", c.Render (), "R#2");
  278. c.Attributes ["style"] = "color:red;";
  279. c.TextAlign = TextAlign.Left;
  280. Assert.AreEqual (@"<span style=""color:red;""><label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" /></span>",
  281. c.Render (), "R#3");
  282. c.TextAlign = TextAlign.Right;
  283. Assert.AreEqual (@"<span style=""color:red;""><input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label></span>",
  284. c.Render (), "R#4");
  285. c.Attributes ["style"] = null;
  286. c.ForeColor = Color.Red;
  287. c.TextAlign = TextAlign.Left;
  288. Assert.AreEqual (@"<span style=""color:Red;""><label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" /></span>",
  289. c.Render (), "R#4");
  290. c.TextAlign = TextAlign.Right;
  291. Assert.AreEqual (@"<span style=""color:Red;""><input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label></span>",
  292. c.Render (), "R#6");
  293. }
  294. //
  295. // Code like
  296. // if (value == null)
  297. // ViewState.Remove ("Text");
  298. // else
  299. // ViewState ["Text"] = value
  300. // is wrong. We need to store when we are tracking viewstate.
  301. //
  302. // Statebag takes care of this behavior, so the code is not needed.
  303. //
  304. [Test]
  305. public void CheckboxViewstateTextNull ()
  306. {
  307. TestCheckBox c = new TestCheckBox ();
  308. c.Text = "text";
  309. c.TrackState ();
  310. c.Text = null;
  311. Assert.AreEqual ("", c.Text);
  312. object o = c.SaveState ();
  313. c = new TestCheckBox ();
  314. c.Text = "text";
  315. c.TrackState ();
  316. c.LoadState (o);
  317. Assert.AreEqual ("", c.Text);
  318. }
  319. #if NET_2_0
  320. [Test]
  321. public void CheckboxViewstateValidation ()
  322. {
  323. // for some reason, MS doesn't save the validation
  324. // properties to the viewstate for the Checkbox
  325. // control. why not?
  326. TestCheckBox o = new TestCheckBox ();
  327. o.ValidationGroup = "VG1";
  328. o.CausesValidation = true;
  329. o.TrackState ();
  330. Assert.AreEqual ("VG1", o.ValidationGroup, "V1");
  331. Assert.IsTrue (o.CausesValidation, "V2");
  332. object state = o.SaveState ();
  333. TestCheckBox copy = new TestCheckBox ();
  334. copy.LoadState (state);
  335. Assert.AreEqual ("", copy.ValidationGroup, "V3");
  336. Assert.IsFalse (copy.CausesValidation, "V4");
  337. }
  338. [Test]
  339. public void RaisePostDataChangedEvent ()
  340. {
  341. PokerCheckBox o = new PokerCheckBox ();
  342. o.CheckedChanged += new EventHandler (o_CheckedChanged);
  343. Assert.AreEqual (false, eventCheckedChanged, "RaisePostDataChangedEven#1");
  344. o.RaisePostDataChangedEvent ();
  345. Assert.AreEqual (true, eventCheckedChanged, "RaisePostDataChangedEven#2");
  346. }
  347. bool eventCheckedChanged;
  348. void o_CheckedChanged (object sender, EventArgs e)
  349. {
  350. eventCheckedChanged = true;
  351. }
  352. [Test]
  353. [Category ("NunitWeb")]
  354. public void LoadPostData () //Just flow and not implementation detail
  355. {
  356. WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData_Load));
  357. string html = t.Run ();
  358. FormRequest fr = new FormRequest (t.Response, "form1");
  359. fr.Controls.Add ("__EVENTTARGET");
  360. fr.Controls.Add ("__EVENTARGUMENT");
  361. fr.Controls["__EVENTTARGET"].Value = "pb";
  362. fr.Controls["__EVENTARGUMENT"].Value = "";
  363. t.Request = fr;
  364. t.Run ();
  365. ArrayList eventlist = t.UserData as ArrayList;
  366. if (eventlist == null)
  367. Assert.Fail ("User data does not been created fail");
  368. Assert.AreEqual ("PageLoad", eventlist[0], "Live Cycle Flow #1");
  369. Assert.AreEqual ("ControlLoad", eventlist[1], "Live Cycle Flow #2");
  370. Assert.AreEqual ("LoadPostData", eventlist[2], "Live Cycle Flow #3");
  371. }
  372. public static void LoadPostData_Load (Page p)
  373. {
  374. PokerCheckBox b = new PokerCheckBox ();
  375. b.AutoPostBack = true;
  376. b.ID = "pb";
  377. p.Form.Controls.Add (b);
  378. if (p.IsPostBack) {
  379. if (WebTest.CurrentTest.UserData == null) {
  380. ArrayList list = new ArrayList ();
  381. list.Add ("PageLoad");
  382. WebTest.CurrentTest.UserData = list;
  383. }
  384. else {
  385. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  386. if (list == null)
  387. throw new NullReferenceException ();
  388. list.Add ("PageLoad");
  389. WebTest.CurrentTest.UserData = list;
  390. }
  391. }
  392. }
  393. [TestFixtureTearDown]
  394. public void TearDown ()
  395. {
  396. WebTest.Unload ();
  397. }
  398. #endif
  399. }
  400. }