2 // XhtmlTextWriterTest.cs
3 // - Unit tests for System.Web.UI.XhtmlTextWriter
6 // Cesar Lopez Nataren <cnataren@novell.com>
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
35 using NUnit.Framework;
36 using System.Collections;
38 namespace MonoTests.System.Web.UI {
40 public class XhtmlTextWriterTester : XhtmlTextWriter
42 public XhtmlTextWriterTester (TextWriter writer)
43 : this (writer, HtmlTextWriter.DefaultTabString)
47 public XhtmlTextWriterTester (TextWriter writer, string tabString)
48 : base (writer, tabString)
52 public Hashtable PublicElementSpecificAttributes {
53 get { return ElementSpecificAttributes; }
56 public bool PublicOnStyleAttributeRender (string name, string value, HtmlTextWriterStyle style)
58 return OnStyleAttributeRender (name, value, style);
61 public bool PublicOnAttributeRender (string name, string value, HtmlTextWriterAttribute attr)
63 return OnAttributeRender (name, value, attr);
66 public string PublicGetAttributeName (HtmlTextWriterAttribute attrKey)
68 return GetAttributeName (attrKey);
71 public string PublicGetStyleName (HtmlTextWriterStyle styleKey)
73 return GetStyleName (styleKey);
78 public class XhtmlTextWriterTest {
80 XhtmlTextWriterTester xhtml;
84 string absent_attr = "absent-attr";
85 string a_attr = "accesskey";
88 string elem_name = "a";
89 string absent_elem = "absent-elem";
94 public void SetupTests ()
96 writer = new StringWriter ();
97 xhtml = new XhtmlTextWriterTester (writer);
98 attrs = (Hashtable) xhtml.PublicElementSpecificAttributes;
102 public void AddRecognizedAttributeTest ()
104 Hashtable elem_attrs = (Hashtable) attrs [elem_name];
107 Assertion.AssertEquals ("#A01", null, elem_attrs [absent_attr]);
109 // recently added attr
110 xhtml.AddRecognizedAttribute (elem_name, absent_attr);
111 Assertion.AssertEquals ("A02", true, elem_attrs [absent_attr]);
113 // ensure there's no absent_elem
114 Assertion.AssertEquals ("#A03", null, attrs [absent_elem]);
116 // Given absent_elem and absent_attr, we must add the element
117 // and bind the given attr to it
118 xhtml.AddRecognizedAttribute (absent_elem, absent_attr);
119 Assertion.AssertEquals ("#A04", true, ((Hashtable) attrs [absent_elem]) [absent_attr]);
121 // Given a known element and attribute
125 [ExpectedException (typeof (ArgumentException))]
126 public void AddRecognizedAttributeTest2 ()
128 // if attr is already there we must throw ArgumentException
129 xhtml.AddRecognizedAttribute (elem_name, a_attr);
133 [Ignore ("NUNIT 2.4 issue - temporarily disabled")]
134 public void RemoveRecognizedAttribute ()
137 xhtml.AddRecognizedAttribute (elem_name, absent_attr);
138 Assertion.AssertEquals ("#B01", true, ((Hashtable) attrs [elem_name]) [absent_attr]);
140 // ensure we remove it
141 xhtml.RemoveRecognizedAttribute (elem_name, absent_attr);
142 Assertion.AssertEquals ("#B02", null, ((Hashtable) attrs [elem_name]) [absent_attr]);
144 // if the element does not exist we must resume cleanly
145 xhtml.RemoveRecognizedAttribute (absent_elem, absent_attr);
147 // if the attr does not exist we must resume cleanly
148 xhtml.RemoveRecognizedAttribute (elem_name, a_attr);
152 public void OnStyleAttributeRenderTest ()
156 foreach (HtmlTextWriterStyle style in Enum.GetValues (typeof (HtmlTextWriterStyle)))
157 Assertion.AssertEquals ("#C0" + i++, false,
158 xhtml.PublicOnStyleAttributeRender (xhtml.PublicGetStyleName (style),
163 public void WriteBreakTest ()
166 Assertion.AssertEquals ("#D01", "<br/>", writer.ToString ());
170 public void OnAttributeRenderTest ()
173 Array attrs = Enum.GetValues (typeof (HtmlTextWriterAttribute));
175 foreach (HtmlTextWriterAttribute attr in attrs) {
177 xhtml.PublicOnAttributeRender (xhtml.PublicGetAttributeName (attr), "foo", attr);
178 } catch (ArgumentNullException e) {
182 Assertion.AssertEquals ("#F01", attrs.Length, i);