CheckBoxTest.cs 13 KB

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