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