HtmlInputControlTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // HtmlInputControlTest.cs
  3. // - Unit tests for System.Web.UI.HtmlControls.HtmlInputControl
  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.IO;
  31. using System.Web.UI;
  32. using System.Web.UI.HtmlControls;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Web.UI.HtmlControls {
  35. public class TestHtmlInputControl : HtmlInputControl {
  36. bool name_called;
  37. public TestHtmlInputControl ()
  38. : base ("mono")
  39. {
  40. }
  41. public TestHtmlInputControl (string type)
  42. : base (type)
  43. {
  44. }
  45. public string RenderAttributes ()
  46. {
  47. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  48. base.RenderAttributes (writer);
  49. return writer.InnerWriter.ToString ();
  50. }
  51. public override string Name {
  52. get {
  53. name_called = true;
  54. return base.Name;
  55. }
  56. }
  57. public bool NameCalled {
  58. get { return name_called; }
  59. set { name_called = value; }
  60. }
  61. }
  62. public class UControl : UserControl {
  63. }
  64. [TestFixture]
  65. public class HtmlInputControlTest {
  66. #if NET_2_0
  67. private const int defaultAttributesCount = 0;
  68. #else
  69. // type is kept in Attributes under 1.x
  70. private const int defaultAttributesCount = 1;
  71. #endif
  72. [Test]
  73. public void DefaultProperties ()
  74. {
  75. TestHtmlInputControl ic = new TestHtmlInputControl ();
  76. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count");
  77. Assert.IsNull (ic.Name, "Name");
  78. Assert.AreEqual ("mono", ic.Type, "Type");
  79. Assert.AreEqual (String.Empty, ic.Value, "Value");
  80. Assert.AreEqual ("input", ic.TagName, "TagName");
  81. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count-2");
  82. }
  83. [Test]
  84. public void NullProperties ()
  85. {
  86. TestHtmlInputControl ic = new TestHtmlInputControl ();
  87. ic.Name = null;
  88. Assert.IsNull (ic.Name, "Name");
  89. ic.Value = null;
  90. Assert.AreEqual (String.Empty, ic.Value, "Value");
  91. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count");
  92. }
  93. [Test]
  94. public void CleanProperties ()
  95. {
  96. TestHtmlInputControl ic = new TestHtmlInputControl ();
  97. ic.Name = "name";
  98. Assert.IsNull (ic.Name, "Name");
  99. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "always null");
  100. ic.Value = "value";
  101. Assert.AreEqual ("value", ic.Value, "Value");
  102. Assert.AreEqual (defaultAttributesCount + 1, ic.Attributes.Count, "1");
  103. ic.Name = null;
  104. Assert.IsNull (ic.Name, "-Name");
  105. ic.Value = null;
  106. Assert.AreEqual (String.Empty, ic.Value, "-Value");
  107. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "0");
  108. }
  109. [Test]
  110. public void Name ()
  111. {
  112. TestHtmlInputControl ic = new TestHtmlInputControl ();
  113. Assert.IsNull (ic.UniqueID, "UniqueID");
  114. Assert.IsNull (ic.ID, "ID");
  115. ic.Name = "name";
  116. Assert.IsNull (ic.Name, "Name");
  117. ic.ID = "id";
  118. Assert.AreEqual ("id", ic.ID, "ID-2");
  119. Assert.AreEqual ("id", ic.UniqueID, "UniqueID");
  120. Assert.AreEqual ("id", ic.Name, "Name-ID");
  121. ic.Name = "name";
  122. Assert.AreEqual ("id", ic.Name, "Name-ID-2");
  123. Assert.AreEqual ("id", ic.UniqueID, "UniqueID-2");
  124. ic.ID = null;
  125. Assert.IsNull (ic.ID, "ID-3");
  126. Assert.IsNull (ic.UniqueID, "UniqueID-3");
  127. Assert.IsNull (ic.Name, "Name-2");
  128. }
  129. [Test]
  130. public void Name_InsideNaming ()
  131. {
  132. Control ctrl = new UControl ();
  133. ctrl.ID = "parent";
  134. TestHtmlInputControl ic = new TestHtmlInputControl ();
  135. ctrl.Controls.Add (ic);
  136. Assert.IsNull (ic.ID, "ID");
  137. Assert.AreEqual (false, ic.NameCalled);
  138. ic.Name = "name";
  139. Assert.AreEqual (ic.Name, ic.UniqueID, "name and unique id");
  140. Assert.AreEqual (true, ic.NameCalled, "name called");
  141. ic.ID = "id";
  142. Assert.AreEqual ("id", ic.ID, "ID-2");
  143. Assert.AreEqual (ic.UniqueID, ic.Name, "Name-ID");
  144. ic.Name = "name";
  145. Assert.AreEqual (ic.Name, ic.UniqueID, "UniqueID-2");
  146. ic.ID = null;
  147. Assert.IsNull (ic.ID, "ID-3");
  148. Assert.IsNotNull (ic.UniqueID, "UniqueID-3");
  149. Assert.IsNotNull (ic.Name, "Name-2");
  150. }
  151. [Test]
  152. public void IDversusValue ()
  153. {
  154. TestHtmlInputControl ic = new TestHtmlInputControl ();
  155. Assert.AreEqual (String.Empty, ic.Value, "Value before");
  156. ic.ID = "id1";
  157. Assert.AreEqual ("id1", ic.ID, "ID");
  158. Assert.AreEqual (String.Empty, ic.Value, "Value after");
  159. // HtmlInputRadioButton has a different behaviour
  160. }
  161. [Test]
  162. public void RenderAttributes ()
  163. {
  164. TestHtmlInputControl ic = new TestHtmlInputControl ("test");
  165. ic.Name = "mono";
  166. ic.Value = "value";
  167. Assert.AreEqual (" name type=\"test\" value=\"value\" /", ic.RenderAttributes ());
  168. ic.ID = "toto";
  169. #if NET_2_0
  170. Assert.AreEqual (" name=\"toto\" type=\"test\" id=\"toto\" value=\"value\" /", ic.RenderAttributes ());
  171. #else
  172. Assert.AreEqual (" name=\"toto\" id=\"toto\" type=\"test\" value=\"value\" /", ic.RenderAttributes ());
  173. #endif
  174. }
  175. [Test]
  176. public void Constructor_Null ()
  177. {
  178. TestHtmlInputControl ic = new TestHtmlInputControl (null);
  179. Assert.AreEqual (String.Empty, ic.Type, "Type");
  180. }
  181. [Test]
  182. public void Password ()
  183. {
  184. TestHtmlInputControl ic = new TestHtmlInputControl ("password");
  185. ic.Name = "mono";
  186. ic.Value = "s3kr3t";
  187. // logic to hide password isn't in HtmlInputControl
  188. Assert.AreEqual (" name type=\"password\" value=\"s3kr3t\" /", ic.RenderAttributes ());
  189. }
  190. }
  191. }