2006-11-22 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / StyleTest.cs
index 69d780eafea1dfeefb4e896787ca5774cd8f3fb5..8c62248f5cae1435775f95160e52144f7874ef70 100644 (file)
@@ -38,6 +38,8 @@ using refl = System.Reflection;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
+using MonoTests.SystemWeb.Framework;
+using MonoTests.stand_alone.WebHarness;
 
 namespace MonoTests.System.Web.UI.WebControls
 {
@@ -66,7 +68,7 @@ namespace MonoTests.System.Web.UI.WebControls
                }
 #if NET_2_0
                public void SetCssClass(string name) {
-                       Type style = Type.GetType("System.Web.UI.WebControls.Style, System.Web");
+                       Type style = typeof (Style);
                        if (style != null) {    
                                refl.MethodInfo methodInfo = style.GetMethod("SetRegisteredCssClass",refl.BindingFlags.NonPublic | refl.BindingFlags.Instance);
                                if (methodInfo != null) {
@@ -75,6 +77,15 @@ namespace MonoTests.System.Web.UI.WebControls
                                }
                        }
                }
+
+               public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner) {
+                       base.AddAttributesToRender (writer, owner);
+               }
+
+               protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver) {
+                       base.FillStyleAttributes (attributes, urlResolver);
+                       attributes.Add ("FillStyleAttributes", "FillStyleAttributes");
+               }
 #endif
 
                public string[] KeyValuePairs() {
@@ -113,6 +124,14 @@ namespace MonoTests.System.Web.UI.WebControls
 
                        return result;
                }
+
+               public bool SetBitCalledFlag = false;
+               public int SetBitCalledValue = 0;
+               protected override void SetBit (int bit) {
+                       SetBitCalledFlag = true;
+                       SetBitCalledValue = bit;
+                       base.SetBit (bit);
+               }
        }
 
        [TestFixture]   
@@ -312,10 +331,147 @@ namespace MonoTests.System.Web.UI.WebControls
                {
                        StyleTestClass s = new StyleTestClass ();
 
-                       Assert.AreEqual (null, s.RegisteredCssClass, "Css1");
+                       Assert.AreEqual (String.Empty, s.RegisteredCssClass, "Css1");
 
-                       s.SetCssClass("blah");
+                       s.SetCssClass ("blah");
                        Assert.AreEqual ("blah", s.RegisteredCssClass, "Css2");
+
+                       s.BackColor = Color.AliceBlue;
+                       Assert.AreEqual ("blah", s.RegisteredCssClass, "Css3");
+               }
+
+               [Test]
+               [Category ("NunitWeb")]
+               public void Style_AddRegisteredCssClassAttribute () {
+                       new WebTest (PageInvoker.CreateOnLoad (Style_AddRegisteredCssClassAttribute_Load)).Run ();
+               }
+               
+               public static void Style_AddRegisteredCssClassAttribute_Load (Page p) {
+                       StringWriter sw = new StringWriter ();
+                       HtmlTextWriter tw = new HtmlTextWriter (sw);
+                       Style s = new Style ();
+                       s.CssClass = "MyClass";
+                       s.BackColor = Color.AliceBlue;
+                       s.AddAttributesToRender (tw);
+                       tw.RenderBeginTag ("span");
+                       tw.RenderEndTag ();
+                       Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass\""), "AddRegisteredCssClassAttribute#1");
+                       Assert.AreEqual (true, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#2");
+                       
+                       sw = new StringWriter ();
+                       tw = new HtmlTextWriter (sw);
+                       s = new Style ();
+                       s.BackColor = Color.AliceBlue;
+                       p.Header.StyleSheet.RegisterStyle (s, p);
+                       s.AddAttributesToRender (tw);
+                       tw.RenderBeginTag ("span");
+                       tw.RenderEndTag ();
+                       Assert.AreEqual (true, sw.ToString ().Contains ("class"), "AddRegisteredCssClassAttribute#3");
+                       Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#4");
+                       
+                       sw = new StringWriter ();
+                       tw = new HtmlTextWriter (sw);
+                       s = new Style ();
+                       s.BackColor = Color.AliceBlue;
+                       s.CssClass = "MyClass";
+                       p.Header.StyleSheet.RegisterStyle (s, p);
+                       s.AddAttributesToRender (tw);
+                       tw.RenderBeginTag ("span");
+                       tw.RenderEndTag ();
+                       Assert.AreEqual (sw.ToString ().LastIndexOf ("class"), sw.ToString ().IndexOf ("class"), "AddRegisteredCssClassAttribute#5");
+                       Assert.AreEqual (false, sw.ToString ().Contains ("style"), "AddRegisteredCssClassAttribute#6");
+                       Assert.AreEqual (true, sw.ToString ().Contains ("class=\"MyClass "), "AddRegisteredCssClassAttribute#7");
+
+                       s = new Style ();
+                       p.Header.StyleSheet.RegisterStyle (s, p);
+                       Assert.AreEqual (false, s.IsEmpty, "AddRegisteredCssClassAttribute#8");
+               }
+
+               [Test]
+               public void Style_AddAttributesToRender_use_FillStyleAttributes () {
+                       StringWriter sw = new StringWriter ();
+                       HtmlTextWriter tw = new HtmlTextWriter (sw);
+                       StyleTestClass s = new StyleTestClass ();
+                       s.AddAttributesToRender (tw);
+                       tw.RenderBeginTag ("span");
+                       tw.RenderEndTag ();
+                       HtmlDiff.AssertAreEqual ("<span style=\"FillStyleAttributes:FillStyleAttributes;\" />", sw.ToString (), "AddAttributesToRender_use_FillStyleAttributes#2");
+               }
+
+               [Test]
+               public void Style_GetStyleAttributes () {
+                       Style s;
+                       CssStyleCollection css;
+
+                       s = new Style ();
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual (0, css.Count, "GetStyleAttributes#1");
+
+                       s.Font.Bold = true;
+                       s.Font.Italic = true;
+                       s.Font.Size = 10;
+                       s.Font.Names = new string [] { "Arial", "Veranda" };
+                       s.Font.Overline = true;
+                       s.Font.Strikeout = true;
+                       s.Font.Underline = true;
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual ("bold", css ["font-weight"], "GetStyleAttributes#2");
+                       Assert.AreEqual ("italic", css ["font-style"], "GetStyleAttributes#3");
+                       Assert.AreEqual ("10pt", css ["font-size"], "GetStyleAttributes#4");
+                       Assert.AreEqual ("Arial,Veranda", css ["font-family"], "GetStyleAttributes#5");
+                       Assert.AreEqual (true, css ["text-decoration"].Contains ("overline"), "GetStyleAttributes#6");
+                       Assert.AreEqual (true, css ["text-decoration"].Contains ("line-through"), "GetStyleAttributes#7");
+                       Assert.AreEqual (true, css ["text-decoration"].Contains ("underline"), "GetStyleAttributes#8");
+
+                       s.Font.Names = null;
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#9");
+
+                       s.Font.Name = "Arial, Veranda";
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual ("Arial, Veranda", css ["font-family"], "GetStyleAttributes#10");
+
+                       s.Font.Name = "";
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#11");
+
+                       s.Font.Bold = false;
+                       s.Font.Italic = false;
+                       s.Font.Size = FontUnit.Empty;
+                       s.Font.Overline = false;
+                       s.Font.Strikeout = false;
+                       s.Font.Underline = false;
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual ("normal", css ["font-weight"], "GetStyleAttributes#12");
+                       Assert.AreEqual ("normal", css ["font-style"], "GetStyleAttributes#13");
+                       Assert.AreEqual (null, css ["font-size"], "GetStyleAttributes#14");
+                       Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#15");
+
+                       s.Reset ();
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual (0, css.Count, "GetStyleAttributes#16");
+
+                       s.Reset ();
+                       s.Font.Underline = false;
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#17");
+
+                       s.Reset ();
+                       s.BorderWidth = 1;
+                       s.BorderStyle = BorderStyle.Dashed;
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual ("Dashed", css ["border-style"], "GetStyleAttributes#18");
+                       Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#19");
+
+                       s.BorderStyle = BorderStyle.NotSet;
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual ("solid", css ["border-style"], "GetStyleAttributes#20");
+                       Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#21");
+
+                       s.BorderWidth = 0;
+                       css = s.GetStyleAttributes (null);
+                       Assert.AreEqual (null, css ["border-style"], "GetStyleAttributes#22");
+                       Assert.AreEqual ("0px", css ["border-width"], "GetStyleAttributes#23");
                }
 #endif
 
