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