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