HtmlInputTextTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // HtmlInputTextTest.cs
  3. // - Unit tests for System.Web.UI.HtmlControls.HtmlInputText
  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 TestHtmlInputText : HtmlInputText {
  37. bool value_changed; // true if the "value" is changed in RenderAttributes
  38. string new_value; // "value" in ViewState if value_changed is true.
  39. bool attr_value_changed; // same but for attributes (instead of viewstate)
  40. string attr_new_value;
  41. public TestHtmlInputText ()
  42. : base ()
  43. {
  44. }
  45. public TestHtmlInputText (string type)
  46. : base (type)
  47. {
  48. }
  49. public string RenderAttributes ()
  50. {
  51. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  52. string val = (string) ViewState ["value"];
  53. string att = Attributes ["value"];
  54. base.RenderAttributes (writer);
  55. if (val != (string) ViewState ["value"]) {
  56. value_changed = true;
  57. new_value = (string) ViewState ["value"];
  58. }
  59. if (att != Attributes ["value"]) {
  60. attr_value_changed = true;
  61. attr_new_value = Attributes ["value"];
  62. }
  63. return writer.InnerWriter.ToString ();
  64. }
  65. public bool ViewStateValueChanged {
  66. get { return value_changed; }
  67. }
  68. public string ViewStateNewValue {
  69. get { return new_value; }
  70. }
  71. public bool AttributeValueChanged {
  72. get { return attr_value_changed; }
  73. }
  74. public string AttributeNewValue {
  75. get { return attr_new_value; }
  76. }
  77. #if NET_2_0
  78. public bool LoadPost (string key, NameValueCollection nvc)
  79. {
  80. return base.LoadPostData (key, nvc);
  81. }
  82. public void Raise ()
  83. {
  84. base.RaisePostDataChangedEvent ();
  85. }
  86. #endif
  87. }
  88. [TestFixture]
  89. public class HtmlInputTextTest {
  90. #if NET_2_0
  91. private const int defaultAttributesCount = 0;
  92. #else
  93. // type is kept in Attributes under 1.x
  94. private const int defaultAttributesCount = 1;
  95. #endif
  96. [Test]
  97. public void ConstructorType ()
  98. {
  99. HtmlInputText it = new HtmlInputText ("mono");
  100. Assert.AreEqual ("mono", it.Type, "Type");
  101. }
  102. [Test]
  103. public void DefaultProperties ()
  104. {
  105. HtmlInputText it = new HtmlInputText ();
  106. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
  107. Assert.AreEqual (-1, it.MaxLength, "MaxLength");
  108. Assert.IsNull (it.Name, "Name");
  109. Assert.AreEqual (-1, it.Size, "Size");
  110. Assert.AreEqual ("text", it.Type, "Type");
  111. Assert.AreEqual (String.Empty, it.Value, "Value");
  112. Assert.AreEqual ("input", it.TagName, "TagName");
  113. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count-2");
  114. }
  115. [Test]
  116. public void NullProperties ()
  117. {
  118. HtmlInputText it = new HtmlInputText ();
  119. it.MaxLength = -1;
  120. Assert.AreEqual (-1, it.MaxLength, "MaxLength");
  121. it.Name = null;
  122. Assert.IsNull (it.Name, "Name");
  123. it.Size = -1;
  124. Assert.AreEqual (-1, it.Size, "Size");
  125. it.Value = null;
  126. Assert.AreEqual (String.Empty, it.Value, "Value");
  127. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
  128. }
  129. [Test]
  130. public void CleanProperties ()
  131. {
  132. HtmlInputText it = new HtmlInputText ();
  133. it.MaxLength = 1;
  134. Assert.AreEqual (1, it.MaxLength, "MaxLength");
  135. it.Name = "name";
  136. Assert.IsNull (it.Name, "Name");
  137. it.Size = 2;
  138. Assert.AreEqual (2, it.Size, "Size");
  139. it.Value = "value";
  140. Assert.AreEqual ("value", it.Value, "Value");
  141. Assert.AreEqual (defaultAttributesCount + 3, it.Attributes.Count, "1");
  142. it.MaxLength = -1;
  143. Assert.AreEqual (-1, it.MaxLength, "-MaxLength");
  144. it.Name = null;
  145. Assert.IsNull (it.Name, "-Name");
  146. it.Size = -1;
  147. Assert.AreEqual (-1, it.Size, "Size");
  148. it.Value = null;
  149. Assert.AreEqual (String.Empty, it.Value, "-Value");
  150. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "0");
  151. }
  152. [Test]
  153. public void Password ()
  154. {
  155. TestHtmlInputText it = new TestHtmlInputText ("password");
  156. it.Value = "s3kr3t";
  157. it.ID = "passwd";
  158. Assert.AreEqual ("s3kr3t", it.Value, "Value");
  159. }
  160. [Test]
  161. public void RenderAttributes ()
  162. {
  163. TestHtmlInputText it = new TestHtmlInputText ();
  164. it.MaxLength = 4;
  165. it.Size = 2;
  166. it.Name = "mono";
  167. it.Value = "value";
  168. Assert.AreEqual (" name type=\"text\" maxlength=\"4\" size=\"2\" value=\"value\" /", it.RenderAttributes ());
  169. }
  170. [Test]
  171. public void RenderAttributes_Password ()
  172. {
  173. TestHtmlInputText it = new TestHtmlInputText ("password");
  174. it.MaxLength = 2;
  175. it.Size = 4;
  176. it.ID = "mono";
  177. it.Value = "s3kr3t";
  178. #if NET_2_0
  179. // value is there, maybe because a new HtmlInputPassword class exists ?
  180. Assert.AreEqual (" name=\"mono\" type=\"password\" id=\"mono\" maxlength=\"2\" size=\"4\" value=\"s3kr3t\" /", it.RenderAttributes ());
  181. Assert.IsFalse (it.ViewStateValueChanged, "ViewStateValueChanged");
  182. Assert.IsFalse (it.AttributeValueChanged, "AttributeValueChanged");
  183. #else
  184. Assert.AreEqual (" name=\"mono\" id=\"mono\" type=\"password\" maxlength=\"2\" size=\"4\" /", it.RenderAttributes ());
  185. Assert.IsTrue (it.ViewStateValueChanged, "ViewStateValueChanged");
  186. Assert.IsTrue (it.AttributeValueChanged, "AttributeValueChanged");
  187. #endif
  188. Assert.IsNull (it.ViewStateNewValue, "ViewStateNewValue");
  189. Assert.IsNull (it.AttributeNewValue, "AttributeNewValue");
  190. }
  191. private bool serverChange;
  192. private void ServerChange (object sender, EventArgs e)
  193. {
  194. serverChange = true;
  195. }
  196. [Test]
  197. public void IPostBackDataHandler_RaisePostBackEvent ()
  198. {
  199. TestHtmlInputText it = new TestHtmlInputText ("password");
  200. it.ServerChange += new EventHandler (ServerChange);
  201. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  202. serverChange = false;
  203. pbdh.RaisePostDataChangedEvent ();
  204. Assert.IsTrue (serverChange, "ServerChange");
  205. }
  206. [Test]
  207. [ExpectedException (typeof (NullReferenceException))]
  208. public void IPostBackDataHandler_LoadPostData_NullCollection ()
  209. {
  210. TestHtmlInputText it = new TestHtmlInputText ("password");
  211. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  212. pbdh.LoadPostData ("id1", null);
  213. }
  214. [Test]
  215. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  216. public void IPostBackDataHandler_LoadPostData_IdNull ()
  217. {
  218. TestHtmlInputText it = new TestHtmlInputText ("password");
  219. it.ID = "id1";
  220. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  221. NameValueCollection nvc = new NameValueCollection ();
  222. nvc.Add ("id1", "mono");
  223. Assert.IsTrue (pbdh.LoadPostData (null, nvc), "LoadPostData");
  224. Assert.AreEqual (String.Empty, it.Value, "Value");
  225. }
  226. [Test]
  227. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  228. public void IPostBackDataHandler_LoadPostData_WrongId ()
  229. {
  230. TestHtmlInputText it = new TestHtmlInputText ("password");
  231. it.ID = "id1";
  232. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  233. NameValueCollection nvc = new NameValueCollection ();
  234. nvc.Add ("id1", "mono");
  235. Assert.IsTrue (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
  236. Assert.AreEqual (String.Empty, it.Value, "Value");
  237. }
  238. [Test]
  239. public void IPostBackDataHandler_LoadPostData ()
  240. {
  241. TestHtmlInputText it = new TestHtmlInputText ("password");
  242. it.ID = "id1";
  243. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  244. NameValueCollection nvc = new NameValueCollection ();
  245. nvc.Add ("id1", "mono");
  246. Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
  247. Assert.AreEqual ("mono", it.Value, "Value");
  248. }
  249. #if NET_2_0
  250. [Test]
  251. public void RaisePostBackEvent ()
  252. {
  253. TestHtmlInputText it = new TestHtmlInputText ("password");
  254. it.ServerChange += new EventHandler (ServerChange);
  255. serverChange = false;
  256. it.Raise ();
  257. Assert.IsTrue (serverChange, "ServerClick");
  258. }
  259. [Test]
  260. [ExpectedException (typeof (NullReferenceException))]
  261. public void LoadPostData_NullCollection ()
  262. {
  263. TestHtmlInputText it = new TestHtmlInputText ("password");
  264. it.LoadPost ("id1", null);
  265. }
  266. [Test]
  267. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  268. public void LoadPostData_IdNull ()
  269. {
  270. TestHtmlInputText it = new TestHtmlInputText ("password");
  271. it.ID = "id1";
  272. NameValueCollection nvc = new NameValueCollection ();
  273. nvc.Add ("id1", "mono");
  274. Assert.IsTrue (it.LoadPost (null, nvc), "LoadPostData");
  275. Assert.AreEqual (String.Empty, it.Value, "Value");
  276. }
  277. [Test]
  278. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  279. public void LoadPostData_WrongId ()
  280. {
  281. TestHtmlInputText it = new TestHtmlInputText ("password");
  282. it.ID = "id1";
  283. NameValueCollection nvc = new NameValueCollection ();
  284. nvc.Add ("id1", "mono");
  285. Assert.IsTrue (it.LoadPost ("id2", nvc), "LoadPostData");
  286. Assert.AreEqual (String.Empty, it.Value, "Value");
  287. }
  288. [Test]
  289. public void LoadPostData ()
  290. {
  291. TestHtmlInputText it = new TestHtmlInputText ("password");
  292. it.ID = "id1";
  293. NameValueCollection nvc = new NameValueCollection ();
  294. nvc.Add ("id1", "mono");
  295. Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
  296. Assert.AreEqual ("mono", it.Value, "Value");
  297. }
  298. #endif
  299. }
  300. }