HtmlInputControlTest.cs 6.2 KB

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