2 // Tests for System.Web.UI.WebControls.Style.cs
5 // Peter Dennis Bartok (pbartok@novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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 NUnit.Framework;
33 using System.Collections;
36 using System.Globalization;
37 using refl = System.Reflection;
40 using System.Web.UI.WebControls;
42 namespace MonoTests.System.Web.UI.WebControls
44 public class StyleTestClass : Style {
46 public StyleTestClass ()
51 public StyleTestClass (StateBag bag)
56 public StateBag StateBag {
57 get { return base.ViewState; }
61 get { return base.IsEmpty; }
64 public bool IsTracking {
65 get { return base.IsTrackingViewState; }
68 public void SetCssClass(string name) {
69 Type style = Type.GetType("System.Web.UI.WebControls.Style, System.Web");
71 refl.MethodInfo methodInfo = style.GetMethod("SetRegisteredCssClass",refl.BindingFlags.NonPublic | refl.BindingFlags.Instance);
72 if (methodInfo != null) {
73 object[] parameters = new object[] {name};
74 methodInfo.Invoke(this, parameters);
80 public string[] KeyValuePairs() {
85 e = ViewState.GetEnumerator();
86 result = new string[ViewState.Keys.Count];
89 while (e.MoveNext()) {
93 d = (DictionaryEntry)e.Current;
94 si = (StateItem)d.Value;
96 if (si.Value is String[]) {
99 values = (string[]) si.Value;
100 result[item] = d.Key.ToString() + "=";
101 if (values.Length > 0) {
102 result[item] += values[0];
104 for (int i = 1; i < values.Length; i++) {
105 result[item] += ", " + values[i];
109 result[item] = d.Key.ToString() + "=" + si.Value;
119 public class StyleTest {
120 private static HtmlTextWriter GetWriter () {
121 StringWriter sw = new StringWriter ();
123 return new HtmlTextWriter (sw);
126 private void SetSomeValues(Style s) {
127 s.BackColor = Color.Red;
128 s.ForeColor = Color.Green;
129 s.Width = new Unit(10);
133 private void SetAllValues(Style s) {
134 s.BackColor = Color.Red;
135 s.BorderColor = Color.Green;
136 s.BorderStyle = BorderStyle.None;
137 s.BorderWidth = new Unit(1);
138 s.CssClass = "Boing";
140 s.Font.Italic = true;
141 s.Font.Names = new string[2] {"namelist1", "namelist2"};
142 //s.Font.Name = string.Empty;
143 //s.Font.Names = new string[0];
144 //Console.WriteLine("Font name after setting name: {0}", s.Font.ToString());
145 s.Font.Overline = true;
146 s.Font.Size = new FontUnit(1);
147 //Console.WriteLine("Font name after setting size: {0}", s.Font.ToString());
148 s.Font.Strikeout = true;
149 s.Font.Underline = true;
150 s.ForeColor = Color.Blue;
151 s.Height = new Unit(2);
152 s.Width = new Unit(3);
155 private bool IsEqual(object[] a1, object[] a2, string assertion) {
159 if (a1.Length != a2.Length) {
160 if (assertion != null) {
161 Assert.Fail(assertion + "( different length )");
167 notfound = new bool[a1.Length];
169 for (int i = 0; i < a1.Length; i++) {
170 for (int j = 0; j < a2.Length; j++) {
171 if (a1[i].Equals(a2[j])) {
176 if ((assertion != null) && (matches != i+1)) {
177 Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
181 return matches == a1.Length;
185 public void Style_Defaults ()
187 Style s = new Style ();
189 Assert.AreEqual (s.BackColor, Color.Empty, "Default1");
190 Assert.AreEqual (s.BorderColor, Color.Empty, "Default22");
191 Assert.AreEqual (s.BorderStyle, BorderStyle.NotSet, "Default3");
192 Assert.AreEqual (s.BorderWidth, Unit.Empty, "Default4");
193 Assert.AreEqual (s.CssClass, string.Empty, "Default5");
194 Assert.AreEqual (s.ForeColor, Color.Empty, "Default6");
195 Assert.AreEqual (s.Height, Unit.Empty, "Default7");
196 Assert.AreEqual (s.Width, Unit.Empty, "Default8");
201 public void Style_State () {
210 "ForeColor=Color [Blue]",
212 "Font_Overline=True",
214 "BorderColor=Color [Green]",
215 "Font_Names=namelist1, namelist2",
216 "Font_Underline=True",
217 "BackColor=Color [Red]",
218 "Font_Strikeout=True" };
226 "ForeColor=Color [Blue]",
228 "Font_Overline=True",
230 "BorderColor=Color [Green]",
231 "Font_Underline=True",
232 "BackColor=Color [Red]",
233 "Font_Strikeout=True" };
241 "ForeColor=Color [Blue]",
243 "Font_Overline=True",
245 "BorderColor=Color [Green]",
247 "Font_Underline=True",
248 "BackColor=Color [Red]",
249 "Font_Strikeout=True" };
253 s = new StyleTestClass();
255 keyvalues = s.KeyValuePairs();
257 Assert.AreEqual (15, keyvalues.Length, "State1");
258 IsEqual(keyvalues, expect1, "State2");
260 s.Font.Name = string.Empty;
261 keyvalues = s.KeyValuePairs();
262 Assert.AreEqual (expect2.Length, keyvalues.Length, "State3");
263 IsEqual(keyvalues, expect2, "State4");
266 keyvalues = s.KeyValuePairs();
267 Assert.AreEqual (expect2.Length, keyvalues.Length, "State5");
268 IsEqual(keyvalues, expect2, "State6");
270 copy = new StyleTestClass();
272 keyvalues = copy.KeyValuePairs();
273 Assert.AreEqual (expect3.Length, keyvalues.Length, "State7");
274 IsEqual(keyvalues, expect3, "State8");
276 Assert.AreEqual (false, copy.IsTracking, "State9");
281 public void Style_Merge ()
283 Style s = new Style ();
284 Style copy = new Style ();
287 copy.ForeColor = Color.Blue;
290 Assert.AreEqual (Color.Red, copy.BackColor, "Merge1");
291 Assert.AreEqual (Color.Blue, copy.ForeColor, "Merge2");
294 copy.MergeWith(null);
298 public void Style_Copy ()
300 Style s = new Style ();
301 Style copy = new Style ();
306 Assert.AreEqual (Color.Red, s.BackColor, "Copy1");
311 public void Style_CssClass ()
313 StyleTestClass s = new StyleTestClass ();
315 Assert.AreEqual (null, s.RegisteredCssClass, "Css1");
317 s.SetCssClass("blah");
318 Assert.AreEqual ("blah", s.RegisteredCssClass, "Css2");
323 public void StyleFonts () {
324 Style s = new Style ();
326 Assert.AreEqual(new string[0], s.Font.Names, "F1");
328 s.Font.Name = string.Empty;
329 Assert.AreEqual(new string[0], s.Font.Names, "F2");
332 Assert.AreEqual(new string[0], s.Font.Names, "F3");
336 [ExpectedException(typeof(ArgumentNullException))]
337 public void NullException1 ()
339 Style s = new Style ();
344 private Style GetStyle ()
346 Style s = new Style ();
347 s.BackColor = Color.Aqua;
348 s.BorderWidth = Unit.Pixel (1);
352 private void CheckStyle (Style s)
354 Assert.AreEqual (Color.Aqua, s.BackColor, "BackColor");
355 Assert.AreEqual (Unit.Pixel (1), s.BorderWidth, "BorderWidth");
360 public void CopyFrom_Null ()
362 Style s = GetStyle ();
368 public void CopyFrom_Self ()
370 Style s = GetStyle ();
376 public void CopyFrom_Empty ()
378 StyleTestClass s = new StyleTestClass ();
379 s.CopyFrom (new Style ());
380 Assert.IsTrue (s.Empty, "Empty");
384 public void CopyFrom ()
386 Style c = GetStyle ();
387 Style s = GetStyle ();
389 c.BorderColor = Color.Azure;
390 c.BorderWidth = Unit.Empty;
395 Assert.AreEqual (Color.Azure, c.BorderColor, "BorderColor");
396 // CopyFrom doesn't do a Reset
400 public void CopyFrom_IsEmpty ()
402 StyleTestClass c = new StyleTestClass ();
403 Style s = GetStyle ();
405 s.BorderColor = Color.Azure;
406 s.BorderWidth = Unit.Empty;
410 Assert.IsFalse (c.Empty, "IsEmpty");
414 public void Constructor_StateBag_Null ()
416 StyleTestClass s = new StyleTestClass (null);
417 Assert.IsNotNull (s.StateBag, "StateBag");
419 Assert.AreEqual ("mono", s.CssClass, "CssClass");
425 StyleTestClass s = new StyleTestClass ();
426 Assert.IsTrue (s.Empty, "Empty");
427 Assert.AreEqual (0, s.StateBag.Count, "Count");
428 s.StateBag["Mono"] = "go!";
429 Assert.IsTrue (s.Empty, "Still Empty");
430 Assert.AreEqual (1, s.StateBag.Count, "Count");
433 public void Render ()
435 HtmlTextWriter writer;
438 writer = StyleTest.GetWriter();
439 s = new StyleTestClass ();
441 s.BorderColor = Color.BlueViolet;
442 s.AddAttributesToRender(writer);
443 // Figure out an order-independent way to verify rendered results
447 public void IsStyleEmpty ()
449 Assert.IsTrue (Style.IsStyleEmpty (null), "null");
450 Assert.IsTrue (Style.IsStyleEmpty (new Style ()), "new");