CheckBoxTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //
  2. // CheckBoxTest.cs
  3. // - Unit tests for System.Web.UI.WebControls.CheckBox
  4. //
  5. // Author:
  6. // Dick Porter <[email protected]>
  7. // Yoni Klain <[email protected]>
  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. public new void RaisePostDataChangedEvent ()
  98. {
  99. base.RaisePostDataChangedEvent ();
  100. }
  101. protected override bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  102. {
  103. if (WebTest.CurrentTest.UserData == null) {
  104. ArrayList list = new ArrayList ();
  105. list.Add ("LoadPostData");
  106. WebTest.CurrentTest.UserData = list;
  107. }
  108. else {
  109. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  110. if (list == null)
  111. throw new NullReferenceException ();
  112. list.Add ("LoadPostData");
  113. WebTest.CurrentTest.UserData = list;
  114. }
  115. return base.LoadPostData (postDataKey, postCollection);
  116. }
  117. protected internal override void OnLoad (EventArgs e)
  118. {
  119. if (this.Page.IsPostBack) {
  120. if (WebTest.CurrentTest.UserData == null) {
  121. ArrayList list = new ArrayList ();
  122. list.Add ("ControlLoad");
  123. WebTest.CurrentTest.UserData = list;
  124. }
  125. else {
  126. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  127. if (list == null)
  128. throw new NullReferenceException ();
  129. list.Add ("ControlLoad");
  130. WebTest.CurrentTest.UserData = list;
  131. }
  132. }
  133. base.OnLoad (e);
  134. }
  135. }
  136. [TestFixture]
  137. public class CheckBoxTest {
  138. [Test]
  139. public void DefaultProperties ()
  140. {
  141. TestCheckBox c = new TestCheckBox ();
  142. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
  143. Assert.IsFalse (c.AutoPostBack, "AutoPostBack");
  144. Assert.IsFalse (c.Checked, "Checked");
  145. Assert.AreEqual (String.Empty, c.Text, "Text");
  146. Assert.AreEqual (TextAlign.Right, c.TextAlign, "TextAlign");
  147. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count-2");
  148. Assert.IsFalse (c.CausesValidation, "CausesValidation");
  149. Assert.AreEqual (String.Empty, c.ValidationGroup, "ValidationGroup");
  150. }
  151. [Test]
  152. public void InputAttributesTest ()
  153. {
  154. TestCheckBox c = new TestCheckBox ();
  155. c.InputAttributes.Add ("Atribute", "Test");
  156. string html = c.Render ();
  157. HtmlDiff.AssertAreEqual ("<input type=\"checkbox\" Atribute=\"Test\" />", html, "Input Attribute fail");
  158. }
  159. [Test]
  160. public void InputAttributesTest2 ()
  161. {
  162. TestCheckBox c = new TestCheckBox ();
  163. c.InputAttributes.Add ("value", "value1");
  164. c.Text = "Title";
  165. c.Checked = true;
  166. string origHtml = "<input id=\"\" type=\"checkbox\" checked=\"checked\" value=\"value1\" /><label for=\"\">Title</label>";
  167. string html = c.Render ();
  168. HtmlDiff.AssertAreEqual (origHtml, html, "#A1");
  169. }
  170. [Test]
  171. public void LabelAttributesTest_1 ()
  172. {
  173. TestCheckBox c = new TestCheckBox ();
  174. c.ID = "Check";
  175. c.Text = "CheckBoxText";
  176. c.LabelAttributes.Add ("Atribute", "Test");
  177. string html = c.Render ();
  178. HtmlDiff.AssertAreEqual ("<input id=\"Check\" type=\"checkbox\" name=\"Check\" /><label for=\"Check\" Atribute=\"Test\">CheckBoxText</label>", html, "Label Attributes fail#1");
  179. }
  180. [Test]
  181. public void LabelAttributesTest_2 ()
  182. {
  183. TestCheckBox c = new TestCheckBox ();
  184. c.LabelAttributes.Add ("Atribute", "Test");
  185. string html = c.Render ();
  186. HtmlDiff.AssertAreEqual ("<input type=\"checkbox\" />", html, "Label Attributes fail#2");
  187. }
  188. [Test]
  189. public void AddAttributesToRenderTest_1 ()
  190. {
  191. PokerCheckBox c = new PokerCheckBox ();
  192. c.Text = "CheckBoxText";
  193. string html = c.Render ();
  194. Assert.AreEqual (true, c.Checker, "AddAttributesToRender dosn't called fail");
  195. HtmlDiff.AssertAreEqual ("<input Attribute=\"AttributeValue\" type=\"checkbox\" style=\"StyleAttribute:StyleAttValue;\" /><label for>CheckBoxText</label>", html, "Add Attributes To Render fail#1");
  196. }
  197. [Test]
  198. public void AddAttributesToRenderTest_2 ()
  199. {
  200. PokerCheckBox c = new PokerCheckBox ();
  201. string html = c.Render ();
  202. Assert.AreEqual (true, c.Checker, "AddAttributesToRender dosn't called fail");
  203. HtmlDiff.AssertAreEqual ("<input Attribute=\"AttributeValue\" type=\"checkbox\" style=\"StyleAttribute:StyleAttValue;\" />", html, "Add Attributes To Render fail#2");
  204. }
  205. [Test]
  206. public void NullProperties ()
  207. {
  208. TestCheckBox c = new TestCheckBox ();
  209. c.Text = null;
  210. Assert.AreEqual (String.Empty, c.Text, "Text");
  211. c.TextAlign = TextAlign.Right;
  212. Assert.AreEqual (TextAlign.Right, c.TextAlign, "TextAlign");
  213. c.AutoPostBack = true;
  214. Assert.IsTrue (c.AutoPostBack, "AutoPostBack");
  215. c.Checked = true;
  216. Assert.IsTrue (c.Checked, "Checked");
  217. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
  218. Assert.AreEqual (3, c.StateBag.Count, "ViewState.Count-1");
  219. }
  220. [Test]
  221. public void CleanProperties ()
  222. {
  223. TestCheckBox c = new TestCheckBox ();
  224. c.Text = "text";
  225. Assert.AreEqual ("text", c.Text, "Text");
  226. c.AutoPostBack = true;
  227. c.TextAlign = TextAlign.Left;
  228. c.Checked = true;
  229. Assert.AreEqual (4, c.StateBag.Count, "ViewState.Count");
  230. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
  231. c.Text = null;
  232. c.AutoPostBack = false;
  233. c.TextAlign = TextAlign.Right;
  234. c.Checked = false;
  235. // If Text is null it is removed from the ViewState
  236. Assert.AreEqual (3, c.StateBag.Count, "ViewState.Count-2");
  237. // This was failing on mono, because the
  238. // viewstate is holding an int not an enum.
  239. // (it passes on ms)
  240. Assert.AreEqual (TextAlign.Right, c.StateBag["TextAlign"], "TextAlign");
  241. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count-2");
  242. }
  243. [Test]
  244. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  245. public void TextAlign_Invalid ()
  246. {
  247. CheckBox c = new CheckBox ();
  248. c.TextAlign = (TextAlign)Int32.MinValue;
  249. }
  250. [Test]
  251. public void TextAlign_Values ()
  252. {
  253. CheckBox c = new CheckBox ();
  254. foreach (TextAlign ta in Enum.GetValues (typeof (TextAlign))) {
  255. c.TextAlign = ta;
  256. }
  257. }
  258. [Test]
  259. public void Render ()
  260. {
  261. TestCheckBox c = new TestCheckBox ();
  262. c.ID = "ID";
  263. c.Text = "Text";
  264. c.TextAlign = TextAlign.Left;
  265. Assert.AreEqual (@"<label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" />", c.Render (), "R#1");
  266. c.TextAlign = TextAlign.Right;
  267. Assert.AreEqual (@"<input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label>", c.Render (), "R#2");
  268. c.Attributes ["style"] = "color:red;";
  269. c.TextAlign = TextAlign.Left;
  270. Assert.AreEqual (@"<span style=""color:red;""><label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" /></span>",
  271. c.Render (), "R#3");
  272. c.TextAlign = TextAlign.Right;
  273. Assert.AreEqual (@"<span style=""color:red;""><input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label></span>",
  274. c.Render (), "R#4");
  275. c.Attributes ["style"] = null;
  276. c.ForeColor = Color.Red;
  277. c.TextAlign = TextAlign.Left;
  278. Assert.AreEqual (@"<span style=""color:Red;""><label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" /></span>",
  279. c.Render (), "R#4");
  280. c.TextAlign = TextAlign.Right;
  281. Assert.AreEqual (@"<span style=""color:Red;""><input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label></span>",
  282. c.Render (), "R#6");
  283. }
  284. //
  285. // Code like
  286. // if (value == null)
  287. // ViewState.Remove ("Text");
  288. // else
  289. // ViewState ["Text"] = value
  290. // is wrong. We need to store when we are tracking viewstate.
  291. //
  292. // Statebag takes care of this behavior, so the code is not needed.
  293. //
  294. [Test]
  295. public void CheckboxViewstateTextNull ()
  296. {
  297. TestCheckBox c = new TestCheckBox ();
  298. c.Text = "text";
  299. c.TrackState ();
  300. c.Text = null;
  301. Assert.AreEqual ("", c.Text);
  302. object o = c.SaveState ();
  303. c = new TestCheckBox ();
  304. c.Text = "text";
  305. c.TrackState ();
  306. c.LoadState (o);
  307. Assert.AreEqual ("", c.Text);
  308. }
  309. [Test]
  310. public void CheckboxViewstateValidation ()
  311. {
  312. // for some reason, MS doesn't save the validation
  313. // properties to the viewstate for the Checkbox
  314. // control. why not?
  315. TestCheckBox o = new TestCheckBox ();
  316. o.ValidationGroup = "VG1";
  317. o.CausesValidation = true;
  318. o.TrackState ();
  319. Assert.AreEqual ("VG1", o.ValidationGroup, "V1");
  320. Assert.IsTrue (o.CausesValidation, "V2");
  321. object state = o.SaveState ();
  322. TestCheckBox copy = new TestCheckBox ();
  323. copy.LoadState (state);
  324. Assert.AreEqual ("", copy.ValidationGroup, "V3");
  325. Assert.IsFalse (copy.CausesValidation, "V4");
  326. }
  327. [Test]
  328. public void RaisePostDataChangedEvent ()
  329. {
  330. PokerCheckBox o = new PokerCheckBox ();
  331. o.CheckedChanged += new EventHandler (o_CheckedChanged);
  332. Assert.AreEqual (false, eventCheckedChanged, "RaisePostDataChangedEven#1");
  333. o.RaisePostDataChangedEvent ();
  334. Assert.AreEqual (true, eventCheckedChanged, "RaisePostDataChangedEven#2");
  335. }
  336. bool eventCheckedChanged;
  337. void o_CheckedChanged (object sender, EventArgs e)
  338. {
  339. eventCheckedChanged = true;
  340. }
  341. [Test]
  342. [Category ("NunitWeb")]
  343. public void LoadPostData () //Just flow and not implementation detail
  344. {
  345. WebTest t = new WebTest (PageInvoker.CreateOnLoad (LoadPostData_Load));
  346. string html = t.Run ();
  347. FormRequest fr = new FormRequest (t.Response, "form1");
  348. fr.Controls.Add ("__EVENTTARGET");
  349. fr.Controls.Add ("__EVENTARGUMENT");
  350. fr.Controls["__EVENTTARGET"].Value = "pb";
  351. fr.Controls["__EVENTARGUMENT"].Value = "";
  352. t.Request = fr;
  353. t.Run ();
  354. ArrayList eventlist = t.UserData as ArrayList;
  355. if (eventlist == null)
  356. Assert.Fail ("User data does not been created fail");
  357. Assert.AreEqual ("PageLoad", eventlist[0], "Live Cycle Flow #1");
  358. Assert.AreEqual ("ControlLoad", eventlist[1], "Live Cycle Flow #2");
  359. Assert.AreEqual ("LoadPostData", eventlist[2], "Live Cycle Flow #3");
  360. }
  361. public static void LoadPostData_Load (Page p)
  362. {
  363. PokerCheckBox b = new PokerCheckBox ();
  364. b.AutoPostBack = true;
  365. b.ID = "pb";
  366. p.Form.Controls.Add (b);
  367. if (p.IsPostBack) {
  368. if (WebTest.CurrentTest.UserData == null) {
  369. ArrayList list = new ArrayList ();
  370. list.Add ("PageLoad");
  371. WebTest.CurrentTest.UserData = list;
  372. }
  373. else {
  374. ArrayList list = WebTest.CurrentTest.UserData as ArrayList;
  375. if (list == null)
  376. throw new NullReferenceException ();
  377. list.Add ("PageLoad");
  378. WebTest.CurrentTest.UserData = list;
  379. }
  380. }
  381. }
  382. [TestFixtureTearDown]
  383. public void TearDown ()
  384. {
  385. WebTest.Unload ();
  386. }
  387. }
  388. }