XhtmlTextWriterTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // XhtmlTextWriterTest.cs
  3. // - Unit tests for System.Web.UI.XhtmlTextWriter
  4. //
  5. // Author:
  6. // Cesar Lopez Nataren <[email protected]>
  7. //
  8. // Copyright (C) 2006 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. #if NET_2_0
  30. using System;
  31. using System.IO;
  32. using System.Web.UI;
  33. using NUnit.Framework;
  34. using System.Collections;
  35. namespace MonoTests.System.Web.UI {
  36. public class XhtmlTextWriterTester : XhtmlTextWriter
  37. {
  38. public XhtmlTextWriterTester (TextWriter writer)
  39. : this (writer, HtmlTextWriter.DefaultTabString)
  40. {
  41. }
  42. public XhtmlTextWriterTester (TextWriter writer, string tabString)
  43. : base (writer, tabString)
  44. {
  45. }
  46. public Hashtable PublicElementSpecificAttributes {
  47. get { return ElementSpecificAttributes; }
  48. }
  49. public bool PublicOnStyleAttributeRender (string name, string value, HtmlTextWriterStyle style)
  50. {
  51. return OnStyleAttributeRender (name, value, style);
  52. }
  53. public bool PublicOnAttributeRender (string name, string value, HtmlTextWriterAttribute attr)
  54. {
  55. return OnAttributeRender (name, value, attr);
  56. }
  57. public string PublicGetAttributeName (HtmlTextWriterAttribute attrKey)
  58. {
  59. return GetAttributeName (attrKey);
  60. }
  61. public string PublicGetStyleName (HtmlTextWriterStyle styleKey)
  62. {
  63. return GetStyleName (styleKey);
  64. }
  65. }
  66. [TestFixture]
  67. public class XhtmlTextWriterTest {
  68. XhtmlTextWriterTester xhtml;
  69. StringWriter writer;
  70. // attributes
  71. string absent_attr = "absent-attr";
  72. string a_attr = "accesskey";
  73. // elements
  74. string elem_name = "a";
  75. string absent_elem = "absent-elem";
  76. Hashtable attrs;
  77. [SetUp]
  78. public void SetupTests ()
  79. {
  80. writer = new StringWriter ();
  81. xhtml = new XhtmlTextWriterTester (writer);
  82. attrs = (Hashtable) xhtml.PublicElementSpecificAttributes;
  83. }
  84. [Test]
  85. public void AddRecognizedAttributeTest ()
  86. {
  87. Hashtable elem_attrs = (Hashtable) attrs [elem_name];
  88. // absent attr
  89. Assert.AreEqual (null, elem_attrs [absent_attr], "#A01");
  90. // recently added attr
  91. xhtml.AddRecognizedAttribute (elem_name, absent_attr);
  92. Assert.AreEqual (true, elem_attrs [absent_attr], "A02");
  93. // ensure there's no absent_elem
  94. Assert.AreEqual (null, attrs [absent_elem], "#A03");
  95. // Given absent_elem and absent_attr, we must add the element
  96. // and bind the given attr to it
  97. xhtml.AddRecognizedAttribute (absent_elem, absent_attr);
  98. Assert.AreEqual (true, ((Hashtable) attrs [absent_elem]) [absent_attr], "#A04");
  99. // Given a known element and attribute
  100. }
  101. [Test]
  102. [ExpectedException (typeof (ArgumentException))]
  103. public void AddRecognizedAttributeTest2 ()
  104. {
  105. // if attr is already there we must throw ArgumentException
  106. xhtml.AddRecognizedAttribute (elem_name, a_attr);
  107. }
  108. [Test]
  109. [Ignore ("NUNIT 2.4 issue - temporarily disabled")]
  110. public void RemoveRecognizedAttribute ()
  111. {
  112. // ensure we add it
  113. xhtml.AddRecognizedAttribute (elem_name, absent_attr);
  114. Assert.AreEqual (true, ((Hashtable) attrs [elem_name]) [absent_attr], "#B01");
  115. // ensure we remove it
  116. xhtml.RemoveRecognizedAttribute (elem_name, absent_attr);
  117. Assert.AreEqual (null, ((Hashtable) attrs [elem_name]) [absent_attr], "#B02");
  118. // if the element does not exist we must resume cleanly
  119. xhtml.RemoveRecognizedAttribute (absent_elem, absent_attr);
  120. // if the attr does not exist we must resume cleanly
  121. xhtml.RemoveRecognizedAttribute (elem_name, a_attr);
  122. }
  123. [Test]
  124. public void OnStyleAttributeRenderTest ()
  125. {
  126. int i = 0;
  127. foreach (HtmlTextWriterStyle style in Enum.GetValues (typeof (HtmlTextWriterStyle)))
  128. Assert.AreEqual (false,
  129. xhtml.PublicOnStyleAttributeRender (xhtml.PublicGetStyleName (style),
  130. "foo", style), "#C0" + i++);
  131. }
  132. [Test]
  133. public void WriteBreakTest ()
  134. {
  135. xhtml.WriteBreak ();
  136. Assert.AreEqual ("<br/>", writer.ToString (), "#D01");
  137. }
  138. [Test]
  139. public void OnAttributeRenderTest ()
  140. {
  141. int i = 0;
  142. Array attrs = Enum.GetValues (typeof (HtmlTextWriterAttribute));
  143. foreach (HtmlTextWriterAttribute attr in attrs) {
  144. try {
  145. xhtml.PublicOnAttributeRender (xhtml.PublicGetAttributeName (attr), "foo", attr);
  146. } catch (ArgumentNullException e) {
  147. i++;
  148. }
  149. }
  150. Assert.AreEqual (attrs.Length, i, "#F01");
  151. }
  152. }
  153. }
  154. #endif