HtmlInputControlTest.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. private const int defaultAttributesCount = 1;
  67. [Test]
  68. public void DefaultProperties ()
  69. {
  70. TestHtmlInputControl ic = new TestHtmlInputControl ();
  71. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count");
  72. Assert.IsNull (ic.Name, "Name");
  73. Assert.AreEqual ("mono", ic.Type, "Type");
  74. Assert.AreEqual (String.Empty, ic.Value, "Value");
  75. Assert.AreEqual ("input", ic.TagName, "TagName");
  76. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count-2");
  77. }
  78. [Test]
  79. public void NullProperties ()
  80. {
  81. TestHtmlInputControl ic = new TestHtmlInputControl ();
  82. ic.Name = null;
  83. Assert.IsNull (ic.Name, "Name");
  84. ic.Value = null;
  85. Assert.AreEqual (String.Empty, ic.Value, "Value");
  86. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count");
  87. }
  88. [Test]
  89. public void CleanProperties ()
  90. {
  91. TestHtmlInputControl ic = new TestHtmlInputControl ();
  92. ic.Name = "name";
  93. Assert.IsNull (ic.Name, "Name");
  94. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "always null");
  95. ic.Value = "value";
  96. Assert.AreEqual ("value", ic.Value, "Value");
  97. Assert.AreEqual (defaultAttributesCount + 1, ic.Attributes.Count, "1");
  98. ic.Name = null;
  99. Assert.IsNull (ic.Name, "-Name");
  100. ic.Value = null;
  101. Assert.AreEqual (String.Empty, ic.Value, "-Value");
  102. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "0");
  103. }
  104. [Test]
  105. public void Name ()
  106. {
  107. TestHtmlInputControl ic = new TestHtmlInputControl ();
  108. Assert.IsNull (ic.UniqueID, "UniqueID");
  109. Assert.IsNull (ic.ID, "ID");
  110. ic.Name = "name";
  111. Assert.IsNull (ic.Name, "Name");
  112. ic.ID = "id";
  113. Assert.AreEqual ("id", ic.ID, "ID-2");
  114. Assert.AreEqual ("id", ic.UniqueID, "UniqueID");
  115. Assert.AreEqual ("id", ic.Name, "Name-ID");
  116. ic.Name = "name";
  117. Assert.AreEqual ("id", ic.Name, "Name-ID-2");
  118. Assert.AreEqual ("id", ic.UniqueID, "UniqueID-2");
  119. ic.ID = null;
  120. Assert.IsNull (ic.ID, "ID-3");
  121. Assert.IsNull (ic.UniqueID, "UniqueID-3");
  122. Assert.IsNull (ic.Name, "Name-2");
  123. }
  124. [Test]
  125. public void Name_InsideNaming ()
  126. {
  127. Control ctrl = new UControl ();
  128. ctrl.ID = "parent";
  129. TestHtmlInputControl ic = new TestHtmlInputControl ();
  130. ctrl.Controls.Add (ic);
  131. Assert.IsNull (ic.ID, "ID");
  132. Assert.AreEqual (false, ic.NameCalled);
  133. ic.Name = "name";
  134. Assert.AreEqual (ic.Name, ic.UniqueID, "name and unique id");
  135. Assert.AreEqual (true, ic.NameCalled, "name called");
  136. ic.ID = "id";
  137. Assert.AreEqual ("id", ic.ID, "ID-2");
  138. Assert.AreEqual (ic.UniqueID, ic.Name, "Name-ID");
  139. ic.Name = "name";
  140. Assert.AreEqual (ic.Name, ic.UniqueID, "UniqueID-2");
  141. ic.ID = null;
  142. Assert.IsNull (ic.ID, "ID-3");
  143. Assert.IsNotNull (ic.UniqueID, "UniqueID-3");
  144. Assert.IsNotNull (ic.Name, "Name-2");
  145. }
  146. [Test]
  147. public void IDversusValue ()
  148. {
  149. TestHtmlInputControl ic = new TestHtmlInputControl ();
  150. Assert.AreEqual (String.Empty, ic.Value, "Value before");
  151. ic.ID = "id1";
  152. Assert.AreEqual ("id1", ic.ID, "ID");
  153. Assert.AreEqual (String.Empty, ic.Value, "Value after");
  154. // HtmlInputRadioButton has a different behaviour
  155. }
  156. [Test]
  157. public void RenderAttributes ()
  158. {
  159. TestHtmlInputControl ic = new TestHtmlInputControl ("test");
  160. ic.Name = "mono";
  161. ic.Value = "value";
  162. Assert.AreEqual (" name type=\"test\" value=\"value\" /", ic.RenderAttributes ());
  163. ic.ID = "toto";
  164. #if NET_2_0
  165. Assert.AreEqual (" name=\"toto\" type=\"test\" id=\"toto\" value=\"value\" /", ic.RenderAttributes ());
  166. #else
  167. Assert.AreEqual (" name=\"toto\" id=\"toto\" type=\"test\" value=\"value\" /", ic.RenderAttributes ());
  168. #endif
  169. }
  170. [Test]
  171. public void Constructor_Null ()
  172. {
  173. TestHtmlInputControl ic = new TestHtmlInputControl (null);
  174. Assert.AreEqual (String.Empty, ic.Type, "Type");
  175. }
  176. [Test]
  177. public void Password ()
  178. {
  179. TestHtmlInputControl ic = new TestHtmlInputControl ("password");
  180. ic.Name = "mono";
  181. ic.Value = "s3kr3t";
  182. // logic to hide password isn't in HtmlInputControl
  183. Assert.AreEqual (" name type=\"password\" value=\"s3kr3t\" /", ic.RenderAttributes ());
  184. }
  185. }
  186. }