HtmlInputPasswordTest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #if NET_2_0
  30. using System;
  31. using System.Collections.Specialized;
  32. using System.IO;
  33. using System.Web.UI;
  34. using System.Web.UI.HtmlControls;
  35. using NUnit.Framework;
  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 string RenderAttributes ()
  47. {
  48. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  49. string val = (string) ViewState ["value"];
  50. string att = Attributes ["value"];
  51. base.RenderAttributes (writer);
  52. if (val != (string) ViewState ["value"]) {
  53. value_changed = true;
  54. new_value = (string) ViewState ["value"];
  55. }
  56. if (att != Attributes ["value"]) {
  57. attr_value_changed = true;
  58. attr_new_value = Attributes ["value"];
  59. }
  60. return writer.InnerWriter.ToString ();
  61. }
  62. public bool ViewStateValueChanged {
  63. get { return value_changed; }
  64. }
  65. public string ViewStateNewValue {
  66. get { return new_value; }
  67. }
  68. public bool AttributeValueChanged {
  69. get { return attr_value_changed; }
  70. }
  71. public string AttributeNewValue {
  72. get { return attr_new_value; }
  73. }
  74. public bool LoadPost (string key, NameValueCollection nvc)
  75. {
  76. return base.LoadPostData (key, nvc);
  77. }
  78. public void Raise ()
  79. {
  80. base.RaisePostDataChangedEvent ();
  81. }
  82. }
  83. [TestFixture]
  84. public class HtmlInputPasswordTest {
  85. private const int defaultAttributesCount = 1;
  86. [Test]
  87. public void DefaultProperties ()
  88. {
  89. HtmlInputPassword it = new HtmlInputPassword ();
  90. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count");
  91. Assert.AreEqual ("password", it.Type, "Type");
  92. Assert.AreEqual (String.Empty, it.Value, "Value");
  93. Assert.AreEqual ("input", it.TagName, "TagName");
  94. Assert.AreEqual (defaultAttributesCount, it.Attributes.Count, "Attributes.Count-2");
  95. }
  96. [Test]
  97. public void RenderAttributes ()
  98. {
  99. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  100. it.MaxLength = 4;
  101. it.Size = 2;
  102. it.Name = "mono";
  103. it.Value = "value";
  104. Assert.AreEqual (" name type=\"password\" maxlength=\"4\" size=\"2\" /", it.RenderAttributes ());
  105. Assert.IsTrue (it.ViewStateValueChanged, "ViewStateValueChanged");
  106. Assert.IsTrue (it.AttributeValueChanged, "AttributeValueChanged");
  107. }
  108. private bool serverChange;
  109. private void ServerChange (object sender, EventArgs e)
  110. {
  111. serverChange = true;
  112. }
  113. [Test]
  114. public void IPostBackDataHandler_RaisePostBackEvent ()
  115. {
  116. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  117. it.ServerChange += new EventHandler (ServerChange);
  118. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  119. serverChange = false;
  120. pbdh.RaisePostDataChangedEvent ();
  121. Assert.IsTrue (serverChange, "ServerChange");
  122. }
  123. [Test]
  124. [ExpectedException (typeof (NullReferenceException))]
  125. public void IPostBackDataHandler_LoadPostData_NullCollection ()
  126. {
  127. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  128. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  129. pbdh.LoadPostData ("id1", null);
  130. }
  131. [Test]
  132. public void IPostBackDataHandler_LoadPostData ()
  133. {
  134. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  135. it.ID = "id1";
  136. IPostBackDataHandler pbdh = (it as IPostBackDataHandler);
  137. NameValueCollection nvc = new NameValueCollection ();
  138. nvc.Add ("id1", "mono");
  139. Assert.IsTrue (pbdh.LoadPostData ("id1", nvc), "LoadPostData");
  140. Assert.AreEqual ("mono", it.Value, "Value");
  141. }
  142. [Test]
  143. public void RaisePostBackEvent ()
  144. {
  145. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  146. it.ServerChange += new EventHandler (ServerChange);
  147. serverChange = false;
  148. it.Raise ();
  149. Assert.IsTrue (serverChange, "ServerClick");
  150. }
  151. [Test]
  152. [ExpectedException (typeof (NullReferenceException))]
  153. public void LoadPostData_NullCollection ()
  154. {
  155. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  156. it.LoadPost ("id1", null);
  157. }
  158. [Test]
  159. public void LoadPostData ()
  160. {
  161. TestHtmlInputPassword it = new TestHtmlInputPassword ();
  162. it.ID = "id1";
  163. NameValueCollection nvc = new NameValueCollection ();
  164. nvc.Add ("id1", "mono");
  165. Assert.IsTrue (it.LoadPost ("id1", nvc), "LoadPostData");
  166. Assert.AreEqual ("mono", it.Value, "Value");
  167. }
  168. }
  169. }
  170. #endif