HtmlInputRadioButtonTest.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // HtmlInputRadioButtonTest.cs
  3. // - Unit tests for System.Web.UI.HtmlControls.HtmlInputRadioButton
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[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.Collections.Specialized;
  31. using System.IO;
  32. using System.Web.UI;
  33. using System.Web.UI.HtmlControls;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Web.UI.HtmlControls {
  36. public class TestHtmlInputRadioButton : HtmlInputRadioButton {
  37. public string RenderAttributes ()
  38. {
  39. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  40. base.RenderAttributes (writer);
  41. return writer.InnerWriter.ToString ();
  42. }
  43. #if NET_2_0
  44. public bool LoadPost (string key, NameValueCollection nvc)
  45. {
  46. return base.LoadPostData (key, nvc);
  47. }
  48. public void Raise ()
  49. {
  50. base.RaisePostDataChangedEvent ();
  51. }
  52. #endif
  53. }
  54. [TestFixture]
  55. public class HtmlInputRadioButtonTest {
  56. private const int defaultAttributesCount = 1;
  57. [Test]
  58. public void DefaultProperties ()
  59. {
  60. HtmlInputRadioButton rb = new HtmlInputRadioButton ();
  61. Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "Attributes.Count");
  62. Assert.IsFalse (rb.Checked, "Checked");
  63. Assert.AreEqual (String.Empty, rb.Name, "Name");
  64. Assert.IsNull (rb.Value, "Value");
  65. Assert.AreEqual ("input", rb.TagName, "TagName");
  66. Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "Attributes.Count-2");
  67. }
  68. [Test]
  69. public void NullProperties ()
  70. {
  71. HtmlInputRadioButton rb = new HtmlInputRadioButton ();
  72. rb.Name = null;
  73. Assert.AreEqual (String.Empty, rb.Name, "Name");
  74. rb.Value = null;
  75. Assert.IsNull (rb.Value, "Value");
  76. Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "Attributes.Count");
  77. }
  78. [Test]
  79. public void CleanProperties ()
  80. {
  81. HtmlInputRadioButton rb = new HtmlInputRadioButton ();
  82. rb.Checked = true;
  83. Assert.IsTrue (rb.Checked, "Checked");
  84. rb.Name = "name";
  85. Assert.AreEqual ("name", rb.Name, "Name");
  86. rb.Value = "value";
  87. Assert.AreEqual ("value", rb.Value, "Value");
  88. Assert.AreEqual (defaultAttributesCount + 3, rb.Attributes.Count, "1");
  89. rb.Checked = false;
  90. Assert.IsFalse (rb.Checked, "-Checked");
  91. rb.Name = null;
  92. Assert.AreEqual (String.Empty, rb.Name, "-Name");
  93. rb.Value = null;
  94. Assert.IsNull (rb.Value, "-Value");
  95. Assert.AreEqual (defaultAttributesCount, rb.Attributes.Count, "0");
  96. }
  97. [Test]
  98. public void Value_Existing ()
  99. {
  100. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  101. rb.Value = "a";
  102. Assert.AreEqual ("a", rb.Value, "Value before");
  103. rb.ID = "id1";
  104. Assert.AreEqual ("id1", rb.ID, "ID");
  105. Assert.AreEqual ("a", rb.Value, "Value after");
  106. }
  107. [Test]
  108. public void Value_Resetting ()
  109. {
  110. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  111. rb.ID = "id1";
  112. Assert.AreEqual ("id1", rb.Value, "Value before");
  113. rb.Value = "a";
  114. Assert.AreEqual ("id1", rb.ID, "ID");
  115. Assert.AreEqual ("a", rb.Value, "Value after");
  116. }
  117. [Test]
  118. public void Value_ResetNull ()
  119. {
  120. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  121. rb.Value = "a";
  122. rb.ID = "id1";
  123. rb.Value = null;
  124. Assert.AreEqual ("id1", rb.ID, "ID");
  125. Assert.AreEqual ("id1", rb.Value, "Value");
  126. }
  127. [Test]
  128. // note: this behaviour isn't present in HtmlInputControl
  129. public void IDversusValue ()
  130. {
  131. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  132. Assert.IsNull (rb.Value, "Value before");
  133. rb.ID = "id1";
  134. Assert.AreEqual ("id1", rb.ID, "ID");
  135. Assert.AreEqual ("id1", rb.Value, "Value after");
  136. }
  137. [Test]
  138. [Ignore ("throws NullReferenceException")]
  139. public void RenderAttributes ()
  140. {
  141. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  142. rb.Checked = true;
  143. rb.Name = "mono";
  144. rb.Value = "value";
  145. rb.RenderAttributes ();
  146. }
  147. private bool serverChange;
  148. private void ServerChange (object sender, EventArgs e)
  149. {
  150. serverChange = true;
  151. }
  152. [Test]
  153. public void IPostBackDataHandler_RaisePostBackEvent ()
  154. {
  155. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  156. rb.ServerChange += new EventHandler (ServerChange);
  157. IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
  158. serverChange = false;
  159. pbdh.RaisePostDataChangedEvent ();
  160. Assert.IsTrue (serverChange, "ServerChange");
  161. }
  162. [Test]
  163. [ExpectedException (typeof (NullReferenceException))]
  164. public void IPostBackDataHandler_LoadPostData_NullCollection ()
  165. {
  166. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  167. IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
  168. pbdh.LoadPostData ("id1", null);
  169. }
  170. [Test]
  171. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  172. public void IPostBackDataHandler_LoadPostData_IdNull ()
  173. {
  174. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  175. rb.ID = "id1";
  176. IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
  177. NameValueCollection nvc = new NameValueCollection ();
  178. nvc.Add ("id1", "mono");
  179. Assert.IsFalse (pbdh.LoadPostData (null, nvc), "LoadPostData");
  180. Assert.AreEqual ("id1", rb.Value, "Value");
  181. }
  182. [Test]
  183. public void IPostBackDataHandler_LoadPostData_WrongId ()
  184. {
  185. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  186. rb.ID = "id1";
  187. IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
  188. NameValueCollection nvc = new NameValueCollection ();
  189. nvc.Add ("id1", "mono");
  190. Assert.IsFalse (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
  191. Assert.AreEqual ("id1", rb.Value, "Value");
  192. }
  193. [Test]
  194. public void IPostBackDataHandler_LoadPostData ()
  195. {
  196. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  197. rb.ID = "id1";
  198. IPostBackDataHandler pbdh = (rb as IPostBackDataHandler);
  199. NameValueCollection nvc = new NameValueCollection ();
  200. nvc.Add ("id1", "id1");
  201. // we didn't change the state of the control
  202. Assert.IsFalse (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
  203. Assert.AreEqual ("id1", rb.Value, "Value");
  204. }
  205. #if NET_2_0
  206. [Test]
  207. public void RaisePostBackEvent ()
  208. {
  209. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  210. rb.ServerChange += new EventHandler (ServerChange);
  211. serverChange = false;
  212. rb.Raise ();
  213. Assert.IsTrue (serverChange, "ServerClick");
  214. }
  215. [Test]
  216. [ExpectedException (typeof (NullReferenceException))]
  217. public void LoadPostData_NullCollection ()
  218. {
  219. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  220. rb.LoadPost ("id1", null);
  221. }
  222. [Test]
  223. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  224. public void LoadPostData_IdNull ()
  225. {
  226. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  227. rb.ID = "id1";
  228. NameValueCollection nvc = new NameValueCollection ();
  229. nvc.Add ("id1", "mono");
  230. Assert.IsFalse (rb.LoadPost (null, nvc), "LoadPostData");
  231. Assert.AreEqual ("id1", rb.Value, "Value");
  232. }
  233. [Test]
  234. public void LoadPostData_WrongId ()
  235. {
  236. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  237. rb.ID = "id1";
  238. NameValueCollection nvc = new NameValueCollection ();
  239. nvc.Add ("id1", "mono");
  240. Assert.IsFalse (rb.LoadPost ("id2", nvc), "LoadPostData");
  241. Assert.AreEqual ("id1", rb.Value, "Value");
  242. }
  243. [Test]
  244. public void LoadPostData ()
  245. {
  246. TestHtmlInputRadioButton rb = new TestHtmlInputRadioButton ();
  247. rb.ID = "id1";
  248. NameValueCollection nvc = new NameValueCollection ();
  249. nvc.Add ("id1", "mono");
  250. Assert.IsFalse (rb.LoadPost ("id1", nvc), "LoadPostData");
  251. Assert.AreEqual ("id1", rb.Value, "Value");
  252. }
  253. #endif
  254. }
  255. }