2006-08-07 Sebastien Pouliot <sebastien@ximian.com>
[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
229                 [Test]
230                 public void TestGetCreateInstanceSupported ()
231                 {
232                         Assert.IsTrue (ptconv.GetCreateInstanceSupported (), "GCIS#1");
233                         Assert.IsTrue (ptconv.GetCreateInstanceSupported (null), "GCIS#2");
234                 }
235
236                 [Test]
237                 public void TestCreateInstance ()
238                 {
239                         Point ptInstance;
240
241                         Hashtable ht = new Hashtable ();
242                         ht.Add ("X", 1); ht.Add ("Y", 2);
243
244                         ptInstance = (Point) ptconv.CreateInstance (ht);
245                         Assert.AreEqual (pt, ptInstance, "CI#1");
246
247                         ht.Clear ();
248                         ht.Add ("X", -2); ht.Add ("Y", -3);
249
250                         ptInstance = (Point) ptconv.CreateInstance (null, ht);
251                         Assert.AreEqual (ptneg, ptInstance, "CI#2");
252                 }
253
254                 [Test]
255 #if NET_2_0
256                 [ExpectedException (typeof (ArgumentException))]
257 #else
258                 [ExpectedException (typeof (NullReferenceException))]
259 #endif
260                 public void TestCreateInstance_CaseSensitive ()
261                 {
262                         Hashtable ht = new Hashtable ();
263                         ht.Add ("x", 2);
264                         ht.Add ("Y", 3);
265                         ptconv.CreateInstance (null, ht);
266                 }
267
268                 [Test]
269                 public void TestGetPropertiesSupported ()
270                 {
271                         Assert.IsTrue (ptconv.GetPropertiesSupported (), "GPS#1");
272                         Assert.IsTrue (ptconv.GetPropertiesSupported (null), "GPS#2");
273                 }
274
275                 [Test]
276 #if TARGET_JVM
277                 [NUnit.Framework.Category ("NotWorking")]
278 #endif
279                 public void TestGetProperties ()
280                 {
281                         Attribute [] attrs;
282                         PropertyDescriptorCollection propsColl;
283
284                         propsColl = ptconv.GetProperties (pt);
285                         Assert.AreEqual (2, propsColl.Count, "GP1#1");
286                         Assert.AreEqual (pt.X, propsColl["X"].GetValue (pt), "GP1#2");
287                         Assert.AreEqual (pt.Y, propsColl["Y"].GetValue (pt), "GP1#3");
288
289                         propsColl = ptconv.GetProperties (null, ptneg);
290                         Assert.AreEqual (2, propsColl.Count, "GP2#1");
291                         Assert.AreEqual (ptneg.X, propsColl["X"].GetValue (ptneg), "GP2#2");
292                         Assert.AreEqual (ptneg.Y, propsColl["Y"].GetValue (ptneg), "GP2#3");
293
294                         propsColl = ptconv.GetProperties (null, pt, null);
295                         Assert.AreEqual (3, propsColl.Count, "GP3#1");
296                         Assert.AreEqual (pt.X, propsColl["X"].GetValue (pt), "GP3#2");
297                         Assert.AreEqual (pt.Y, propsColl["Y"].GetValue (pt), "GP3#3");
298                         Assert.AreEqual (pt.IsEmpty, propsColl["IsEmpty"].GetValue (pt), "GP3#4");
299
300                         Type type = typeof (Point);
301                         attrs = Attribute.GetCustomAttributes (type, true);
302                         propsColl = ptconv.GetProperties (null, pt, attrs);
303                         Assert.AreEqual (0, propsColl.Count, "GP3#5");
304                 }
305
306                 [Test]
307                 public void ConvertFromInvariantString_string ()
308                 {
309                         Assert.AreEqual (pt, ptconv.ConvertFromInvariantString ("1, 2"), "CFISS#1");
310                         Assert.AreEqual (ptneg, ptconv.ConvertFromInvariantString ("-2, -3"), "CFISS#2");
311                 }
312
313                 [Test]
314                 [ExpectedException (typeof (ArgumentException))]
315                 public void ConvertFromInvariantString_string_exc_1 ()
316                 {
317                         ptconv.ConvertFromInvariantString ("1");
318                 }
319
320                 [Test]
321 #if TARGET_JVM
322                 [NUnit.Framework.Category ("NotWorking")]
323 #endif
324                 public void ConvertFromInvariantString_string_exc_2 ()
325                 {
326                         try {
327                                 ptconv.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 #if TARGET_JVM
338                 [NUnit.Framework.Category ("NotWorking")]
339 #endif
340                 public void ConvertFromString_string ()
341                 {
342                         // save current culture
343                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
344
345                         try {
346                                 PerformConvertFromStringTest (new CultureInfo ("en-US"));
347                                 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
348                                 PerformConvertFromStringTest (new MyCultureInfo ());
349                         } finally {
350                                 // restore original culture
351                                 Thread.CurrentThread.CurrentCulture = currentCulture;
352                         }
353                 }
354
355                 [Test]
356                 [ExpectedException (typeof (ArgumentException))]
357                 public void ConvertFromString_string_exc_1 ()
358                 {
359                         ptconv.ConvertFromString ("1");
360                 }
361
362                 [Test]
363 #if TARGET_JVM
364                 [NUnit.Framework.Category ("NotWorking")]
365 #endif
366                 public void ConvertFromString_string_exc_2 ()
367                 {
368                         try {
369                                 ptconv.ConvertFromString ("hello");
370                                 Assert.Fail ("#1");
371                         } catch (Exception ex) {
372                                 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
373                                 Assert.IsNotNull (ex.InnerException, "#3");
374                                 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
375                         }
376                 }
377
378                 [Test]
379                 public void ConvertToInvariantString_string ()
380                 {
381                         Assert.AreEqual ("1" + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " 2",
382                                 ptconv.ConvertToInvariantString (pt), "CFISS#1");
383                         Assert.AreEqual ("-2" + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " -3",
384                                 ptconv.ConvertToInvariantString (ptneg), "CFISS#2");
385                 }
386
387                 [Test]
388                 public void ConvertToString_string ()
389                 {
390                         // save current culture
391                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
392
393                         try {
394                                 PerformConvertToStringTest (new CultureInfo ("en-US"));
395                                 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
396                                 PerformConvertToStringTest (new MyCultureInfo ());
397                         } finally {
398                                 // restore original culture
399                                 Thread.CurrentThread.CurrentCulture = currentCulture;
400                         }
401                 }
402
403                 [Test]
404                 public void GetStandardValuesSupported ()
405                 {
406                         Assert.IsFalse (ptconv.GetStandardValuesSupported ());
407                 }
408
409                 [Test]
410                 public void GetStandardValues ()
411                 {
412                         Assert.IsNull (ptconv.GetStandardValues ());
413                 }
414
415                 [Test]
416                 public void GetStandardValuesExclusive ()
417                 {
418                         Assert.IsFalse (ptconv.GetStandardValuesExclusive ());
419                 }
420
421                 private void PerformConvertFromStringTest (CultureInfo culture)
422                 {
423                         // set current culture
424                         Thread.CurrentThread.CurrentCulture = culture;
425
426                         // perform tests
427                         Assert.AreEqual (pt, ptconv.ConvertFromString (CreatePointString (culture, pt)),
428                                 "CFSS#1-" + culture.Name);
429                         Assert.AreEqual (ptneg, ptconv.ConvertFromString (CreatePointString (culture, ptneg)),
430                                 "CFSS#2-" + culture.Name);
431                 }
432
433                 private void PerformConvertToStringTest (CultureInfo culture)
434                 {
435                         // set current culture
436                         Thread.CurrentThread.CurrentCulture = culture;
437
438                         // perform tests
439                         Assert.AreEqual (CreatePointString (culture, pt), ptconv.ConvertToString (pt),
440                                 "CFISS#1-" + culture.Name);
441                         Assert.AreEqual (CreatePointString (culture, ptneg), ptconv.ConvertToString (ptneg),
442                                 "CFISS#2-" + culture.Name);
443                 }
444
445                 private static string CreatePointString (Point point)
446                 {
447                         return CreatePointString (CultureInfo.CurrentCulture, point);
448                 }
449
450                 private static string CreatePointString (CultureInfo culture, Point point)
451                 {
452                         return string.Format ("{0}{1} {2}", point.X.ToString (culture),
453                                 culture.TextInfo.ListSeparator, point.Y.ToString (culture));
454                 }
455
456                 [Serializable]
457                 private sealed class MyCultureInfo : CultureInfo
458                 {
459                         internal MyCultureInfo () : base ("en-US")
460                         {
461                         }
462
463                         public override object GetFormat (Type formatType)
464                         {
465                                 if (formatType == typeof (NumberFormatInfo)) {
466                                         NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
467
468                                         nfi.NegativeSign = "myNegativeSign";
469                                         return NumberFormatInfo.ReadOnly (nfi);
470                                 } else {
471                                         return base.GetFormat (formatType);
472                                 }
473                         }
474                 }
475         }
476 }