XhtmlTextWriterTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. Assertion.AssertEquals ("#A01", null, elem_attrs [absent_attr]);
  90. // recently added attr
  91. xhtml.AddRecognizedAttribute (elem_name, absent_attr);
  92. Assertion.AssertEquals ("A02", true, elem_attrs [absent_attr]);
  93. // ensure there's no absent_elem
  94. Assertion.AssertEquals ("#A03", null, attrs [absent_elem]);
  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. Assertion.AssertEquals ("#A04", true, ((Hashtable) attrs [absent_elem]) [absent_attr]);
  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. public void RemoveRecognizedAttribute ()
  110. {
  111. // ensure we add it
  112. xhtml.AddRecognizedAttribute (elem_name, absent_attr);
  113. Assertion.AssertEquals ("#B01", true, ((Hashtable) attrs [elem_name]) [absent_attr]);
  114. // ensure we remove it
  115. xhtml.RemoveRecognizedAttribute (elem_name, absent_attr);
  116. Assertion.AssertEquals ("#B02", null, ((Hashtable) attrs [elem_name]) [absent_attr]);
  117. // if the element does not exist we must resume cleanly
  118. xhtml.RemoveRecognizedAttribute (absent_elem, absent_attr);
  119. // if the attr does not exist we must resume cleanly
  120. xhtml.RemoveRecognizedAttribute (elem_name, a_attr);
  121. }
  122. [Test]
  123. public void OnStyleAttributeRenderTest ()
  124. {
  125. int i = 0;
  126. foreach (HtmlTextWriterStyle style in Enum.GetValues (typeof (HtmlTextWriterStyle)))
  127. Assertion.AssertEquals ("#C0" + i++, false,
  128. xhtml.PublicOnStyleAttributeRender (xhtml.PublicGetStyleName (style),
  129. "foo", style));
  130. }
  131. [Test]
  132. public void WriteBreakTest ()
  133. {
  134. xhtml.WriteBreak ();
  135. Assertion.AssertEquals ("#D01", "<br/>", writer.ToString ());
  136. }
  137. [Test]
  138. public void OnAttributeRenderTest ()
  139. {
  140. int i = 0;
  141. Array attrs = Enum.GetValues (typeof (HtmlTextWriterAttribute));
  142. foreach (HtmlTextWriterAttribute attr in attrs) {
  143. try {
  144. xhtml.PublicOnAttributeRender (xhtml.PublicGetAttributeName (attr), "foo", attr);
  145. } catch (ArgumentNullException e) {
  146. i++;
  147. }
  148. }
  149. Assertion.AssertEquals ("#F01", attrs.Length, i);
  150. }
  151. }
  152. }
  153. #endif