svn path=/branches/mono-1-1-9/mcs/; revision=51207
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestSizeConverter.cs
1 //
2 // Tests for System.Drawing.SizeConverter.cs 
3 //
4 // Author:
5 //      Ravindra (rkumar@novell.com)
6 //
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.ComponentModel.Design.Serialization;
35 using System.Drawing;
36 using System.Globalization;
37 using System.Security.Permissions;
38 using System.Threading;
39
40 using NUnit.Framework;
41
42 namespace MonoTests.System.Drawing
43 {
44         [TestFixture]
45         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
46         public class SizeConverterTest
47         {
48                 Size sz;
49                 Size szneg;
50                 SizeConverter szconv;
51                 String szStrInvariant;
52                 String sznegStrInvariant;
53
54                 [SetUp]
55                 public void SetUp ()
56                 {
57                         sz = new Size (10, 20);
58                         szStrInvariant = sz.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + sz.Height;
59
60                         szneg = new Size (-20, -30);
61                         sznegStrInvariant = szneg.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + szneg.Height;
62
63                         szconv = (SizeConverter) TypeDescriptor.GetConverter (sz);
64                 }
65
66                 [Test]
67                 public void TestCanConvertFrom ()
68                 {
69                         Assert.IsTrue (szconv.CanConvertFrom (typeof (String)), "CCF#1");
70                         Assert.IsTrue (szconv.CanConvertFrom (null, typeof (String)), "CCF#2");
71                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
72                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#4");
73                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Point)), "CCF#5");
74                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (PointF)), "CCF#6");
75                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Size)), "CCF#7");
76                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (SizeF)), "CCF#8");
77                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Object)), "CCF#9");
78                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (int)), "CCF#10");
79                         Assert.IsTrue (szconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#11");
80                 }
81
82                 [Test]
83                 public void TestCanConvertTo ()
84                 {
85                         Assert.IsTrue (szconv.CanConvertTo (typeof (String)), "CCT#1");
86                         Assert.IsTrue (szconv.CanConvertTo (null, typeof (String)), "CCT#2");
87                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
88                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (RectangleF)), "CCT#4");
89                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (Point)), "CCT#5");
90                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (PointF)), "CCT#6");
91                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (Size)), "CCT#7");
92                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (SizeF)), "CCT#8");
93                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (Object)), "CCT#9");
94                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (int)), "CCT#10");
95                 }
96
97                 [Test]
98                 public void TestConvertFrom ()
99                 {
100                         Assert.AreEqual (sz, (Size) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
101                                 "10, 20"), "CF#1");
102                         Assert.AreEqual (szneg, (Size) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
103                                 "-20, -30"), "CF#2");
104
105                         try {
106                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10");
107                                 Assert.Fail ("CF#3: must throw ArgumentException");
108                         } catch (Exception e) {
109                                 Assert.IsTrue (e is ArgumentException, "CF#3");
110                         }
111
112                         try {
113                                 szconv.ConvertFrom ("10");
114                                 Assert.Fail ("CF#3a: must throw ArgumentException");
115                         } catch (Exception e) {
116                                 Assert.IsTrue (e is ArgumentException, "CF#3a");
117                         }
118
119                         try {
120                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
121                                         "1, 1, 1");
122                                 Assert.Fail ("CF#4: must throw ArgumentException");
123                         } catch (Exception e) {
124                                 Assert.IsTrue (e is ArgumentException, "CF#4");
125                         }
126
127                         try {
128                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
129                                         "*1, 1");
130                                 Assert.Fail ("CF#5-1: must throw Exception");
131                         } catch (Exception ex) {
132                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "CF#5-2");
133                                 Assert.IsNotNull (ex.InnerException, "CF#5-3");
134                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "CF#5-4");
135                         }
136
137                         try {
138                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
139                                         new Point (10, 10));
140                                 Assert.Fail ("CF#6: must throw NotSupportedException");
141                         } catch (Exception e) {
142                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
143                         }
144
145                         try {
146                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
147                                         new PointF (10, 10));
148                                 Assert.Fail ("CF#7: must throw NotSupportedException");
149                         } catch (Exception e) {
150                                 Assert.IsTrue (e is NotSupportedException, "CF#7");
151                         }
152
153                         try {
154                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
155                                         new Size (10, 10));
156                                 Assert.Fail ("CF#8: must throw NotSupportedException");
157                         } catch (Exception e) {
158                                 Assert.IsTrue (e is NotSupportedException, "CF#8");
159                         }
160
161                         try {
162                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
163                                         new SizeF (10, 10));
164                                 Assert.Fail ("CF#9: must throw NotSupportedException");
165                         } catch (Exception e) {
166                                 Assert.IsTrue (e is NotSupportedException, "CF#9");
167                         }
168
169                         try {
170                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
171                                 Assert.Fail ("CF#10: must throw NotSupportedException");
172                         } catch (Exception e) {
173                                 Assert.IsTrue (e is NotSupportedException, "CF#10");
174                         }
175                 }
176
177                 [Test]
178                 public void TestConvertTo ()
179                 {
180                         Assert.AreEqual (szStrInvariant, (String) szconv.ConvertTo (null,
181                                 CultureInfo.InvariantCulture, sz, typeof (String)), "CT#1");
182                         Assert.AreEqual (sznegStrInvariant, (String) szconv.ConvertTo (null,
183                                 CultureInfo.InvariantCulture, szneg, typeof (String)), "CT#2");
184
185                         try {
186                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
187                                         typeof (Size));
188                                 Assert.Fail ("CT#3: must throw NotSupportedException");
189                         } catch (Exception e) {
190                                 Assert.IsTrue (e is NotSupportedException, "CT#3");
191                         }
192
193                         try {
194                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
195                                         typeof (SizeF));
196                                 Assert.Fail ("CT#4: must throw NotSupportedException");
197                         } catch (Exception e) {
198                                 Assert.IsTrue (e is NotSupportedException, "CT#4");
199                         }
200
201                         try {
202                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
203                                         typeof (Point));
204                                 Assert.Fail ("CT#5: must throw NotSupportedException");
205                         } catch (Exception e) {
206                                 Assert.IsTrue (e is NotSupportedException, "CT#5");
207                         }
208
209                         try {
210                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
211                                         typeof (PointF));
212                                 Assert.Fail ("CT#6: must throw NotSupportedException");
213                         } catch (Exception e) {
214                                 Assert.IsTrue (e is NotSupportedException, "CT#6");
215                         }
216
217                         try {
218                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
219                                         typeof (int));
220                                 Assert.Fail ("CT#7: must throw NotSupportedException");
221                         } catch (Exception e) {
222                                 Assert.IsTrue (e is NotSupportedException, "CT#7");
223                         }
224                 }
225
226                 [Test]
227                 public void TestGetCreateInstanceSupported ()
228                 {
229                         Assert.IsTrue (szconv.GetCreateInstanceSupported (), "GCIS#1");
230                         Assert.IsTrue (szconv.GetCreateInstanceSupported (null), "GCIS#2");
231                 }
232
233                 [Test]
234                 public void TestCreateInstance ()
235                 {
236                         Size szInstance;
237
238                         Hashtable ht = new Hashtable ();
239                         ht.Add ("Width", 10); ht.Add ("Height", 20);
240
241                         szInstance = (Size) szconv.CreateInstance (ht);
242                         Assert.AreEqual (sz, szInstance, "CI#1");
243
244                         ht.Clear ();
245                         ht.Add ("Width", -20); ht.Add ("Height", -30);
246
247                         szInstance = (Size) szconv.CreateInstance (null, ht);
248                         Assert.AreEqual (szneg, szInstance, "CI#2");
249
250                         // Property names are case-sensitive. It should throw 
251                         // NullRefExc if any of the property names does not match
252                         ht.Clear ();
253                         ht.Add ("width", 20); ht.Add ("Height", 30);
254                         try {
255                                 szInstance = (Size) szconv.CreateInstance (null, ht);
256                                 Assert.Fail ("CI#3: must throw NullReferenceException");
257                         } catch (Exception e) {
258                                 Assert.IsTrue (e is NullReferenceException, "CI#3");
259                         }
260                 }
261
262                 [Test]
263                 public void TestGetPropertiesSupported ()
264                 {
265                         Assert.IsTrue (szconv.GetPropertiesSupported (), "GPS#1");
266                         Assert.IsTrue (szconv.GetPropertiesSupported (null), "GPS#2");
267                 }
268
269                 [Test]
270                 public void TestGetProperties ()
271                 {
272                         Attribute [] attrs;
273                         PropertyDescriptorCollection propsColl;
274
275                         propsColl = szconv.GetProperties (sz);
276                         Assert.AreEqual (2, propsColl.Count, "GP1#1");
277                         Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP1#2");
278                         Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP1#3");
279
280                         propsColl = szconv.GetProperties (null, szneg);
281                         Assert.AreEqual (2, propsColl.Count, "GP2#1");
282                         Assert.AreEqual (szneg.Width, propsColl["Width"].GetValue (szneg), "GP2#2");
283                         Assert.AreEqual (szneg.Height, propsColl["Height"].GetValue (szneg), "GP2#3");
284
285                         propsColl = szconv.GetProperties (null, sz, null);
286                         Assert.AreEqual (3, propsColl.Count, "GP3#1");
287                         Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP3#2");
288                         Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP3#3");
289                         Assert.AreEqual (sz.IsEmpty, propsColl["IsEmpty"].GetValue (sz), "GP3#4");
290
291                         Type type = typeof (Size);
292                         attrs = Attribute.GetCustomAttributes (type, true);
293                         propsColl = szconv.GetProperties (null, sz, attrs);
294                         Assert.AreEqual (0, propsColl.Count, "GP3#5");
295                 }
296
297                 [Test]
298                 public void ConvertFromInvariantString_string ()
299                 {
300                         Assert.AreEqual (sz, szconv.ConvertFromInvariantString (szStrInvariant),
301                                 "CFISS#1");
302                         Assert.AreEqual (szneg, szconv.ConvertFromInvariantString (sznegStrInvariant),
303                                 "CFISS#2");
304                 }
305
306                 [Test]
307                 [ExpectedException (typeof (ArgumentException))]
308                 public void ConvertFromInvariantString_string_exc_1 ()
309                 {
310                         szconv.ConvertFromInvariantString ("1, 2, 3");
311                 }
312
313                 [Test]
314                 public void ConvertFromInvariantString_string_exc_2 ()
315                 {
316                         try {
317                                 szconv.ConvertFromInvariantString ("hello");
318                                 Assert.Fail ("#1");
319                         } catch (Exception ex) {
320                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
321                                 Assert.IsNotNull (ex.InnerException, "#3");
322                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
323                         }
324                 }
325
326                 [Test]
327                 public void ConvertFromString_string ()
328                 {
329                         // save current culture
330                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
331
332                         try {
333                                 PerformConvertFromStringTest (new CultureInfo ("en-US"));
334                                 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
335                                 PerformConvertFromStringTest (new MyCultureInfo ());
336                         } finally {
337                                 // restore original culture
338                                 Thread.CurrentThread.CurrentCulture = currentCulture;
339                         }
340                 }
341
342                 [Test]
343                 [ExpectedException (typeof (ArgumentException))]
344                 public void ConvertFromString_string_exc_1 ()
345                 {
346                         CultureInfo culture = CultureInfo.CurrentCulture;
347                         szconv.ConvertFromString (string.Format(culture,
348                                 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator));
349                 }
350
351                 [Test]
352                 public void ConvertFromString_string_exc_2 ()
353                 {
354                         try {
355                                 szconv.ConvertFromString ("hello");
356                                 Assert.Fail ("#1");
357                         } catch (Exception ex) {
358                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
359                                 Assert.IsNotNull (ex.InnerException, "#3");
360                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
361                         }
362                 }
363
364                 [Test]
365                 public void ConvertToInvariantString_string ()
366                 {
367                         Assert.AreEqual (szStrInvariant, szconv.ConvertToInvariantString (sz),
368                                 "CFISS#1");
369                         Assert.AreEqual (sznegStrInvariant, szconv.ConvertToInvariantString (szneg),
370                                 "CFISS#2");
371                 }
372
373                 [Test]
374                 public void ConvertToString_string ()
375                 {
376                         // save current culture
377                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
378
379                         try {
380                                 PerformConvertToStringTest (new CultureInfo ("en-US"));
381                                 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
382                                 PerformConvertToStringTest (new MyCultureInfo ());
383                         } finally {
384                                 // restore original culture
385                                 Thread.CurrentThread.CurrentCulture = currentCulture;
386                         }
387                 }
388
389                 [Test]
390                 public void GetStandardValuesSupported ()
391                 {
392                         Assert.IsFalse (szconv.GetStandardValuesSupported ());
393                 }
394
395                 [Test]
396                 public void GetStandardValues ()
397                 {
398                         Assert.IsNull (szconv.GetStandardValues ());
399                 }
400
401                 [Test]
402                 public void GetStandardValuesExclusive ()
403                 {
404                         Assert.IsFalse (szconv.GetStandardValuesExclusive ());
405                 }
406
407                 private void PerformConvertFromStringTest (CultureInfo culture)
408                 {
409                         // set current culture
410                         Thread.CurrentThread.CurrentCulture = culture;
411
412                         // perform tests
413                         Assert.AreEqual (sz, szconv.ConvertFromString (CreateSizeString (culture, sz)),
414                                 "CFSS#1-" + culture.Name);
415                         Assert.AreEqual (szneg, szconv.ConvertFromString (CreateSizeString (culture, szneg)),
416                                 "CFSS#2-" + culture.Name);
417                 }
418
419                 private void PerformConvertToStringTest (CultureInfo culture)
420                 {
421                         // set current culture
422                         Thread.CurrentThread.CurrentCulture = culture;
423
424                         // perform tests
425                         Assert.AreEqual (CreateSizeString (culture, sz), szconv.ConvertToString (sz),
426                                 "CFISS#1-" + culture.Name);
427                         Assert.AreEqual (CreateSizeString (culture, szneg), szconv.ConvertToString (szneg),
428                                 "CFISS#2-" + culture.Name);
429                 }
430
431                 private static string CreateSizeString (Size size)
432                 {
433                         return CreateSizeString (CultureInfo.CurrentCulture, size);
434                 }
435
436                 private static string CreateSizeString (CultureInfo culture, Size size)
437                 {
438                         return string.Format ("{0}{1} {2}", size.Width.ToString (culture),
439                                 culture.TextInfo.ListSeparator, size.Height.ToString (culture));
440                 }
441
442                 [Serializable]
443                 private sealed class MyCultureInfo : CultureInfo
444                 {
445                         internal MyCultureInfo () : base ("en-US")
446                         {
447                         }
448
449                         public override object GetFormat (Type formatType)
450                         {
451                                 if (formatType == typeof (NumberFormatInfo)) {
452                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
453
454                                         nfi.NegativeSign = "myNegativeSign";
455                                         return NumberFormatInfo.ReadOnly (nfi);
456                                 } else {
457                                         return base.GetFormat (formatType);
458                                 }
459                         }
460                 }
461         }
462 }