Merge pull request #757 from mlintner/master
[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                 public void CanConvertFrom () {
63                         Assert.IsTrue (colconv.CanConvertFrom (typeof (String)), "CCF#1");
64                         Assert.IsTrue (colconv.CanConvertFrom (null, typeof (String)), "CCF#1a");
65                         Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#2");
66                         Assert.IsFalse (colconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#3");
67                         Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Point)), "CCF#4");
68                         Assert.IsFalse (colconv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
69                         Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Color)), "CCF#6");
70                         Assert.IsFalse (colconv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
71                         Assert.IsFalse (colconv.CanConvertFrom (null, typeof (Object)), "CCF#8");
72                         Assert.IsFalse ( colconv.CanConvertFrom (null, typeof (int)), "CCF#9");
73                         Assert.IsTrue (colconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#10");
74                 }
75
76                 [Test]
77                 public void CanConvertTo () {
78                         Assert.IsTrue (colconv.CanConvertTo (typeof (String)), "CCT#1");
79                         Assert.IsTrue (colconv.CanConvertTo (null, typeof (String)), "CCT#1a");
80                         Assert.IsFalse (colconv.CanConvertTo (null, typeof (Rectangle)), "CCT#2");
81                         Assert.IsFalse (colconv.CanConvertTo (null, typeof (RectangleF)), "CCT#3");
82                         Assert.IsFalse (colconv.CanConvertTo (null, typeof (Point)), "CCT#4");
83                         Assert.IsFalse (colconv.CanConvertTo (null, typeof (PointF)), "CCT#5");
84                         Assert.IsFalse (colconv.CanConvertTo (null, typeof (Color)), "CCT#6");
85                         Assert.IsFalse (colconv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
86                         Assert.IsFalse (colconv.CanConvertTo (null, typeof (Object)), "CCT#8");
87                         Assert.IsFalse (colconv.CanConvertTo (null, typeof (int)), "CCT#9");
88                         Assert.IsTrue (colconv.CanConvertTo (typeof (InstanceDescriptor)), "CCT#10");
89                 }
90
91                 [Test]
92                 public void ConvertFrom ()
93                 {
94                         Color color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
95                                 "#0x23190A44");
96                         Assert.AreEqual (35, color.A, "CF1#1");
97                         Assert.AreEqual (25, color.R, "CF1#2");
98                         Assert.AreEqual (10, color.G, "CF1#3");
99                         Assert.AreEqual (68, color.B, "CF1#4");
100
101                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
102                                 "#0X190A44");
103                         Assert.AreEqual (0, color.A, "CF2#1");
104                         Assert.AreEqual (25, color.R, "CF2#2");
105                         Assert.AreEqual (10, color.G, "CF2#3");
106                         Assert.AreEqual (68, color.B, "CF2#4");
107
108                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
109                                 "0x190A44");
110                         Assert.AreEqual (255, color.A, "CF3#1");
111                         Assert.AreEqual (25, color.R, "CF3#2");
112                         Assert.AreEqual (10, color.G, "CF3#3");
113                         Assert.AreEqual (68, color.B, "CF3#4");
114
115                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
116                                 "0X190A44");
117                         Assert.AreEqual (255, color.A, "CF4#1");
118                         Assert.AreEqual (25, color.R, "CF4#2");
119                         Assert.AreEqual (10, color.G, "CF4#3");
120                         Assert.AreEqual (68, color.B, "CF4#4");
121
122                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
123                                 "111111");
124                         Assert.AreEqual (0, color.A, "CF5#1");
125                         Assert.AreEqual (1, color.R, "CF5#2");
126                         Assert.AreEqual (178, color.G, "CF5#3");
127                         Assert.AreEqual (7, color.B, "CF5#4");
128
129                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
130                                 "10");
131                         Assert.AreEqual (0, color.A, "CF6#1");
132                         Assert.AreEqual (0, color.R, "CF6#2");
133                         Assert.AreEqual (0, color.G, "CF6#3");
134                         Assert.AreEqual (10, color.B, "CF6#4");
135
136                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
137                                 "0");
138                         Assert.AreEqual (0, color.A, "CF7#1");
139                         Assert.AreEqual (0, color.R, "CF7#2");
140                         Assert.AreEqual (0, color.G, "CF7#3");
141                         Assert.AreEqual (0, color.B, "CF7#4");
142
143
144                         Assert.AreEqual (col, (Color) colconv.ConvertFrom (null,
145                                 CultureInfo.InvariantCulture, colStrInvariant), "CF#1");
146                         Assert.AreEqual (colnamed, (Color) colconv.ConvertFrom (null,
147                                 CultureInfo.InvariantCulture, colnamedStr), "CF#2");
148
149                         Assert.AreEqual (Color.Empty, colconv.ConvertFrom (string.Empty), "CF#3");
150                         Assert.AreEqual (Color.Empty, colconv.ConvertFrom (" "), "CF#4");
151                         Assert.AreEqual (Color.Red, colconv.ConvertFrom ("Red"), "CF#5");
152                         Assert.AreEqual (Color.Red, colconv.ConvertFrom (" Red "), "CF#6");
153
154                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
155                                 "0x123");
156                         Assert.AreEqual (0, color.A, "CF8#1");
157                         Assert.AreEqual (0, color.R, "CF8#2");
158                         Assert.AreEqual (1, color.G, "CF8#3");
159                         Assert.AreEqual (35, color.B, "CF8#4");
160
161                         color = (Color) colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
162                                 "#123");
163                         Assert.AreEqual (0, color.A, "CF9#1");
164                         Assert.AreEqual (0, color.R, "CF9#2");
165                         Assert.AreEqual (1, color.G, "CF9#3");
166                         Assert.AreEqual (35, color.B, "CF9#4");
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof (ArgumentException))]
171                 public void ConvertFrom_x1 ()
172                 {
173                                 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10, 20");
174                 }
175
176                 [Test]
177                 [ExpectedException (typeof (ArgumentException))]
178                 public void ConvertFrom_x2 ()
179                 {
180                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "-10, 20, 30");
181                 }
182
183                 [Test]
184                 [ExpectedException (typeof (ArgumentException))]
185                 public void ConvertFrom_x3 ()
186                 {
187                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
188                                         "1, 1, 1, 1, 1");
189                 }
190
191                 [Test]
192                 [ExpectedException (typeof (Exception))]
193                 public void ConvertFrom_x4 ()
194                 {
195                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
196                                 "*1, 1");
197                 }
198
199                 [Test]
200                 [ExpectedException (typeof (NotSupportedException))]
201                 public void ConvertFrom_x5 ()
202                 {
203                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
204                                         new Point (10, 10));
205                 }
206
207                 [Test]
208                 [ExpectedException (typeof (NotSupportedException))]
209                 public void ConvertFrom_x6 ()
210                 {
211                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
212                                         new PointF (10, 10));
213                 }
214
215                 [Test]
216                 [ExpectedException (typeof (NotSupportedException))]
217                 public void ConvertFrom_x7 ()
218                 {
219                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
220                                         new Size (10, 10));
221                 }
222
223                 [Test]
224                 [ExpectedException (typeof (NotSupportedException))]
225                 public void ConvertFrom_x8 ()
226                 {
227                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
228                                         new SizeF (10, 10));
229                 }
230
231                 [Test]
232                 [ExpectedException (typeof (NotSupportedException))]
233                 public void ConvertFrom_x9 ()
234                 {
235                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
236                 }
237
238                 [Test]
239                 public void ConvertFrom_CultureNull ()
240                 {
241                         Color color = (Color) colconv.ConvertFrom (null, null, "#0x23190A44");
242                         Assert.AreEqual (35, color.A, "A");
243                         Assert.AreEqual (25, color.R, "R");
244                         Assert.AreEqual (10, color.G, "G");
245                         Assert.AreEqual (68, color.B, "B");
246                 }
247
248                 [Test]
249                 public void ConvertTo ()
250                 {
251                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
252                                 Color.FromArgb (10, 20, 30), typeof (String)), "CT#1");
253                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
254                                 Color.FromArgb (255, 10, 20, 30), typeof (String)), "CT#2");
255                         Assert.AreEqual ("10, 20, 30, 40", colconv.ConvertTo (null, CultureInfo.InvariantCulture,
256                                 Color.FromArgb (10, 20, 30, 40), typeof (String)), "CT#3");
257                         Assert.AreEqual (colnamedStr, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
258                                 colnamed, typeof (String)), "CT#4");
259
260                         Assert.AreEqual (string.Empty, colconv.ConvertTo (Color.Empty, typeof (string)), "CT#5");
261                         Assert.AreEqual ("Red", colconv.ConvertTo (Color.Red, typeof (string)), "CT#6");
262                         Assert.AreEqual (string.Empty, colconv.ConvertTo (null, typeof (string)), "CT#7");
263                         Assert.AreEqual ("test", colconv.ConvertTo ("test", typeof (string)), "CT#8");
264                 }
265
266                 [Test]
267                 [ExpectedException (typeof (NotSupportedException))]
268                 public void ConvertTo_x1 ()
269                 {
270                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
271                                         typeof (Color));
272                 }
273
274                 [Test]
275                 [ExpectedException (typeof (NotSupportedException))]
276                 public void ConvertTo_x2 ()
277                 {
278                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
279                                         typeof (SizeF));
280                 }
281
282                 [Test]
283                 [ExpectedException (typeof (NotSupportedException))]
284                 public void ConvertTo_x3 ()
285                 {
286                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
287                                         typeof (Point));
288                 }
289
290                 [Test]
291                 [ExpectedException (typeof (NotSupportedException))]
292                 public void ConvertTo_x4 ()
293                 {
294                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
295                                         typeof (PointF));
296                 }
297
298                 [Test]
299                 [ExpectedException (typeof (NotSupportedException))]
300                 public void ConvertTo_x5 ()
301                 {
302                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
303                                         typeof (int));
304                 }
305
306                 [Test]
307                 public void GetCreateInstanceSupported ()
308                 {
309                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (), "GCIS#1");
310                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (null), "GCIS#2");
311                 }
312
313                 [Test]
314                 public void CreateInstance ()
315                 {
316                         Hashtable ht = new Hashtable ();
317                         ht.Add ("R", 10); ht.Add ("G", 20); ht.Add ("B", 30);
318
319                         Assert.AreEqual (null, colconv.CreateInstance (ht), "CI#1");
320
321                         ht.Add ("Name", "ForestGreen");
322
323                         Assert.AreEqual (null, colconv.CreateInstance (null, ht), "CI#2");
324                 }
325
326                 [Test]
327                 public void GetPropertiesSupported ()
328                 {
329                         Assert.IsTrue (!colconv.GetPropertiesSupported (), "GPS#1");
330                         Assert.IsTrue (!colconv.GetPropertiesSupported (null), "GPS#2");
331                 }
332
333                 [Test]
334                 public void GetProperties ()
335                 {
336                         Attribute [] attrs;
337
338                         Assert.AreEqual (null, colconv.GetProperties (col), "GP1#1");
339
340                         Assert.AreEqual (null, colconv.GetProperties (null, col, null), "GP2#1");
341
342                         attrs = Attribute.GetCustomAttributes (typeof (Color), true);
343                         Assert.AreEqual (null, colconv.GetProperties (null, col, attrs), "GP3#5");
344                 }
345
346                 [Test]
347                 public void ConvertFromInvariantString_string ()
348                 {
349                         Assert.AreEqual (col, colconv.ConvertFromInvariantString (colStrInvariant), "CFISS#1");
350                         Assert.AreEqual (colnamed, colconv.ConvertFromInvariantString (colnamedStr), "CFISS#2");
351                 }
352
353                 [Test]
354                 [ExpectedException (typeof (ArgumentException))]
355                 public void ConvertFromInvariantString_InvalidComponentCount ()
356                 {
357                         colconv.ConvertFromInvariantString ("1, 2, 3, 4, 5");
358                 }
359
360                 [Test]
361                 public void ConvertFromInvariantString_InvalidNumber ()
362                 {
363                         try {
364                                 colconv.ConvertFromInvariantString ("hello");
365                                 Assert.Fail ("#1");
366                         } catch (Exception ex) {
367                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
368                                 Assert.IsNotNull (ex.InnerException, "#3");
369                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
370                         }
371                 }
372
373                 [Test]
374                 public void ConvertFromString_string ()
375                 {
376                         Assert.AreEqual (col, colconv.ConvertFromString (colStr), "CFSS#1");
377                         Assert.AreEqual (colnamed, colconv.ConvertFromString (colnamedStr), "CFSS#2");
378                 }
379
380                 [Test]
381                 [ExpectedException (typeof (ArgumentException))]
382                 public void ConvertFromString_InvalidComponentCount ()
383                 {
384                         CultureInfo culture = CultureInfo.CurrentCulture;
385                         colconv.ConvertFromString (string.Format (culture,
386                                 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator[0]));
387                 }
388
389                 [Test]
390                 public void ConvertFromString_InvalidNumber ()
391                 {
392                         try {
393                                 colconv.ConvertFromString ("hello");
394                                 Assert.Fail ("#1");
395                         } catch (Exception ex) {
396                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
397                                 Assert.IsNotNull (ex.InnerException, "#3");
398                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
399                         }
400                 }
401
402                 [Test]
403                 public void ConvertToInvariantString_string () {
404                         Assert.AreEqual (colStrInvariant, colconv.ConvertToInvariantString (col), "CFISS#1");
405                         Assert.AreEqual (colnamedStr, colconv.ConvertToInvariantString (colnamed), "CFISS#2");
406                 }
407
408                 [Test]
409                 public void ConvertToString_string () {
410                         Assert.AreEqual (colStr, colconv.ConvertToString (col), "CFISS#1");
411                         Assert.AreEqual (colnamedStr, colconv.ConvertToString (colnamed), "CFISS#3");
412                 }
413
414                 [Test]
415                 public void GetStandardValuesSupported () {
416                         Assert.IsTrue (colconv.GetStandardValuesSupported ());
417                 }
418
419                 [Test]
420                 public void GetStandardValues () {
421                         Assert.AreEqual ((int)KnownColor.MenuHighlight, colconv.GetStandardValues ().Count);
422                         Assert.AreEqual ((int)KnownColor.MenuHighlight, colconv.GetStandardValues (null).Count);                        
423                 }
424
425                 [Test]
426                 public void GetStandardValuesExclusive () {
427                         Assert.AreEqual (false, colconv.GetStandardValuesExclusive ());
428                 }
429
430                 [Test]
431                 [ExpectedException (typeof (Exception))]
432                 public void ConvertFromString_FromHtml_PoundTooLarge ()
433                 {
434                         colconv.ConvertFromString ("#100000000");
435                 }
436         }
437 }
438