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 public void TestGetCreateInstanceSupported ()
232 Assert.IsTrue (ptconv.GetCreateInstanceSupported (), "GCIS#1");
233 Assert.IsTrue (ptconv.GetCreateInstanceSupported (null), "GCIS#2");
237 public void TestCreateInstance ()
241 Hashtable ht = new Hashtable ();
242 ht.Add ("X", 1); ht.Add ("Y", 2);
244 ptInstance = (Point) ptconv.CreateInstance (ht);
245 Assert.AreEqual (pt, ptInstance, "CI#1");
248 ht.Add ("X", -2); ht.Add ("Y", -3);
250 ptInstance = (Point) ptconv.CreateInstance (null, ht);
251 Assert.AreEqual (ptneg, ptInstance, "CI#2");
256 [ExpectedException (typeof (ArgumentException))]
258 [ExpectedException (typeof (NullReferenceException))]
260 public void TestCreateInstance_CaseSensitive ()
262 Hashtable ht = new Hashtable ();
265 ptconv.CreateInstance (null, ht);
269 public void TestGetPropertiesSupported ()
271 Assert.IsTrue (ptconv.GetPropertiesSupported (), "GPS#1");
272 Assert.IsTrue (ptconv.GetPropertiesSupported (null), "GPS#2");
277 [NUnit.Framework.Category ("NotWorking")]
279 public void TestGetProperties ()
282 PropertyDescriptorCollection propsColl;
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");
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");
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");
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");
307 public void ConvertFromInvariantString_string ()
309 Assert.AreEqual (pt, ptconv.ConvertFromInvariantString ("1, 2"), "CFISS#1");
310 Assert.AreEqual (ptneg, ptconv.ConvertFromInvariantString ("-2, -3"), "CFISS#2");
314 [ExpectedException (typeof (ArgumentException))]
315 public void ConvertFromInvariantString_string_exc_1 ()
317 ptconv.ConvertFromInvariantString ("1");
322 [NUnit.Framework.Category ("NotWorking")]
324 public void ConvertFromInvariantString_string_exc_2 ()
327 ptconv.ConvertFromInvariantString ("hello");
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");
338 [NUnit.Framework.Category ("NotWorking")]
340 public void ConvertFromString_string ()
342 // save current culture
343 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
346 PerformConvertFromStringTest (new CultureInfo ("en-US"));
347 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
348 PerformConvertFromStringTest (new MyCultureInfo ());
350 // restore original culture
351 Thread.CurrentThread.CurrentCulture = currentCulture;
356 [ExpectedException (typeof (ArgumentException))]
357 public void ConvertFromString_string_exc_1 ()
359 ptconv.ConvertFromString ("1");
364 [NUnit.Framework.Category ("NotWorking")]
366 public void ConvertFromString_string_exc_2 ()
369 ptconv.ConvertFromString ("hello");
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");
379 public void ConvertToInvariantString_string ()
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");
388 public void ConvertToString_string ()
390 // save current culture
391 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
394 PerformConvertToStringTest (new CultureInfo ("en-US"));
395 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
396 PerformConvertToStringTest (new MyCultureInfo ());
398 // restore original culture
399 Thread.CurrentThread.CurrentCulture = currentCulture;
404 public void GetStandardValuesSupported ()
406 Assert.IsFalse (ptconv.GetStandardValuesSupported ());
410 public void GetStandardValues ()
412 Assert.IsNull (ptconv.GetStandardValues ());
416 public void GetStandardValuesExclusive ()
418 Assert.IsFalse (ptconv.GetStandardValuesExclusive ());
421 private void PerformConvertFromStringTest (CultureInfo culture)
423 // set current culture
424 Thread.CurrentThread.CurrentCulture = culture;
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);
433 private void PerformConvertToStringTest (CultureInfo culture)
435 // set current culture
436 Thread.CurrentThread.CurrentCulture = culture;
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);
445 private static string CreatePointString (Point point)
447 return CreatePointString (CultureInfo.CurrentCulture, point);
450 private static string CreatePointString (CultureInfo culture, Point point)
452 return string.Format ("{0}{1} {2}", point.X.ToString (culture),
453 culture.TextInfo.ListSeparator, point.Y.ToString (culture));
457 private sealed class MyCultureInfo : CultureInfo
459 internal MyCultureInfo () : base ("en-US")
463 public override object GetFormat (Type formatType)
465 if (formatType == typeof (NumberFormatInfo)) {
466 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
468 nfi.NegativeSign = "myNegativeSign";
469 return NumberFormatInfo.ReadOnly (nfi);
471 return base.GetFormat (formatType);