2009-10-13 Marek Habersack <mhabersack@novell.com>
authorMarek Habersack <grendel@twistedcode.net>
Mon, 12 Oct 2009 22:51:21 +0000 (22:51 -0000)
committerMarek Habersack <grendel@twistedcode.net>
Mon, 12 Oct 2009 22:51:21 +0000 (22:51 -0000)
* WebColorConverter.cs: don't use a dictionary to check for valid
color names, use Color.IsKnownColor instead. Update for bug
#546173 fix.
Added more CSS2 color name mappings.

svn path=/trunk/mcs/; revision=143990

mcs/class/System.Web/System.Web.UI.WebControls/ChangeLog
mcs/class/System.Web/System.Web.UI.WebControls/WebColorConverter.cs
mcs/class/System.Web/Test/System.Web.UI.WebControls/ChangeLog
mcs/class/System.Web/Test/System.Web.UI.WebControls/WebColorConverterTest.cs

index ec4a343fc7c7c33a9cd10ef85be4d78339c9ab7f..aec4abdd0fd6e4d093d76abcdce766941a626754 100644 (file)
@@ -1,3 +1,10 @@
+2009-10-13  Marek Habersack  <mhabersack@novell.com>
+
+       * WebColorConverter.cs: don't use a dictionary to check for valid
+       color names, use Color.IsKnownColor instead. Update for bug
+       #546173 fix.
+       Added more CSS2 color name mappings.
+
 2009-10-12  Marek Habersack  <mhabersack@novell.com>
 
        * WebColorConverter.cs: when converting from color name, check if
