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