2 // ColorTranslator class testing unit
4 // Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Security.Permissions;
29 using NUnit.Framework;
31 namespace MonoTests.System.Drawing {
34 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
35 public class ColorTranslatorTest {
38 public void FromHtml_Null ()
40 Assert.AreEqual (0, ColorTranslator.FromHtml (null).ToArgb ());
44 public void FromHtml_Empty ()
46 Assert.AreEqual (0, ColorTranslator.FromHtml (String.Empty).ToArgb ());
50 public void FromHtml_Int ()
52 Assert.AreEqual (-1, ColorTranslator.FromHtml ("-1").ToArgb (), "-1");
53 Assert.AreEqual (0, ColorTranslator.FromHtml ("0").ToArgb (), "0");
54 Assert.AreEqual (1, ColorTranslator.FromHtml ("1").ToArgb (), "1");
58 public void FromHtml_PoundInt ()
60 Assert.AreEqual (0, ColorTranslator.FromHtml ("#0").ToArgb (), "#0");
61 Assert.AreEqual (1, ColorTranslator.FromHtml ("#1").ToArgb (), "#1");
62 Assert.AreEqual (255, ColorTranslator.FromHtml ("#FF").ToArgb (), "#FF");
63 Assert.AreEqual (65535, ColorTranslator.FromHtml ("#FFFF").ToArgb (), "#FFFF");
64 Assert.AreEqual (-15584170, ColorTranslator.FromHtml ("#123456").ToArgb (), "#123456");
65 Assert.AreEqual (-1, ColorTranslator.FromHtml ("#FFFFFF").ToArgb (), "#FFFFFF");
66 Assert.AreEqual (305419896, ColorTranslator.FromHtml ("#12345678").ToArgb (), "#12345678");
67 Assert.AreEqual (-1, ColorTranslator.FromHtml ("#FFFFFFFF").ToArgb (), "#FFFFFFFF");
71 [ExpectedException (typeof (Exception))]
73 [Category ("NotWorking")]
75 public void FromHtml_PoundNegative ()
77 ColorTranslator.FromHtml ("#-1");
81 [ExpectedException (typeof (Exception))]
82 public void FromHtml_PoundTooLarge ()
84 ColorTranslator.FromHtml ("#100000000");
88 [ExpectedException (typeof (Exception))]
90 [Category ("NotWorking")]
92 public void FromHtml_Unknown ()
94 ColorTranslator.FromHtml ("unknown-color-test");
98 public void FromHtml ()
100 Color [] colors = new Color [] {
101 Color.Aqua, Color.Black, Color.Blue, Color.Fuchsia, Color.Gray,
102 Color.Green, Color.Lime, Color.Maroon, Color.Navy, Color.Olive,
103 Color.Purple, Color.Red, Color.Silver, Color.Teal, Color.White,
106 SystemColors.ActiveBorder, SystemColors.ActiveCaption,
107 SystemColors.Control,
108 //SystemColors.ControlLightLight,
109 SystemColors.ActiveCaptionText, SystemColors.GrayText,
110 //SystemColors.InactiveBorder, SystemColors.InactiveCaption,
111 SystemColors.InfoText, SystemColors.Menu,
112 SystemColors.ControlDarkDark,
113 //SystemColors.ControlText, SystemColors.ControlDark,
115 SystemColors.AppWorkspace, SystemColors.Desktop,
116 //SystemColors.ControlDark,
117 SystemColors.ControlText,
118 SystemColors.Highlight, SystemColors.HighlightText,
119 //SystemColors.InactiveCaptionText,
121 SystemColors.MenuText, SystemColors.ScrollBar,
122 //SystemColors.ControlLight, SystemColors.ControlLightLight
124 string [] htmlColors = new string [] {
125 "Aqua", "Black", "Blue", "Fuchsia", "Gray", "Green",
126 "Lime", "Maroon", "Navy", "Olive", "Purple", "Red",
127 "Silver", "Teal", "White", "Yellow",
129 "activeborder", "activecaption", "buttonface",
131 "captiontext", "graytext",
132 //"iborder", "Icaption",
133 "infotext", "menu", "threeddarkshadow",
134 //"thrface", "Threedshadow",
135 "window", "appworkspace",
138 "buttontext", "highlight",
142 "menutext", "scrollbar",
143 //"thhighlight", "thlightshadow"
146 for (int i=0; i<colors.Length; i++)
147 Assert.AreEqual (colors[i], ColorTranslator.FromHtml (htmlColors [i]));
151 public void FromOle ()
153 Assert.AreEqual (Color.FromArgb (0x10, 0x20, 0x30), ColorTranslator.FromOle (0x302010));
154 Assert.AreEqual (Color.FromArgb (0xbb, 0x20, 0x30), ColorTranslator.FromOle (unchecked ((int)0xee3020bb)));
158 public void FromWin32 ()
160 Assert.AreEqual (Color.FromArgb (0x10, 0x20, 0x30), ColorTranslator.FromWin32 (0x302010));
161 Assert.AreEqual (Color.FromArgb (0xbb, 0x20, 0x30), ColorTranslator.FromWin32 (unchecked ((int)0xee3020bb)));
165 public void ToHtml ()
167 string [] htmlColors = new string [] {
168 "activeborder", "activecaption", "captiontext", "appworkspace", "buttonface",
169 "buttonshadow", "threeddarkshadow", "buttonface", "buttonhighlight", "buttontext",
170 "background", "graytext", "highlight", "highlighttext", "highlight", "inactiveborder",
171 "inactivecaption", "inactivecaptiontext", "infobackground", "infotext", "menu",
172 "menutext", "scrollbar", "window", "windowframe", "windowtext",
174 "Transparent", "AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige",
175 "Bisque", "Black", "BlanchedAlmond", "Blue", "BlueViolet", "Brown", "BurlyWood",
176 "CadetBlue", "Chartreuse", "Chocolate", "Coral", "CornflowerBlue", "Cornsilk",
177 "Crimson", "Cyan", "DarkBlue", "DarkCyan", "DarkGoldenrod", "DarkGray", "DarkGreen",
178 "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "DarkOrange", "DarkOrchid", "DarkRed",
179 "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkTurquoise", "DarkViolet",
180 "DeepPink", "DeepSkyBlue", "DimGray", "DodgerBlue", "Firebrick", "FloralWhite", "ForestGreen",
181 "Fuchsia", "Gainsboro", "GhostWhite", "Gold", "Goldenrod", "Gray", "Green", "GreenYellow",
182 "Honeydew", "HotPink", "IndianRed", "Indigo", "Ivory", "Khaki", "Lavender", "LavenderBlush",
183 "LawnGreen", "LemonChiffon", "LightBlue", "LightCoral", "LightCyan", "LightGoldenrodYellow",
184 "LightGrey", "LightGreen", "LightPink", "LightSalmon", "LightSeaGreen", "LightSkyBlue",
185 "LightSlateGray", "LightSteelBlue", "LightYellow", "Lime", "LimeGreen", "Linen", "Magenta",
186 "Maroon", "MediumAquamarine", "MediumBlue", "MediumOrchid", "MediumPurple", "MediumSeaGreen",
187 "MediumSlateBlue", "MediumSpringGreen", "MediumTurquoise", "MediumVioletRed", "MidnightBlue",
188 "MintCream", "MistyRose", "Moccasin", "NavajoWhite", "Navy", "OldLace", "Olive", "OliveDrab",
189 "Orange", "OrangeRed", "Orchid", "PaleGoldenrod", "PaleGreen", "PaleTurquoise", "PaleVioletRed",
190 "PapayaWhip", "PeachPuff", "Peru", "Pink", "Plum", "PowderBlue", "Purple", "Red", "RosyBrown",
191 "RoyalBlue", "SaddleBrown", "Salmon", "SandyBrown", "SeaGreen", "SeaShell", "Sienna", "Silver",
192 "SkyBlue", "SlateBlue", "SlateGray", "Snow", "SpringGreen", "SteelBlue", "Tan", "Teal",
193 "Thistle", "Tomato", "Turquoise", "Violet", "Wheat", "White", "WhiteSmoke", "Yellow", "YellowGreen",
196 for (KnownColor i=KnownColor.ActiveBorder; i<=KnownColor.YellowGreen; i++)
197 Assert.AreEqual (htmlColors[(int)i-1], ColorTranslator.ToHtml (Color.FromKnownColor (i)));
201 public void ToOle () {
202 Assert.AreEqual (0x302010, ColorTranslator.ToOle (Color.FromArgb (0x10, 0x20, 0x30)));
203 Assert.AreEqual (unchecked ((int)0x3020bb), ColorTranslator.ToOle (Color.FromArgb (0xee, 0xbb, 0x20, 0x30)));
207 public void ToWin32 () {
208 Assert.AreEqual (0x302010, ColorTranslator.ToWin32 (Color.FromArgb (0x10, 0x20, 0x30)));
209 Assert.AreEqual (unchecked ((int)0x3020bb), ColorTranslator.ToWin32 (Color.FromArgb (0xee, 0xbb, 0x20, 0x30)));