4877f496996b847baa5f131a26a54a155ff620d9
[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                 public void ConvertFrom_x1 ()
171                 {
172                         Assert.Throws<ArgumentException> (() => colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10, 20"));
173                 }
174
175                 [Test]
176                 public void ConvertFrom_x2 ()
177                 {
178                         Assert.Throws<ArgumentException> (() => colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "-10, 20, 30"));
179                 }
180
181                 [Test]
182                 public void ConvertFrom_x3 ()
183                 {
184                         Assert.Throws<ArgumentException> (() => colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
185                                         "1, 1, 1, 1, 1"));
186                 }
187
188                 [Test]
189                 public void ConvertFrom_x4 ()
190                 {
191                         Assert.Throws<Exception> (() => colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
192                                 "*1, 1"));
193                 }
194
195                 [Test]
196                 public void ConvertFrom_x5 ()
197                 {
198                         Assert.Throws<NotSupportedException> (() => colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
199                                         new Point (10, 10)));
200                 }
201
202                 [Test]
203                 public void ConvertFrom_x6 ()
204                 {
205                         Assert.Throws<NotSupportedException> (() => colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
206                                         new PointF (10, 10)));
207                 }
208
209                 [Test]
210                 public void ConvertFrom_x7 ()
211                 {
212                         Assert.Throws<NotSupportedException> (() => colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
213                                         new Size (10, 10)));
214                 }
215
216                 [Test]
217                 public void ConvertFrom_x8 ()
218                 {
219                         Assert.Throws<NotSupportedException> (() => colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
220                                         new SizeF (10, 10)));
221                 }
222
223                 [Test]
224                 public void ConvertFrom_x9 ()
225                 {
226                         Assert.Throws<NotSupportedException> (() => colconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10));
227                 }
228
229                 [Test]
230                 public void ConvertFrom_CultureNull ()
231                 {
232                         Color color = (Color) colconv.ConvertFrom (null, null, "#0x23190A44");
233                         Assert.AreEqual (35, color.A, "A");
234                         Assert.AreEqual (25, color.R, "R");
235                         Assert.AreEqual (10, color.G, "G");
236                         Assert.AreEqual (68, color.B, "B");
237                 }
238
239                 [Test]
240                 public void ConvertTo ()
241                 {
242                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
243                                 Color.FromArgb (10, 20, 30), typeof (String)), "CT#1");
244                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
245                                 Color.FromArgb (255, 10, 20, 30), typeof (String)), "CT#2");
246                         Assert.AreEqual ("10, 20, 30, 40", colconv.ConvertTo (null, CultureInfo.InvariantCulture,
247                                 Color.FromArgb (10, 20, 30, 40), typeof (String)), "CT#3");
248                         Assert.AreEqual (colnamedStr, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
249                                 colnamed, typeof (String)), "CT#4");
250
251                         Assert.AreEqual (string.Empty, colconv.ConvertTo (Color.Empty, typeof (string)), "CT#5");
252                         Assert.AreEqual ("Red", colconv.ConvertTo (Color.Red, typeof (string)), "CT#6");
253                         Assert.AreEqual (string.Empty, colconv.ConvertTo (null, typeof (string)), "CT#7");
254                         Assert.AreEqual ("test", colconv.ConvertTo ("test", typeof (string)), "CT#8");
255                 }
256
257                 [Test]
258                 public void ConvertTo_x1 ()
259                 {
260                         Assert.Throws<NotSupportedException> (() => colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
261                                         typeof (Color)));
262                 }
263
264                 [Test]
265                 public void ConvertTo_x2 ()
266                 {
267                         Assert.Throws<NotSupportedException> (() => colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
268                                         typeof (SizeF)));
269                 }
270
271                 [Test]
272                 public void ConvertTo_x3 ()
273                 {
274                         Assert.Throws<NotSupportedException> (() => colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
275                                         typeof (Point)));
276                 }
277
278                 [Test]
279                 public void ConvertTo_x4 ()
280                 {
281                         Assert.Throws<NotSupportedException> (() => colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
282                                         typeof (PointF)));
283                 }
284
285                 [Test]
286                 public void ConvertTo_x5 ()
287                 {
288                         Assert.Throws<NotSupportedException> (() => colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
289                                         typeof (int)));
290                 }
291
292                 [Test]
293                 public void GetCreateInstanceSupported ()
294                 {
295                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (), "GCIS#1");
296                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (null), "GCIS#2");
297                 }
298
299                 [Test]
300                 public void CreateInstance ()
301                 {
302                         Hashtable ht = new Hashtable ();
303                         ht.Add ("R", 10); ht.Add ("G", 20); ht.Add ("B", 30);
304
305                         Assert.AreEqual (null, colconv.CreateInstance (ht), "CI#1");
306
307                         ht.Add ("Name", "ForestGreen");
308
309                         Assert.AreEqual (null, colconv.CreateInstance (null, ht), "CI#2");
310                 }
311
312                 [Test]
313                 public void GetPropertiesSupported ()
314                 {
315                         Assert.IsTrue (!colconv.GetPropertiesSupported (), "GPS#1");
316                         Assert.IsTrue (!colconv.GetPropertiesSupported (null), "GPS#2");
317                 }
318
319                 [Test]
320                 public void GetProperties ()
321                 {
322                         Attribute [] attrs;
323
324                         Assert.AreEqual (null, colconv.GetProperties (col), "GP1#1");
325
326                         Assert.AreEqual (null, colconv.GetProperties (null, col, null), "GP2#1");
327
328                         attrs = Attribute.GetCustomAttributes (typeof (Color), true);
329                         Assert.AreEqual (null, colconv.GetProperties (null, col, attrs), "GP3#5");
330                 }
331
332                 [Test]
333                 public void ConvertFromInvariantString_string ()
334                 {
335                         Assert.AreEqual (col, colconv.ConvertFromInvariantString (colStrInvariant), "CFISS#1");
336                         Assert.AreEqual (colnamed, colconv.ConvertFromInvariantString (colnamedStr), "CFISS#2");
337                 }
338
339                 [Test]
340                 public void ConvertFromInvariantString_InvalidComponentCount ()
341                 {
342                         Assert.Throws<ArgumentException> (() => colconv.ConvertFromInvariantString ("1, 2, 3, 4, 5"));
343                 }
344
345                 [Test]
346                 public void ConvertFromInvariantString_InvalidNumber ()
347                 {
348                         try {
349                                 colconv.ConvertFromInvariantString ("hello");
350                                 Assert.Fail ("#1");
351                         } catch (Exception ex) {
352                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
353                                 Assert.IsNotNull (ex.InnerException, "#3");
354                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
355                         }
356                 }
357
358                 [Test]
359                 public void ConvertFromString_string ()
360                 {
361                         Assert.AreEqual (col, colconv.ConvertFromString (colStr), "CFSS#1");
362                         Assert.AreEqual (colnamed, colconv.ConvertFromString (colnamedStr), "CFSS#2");
363                 }
364
365                 [Test]
366                 public void ConvertFromString_InvalidComponentCount ()
367                 {
368                         CultureInfo culture = CultureInfo.CurrentCulture;
369                         Assert.Throws<ArgumentException> (() => colconv.ConvertFromString (string.Format (culture,
370                                 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator[0])));
371                 }
372
373                 [Test]
374                 public void ConvertFromString_InvalidNumber ()
375                 {
376                         try {
377                                 colconv.ConvertFromString ("hello");
378                                 Assert.Fail ("#1");
379                         } catch (Exception ex) {
380                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
381                                 Assert.IsNotNull (ex.InnerException, "#3");
382                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
383                         }
384                 }
385
386                 [Test]
387                 public void ConvertToInvariantString_string () {
388                         Assert.AreEqual (colStrInvariant, colconv.ConvertToInvariantString (col), "CFISS#1");
389                         Assert.AreEqual (colnamedStr, colconv.ConvertToInvariantString (colnamed), "CFISS#2");
390                 }
391
392                 [Test]
393                 public void ConvertToString_string () {
394                         Assert.AreEqual (colStr, colconv.ConvertToString (col), "CFISS#1");
395                         Assert.AreEqual (colnamedStr, colconv.ConvertToString (colnamed), "CFISS#3");
396                 }
397
398                 [Test]
399                 public void GetStandardValuesSupported () {
400                         Assert.IsTrue (colconv.GetStandardValuesSupported ());
401                 }
402
403                 [Test]
404                 public void GetStandardValues () {
405                         Assert.AreEqual ((int)KnownColor.MenuHighlight, colconv.GetStandardValues ().Count);
406                         Assert.AreEqual ((int)KnownColor.MenuHighlight, colconv.GetStandardValues (null).Count);                        
407                 }
408
409                 [Test]
410                 public void GetStandardValuesExclusive () {
411                         Assert.AreEqual (false, colconv.GetStandardValuesExclusive ());
412                 }
413
414                 [Test]
415                 public void ConvertFromString_FromHtml_PoundTooLarge ()
416                 {
417                         Assert.Throws<Exception> (() => colconv.ConvertFromString ("#100000000"));
418                 }
419         }
420 }
421