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