merge -r 58784:58785
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / ColorConverter.cs
1 using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.ComponentModel.Design.Serialization;
5 using System.Drawing;
6 using System.Globalization;
7 using System.Security.Permissions;
8
9 using NUnit.Framework;
10
11 namespace MonoTests.System.Drawing {
12
13         [TestFixture]
14         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
15         public class ColorConverterFixture
16         {
17                 Color col;
18                 Color colnamed;
19                 ColorConverter colconv;
20                 String colStr;
21                 String colStrInvariant;
22                 String colnamedStr;
23
24                 [SetUp]
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);
29
30                         colnamed = Color.ForestGreen;
31                         colnamedStr = "ForestGreen";
32
33                         colconv = (ColorConverter) TypeDescriptor.GetConverter (col);
34                 }
35
36                 [Test]
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");
49                 }
50
51                 [Test]
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");
64                 }
65
66                 [Test]
67                 public void ConvertFrom ()
68                 {
69                         Color color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
70                                 "#0x23190A44");
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");
75
76                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
77                                 "#0X190A44");
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");
82
83                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
84                                 "0x190A44");
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");
89
90                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
91                                 "0X190A44");
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");
96
97                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
98                                 "111111");
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");
103
104                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
105                                 "10");
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");
110
111                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
112                                 "0");
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");
117
118                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
119                                 "0x123");
120                         Assert.AreEqual (255, color.A, "CF8#1");
121                         Assert.AreEqual (17, color.R, "CF8#2");
122                         Assert.AreEqual (34, color.G, "CF8#3");
123                         Assert.AreEqual (51, color.B, "CF8#4");
124
125                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
126                                 "#123");
127                         Assert.AreEqual (255, color.A, "CF9#1");
128                         Assert.AreEqual (17, color.R, "CF9#2");
129                         Assert.AreEqual (34, color.G, "CF9#3");
130                         Assert.AreEqual (51, color.B, "CF9#4");
131
132                         Assert.AreEqual (col, (Color) colconv.ConvertFrom (null,
133                                 CultureInfo.InvariantCulture, colStrInvariant), "CF#1");
134                         Assert.AreEqual (colnamed, (Color) colconv.ConvertFrom (null,
135                                 CultureInfo.InvariantCulture, colnamedStr), "CF#2");
136
137                         Assert.AreEqual (Color.Empty, colconv.ConvertFrom (string.Empty), "CF#3");
138                         Assert.AreEqual (Color.Empty, colconv.ConvertFrom (" "), "CF#4");
139                         Assert.AreEqual (Color.Red, colconv.ConvertFrom ("Red"), "CF#5");
140                         Assert.AreEqual (Color.Red, colconv.ConvertFrom (" Red "), "CF#6");
141
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (ArgumentException))]
146                 public void ConvertFrom_x1 ()
147                 {
148                                 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10, 20");
149                 }
150
151                 [Test]
152                 [ExpectedException (typeof (ArgumentException))]
153                 public void ConvertFrom_x2 ()
154                 {
155                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "-10, 20, 30");
156                 }
157
158                 [Test]
159                 [ExpectedException (typeof (ArgumentException))]
160                 public void ConvertFrom_x3 ()
161                 {
162                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
163                                         "1, 1, 1, 1, 1");
164                 }
165
166                 [Test]
167                 [ExpectedException (typeof (Exception))]
168                 public void ConvertFrom_x4 ()
169                 {
170                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
171                                 "*1, 1");
172                 }
173
174                 [Test]
175                 [ExpectedException (typeof (NotSupportedException))]
176                 public void ConvertFrom_x5 ()
177                 {
178                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
179                                         new Point (10, 10));
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (NotSupportedException))]
184                 public void ConvertFrom_x6 ()
185                 {
186                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
187                                         new PointF (10, 10));
188                 }
189
190                 [Test]
191                 [ExpectedException (typeof (NotSupportedException))]
192                 public void ConvertFrom_x7 ()
193                 {
194                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
195                                         new Size (10, 10));
196                 }
197
198                 [Test]
199                 [ExpectedException (typeof (NotSupportedException))]
200                 public void ConvertFrom_x8 ()
201                 {
202                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
203                                         new SizeF (10, 10));
204                 }
205
206                 [Test]
207                 [ExpectedException (typeof (NotSupportedException))]
208                 public void ConvertFrom_x9 ()
209                 {
210                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
211                 }
212
213                 [Test]
214                 public void ConvertTo ()
215                 {
216                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
217                                 Color.FromArgb (10, 20, 30), typeof (String)), "CT#1");
218                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
219                                 Color.FromArgb (255, 10, 20, 30), typeof (String)), "CT#2");
220                         Assert.AreEqual ("10, 20, 30, 40", colconv.ConvertTo (null, CultureInfo.InvariantCulture,
221                                 Color.FromArgb (10, 20, 30, 40), typeof (String)), "CT#3");
222                         Assert.AreEqual (colnamedStr, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
223                                 colnamed, typeof (String)), "CT#4");
224
225                         Assert.AreEqual (string.Empty, colconv.ConvertTo (Color.Empty, typeof (string)), "CT#5");
226                         Assert.AreEqual ("Red", colconv.ConvertTo (Color.Red, typeof (string)), "CT#6");
227                         Assert.AreEqual (string.Empty, colconv.ConvertTo (null, typeof (string)), "CT#7");
228                         Assert.AreEqual ("test", colconv.ConvertTo ("test", typeof (string)), "CT#8");
229                 }
230
231                 [Test]
232                 [ExpectedException (typeof (NotSupportedException))]
233                 public void ConvertTo_x1 ()
234                 {
235                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
236                                         typeof (Color));
237                 }
238
239                 [Test]
240                 [ExpectedException (typeof (NotSupportedException))]
241                 public void ConvertTo_x2 ()
242                 {
243                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
244                                         typeof (SizeF));
245                 }
246
247                 [Test]
248                 [ExpectedException (typeof (NotSupportedException))]
249                 public void ConvertTo_x3 ()
250                 {
251                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
252                                         typeof (Point));
253                 }
254
255                 [Test]
256                 [ExpectedException (typeof (NotSupportedException))]
257                 public void ConvertTo_x4 ()
258                 {
259                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
260                                         typeof (PointF));
261                 }
262
263                 [Test]
264                 [ExpectedException (typeof (NotSupportedException))]
265                 public void ConvertTo_x5 ()
266                 {
267                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
268                                         typeof (int));
269                 }
270
271                 [Test]
272                 public void GetCreateInstanceSupported ()
273                 {
274                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (), "GCIS#1");
275                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (null), "GCIS#2");
276                 }
277
278                 [Test]
279                 public void CreateInstance ()
280                 {
281                         Hashtable ht = new Hashtable ();
282                         ht.Add ("R", 10); ht.Add ("G", 20); ht.Add ("B", 30);
283
284                         Assert.AreEqual (null, colconv.CreateInstance (ht), "CI#1");
285
286                         ht.Add ("Name", "ForestGreen");
287
288                         Assert.AreEqual (null, colconv.CreateInstance (null, ht), "CI#2");
289                 }
290
291                 [Test]
292                 public void GetPropertiesSupported ()
293                 {
294                         Assert.IsTrue (!colconv.GetPropertiesSupported (), "GPS#1");
295                         Assert.IsTrue (!colconv.GetPropertiesSupported (null), "GPS#2");
296                 }
297
298                 [Test]
299                 public void GetProperties ()
300                 {
301                         Attribute [] attrs;
302
303                         Assert.AreEqual (null, colconv.GetProperties (col), "GP1#1");
304
305                         Assert.AreEqual (null, colconv.GetProperties (null, col, null), "GP2#1");
306
307                         attrs = Attribute.GetCustomAttributes (typeof (Color), true);
308                         Assert.AreEqual (null, colconv.GetProperties (null, col, attrs), "GP3#5");
309                 }
310
311                 [Test]
312                 public void ConvertFromInvariantString_string ()
313                 {
314                         Assert.AreEqual (col, colconv.ConvertFromInvariantString (colStrInvariant), "CFISS#1");
315                         Assert.AreEqual (colnamed, colconv.ConvertFromInvariantString (colnamedStr), "CFISS#2");
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (ArgumentException))]
320                 public void ConvertFromInvariantString_InvalidComponentCount ()
321                 {
322                         colconv.ConvertFromInvariantString ("1, 2, 3, 4, 5");
323                 }
324
325                 [Test]
326                 public void ConvertFromInvariantString_InvalidNumber ()
327                 {
328                         try {
329                                 colconv.ConvertFromInvariantString ("hello");
330                                 Assert.Fail ("#1");
331                         } catch (Exception ex) {
332                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
333                                 Assert.IsNotNull (ex.InnerException, "#3");
334                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
335                         }
336                 }
337
338                 [Test]
339                 public void ConvertFromString_string ()
340                 {
341                         Assert.AreEqual (col, colconv.ConvertFromString (colStr), "CFSS#1");
342                         Assert.AreEqual (colnamed, colconv.ConvertFromString (colnamedStr), "CFSS#2");
343                 }
344
345                 [Test]
346                 [ExpectedException (typeof (ArgumentException))]
347                 public void ConvertFromString_InvalidComponentCount ()
348                 {
349                         CultureInfo culture = CultureInfo.CurrentCulture;
350                         colconv.ConvertFromString (string.Format (culture,
351                                 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator[0]));
352                 }
353
354                 [Test]
355                 public void ConvertFromString_InvalidNumber ()
356                 {
357                         try {
358                                 colconv.ConvertFromString ("hello");
359                                 Assert.Fail ("#1");
360                         } catch (Exception ex) {
361                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
362                                 Assert.IsNotNull (ex.InnerException, "#3");
363                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
364                         }
365                 }
366
367                 [Test]
368                 public void ConvertToInvariantString_string () {
369                         Assert.AreEqual (colStrInvariant, colconv.ConvertToInvariantString (col), "CFISS#1");
370                         Assert.AreEqual (colnamedStr, colconv.ConvertToInvariantString (colnamed), "CFISS#2");
371                 }
372
373                 [Test]
374                 public void ConvertToString_string () {
375                         Assert.AreEqual (colStr, colconv.ConvertToString (col), "CFISS#1");
376                         Assert.AreEqual (colnamedStr, colconv.ConvertToString (colnamed), "CFISS#3");
377                 }
378
379                 [Test]
380                 public void GetStandardValuesSupported () {
381                         Assert.IsTrue (colconv.GetStandardValuesSupported ());
382                 }
383
384                 [Test]
385                 public void GetStandardValues () {
386                         Assert.AreEqual (167, colconv.GetStandardValues ().Count);
387                         Assert.AreEqual (167, colconv.GetStandardValues (null).Count);
388                 }
389
390                 [Test]
391                 public void GetStandardValuesExclusive () {
392                         Assert.AreEqual (false, colconv.GetStandardValuesExclusive ());
393                 }
394         }
395 }
396