2
0

HtmlInputTextTest.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. public void RenderAttributes_Password ()
  167. {
  168. TestHtmlInputText it = new TestHtmlInputText ("password");
  169. it.MaxLength = 2;
  170. it.Size = 4;
  171. it.ID = "mono";
  172. it.Value = "s3kr3t";
  173. #if NET_2_0
  174. // value is there, maybe because a new HtmlInputPassword class exists ?
  175. Assert.AreEqual (" name=\"mono\" type=\"password\" id=\"mono\" maxlength=\"2\" size=\"4\" value=\"s3kr3t\" /", it.RenderAttributes ());
  176. Assert.IsFalse (it.ViewStateValueChanged, "ViewStateValueChanged");
  177. Assert.IsFalse (it.AttributeValueChanged, "AttributeValueChanged");
  178. #else
  179. Assert.AreEqual (" name=\"mono\" id=\"mono\" type=\"password\" maxlength=\"2\" size=\"4\" /", it.RenderAttributes ());
  180. Assert.IsTrue (it.ViewStateValueChanged, "ViewStateValueChanged");
  181. Assert.IsTrue (it.AttributeValueChanged, "AttributeValueChanged");
  182. #endif
  183. Assert.IsNull (it.ViewStateNewValue, "ViewStateNewValue");
  184. Assert.IsNull (it.AttributeNewValue, "AttributeNewValue");
  185. }
  186. private bool serverChange;
  187. private void ServerChange (object sender, EventArgs e)
  188. {
  189. serverChange = true;
  190. }
  191. [Test]
  192. public void IPostBackDataHandler_RaisePostBackEvent ()
  193. {
  194. TestHtmlInputText it = new TestHtmlInputText ("password");
  195. it.ServerChange += new EventHandler (ServerChange);
  196. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  197. serverChange = false;
  198. pbdh.RaisePostDataChangedEvent ();
  199. Assert.IsTrue (serverChange, "ServerChange");
  200. }
  201. [Test]
  202. [ExpectedException (typeof (NullReferenceException))]
  203. public void IPostBackDataHandler_LoadPostData_NullCollection ()
  204. {
  205. TestHtmlInputText it = new TestHtmlInputText ("password");
  206. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  207. pbdh.LoadPostData ("id1", null);
  208. }
  209. [Test]
  210. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  211. public void IPostBackDataHandler_LoadPostData_IdNull ()
  212. {
  213. TestHtmlInputText it = new TestHtmlInputText ("password");
  214. it.ID = "id1";
  215. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  216. NameValueCollection nvc = new NameValueCollection ();
  217. nvc.Add ("id1", "mono");
  218. Assert.IsTrue (pbdh.LoadPostData (null, nvc), "LoadPostData");
  219. Assert.AreEqual (String.Empty, it.Value, "Value");
  220. }
  221. [Test]
  222. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  223. public void IPostBackDataHandler_LoadPostData_WrongId ()
  224. {
  225. TestHtmlInputText it = new TestHtmlInputText ("password");
  226. it.ID = "id1";
  227. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  228. NameValueCollection nvc = new NameValueCollection ();
  229. nvc.Add ("id1", "mono");
  230. Assert.IsTrue (pbdh.LoadPostData ("id2", nvc), "LoadPostData");
  231. Assert.AreEqual (String.Empty, it.Value, "Value");
  232. }
  233. [Test]
  234. public void IPostBackDataHandler_LoadPostData ()
  235. {
  236. TestHtmlInputText it = new TestHtmlInputText ("password");
  237. it.ID = "id1";
  238. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  239. NameValueCollection nvc = new NameValueCollection ();
  240. nvc.Add ("id1", "mono");
  241. Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
  242. Assert.AreEqual ("mono", it.Value, "Value");
  243. }
  244. #if NET_2_0
  245. [Test]
  246. public void RaisePostBackEvent ()
  247. {
  248. TestHtmlInputText it = new TestHtmlInputText ("password");
  249. it.ServerChange += new EventHandler (ServerChange);
  250. serverChange = false;
  251. it.Raise ();
  252. Assert.IsTrue (serverChange, "ServerClick");
  253. }
  254. [Test]
  255. [ExpectedException (typeof (NullReferenceException))]
  256. public void LoadPostData_NullCollection ()
  257. {
  258. TestHtmlInputText it = new TestHtmlInputText ("password");
  259. it.LoadPost ("id1", null);
  260. }
  261. [Test]
  262. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  263. public void LoadPostData_IdNull ()
  264. {
  265. TestHtmlInputText it = new TestHtmlInputText ("password");
  266. it.ID = "id1";
  267. NameValueCollection nvc = new NameValueCollection ();
  268. nvc.Add ("id1", "mono");
  269. Assert.IsTrue (it.LoadPost (null, nvc), "LoadPostData");
  270. Assert.AreEqual (String.Empty, it.Value, "Value");
  271. }
  272. [Test]
  273. [Category ("NotDotNet")] // MS throws a NullReferenceException here
  274. public void LoadPostData_WrongId ()
  275. {
  276. TestHtmlInputText it = new TestHtmlInputText ("password");
  277. it.ID = "id1";
  278. NameValueCollection nvc = new NameValueCollection ();
  279. nvc.Add ("id1", "mono");
  280. Assert.IsTrue (it.LoadPost ("id2", nvc), "LoadPostData");
  281. Assert.AreEqual (String.Empty, it.Value, "Value");
  282. }
  283. [Test]
  284. public void LoadPostData ()
  285. {
  286. TestHtmlInputText it = new TestHtmlInputText ("password");
  287. it.ID = "id1";
  288. NameValueCollection nvc = new NameValueCollection ();
  289. nvc.Add ("id1", "mono");
  290. Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
  291. Assert.AreEqual ("mono", it.Value, "Value");
  292. }
  293. #endif
  294. }
  295. }