[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestPointConverter.cs
1 //
2 // Tests for System.Drawing.PointConverter.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 // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Collections;
32 using System.ComponentModel;
33 using System.ComponentModel.Design.Serialization;
34 using System.Drawing;
35 using System.Globalization;
36 using System.Security.Permissions;
37 using System.Threading;
38
39 using NUnit.Framework;
40
41 namespace MonoTests.System.Drawing
42 {
43         [TestFixture]   
44         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
45         public class PointConverterTest
46         {
47                 Point pt;
48                 Point ptneg;
49                 PointConverter ptconv;
50                 String ptStr;
51                 String ptnegStr;
52
53                 [SetUp]
54                 public void SetUp ()
55                 {
56                         pt = new Point (1, 2);
57                         ptStr = pt.X + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + pt.Y;
58
59                         ptneg = new Point (-2, -3);
60                         ptnegStr = ptneg.X + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + ptneg.Y;
61
62                         ptconv = (PointConverter) TypeDescriptor.GetConverter (pt);
63                 }
64
65                 [Test]
66 #if TARGET_JVM
67                 [NUnit.Framework.Category ("NotWorking")]
68 #endif
69                 public void TestCanConvertFrom ()
70                 {
71                         Assert.IsTrue (ptconv.CanConvertFrom (typeof (String)), "CCF#1");
72                         Assert.IsTrue (ptconv.CanConvertFrom (null, typeof (String)), "CCF#2");
73                         Assert.IsFalse (ptconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
74                         Assert.IsFalse (ptconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#4");
75                         Assert.IsFalse (ptconv.CanConvertFrom (null, typeof (Point)), "CCF#5");
76                         Assert.IsFalse (ptconv.CanConvertFrom (null, typeof (PointF)), "CCF#6");
77                         Assert.IsFalse (ptconv.CanConvertFrom (null, typeof (Size)), "CCF#7");
78                         Assert.IsFalse (ptconv.CanConvertFrom (null, typeof (SizeF)), "CCF#8");
79                         Assert.IsFalse (ptconv.CanConvertFrom (null, typeof (Object)), "CCF#9");
80                         Assert.IsFalse (ptconv.CanConvertFrom (null, typeof (int)), "CCF#10");
81                         Assert.IsTrue (ptconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#11");
82                 }
83
84                 [Test]
85                 public void TestCanConvertTo ()
86                 {
87                         Assert.IsTrue (ptconv.CanConvertTo (typeof (String)), "CCT#1");
88                         Assert.IsTrue (ptconv.CanConvertTo (null, typeof (String)), "CCT#2");
89                         Assert.IsFalse (ptconv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
90                         Assert.IsFalse (ptconv.CanConvertTo (null, typeof (RectangleF)), "CCT#4");
91                         Assert.IsFalse (ptconv.CanConvertTo (null, typeof (Point)), "CCT#5");
92                         Assert.IsFalse (ptconv.CanConvertTo (null, typeof (PointF)), "CCT#6");
93                         Assert.IsFalse (ptconv.CanConvertTo (null, typeof (Size)), "CCT#7");
94                         Assert.IsFalse (ptconv.CanConvertTo (null, typeof (SizeF)), "CCT#8");
95                         Assert.IsFalse (ptconv.CanConvertTo (null, typeof (Object)), "CCT#9");
96                         Assert.IsFalse (ptconv.CanConvertTo (null, typeof (int)), "CCT#10");
97                 }
98
99                 [Test]
100 #if TARGET_JVM
101                 [NUnit.Framework.Category ("NotWorking")]
102 #endif
103                 public void TestConvertFrom ()
104                 {
105                         Assert.AreEqual (pt, (Point) ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
106                                                                 "1, 2"), "CF#1");
107                         Assert.AreEqual (ptneg, (Point) ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
108                                                                 "-2, -3"), "CF#2");
109
110                         try {
111                                 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture, "1");
112                                 Assert.Fail ("CF#3: must throw ArgumentException");
113                         } catch (Exception e) {
114                                 Assert.IsTrue (e is ArgumentException, "CF#3");
115                         }
116
117                         try {
118                                 ptconv.ConvertFrom ("1");
119                                 Assert.Fail ("CF#3a: must throw ArgumentException");
120                         } catch (Exception e) {
121                                 Assert.IsTrue (e is ArgumentException, "CF#3a");
122                         }
123
124                         try {
125                                 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture, "1, 1, 1");
126                                 Assert.Fail ("CF#4: must throw ArgumentException");
127                         } catch (Exception e) {
128                                 Assert.IsTrue (e is ArgumentException, "CF#4");
129                         }
130
131                         try {
132                                 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture, "*1, 1");
133                                 Assert.Fail ("CF#5-1");
134                         } catch (Exception ex) {
135                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "CF#5-2");
136                                 Assert.IsNotNull (ex.InnerException, "CF#5-3");
137                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "CF#5-4");
138                         }
139
140                         try {
141                                 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture, 
142                                         new Point (1, 1));
143                                 Assert.Fail ("CF#6: must throw NotSupportedException");
144                         } catch (Exception e) {
145                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
146                         }
147
148                         try {
149                                 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
150                                         new PointF (1, 1));
151                                 Assert.Fail ("CF#7: must throw NotSupportedException");
152                         } catch (Exception e) {
153                                 Assert.IsTrue (e is NotSupportedException, "CF#7");
154                         }
155
156                         try {
157                                 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture, 
158                                         new Size (1, 1));
159                                 Assert.Fail ("CF#8: must throw NotSupportedException");
160                         } catch (Exception e) {
161                                 Assert.IsTrue (e is NotSupportedException, "CF#8");
162                         }
163
164                         try {
165                                 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
166                                         new SizeF (1, 1));
167                                 Assert.Fail ("CF#9: must throw NotSupportedException");
168                         } catch (Exception e) {
169                                 Assert.IsTrue (e is NotSupportedException, "CF#9");
170                         }
171
172                         try {
173                                 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
174                                 Assert.Fail ("CF#10: must throw NotSupportedException");
175                         } catch (Exception e) {
176                                 Assert.IsTrue (e is NotSupportedException, "CF#10");
177                         }
178                 }
179
180                 [Test]
181                 public void TestConvertTo ()
182                 {
183                         Assert.AreEqual (ptStr, (String) ptconv.ConvertTo (null, CultureInfo.InvariantCulture,
184                                                                 pt, typeof (String)), "CT#1");
185                         Assert.AreEqual (ptnegStr, (String) ptconv.ConvertTo (null, CultureInfo.InvariantCulture,
186                                                                 ptneg, typeof (String)), "CT#2");
187
188                         try {
189                                 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
190                                                   typeof (Size));
191                                 Assert.Fail ("CT#3: must throw NotSupportedException");
192                         } catch (Exception e) {
193                                 Assert.IsTrue (e is NotSupportedException, "CT#3");
194                         }
195
196                         try {
197                                 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
198                                                   typeof (SizeF));
199                                 Assert.Fail ("CT#4: must throw NotSupportedException");
200                         } catch (Exception e) {
201                                 Assert.IsTrue (e is NotSupportedException, "CT#4");
202                         }
203
204                         try {
205                                 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
206                                                   typeof (Point));
207                                 Assert.Fail ("CT#5: must throw NotSupportedException");
208                         } catch (Exception e) {
209                                 Assert.IsTrue (e is NotSupportedException, "CT#5");
210                         }
211
212                         try {
213                                 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
214                                                   typeof (PointF));
215                                 Assert.Fail ("CT#6: must throw NotSupportedException");
216                         } catch (Exception e) {
217                                 Assert.IsTrue (e is NotSupportedException, "CT#6");
218                         }
219
220                         try {
221                                 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
222                                                   typeof (int));
223                                 Assert.Fail ("CT#7: must throw NotSupportedException");
224                         } catch (Exception e) {
225                                 Assert.IsTrue (e is NotSupportedException, "CT#7");
226                         }
227
228                         try {
229                                 // culture == null
230                                 ptconv.ConvertTo (null, null, pt, typeof (string));
231                         } catch (NullReferenceException e) {
232                                 Assert.Fail ("CT#8: must not throw NullReferenceException");
233                         }
234                 }
235
236                 [Test]
237                 public void TestGetCreateInstanceSupported ()
238                 {
239                         Assert.IsTrue (ptconv.GetCreateInstanceSupported (), "GCIS#1");
240                         Assert.IsTrue (ptconv.GetCreateInstanceSupported (null), "GCIS#2");
241                 }
242
243                 [Test]
244                 public void TestCreateInstance ()
245                 {
246                         Point ptInstance;
247
248                         Hashtable ht = new Hashtable ();
249                         ht.Add ("X", 1); ht.Add ("Y", 2);
250
251                         ptInstance = (Point) ptconv.CreateInstance (ht);
252                         Assert.AreEqual (pt, ptInstance, "CI#1");
253
254                         ht.Clear ();
255                         ht.Add ("X", -2); ht.Add ("Y", -3);
256
257                         ptInstance = (Point) ptconv.CreateInstance (null, ht);
258                         Assert.AreEqual (ptneg, ptInstance, "CI#2");
259                 }
260
261                 [Test]
262 #if NET_2_0
263                 [ExpectedException (typeof (ArgumentException))]
264 #else
265                 [ExpectedException (typeof (NullReferenceException))]
266 #endif
267                 public void TestCreateInstance_CaseSensitive ()
268                 {
269                         Hashtable ht = new Hashtable ();
270                         ht.Add ("x", 2);
271                         ht.Add ("Y", 3);
272                         ptconv.CreateInstance (null, ht);
273                 }
274
275                 [Test]
276                 public void TestGetPropertiesSupported ()
277                 {
278                         Assert.IsTrue (ptconv.GetPropertiesSupported (), "GPS#1");
279                         Assert.IsTrue (ptconv.GetPropertiesSupported (null), "GPS#2");
280                 }
281
282                 [Test]
283 #if TARGET_JVM
284                 [NUnit.Framework.Category ("NotWorking")]
285 #endif
286                 public void TestGetProperties ()
287                 {
288                         Attribute [] attrs;
289                         PropertyDescriptorCollection propsColl;
290
291                         propsColl = ptconv.GetProperties (pt);
292                         Assert.AreEqual (2, propsColl.Count, "GP1#1");
293                         Assert.AreEqual (pt.X, propsColl["X"].GetValue (pt), "GP1#2");
294                         Assert.AreEqual (pt.Y, propsColl["Y"].GetValue (pt), "GP1#3");
295
296                         propsColl = ptconv.GetProperties (null, ptneg);
297                         Assert.AreEqual (2, propsColl.Count, "GP2#1");
298                         Assert.AreEqual (ptneg.X, propsColl["X"].GetValue (ptneg), "GP2#2");
299                         Assert.AreEqual (ptneg.Y, propsColl["Y"].GetValue (ptneg), "GP2#3");
300
301                         propsColl = ptconv.GetProperties (null, pt, null);
302                         Assert.AreEqual (3, propsColl.Count, "GP3#1");
303                         Assert.AreEqual (pt.X, propsColl["X"].GetValue (pt), "GP3#2");
304                         Assert.AreEqual (pt.Y, propsColl["Y"].GetValue (pt), "GP3#3");
305                         Assert.AreEqual (pt.IsEmpty, propsColl["IsEmpty"].GetValue (pt), "GP3#4");
306
307                         Type type = typeof (Point);
308                         attrs = Attribute.GetCustomAttributes (type, true);
309                         propsColl = ptconv.GetProperties (null, pt, attrs);
310                         Assert.AreEqual (0, propsColl.Count, "GP3#5");
311                 }
312
313                 [Test]
314                 public void ConvertFromInvariantString_string ()
315                 {
316                         Assert.AreEqual (pt, ptconv.ConvertFromInvariantString ("1, 2"), "CFISS#1");
317                         Assert.AreEqual (ptneg, ptconv.ConvertFromInvariantString ("-2, -3"), "CFISS#2");
318                 }
319
320                 [Test]
321                 [ExpectedException (typeof (ArgumentException))]
322                 public void ConvertFromInvariantString_string_exc_1 ()
323                 {
324                         ptconv.ConvertFromInvariantString ("1");
325                 }
326
327                 [Test]
328 #if TARGET_JVM
329                 [NUnit.Framework.Category ("NotWorking")]
330 #endif
331                 public void ConvertFromInvariantString_string_exc_2 ()
332                 {
333                         try {
334                                 ptconv.ConvertFromInvariantString ("hello");
335                                 Assert.Fail ("#1");
336                         } catch (Exception ex) {
337                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
338                                 Assert.IsNotNull (ex.InnerException, "#3");
339                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
340                         }
341                 }
342
343                 [Test]
344 #if TARGET_JVM
345                 [NUnit.Framework.Category ("NotWorking")]
346 #endif
347                 public void ConvertFromString_string ()
348                 {
349                         // save current culture
350                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
351
352                         try {
353                                 PerformConvertFromStringTest (new CultureInfo ("en-US"));
354                                 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
355                                 PerformConvertFromStringTest (new MyCultureInfo ());
356                         } finally {
357                                 // restore original culture
358                                 Thread.CurrentThread.CurrentCulture = currentCulture;
359                         }
360                 }
361
362                 [Test]
363                 [ExpectedException (typeof (ArgumentException))]
364                 public void ConvertFromString_string_exc_1 ()
365                 {
366                         ptconv.ConvertFromString ("1");
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                                 ptconv.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 ("1" + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " 2",
389                                 ptconv.ConvertToInvariantString (pt), "CFISS#1");
390                         Assert.AreEqual ("-2" + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " -3",
391                                 ptconv.ConvertToInvariantString (ptneg), "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 (ptconv.GetStandardValuesSupported ());
414                 }
415
416                 [Test]
417                 public void GetStandardValues ()
418                 {
419                         Assert.IsNull (ptconv.GetStandardValues ());
420                 }
421
422                 [Test]
423                 public void GetStandardValuesExclusive ()
424                 {
425                         Assert.IsFalse (ptconv.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 (pt, ptconv.ConvertFromString (CreatePointString (culture, pt)),
435                                 "CFSS#1-" + culture.Name);
436                         Assert.AreEqual (ptneg, ptconv.ConvertFromString (CreatePointString (culture, ptneg)),
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 (CreatePointString (culture, pt), ptconv.ConvertToString (pt),
447                                 "CFISS#1-" + culture.Name);
448                         Assert.AreEqual (CreatePointString (culture, ptneg), ptconv.ConvertToString (ptneg),
449                                 "CFISS#2-" + culture.Name);
450                 }
451
452                 private static string CreatePointString (Point point)
453                 {
454                         return CreatePointString (CultureInfo.CurrentCulture, point);
455                 }
456
457                 private static string CreatePointString (CultureInfo culture, Point point)
458                 {
459                         return string.Format ("{0}{1} {2}", point.X.ToString (culture),
460                                 culture.TextInfo.ListSeparator, point.Y.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 }