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;
41 using MonoTests.SystemWeb.Framework;
42 using MonoTests.stand_alone.WebHarness;
44 namespace MonoTests.System.Web.UI.WebControls
46 public class StyleTestClass : Style {
48 public StyleTestClass ()
53 public StyleTestClass (StateBag bag)
58 public StateBag StateBag {
59 get { return base.ViewState; }
63 get { return base.IsEmpty; }
66 public bool IsTracking {
67 get { return base.IsTrackingViewState; }
70 public void SetCssClass(string name) {
71 Type style = typeof (Style);
73 refl.MethodInfo methodInfo = style.GetMethod("SetRegisteredCssClass",refl.BindingFlags.NonPublic | refl.BindingFlags.Instance);
74 if (methodInfo != null) {
75 object[] parameters = new object[] {name};
76 methodInfo.Invoke(this, parameters);
81 public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner) {
82 base.AddAttributesToRender (writer, owner);
85 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver) {
86 base.FillStyleAttributes (attributes, urlResolver);
87 attributes.Add ("FillStyleAttributes", "FillStyleAttributes");
91 public string[] KeyValuePairs() {
96 e = ViewState.GetEnumerator();
97 result = new string[ViewState.Keys.Count];
100 while (e.MoveNext()) {
104 d = (DictionaryEntry)e.Current;
105 si = (StateItem)d.Value;
107 if (si.Value is String[]) {
110 values = (string[]) si.Value;
111 result[item] = d.Key.ToString() + "=";
112 if (values.Length > 0) {
113 result[item] += values[0];
115 for (int i = 1; i < values.Length; i++) {
116 result[item] += ", " + values[i];
120 result[item] = d.Key.ToString() + "=" + si.Value;
128 public bool SetBitCalledFlag = false;
129 public int SetBitCalledValue = 0;
130 protected override void SetBit (int bit) {
131 SetBitCalledFlag = true;
132 SetBitCalledValue = bit;
138 public class StyleTest {
139 private static HtmlTextWriter GetWriter () {
140 StringWriter sw = new StringWriter ();
142 return new HtmlTextWriter (sw);
145 private void SetSomeValues(Style s) {
146 s.BackColor = Color.Red;
147 s.ForeColor = Color.Green;
148 s.Width = new Unit(10);
152 private void SetAllValues(Style s) {
153 s.BackColor = Color.Red;
154 s.BorderColor = Color.Green;
155 s.BorderStyle = BorderStyle.None;
156 s.BorderWidth = new Unit(1);
157 s.CssClass = "Boing";
159 s.Font.Italic = true;
160 s.Font.Names = new string[2] {"namelist1", "namelist2"};
161 //s.Font.Name = string.Empty;
162 //s.Font.Names = new string[0];
163 //Console.WriteLine("Font name after setting name: {0}", s.Font.ToString());
164 s.Font.Overline = true;
165 s.Font.Size = new FontUnit(1);
166 //Console.WriteLine("Font name after setting size: {0}", s.Font.ToString());
167 s.Font.Strikeout = true;
168 s.Font.Underline = true;
169 s.ForeColor = Color.Blue;
170 s.Height = new Unit(2);
171 s.Width = new Unit(3);
174 private bool IsEqual(object[] a1, object[] a2, string assertion) {
178 if (a1.Length != a2.Length) {
179 if (assertion != null) {
180 Assert.Fail(assertion + "( different length )");
186 notfound = new bool[a1.Length];
188 for (int i = 0; i < a1.Length; i++) {
189 for (int j = 0; j < a2.Length; j++) {
190 if (a1[i].Equals(a2[j])) {
195 if ((assertion != null) && (matches != i+1)) {
196 Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
200 return matches == a1.Length;
204 public void Style_Defaults ()
206 Style s = new Style ();
208 Assert.AreEqual (s.BackColor, Color.Empty, "Default1");
209 Assert.AreEqual (s.BorderColor, Color.Empty, "Default22");
210 Assert.AreEqual (s.BorderStyle, BorderStyle.NotSet, "Default3");
211 Assert.AreEqual (s.BorderWidth, Unit.Empty, "Default4");
212 Assert.AreEqual (s.CssClass, string.Empty, "Default5");
213 Assert.AreEqual (s.ForeColor, Color.Empty, "Default6");
214 Assert.AreEqual (s.Height, Unit.Empty, "Default7");
215 Assert.AreEqual (s.Width, Unit.Empty, "Default8");
220 public void Style_State () {
229 "ForeColor=Color [Blue]",
231 "Font_Overline=True",
233 "BorderColor=Color [Green]",
234 "Font_Names=namelist1, namelist2",
235 "Font_Underline=True",
236 "BackColor=Color [Red]",
237 "Font_Strikeout=True" };
245 "ForeColor=Color [Blue]",
247 "Font_Overline=True",
249 "BorderColor=Color [Green]",
250 "Font_Underline=True",
251 "BackColor=Color [Red]",
252 "Font_Strikeout=True" };
260 "ForeColor=Color [Blue]",
262 "Font_Overline=True",
264 "BorderColor=Color [Green]",
266 "Font_Underline=True",
267 "BackColor=Color [Red]",
268 "Font_Strikeout=True" };
272 s = new StyleTestClass();
274 keyvalues = s.KeyValuePairs();
276 Assert.AreEqual (15, keyvalues.Length, "State1");
277 IsEqual(keyvalues, expect1, "State2");
279 s.Font.Name = string.Empty;
280 keyvalues = s.KeyValuePairs();
281 Assert.AreEqual (expect2.Length, keyvalues.Length, "State3");
282 IsEqual(keyvalues, expect2, "State4");
285 keyvalues = s.KeyValuePairs();
286 Assert.AreEqual (expect2.Length, keyvalues.Length, "State5");
287 IsEqual(keyvalues, expect2, "State6");
289 copy = new StyleTestClass();
291 keyvalues = copy.KeyValuePairs();
292 Assert.AreEqual (expect3.Length, keyvalues.Length, "State7");
293 IsEqual(keyvalues, expect3, "State8");
295 Assert.AreEqual (false, copy.IsTracking, "State9");
300 public void Style_Merge ()
302 Style s = new Style ();
303 Style copy = new Style ();
306 copy.ForeColor = Color.Blue;
309 Assert.AreEqual (Color.Red, copy.BackColor, "Merge1");
310 Assert.AreEqual (Color.Blue, copy.ForeColor, "Merge2");
313 copy.MergeWith(null);
317 public void Style_Copy ()
319 Style s = new Style ();
320 Style copy = new Style ();
325 Assert.AreEqual (Color.Red, s.BackColor, "Copy1");
330 public void Style_CssClass ()
332 StyleTestClass s = new StyleTestClass ();
334 Assert.AreEqual (String.Empty, s.RegisteredCssClass, "Css1");
336 s.SetCssClass ("blah");
337 Assert.AreEqual ("blah", s.RegisteredCssClass, "Css2");
339 s.BackColor = Color.AliceBlue;
340 Assert.AreEqual ("blah", s.RegisteredCssClass, "Css3");
344 [Category ("NunitWeb")]
345 public void Style_AddRegisteredCssClassAttribute () {
346 new WebTest (PageInvoker.CreateOnLoad (Style_AddRegisteredCssClassAttribute_Load)).Run ();
349 public static void Style_AddRegisteredCssClassAttribute_Load (Page p) {
350 StringWriter sw = new StringWriter ();
351 HtmlTextWriter tw = new HtmlTextWriter (sw);
352 Style s = new Style ();
353 s.CssClass = "MyClass";
354 s.BackColor = Color.AliceBlue;
355 s.AddAttributesToRender (tw);
356 tw.RenderBeginTag ("span");
358 Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass\""), "AddRegisteredCssClassAttribute#1");
359 Assert.AreEqual (true, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#2");
361 sw = new StringWriter ();
362 tw = new HtmlTextWriter (sw);
364 s.BackColor = Color.AliceBlue;
365 p.Header.StyleSheet.RegisterStyle (s, p);
366 s.AddAttributesToRender (tw);
367 tw.RenderBeginTag ("span");
369 Assert.AreEqual (true, sw.ToString ().Contains ("class"), "AddRegisteredCssClassAttribute#3");
370 Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#4");
372 sw = new StringWriter ();
373 tw = new HtmlTextWriter (sw);
375 s.BackColor = Color.AliceBlue;
376 s.CssClass = "MyClass";
377 p.Header.StyleSheet.RegisterStyle (s, p);
378 s.AddAttributesToRender (tw);
379 tw.RenderBeginTag ("span");
381 Assert.AreEqual (sw.ToString ().LastIndexOf ("class"), sw.ToString ().IndexOf ("class"), "AddRegisteredCssClassAttribute#5");
382 Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#6");
383 Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass "), "AddRegisteredCssClassAttribute#7");
386 p.Header.StyleSheet.RegisterStyle (s, p);
387 Assert.AreEqual (false, s.IsEmpty, "AddRegisteredCssClassAttribute#8");
391 public void Style_AddAttributesToRender_use_FillStyleAttributes () {
392 StringWriter sw = new StringWriter ();
393 HtmlTextWriter tw = new HtmlTextWriter (sw);
394 StyleTestClass s = new StyleTestClass ();
395 s.AddAttributesToRender (tw);
396 tw.RenderBeginTag ("span");
398 HtmlDiff.AssertAreEqual ("<span style=\"FillStyleAttributes:FillStyleAttributes;\" />", sw.ToString (), "AddAttributesToRender_use_FillStyleAttributes#2");
402 public void Style_GetStyleAttributes () {
404 CssStyleCollection css;
407 css = s.GetStyleAttributes (null);
408 Assert.AreEqual (0, css.Count, "GetStyleAttributes#1");
411 s.Font.Italic = true;
413 s.Font.Names = new string [] { "Arial", "Veranda" };
414 s.Font.Overline = true;
415 s.Font.Strikeout = true;
416 s.Font.Underline = true;
417 css = s.GetStyleAttributes (null);
418 Assert.AreEqual ("bold", css ["font-weight"], "GetStyleAttributes#2");
419 Assert.AreEqual ("italic", css ["font-style"], "GetStyleAttributes#3");
420 Assert.AreEqual ("10pt", css ["font-size"], "GetStyleAttributes#4");
421 Assert.AreEqual ("Arial,Veranda", css ["font-family"], "GetStyleAttributes#5");
422 Assert.AreEqual (true, css ["text-decoration"].Contains ("overline"), "GetStyleAttributes#6");
423 Assert.AreEqual (true, css ["text-decoration"].Contains ("line-through"), "GetStyleAttributes#7");
424 Assert.AreEqual (true, css ["text-decoration"].Contains ("underline"), "GetStyleAttributes#8");
427 css = s.GetStyleAttributes (null);
428 Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#9");
430 s.Font.Name = "Arial, Veranda";
431 css = s.GetStyleAttributes (null);
432 Assert.AreEqual ("Arial, Veranda", css ["font-family"], "GetStyleAttributes#10");
435 css = s.GetStyleAttributes (null);
436 Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#11");
439 s.Font.Italic = false;
440 s.Font.Size = FontUnit.Empty;
441 s.Font.Overline = false;
442 s.Font.Strikeout = false;
443 s.Font.Underline = false;
444 css = s.GetStyleAttributes (null);
445 Assert.AreEqual ("normal", css ["font-weight"], "GetStyleAttributes#12");
446 Assert.AreEqual ("normal", css ["font-style"], "GetStyleAttributes#13");
447 Assert.AreEqual (null, css ["font-size"], "GetStyleAttributes#14");
448 Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#15");
451 css = s.GetStyleAttributes (null);
452 Assert.AreEqual (0, css.Count, "GetStyleAttributes#16");
455 s.Font.Underline = false;
456 css = s.GetStyleAttributes (null);
457 Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#17");
461 s.BorderStyle = BorderStyle.Dashed;
462 css = s.GetStyleAttributes (null);
463 Assert.AreEqual ("Dashed", css ["border-style"], "GetStyleAttributes#18");
464 Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#19");
466 s.BorderStyle = BorderStyle.NotSet;
467 css = s.GetStyleAttributes (null);
468 Assert.AreEqual ("solid", css ["border-style"], "GetStyleAttributes#20");
469 Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#21");
472 css = s.GetStyleAttributes (null);
473 Assert.AreEqual (null, css ["border-style"], "GetStyleAttributes#22");
474 Assert.AreEqual ("0px", css ["border-width"], "GetStyleAttributes#23");
479 public void StyleFonts () {
480 Style s = new Style ();
482 Assert.AreEqual(new string[0], s.Font.Names, "F1");
484 s.Font.Name = string.Empty;
485 Assert.AreEqual(new string[0], s.Font.Names, "F2");
488 Assert.AreEqual(new string[0], s.Font.Names, "F3");
492 [ExpectedException(typeof(ArgumentNullException))]
493 public void NullException1 ()
495 Style s = new Style ();
500 private Style GetStyle ()
502 Style s = new Style ();
503 s.BackColor = Color.Aqua;
504 s.BorderWidth = Unit.Pixel (1);
508 private void CheckStyle (Style s)
510 Assert.AreEqual (Color.Aqua, s.BackColor, "BackColor");
511 Assert.AreEqual (Unit.Pixel (1), s.BorderWidth, "BorderWidth");
516 public void CopyFrom_Null ()
518 Style s = GetStyle ();
524 public void CopyFrom_Self ()
526 Style s = GetStyle ();
532 public void CopyFrom_Empty ()
534 StyleTestClass s = new StyleTestClass ();
535 s.CopyFrom (new Style ());
536 Assert.IsTrue (s.Empty, "Empty");
540 public void CopyFrom ()
542 Style c = GetStyle ();
543 Style s = GetStyle ();
545 c.BorderColor = Color.Azure;
546 c.BorderWidth = Unit.Empty;
551 Assert.AreEqual (Color.Azure, c.BorderColor, "BorderColor");
552 // CopyFrom doesn't do a Reset
556 public void CopyFrom_IsEmpty ()
558 StyleTestClass c = new StyleTestClass ();
559 Style s = GetStyle ();
561 s.BorderColor = Color.Azure;
562 s.BorderWidth = Unit.Empty;
566 Assert.IsFalse (c.Empty, "IsEmpty");
570 public void Constructor_StateBag_Null ()
572 StyleTestClass s = new StyleTestClass (null);
573 Assert.IsNotNull (s.StateBag, "StateBag");
575 Assert.AreEqual ("mono", s.CssClass, "CssClass");
581 StyleTestClass s = new StyleTestClass ();
582 Assert.IsTrue (s.Empty, "Empty");
583 Assert.AreEqual (0, s.StateBag.Count, "Count");
584 s.StateBag["Mono"] = "go!";
585 Assert.IsTrue (s.Empty, "Still Empty");
586 Assert.AreEqual (1, s.StateBag.Count, "Count");
590 public void FontInfo_Empty ()
593 StyleTestClass s = new StyleTestClass ();
595 Assert.IsTrue (s.Empty, "Empty after getter");
596 s.Font.Name = "Arial";
597 Assert.IsFalse (s.Empty, "No longer empty");
601 public void SetBitCalledWhenSetProperty () {
602 StyleTestClass s = new StyleTestClass ();
604 s.SetBitCalledFlag = false;
605 s.BackColor = Color.Aqua;
606 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BackColor");
607 Assert.AreEqual (0x08, s.SetBitCalledValue, "SetBit() was called with wrong argument : BackColor");
609 s.SetBitCalledFlag = false;
610 s.BorderColor = Color.Blue;
611 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderColor");
612 Assert.AreEqual (0x10, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderColor");
614 s.SetBitCalledFlag = false;
615 s.BorderStyle = BorderStyle.Dashed;
616 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderStyle");
617 Assert.AreEqual (0x40, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderStyle");
619 s.SetBitCalledFlag = false;
621 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderWidth");
622 Assert.AreEqual (0x20, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderWidth");
624 s.SetBitCalledFlag = false;
625 s.CssClass = "class";
626 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : CssClass");
627 Assert.AreEqual (0x02, s.SetBitCalledValue, "SetBit() was called with wrong argument : CssClass");
629 s.SetBitCalledFlag = false;
630 s.ForeColor = Color.Red;
631 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : ForeColor");
632 Assert.AreEqual (0x04, s.SetBitCalledValue, "SetBit() was called with wrong argument : ForeColor");
634 s.SetBitCalledFlag = false;
636 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Height");
637 Assert.AreEqual (0x80, s.SetBitCalledValue, "SetBit() was called with wrong argument : Height");
639 s.SetBitCalledFlag = false;
641 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Width");
642 Assert.AreEqual (0x100, s.SetBitCalledValue, "SetBit() was called with wrong argument : Width");
645 public void Render ()
647 HtmlTextWriter writer;
650 writer = StyleTest.GetWriter();
651 s = new StyleTestClass ();
653 s.BorderColor = Color.BlueViolet;
654 s.AddAttributesToRender(writer);
655 // Figure out an order-independent way to verify rendered results