Merge pull request #5714 from alexischr/update_bockbuild
[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                 public void InitialNoBag10()
117                 {
118                     AC ac = new AC(null);
119                     CssStyleCollection css = ac.CssStyle;
120                     int i = css.Count;
121                     Assert.AreEqual(0, i, "InitialNoBag10");
122                 }
123
124                 [Test]
125                 public void InitialNoBag11()
126                 {
127                     AC ac = new AC(null);
128                     CssStyleCollection css = ac.CssStyle;
129                     ICollection coll = css.Keys;
130                     Assert.AreEqual(0, coll.Count, "InitialNoBag11");
131                 }
132
133                 [Test]
134                 public void InitialNoBag12()
135                 {
136                     AC ac = new AC(null);
137                     CssStyleCollection css = ac.CssStyle;
138                     string v = css["hola"];
139                     Assert.AreEqual(null, v, "InitialNoBag12");
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                 [Test]
211                 public void Count1 ()
212                 {
213                         StateBag bag = new StateBag (true);
214                         AC ac = new AC (bag);
215                         ac.Add ("style", "padding: 0px; margin: 0px");
216                         Assert.AreEqual (1, ac.Count, "AttributeCollection.Count");
217                         Assert.AreEqual (2, ac.CssStyle.Count, "AttributeCollection.Count");
218                         
219                         ac.Remove ("style");
220                         Assert.AreEqual (0, ac.Count, "AttributeCollection.Count");
221                         Assert.AreEqual (0, ac.CssStyle.Count, "AttributeCollection.Count");
222                 }
223
224                 [Test]
225                 public void Count2 ()
226                 {
227                         StateBag bag = new StateBag (true);
228                         AC ac = new AC (bag);
229                         ac ["style"] = "padding: 0px; margin: 0px";
230                         Assert.AreEqual (1, ac.Count, "AttributeCollection.Count");
231                         Assert.AreEqual (2, ac.CssStyle.Count, "AttributeCollection.Count");
232
233                         ac ["style"] = null;
234                         Assert.AreEqual (0, ac.Count, "AttributeCollection.Count");
235                         Assert.AreEqual (0, ac.CssStyle.Count, "AttributeCollection.Count");
236                 }
237
238                 [Test]
239                 public void Count3 ()
240                 {
241                         StateBag bag = new StateBag (true);
242                         AC ac = new AC (bag);
243                         ac.CssStyle.Add("padding", "0px");
244                         ac.CssStyle.Add("margin", "0px");
245                         Assert.AreEqual (1, ac.Count, "AttributeCollection.Count");
246                         Assert.AreEqual (2, ac.CssStyle.Count, "AttributeCollection.Count");
247
248                         ac.CssStyle.Remove ("padding");
249                         ac.CssStyle.Remove ("margin");
250                         Assert.AreEqual (0, ac.Count, "AttributeCollection.Count");
251                         Assert.AreEqual (0, ac.CssStyle.Count, "AttributeCollection.Count");
252                 }
253
254                 [Test]
255                 public void Count4 ()
256                 {
257                         StateBag bag = new StateBag (true);
258                         AC ac = new AC (bag);
259                         ac.CssStyle ["padding"] = "0px";
260                         ac.CssStyle ["margin"] = "0px";
261                         Assert.AreEqual (1, ac.Count, "AttributeCollection.Count");
262                         Assert.AreEqual (2, ac.CssStyle.Count, "AttributeCollection.Count");
263
264                         ac.CssStyle.Value = null;
265                         Assert.AreEqual (0, ac.Count, "AttributeCollection.Count");
266                         Assert.AreEqual (0, ac.CssStyle.Count, "AttributeCollection.Count");
267                 }
268         }
269 }
270