index d33cd326df01262f71f2f2e4c8f302d317d1a5c4..bdcca7b7936c35020205c971ba440ee146fa37b8 100644 (file)
@@ -41,24 +41,6 @@ namespace System.Web.UI.WebControls {
        [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        public class WebColorConverter : ColorConverter
        {
-#if NET_2_0
-               static Dictionary <string, bool> knownColorNames;
-#else
-               static Hashtable knownColorNames;
-#endif
-
-               static WebColorConverter ()
-               {
-#if NET_2_0
-                       knownColorNames = new Dictionary <string, bool> (StringComparer.Ordinal);
-#else
-                       knownColorNames = new Hashtable ();
-#endif
-
-                       foreach (string name in Enum.GetNames (typeof (KnownColor)))
-                               knownColorNames.Add (name, true);
-               }
-               
                // Converts from string to Color
                public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) 
                {
@@ -121,17 +103,48 @@ namespace System.Web.UI.WebControls {
 
                                                // Bug #546173
                                                switch (s.ToLower (CultureInfo.InvariantCulture)) {
+                                                       case "background":
+                                                               s = "Desktop";
+                                                               break;
+
+                                                       case "buttonface":
+                                                       case "threedface":
+                                                               s = "Control";
+                                                               break;
+
+                                                       case "buttonhighlight":
+                                                       case "threedlightshadow":
+                                                               s = "ControlLightLight";
+                                                               break;
+
+                                                       case "buttonshadow":
+                                                               s = "ControlDark";
+                                                               break;
+
+                                                       case "buttontext":
+                                                               s = "ControlText";
+                                                               break;
+                                                               
                                                        case "captiontext":
                                                                s = "ActiveCaptionText";
                                                                break;
 
-                                                       case "background":
-                                                               s = "Desktop";
+                                                       case "infobackground":
+                                                               s = "Info";
+                                                               break;
+
+                                                       case "threeddarkshadow":
+                                                               s = "ControlDarkDark";
+                                                               break;
+
+                                                       case "threedhighlight":
+                                                               s = "ControlLight";
                                                                break;
+                                                               
                                                }
                                                
                                                c = Color.FromName(s);
-                                               if (knownColorNames.ContainsKey (c.Name) || (c.A != 0) || (c.R != 0) || (c.G != 0) || (c.B != 0)) 
+                                               if (c.IsKnownColor || (c.A != 0) || (c.R != 0) || (c.G != 0) || (c.B != 0)) 
                                                {
                                                        return c;
                                                }
index 2d31993a2994b42093df5101638756568aa48e8a..d9e771a876bc273a69601d6fa2f49d69130f29d7 100644 (file)
@@ -1,3 +1,7 @@
+2009-10-13  Marek Habersack  <mhabersack@novell.com>
+
+       * WebColorConverterTest.cs: added test for bug #546173
+
 2009-03-17  Marek Habersack  <mhabersack@novell.com>
 
        * UnitTest.cs: added a test for equality with Unit.Empty
index 71a02a0ea907e71e46f931fb711a480d8c3779a3..57f75852b96f9c17f6f8a1bd3a71dbb6741c2727 100644 (file)
@@ -175,5 +175,127 @@ namespace MonoTests.System.Web.UI.WebControls
                        Assert.AreEqual(Color.FromArgb(255, 254, 254, 254), conv.ConvertFrom(null, null, "garbage"), "M7");
                }
 
+               [Test (Description="Bug #546173")]
+               public void NamedColorsCSS2 ()
+               {
+                       WebColorConverter wcc = new WebColorConverter ();
+                       Color c;
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ActiveBorder");
+                       Assert.IsTrue (c.IsKnownColor, "#A1");
+                       Assert.AreEqual ("ActiveBorder", c.Name, "#A1-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ActiveCaption");
+                       Assert.IsTrue (c.IsKnownColor, "#A2");
+                       Assert.AreEqual ("ActiveCaption", c.Name, "#A2-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "AppWorkspace");
+                       Assert.IsTrue (c.IsKnownColor, "#A3");
+                       Assert.AreEqual ("AppWorkspace", c.Name, "#A3-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "Background");
+                       Assert.IsTrue (c.IsKnownColor, "#A4");
+                       Assert.AreEqual ("Desktop", c.Name, "#A1-4");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ButtonFace");
+                       Assert.IsTrue (c.IsKnownColor, "#A5");
+                       Assert.AreEqual ("Control", c.Name, "#A5-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ButtonHighlight");
+                       Assert.IsTrue (c.IsKnownColor, "#A6");
+                       Assert.AreEqual ("ControlLightLight", c.Name, "#A6-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ButtonShadow");
+                       Assert.IsTrue (c.IsKnownColor, "#A7");
+                       Assert.AreEqual ("ControlDark", c.Name, "#A7-1");
+                       
+                       c = (Color)wcc.ConvertFrom (null, null, "ButtonText");
+                       Assert.IsTrue (c.IsKnownColor, "#A8");
+                       Assert.AreEqual ("ControlText", c.Name, "#A8-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "CaptionText");
+                       Assert.IsTrue (c.IsKnownColor, "#A9");
+                       Assert.AreEqual ("ActiveCaptionText", c.Name, "#A9-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "GrayText");
+                       Assert.IsTrue (c.IsKnownColor, "#A10");
+                       Assert.AreEqual ("GrayText", c.Name, "#A10-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "Highlight");
+                       Assert.IsTrue (c.IsKnownColor, "#A11");
+                       Assert.AreEqual ("Highlight", c.Name, "#A11-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "HighlightText");
+                       Assert.IsTrue (c.IsKnownColor, "#A12");
+                       Assert.AreEqual ("HighlightText", c.Name, "#A12-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "InactiveBorder");
+                       Assert.IsTrue (c.IsKnownColor, "#A13");
+                       Assert.AreEqual ("InactiveBorder", c.Name, "#A13-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "InactiveCaption");
+                       Assert.IsTrue (c.IsKnownColor, "#A14");
+                       Assert.AreEqual ("InactiveCaption", c.Name, "#A14-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "InactiveCaptionText");
+                       Assert.IsTrue (c.IsKnownColor, "#A15");
+                       Assert.AreEqual ("InactiveCaptionText", c.Name, "#A15-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "InfoBackground");
+                       Assert.IsTrue (c.IsKnownColor, "#A16");
+                       Assert.AreEqual ("Info", c.Name, "#A16-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "InfoText");
+                       Assert.IsTrue (c.IsKnownColor, "#A17");
+                       Assert.AreEqual ("InfoText", c.Name, "#A17-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "Menu");
+                       Assert.IsTrue (c.IsKnownColor, "#A18");
+                       Assert.AreEqual ("Menu", c.Name, "#A18-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "MenuText");
+                       Assert.IsTrue (c.IsKnownColor, "#A19");
+                       Assert.AreEqual ("MenuText", c.Name, "#A19-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ScrollBar");
+                       Assert.IsTrue (c.IsKnownColor, "#A20");
+                       Assert.AreEqual ("ScrollBar", c.Name, "#A20-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ThreeDDarkShadow");
+                       Assert.IsTrue (c.IsKnownColor, "#A21");
+                       Assert.AreEqual ("ControlDarkDark", c.Name, "#A21-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ThreeDFace");
+                       Assert.IsTrue (c.IsKnownColor, "#A22");
+                       Assert.AreEqual ("Control", c.Name, "#A22-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ThreeDHighlight");
+                       Assert.IsTrue (c.IsKnownColor, "#A23");
+                       Assert.AreEqual ("ControlLight", c.Name, "#A23-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "ThreeDLightShadow");
+                       Assert.IsTrue (c.IsKnownColor, "#A24");
+                       Assert.AreEqual ("ControlLightLight", c.Name, "#A24-1");
+
+                       Exception ex = null;
+                       try {
+                               c = (Color)wcc.ConvertFrom (null, null, "ThreeDShadow");
+                       } catch (Exception e) {
+                               ex = e;
+                       }                       
+                       Assert.IsNotNull (ex, "#A25");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "Window");
+                       Assert.IsTrue (c.IsKnownColor, "#A26");
+                       Assert.AreEqual ("Window", c.Name, "#A26-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "WindowFrame");
+                       Assert.IsTrue (c.IsKnownColor, "#A27");
+                       Assert.AreEqual ("WindowFrame", c.Name, "#A27-1");
+
+                       c = (Color)wcc.ConvertFrom (null, null, "WindowText");
+                       Assert.IsTrue (c.IsKnownColor, "#A28");
+                       Assert.AreEqual ("WindowText", c.Name, "#A28-1");
+               }
        }
 }