svn path=/branches/mono-1-1-9/mcs/; revision=50438
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / StyleTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.Style.cs 
3 //
4 // Author:
5 //      Peter Dennis Bartok (pbartok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
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.
29 //
30
31 using NUnit.Framework;
32 using System;
33 using System.Collections;
34 using System.Drawing;
35 using System.IO;
36 using System.Globalization;
37 using refl = System.Reflection;
38 using System.Web;
39 using System.Web.UI;
40 using System.Web.UI.WebControls;
41
42 namespace MonoTests.System.Web.UI.WebControls
43 {
44         public class StyleTestClass : Style {
45
46                 public StyleTestClass ()
47                         : base ()
48                 {
49                 }
50
51                 public StyleTestClass (StateBag bag)
52                         : base (bag)
53                 {
54                 }
55
56                 public StateBag StateBag {
57                         get { return base.ViewState; }
58                 }
59
60                 public bool Empty {
61                         get { return base.IsEmpty; }
62                 }
63
64                 public bool IsTracking {
65                         get { return base.IsTrackingViewState; }
66                 }
67 #if NET_2_0
68                 public void SetCssClass(string name) {
69                         Type style = Type.GetType("System.Web.UI.WebControls.Style, System.Web");
70                         if (style != null) {    
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);
75                                 }
76                         }
77                 }
78 #endif
79
80                 public string[] KeyValuePairs() {
81                         IEnumerator     e;
82                         string[]        result;
83                         int             item;
84
85                         e = ViewState.GetEnumerator();
86                         result = new string[ViewState.Keys.Count];
87                         item = 0;
88
89                         while (e.MoveNext()) {
90                                 DictionaryEntry d;
91                                 StateItem       si;
92
93                                 d = (DictionaryEntry)e.Current;
94                                 si = (StateItem)d.Value;
95
96                                 if (si.Value is String[]) {
97                                         string[] values;
98
99                                         values = (string[]) si.Value;
100                                         result[item] = d.Key.ToString() + "=";
101                                         if (values.Length > 0) {
102                                                 result[item] += values[0];
103
104                                                 for (int i = 1; i < values.Length; i++) {
105                                                         result[item] += ", " + values[i];
106                                                 }
107                                         }
108                                 } else {
109                                         result[item] =  d.Key.ToString() + "=" + si.Value;
110                                 }
111                                 item++;
112                         }
113
114                         return result;
115                 }
116         }
117
118         [TestFixture]   
119         public class StyleTest {
120                 private static HtmlTextWriter GetWriter () {
121                         StringWriter sw = new StringWriter ();
122                         sw.NewLine = "\n";
123                         return new HtmlTextWriter (sw);
124                 }
125
126                 private void SetSomeValues(Style s) {
127                         s.BackColor = Color.Red;
128                         s.ForeColor = Color.Green;
129                         s.Width = new Unit(10);
130                         s.Font.Bold = true;
131                 }
132
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";
139                         s.Font.Bold = true;
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);
153                 }
154
155                 private bool IsEqual(object[] a1, object[] a2, string assertion) {
156                         int     matches;
157                         bool[]  notfound;       
158
159                         if (a1.Length != a2.Length) {
160                                 if (assertion != null) {
161                                         Assert.Fail(assertion + "( different length )");
162                                 }
163                                 return false;
164                         }
165
166                         matches = 0;
167                         notfound = new bool[a1.Length];
168
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])) {
172                                                 matches++;
173                                                 break;
174                                         }
175                                 }
176                                 if ((assertion != null) && (matches != i+1)) {
177                                         Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
178                                 }
179                         }
180
181                         return matches == a1.Length;
182                 }
183
184                 [Test]
185                 public void Style_Defaults ()
186                 {
187                         Style s = new Style ();
188
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");
197                 }
198
199
200                 [Test]
201                 public void Style_State () {
202                         string[]        keyvalues;
203                         string[]        expect1 = {
204                                                 "BorderStyle=None",
205                                                 "Font_Bold=True",
206                                                 "Font_Italic=True",
207                                                 "Height=2px",
208                                                 "CssClass=Boing",
209                                                 "BorderWidth=1px",
210                                                 "ForeColor=Color [Blue]",
211                                                 "Font_Size=1pt",
212                                                 "Font_Overline=True",
213                                                 "Width=3px",
214                                                 "BorderColor=Color [Green]",
215                                                 "Font_Names=namelist1, namelist2",
216                                                 "Font_Underline=True",
217                                                 "BackColor=Color [Red]",
218                                                 "Font_Strikeout=True" };
219                         string[]        expect2 = {
220                                                 "BorderStyle=None",
221                                                 "Font_Bold=True",
222                                                 "Font_Italic=True",
223                                                 "Height=2px",
224                                                 "CssClass=Boing",
225                                                 "BorderWidth=1px",
226                                                 "ForeColor=Color [Blue]",
227                                                 "Font_Size=1pt",
228                                                 "Font_Overline=True",
229                                                 "Width=3px",
230                                                 "BorderColor=Color [Green]",
231                                                 "Font_Underline=True",
232                                                 "BackColor=Color [Red]",
233                                                 "Font_Strikeout=True" };
234                         string[]        expect3 = {
235                                                 "BorderStyle=None",
236                                                 "Font_Bold=True",
237                                                 "Font_Italic=True",
238                                                 "Height=2px",
239                                                 "CssClass=Boing",
240                                                 "BorderWidth=1px",
241                                                 "ForeColor=Color [Blue]",
242                                                 "Font_Size=1pt",
243                                                 "Font_Overline=True",
244                                                 "Width=3px",
245                                                 "BorderColor=Color [Green]",
246                                                 "Font_Names=",
247                                                 "Font_Underline=True",
248                                                 "BackColor=Color [Red]",
249                                                 "Font_Strikeout=True" };
250                         StyleTestClass  s;
251                         StyleTestClass  copy;
252
253                         s = new StyleTestClass();
254                         SetAllValues(s);
255                         keyvalues = s.KeyValuePairs();
256                         
257                         Assert.AreEqual (15, keyvalues.Length, "State1");
258                         IsEqual(keyvalues, expect1, "State2");
259
260                         s.Font.Name = string.Empty;
261                         keyvalues = s.KeyValuePairs();
262                         Assert.AreEqual (expect2.Length, keyvalues.Length, "State3");
263                         IsEqual(keyvalues, expect2, "State4");
264
265                         s.Font.Names = null;
266                         keyvalues = s.KeyValuePairs();
267                         Assert.AreEqual (expect2.Length, keyvalues.Length, "State5");
268                         IsEqual(keyvalues, expect2, "State6");
269
270                         copy = new StyleTestClass();
271                         copy.CopyFrom(s);
272                         keyvalues = copy.KeyValuePairs();
273                         Assert.AreEqual (expect3.Length, keyvalues.Length, "State7");
274                         IsEqual(keyvalues, expect3, "State8");
275
276                         Assert.AreEqual (false, copy.IsTracking, "State9");
277
278                 }
279
280                 [Test]
281                 public void Style_Merge ()
282                 {
283                         Style s = new Style ();
284                         Style copy = new Style ();
285
286                         SetSomeValues(s);
287                         copy.ForeColor = Color.Blue;
288
289                         copy.MergeWith(s);
290                         Assert.AreEqual (Color.Red, copy.BackColor, "Merge1");
291                         Assert.AreEqual (Color.Blue, copy.ForeColor, "Merge2");
292
293                         // Don't fail here
294                         copy.MergeWith(null);
295                 }
296
297                 [Test]
298                 public void Style_Copy ()
299                 {
300                         Style s = new Style ();
301                         Style copy = new Style ();
302
303                         SetSomeValues(s);
304
305                         copy.CopyFrom (s);
306                         Assert.AreEqual (Color.Red, s.BackColor, "Copy1");
307                 }
308
309 #if NET_2_0
310                 [Test]
311                 public void Style_CssClass ()
312                 {
313                         StyleTestClass s = new StyleTestClass ();
314
315                         Assert.AreEqual (null, s.RegisteredCssClass, "Css1");
316
317                         s.SetCssClass("blah");
318                         Assert.AreEqual ("blah", s.RegisteredCssClass, "Css2");
319                 }
320 #endif
321
322                 [Test]
323                 public void StyleFonts () {
324                         Style s = new Style ();
325
326                         Assert.AreEqual(new string[0], s.Font.Names, "F1");
327
328                         s.Font.Name = string.Empty;
329                         Assert.AreEqual(new string[0], s.Font.Names, "F2");
330
331                         s.Font.Names = null;
332                         Assert.AreEqual(new string[0], s.Font.Names, "F3");
333                 }
334
335                 [Test]
336                 [ExpectedException(typeof(ArgumentNullException))]
337                 public void NullException1 ()
338                 {
339                         Style s = new Style ();
340
341                         s.Font.Name = null;
342                 }
343
344                 private Style GetStyle ()
345                 {
346                         Style s = new Style ();
347                         s.BackColor = Color.Aqua;
348                         s.BorderWidth = Unit.Pixel (1);
349                         return s;
350                 }
351
352                 private void CheckStyle (Style s)
353                 {
354                         Assert.AreEqual (Color.Aqua, s.BackColor, "BackColor");
355                         Assert.AreEqual (Unit.Pixel (1), s.BorderWidth, "BorderWidth");
356                 }
357
358
359                 [Test]
360                 public void CopyFrom_Null ()
361                 {
362                         Style s = GetStyle ();
363                         s.CopyFrom (null);
364                         CheckStyle (s);
365                 }
366
367                 [Test]
368                 public void CopyFrom_Self ()
369                 {
370                         Style s = GetStyle ();
371                         s.CopyFrom (s);
372                         CheckStyle (s);
373                 }
374
375                 [Test]
376                 public void CopyFrom_Empty ()
377                 {
378                         StyleTestClass s = new StyleTestClass ();
379                         s.CopyFrom (new Style ());
380                         Assert.IsTrue (s.Empty, "Empty");
381                 }
382
383                 [Test]
384                 public void CopyFrom ()
385                 {
386                         Style c = GetStyle ();
387                         Style s = GetStyle ();
388
389                         c.BorderColor = Color.Azure;
390                         c.BorderWidth = Unit.Empty;
391
392                         c.CopyFrom (s);
393                         CheckStyle (c);
394
395                         Assert.AreEqual (Color.Azure, c.BorderColor, "BorderColor");
396                         // CopyFrom doesn't do a Reset
397                 }
398
399                 [Test]
400                 public void CopyFrom_IsEmpty ()
401                 {
402                         StyleTestClass c = new StyleTestClass ();
403                         Style s = GetStyle ();
404
405                         s.BorderColor = Color.Azure;
406                         s.BorderWidth = Unit.Empty;
407
408                         c.CopyFrom (s);
409
410                         Assert.IsFalse (c.Empty, "IsEmpty");
411                 }
412
413                 [Test]
414                 public void Constructor_StateBag_Null ()
415                 {
416                         StyleTestClass s = new StyleTestClass (null);
417                         Assert.IsNotNull (s.StateBag, "StateBag");
418                         s.CssClass = "mono";
419                         Assert.AreEqual ("mono", s.CssClass, "CssClass");
420                 }
421
422                 [Test]
423                 public void Empty ()
424                 {
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");
431                 }
432
433                 public void Render ()
434                 {
435                         HtmlTextWriter  writer;
436                         StyleTestClass  s;
437
438                         writer = StyleTest.GetWriter();
439                         s  = new StyleTestClass ();
440
441                         s.BorderColor = Color.BlueViolet;
442                         s.AddAttributesToRender(writer);
443                         // Figure out an order-independent way to verify rendered results
444                 }
445 #if NET_2_0
446                 [Test]
447                 public void IsStyleEmpty ()
448                 {
449                         Assert.IsTrue (Style.IsStyleEmpty (null), "null");
450                         Assert.IsTrue (Style.IsStyleEmpty (new Style ()), "new");
451                 }
452 #endif
453         }
454 }