update readme (#21797)
[mono-project.git] / mcs / class / System.Web / Test / System.Web.UI / HtmlTextWriterTest.cs
blobce7399b4942aefc72bc08869835f8e2740172487
1 //
2 // HtmlTextWriterTest.cs
3 // - Unit tests for System.Web.UI.HtmlTextWriter
4 //
5 // Authors:
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Ben Maurer <bmaurer@novell.com>
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System;
32 using System.IO;
33 using System.Web.UI;
35 using HtwTag = System.Web.UI.HtmlTextWriterTag;
36 using HtwAttribute = System.Web.UI.HtmlTextWriterAttribute;
37 using HtwStyle = System.Web.UI.HtmlTextWriterStyle;
39 using NUnit.Framework;
41 namespace MonoTests.System.Web.UI {
43 [TestFixture]
44 public class HtmlTextWriterTest {
46 StringWriter sw;
47 HtmlTextWriter w;
49 [SetUp]
50 public void SetupTests ()
52 sw = new StringWriter ();
53 sw.NewLine = "\n"; // Keep sanity.
54 w = new HtmlTextWriter (sw);
57 [Test]
58 public void DefaultConstFields ()
60 Assert.AreEqual ("=\"", HtmlTextWriter.EqualsDoubleQuoteString, "EqualsDoubleQuoteString");
61 // add more
64 [Test]
65 public void NullWriter ()
67 HtmlTextWriter htw = new HtmlTextWriter (null);
68 Assert.IsNotNull (htw, "NullWriter");
69 Assert.IsNull (htw.InnerWriter, "InnerWriter");
72 [Test]
73 public void WriteAttributes_NullValue ()
75 w.WriteAttribute ("name", null);
76 Assert.AreEqual (" name", w.InnerWriter.ToString ());
79 [Test]
80 public void Write1 ()
82 w.RenderBeginTag ("a");
83 w.RenderEndTag ();
85 Assert.AreEqual ("<a></a>", sw.ToString ());
88 [Test]
89 public void WriteATagByKey ()
91 w.RenderBeginTag (HtwTag.A);
92 w.RenderEndTag ();
94 Assert.AreEqual ("<a></a>", sw.ToString ());
97 [Test]
98 public void WriteDivTagByKey ()
100 w.RenderBeginTag (HtwTag.Div);
101 w.RenderEndTag ();
103 Assert.AreEqual ("<div>\n\n</div>", sw.ToString ());
106 [Test]
107 public void WriteOptionTagByKey ()
109 w.RenderBeginTag (HtwTag.Option);
110 w.RenderEndTag ();
112 Assert.AreEqual ("<option>\n\n</option>", sw.ToString ());
115 [Test]
116 public void WriteDivAndATagByKey ()
118 w.RenderBeginTag (HtwTag.Div);
119 w.RenderBeginTag (HtwTag.A);
120 w.RenderEndTag ();
121 w.RenderEndTag ();
123 Assert.AreEqual ("<div>\n\t<a></a>\n</div>", sw.ToString ());
126 [Test]
127 public void NestedIndent ()
129 w.RenderBeginTag (HtwTag.Div);
130 w.RenderBeginTag (HtwTag.Div);
131 w.RenderEndTag ();
132 w.RenderEndTag ();
134 Assert.AreEqual ("<div>\n\t<div>\n\n\t</div>\n</div>", sw.ToString ());
137 [Test]
138 public void WriteDivAndATagByKeyWithAttr ()
140 w.RenderBeginTag (HtwTag.Div);
142 w.AddAttribute (HtwAttribute.Href, "http://slashdot.org/");
143 w.RenderBeginTag (HtwTag.A);
144 w.RenderEndTag ();
145 w.RenderEndTag ();
146 Assert.AreEqual ("<div>\n\t<a href=\"http://slashdot.org/\"></a>\n</div>", sw.ToString ());
149 [Test]
150 public void WriteDivTagWithStyle ()
152 w.AddAttribute ("id", "foo");
153 w.AddAttribute ("style", "align:left");
154 w.AddStyleAttribute (HtwStyle.Color, "red");
155 w.AddStyleAttribute (HtwStyle.FontWeight, "bold");
156 w.RenderBeginTag (HtwTag.Div);
157 w.RenderEndTag ();
158 Assert.AreEqual ("<div id=\"foo\" style=\"color:red;font-weight:bold;align:left\">\n\n</div>", sw.ToString ());
161 [Test]
162 public void EscapeJScript ()
164 w.AddAttribute (HtwAttribute.Onclick, "this.style.color = \"red\"");
165 w.RenderBeginTag (HtwTag.Div);
166 w.RenderEndTag ();
167 Assert.AreEqual ("<div onclick=\"this.style.color = &quot;red&quot;\">\n\n</div>", sw.ToString ());
170 [Test]
171 public void EscapeUrl ()
173 w.AddAttribute (HtwAttribute.Href, "http://www.example.com/search?hl=en&q=i+love+nunit&btnG=Google+Search");
174 w.RenderBeginTag (HtwTag.A);
175 w.RenderEndTag ();
176 Assert.AreEqual ("<a href=\"http://www.example.com/search?hl=en&amp;q=i+love+nunit&amp;btnG=Google+Search\"></a>", sw.ToString ());
179 // Which attrs fall here
180 [Test]
181 public void NoEscapeAttrName ()
183 w.AddAttribute (HtwAttribute.Name, "cookies&cream");
184 w.RenderBeginTag (HtwTag.Div);
185 w.RenderEndTag ();
186 Assert.AreEqual ("<div name=\"cookies&cream\">\n\n</div>", sw.ToString ());
189 [Test]
190 public void NoEscapeAttrName2 () {
191 w.AddAttribute ("name", "cookies&cream");
192 w.RenderBeginTag (HtwTag.Div);
193 w.RenderEndTag ();
194 Assert.AreEqual ("<div name=\"cookies&cream\">\n\n</div>", sw.ToString ());
197 [Test]
198 public void NoEscapeAttrName3 () {
199 w.AddAttribute (HtwAttribute.Name, "cookies&cream", true);
200 w.RenderBeginTag (HtwTag.Div);
201 w.RenderEndTag ();
202 Assert.AreEqual ("<div name=\"cookies&amp;cream\">\n\n</div>", sw.ToString ());
205 [Test]
206 public void NoEscapeAttrName4 () {
207 w.AddAttribute ("NaMe", "cookies&cream");
208 w.RenderBeginTag (HtwTag.Div);
209 w.RenderEndTag ();
210 Assert.AreEqual ("<div NaMe=\"cookies&cream\">\n\n</div>", sw.ToString ());
213 [Test]
214 public void EscapeAttribute1 () {
215 sw = new StringWriter ();
216 sw.NewLine = "\n"; // Keep sanity.
217 Poker w1 = new Poker (sw);
219 w1.AddAttribute ("attr", "cookies&cream");
220 w1.RenderBeginTag (HtwTag.Div);
221 w1.RenderEndTag ();
223 Assert.AreEqual ("cookies&amp;cream", w1.AttrValue_At_AddAttribute, "AttrValue_At_AddAttribute");
224 Assert.AreEqual ("cookies&amp;cream", w1.AttrValue_At_OnAttributeRender, "AttrValue_At_OnAttributeRender");
225 Assert.AreEqual ("<div attr=\"cookies&amp;cream\">\n\n</div>", sw.ToString ());
228 [Test]
229 public void EscapeAttribute2 () {
230 sw = new StringWriter ();
231 sw.NewLine = "\n"; // Keep sanity.
232 Poker w1 = new Poker (sw);
234 w1.AddAttribute ("attr", "cookies&cream", false);
235 w1.RenderBeginTag (HtwTag.Div);
236 w1.RenderEndTag ();
238 Assert.AreEqual ("cookies&cream", w1.AttrValue_At_AddAttribute, "AttrValue_At_AddAttribute");
239 Assert.AreEqual ("cookies&cream", w1.AttrValue_At_OnAttributeRender, "AttrValue_At_OnAttributeRender");
240 Assert.AreEqual ("<div attr=\"cookies&cream\">\n\n</div>", sw.ToString ());
243 [Test]
244 public void EscapeAttribute3 () {
245 sw = new StringWriter ();
246 sw.NewLine = "\n"; // Keep sanity.
247 Poker w1 = new Poker (sw);
249 w1.AddAttribute ("attr", "cookies&cream", true);
250 w1.RenderBeginTag (HtwTag.Div);
251 w1.RenderEndTag ();
253 Assert.AreEqual ("cookies&amp;cream", w1.AttrValue_At_AddAttribute, "AttrValue_At_AddAttribute");
254 Assert.AreEqual ("cookies&amp;cream", w1.AttrValue_At_OnAttributeRender, "AttrValue_At_OnAttributeRender");
255 Assert.AreEqual ("<div attr=\"cookies&amp;cream\">\n\n</div>", sw.ToString ());
258 [Test]
259 public void WriteInput ()
261 w.RenderBeginTag (HtwTag.Input);
262 w.RenderEndTag ();
263 Assert.AreEqual ("<input />", sw.ToString ());
265 [Test]
266 public void WriteInputStringTag ()
268 w.RenderBeginTag ("input");
269 w.RenderEndTag ();
270 Assert.AreEqual ("<input />", sw.ToString ());
273 [Test]
274 public void WriteDivStringTag ()
276 w.RenderBeginTag ("div");
277 w.RenderEndTag ();
278 Assert.AreEqual ("<div>\n\n</div>", sw.ToString ());
281 [Test]
282 public void WriteUnknownTag ()
284 w.RenderBeginTag ("somerandomtag");
285 w.RenderEndTag ();
286 Assert.AreEqual ("<somerandomtag>\n\n</somerandomtag>", sw.ToString ());
289 [Test]
290 public void WritePartialBlock ()
292 w.RenderBeginTag ("div");
293 Assert.AreEqual ("<div>\n", sw.ToString ());
296 [Test]
297 public void WritePartialInline ()
299 w.RenderBeginTag ("a");
300 Assert.AreEqual ("<a>", sw.ToString ());
303 [Test]
304 public void WritePartialSelfClosing ()
306 w.RenderBeginTag ("input");
307 Assert.AreEqual ("<input />", sw.ToString ());
310 [Test]
311 [ExpectedException (typeof (InvalidOperationException))]
312 public void NoMatchingClose ()
314 w.RenderEndTag ();
317 [Test]
318 public void AttributeAfterBegin ()
320 w.RenderBeginTag ("div");
321 w.AddAttribute ("class", "foo");
322 w.RenderEndTag ();
323 w.RenderBeginTag ("div");
324 w.RenderEndTag ();
326 Assert.AreEqual ("<div>\n\n</div><div class=\"foo\">\n\n</div>", sw.ToString ());
329 [Test]
330 public void StyleAttribute ()
332 w.WriteStyleAttribute ("a", "b");
334 Assert.AreEqual ("a:b;", sw.ToString ());
337 [Test]
338 public void TagByNameGetsCaseChanged ()
340 w.RenderBeginTag ("InPuT");
341 Assert.AreEqual ("<input />", sw.ToString ());
344 [Test]
345 public void EnsurePopWorks ()
347 w.RenderBeginTag (HtwTag.A);
348 w.RenderBeginTag (HtwTag.B);
349 w.RenderEndTag ();
350 w.RenderEndTag ();
352 Assert.AreEqual ("<a><b></b></a>", sw.ToString ());
355 [Test]
356 public void AddAttributeTwice_SameValue ()
358 w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
359 w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
360 w.RenderBeginTag ("div");
361 w.RenderEndTag ();
363 Assert.AreEqual ("<div border=\"0\" border=\"0\">\n\n</div>", sw.ToString ());
366 [Test]
367 public void AddAttributeTwice_DifferentValue ()
369 w.AddAttribute (HtmlTextWriterAttribute.Border, "0");
370 w.AddAttribute (HtmlTextWriterAttribute.Border, "1");
371 w.RenderBeginTag ("div");
372 w.RenderEndTag ();
374 Assert.AreEqual ("<div border=\"0\" border=\"1\">\n\n</div>", sw.ToString ());
377 [Test]
378 public void AddStyleAttribute1 ()
380 w.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, "http://www.example.com/");
381 w.RenderBeginTag ("div");
382 w.RenderEndTag ();
384 Assert.AreEqual ("<div style=\"background-image:url(http://www.example.com/);\">\n\n</div>", sw.ToString ());
387 [Test]
388 public void AddStyleAttribute3 ()
390 sw = new StringWriter ();
391 sw.NewLine = "\n"; // Keep sanity.
392 Poker w1 = new Poker (sw);
394 w1.AddStyleAttribute ("mystyle", "my value&space");
395 w1.RenderBeginTag ("div");
396 w1.RenderEndTag ();
397 Assert.AreEqual ("my value&space", w1.StyleValue_At_AddStyleAttribute, "StyleValue_At_AddStyleAttribute");
398 Assert.AreEqual ("my value&amp;space", w1.StyleValue_At_OnStyleAttributeRender, "StyleValue_At_OnStyleAttributeRender");
399 Assert.AreEqual ("<div style=\"mystyle:my value&amp;space;\">\n\n</div>", sw.ToString ());
402 [Test]
403 public void WriteAttribute1 ()
405 w.WriteAttribute ("attr", "my value&space");
406 Assert.AreEqual (" attr=\"my value&space\"", sw.ToString ());
409 [Test]
410 public void WriteAttribute2 ()
412 w.WriteAttribute ("attr", "my value&space", false);
413 Assert.AreEqual (" attr=\"my value&space\"", sw.ToString ());
416 [Test]
417 public void WriteAttribute3 ()
419 w.WriteAttribute ("attr", "my value&space", true);
420 Assert.AreEqual (" attr=\"my value&amp;space\"", sw.ToString ());
423 [Test]
424 public void WriteStyleAttribute1 ()
426 w.WriteStyleAttribute ("mystyle", "my value&space");
427 Assert.AreEqual ("mystyle:my value&space;", sw.ToString ());
430 [Test]
431 public void WriteStyleAttribute2 ()
433 w.WriteStyleAttribute ("mystyle", "my value&space", false);
434 Assert.AreEqual ("mystyle:my value&space;", sw.ToString ());
437 [Test]
438 public void WriteStyleAttribute3 ()
440 w.WriteStyleAttribute ("mystyle", "my value&space", true);
441 Assert.AreEqual ("mystyle:my value&amp;space;", sw.ToString ());
444 [Test]
445 public void WriteStyleAttribute_BackgroundImage1 ()
447 w.WriteStyleAttribute ("background-image", "http://www.mainsoft.com/space here?a=b&c=d");
448 Assert.AreEqual ("background-image:http://www.mainsoft.com/space here?a=b&c=d;", sw.ToString ());
451 [Test]
452 public void WriteStyleAttribute_BackgroundImage2 ()
454 w.WriteStyleAttribute ("BackGround-Image", "http://www.mainsoft.com/space here?a=b&c=d");
455 Assert.AreEqual ("BackGround-Image:http://www.mainsoft.com/space here?a=b&c=d;", sw.ToString ());
458 [Test]
459 public void AddStyleAttribute_BackgroundImage1 ()
461 sw = new StringWriter ();
462 sw.NewLine = "\n"; // Keep sanity.
463 Poker w1 = new Poker (sw);
465 w1.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, "http://www.mainsoft.com/space here?a=b&c=d");
466 w1.RenderBeginTag ("div");
467 w1.RenderEndTag ();
469 Assert.AreEqual ("http://www.mainsoft.com/space here?a=b&c=d", w1.StyleValue_At_AddStyleAttribute, "StyleValue_At_AddStyleAttribute");
470 Assert.AreEqual ("http://www.mainsoft.com/space here?a=b&amp;c=d", w1.StyleValue_At_OnStyleAttributeRender, "StyleValue_At_OnStyleAttributeRender");
471 Assert.AreEqual ("<div style=\"background-image:url(http://www.mainsoft.com/space%20here?a=b&amp;c=d);\">\n\n</div>", sw.ToString ());
474 [Test]
475 public void AddStyleAttribute_BackgroundImage2 ()
477 sw = new StringWriter ();
478 sw.NewLine = "\n"; // Keep sanity.
479 Poker w1 = new Poker (sw);
481 w1.AddStyleAttribute ("background-image", "http://www.mainsoft.com/space here?a=b&c=d");
482 w1.RenderBeginTag ("div");
483 w1.RenderEndTag ();
485 Assert.AreEqual ("http://www.mainsoft.com/space here?a=b&c=d", w1.StyleValue_At_AddStyleAttribute, "StyleValue_At_AddStyleAttribute");
486 Assert.AreEqual ("http://www.mainsoft.com/space here?a=b&amp;c=d", w1.StyleValue_At_OnStyleAttributeRender, "StyleValue_At_OnStyleAttributeRender");
487 Assert.AreEqual ("<div style=\"background-image:url(http://www.mainsoft.com/space%20here?a=b&amp;c=d);\">\n\n</div>", sw.ToString ());
490 [Test]
491 public void AddStyleAttribute_BackgroundImage3 ()
493 sw = new StringWriter ();
494 sw.NewLine = "\n"; // Keep sanity.
495 Poker w1 = new Poker (sw);
497 w1.AddStyleAttribute ("BackGround-Image", "http://www.mainsoft.com/space here?a=b&c=d");
498 w1.RenderBeginTag ("div");
499 w1.RenderEndTag ();
501 Assert.AreEqual ("http://www.mainsoft.com/space here?a=b&c=d", w1.StyleValue_At_AddStyleAttribute, "StyleValue_At_AddStyleAttribute");
502 Assert.AreEqual ("http://www.mainsoft.com/space here?a=b&amp;c=d", w1.StyleValue_At_OnStyleAttributeRender, "StyleValue_At_OnStyleAttributeRender");
503 Assert.AreEqual ("<div style=\"BackGround-Image:url(http://www.mainsoft.com/space%20here?a=b&amp;c=d);\">\n\n</div>", sw.ToString ());
507 [Test]
508 public void AddStyleAttribute2 ()
510 w.AddStyleAttribute (HtmlTextWriterStyle.BackgroundColor, "Aqua");
511 w.RenderBeginTag ("div");
512 w.RenderEndTag ();
514 Assert.AreEqual ("<div style=\"background-color:Aqua;\">\n\n</div>", sw.ToString ());
517 [Test]
518 public void WriteIndentation ()
520 w.RenderBeginTag (HtwTag.Div);
521 w.Write ("Hello");
522 w.RenderEndTag ();
524 Assert.AreEqual ("<div>\n\tHello\n</div>", sw.ToString ());
527 [Test]
528 public void WriteIndentation2 ()
530 w.RenderBeginTag (HtwTag.Div);
531 w.Write ("");
532 w.RenderEndTag ();
534 Assert.AreEqual ("<div>\n\t\n</div>", sw.ToString ());
537 [Test]
538 public void WriteIndentation3 ()
540 w.RenderBeginTag (HtwTag.Div);
541 w.RenderEndTag ();
543 Assert.AreEqual ("<div>\n\n</div>", sw.ToString ());
546 [Test]
547 public void WriteBreakTest ()
549 w.WriteBreak ();
550 Assert.AreEqual ("<br />", sw.ToString (), "WriteBreakTest01");
553 class MyHttpTextWriter : HtmlTextWriter {
554 int i = 0;
555 public MyHttpTextWriter(TextWriter tw) : base(tw) {
558 protected override bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key) {
559 switch (i++) {
560 case 0:
561 case 1: Assert.AreEqual(HtmlTextWriterAttribute.Border, key);
562 break;
563 case 2: Assert.AreEqual((HtmlTextWriterAttribute)(-1), key);
564 break;
566 return base.OnAttributeRender (name, value, key);
571 class Poker : HtmlTextWriter
573 public string StyleValue_At_OnStyleAttributeRender;
574 public string StyleValue_At_AddStyleAttribute;
575 public string AttrValue_At_OnAttributeRender;
576 public string AttrValue_At_AddAttribute;
578 public Poker (TextWriter tw)
579 : base (tw)
583 protected override bool OnAttributeRender (string name, string value, HtmlTextWriterAttribute key)
585 AttrValue_At_OnAttributeRender = value;
586 return base.OnAttributeRender (name, value, key);
589 protected override bool OnStyleAttributeRender (string name, string value, HtmlTextWriterStyle key)
591 StyleValue_At_OnStyleAttributeRender = value;
592 return base.OnStyleAttributeRender (name, value, key);
595 protected override void AddStyleAttribute (string name, string value, HtmlTextWriterStyle key)
597 StyleValue_At_AddStyleAttribute = value;
598 base.AddStyleAttribute (name, value, key);
601 protected override void AddAttribute (string name, string value, HtmlTextWriterAttribute key)
603 AttrValue_At_AddAttribute = value;
604 base.AddAttribute (name, value, key);
609 [Test]
610 public void TestOnAttributeRender() {
611 MyHttpTextWriter myw = new MyHttpTextWriter (sw);
613 myw.AddAttribute ("border", "0");
614 myw.AddAttribute ("BoRDeR", "0");
615 myw.AddAttribute ("error", "0");
616 myw.RenderBeginTag ("div");
617 myw.RenderEndTag ();