HtmlInputRadioButtonTest.cs 8.6 KB

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