2
0

CheckBoxTest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // CheckBoxTest.cs
  3. // - Unit tests for System.Web.UI.WebControls.CheckBox
  4. //
  5. // Author:
  6. // Dick Porter <[email protected]>
  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. //
  29. using System;
  30. using System.IO;
  31. using System.Web;
  32. using System.Web.UI;
  33. using System.Web.UI.WebControls;
  34. using System.Drawing;
  35. using System.Collections.Specialized;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.Web.UI.WebControls {
  38. public class TestCheckBox : CheckBox {
  39. public StateBag StateBag {
  40. get { return base.ViewState; }
  41. }
  42. public string Render ()
  43. {
  44. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  45. base.Render (writer);
  46. return writer.InnerWriter.ToString ();
  47. }
  48. public void TrackState ()
  49. {
  50. TrackViewState ();
  51. }
  52. public object SaveState ()
  53. {
  54. return SaveViewState ();
  55. }
  56. public void LoadState (object o)
  57. {
  58. LoadViewState (o);
  59. }
  60. public void LoadPostbackData (string value)
  61. {
  62. NameValueCollection nvc = new NameValueCollection ();
  63. if (value != null)
  64. nvc.Add ("mykey", value);
  65. ((IPostBackDataHandler) this).LoadPostData ("mykey", nvc);
  66. }
  67. }
  68. [TestFixture]
  69. public class CheckBoxTest {
  70. [Test]
  71. public void DefaultProperties ()
  72. {
  73. TestCheckBox c = new TestCheckBox ();
  74. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
  75. Assert.IsFalse (c.AutoPostBack, "AutoPostBack");
  76. Assert.IsFalse (c.Checked, "Checked");
  77. Assert.AreEqual (String.Empty, c.Text, "Text");
  78. Assert.AreEqual (TextAlign.Right, c.TextAlign, "TextAlign");
  79. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count-2");
  80. #if NET_2_0
  81. Assert.IsFalse (c.CausesValidation, "CausesValidation");
  82. Assert.AreEqual (String.Empty, c.ValidationGroup, "ValidationGroup");
  83. #endif
  84. }
  85. [Test]
  86. public void NullProperties ()
  87. {
  88. TestCheckBox c = new TestCheckBox ();
  89. c.Text = null;
  90. Assert.AreEqual (String.Empty, c.Text, "Text");
  91. c.TextAlign = TextAlign.Right;
  92. Assert.AreEqual (TextAlign.Right, c.TextAlign, "TextAlign");
  93. c.AutoPostBack = true;
  94. Assert.IsTrue (c.AutoPostBack, "AutoPostBack");
  95. c.Checked = true;
  96. Assert.IsTrue (c.Checked, "Checked");
  97. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
  98. Assert.AreEqual (3, c.StateBag.Count, "ViewState.Count-1");
  99. }
  100. [Test]
  101. public void CleanProperties ()
  102. {
  103. TestCheckBox c = new TestCheckBox ();
  104. c.Text = "text";
  105. Assert.AreEqual ("text", c.Text, "Text");
  106. c.AutoPostBack = true;
  107. c.TextAlign = TextAlign.Left;
  108. c.Checked = true;
  109. Assert.AreEqual (4, c.StateBag.Count, "ViewState.Count");
  110. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count");
  111. c.Text = null;
  112. c.AutoPostBack = false;
  113. c.TextAlign = TextAlign.Right;
  114. c.Checked = false;
  115. // If Text is null it is removed from the ViewState
  116. Assert.AreEqual (3, c.StateBag.Count, "ViewState.Count-2");
  117. // This was failing on mono, because the
  118. // viewstate is holding an int not an enum.
  119. // (it passes on ms)
  120. Assert.AreEqual (TextAlign.Right, c.StateBag["TextAlign"], "TextAlign");
  121. Assert.AreEqual (0, c.Attributes.Count, "Attributes.Count-2");
  122. }
  123. [Test]
  124. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  125. public void TextAlign_Invalid ()
  126. {
  127. CheckBox c = new CheckBox ();
  128. c.TextAlign = (TextAlign)Int32.MinValue;
  129. }
  130. [Test]
  131. public void TextAlign_Values ()
  132. {
  133. CheckBox c = new CheckBox ();
  134. foreach (TextAlign ta in Enum.GetValues (typeof (TextAlign))) {
  135. c.TextAlign = ta;
  136. }
  137. }
  138. [Test]
  139. public void Render ()
  140. {
  141. TestCheckBox c = new TestCheckBox ();
  142. c.ID = "ID";
  143. c.Text = "Text";
  144. c.TextAlign = TextAlign.Left;
  145. Assert.AreEqual (@"<label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" />", c.Render (), "R#1");
  146. c.TextAlign = TextAlign.Right;
  147. Assert.AreEqual (@"<input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label>", c.Render (), "R#2");
  148. c.Attributes ["style"] = "color:red;";
  149. c.TextAlign = TextAlign.Left;
  150. Assert.AreEqual (@"<span style=""color:red;""><label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" /></span>",
  151. c.Render (), "R#3");
  152. c.TextAlign = TextAlign.Right;
  153. Assert.AreEqual (@"<span style=""color:red;""><input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label></span>",
  154. c.Render (), "R#4");
  155. c.Attributes ["style"] = null;
  156. c.ForeColor = Color.Red;
  157. c.TextAlign = TextAlign.Left;
  158. Assert.AreEqual (@"<span style=""color:Red;""><label for=""ID"">Text</label><input id=""ID"" type=""checkbox"" name=""ID"" /></span>",
  159. c.Render (), "R#4");
  160. c.TextAlign = TextAlign.Right;
  161. Assert.AreEqual (@"<span style=""color:Red;""><input id=""ID"" type=""checkbox"" name=""ID"" /><label for=""ID"">Text</label></span>",
  162. c.Render (), "R#6");
  163. }
  164. //
  165. // Code like
  166. // if (value == null)
  167. // ViewState.Remove ("Text");
  168. // else
  169. // ViewState ["Text"] = value
  170. // is wrong. We need to store when we are tracking viewstate.
  171. //
  172. // Statebag takes care of this behavior, so the code is not needed.
  173. //
  174. [Test]
  175. public void CheckboxViewstateTextNull ()
  176. {
  177. TestCheckBox c = new TestCheckBox ();
  178. c.Text = "text";
  179. c.TrackState ();
  180. c.Text = null;
  181. Assert.AreEqual ("", c.Text);
  182. object o = c.SaveState ();
  183. c = new TestCheckBox ();
  184. c.Text = "text";
  185. c.TrackState ();
  186. c.LoadState (o);
  187. Assert.AreEqual ("", c.Text);
  188. }
  189. #if NET_2_0
  190. [Test]
  191. public void CheckboxViewstateValidation ()
  192. {
  193. // for some reason, MS doesn't save the validation
  194. // properties to the viewstate for the Checkbox
  195. // control. why not?
  196. TestCheckBox o = new TestCheckBox ();
  197. o.ValidationGroup = "VG1";
  198. o.CausesValidation = true;
  199. o.TrackState ();
  200. Assert.AreEqual ("VG1", o.ValidationGroup, "V1");
  201. Assert.IsTrue (o.CausesValidation, "V2");
  202. object state = o.SaveState ();
  203. TestCheckBox copy = new TestCheckBox ();
  204. copy.LoadState (state);
  205. Assert.AreEqual ("", copy.ValidationGroup, "V3");
  206. Assert.IsFalse (copy.CausesValidation, "V4");
  207. }
  208. #endif
  209. }
  210. }