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