HtmlInputTextTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. private const int defaultAttributesCount = 1;
  91. [Test]
  92. public void ConstructorType ()
  93. {
  94. HtmlInputText it = new HtmlInputText ("mono");
  95. Assert.AreEqual ("mono", it.Type, "Type");
  96. }
  97. [Test]
  98. public void DefaultProperties ()
  99. {
  100. HtmlInputText it = new HtmlInputText ();
  101. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
  102. Assert.AreEqual (-1, it.MaxLength, "MaxLength");
  103. Assert.IsNull (it.Name, "Name");
  104. Assert.AreEqual (-1, it.Size, "Size");
  105. Assert.AreEqual ("text", it.Type, "Type");
  106. Assert.AreEqual (String.Empty, it.Value, "Value");
  107. Assert.AreEqual ("input", it.TagName, "TagName");
  108. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count-2");
  109. }
  110. [Test]
  111. public void NullProperties ()
  112. {
  113. HtmlInputText it = new HtmlInputText ();
  114. it.MaxLength = -1;
  115. Assert.AreEqual (-1, it.MaxLength, "MaxLength");
  116. it.Name = null;
  117. Assert.IsNull (it.Name, "Name");
  118. it.Size = -1;
  119. Assert.AreEqual (-1, it.Size, "Size");
  120. it.Value = null;
  121. Assert.AreEqual (String.Empty, it.Value, "Value");
  122. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
  123. }
  124. [Test]
  125. public void CleanProperties ()
  126. {
  127. HtmlInputText it = new HtmlInputText ();
  128. it.MaxLength = 1;
  129. Assert.AreEqual (1, it.MaxLength, "MaxLength");
  130. it.Name = "name";
  131. Assert.IsNull (it.Name, "Name");
  132. it.Size = 2;
  133. Assert.AreEqual (2, it.Size, "Size");
  134. it.Value = "value";
  135. Assert.AreEqual ("value", it.Value, "Value");
  136. Assert.AreEqual (defaultAttributesCount + 3, it.Attributes.Count, "1");
  137. it.MaxLength = -1;
  138. Assert.AreEqual (-1, it.MaxLength, "-MaxLength");
  139. it.Name = null;
  140. Assert.IsNull (it.Name, "-Name");
  141. it.Size = -1;
  142. Assert.AreEqual (-1, it.Size, "Size");
  143. it.Value = null;
  144. Assert.AreEqual (String.Empty, it.Value, "-Value");
  145. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "0");
  146. }
  147. [Test]
  148. public void Password ()
  149. {
  150. TestHtmlInputText it = new TestHtmlInputText ("password");
  151. it.Value = "s3kr3t";
  152. it.ID = "passwd";
  153. Assert.AreEqual ("s3kr3t", it.Value, "Value");
  154. }
  155. [Test]
  156. public void RenderAttributes ()
  157. {
  158. TestHtmlInputText it = new TestHtmlInputText ();
  159. it.MaxLength = 4;
  160. it.Size = 2;
  161. it.Name = "mono";
  162. it.Value = "value";
  163. Assert.AreEqual (" name type=\"text\" maxlength=\"4\" size=\"2\" value=\"value\" /", it.RenderAttributes ());
  164. }
  165. [Test]
  166. [Category ("NotWorking")]
  167. public void RenderAttributes_Password ()
  168. {
  169. TestHtmlInputText it = new TestHtmlInputText ("password");
  170. it.MaxLength = 2;
  171. it.Size = 4;
  172. it.ID = "mono";
  173. it.Value = "s3kr3t";
  174. #if NET_2_0
  175. // value is there, maybe because a new HtmlInputPassword class exists ?
  176. Assert.AreEqual (" name=\"mono\" type=\"password\" id=\"mono\" maxlength=\"2\" size=\"4\" value=\"s3kr3t\" /", it.RenderAttributes ());
  177. Assert.IsFalse (it.ViewStateValueChanged, "ViewStateValueChanged");
  178. Assert.IsFalse (it.AttributeValueChanged, "AttributeValueChanged");
  179. #else
  180. Assert.AreEqual (" name=\"mono\" id=\"mono\" type=\"password\" maxlength=\"2\" size=\"4\" /", it.RenderAttributes ());
  181. Assert.IsTrue (it.ViewStateValueChanged, "ViewStateValueChanged");
  182. Assert.IsTrue (it.AttributeValueChanged, "AttributeValueChanged");
  183. #endif
  184. Assert.IsNull (it.ViewStateNewValue, "ViewStateNewValue");
  185. Assert.IsNull (it.AttributeNewValue, "AttributeNewValue");
  186. }
  187. private bool serverChange;
  188. private void ServerChange (object sender, EventArgs e)
  189. {
  190. serverChange = true;
  191. }
  192. [Test]
  193. public void IPostBackDataHandler_RaisePostBackEvent ()
  194. {
  195. TestHtmlInputText it = new TestHtmlInputText ("password");
  196. it.ServerChange += new EventHandler (ServerChange);
  197. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  198. serverChange = false;
  199. pbdh.RaisePostDataChangedEvent ();
  200. Assert.IsTrue (serverChange, "ServerChange");
  201. }
  202. [Test]
  203. [ExpectedException (typeof (NullReferenceException))]
  204. public void IPostBackDataHandler_LoadPostData_NullCollection ()
  205. {
  206. TestHtmlInputText it = new TestHtmlInputText ("password");
  207. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  208. pbdh.LoadPostData ("id1", null);
  209. }
  210. [Test]
  211. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  212. public void IPostBackDataHandler_LoadPostData_IdNull ()
  213. {
  214. TestHtmlInputText it = new TestHtmlInputText ("password");
  215. it.ID = "id1";
  216. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  217. NameValueCollection nvc = new NameValueCollection ();
  218. nvc.Add ("id1", "mono");
  219. Assert.IsTrue (pbdh.LoadPostData (null, nvc), "LoadPostData");
  220. Assert.AreEqual (String.Empty, it.Value, "Value");
  221. }
  222. [Test]
  223. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  224. public void IPostBackDataHandler_LoadPostData_WrongId ()
  225. {
  226. TestHtmlInputText it = new TestHtmlInputText ("password");
  227. it.ID = "id1";
  228. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  229. NameValueCollection nvc = new NameValueCollection ();
  230. nvc.Add ("id1", "mono");
  231. Assert.IsTrue (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
  232. Assert.AreEqual (String.Empty, it.Value, "Value");
  233. }
  234. [Test]
  235. public void IPostBackDataHandler_LoadPostData ()
  236. {
  237. TestHtmlInputText it = new TestHtmlInputText ("password");
  238. it.ID = "id1";
  239. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  240. NameValueCollection nvc = new NameValueCollection ();
  241. nvc.Add ("id1", "mono");
  242. Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
  243. Assert.AreEqual ("mono", it.Value, "Value");
  244. }
  245. #if NET_2_0
  246. [Test]
  247. public void RaisePostBackEvent ()
  248. {
  249. TestHtmlInputText it = new TestHtmlInputText ("password");
  250. it.ServerChange += new EventHandler (ServerChange);
  251. serverChange = false;
  252. it.Raise ();
  253. Assert.IsTrue (serverChange, "ServerClick");
  254. }
  255. [Test]
  256. [ExpectedException (typeof (NullReferenceException))]
  257. public void LoadPostData_NullCollection ()
  258. {
  259. TestHtmlInputText it = new TestHtmlInputText ("password");
  260. it.LoadPost ("id1", null);
  261. }
  262. [Test]
  263. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  264. public void LoadPostData_IdNull ()
  265. {
  266. TestHtmlInputText it = new TestHtmlInputText ("password");
  267. it.ID = "id1";
  268. NameValueCollection nvc = new NameValueCollection ();
  269. nvc.Add ("id1", "mono");
  270. Assert.IsTrue (it.LoadPost (null, nvc), "LoadPostData");
  271. Assert.AreEqual (String.Empty, it.Value, "Value");
  272. }
  273. [Test]
  274. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  275. public void LoadPostData_WrongId ()
  276. {
  277. TestHtmlInputText it = new TestHtmlInputText ("password");
  278. it.ID = "id1";
  279. NameValueCollection nvc = new NameValueCollection ();
  280. nvc.Add ("id1", "mono");
  281. Assert.IsTrue (it.LoadPost ("id2", nvc), "LoadPostData");
  282. Assert.AreEqual (String.Empty, it.Value, "Value");
  283. }
  284. [Test]
  285. public void LoadPostData ()
  286. {
  287. TestHtmlInputText it = new TestHtmlInputText ("password");
  288. it.ID = "id1";
  289. NameValueCollection nvc = new NameValueCollection ();
  290. nvc.Add ("id1", "mono");
  291. Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
  292. Assert.AreEqual ("mono", it.Value, "Value");
  293. }
  294. #endif
  295. }
  296. }