@@ -440,6 +596,51 @@ namespace MonoTests.System.Web.UI.WebControls
                        s.Font.Name = "Arial";
                        Assert.IsFalse (s.Empty, "No longer empty");
                }
+               
+               [Test]
+               public void SetBitCalledWhenSetProperty () {
+                       StyleTestClass s = new StyleTestClass ();
+
+                       s.SetBitCalledFlag = false;
+                       s.BackColor = Color.Aqua;
+                       Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BackColor");
+                       Assert.AreEqual (0x08, s.SetBitCalledValue, "SetBit() was called with wrong argument : BackColor");
+
+                       s.SetBitCalledFlag = false;
+                       s.BorderColor = Color.Blue;
+                       Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderColor");
+                       Assert.AreEqual (0x10, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderColor");
+
+                       s.SetBitCalledFlag = false;
+                       s.BorderStyle = BorderStyle.Dashed;
+                       Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderStyle");
+                       Assert.AreEqual (0x40, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderStyle");
+
+                       s.SetBitCalledFlag = false;
+                       s.BorderWidth = 1;
+                       Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : BorderWidth");
+                       Assert.AreEqual (0x20, s.SetBitCalledValue, "SetBit() was called with wrong argument : BorderWidth");
+
+                       s.SetBitCalledFlag = false;
+                       s.CssClass = "class";
+                       Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : CssClass");
+                       Assert.AreEqual (0x02, s.SetBitCalledValue, "SetBit() was called with wrong argument : CssClass");
+
+                       s.SetBitCalledFlag = false;
+                       s.ForeColor = Color.Red;
+                       Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : ForeColor");
+                       Assert.AreEqual (0x04, s.SetBitCalledValue, "SetBit() was called with wrong argument : ForeColor");
+
+                       s.SetBitCalledFlag = false;
+                       s.Height = 1;
+                       Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Height");
+                       Assert.AreEqual (0x80, s.SetBitCalledValue, "SetBit() was called with wrong argument : Height");
+
+                       s.SetBitCalledFlag = false;
+                       s.Width = 1;
+                       Assert.IsTrue (s.SetBitCalledFlag, "SetBit() was not called : Width");
+                       Assert.AreEqual (0x100, s.SetBitCalledValue, "SetBit() was called with wrong argument : Width");
+               }
 
                public void Render ()
                {
@@ -453,13 +654,5 @@ namespace MonoTests.System.Web.UI.WebControls
                        s.AddAttributesToRender(writer);
                        // Figure out an order-independent way to verify rendered results
                }
-#if NET_2_0
-               [Test]
-               public void IsStyleEmpty ()
-               {
-                       Assert.IsTrue (Style.IsStyleEmpty (null), "null");
-                       Assert.IsTrue (Style.IsStyleEmpty (new Style ()), "new");
-               }
-#endif
        }
 }