* PEAPI.cs: Clean up indentation. Split into ...
[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                         Assert.AreEqual (col, (Color) colconv.ConvertFrom (null,
119                                 CultureInfo.InvariantCulture, colStrInvariant), "CF#1");
120                         Assert.AreEqual (colnamed, (Color) colconv.ConvertFrom (null,
121                                 CultureInfo.InvariantCulture, colnamedStr), "CF#2");
122
123                         Assert.AreEqual (Color.Empty, colconv.ConvertFrom (string.Empty), "CF#3");
124                         Assert.AreEqual (Color.Empty, colconv.ConvertFrom (" "), "CF#4");
125                         Assert.AreEqual (Color.Red, colconv.ConvertFrom ("Red"), "CF#5");
126                         Assert.AreEqual (Color.Red, colconv.ConvertFrom (" Red "), "CF#6");
127
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof (ArgumentException))]
132                 public void ConvertFrom_x1 ()
133                 {
134                                 colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10, 20");
135                 }
136
137                 [Test]
138                 [ExpectedException (typeof (ArgumentException))]
139                 public void ConvertFrom_x2 ()
140                 {
141                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture, "-10, 20, 30");
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (ArgumentException))]
146                 public void ConvertFrom_x3 ()
147                 {
148                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
149                                         "1, 1, 1, 1, 1");
150                 }
151
152                 [Test]
153                 [ExpectedException (typeof (Exception))]
154                 public void ConvertFrom_x4 ()
155                 {
156                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
157                                 "*1, 1");
158                 }
159
160                 [Test]
161                 [ExpectedException (typeof (NotSupportedException))]
162                 public void ConvertFrom_x5 ()
163                 {
164                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
165                                         new Point (10, 10));
166                 }
167
168                 [Test]
169                 [ExpectedException (typeof (NotSupportedException))]
170                 public void ConvertFrom_x6 ()
171                 {
172                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
173                                         new PointF (10, 10));
174                 }
175
176                 [Test]
177                 [ExpectedException (typeof (NotSupportedException))]
178                 public void ConvertFrom_x7 ()
179                 {
180                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
181                                         new Size (10, 10));
182                 }
183
184                 [Test]
185                 [ExpectedException (typeof (NotSupportedException))]
186                 public void ConvertFrom_x8 ()
187                 {
188                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture,
189                                         new SizeF (10, 10));
190                 }
191
192                 [Test]
193                 [ExpectedException (typeof (NotSupportedException))]
194                 public void ConvertFrom_x9 ()
195                 {
196                         colconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
197                 }
198
199                 [Test]
200                 public void ConvertTo ()
201                 {
202                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
203                                 Color.FromArgb (10, 20, 30), typeof (String)), "CT#1");
204                         Assert.AreEqual (colStrInvariant, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
205                                 Color.FromArgb (255, 10, 20, 30), typeof (String)), "CT#2");
206                         Assert.AreEqual ("10, 20, 30, 40", colconv.ConvertTo (null, CultureInfo.InvariantCulture,
207                                 Color.FromArgb (10, 20, 30, 40), typeof (String)), "CT#3");
208                         Assert.AreEqual (colnamedStr, colconv.ConvertTo (null, CultureInfo.InvariantCulture,
209                                 colnamed, typeof (String)), "CT#4");
210
211                         Assert.AreEqual (string.Empty, colconv.ConvertTo (Color.Empty, typeof (string)), "CT#5");
212                         Assert.AreEqual ("Red", colconv.ConvertTo (Color.Red, typeof (string)), "CT#6");
213                         Assert.AreEqual (string.Empty, colconv.ConvertTo (null, typeof (string)), "CT#7");
214                         Assert.AreEqual ("test", colconv.ConvertTo ("test", typeof (string)), "CT#8");
215                 }
216
217                 [Test]
218                 [ExpectedException (typeof (NotSupportedException))]
219                 public void ConvertTo_x1 ()
220                 {
221                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
222                                         typeof (Color));
223                 }
224
225                 [Test]
226                 [ExpectedException (typeof (NotSupportedException))]
227                 public void ConvertTo_x2 ()
228                 {
229                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
230                                         typeof (SizeF));
231                 }
232
233                 [Test]
234                 [ExpectedException (typeof (NotSupportedException))]
235                 public void ConvertTo_x3 ()
236                 {
237                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
238                                         typeof (Point));
239                 }
240
241                 [Test]
242                 [ExpectedException (typeof (NotSupportedException))]
243                 public void ConvertTo_x4 ()
244                 {
245                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
246                                         typeof (PointF));
247                 }
248
249                 [Test]
250                 [ExpectedException (typeof (NotSupportedException))]
251                 public void ConvertTo_x5 ()
252                 {
253                         colconv.ConvertTo (null, CultureInfo.InvariantCulture, col,
254                                         typeof (int));
255                 }
256
257                 [Test]
258                 public void GetCreateInstanceSupported ()
259                 {
260                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (), "GCIS#1");
261                         Assert.IsTrue (!colconv.GetCreateInstanceSupported (null), "GCIS#2");
262                 }
263
264                 [Test]
265                 public void CreateInstance ()
266                 {
267                         Hashtable ht = new Hashtable ();
268                         ht.Add ("R", 10); ht.Add ("G", 20); ht.Add ("B", 30);
269
270                         Assert.AreEqual (null, colconv.CreateInstance (ht), "CI#1");
271
272                         ht.Add ("Name", "ForestGreen");
273
274                         Assert.AreEqual (null, colconv.CreateInstance (null, ht), "CI#2");
275                 }
276
277                 [Test]
278                 public void GetPropertiesSupported ()
279                 {
280                         Assert.IsTrue (!colconv.GetPropertiesSupported (), "GPS#1");
281                         Assert.IsTrue (!colconv.GetPropertiesSupported (null), "GPS#2");
282                 }
283
284                 [Test]
285                 public void GetProperties ()
286                 {
287                         Attribute [] attrs;
288
289                         Assert.AreEqual (null, colconv.GetProperties (col), "GP1#1");
290
291                         Assert.AreEqual (null, colconv.GetProperties (null, col, null), "GP2#1");
292
293                         attrs = Attribute.GetCustomAttributes (typeof (Color), true);
294                         Assert.AreEqual (null, colconv.GetProperties (null, col, attrs), "GP3#5");
295                 }
296
297                 [Test]
298                 public void ConvertFromInvariantString_string ()
299                 {
300                         Assert.AreEqual (col, colconv.ConvertFromInvariantString (colStrInvariant), "CFISS#1");
301                         Assert.AreEqual (colnamed, colconv.ConvertFromInvariantString (colnamedStr), "CFISS#2");
302                 }
303
304                 [Test]
305                 [ExpectedException (typeof (ArgumentException))]
306                 public void ConvertFromInvariantString_InvalidComponentCount ()
307                 {
308                         colconv.ConvertFromInvariantString ("1, 2, 3, 4, 5");
309                 }
310
311                 [Test]
312                 public void ConvertFromInvariantString_InvalidNumber ()
313                 {
314                         try {
315                                 colconv.ConvertFromInvariantString ("hello");
316                                 Assert.Fail ("#1");
317                         } catch (Exception ex) {
318                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
319                                 Assert.IsNotNull (ex.InnerException, "#3");
320                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
321                         }
322                 }
323
324                 [Test]
325                 public void ConvertFromString_string ()
326                 {
327                         Assert.AreEqual (col, colconv.ConvertFromString (colStr), "CFSS#1");
328                         Assert.AreEqual (colnamed, colconv.ConvertFromString (colnamedStr), "CFSS#2");
329                 }
330
331                 [Test]
332                 [ExpectedException (typeof (ArgumentException))]
333                 public void ConvertFromString_InvalidComponentCount ()
334                 {
335                         CultureInfo culture = CultureInfo.CurrentCulture;
336                         colconv.ConvertFromString (string.Format (culture,
337                                 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator[0]));
338                 }
339
340                 [Test]
341                 public void ConvertFromString_InvalidNumber ()
342                 {
343                         try {
344                                 colconv.ConvertFromString ("hello");
345                                 Assert.Fail ("#1");
346                         } catch (Exception ex) {
347                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
348                                 Assert.IsNotNull (ex.InnerException, "#3");
349                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
350                         }
351                 }
352
353                 [Test]
354                 public void ConvertToInvariantString_string () {
355                         Assert.AreEqual (colStrInvariant, colconv.ConvertToInvariantString (col), "CFISS#1");
356                         Assert.AreEqual (colnamedStr, colconv.ConvertToInvariantString (colnamed), "CFISS#2");
357                 }
358
359                 [Test]
360                 public void ConvertToString_string () {
361                         Assert.AreEqual (colStr, colconv.ConvertToString (col), "CFISS#1");
362                         Assert.AreEqual (colnamedStr, colconv.ConvertToString (colnamed), "CFISS#3");
363                 }
364
365                 [Test]
366                 public void GetStandardValuesSupported () {
367                         Assert.IsTrue (colconv.GetStandardValuesSupported ());
368                 }
369
370                 [Test]
371                 public void GetStandardValues () {
372                         Assert.AreEqual (167, colconv.GetStandardValues ().Count);
373                         Assert.AreEqual (167, colconv.GetStandardValues (null).Count);
374                 }
375
376                 [Test]
377                 public void GetStandardValuesExclusive () {
378                         Assert.AreEqual (false, colconv.GetStandardValuesExclusive ());
379                 }
380         }
381 }
382