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