New test.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI / AttributeCollectionTest.cs
1 //
2 // Tests for System.Web.UI.AttributeCollection.cs and CssStyleCollection
3 //
4 // Author:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 //
8 // Copyright (C) 2005 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 using NUnit.Framework;
31 using System;
32 using System.IO;
33 using System.Globalization;
34 using System.Web;
35 using System.Web.UI;
36 using System.Collections;
37 using AC = System.Web.UI.AttributeCollection;
38
39 namespace MonoTests.System.Web.UI {
40         [TestFixture]
41         public class AttributeCollectionTest {
42                 [Test]
43                 public void InitialNoBag1 ()
44                 {
45                         AC ac = new AC (null);
46                         Assert.IsNotNull (ac.CssStyle, "style");
47                 }
48
49                 [Test]
50                 [ExpectedException (typeof (NullReferenceException))]
51                 public void InitialNoBag2 ()
52                 {
53                         AC ac = new AC (null);
54                         int i = ac.Count;
55                 }
56
57                 [Test]
58                 [ExpectedException (typeof (NullReferenceException))]
59                 public void InitialNoBag3 ()
60                 {
61                         AC ac = new AC (null);
62                         ICollection coll = ac.Keys;
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (NullReferenceException))]
67                 public void InitialNoBag4 ()
68                 {
69                         AC ac = new AC (null);
70                         string k = ac ["hola"];
71                 }
72
73                 [Test]
74                 [ExpectedException (typeof (NullReferenceException))]
75                 public void InitialNoBag5 ()
76                 {
77                         AC ac = new AC (null);
78                         ac.Add ("att", "value");
79                 }
80
81                 [Test]
82                 [ExpectedException (typeof (NullReferenceException))]
83                 public void InitialNoBag6 ()
84                 {
85                         AC ac = new AC (null);
86                         ac.Clear ();
87                 }
88
89                 [Test]
90                 [ExpectedException (typeof (NullReferenceException))]
91                 public void InitialNoBag7 ()
92                 {
93                         AC ac = new AC (null);
94                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
95                         ac.AddAttributes (writer);
96                 }
97
98                 [Test]
99                 [ExpectedException (typeof (NullReferenceException))]
100                 public void InitialNoBag8 ()
101                 {
102                         AC ac = new AC (null);
103                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
104                         ac.Render (writer);
105                 }
106
107                 [Test]
108                 [ExpectedException (typeof (NullReferenceException))]
109                 public void InitialNoBag9 ()
110                 {
111                         AC ac = new AC (null);
112                         ac.Remove ("hola");
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (NullReferenceException))]
117                 public void InitialNoBag10 ()
118                 {
119                         AC ac = new AC (null);
120                         CssStyleCollection css = ac.CssStyle;
121                         int i = css.Count;
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (NullReferenceException))]
126                 public void InitialNoBag11 ()
127                 {
128                         AC ac = new AC (null);
129                         CssStyleCollection css = ac.CssStyle;
130                         ICollection coll = css.Keys;
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (NullReferenceException))]
135                 public void InitialNoBag12 ()
136                 {
137                         AC ac = new AC (null);
138                         CssStyleCollection css = ac.CssStyle;
139                         string v = css ["hola"];
140                 }
141
142                 [Test]
143                 public void InitialBag1 ()
144                 {
145                         StateBag bag = new StateBag (true);
146                         AC ac = new AC (bag);
147                         Assert.AreEqual (0, ac.Count, "count");
148                         Assert.AreEqual (null, ac ["hola"], "item");
149                         Assert.AreEqual (0, ac.Keys.Count, "keys");
150                         ac.Add ("notexists", "invalid");
151                         ac.Remove ("notexists");
152                         ac.Remove ("notexists");
153
154                         HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
155                         ac.AddAttributes (writer);
156                         ac.Render (writer);
157                         Assert.AreEqual (0, writer.InnerWriter.ToString().Length, "length");
158                         CssStyleCollection css = ac.CssStyle;
159                         Assert.AreEqual (0, css.Count, "csscount");
160                         Assert.AreEqual (null, css ["hola"], "cssitem");
161                         Assert.AreEqual (0, css.Keys.Count, "csskeys");
162                         css.Add ("notexists", "invalid");
163                         css.Remove ("notexists");
164                         css.Remove ("notexists");
165                         css.Add ("notexists", "invalid");
166                         css.Clear ();
167                         Assert.AreEqual (0, css.Keys.Count, "csskeys2");
168                 }
169
170                 [Test]
171                 public void NonStyleAttributes1 ()
172                 {
173                         StateBag bag = new StateBag (true);
174                         AC ac = new AC (bag);
175                         StringWriter sr = new StringWriter ();
176                         HtmlTextWriter writer = new HtmlTextWriter (sr);
177                         ac.Add ("notexists", "somevalue");
178                         ac.AddAttributes (writer);
179                         string str = sr.ToString ();
180                         Assert.AreEqual ("", str, "value1");
181                         Assert.AreEqual (1, bag.Count, "count1");
182                         writer = new HtmlTextWriter (sr);
183                         writer.RenderBeginTag (HtmlTextWriterTag.A);
184                         ac.AddAttributes (writer);
185                         writer.RenderEndTag ();
186                         Assert.AreEqual ("", str, "value2");
187                         Assert.AreEqual (1, bag.Count, "count2");
188                 }
189
190                 [Test]
191                 public void NonStyleAttributes2 ()
192                 {
193                         StateBag bag = new StateBag (true);
194                         AC ac = new AC (bag);
195                         StringWriter sr = new StringWriter ();
196                         HtmlTextWriter writer = new HtmlTextWriter (sr);
197                         ac.Add ("class", "classname");
198                         ac.AddAttributes (writer);
199                         string str = sr.ToString ();
200                         Assert.AreEqual ("", str, "value1");
201                         Assert.AreEqual (1, bag.Count, "count1");
202                         writer = new HtmlTextWriter (sr);
203                         writer.RenderBeginTag (HtmlTextWriterTag.A);
204                         ac.AddAttributes (writer);
205                         writer.RenderEndTag ();
206                         Assert.AreEqual ("", str, "value2");
207                         Assert.AreEqual (1, bag.Count, "count2");
208                 }
209         }
210 }
211