ControlTest.cs added new tests and resources
[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                 public void RemoveRecognizedAttribute ()
134                 {
135                         // ensure we add it
136                         xhtml.AddRecognizedAttribute (elem_name, absent_attr);
137                         Assertion.AssertEquals ("#B01", true, ((Hashtable) attrs [elem_name]) [absent_attr]);
138
139                         // ensure we remove it
140                         xhtml.RemoveRecognizedAttribute (elem_name, absent_attr);
141                         Assertion.AssertEquals ("#B02", null, ((Hashtable) attrs [elem_name]) [absent_attr]);
142
143                         // if the element does not exist we must resume cleanly
144                         xhtml.RemoveRecognizedAttribute (absent_elem, absent_attr);
145
146                         // if the attr does not exist we must resume cleanly
147                         xhtml.RemoveRecognizedAttribute (elem_name, a_attr);
148                 }
149
150                 [Test]
151                 public void OnStyleAttributeRenderTest ()
152                 {
153                         int i = 0;
154
155                         foreach (HtmlTextWriterStyle style in Enum.GetValues (typeof (HtmlTextWriterStyle)))
156                                 Assertion.AssertEquals ("#C0" + i++, false,
157                                                 xhtml.PublicOnStyleAttributeRender (xhtml.PublicGetStyleName (style), 
158                                                                             "foo", style));
159                 }
160
161                 [Test]
162                 public void WriteBreakTest ()
163                 {
164                         xhtml.WriteBreak ();
165                         Assertion.AssertEquals ("#D01", "<br/>", writer.ToString ());
166                 }
167
168                 [Test]
169                 public void OnAttributeRenderTest ()
170                 {
171                         int i = 0;
172                         Array attrs = Enum.GetValues (typeof (HtmlTextWriterAttribute));
173
174                         foreach (HtmlTextWriterAttribute attr in attrs) {
175                                 try {
176                                         xhtml.PublicOnAttributeRender (xhtml.PublicGetAttributeName (attr), "foo", attr);
177                                 } catch (ArgumentNullException e) {
178                                         i++;
179                                 }
180                         }
181                         Assertion.AssertEquals ("#F01", attrs.Length, i);
182                 }
183         }
184 }
185
186 #endif