HtmlInputControlTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. public TestHtmlInputControl ()
  37. : base ("mono")
  38. {
  39. }
  40. public TestHtmlInputControl (string type)
  41. : base (type)
  42. {
  43. }
  44. public string RenderAttributes ()
  45. {
  46. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  47. base.RenderAttributes (writer);
  48. return writer.InnerWriter.ToString ();
  49. }
  50. }
  51. [TestFixture]
  52. public class HtmlInputControlTest {
  53. #if NET_2_0
  54. private const int defaultAttributesCount = 0;
  55. #else
  56. // type is kept in Attributes under 1.x
  57. private const int defaultAttributesCount = 1;
  58. #endif
  59. [Test]
  60. public void DefaultProperties ()
  61. {
  62. TestHtmlInputControl ic = new TestHtmlInputControl ();
  63. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count");
  64. Assert.IsNull (ic.Name, "Name");
  65. Assert.AreEqual ("mono", ic.Type, "Type");
  66. Assert.AreEqual (String.Empty, ic.Value, "Value");
  67. Assert.AreEqual ("input", ic.TagName, "TagName");
  68. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count-2");
  69. }
  70. [Test]
  71. public void NullProperties ()
  72. {
  73. TestHtmlInputControl ic = new TestHtmlInputControl ();
  74. ic.Name = null;
  75. Assert.IsNull (ic.Name, "Name");
  76. ic.Value = null;
  77. Assert.AreEqual (String.Empty, ic.Value, "Value");
  78. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "Attributes.Count");
  79. }
  80. [Test]
  81. public void CleanProperties ()
  82. {
  83. TestHtmlInputControl ic = new TestHtmlInputControl ();
  84. ic.Name = "name";
  85. Assert.IsNull (ic.Name, "Name");
  86. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "always null");
  87. ic.Value = "value";
  88. Assert.AreEqual ("value", ic.Value, "Value");
  89. Assert.AreEqual (defaultAttributesCount + 1, ic.Attributes.Count, "1");
  90. ic.Name = null;
  91. Assert.IsNull (ic.Name, "-Name");
  92. ic.Value = null;
  93. Assert.AreEqual (String.Empty, ic.Value, "-Value");
  94. Assert.AreEqual (defaultAttributesCount, ic.Attributes.Count, "0");
  95. }
  96. [Test]
  97. public void Name ()
  98. {
  99. TestHtmlInputControl ic = new TestHtmlInputControl ();
  100. Assert.IsNull (ic.UniqueID, "UniqueID");
  101. Assert.IsNull (ic.ID, "ID");
  102. ic.Name = "name";
  103. Assert.IsNull (ic.Name, "Name");
  104. ic.ID = "id";
  105. Assert.AreEqual ("id", ic.ID, "ID-2");
  106. Assert.AreEqual ("id", ic.UniqueID, "UniqueID");
  107. Assert.AreEqual ("id", ic.Name, "Name-ID");
  108. ic.Name = "name";
  109. Assert.AreEqual ("id", ic.Name, "Name-ID-2");
  110. Assert.AreEqual ("id", ic.UniqueID, "UniqueID-2");
  111. ic.ID = null;
  112. Assert.IsNull (ic.ID, "ID-3");
  113. Assert.IsNull (ic.UniqueID, "UniqueID-3");
  114. Assert.IsNull (ic.Name, "Name-2");
  115. }
  116. [Test]
  117. public void IDversusValue ()
  118. {
  119. TestHtmlInputControl ic = new TestHtmlInputControl ();
  120. Assert.AreEqual (String.Empty, ic.Value, "Value before");
  121. ic.ID = "id1";
  122. Assert.AreEqual ("id1", ic.ID, "ID");
  123. Assert.AreEqual (String.Empty, ic.Value, "Value after");
  124. // HtmlInputRadioButton has a different behaviour
  125. }
  126. [Test]
  127. public void RenderAttributes ()
  128. {
  129. TestHtmlInputControl ic = new TestHtmlInputControl ("test");
  130. ic.Name = "mono";
  131. ic.Value = "value";
  132. Assert.AreEqual (" name type=\"test\" value=\"value\" /", ic.RenderAttributes ());
  133. ic.ID = "toto";
  134. #if NET_2_0
  135. Assert.AreEqual (" name=\"toto\" type=\"test\" id=\"toto\" value=\"value\" /", ic.RenderAttributes ());
  136. #else
  137. Assert.AreEqual (" name=\"toto\" id=\"toto\" type=\"test\" value=\"value\" /", ic.RenderAttributes ());
  138. #endif
  139. }
  140. [Test]
  141. public void Constructor_Null ()
  142. {
  143. TestHtmlInputControl ic = new TestHtmlInputControl (null);
  144. Assert.AreEqual (String.Empty, ic.Type, "Type");
  145. }
  146. [Test]
  147. public void Password ()
  148. {
  149. TestHtmlInputControl ic = new TestHtmlInputControl ("password");
  150. ic.Name = "mono";
  151. ic.Value = "s3kr3t";
  152. // logic to hide password isn't in HtmlInputControl
  153. Assert.AreEqual (" name type=\"password\" value=\"s3kr3t\" /", ic.RenderAttributes ());
  154. }
  155. }
  156. }