HtmlInputPasswordTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // HtmlInputPasswordTest.cs
  3. // - Unit tests for System.Web.UI.HtmlControls.HtmlInputPassword
  4. //
  5. // Author:
  6. // Chris Toshok <[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. #if NET_2_0
  36. namespace MonoTests.System.Web.UI.HtmlControls {
  37. public class TestHtmlInputPassword : HtmlInputPassword {
  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 TestHtmlInputPassword ()
  43. : base ()
  44. {
  45. }
  46. public TestHtmlInputPassword (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 HtmlInputPasswordTest {
  91. private const int defaultAttributesCount = 0;
  92. [Test]
  93. public void ConstructorType ()
  94. {
  95. HtmlInputPassword it = new HtmlInputPassword ("mono");
  96. Assert.AreEqual ("mono", it.Type, "Type");
  97. }
  98. [Test]
  99. public void DefaultProperties ()
  100. {
  101. HtmlInputPassword it = new HtmlInputPassword ();
  102. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
  103. Assert.AreEqual ("password", it.Type, "Type");
  104. Assert.AreEqual (String.Empty, it.Value, "Value");
  105. Assert.AreEqual ("input", it.TagName, "TagName");
  106. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count-2");
  107. }
  108. [Test]
  109. public void RenderAttributes ()
  110. {
  111. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  112. it.MaxLength = 4;
  113. it.Size = 2;
  114. it.Name = "mono";
  115. it.Value = "value";
  116. Assert.AreEqual (" name type=\"password\" maxlength=\"4\" size=\"2\" /", it.RenderAttributes ());
  117. Assert.IsTrue (it.ViewStateValueChanged, "ViewStateValueChanged");
  118. Assert.IsTrue (it.AttributeValueChanged, "AttributeValueChanged");
  119. }
  120. private bool serverChange;
  121. private void ServerChange (object sender, EventArgs e)
  122. {
  123. serverChange = true;
  124. }
  125. [Test]
  126. public void IPostBackDataHandler_RaisePostBackEvent ()
  127. {
  128. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  129. it.ServerChange += new EventHandler (ServerChange);
  130. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  131. serverChange = false;
  132. pbdh.RaisePostDataChangedEvent ();
  133. Assert.IsTrue (serverChange, "ServerChange");
  134. }
  135. [Test]
  136. [ExpectedException (typeof (NullReferenceException))]
  137. public void IPostBackDataHandler_LoadPostData_NullCollection ()
  138. {
  139. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  140. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  141. pbdh.LoadPostData ("id1", null);
  142. }
  143. [Test]
  144. public void IPostBackDataHandler_LoadPostData ()
  145. {
  146. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  147. it.ID = "id1";
  148. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  149. NameValueCollection nvc = new NameValueCollection ();
  150. nvc.Add ("id1", "mono");
  151. Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
  152. Assert.AreEqual ("mono", it.Value, "Value");
  153. }
  154. [Test]
  155. public void RaisePostBackEvent ()
  156. {
  157. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  158. it.ServerChange += new EventHandler (ServerChange);
  159. serverChange = false;
  160. it.Raise ();
  161. Assert.IsTrue (serverChange, "ServerClick");
  162. }
  163. [Test]
  164. [ExpectedException (typeof (NullReferenceException))]
  165. public void LoadPostData_NullCollection ()
  166. {
  167. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  168. it.LoadPost ("id1", null);
  169. }
  170. [Test]
  171. public void LoadPostData ()
  172. {
  173. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  174. it.ID = "id1";
  175. NameValueCollection nvc = new NameValueCollection ();
  176. nvc.Add ("id1", "mono");
  177. Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
  178. Assert.AreEqual ("mono", it.Value, "Value");
  179. }
  180. }
  181. }
  182. #endif