2007-03-15 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / ColorTranslator.cs
1 //
2 // ColorTranslator class testing unit
3 //
4 // Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
5 //
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:
13 // 
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 // 
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.
24 //
25
26 using System;
27 using System.Drawing;
28 using System.Security.Permissions;
29 using NUnit.Framework;
30
31 namespace MonoTests.System.Drawing {
32
33         [TestFixture]
34         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
35         public class ColorTranslatorTest {
36
37                 [Test]
38                 public void FromHtml_Null ()
39                 {
40                         Assert.AreEqual (0, ColorTranslator.FromHtml (null).ToArgb ());
41                 }
42
43                 [Test]
44                 public void FromHtml_Empty ()
45                 {
46                         Assert.AreEqual (0, ColorTranslator.FromHtml (String.Empty).ToArgb ());
47                 }
48
49                 [Test]
50                 public void FromHtml_Int ()
51                 {
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");
55                 }
56
57                 [Test]
58                 public void FromHtml_PoundInt ()
59                 {
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");
68                 }
69
70                 [Test]
71                 [ExpectedException (typeof (Exception))]
72 #if TARGET_JVM
73                 [Category ("NotWorking")]
74 #endif
75                 public void FromHtml_PoundNegative ()
76                 {
77                         ColorTranslator.FromHtml ("#-1");
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (Exception))]
82                 public void FromHtml_PoundTooLarge ()
83                 {
84                         ColorTranslator.FromHtml ("#100000000");
85                 }
86
87                 [Test]
88                 [ExpectedException (typeof (Exception))]
89 #if TARGET_JVM
90                 [Category ("NotWorking")]
91 #endif
92                 public void FromHtml_Unknown ()
93                 {
94                         ColorTranslator.FromHtml ("unknown-color-test");
95                 }
96
97                 [Test]
98                 public void FromHtml ()
99                 {
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,
104 Color.Yellow,
105
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,
114 SystemColors.Window,
115 SystemColors.AppWorkspace, SystemColors.Desktop,
116 //SystemColors.ControlDark,
117 SystemColors.ControlText,
118 SystemColors.Highlight, SystemColors.HighlightText,
119 //SystemColors.InactiveCaptionText,
120 SystemColors.Info,
121 SystemColors.MenuText, SystemColors.ScrollBar,
122 //SystemColors.ControlLight, SystemColors.ControlLightLight
123                         };
124                         string [] htmlColors = new string [] {
125 "Aqua", "Black", "Blue", "Fuchsia", "Gray", "Green",
126 "Lime", "Maroon", "Navy", "Olive", "Purple", "Red",
127 "Silver", "Teal", "White", "Yellow",
128
129 "activeborder", "activecaption", "buttonface",
130 //"buhighlight",
131 "captiontext", "graytext",
132 //"iborder", "Icaption", 
133 "infotext", "menu", "threeddarkshadow",
134 //"thrface", "Threedshadow",
135 "window", "appworkspace",
136 "background", 
137 //"bshadow",
138 "buttontext", "highlight",
139 "highlighttext",
140 //"icaptiontext",
141 "infobackground",
142 "menutext", "scrollbar", 
143 //"thhighlight", "thlightshadow"
144                         };
145                 
146                         for (int i=0; i<colors.Length; i++)
147                                 Assert.AreEqual (colors[i], ColorTranslator.FromHtml (htmlColors [i]));
148                 }
149
150                 [Test]
151                 public void FromOle ()
152                 {
153                         Assert.AreEqual (Color.FromArgb (0x10, 0x20, 0x30), ColorTranslator.FromOle (0x302010));
154                         Assert.AreEqual (Color.FromArgb (0xbb, 0x20, 0x30), ColorTranslator.FromOle (unchecked ((int)0xee3020bb)));
155                 }
156
157                 [Test]
158                 public void FromWin32 ()
159                 {
160                         Assert.AreEqual (Color.FromArgb (0x10, 0x20, 0x30), ColorTranslator.FromWin32 (0x302010));
161                         Assert.AreEqual (Color.FromArgb (0xbb, 0x20, 0x30), ColorTranslator.FromWin32 (unchecked ((int)0xee3020bb)));
162                 }
163
164                 [Test]
165                 public void ToHtml ()
166                 {
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", 
173
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",
194                                                                                         };
195
196                         for (KnownColor i=KnownColor.ActiveBorder; i<=KnownColor.YellowGreen; i++)
197                                 Assert.AreEqual (htmlColors[(int)i-1], ColorTranslator.ToHtml (Color.FromKnownColor (i)));
198                 }
199
200                 [Test]
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)));
204                 }
205
206                 [Test]
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)));
210                 }
211
212         }
213 }
214