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_KnownValues ()
52 Assert.AreEqual (SystemColors.Control, ColorTranslator.FromHtml ("buttonface"), "buttonface");
53 Assert.AreEqual (SystemColors.ActiveCaptionText, ColorTranslator.FromHtml ("CAPTIONTEXT"), "captiontext");
54 Assert.AreEqual (SystemColors.ControlDarkDark, ColorTranslator.FromHtml ("threedDARKshadow"), "threeddarkshadow");
55 Assert.AreEqual (SystemColors.Desktop, ColorTranslator.FromHtml ("background"), "background");
56 Assert.AreEqual (SystemColors.ControlText, ColorTranslator.FromHtml ("ButtonText"), "buttontext");
57 Assert.AreEqual (SystemColors.Info, ColorTranslator.FromHtml ("infobackground"), "infobackground");
61 public void FromHtml_Int ()
63 Assert.AreEqual (-1, ColorTranslator.FromHtml ("-1").ToArgb (), "-1");
64 Assert.AreEqual (0, ColorTranslator.FromHtml ("0").ToArgb (), "0");
65 Assert.AreEqual (1, ColorTranslator.FromHtml ("1").ToArgb (), "1");
69 public void FromHtml_PoundInt ()
71 Assert.AreEqual (0, ColorTranslator.FromHtml ("#0").ToArgb (), "#0");
72 Assert.AreEqual (1, ColorTranslator.FromHtml ("#1").ToArgb (), "#1");
73 Assert.AreEqual (255, ColorTranslator.FromHtml ("#FF").ToArgb (), "#FF");
74 Assert.AreEqual (65535, ColorTranslator.FromHtml ("#FFFF").ToArgb (), "#FFFF");
75 Assert.AreEqual (-15584170, ColorTranslator.FromHtml ("#123456").ToArgb (), "#123456");
76 Assert.AreEqual (-1, ColorTranslator.FromHtml ("#FFFFFF").ToArgb (), "#FFFFFF");
77 Assert.AreEqual (305419896, ColorTranslator.FromHtml ("#12345678").ToArgb (), "#12345678");
78 Assert.AreEqual (-1, ColorTranslator.FromHtml ("#FFFFFFFF").ToArgb (), "#FFFFFFFF");
80 Assert.AreEqual (Color.White, ColorTranslator.FromHtml ("#FFFFFF"), "used to resolve to some KnownColor");
81 Assert.AreEqual (Color.White, ColorTranslator.FromHtml ("0xFFFFFF"), "used to resolve to some KnownColor");
85 [ExpectedException (typeof (Exception))]
86 public void FromHtml_PoundNegative ()
88 ColorTranslator.FromHtml ("#-1");
92 [ExpectedException (typeof (Exception))]
93 public void FromHtml_PoundTooLarge ()
95 ColorTranslator.FromHtml ("#100000000");
99 [ExpectedException (typeof (Exception))]
100 public void FromHtml_Unknown ()
102 ColorTranslator.FromHtml ("unknown-color-test");
106 public void FromHtml ()
108 Color [] colors = new Color [] {
109 Color.Aqua, Color.Black, Color.Blue, Color.Fuchsia, Color.Gray,
110 Color.Green, Color.Lime, Color.Maroon, Color.Navy, Color.Olive,
111 Color.Purple, Color.Red, Color.Silver, Color.Teal, Color.White,
114 SystemColors.ActiveBorder, SystemColors.ActiveCaption,
115 SystemColors.Control,
116 //SystemColors.ControlLightLight,
117 SystemColors.ActiveCaptionText, SystemColors.GrayText,
118 //SystemColors.InactiveBorder, SystemColors.InactiveCaption,
119 SystemColors.InfoText, SystemColors.Menu,
120 SystemColors.ControlDarkDark,
121 //SystemColors.ControlText, SystemColors.ControlDark,
123 SystemColors.AppWorkspace, SystemColors.Desktop,
124 //SystemColors.ControlDark,
125 SystemColors.ControlText,
126 SystemColors.Highlight, SystemColors.HighlightText,
127 //SystemColors.InactiveCaptionText,
129 SystemColors.MenuText, SystemColors.ScrollBar,
130 //SystemColors.ControlLight, SystemColors.ControlLightLight
132 string [] htmlColors = new string [] {
133 "Aqua", "Black", "Blue", "Fuchsia", "Gray", "Green",
134 "Lime", "Maroon", "Navy", "Olive", "Purple", "Red",
135 "Silver", "Teal", "White", "Yellow",
137 "activeborder", "activecaption", "buttonface",
139 "captiontext", "graytext",
140 //"iborder", "Icaption",
141 "infotext", "menu", "threeddarkshadow",
142 //"thrface", "Threedshadow",
143 "window", "appworkspace",
146 "buttontext", "highlight",
150 "menutext", "scrollbar",
151 //"thhighlight", "thlightshadow"
154 for (int i=0; i<colors.Length; i++)
155 Assert.AreEqual (colors[i], ColorTranslator.FromHtml (htmlColors [i]));
159 public void FromHtml_LightGrey ()
161 Assert.AreEqual (Color.LightGray, ColorTranslator.FromHtml(ColorTranslator.ToHtml(Color.LightGray)));
165 public void FromOle ()
167 Assert.AreEqual (Color.FromArgb (0x10, 0x20, 0x30), ColorTranslator.FromOle (0x302010));
168 Assert.AreEqual (Color.FromArgb (0xbb, 0x20, 0x30), ColorTranslator.FromOle (unchecked ((int)0xee3020bb)));
172 public void FromWin32 ()
174 Assert.AreEqual (Color.FromArgb (0x10, 0x20, 0x30), ColorTranslator.FromWin32 (0x302010));
175 Assert.AreEqual (Color.FromArgb (0xbb, 0x20, 0x30), ColorTranslator.FromWin32 (unchecked ((int)0xee3020bb)));
179 public void ToHtml ()
181 string [] htmlColors = new string [] {
182 "activeborder", "activecaption", "captiontext", "appworkspace", "buttonface",
183 "buttonshadow", "threeddarkshadow", "buttonface", "buttonhighlight", "buttontext",
184 "background", "graytext", "highlight", "highlighttext", "highlight", "inactiveborder",
185 "inactivecaption", "inactivecaptiontext", "infobackground", "infotext", "menu",
186 "menutext", "scrollbar", "window", "windowframe", "windowtext",
188 "Transparent", "AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige",
189 "Bisque", "Black", "BlanchedAlmond", "Blue", "BlueViolet", "Brown", "BurlyWood",
190 "CadetBlue", "Chartreuse", "Chocolate", "Coral", "CornflowerBlue", "Cornsilk",
191 "Crimson", "Cyan", "DarkBlue", "DarkCyan", "DarkGoldenrod", "DarkGray", "DarkGreen",
192 "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "DarkOrange", "DarkOrchid", "DarkRed",
193 "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkTurquoise", "DarkViolet",
194 "DeepPink", "DeepSkyBlue", "DimGray", "DodgerBlue", "Firebrick", "FloralWhite", "ForestGreen",
195 "Fuchsia", "Gainsboro", "GhostWhite", "Gold", "Goldenrod", "Gray", "Green", "GreenYellow",
196 "Honeydew", "HotPink", "IndianRed", "Indigo", "Ivory", "Khaki", "Lavender", "LavenderBlush",
197 "LawnGreen", "LemonChiffon", "LightBlue", "LightCoral", "LightCyan", "LightGoldenrodYellow",
198 "LightGrey", "LightGreen", "LightPink", "LightSalmon", "LightSeaGreen", "LightSkyBlue",
199 "LightSlateGray", "LightSteelBlue", "LightYellow", "Lime", "LimeGreen", "Linen", "Magenta",
200 "Maroon", "MediumAquamarine", "MediumBlue", "MediumOrchid", "MediumPurple", "MediumSeaGreen",
201 "MediumSlateBlue", "MediumSpringGreen", "MediumTurquoise", "MediumVioletRed", "MidnightBlue",
202 "MintCream", "MistyRose", "Moccasin", "NavajoWhite", "Navy", "OldLace", "Olive", "OliveDrab",
203 "Orange", "OrangeRed", "Orchid", "PaleGoldenrod", "PaleGreen", "PaleTurquoise", "PaleVioletRed",
204 "PapayaWhip", "PeachPuff", "Peru", "Pink", "Plum", "PowderBlue", "Purple", "Red", "RosyBrown",
205 "RoyalBlue", "SaddleBrown", "Salmon", "SandyBrown", "SeaGreen", "SeaShell", "Sienna", "Silver",
206 "SkyBlue", "SlateBlue", "SlateGray", "Snow", "SpringGreen", "SteelBlue", "Tan", "Teal",
207 "Thistle", "Tomato", "Turquoise", "Violet", "Wheat", "White", "WhiteSmoke", "Yellow", "YellowGreen",
210 for (KnownColor i=KnownColor.ActiveBorder; i<=KnownColor.YellowGreen; i++)
211 Assert.AreEqual (htmlColors[(int)i-1], ColorTranslator.ToHtml (Color.FromKnownColor (i)));
215 public void ToOle () {
216 Assert.AreEqual (0x302010, ColorTranslator.ToOle (Color.FromArgb (0x10, 0x20, 0x30)));
217 Assert.AreEqual (unchecked ((int)0x3020bb), ColorTranslator.ToOle (Color.FromArgb (0xee, 0xbb, 0x20, 0x30)));
221 public void ToWin32 () {
222 Assert.AreEqual (0x302010, ColorTranslator.ToWin32 (Color.FromArgb (0x10, 0x20, 0x30)));
223 Assert.AreEqual (unchecked ((int)0x3020bb), ColorTranslator.ToWin32 (Color.FromArgb (0xee, 0xbb, 0x20, 0x30)));