2010-07-08 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / ColorConverter.cs
1 //
2 // ColorConverter class testing unit
3 //
4 // Copyright (C) 2006 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.Collections;
28 using System.ComponentModel;
29 using System.ComponentModel.Design.Serialization;
30 using System.Drawing;
31 using System.Globalization;
32 using System.Security.Permissions;
33
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Drawing {
37
38         [TestFixture]
39         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
40         public class ColorConverterTest {
41
42                 Color col;
43                 Color colnamed;
44                 ColorConverter colconv;
45                 String colStr;
46                 String colStrInvariant;
47                 String colnamedStr;
48
49                 [SetUp]
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);
54
55                         colnamed = Color.ForestGreen;
56                         colnamedStr = "ForestGreen";
57
58                         colconv = (ColorConverter) TypeDescriptor.GetConverter (col);
59                 }
60
61                 [Test]
62 #if TARGET_JVM
63                 [NUnit.Framework.Category ("NotWorking")]
64 #endif
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");
77                 }
78
79                 [Test]
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");
92                 }
93
94                 [Test]
95 #if TARGET_JVM
96                 [NUnit.Framework.Category ("NotWorking")]
97 #endif
98                 public void ConvertFrom ()
99                 {
100                         Color color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
101                                 "#0x23190A44");
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");
106
107                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
108                                 "#0X190A44");
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");
113
114                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
115                                 "0x190A44");
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");
120
121                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
122                                 "0X190A44");
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");
127
128                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
129                                 "111111");
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");
134
135                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
136                                 "10");
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");
141
142                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
143                                 "0");
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");
148
149
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");
154
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");
159
160                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
161                                 "0x123");
162                         Assert.AreEqual (0, color.A, "CF8#1");
163                         Assert.AreEqual (0, color.R, "CF8#2");
164                         Assert.AreEqual (1, color.G, "CF8#3");
165                         Assert.AreEqual (35, color.B, "CF8#4");
166
167                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
168                                 "#123");
169                         Assert.AreEqual (0, color.A, "CF9#1");
170                         Assert.AreEqual (0, color.R, "CF9#2");
171                         Assert.AreEqual (1, color.G, "CF9#3");
172                         Assert.AreEqual (35, color.B, "CF9#4");
173                 }
174
175                 [Test]
176                 [ExpectedException (typeof (ArgumentException))]
177                 public void ConvertFrom_x1 ()
178                 {
179                                 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10, 20");
180                 }
181
182                 [Test]
183                 [ExpectedException (typeof (ArgumentException))]
184                 public void ConvertFrom_x2 ()
185                 {
186                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "-10, 20, 30");
187                 }
188
189                 [Test]
190                 [ExpectedException (typeof (ArgumentException))]
191                 public void ConvertFrom_x3 ()
192                 {
193                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
194                                         "1, 1, 1, 1, 1");
195                 }
196
197                 [Test]
198                 [ExpectedException (typeof (Exception))]
199 #if TARGET_JVM
200                 [NUnit.Framework.Category ("NotWorking")]
201 #endif
202                 public void ConvertFrom_x4 ()
203                 {
204                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
205                                 "*1, 1");
206                 }
207
208                 [Test]
209                 [ExpectedException (typeof (NotSupportedException))]
210                 public void ConvertFrom_x5 ()
211                 {
212                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
213                                         new Point (10, 10));
214                 }
215
216                 [Test]
217                 [ExpectedException (typeof (NotSupportedException))]
218                 public void ConvertFrom_x6 ()
219                 {
220                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
221                                         new PointF (10, 10));
222                 }
223
224                 [Test]
225                 [ExpectedException (typeof (NotSupportedException))]
226                 public void ConvertFrom_x7 ()
227                 {
228                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
229                                         new Size (10, 10));
230                 }
231
232                 [Test]
233                 [ExpectedException (typeof (NotSupportedException))]
234                 public void ConvertFrom_x8 ()
235                 {
236                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
237                                         new SizeF (10, 10));
238                 }
239
240                 [Test]
241                 [ExpectedException (typeof (NotSupportedException))]
242                 public void ConvertFrom_x9 ()
243                 {
244                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
245                 }
246
247                 [Test]
248                 public void ConvertFrom_CultureNull ()
249                 {
250                         Color color = (Color) colconv.ConvertFrom (null, null, "#0x23190A44");
251                         Assert.AreEqual (35, color.A, "A");
252                         Assert.AreEqual (25, color.R, "R");
253                         Assert.AreEqual (10, color.G, "G");
254                         Assert.AreEqual (68, color.B, "B");
255                 }
256
257                 [Test]
258                 public void ConvertTo ()
259                 {
260                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
261                                 Color.FromArgb (10, 20, 30), typeof (String)), "CT#1");
262                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
263                                 Color.FromArgb (255, 10, 20, 30), typeof (String)), "CT#2");
264                         Assert.AreEqual ("10, 20, 30, 40", colconv.ConvertTo (null, CultureInfo.InvariantCulture,
265                                 Color.FromArgb (10, 20, 30, 40), typeof (String)), "CT#3");
266                         Assert.AreEqual (colnamedStr, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
267                                 colnamed, typeof (String)), "CT#4");
268
269                         Assert.AreEqual (string.Empty, colconv.ConvertTo (Color.Empty, typeof (string)), "CT#5");
270                         Assert.AreEqual ("Red", colconv.ConvertTo (Color.Red, typeof (string)), "CT#6");
271                         Assert.AreEqual (string.Empty, colconv.ConvertTo (null, typeof (string)), "CT#7");
272                         Assert.AreEqual ("test", colconv.ConvertTo ("test", typeof (string)), "CT#8");
273                 }
274
275                 [Test]
276                 [ExpectedException (typeof (NotSupportedException))]
277                 public void ConvertTo_x1 ()
278                 {
279                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
280                                         typeof (Color));
281                 }
282
283                 [Test]
284                 [ExpectedException (typeof (NotSupportedException))]
285                 public void ConvertTo_x2 ()
286                 {
287                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
288                                         typeof (SizeF));
289                 }
290
291                 [Test]
292                 [ExpectedException (typeof (NotSupportedException))]
293                 public void ConvertTo_x3 ()
294                 {
295                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
296                                         typeof (Point));
297                 }
298
299                 [Test]
300                 [ExpectedException (typeof (NotSupportedException))]
301                 public void ConvertTo_x4 ()
302                 {
303                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
304                                         typeof (PointF));
305                 }
306
307                 [Test]
308                 [ExpectedException (typeof (NotSupportedException))]
309                 public void ConvertTo_x5 ()
310                 {
311                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
312                                         typeof (int));
313                 }
314
315                 [Test]
316                 public void GetCreateInstanceSupported ()
317                 {
318                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (), "GCIS#1");
319                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (null), "GCIS#2");
320                 }
321
322                 [Test]
323                 public void CreateInstance ()
324                 {
325                         Hashtable ht = new Hashtable ();
326                         ht.Add ("R", 10); ht.Add ("G", 20); ht.Add ("B", 30);
327
328                         Assert.AreEqual (null, colconv.CreateInstance (ht), "CI#1");
329
330                         ht.Add ("Name", "ForestGreen");
331
332                         Assert.AreEqual (null, colconv.CreateInstance (null, ht), "CI#2");
333                 }
334
335                 [Test]
336                 public void GetPropertiesSupported ()
337                 {
338                         Assert.IsTrue (!colconv.GetPropertiesSupported (), "GPS#1");
339                         Assert.IsTrue (!colconv.GetPropertiesSupported (null), "GPS#2");
340                 }
341
342                 [Test]
343                 public void GetProperties ()
344                 {
345                         Attribute [] attrs;
346
347                         Assert.AreEqual (null, colconv.GetProperties (col), "GP1#1");
348
349                         Assert.AreEqual (null, colconv.GetProperties (null, col, null), "GP2#1");
350
351                         attrs = Attribute.GetCustomAttributes (typeof (Color), true);
352                         Assert.AreEqual (null, colconv.GetProperties (null, col, attrs), "GP3#5");
353                 }
354
355                 [Test]
356                 public void ConvertFromInvariantString_string ()
357                 {
358                         Assert.AreEqual (col, colconv.ConvertFromInvariantString (colStrInvariant), "CFISS#1");
359                         Assert.AreEqual (colnamed, colconv.ConvertFromInvariantString (colnamedStr), "CFISS#2");
360                 }
361
362                 [Test]
363                 [ExpectedException (typeof (ArgumentException))]
364                 public void ConvertFromInvariantString_InvalidComponentCount ()
365                 {
366                         colconv.ConvertFromInvariantString ("1, 2, 3, 4, 5");
367                 }
368
369                 [Test]
370 #if TARGET_JVM
371                 [NUnit.Framework.Category ("NotWorking")]
372 #endif
373                 public void ConvertFromInvariantString_InvalidNumber ()
374                 {
375                         try {
376                                 colconv.ConvertFromInvariantString ("hello");
377                                 Assert.Fail ("#1");
378                         } catch (Exception ex) {
379                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
380                                 Assert.IsNotNull (ex.InnerException, "#3");
381                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
382                         }
383                 }
384
385                 [Test]
386                 public void ConvertFromString_string ()
387                 {
388                         Assert.AreEqual (col, colconv.ConvertFromString (colStr), "CFSS#1");
389                         Assert.AreEqual (colnamed, colconv.ConvertFromString (colnamedStr), "CFSS#2");
390                 }
391
392                 [Test]
393                 [ExpectedException (typeof (ArgumentException))]
394                 public void ConvertFromString_InvalidComponentCount ()
395                 {
396                         CultureInfo culture = CultureInfo.CurrentCulture;
397                         colconv.ConvertFromString (string.Format (culture,
398                                 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator[0]));
399                 }
400
401                 [Test]
402 #if TARGET_JVM
403                 [NUnit.Framework.Category ("NotWorking")]
404 #endif
405                 public void ConvertFromString_InvalidNumber ()
406                 {
407                         try {
408                                 colconv.ConvertFromString ("hello");
409                                 Assert.Fail ("#1");
410                         } catch (Exception ex) {
411                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
412                                 Assert.IsNotNull (ex.InnerException, "#3");
413                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
414                         }
415                 }
416
417                 [Test]
418                 public void ConvertToInvariantString_string () {
419                         Assert.AreEqual (colStrInvariant, colconv.ConvertToInvariantString (col), "CFISS#1");
420                         Assert.AreEqual (colnamedStr, colconv.ConvertToInvariantString (colnamed), "CFISS#2");
421                 }
422
423                 [Test]
424                 public void ConvertToString_string () {
425                         Assert.AreEqual (colStr, colconv.ConvertToString (col), "CFISS#1");
426                         Assert.AreEqual (colnamedStr, colconv.ConvertToString (colnamed), "CFISS#3");
427                 }
428
429                 [Test]
430                 public void GetStandardValuesSupported () {
431                         Assert.IsTrue (colconv.GetStandardValuesSupported ());
432                 }
433
434                 [Test]
435                 public void GetStandardValues () {
436 #if NET_2_0
437                         Assert.AreEqual ((int)KnownColor.MenuHighlight, colconv.GetStandardValues ().Count);
438                         Assert.AreEqual ((int)KnownColor.MenuHighlight, colconv.GetStandardValues (null).Count);                        
439 #else
440                         Assert.AreEqual ((int)KnownColor.YellowGreen, colconv.GetStandardValues ().Count);
441                         Assert.AreEqual ((int)KnownColor.YellowGreen, colconv.GetStandardValues (null).Count);  
442 #endif
443                 }
444
445                 [Test]
446                 public void GetStandardValuesExclusive () {
447                         Assert.AreEqual (false, colconv.GetStandardValuesExclusive ());
448                 }
449
450                 [Test]
451                 [ExpectedException (typeof (Exception))]
452                 public void ConvertFromString_FromHtml_PoundTooLarge ()
453                 {
454                         colconv.ConvertFromString ("#100000000");
455                 }
456         }
457 }
458