merge -r 60814:60815
[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 #if TARGET_JVM
68                 [NUnit.Framework.Category ("NotWorking")]
69 #endif
70                 public void TestCanConvertFrom ()
71                 {
72                         Assert.IsTrue (szconv.CanConvertFrom (typeof (String)), "CCF#1");
73                         Assert.IsTrue (szconv.CanConvertFrom (null, typeof (String)), "CCF#2");
74                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
75                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#4");
76                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Point)), "CCF#5");
77                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (PointF)), "CCF#6");
78                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Size)), "CCF#7");
79                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (SizeF)), "CCF#8");
80                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Object)), "CCF#9");
81                         Assert.IsFalse (szconv.CanConvertFrom (null, typeof (int)), "CCF#10");
82                         Assert.IsTrue (szconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#11");
83                 }
84
85                 [Test]
86                 public void TestCanConvertTo ()
87                 {
88                         Assert.IsTrue (szconv.CanConvertTo (typeof (String)), "CCT#1");
89                         Assert.IsTrue (szconv.CanConvertTo (null, typeof (String)), "CCT#2");
90                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
91                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (RectangleF)), "CCT#4");
92                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (Point)), "CCT#5");
93                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (PointF)), "CCT#6");
94                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (Size)), "CCT#7");
95                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (SizeF)), "CCT#8");
96                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (Object)), "CCT#9");
97                         Assert.IsFalse (szconv.CanConvertTo (null, typeof (int)), "CCT#10");
98                 }
99
100                 [Test]
101 #if TARGET_JVM
102                 [NUnit.Framework.Category ("NotWorking")]
103 #endif
104                 public void TestConvertFrom ()
105                 {
106                         Assert.AreEqual (sz, (Size) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
107                                 "10, 20"), "CF#1");
108                         Assert.AreEqual (szneg, (Size) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
109                                 "-20, -30"), "CF#2");
110
111                         try {
112                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10");
113                                 Assert.Fail ("CF#3: must throw ArgumentException");
114                         } catch (Exception e) {
115                                 Assert.IsTrue (e is ArgumentException, "CF#3");
116                         }
117
118                         try {
119                                 szconv.ConvertFrom ("10");
120                                 Assert.Fail ("CF#3a: must throw ArgumentException");
121                         } catch (Exception e) {
122                                 Assert.IsTrue (e is ArgumentException, "CF#3a");
123                         }
124
125                         try {
126                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
127                                         "1, 1, 1");
128                                 Assert.Fail ("CF#4: must throw ArgumentException");
129                         } catch (Exception e) {
130                                 Assert.IsTrue (e is ArgumentException, "CF#4");
131                         }
132
133                         try {
134                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
135                                         "*1, 1");
136                                 Assert.Fail ("CF#5-1: must throw Exception");
137                         } catch (Exception ex) {
138                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "CF#5-2");
139                                 Assert.IsNotNull (ex.InnerException, "CF#5-3");
140                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "CF#5-4");
141                         }
142
143                         try {
144                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
145                                         new Point (10, 10));
146                                 Assert.Fail ("CF#6: must throw NotSupportedException");
147                         } catch (Exception e) {
148                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
149                         }
150
151                         try {
152                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
153                                         new PointF (10, 10));
154                                 Assert.Fail ("CF#7: must throw NotSupportedException");
155                         } catch (Exception e) {
156                                 Assert.IsTrue (e is NotSupportedException, "CF#7");
157                         }
158
159                         try {
160                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
161                                         new Size (10, 10));
162                                 Assert.Fail ("CF#8: must throw NotSupportedException");
163                         } catch (Exception e) {
164                                 Assert.IsTrue (e is NotSupportedException, "CF#8");
165                         }
166
167                         try {
168                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
169                                         new SizeF (10, 10));
170                                 Assert.Fail ("CF#9: must throw NotSupportedException");
171                         } catch (Exception e) {
172                                 Assert.IsTrue (e is NotSupportedException, "CF#9");
173                         }
174
175                         try {
176                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
177                                 Assert.Fail ("CF#10: must throw NotSupportedException");
178                         } catch (Exception e) {
179                                 Assert.IsTrue (e is NotSupportedException, "CF#10");
180                         }
181                 }
182
183                 [Test]
184                 public void TestConvertTo ()
185                 {
186                         Assert.AreEqual (szStrInvariant, (String) szconv.ConvertTo (null,
187                                 CultureInfo.InvariantCulture, sz, typeof (String)), "CT#1");
188                         Assert.AreEqual (sznegStrInvariant, (String) szconv.ConvertTo (null,
189                                 CultureInfo.InvariantCulture, szneg, typeof (String)), "CT#2");
190
191                         try {
192                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
193                                         typeof (Size));
194                                 Assert.Fail ("CT#3: must throw NotSupportedException");
195                         } catch (Exception e) {
196                                 Assert.IsTrue (e is NotSupportedException, "CT#3");
197                         }
198
199                         try {
200                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
201                                         typeof (SizeF));
202                                 Assert.Fail ("CT#4: must throw NotSupportedException");
203                         } catch (Exception e) {
204                                 Assert.IsTrue (e is NotSupportedException, "CT#4");
205                         }
206
207                         try {
208                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
209                                         typeof (Point));
210                                 Assert.Fail ("CT#5: must throw NotSupportedException");
211                         } catch (Exception e) {
212                                 Assert.IsTrue (e is NotSupportedException, "CT#5");
213                         }
214
215                         try {
216                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
217                                         typeof (PointF));
218                                 Assert.Fail ("CT#6: must throw NotSupportedException");
219                         } catch (Exception e) {
220                                 Assert.IsTrue (e is NotSupportedException, "CT#6");
221                         }
222
223                         try {
224                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
225                                         typeof (int));
226                                 Assert.Fail ("CT#7: must throw NotSupportedException");
227                         } catch (Exception e) {
228                                 Assert.IsTrue (e is NotSupportedException, "CT#7");
229                         }
230                 }
231
232                 [Test]
233                 public void TestGetCreateInstanceSupported ()
234                 {
235                         Assert.IsTrue (szconv.GetCreateInstanceSupported (), "GCIS#1");
236                         Assert.IsTrue (szconv.GetCreateInstanceSupported (null), "GCIS#2");
237                 }
238
239                 [Test]
240                 public void TestCreateInstance ()
241                 {
242                         Size szInstance;
243
244                         Hashtable ht = new Hashtable ();
245                         ht.Add ("Width", 10); ht.Add ("Height", 20);
246
247                         szInstance = (Size) szconv.CreateInstance (ht);
248                         Assert.AreEqual (sz, szInstance, "CI#1");
249
250                         ht.Clear ();
251                         ht.Add ("Width", -20); ht.Add ("Height", -30);
252
253                         szInstance = (Size) szconv.CreateInstance (null, ht);
254                         Assert.AreEqual (szneg, szInstance, "CI#2");
255                 }
256
257                 [Test]
258 #if NET_2_0
259                 [ExpectedException (typeof (ArgumentException))]
260 #else
261                 [ExpectedException (typeof (NullReferenceException))]
262 #endif
263                 public void TestCreateInstance_CaseSensitive ()
264                 {
265                         Hashtable ht = new Hashtable ();
266                         ht.Add ("width", 20);
267                         ht.Add ("Height", 30);
268                         szconv.CreateInstance (null, ht);
269                 }
270
271                 [Test]
272                 public void TestGetPropertiesSupported ()
273                 {
274                         Assert.IsTrue (szconv.GetPropertiesSupported (), "GPS#1");
275                         Assert.IsTrue (szconv.GetPropertiesSupported (null), "GPS#2");
276                 }
277
278                 [Test]
279 #if TARGET_JVM
280                 [NUnit.Framework.Category ("NotWorking")]
281 #endif
282                 public void TestGetProperties ()
283                 {
284                         Attribute [] attrs;
285                         PropertyDescriptorCollection propsColl;
286
287                         propsColl = szconv.GetProperties (sz);
288                         Assert.AreEqual (2, propsColl.Count, "GP1#1");
289                         Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP1#2");
290                         Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP1#3");
291
292                         propsColl = szconv.GetProperties (null, szneg);
293                         Assert.AreEqual (2, propsColl.Count, "GP2#1");
294                         Assert.AreEqual (szneg.Width, propsColl["Width"].GetValue (szneg), "GP2#2");
295                         Assert.AreEqual (szneg.Height, propsColl["Height"].GetValue (szneg), "GP2#3");
296
297                         propsColl = szconv.GetProperties (null, sz, null);
298                         Assert.AreEqual (3, propsColl.Count, "GP3#1");
299                         Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP3#2");
300                         Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP3#3");
301                         Assert.AreEqual (sz.IsEmpty, propsColl["IsEmpty"].GetValue (sz), "GP3#4");
302
303                         Type type = typeof (Size);
304                         attrs = Attribute.GetCustomAttributes (type, true);
305                         propsColl = szconv.GetProperties (null, sz, attrs);
306                         Assert.AreEqual (0, propsColl.Count, "GP3#5");
307                 }
308
309                 [Test]
310                 public void ConvertFromInvariantString_string ()
311                 {
312                         Assert.AreEqual (sz, szconv.ConvertFromInvariantString (szStrInvariant),
313                                 "CFISS#1");
314                         Assert.AreEqual (szneg, szconv.ConvertFromInvariantString (sznegStrInvariant),
315                                 "CFISS#2");
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (ArgumentException))]
320                 public void ConvertFromInvariantString_string_exc_1 ()
321                 {
322                         szconv.ConvertFromInvariantString ("1, 2, 3");
323                 }
324
325                 [Test]
326 #if TARGET_JVM
327                 [NUnit.Framework.Category ("NotWorking")]
328 #endif
329                 public void ConvertFromInvariantString_string_exc_2 ()
330                 {
331                         try {
332                                 szconv.ConvertFromInvariantString ("hello");
333                                 Assert.Fail ("#1");
334                         } catch (Exception ex) {
335                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
336                                 Assert.IsNotNull (ex.InnerException, "#3");
337                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
338                         }
339                 }
340
341                 [Test]
342 #if TARGET_JVM
343                 [NUnit.Framework.Category ("NotWorking")]
344 #endif
345                 public void ConvertFromString_string ()
346                 {
347                         // save current culture
348                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
349
350                         try {
351                                 PerformConvertFromStringTest (new CultureInfo ("en-US"));
352                                 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
353                                 PerformConvertFromStringTest (new MyCultureInfo ());
354                         } finally {
355                                 // restore original culture
356                                 Thread.CurrentThread.CurrentCulture = currentCulture;
357                         }
358                 }
359
360                 [Test]
361                 [ExpectedException (typeof (ArgumentException))]
362                 public void ConvertFromString_string_exc_1 ()
363                 {
364                         CultureInfo culture = CultureInfo.CurrentCulture;
365                         szconv.ConvertFromString (string.Format(culture,
366                                 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator));
367                 }
368
369                 [Test]
370 #if TARGET_JVM
371                 [NUnit.Framework.Category ("NotWorking")]
372 #endif
373                 public void ConvertFromString_string_exc_2 ()
374                 {
375                         try {
376                                 szconv.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                 {
388                         Assert.AreEqual (szStrInvariant, szconv.ConvertToInvariantString (sz),
389                                 "CFISS#1");
390                         Assert.AreEqual (sznegStrInvariant, szconv.ConvertToInvariantString (szneg),
391                                 "CFISS#2");
392                 }
393
394                 [Test]
395                 public void ConvertToString_string ()
396                 {
397                         // save current culture
398                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
399
400                         try {
401                                 PerformConvertToStringTest (new CultureInfo ("en-US"));
402                                 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
403                                 PerformConvertToStringTest (new MyCultureInfo ());
404                         } finally {
405                                 // restore original culture
406                                 Thread.CurrentThread.CurrentCulture = currentCulture;
407                         }
408                 }
409
410                 [Test]
411                 public void GetStandardValuesSupported ()
412                 {
413                         Assert.IsFalse (szconv.GetStandardValuesSupported ());
414                 }
415
416                 [Test]
417                 public void GetStandardValues ()
418                 {
419                         Assert.IsNull (szconv.GetStandardValues ());
420                 }
421
422                 [Test]
423                 public void GetStandardValuesExclusive ()
424                 {
425                         Assert.IsFalse (szconv.GetStandardValuesExclusive ());
426                 }
427
428                 private void PerformConvertFromStringTest (CultureInfo culture)
429                 {
430                         // set current culture
431                         Thread.CurrentThread.CurrentCulture = culture;
432
433                         // perform tests
434                         Assert.AreEqual (sz, szconv.ConvertFromString (CreateSizeString (culture, sz)),
435                                 "CFSS#1-" + culture.Name);
436                         Assert.AreEqual (szneg, szconv.ConvertFromString (CreateSizeString (culture, szneg)),
437                                 "CFSS#2-" + culture.Name);
438                 }
439
440                 private void PerformConvertToStringTest (CultureInfo culture)
441                 {
442                         // set current culture
443                         Thread.CurrentThread.CurrentCulture = culture;
444
445                         // perform tests
446                         Assert.AreEqual (CreateSizeString (culture, sz), szconv.ConvertToString (sz),
447                                 "CFISS#1-" + culture.Name);
448                         Assert.AreEqual (CreateSizeString (culture, szneg), szconv.ConvertToString (szneg),
449                                 "CFISS#2-" + culture.Name);
450                 }
451
452                 private static string CreateSizeString (Size size)
453                 {
454                         return CreateSizeString (CultureInfo.CurrentCulture, size);
455                 }
456
457                 private static string CreateSizeString (CultureInfo culture, Size size)
458                 {
459                         return string.Format ("{0}{1} {2}", size.Width.ToString (culture),
460                                 culture.TextInfo.ListSeparator, size.Height.ToString (culture));
461                 }
462
463                 [Serializable]
464                 private sealed class MyCultureInfo : CultureInfo
465                 {
466                         internal MyCultureInfo () : base ("en-US")
467                         {
468                         }
469
470                         public override object GetFormat (Type formatType)
471                         {
472                                 if (formatType == typeof (NumberFormatInfo)) {
473                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
474
475                                         nfi.NegativeSign = "myNegativeSign";
476                                         return NumberFormatInfo.ReadOnly (nfi);
477                                 } else {
478                                         return base.GetFormat (formatType);
479                                 }
480                         }
481                 }
482         }
483 }