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 {
140 [TestFixtureTearDown]
141 public void TearDown ()
146 private static HtmlTextWriter GetWriter () {
147 StringWriter sw = new StringWriter ();
149 return new HtmlTextWriter (sw);
152 private void SetSomeValues(Style s) {
153 s.BackColor = Color.Red;
154 s.ForeColor = Color.Green;
155 s.Width = new Unit(10);
159 private void SetAllValues(Style s) {
160 s.BackColor = Color.Red;
161 s.BorderColor = Color.Green;
162 s.BorderStyle = BorderStyle.None;
163 s.BorderWidth = new Unit(1);
164 s.CssClass = "Boing";
166 s.Font.Italic = true;
167 s.Font.Names = new string[2] {"namelist1", "namelist2"};
168 //s.Font.Name = string.Empty;
169 //s.Font.Names = new string[0];
170 //Console.WriteLine("Font name after setting name: {0}", s.Font.ToString());
171 s.Font.Overline = true;
172 s.Font.Size = new FontUnit(1);
173 //Console.WriteLine("Font name after setting size: {0}", s.Font.ToString());
174 s.Font.Strikeout = true;
175 s.Font.Underline = true;
176 s.ForeColor = Color.Blue;
177 s.Height = new Unit(2);
178 s.Width = new Unit(3);
181 private bool IsEqual(object[] a1, object[] a2, string assertion) {
185 if (a1.Length != a2.Length) {
186 if (assertion != null) {
187 Assert.Fail(assertion + "( different length )");
193 notfound = new bool[a1.Length];
195 for (int i = 0; i < a1.Length; i++) {
196 for (int j = 0; j < a2.Length; j++) {
197 if (a1[i].Equals(a2[j])) {
202 if ((assertion != null) && (matches != i+1)) {
203 Assert.Fail(assertion + "( missing " + a1[i].ToString() + " )");
207 return matches == a1.Length;
211 public void Style_Defaults ()
213 Style s = new Style ();
215 Assert.AreEqual (s.BackColor, Color.Empty, "Default1");
216 Assert.AreEqual (s.BorderColor, Color.Empty, "Default22");
217 Assert.AreEqual (s.BorderStyle, BorderStyle.NotSet, "Default3");
218 Assert.AreEqual (s.BorderWidth, Unit.Empty, "Default4");
219 Assert.AreEqual (s.CssClass, string.Empty, "Default5");
220 Assert.AreEqual (s.ForeColor, Color.Empty, "Default6");
221 Assert.AreEqual (s.Height, Unit.Empty, "Default7");
222 Assert.AreEqual (s.Width, Unit.Empty, "Default8");
226 public void Style_ViewState () {
227 Style s = new Style ();
228 ((IStateManager) s).TrackViewState ();
230 object state = ((IStateManager) s).SaveViewState ();
232 Style copy = new Style ();
233 ((IStateManager) copy).LoadViewState (state);
235 Assert.AreEqual (Color.Red, copy.BackColor, "ViewState1");
236 Assert.AreEqual (Color.Green, copy.ForeColor, "ViewState2");
237 Assert.AreEqual (new Unit (10), copy.Width, "ViewState3");
238 Assert.AreEqual (true, copy.Font.Bold, "ViewState4");
242 public void Style_ViewState2 () {
243 Style s = new Style (null);
244 ((IStateManager) s).TrackViewState ();
246 object state = ((IStateManager) s).SaveViewState ();
248 Assert.AreEqual (null, state, "ViewState2");
252 public void Style_ViewState3 () {
253 Style s = new Style (new StateBag());
254 ((IStateManager) s).TrackViewState ();
256 object state = ((IStateManager) s).SaveViewState ();
258 Assert.AreEqual (null, state, "ViewState3");
262 public void Style_State () {
271 "ForeColor=Color [Blue]",
273 "Font_Overline=True",
275 "BorderColor=Color [Green]",
276 "Font_Names=namelist1, namelist2",
277 "Font_Underline=True",
278 "BackColor=Color [Red]",
279 "Font_Strikeout=True" };
287 "ForeColor=Color [Blue]",
289 "Font_Overline=True",
291 "BorderColor=Color [Green]",
292 "Font_Underline=True",
293 "BackColor=Color [Red]",
294 "Font_Strikeout=True" };
302 "ForeColor=Color [Blue]",
304 "Font_Overline=True",
306 "BorderColor=Color [Green]",
308 "Font_Underline=True",
309 "BackColor=Color [Red]",
310 "Font_Strikeout=True" };
314 s = new StyleTestClass();
316 keyvalues = s.KeyValuePairs();
318 Assert.AreEqual (15, keyvalues.Length, "State1");
319 IsEqual(keyvalues, expect1, "State2");
321 s.Font.Name = string.Empty;
322 keyvalues = s.KeyValuePairs();
323 Assert.AreEqual (expect2.Length, keyvalues.Length, "State3");
324 IsEqual(keyvalues, expect2, "State4");
327 keyvalues = s.KeyValuePairs();
328 Assert.AreEqual (expect2.Length, keyvalues.Length, "State5");
329 IsEqual(keyvalues, expect2, "State6");
331 copy = new StyleTestClass();
333 keyvalues = copy.KeyValuePairs();
334 Assert.AreEqual (expect3.Length, keyvalues.Length, "State7");
335 IsEqual(keyvalues, expect3, "State8");
337 Assert.AreEqual (false, copy.IsTracking, "State9");
342 public void Style_Merge ()
344 Style s = new Style ();
345 Style copy = new Style ();
348 copy.ForeColor = Color.Blue;
351 Assert.AreEqual (Color.Red, copy.BackColor, "Merge1");
352 Assert.AreEqual (Color.Blue, copy.ForeColor, "Merge2");
355 copy.MergeWith(null);
359 public void Style_Copy ()
361 Style s = new Style ();
362 Style copy = new Style ();
367 Assert.AreEqual (Color.Red, s.BackColor, "Copy1");
372 public void Style_CssClass ()
374 StyleTestClass s = new StyleTestClass ();
376 Assert.AreEqual (String.Empty, s.RegisteredCssClass, "Css1");
378 s.SetCssClass ("blah");
379 Assert.AreEqual ("blah", s.RegisteredCssClass, "Css2");
381 s.BackColor = Color.AliceBlue;
382 Assert.AreEqual ("blah", s.RegisteredCssClass, "Css3");
386 [Category ("NunitWeb")]
387 public void Style_AddRegisteredCssClassAttribute () {
388 new WebTest (PageInvoker.CreateOnLoad (Style_AddRegisteredCssClassAttribute_Load)).Run ();
391 public static void Style_AddRegisteredCssClassAttribute_Load (Page p) {
392 StringWriter sw = new StringWriter ();
393 HtmlTextWriter tw = new HtmlTextWriter (sw);
394 Style s = new Style ();
395 s.CssClass = "MyClass";
396 s.BackColor = Color.AliceBlue;
397 s.AddAttributesToRender (tw);
398 tw.RenderBeginTag ("span");
400 Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass\""), "AddRegisteredCssClassAttribute#1");
401 Assert.AreEqual (true, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#2");
403 sw = new StringWriter ();
404 tw = new HtmlTextWriter (sw);
406 s.BackColor = Color.AliceBlue;
407 p.Header.StyleSheet.RegisterStyle (s, p);
408 s.AddAttributesToRender (tw);
409 tw.RenderBeginTag ("span");
411 Assert.AreEqual (true, sw.ToString ().Contains ("class"), "AddRegisteredCssClassAttribute#3");
412 Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#4");
414 sw = new StringWriter ();
415 tw = new HtmlTextWriter (sw);
417 s.BackColor = Color.AliceBlue;
418 s.CssClass = "MyClass";
419 p.Header.StyleSheet.RegisterStyle (s, p);
420 s.AddAttributesToRender (tw);
421 tw.RenderBeginTag ("span");
423 Assert.AreEqual (sw.ToString ().LastIndexOf ("class"), sw.ToString ().IndexOf ("class"), "AddRegisteredCssClassAttribute#5");
424 Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#6");
425 Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass "), "AddRegisteredCssClassAttribute#7");
428 p.Header.StyleSheet.RegisterStyle (s, p);
429 Assert.AreEqual (false, s.IsEmpty, "AddRegisteredCssClassAttribute#8");
433 public void Style_AddAttributesToRender_use_FillStyleAttributes () {
434 StringWriter sw = new StringWriter ();
435 HtmlTextWriter tw = new HtmlTextWriter (sw);
436 StyleTestClass s = new StyleTestClass ();
437 s.AddAttributesToRender (tw);
438 tw.RenderBeginTag ("span");
440 HtmlDiff.AssertAreEqual ("<span style=\"FillStyleAttributes:FillStyleAttributes;\" />", sw.ToString (), "AddAttributesToRender_use_FillStyleAttributes#2");
444 public void Style_GetStyleAttributes () {
446 CssStyleCollection css;
449 css = s.GetStyleAttributes (null);
450 Assert.AreEqual (0, css.Count, "GetStyleAttributes#1");
453 s.Font.Italic = true;
455 s.Font.Names = new string [] { "Arial", "Veranda" };
456 s.Font.Overline = true;
457 s.Font.Strikeout = true;
458 s.Font.Underline = true;
459 css = s.GetStyleAttributes (null);
460 Assert.AreEqual ("bold", css ["font-weight"], "GetStyleAttributes#2");
461 Assert.AreEqual ("italic", css ["font-style"], "GetStyleAttributes#3");
462 Assert.AreEqual ("10pt", css ["font-size"], "GetStyleAttributes#4");
463 Assert.AreEqual ("Arial,Veranda", css ["font-family"], "GetStyleAttributes#5");
464 Assert.AreEqual (true, css ["text-decoration"].Contains ("overline"), "GetStyleAttributes#6");
465 Assert.AreEqual (true, css ["text-decoration"].Contains ("line-through"), "GetStyleAttributes#7");
466 Assert.AreEqual (true, css ["text-decoration"].Contains ("underline"), "GetStyleAttributes#8");
469 css = s.GetStyleAttributes (null);
470 Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#9");
472 s.Font.Name = "Arial, Veranda";
473 css = s.GetStyleAttributes (null);
474 Assert.AreEqual ("Arial, Veranda", css ["font-family"], "GetStyleAttributes#10");
477 css = s.GetStyleAttributes (null);
478 Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#11");
481 s.Font.Italic = false;
482 s.Font.Size = FontUnit.Empty;
483 s.Font.Overline = false;
484 s.Font.Strikeout = false;
485 s.Font.Underline = false;
486 css = s.GetStyleAttributes (null);
487 Assert.AreEqual ("normal", css ["font-weight"], "GetStyleAttributes#12");
488 Assert.AreEqual ("normal", css ["font-style"], "GetStyleAttributes#13");
489 Assert.AreEqual (null, css ["font-size"], "GetStyleAttributes#14");
490 Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#15");
493 css = s.GetStyleAttributes (null);
494 Assert.AreEqual (0, css.Count, "GetStyleAttributes#16");
497 s.Font.Underline = false;
498 css = s.GetStyleAttributes (null);
499 Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#17");
503 s.BorderStyle = BorderStyle.Dashed;
504 css = s.GetStyleAttributes (null);
505 Assert.AreEqual ("Dashed", css ["border-style"], "GetStyleAttributes#18");
506 Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#19");
508 s.BorderStyle = BorderStyle.NotSet;
509 css = s.GetStyleAttributes (null);
510 Assert.AreEqual ("solid", css ["border-style"], "GetStyleAttributes#20");
511 Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#21");
514 css = s.GetStyleAttributes (null);
515 Assert.AreEqual (null, css ["border-style"], "GetStyleAttributes#22");
516 Assert.AreEqual ("0px", css ["border-width"], "GetStyleAttributes#23");
521 public void StyleFonts () {
522 Style s = new Style ();
524 Assert.AreEqual(new string[0], s.Font.Names, "F1");
526 s.Font.Name = string.Empty;
527 Assert.AreEqual(new string[0], s.Font.Names, "F2");
530 Assert.AreEqual(new string[0], s.Font.Names, "F3");
534 [ExpectedException(typeof(ArgumentNullException))]
535 public void NullException1 ()
537 Style s = new Style ();
542 private Style GetStyle ()
544 Style s = new Style ();
545 s.BackColor = Color.Aqua;
546 s.BorderWidth = Unit.Pixel (1);
550 private void CheckStyle (Style s)
552 Assert.AreEqual (Color.Aqua, s.BackColor, "BackColor");
553 Assert.AreEqual (Unit.Pixel (1), s.BorderWidth, "BorderWidth");
558 public void CopyFrom_Null ()
560 Style s = GetStyle ();
566 public void CopyFrom_Self ()
568 Style s = GetStyle ();
574 public void CopyFrom_Empty ()
576 StyleTestClass s = new StyleTestClass ();
577 s.CopyFrom (new Style ());
578 Assert.IsTrue (s.Empty, "Empty");
582 public void CopyFrom ()
584 Style c = GetStyle ();
585 Style s = GetStyle ();
587 c.BorderColor = Color.Azure;
588 c.BorderWidth = Unit.Empty;
593 Assert.AreEqual (Color.Azure, c.BorderColor, "BorderColor");
594 // CopyFrom doesn't do a Reset
598 public void CopyFrom_IsEmpty ()
600 StyleTestClass c = new StyleTestClass ();
601 Style s = GetStyle ();
603 s.BorderColor = Color.Azure;
604 s.BorderWidth = Unit.Empty;
608 Assert.IsFalse (c.Empty, "IsEmpty");
612 public void Constructor_StateBag_Null ()
614 StyleTestClass s = new StyleTestClass (null);
615 Assert.IsNotNull (s.StateBag, "StateBag");
617 Assert.AreEqual ("mono", s.CssClass, "CssClass");
623 StyleTestClass s = new StyleTestClass ();
624 Assert.IsTrue (s.Empty, "Empty");
625 Assert.AreEqual (0, s.StateBag.Count, "Count");
626 s.StateBag["Mono"] = "go!";
627 Assert.IsTrue (s.Empty, "Still Empty");
628 Assert.AreEqual (1, s.StateBag.Count, "Count");
632 public void FontInfo_Empty ()
635 StyleTestClass s = new StyleTestClass ();
637 Assert.IsTrue (s.Empty, "Empty after getter");
638 s.Font.Name = "Arial";
639 Assert.IsFalse (s.Empty, "No longer empty");
643 public void SetBitCalledWhenSetProperty () {
644 StyleTestClass s = new StyleTestClass ();
646 s.SetBitCalledFlag = false;
647 s.BackColor = Color.Aqua;
648 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BackColor");
649 Assert.AreEqual (0x08, s.SetBitCalledValue, "SetBit() was called with wrong argument : BackColor");
651 s.SetBitCalledFlag = false;
652 s.BorderColor = Color.Blue;
653 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderColor");
654 Assert.AreEqual (0x10, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderColor");
656 s.SetBitCalledFlag = false;
657 s.BorderStyle = BorderStyle.Dashed;
658 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderStyle");
659 Assert.AreEqual (0x40, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderStyle");
661 s.SetBitCalledFlag = false;
663 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderWidth");
664 Assert.AreEqual (0x20, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderWidth");
666 s.SetBitCalledFlag = false;
667 s.CssClass = "class";
668 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : CssClass");
669 Assert.AreEqual (0x02, s.SetBitCalledValue, "SetBit() was called with wrong argument : CssClass");
671 s.SetBitCalledFlag = false;
672 s.ForeColor = Color.Red;
673 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : ForeColor");
674 Assert.AreEqual (0x04, s.SetBitCalledValue, "SetBit() was called with wrong argument : ForeColor");
676 s.SetBitCalledFlag = false;
678 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Height");
679 Assert.AreEqual (0x80, s.SetBitCalledValue, "SetBit() was called with wrong argument : Height");
681 s.SetBitCalledFlag = false;
683 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Width");
684 Assert.AreEqual (0x100, s.SetBitCalledValue, "SetBit() was called with wrong argument : Width");
686 s.SetBitCalledFlag = false;
688 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Bold");
689 Assert.AreEqual (0x800, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Bold");
691 s.SetBitCalledFlag = false;
692 s.Font.Italic = true;
693 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Italic");
694 Assert.AreEqual (0x1000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Italic");
696 s.SetBitCalledFlag = false;
697 s.Font.Overline = true;
698 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Overline");
699 Assert.AreEqual (0x4000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Overline");
701 s.SetBitCalledFlag = false;
702 s.Font.Underline = true;
703 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Underline");
704 Assert.AreEqual (0x2000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Underline");
706 s.SetBitCalledFlag = false;
707 s.Font.Strikeout = true;
708 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Strikeout");
709 Assert.AreEqual (0x8000, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Strikeout");
711 s.SetBitCalledFlag = false;
712 s.Font.Names = new string [] { "Arial" };
713 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Names");
714 Assert.AreEqual (0x200, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Strikeout");
716 s.SetBitCalledFlag = false;
717 s.Font.Size = new FontUnit (10);
718 Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Font.Size");
719 Assert.AreEqual (0x400, s.SetBitCalledValue, "SetBit() was called with wrong argument : Font.Size");
722 public void Render ()
724 HtmlTextWriter writer;
727 writer = StyleTest.GetWriter();
728 s = new StyleTestClass ();
730 s.BorderColor = Color.BlueViolet;
731 s.AddAttributesToRender(writer);
732 // Figure out an order-independent way to verify rendered results
735 class PokerStyle : Style
737 public IUrlResolutionService UrlResolver;
739 protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver)
741 UrlResolver = urlResolver;
742 base.FillStyleAttributes (attributes, urlResolver);
746 class PokerWebControl : WebControl
748 protected override Style CreateControlStyle ()
750 return new PokerStyle ();
755 public void FillStyleAttributes_UrlResolver ()
757 PokerWebControl c = new PokerWebControl ();
758 c.BackColor = Color.AliceBlue;
759 c.RenderControl (new HtmlTextWriter (new StringWriter ()));
761 Assert.AreEqual (c, ((PokerStyle) c.ControlStyle).UrlResolver);