2 // Tests for System.Drawing.PointConverter.cs
5 // Ravindra (rkumar@novell.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
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.
31 using System.Collections;
32 using System.ComponentModel;
33 using System.ComponentModel.Design.Serialization;
35 using System.Globalization;
36 using System.Security.Permissions;
37 using System.Threading;
39 using NUnit.Framework;
41 namespace MonoTests.System.Drawing
44 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
45 public class PointConverterTest
49 PointConverter ptconv;
56 pt = new Point (1, 2);
57 ptStr = pt.X + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + pt.Y;
59 ptneg = new Point (-2, -3);
60 ptnegStr = ptneg.X + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + ptneg.Y;
62 ptconv = (PointConverter) TypeDescriptor.GetConverter (pt);
67 [NUnit.Framework.Category ("NotWorking")]
69 public void TestCanConvertFrom ()
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");
85 public void TestCanConvertTo ()
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");
101 [NUnit.Framework.Category ("NotWorking")]
103 public void TestConvertFrom ()
105 Assert.AreEqual (pt, (Point) ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
107 Assert.AreEqual (ptneg, (Point) ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
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");
118 ptconv.ConvertFrom ("1");
119 Assert.Fail ("CF#3a: must throw ArgumentException");
120 } catch (Exception e) {
121 Assert.IsTrue (e is ArgumentException, "CF#3a");
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");
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");
141 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
143 Assert.Fail ("CF#6: must throw NotSupportedException");
144 } catch (Exception e) {
145 Assert.IsTrue (e is NotSupportedException, "CF#6");
149 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
151 Assert.Fail ("CF#7: must throw NotSupportedException");
152 } catch (Exception e) {
153 Assert.IsTrue (e is NotSupportedException, "CF#7");
157 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
159 Assert.Fail ("CF#8: must throw NotSupportedException");
160 } catch (Exception e) {
161 Assert.IsTrue (e is NotSupportedException, "CF#8");
165 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
167 Assert.Fail ("CF#9: must throw NotSupportedException");
168 } catch (Exception e) {
169 Assert.IsTrue (e is NotSupportedException, "CF#9");
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");
181 public void TestConvertTo ()
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");
189 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
191 Assert.Fail ("CT#3: must throw NotSupportedException");
192 } catch (Exception e) {
193 Assert.IsTrue (e is NotSupportedException, "CT#3");
197 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
199 Assert.Fail ("CT#4: must throw NotSupportedException");
200 } catch (Exception e) {
201 Assert.IsTrue (e is NotSupportedException, "CT#4");
205 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
207 Assert.Fail ("CT#5: must throw NotSupportedException");
208 } catch (Exception e) {
209 Assert.IsTrue (e is NotSupportedException, "CT#5");
213 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
215 Assert.Fail ("CT#6: must throw NotSupportedException");
216 } catch (Exception e) {
217 Assert.IsTrue (e is NotSupportedException, "CT#6");
221 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
223 Assert.Fail ("CT#7: must throw NotSupportedException");
224 } catch (Exception e) {
225 Assert.IsTrue (e is NotSupportedException, "CT#7");
230 ptconv.ConvertTo (null, null, pt, typeof (string));
231 } catch (NullReferenceException e) {
232 Assert.Fail ("CT#8: must not throw NullReferenceException");
237 public void TestGetCreateInstanceSupported ()
239 Assert.IsTrue (ptconv.GetCreateInstanceSupported (), "GCIS#1");
240 Assert.IsTrue (ptconv.GetCreateInstanceSupported (null), "GCIS#2");
244 public void TestCreateInstance ()
248 Hashtable ht = new Hashtable ();
249 ht.Add ("X", 1); ht.Add ("Y", 2);
251 ptInstance = (Point) ptconv.CreateInstance (ht);
252 Assert.AreEqual (pt, ptInstance, "CI#1");
255 ht.Add ("X", -2); ht.Add ("Y", -3);
257 ptInstance = (Point) ptconv.CreateInstance (null, ht);
258 Assert.AreEqual (ptneg, ptInstance, "CI#2");
263 [ExpectedException (typeof (ArgumentException))]
265 [ExpectedException (typeof (NullReferenceException))]
267 public void TestCreateInstance_CaseSensitive ()
269 Hashtable ht = new Hashtable ();
272 ptconv.CreateInstance (null, ht);
276 public void TestGetPropertiesSupported ()
278 Assert.IsTrue (ptconv.GetPropertiesSupported (), "GPS#1");
279 Assert.IsTrue (ptconv.GetPropertiesSupported (null), "GPS#2");
284 [NUnit.Framework.Category ("NotWorking")]
286 public void TestGetProperties ()
289 PropertyDescriptorCollection propsColl;
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");
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");
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");
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");
314 public void ConvertFromInvariantString_string ()
316 Assert.AreEqual (pt, ptconv.ConvertFromInvariantString ("1, 2"), "CFISS#1");
317 Assert.AreEqual (ptneg, ptconv.ConvertFromInvariantString ("-2, -3"), "CFISS#2");
321 [ExpectedException (typeof (ArgumentException))]
322 public void ConvertFromInvariantString_string_exc_1 ()
324 ptconv.ConvertFromInvariantString ("1");
329 [NUnit.Framework.Category ("NotWorking")]
331 public void ConvertFromInvariantString_string_exc_2 ()
334 ptconv.ConvertFromInvariantString ("hello");
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");
345 [NUnit.Framework.Category ("NotWorking")]
347 public void ConvertFromString_string ()
349 // save current culture
350 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
353 PerformConvertFromStringTest (new CultureInfo ("en-US"));
354 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
355 PerformConvertFromStringTest (new MyCultureInfo ());
357 // restore original culture
358 Thread.CurrentThread.CurrentCulture = currentCulture;
363 [ExpectedException (typeof (ArgumentException))]
364 public void ConvertFromString_string_exc_1 ()
366 ptconv.ConvertFromString ("1");
371 [NUnit.Framework.Category ("NotWorking")]
373 public void ConvertFromString_string_exc_2 ()
376 ptconv.ConvertFromString ("hello");
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");
386 public void ConvertToInvariantString_string ()
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");
395 public void ConvertToString_string ()
397 // save current culture
398 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
401 PerformConvertToStringTest (new CultureInfo ("en-US"));
402 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
403 PerformConvertToStringTest (new MyCultureInfo ());
405 // restore original culture
406 Thread.CurrentThread.CurrentCulture = currentCulture;
411 public void GetStandardValuesSupported ()
413 Assert.IsFalse (ptconv.GetStandardValuesSupported ());
417 public void GetStandardValues ()
419 Assert.IsNull (ptconv.GetStandardValues ());
423 public void GetStandardValuesExclusive ()
425 Assert.IsFalse (ptconv.GetStandardValuesExclusive ());
428 private void PerformConvertFromStringTest (CultureInfo culture)
430 // set current culture
431 Thread.CurrentThread.CurrentCulture = culture;
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);
440 private void PerformConvertToStringTest (CultureInfo culture)
442 // set current culture
443 Thread.CurrentThread.CurrentCulture = culture;
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);
452 private static string CreatePointString (Point point)
454 return CreatePointString (CultureInfo.CurrentCulture, point);
457 private static string CreatePointString (CultureInfo culture, Point point)
459 return string.Format ("{0}{1} {2}", point.X.ToString (culture),
460 culture.TextInfo.ListSeparator, point.Y.ToString (culture));
464 private sealed class MyCultureInfo : CultureInfo
466 internal MyCultureInfo () : base ("en-US")
470 public override object GetFormat (Type formatType)
472 if (formatType == typeof (NumberFormatInfo)) {
473 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
475 nfi.NegativeSign = "myNegativeSign";
476 return NumberFormatInfo.ReadOnly (nfi);
478 return base.GetFormat (formatType);