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