2 // ColorConverter class testing unit
4 // Copyright (C) 2006 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.
27 using System.Collections;
28 using System.ComponentModel;
29 using System.ComponentModel.Design.Serialization;
31 using System.Globalization;
32 using System.Security.Permissions;
34 using NUnit.Framework;
36 namespace MonoTests.System.Drawing {
39 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
40 public class ColorConverterFixture
44 ColorConverter colconv;
46 String colStrInvariant;
50 public void SetUp () {
51 col = Color.FromArgb (10, 20, 30);
52 colStr = string.Format ("10{0} 20{0} 30", CultureInfo.CurrentCulture.TextInfo.ListSeparator);
53 colStrInvariant = string.Format ("10{0} 20{0} 30", CultureInfo.InvariantCulture.TextInfo.ListSeparator);
55 colnamed = Color.ForestGreen;
56 colnamedStr = "ForestGreen";
58 colconv = (ColorConverter) TypeDescriptor.GetConverter (col);
63 [NUnit.Framework.Category ("NotWorking")]
65 public void CanConvertFrom () {
66 Assert.IsTrue (colconv.CanConvertFrom (typeof (String)), "CCF#1");
67 Assert.IsTrue (colconv.CanConvertFrom (null, typeof (String)), "CCF#1a");
68 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#2");
69 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#3");
70 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Point)), "CCF#4");
71 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
72 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Color)), "CCF#6");
73 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
74 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Object)), "CCF#8");
75 Assert.IsFalse ( colconv.CanConvertFrom (null, typeof (int)), "CCF#9");
76 Assert.IsTrue (colconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#10");
80 public void CanConvertTo () {
81 Assert.IsTrue (colconv.CanConvertTo (typeof (String)), "CCT#1");
82 Assert.IsTrue (colconv.CanConvertTo (null, typeof (String)), "CCT#1a");
83 Assert.IsFalse (colconv.CanConvertTo (null, typeof (Rectangle)), "CCT#2");
84 Assert.IsFalse (colconv.CanConvertTo (null, typeof (RectangleF)), "CCT#3");
85 Assert.IsFalse (colconv.CanConvertTo (null, typeof (Point)), "CCT#4");
86 Assert.IsFalse (colconv.CanConvertTo (null, typeof (PointF)), "CCT#5");
87 Assert.IsFalse (colconv.CanConvertTo (null, typeof (Color)), "CCT#6");
88 Assert.IsFalse (colconv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
89 Assert.IsFalse (colconv.CanConvertTo (null, typeof (Object)), "CCT#8");
90 Assert.IsFalse (colconv.CanConvertTo (null, typeof (int)), "CCT#9");
91 Assert.IsTrue (colconv.CanConvertTo (typeof (InstanceDescriptor)), "CCT#10");
96 [NUnit.Framework.Category ("NotWorking")]
98 public void ConvertFrom ()
100 Color color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
102 Assert.AreEqual (35, color.A, "CF1#1");
103 Assert.AreEqual (25, color.R, "CF1#2");
104 Assert.AreEqual (10, color.G, "CF1#3");
105 Assert.AreEqual (68, color.B, "CF1#4");
107 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
109 Assert.AreEqual (0, color.A, "CF2#1");
110 Assert.AreEqual (25, color.R, "CF2#2");
111 Assert.AreEqual (10, color.G, "CF2#3");
112 Assert.AreEqual (68, color.B, "CF2#4");
114 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
116 Assert.AreEqual (255, color.A, "CF3#1");
117 Assert.AreEqual (25, color.R, "CF3#2");
118 Assert.AreEqual (10, color.G, "CF3#3");
119 Assert.AreEqual (68, color.B, "CF3#4");
121 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
123 Assert.AreEqual (255, color.A, "CF4#1");
124 Assert.AreEqual (25, color.R, "CF4#2");
125 Assert.AreEqual (10, color.G, "CF4#3");
126 Assert.AreEqual (68, color.B, "CF4#4");
128 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
130 Assert.AreEqual (0, color.A, "CF5#1");
131 Assert.AreEqual (1, color.R, "CF5#2");
132 Assert.AreEqual (178, color.G, "CF5#3");
133 Assert.AreEqual (7, color.B, "CF5#4");
135 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
137 Assert.AreEqual (0, color.A, "CF6#1");
138 Assert.AreEqual (0, color.R, "CF6#2");
139 Assert.AreEqual (0, color.G, "CF6#3");
140 Assert.AreEqual (10, color.B, "CF6#4");
142 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
144 Assert.AreEqual (0, color.A, "CF7#1");
145 Assert.AreEqual (0, color.R, "CF7#2");
146 Assert.AreEqual (0, color.G, "CF7#3");
147 Assert.AreEqual (0, color.B, "CF7#4");
150 Assert.AreEqual (col, (Color) colconv.ConvertFrom (null,
151 CultureInfo.InvariantCulture, colStrInvariant), "CF#1");
152 Assert.AreEqual (colnamed, (Color) colconv.ConvertFrom (null,
153 CultureInfo.InvariantCulture, colnamedStr), "CF#2");
155 Assert.AreEqual (Color.Empty, colconv.ConvertFrom (string.Empty), "CF#3");
156 Assert.AreEqual (Color.Empty, colconv.ConvertFrom (" "), "CF#4");
157 Assert.AreEqual (Color.Red, colconv.ConvertFrom ("Red"), "CF#5");
158 Assert.AreEqual (Color.Red, colconv.ConvertFrom (" Red "), "CF#6");
162 [NUnit.Framework.Category ("NotWorking")]
163 public void ConvertFrom_NotWorking ()
165 Color color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
167 Assert.AreEqual (0, color.A, "CF8#1");
168 Assert.AreEqual (0, color.R, "CF8#2");
169 Assert.AreEqual (1, color.G, "CF8#3");
170 Assert.AreEqual (35, color.B, "CF8#4");
172 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
174 Assert.AreEqual (0, color.A, "CF9#1");
175 Assert.AreEqual (0, color.R, "CF9#2");
176 Assert.AreEqual (1, color.G, "CF9#3");
177 Assert.AreEqual (35, color.B, "CF9#4");
181 [ExpectedException (typeof (ArgumentException))]
182 public void ConvertFrom_x1 ()
184 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10, 20");
188 [ExpectedException (typeof (ArgumentException))]
189 public void ConvertFrom_x2 ()
191 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "-10, 20, 30");
195 [ExpectedException (typeof (ArgumentException))]
196 public void ConvertFrom_x3 ()
198 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
203 [ExpectedException (typeof (Exception))]
205 [NUnit.Framework.Category ("NotWorking")]
207 public void ConvertFrom_x4 ()
209 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
214 [ExpectedException (typeof (NotSupportedException))]
215 public void ConvertFrom_x5 ()
217 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
222 [ExpectedException (typeof (NotSupportedException))]
223 public void ConvertFrom_x6 ()
225 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
226 new PointF (10, 10));
230 [ExpectedException (typeof (NotSupportedException))]
231 public void ConvertFrom_x7 ()
233 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
238 [ExpectedException (typeof (NotSupportedException))]
239 public void ConvertFrom_x8 ()
241 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
246 [ExpectedException (typeof (NotSupportedException))]
247 public void ConvertFrom_x9 ()
249 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
253 public void ConvertTo ()
255 Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
256 Color.FromArgb (10, 20, 30), typeof (String)), "CT#1");
257 Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
258 Color.FromArgb (255, 10, 20, 30), typeof (String)), "CT#2");
259 Assert.AreEqual ("10, 20, 30, 40", colconv.ConvertTo (null, CultureInfo.InvariantCulture,
260 Color.FromArgb (10, 20, 30, 40), typeof (String)), "CT#3");
261 Assert.AreEqual (colnamedStr, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
262 colnamed, typeof (String)), "CT#4");
264 Assert.AreEqual (string.Empty, colconv.ConvertTo (Color.Empty, typeof (string)), "CT#5");
265 Assert.AreEqual ("Red", colconv.ConvertTo (Color.Red, typeof (string)), "CT#6");
266 Assert.AreEqual (string.Empty, colconv.ConvertTo (null, typeof (string)), "CT#7");
267 Assert.AreEqual ("test", colconv.ConvertTo ("test", typeof (string)), "CT#8");
271 [ExpectedException (typeof (NotSupportedException))]
272 public void ConvertTo_x1 ()
274 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
279 [ExpectedException (typeof (NotSupportedException))]
280 public void ConvertTo_x2 ()
282 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
287 [ExpectedException (typeof (NotSupportedException))]
288 public void ConvertTo_x3 ()
290 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
295 [ExpectedException (typeof (NotSupportedException))]
296 public void ConvertTo_x4 ()
298 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
303 [ExpectedException (typeof (NotSupportedException))]
304 public void ConvertTo_x5 ()
306 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
311 public void GetCreateInstanceSupported ()
313 Assert.IsTrue (!colconv.GetCreateInstanceSupported (), "GCIS#1");
314 Assert.IsTrue (!colconv.GetCreateInstanceSupported (null), "GCIS#2");
318 public void CreateInstance ()
320 Hashtable ht = new Hashtable ();
321 ht.Add ("R", 10); ht.Add ("G", 20); ht.Add ("B", 30);
323 Assert.AreEqual (null, colconv.CreateInstance (ht), "CI#1");
325 ht.Add ("Name", "ForestGreen");
327 Assert.AreEqual (null, colconv.CreateInstance (null, ht), "CI#2");
331 public void GetPropertiesSupported ()
333 Assert.IsTrue (!colconv.GetPropertiesSupported (), "GPS#1");
334 Assert.IsTrue (!colconv.GetPropertiesSupported (null), "GPS#2");
338 public void GetProperties ()
342 Assert.AreEqual (null, colconv.GetProperties (col), "GP1#1");
344 Assert.AreEqual (null, colconv.GetProperties (null, col, null), "GP2#1");
346 attrs = Attribute.GetCustomAttributes (typeof (Color), true);
347 Assert.AreEqual (null, colconv.GetProperties (null, col, attrs), "GP3#5");
351 public void ConvertFromInvariantString_string ()
353 Assert.AreEqual (col, colconv.ConvertFromInvariantString (colStrInvariant), "CFISS#1");
354 Assert.AreEqual (colnamed, colconv.ConvertFromInvariantString (colnamedStr), "CFISS#2");
358 [ExpectedException (typeof (ArgumentException))]
359 public void ConvertFromInvariantString_InvalidComponentCount ()
361 colconv.ConvertFromInvariantString ("1, 2, 3, 4, 5");
366 [NUnit.Framework.Category ("NotWorking")]
368 public void ConvertFromInvariantString_InvalidNumber ()
371 colconv.ConvertFromInvariantString ("hello");
373 } catch (Exception ex) {
374 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
375 Assert.IsNotNull (ex.InnerException, "#3");
376 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
381 public void ConvertFromString_string ()
383 Assert.AreEqual (col, colconv.ConvertFromString (colStr), "CFSS#1");
384 Assert.AreEqual (colnamed, colconv.ConvertFromString (colnamedStr), "CFSS#2");
388 [ExpectedException (typeof (ArgumentException))]
389 public void ConvertFromString_InvalidComponentCount ()
391 CultureInfo culture = CultureInfo.CurrentCulture;
392 colconv.ConvertFromString (string.Format (culture,
393 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator[0]));
398 [NUnit.Framework.Category ("NotWorking")]
400 public void ConvertFromString_InvalidNumber ()
403 colconv.ConvertFromString ("hello");
405 } catch (Exception ex) {
406 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
407 Assert.IsNotNull (ex.InnerException, "#3");
408 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
413 public void ConvertToInvariantString_string () {
414 Assert.AreEqual (colStrInvariant, colconv.ConvertToInvariantString (col), "CFISS#1");
415 Assert.AreEqual (colnamedStr, colconv.ConvertToInvariantString (colnamed), "CFISS#2");
419 public void ConvertToString_string () {
420 Assert.AreEqual (colStr, colconv.ConvertToString (col), "CFISS#1");
421 Assert.AreEqual (colnamedStr, colconv.ConvertToString (colnamed), "CFISS#3");
425 public void GetStandardValuesSupported () {
426 Assert.IsTrue (colconv.GetStandardValuesSupported ());
430 public void GetStandardValues () {
432 Assert.AreEqual ((int)KnownColor.MenuHighlight, colconv.GetStandardValues ().Count);
433 Assert.AreEqual ((int)KnownColor.MenuHighlight, colconv.GetStandardValues (null).Count);
435 Assert.AreEqual ((int)KnownColor.YellowGreen, colconv.GetStandardValues ().Count);
436 Assert.AreEqual ((int)KnownColor.YellowGreen, colconv.GetStandardValues (null).Count);
441 public void GetStandardValuesExclusive () {
442 Assert.AreEqual (false, colconv.GetStandardValuesExclusive ());
446 [ExpectedException (typeof (Exception))]
447 public void ConvertFromString_FromHtml_PoundTooLarge ()
449 colconv.ConvertFromString ("#100000000");