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