2 // Tests for System.Web.UI.AttributeCollection.cs and CssStyleCollection
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
33 using System.Globalization;
36 using System.Collections;
37 using AC = System.Web.UI.AttributeCollection;
39 namespace MonoTests.System.Web.UI {
41 public class AttributeCollectionTest {
43 public void InitialNoBag1 ()
45 AC ac = new AC (null);
46 Assert.IsNotNull (ac.CssStyle, "style");
50 [ExpectedException (typeof (NullReferenceException))]
51 public void InitialNoBag2 ()
53 AC ac = new AC (null);
58 [ExpectedException (typeof (NullReferenceException))]
59 public void InitialNoBag3 ()
61 AC ac = new AC (null);
62 ICollection coll = ac.Keys;
66 [ExpectedException (typeof (NullReferenceException))]
67 public void InitialNoBag4 ()
69 AC ac = new AC (null);
70 string k = ac ["hola"];
74 [ExpectedException (typeof (NullReferenceException))]
75 public void InitialNoBag5 ()
77 AC ac = new AC (null);
78 ac.Add ("att", "value");
82 [ExpectedException (typeof (NullReferenceException))]
83 public void InitialNoBag6 ()
85 AC ac = new AC (null);
90 [ExpectedException (typeof (NullReferenceException))]
91 public void InitialNoBag7 ()
93 AC ac = new AC (null);
94 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
95 ac.AddAttributes (writer);
99 [ExpectedException (typeof (NullReferenceException))]
100 public void InitialNoBag8 ()
102 AC ac = new AC (null);
103 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
108 [ExpectedException (typeof (NullReferenceException))]
109 public void InitialNoBag9 ()
111 AC ac = new AC (null);
116 public void InitialNoBag10()
118 AC ac = new AC(null);
119 CssStyleCollection css = ac.CssStyle;
121 Assert.AreEqual(0, i, "InitialNoBag10");
125 public void InitialNoBag11()
127 AC ac = new AC(null);
128 CssStyleCollection css = ac.CssStyle;
129 ICollection coll = css.Keys;
130 Assert.AreEqual(0, coll.Count, "InitialNoBag11");
134 public void InitialNoBag12()
136 AC ac = new AC(null);
137 CssStyleCollection css = ac.CssStyle;
138 string v = css["hola"];
139 Assert.AreEqual(null, v, "InitialNoBag12");
143 public void InitialBag1 ()
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");
154 HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
155 ac.AddAttributes (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");
167 Assert.AreEqual (0, css.Keys.Count, "csskeys2");
171 public void NonStyleAttributes1 ()
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");
191 public void NonStyleAttributes2 ()
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");
211 public void Count1 ()
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");
220 Assert.AreEqual (0, ac.Count, "AttributeCollection.Count");
221 Assert.AreEqual (0, ac.CssStyle.Count, "AttributeCollection.Count");
225 public void Count2 ()
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");
234 Assert.AreEqual (0, ac.Count, "AttributeCollection.Count");
235 Assert.AreEqual (0, ac.CssStyle.Count, "AttributeCollection.Count");
239 public void Count3 ()
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");
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");
255 public void Count4 ()
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");
264 ac.CssStyle.Value = null;
265 Assert.AreEqual (0, ac.Count, "AttributeCollection.Count");
266 Assert.AreEqual (0, ac.CssStyle.Count, "AttributeCollection.Count");