2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI / ChtmlTextWriterTest.cs
1 //
2 // ChtmlTextWriterTest.cs: Unit tests for System.Web.UI.ChtmlTextWriter
3 //
4 // Author:
5 //      Cesar Lopez Nataren <cnataren@novell.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.IO;
33 using System.Web.UI;
34 using NUnit.Framework;
35 using System.Collections;
36
37 namespace MonoTests.System.Web.UI {
38
39         public class ChtmlTextWriterTester : ChtmlTextWriter {
40
41                 public ChtmlTextWriterTester (TextWriter writer)
42                         : this (writer, DefaultTabString)
43                 {
44                 }
45
46                 public ChtmlTextWriterTester (TextWriter writer, string tabString)
47                         : base (writer, tabString)
48                 {
49                 }
50
51                 public bool IsRecognizedAttribute (string elementName, string attributeName)
52                 {
53                         Hashtable elem_attrs = (Hashtable) RecognizedAttributes [elementName];
54
55                         if (elem_attrs == null)
56                                 return false;
57                         return elem_attrs [attributeName] != null;
58                 }
59
60                 public string PublicGetAttributeName (HtmlTextWriterAttribute attrKey)
61                 {
62                         return GetAttributeName (attrKey);
63                 }
64
65                 public bool PublicOnAttributeRender (string name, string value, HtmlTextWriterAttribute attr)
66                 {
67                         return OnAttributeRender (name, value, attr);
68                 }
69
70                 public bool PublicOnStyleAttributeRender (string name, string value, HtmlTextWriterStyle key)
71                 {
72                         return OnStyleAttributeRender (name, value, key);
73                 }
74
75                 public bool PublicOnTagRender (string name, HtmlTextWriterTag tag)
76                 {
77                         return OnTagRender (name, tag);
78                 }
79
80                 public Hashtable PublicGlobalSuppressedAttributes {
81                         get { return GlobalSuppressedAttributes; }
82                 }
83
84                 public Hashtable PublicSuppressedAttributes {
85                         get { return SuppressedAttributes; }
86                 }
87         }
88
89         [TestFixture]
90         public class ChtmlTextWriterTest {
91
92                 ChtmlTextWriterTester chtml;
93                 StringWriter writer;
94
95                 string absent_element = "absent-elem";
96                 string absent_attr = "absent-attr";
97
98                 [SetUp]
99                 public void SetupTests ()
100                 {
101                         writer = new StringWriter ();
102                         chtml = new ChtmlTextWriterTester (writer);
103                 }
104
105                 [Test]
106                 public void AddRecognizedAttributeTest ()
107                 {
108                         chtml.AddRecognizedAttribute (absent_element, absent_attr);
109                         Assert.AreEqual (true, chtml.IsRecognizedAttribute (absent_element, absent_attr), "#A01");
110                 }
111
112                 [Test]
113                 [ExpectedException (typeof (ArgumentException))]
114                 public void AddRecognizedAttribute2 ()
115                 {
116                         AddRecognizedAttributeTest ();
117                         AddRecognizedAttributeTest ();
118                 }
119
120                 [Test]
121                 public void RemoveRecognizedAttributeTest ()
122                 {
123                         AddRecognizedAttributeTest ();
124
125                         chtml.RemoveRecognizedAttribute (absent_element, absent_attr);
126                         Assert.AreEqual (false, chtml.IsRecognizedAttribute (absent_element, absent_attr), "#B01");
127
128                         string version_two = "v2";
129                         chtml.RemoveRecognizedAttribute (absent_element + version_two, absent_attr + version_two);
130                 }
131
132                 [Test]
133                 public void WriteBreakTest ()
134                 {
135                         string br = "<br>";
136                         chtml.WriteBreak ();
137                         Assert.AreEqual (true, br == writer.ToString (), "#C01");
138                 }
139
140                 [Test]
141                 public void WriteEncodedTest ()
142                 {
143                         string encoded_text = "<custID> & <invoice#>";
144                         string unencoded_text = "&lt;custID&gt; &amp; &lt;invoice#&gt;";
145                         chtml.WriteEncodedText (encoded_text);
146
147                         Assert.AreEqual (true, unencoded_text == writer.ToString (), "#D01");
148                 }
149
150                 [Test]
151                 public void OnAttributeRenderTest ()
152                 {
153                         HtmlTextWriterAttribute [] enum_values = (HtmlTextWriterAttribute []) Enum.GetValues (typeof (HtmlTextWriterAttribute));
154                         int i = 0;
155
156                         foreach (HtmlTextWriterAttribute attr in enum_values) {
157                                 try {
158                                         chtml.PublicOnAttributeRender (chtml.PublicGetAttributeName (attr), "accesskey", attr);
159                                 } catch (ArgumentNullException e) {
160                                         i++;
161                                 }
162                         }
163                         Assert.AreEqual (enum_values.Length, i, "#E01");
164                 }
165
166                 [Test]
167                 public void OnStyleAttributeRenderTest ()
168                 {
169                         bool expected;
170                         int i = 0;
171
172                         foreach (HtmlTextWriterStyle tag in Enum.GetValues (typeof (HtmlTextWriterStyle))) {
173                                 expected = (tag == HtmlTextWriterStyle.Display);
174                                 Assert.AreEqual (expected, chtml.PublicOnStyleAttributeRender ("foo", "foo", tag), "#F0" + i++);
175                         }
176                 }
177
178
179                 [Test]
180                 public void OnTagRenderTest ()
181                 {
182                         int i = 0;
183                         bool expected;
184
185                         foreach (HtmlTextWriterTag tag in Enum.GetValues (typeof (HtmlTextWriterTag))) {
186                                 expected = (tag != HtmlTextWriterTag.Span);
187                                 Assert.AreEqual (expected, chtml.PublicOnTagRender ("foo", tag), "#G0" + i++);
188                         }
189                 }
190         }
191 }
192
193 #endif