2 using System.Collections;
3 using System.ComponentModel;
4 using System.ComponentModel.Design.Serialization;
6 using System.Globalization;
7 using System.Security.Permissions;
11 namespace MonoTests.System.Drawing {
14 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
15 public class ColorConverterFixture
19 ColorConverter colconv;
21 String colStrInvariant;
25 public void SetUp () {
26 col = Color.FromArgb (10, 20, 30);
27 colStr = string.Format ("10{0} 20{0} 30", CultureInfo.CurrentCulture.TextInfo.ListSeparator);
28 colStrInvariant = string.Format ("10{0} 20{0} 30", CultureInfo.InvariantCulture.TextInfo.ListSeparator);
30 colnamed = Color.ForestGreen;
31 colnamedStr = "ForestGreen";
33 colconv = (ColorConverter) TypeDescriptor.GetConverter (col);
37 public void CanConvertFrom () {
38 Assert.IsTrue (colconv.CanConvertFrom (typeof (String)), "CCF#1");
39 Assert.IsTrue (colconv.CanConvertFrom (null, typeof (String)), "CCF#1a");
40 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#2");
41 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#3");
42 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Point)), "CCF#4");
43 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
44 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Color)), "CCF#6");
45 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
46 Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Object)), "CCF#8");
47 Assert.IsFalse ( colconv.CanConvertFrom (null, typeof (int)), "CCF#9");
48 Assert.IsTrue (colconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#10");
52 public void CanConvertTo () {
53 Assert.IsTrue (colconv.CanConvertTo (typeof (String)), "CCT#1");
54 Assert.IsTrue (colconv.CanConvertTo (null, typeof (String)), "CCT#1a");
55 Assert.IsFalse (colconv.CanConvertTo (null, typeof (Rectangle)), "CCT#2");
56 Assert.IsFalse (colconv.CanConvertTo (null, typeof (RectangleF)), "CCT#3");
57 Assert.IsFalse (colconv.CanConvertTo (null, typeof (Point)), "CCT#4");
58 Assert.IsFalse (colconv.CanConvertTo (null, typeof (PointF)), "CCT#5");
59 Assert.IsFalse (colconv.CanConvertTo (null, typeof (Color)), "CCT#6");
60 Assert.IsFalse (colconv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
61 Assert.IsFalse (colconv.CanConvertTo (null, typeof (Object)), "CCT#8");
62 Assert.IsFalse (colconv.CanConvertTo (null, typeof (int)), "CCT#9");
63 Assert.IsTrue (colconv.CanConvertTo (typeof (InstanceDescriptor)), "CCT#10");
67 public void ConvertFrom ()
69 Color color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
71 Assert.AreEqual (35, color.A, "CF1#1");
72 Assert.AreEqual (25, color.R, "CF1#2");
73 Assert.AreEqual (10, color.G, "CF1#3");
74 Assert.AreEqual (68, color.B, "CF1#4");
76 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
78 Assert.AreEqual (0, color.A, "CF2#1");
79 Assert.AreEqual (25, color.R, "CF2#2");
80 Assert.AreEqual (10, color.G, "CF2#3");
81 Assert.AreEqual (68, color.B, "CF2#4");
83 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
85 Assert.AreEqual (255, color.A, "CF3#1");
86 Assert.AreEqual (25, color.R, "CF3#2");
87 Assert.AreEqual (10, color.G, "CF3#3");
88 Assert.AreEqual (68, color.B, "CF3#4");
90 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
92 Assert.AreEqual (255, color.A, "CF4#1");
93 Assert.AreEqual (25, color.R, "CF4#2");
94 Assert.AreEqual (10, color.G, "CF4#3");
95 Assert.AreEqual (68, color.B, "CF4#4");
97 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
99 Assert.AreEqual (0, color.A, "CF5#1");
100 Assert.AreEqual (1, color.R, "CF5#2");
101 Assert.AreEqual (178, color.G, "CF5#3");
102 Assert.AreEqual (7, color.B, "CF5#4");
104 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
106 Assert.AreEqual (0, color.A, "CF6#1");
107 Assert.AreEqual (0, color.R, "CF6#2");
108 Assert.AreEqual (0, color.G, "CF6#3");
109 Assert.AreEqual (10, color.B, "CF6#4");
111 color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
113 Assert.AreEqual (0, color.A, "CF7#1");
114 Assert.AreEqual (0, color.R, "CF7#2");
115 Assert.AreEqual (0, color.G, "CF7#3");
116 Assert.AreEqual (0, color.B, "CF7#4");
118 Assert.AreEqual (col, (Color) colconv.ConvertFrom (null,
119 CultureInfo.InvariantCulture, colStrInvariant), "CF#1");
120 Assert.AreEqual (colnamed, (Color) colconv.ConvertFrom (null,
121 CultureInfo.InvariantCulture, colnamedStr), "CF#2");
123 Assert.AreEqual (Color.Empty, colconv.ConvertFrom (string.Empty), "CF#3");
124 Assert.AreEqual (Color.Empty, colconv.ConvertFrom (" "), "CF#4");
125 Assert.AreEqual (Color.Red, colconv.ConvertFrom ("Red"), "CF#5");
126 Assert.AreEqual (Color.Red, colconv.ConvertFrom (" Red "), "CF#6");
131 [ExpectedException (typeof (ArgumentException))]
132 public void ConvertFrom_x1 ()
134 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10, 20");
138 [ExpectedException (typeof (ArgumentException))]
139 public void ConvertFrom_x2 ()
141 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "-10, 20, 30");
145 [ExpectedException (typeof (ArgumentException))]
146 public void ConvertFrom_x3 ()
148 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
153 [ExpectedException (typeof (Exception))]
154 public void ConvertFrom_x4 ()
156 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
161 [ExpectedException (typeof (NotSupportedException))]
162 public void ConvertFrom_x5 ()
164 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
169 [ExpectedException (typeof (NotSupportedException))]
170 public void ConvertFrom_x6 ()
172 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
173 new PointF (10, 10));
177 [ExpectedException (typeof (NotSupportedException))]
178 public void ConvertFrom_x7 ()
180 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
185 [ExpectedException (typeof (NotSupportedException))]
186 public void ConvertFrom_x8 ()
188 colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
193 [ExpectedException (typeof (NotSupportedException))]
194 public void ConvertFrom_x9 ()
196 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
200 public void ConvertTo ()
202 Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
203 Color.FromArgb (10, 20, 30), typeof (String)), "CT#1");
204 Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
205 Color.FromArgb (255, 10, 20, 30), typeof (String)), "CT#2");
206 Assert.AreEqual ("10, 20, 30, 40", colconv.ConvertTo (null, CultureInfo.InvariantCulture,
207 Color.FromArgb (10, 20, 30, 40), typeof (String)), "CT#3");
208 Assert.AreEqual (colnamedStr, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
209 colnamed, typeof (String)), "CT#4");
211 Assert.AreEqual (string.Empty, colconv.ConvertTo (Color.Empty, typeof (string)), "CT#5");
212 Assert.AreEqual ("Red", colconv.ConvertTo (Color.Red, typeof (string)), "CT#6");
213 Assert.AreEqual (string.Empty, colconv.ConvertTo (null, typeof (string)), "CT#7");
214 Assert.AreEqual ("test", colconv.ConvertTo ("test", typeof (string)), "CT#8");
218 [ExpectedException (typeof (NotSupportedException))]
219 public void ConvertTo_x1 ()
221 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
226 [ExpectedException (typeof (NotSupportedException))]
227 public void ConvertTo_x2 ()
229 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
234 [ExpectedException (typeof (NotSupportedException))]
235 public void ConvertTo_x3 ()
237 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
242 [ExpectedException (typeof (NotSupportedException))]
243 public void ConvertTo_x4 ()
245 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
250 [ExpectedException (typeof (NotSupportedException))]
251 public void ConvertTo_x5 ()
253 colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
258 public void GetCreateInstanceSupported ()
260 Assert.IsTrue (!colconv.GetCreateInstanceSupported (), "GCIS#1");
261 Assert.IsTrue (!colconv.GetCreateInstanceSupported (null), "GCIS#2");
265 public void CreateInstance ()
267 Hashtable ht = new Hashtable ();
268 ht.Add ("R", 10); ht.Add ("G", 20); ht.Add ("B", 30);
270 Assert.AreEqual (null, colconv.CreateInstance (ht), "CI#1");
272 ht.Add ("Name", "ForestGreen");
274 Assert.AreEqual (null, colconv.CreateInstance (null, ht), "CI#2");
278 public void GetPropertiesSupported ()
280 Assert.IsTrue (!colconv.GetPropertiesSupported (), "GPS#1");
281 Assert.IsTrue (!colconv.GetPropertiesSupported (null), "GPS#2");
285 public void GetProperties ()
289 Assert.AreEqual (null, colconv.GetProperties (col), "GP1#1");
291 Assert.AreEqual (null, colconv.GetProperties (null, col, null), "GP2#1");
293 attrs = Attribute.GetCustomAttributes (typeof (Color), true);
294 Assert.AreEqual (null, colconv.GetProperties (null, col, attrs), "GP3#5");
298 public void ConvertFromInvariantString_string ()
300 Assert.AreEqual (col, colconv.ConvertFromInvariantString (colStrInvariant), "CFISS#1");
301 Assert.AreEqual (colnamed, colconv.ConvertFromInvariantString (colnamedStr), "CFISS#2");
305 [ExpectedException (typeof (ArgumentException))]
306 public void ConvertFromInvariantString_InvalidComponentCount ()
308 colconv.ConvertFromInvariantString ("1, 2, 3, 4, 5");
312 public void ConvertFromInvariantString_InvalidNumber ()
315 colconv.ConvertFromInvariantString ("hello");
317 } catch (Exception ex) {
318 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
319 Assert.IsNotNull (ex.InnerException, "#3");
320 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
325 public void ConvertFromString_string ()
327 Assert.AreEqual (col, colconv.ConvertFromString (colStr), "CFSS#1");
328 Assert.AreEqual (colnamed, colconv.ConvertFromString (colnamedStr), "CFSS#2");
332 [ExpectedException (typeof (ArgumentException))]
333 public void ConvertFromString_InvalidComponentCount ()
335 CultureInfo culture = CultureInfo.CurrentCulture;
336 colconv.ConvertFromString (string.Format (culture,
337 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator[0]));
341 public void ConvertFromString_InvalidNumber ()
344 colconv.ConvertFromString ("hello");
346 } catch (Exception ex) {
347 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
348 Assert.IsNotNull (ex.InnerException, "#3");
349 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
354 public void ConvertToInvariantString_string () {
355 Assert.AreEqual (colStrInvariant, colconv.ConvertToInvariantString (col), "CFISS#1");
356 Assert.AreEqual (colnamedStr, colconv.ConvertToInvariantString (colnamed), "CFISS#2");
360 public void ConvertToString_string () {
361 Assert.AreEqual (colStr, colconv.ConvertToString (col), "CFISS#1");
362 Assert.AreEqual (colnamedStr, colconv.ConvertToString (colnamed), "CFISS#3");
366 public void GetStandardValuesSupported () {
367 Assert.IsTrue (colconv.GetStandardValuesSupported ());
371 public void GetStandardValues () {
372 Assert.AreEqual (167, colconv.GetStandardValues ().Count);
373 Assert.AreEqual (167, colconv.GetStandardValues (null).Count);
377 public void GetStandardValuesExclusive () {
378 Assert.AreEqual (false, colconv.GetStandardValuesExclusive ());