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);
66 public void TestCanConvertFrom ()
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");
82 public void TestCanConvertTo ()
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");
97 public void TestConvertFrom ()
99 Assert.AreEqual (pt, (Point) ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
101 Assert.AreEqual (ptneg, (Point) ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
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");
112 ptconv.ConvertFrom ("1");
113 Assert.Fail ("CF#3a: must throw ArgumentException");
114 } catch (Exception e) {
115 Assert.IsTrue (e is ArgumentException, "CF#3a");
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");
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");
135 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
137 Assert.Fail ("CF#6: must throw NotSupportedException");
138 } catch (Exception e) {
139 Assert.IsTrue (e is NotSupportedException, "CF#6");
143 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
145 Assert.Fail ("CF#7: must throw NotSupportedException");
146 } catch (Exception e) {
147 Assert.IsTrue (e is NotSupportedException, "CF#7");
151 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
153 Assert.Fail ("CF#8: must throw NotSupportedException");
154 } catch (Exception e) {
155 Assert.IsTrue (e is NotSupportedException, "CF#8");
159 ptconv.ConvertFrom (null, CultureInfo.InvariantCulture,
161 Assert.Fail ("CF#9: must throw NotSupportedException");
162 } catch (Exception e) {
163 Assert.IsTrue (e is NotSupportedException, "CF#9");
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");
175 public void TestConvertTo ()
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");
183 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
185 Assert.Fail ("CT#3: must throw NotSupportedException");
186 } catch (Exception e) {
187 Assert.IsTrue (e is NotSupportedException, "CT#3");
191 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
193 Assert.Fail ("CT#4: must throw NotSupportedException");
194 } catch (Exception e) {
195 Assert.IsTrue (e is NotSupportedException, "CT#4");
199 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
201 Assert.Fail ("CT#5: must throw NotSupportedException");
202 } catch (Exception e) {
203 Assert.IsTrue (e is NotSupportedException, "CT#5");
207 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
209 Assert.Fail ("CT#6: must throw NotSupportedException");
210 } catch (Exception e) {
211 Assert.IsTrue (e is NotSupportedException, "CT#6");
215 ptconv.ConvertTo (null, CultureInfo.InvariantCulture, pt,
217 Assert.Fail ("CT#7: must throw NotSupportedException");
218 } catch (Exception e) {
219 Assert.IsTrue (e is NotSupportedException, "CT#7");
224 ptconv.ConvertTo (null, null, pt, typeof (string));
225 } catch (NullReferenceException e) {
226 Assert.Fail ("CT#8: must not throw NullReferenceException");
231 public void TestGetCreateInstanceSupported ()
233 Assert.IsTrue (ptconv.GetCreateInstanceSupported (), "GCIS#1");
234 Assert.IsTrue (ptconv.GetCreateInstanceSupported (null), "GCIS#2");
238 public void TestCreateInstance ()
242 Hashtable ht = new Hashtable ();
243 ht.Add ("X", 1); ht.Add ("Y", 2);
245 ptInstance = (Point) ptconv.CreateInstance (ht);
246 Assert.AreEqual (pt, ptInstance, "CI#1");
249 ht.Add ("X", -2); ht.Add ("Y", -3);
251 ptInstance = (Point) ptconv.CreateInstance (null, ht);
252 Assert.AreEqual (ptneg, ptInstance, "CI#2");
256 [ExpectedException (typeof (ArgumentException))]
257 public void TestCreateInstance_CaseSensitive ()
259 Hashtable ht = new Hashtable ();
262 ptconv.CreateInstance (null, ht);
266 public void TestGetPropertiesSupported ()
268 Assert.IsTrue (ptconv.GetPropertiesSupported (), "GPS#1");
269 Assert.IsTrue (ptconv.GetPropertiesSupported (null), "GPS#2");
273 public void TestGetProperties ()
276 PropertyDescriptorCollection propsColl;
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");
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");
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");
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");
301 public void ConvertFromInvariantString_string ()
303 Assert.AreEqual (pt, ptconv.ConvertFromInvariantString ("1, 2"), "CFISS#1");
304 Assert.AreEqual (ptneg, ptconv.ConvertFromInvariantString ("-2, -3"), "CFISS#2");
308 [ExpectedException (typeof (ArgumentException))]
309 public void ConvertFromInvariantString_string_exc_1 ()
311 ptconv.ConvertFromInvariantString ("1");
315 public void ConvertFromInvariantString_string_exc_2 ()
318 ptconv.ConvertFromInvariantString ("hello");
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");
328 public void ConvertFromString_string ()
330 // save current culture
331 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
334 PerformConvertFromStringTest (new CultureInfo ("en-US"));
335 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
336 PerformConvertFromStringTest (new MyCultureInfo ());
338 // restore original culture
339 Thread.CurrentThread.CurrentCulture = currentCulture;
344 [ExpectedException (typeof (ArgumentException))]
345 public void ConvertFromString_string_exc_1 ()
347 ptconv.ConvertFromString ("1");
351 public void ConvertFromString_string_exc_2 ()
354 ptconv.ConvertFromString ("hello");
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");
364 public void ConvertToInvariantString_string ()
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");
373 public void ConvertToString_string ()
375 // save current culture
376 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
379 PerformConvertToStringTest (new CultureInfo ("en-US"));
380 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
381 PerformConvertToStringTest (new MyCultureInfo ());
383 // restore original culture
384 Thread.CurrentThread.CurrentCulture = currentCulture;
389 public void GetStandardValuesSupported ()
391 Assert.IsFalse (ptconv.GetStandardValuesSupported ());
395 public void GetStandardValues ()
397 Assert.IsNull (ptconv.GetStandardValues ());
401 public void GetStandardValuesExclusive ()
403 Assert.IsFalse (ptconv.GetStandardValuesExclusive ());
406 private void PerformConvertFromStringTest (CultureInfo culture)
408 // set current culture
409 Thread.CurrentThread.CurrentCulture = culture;
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);
418 private void PerformConvertToStringTest (CultureInfo culture)
420 // set current culture
421 Thread.CurrentThread.CurrentCulture = culture;
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);
430 private static string CreatePointString (Point point)
432 return CreatePointString (CultureInfo.CurrentCulture, point);
435 private static string CreatePointString (CultureInfo culture, Point point)
437 return string.Format ("{0}{1} {2}", point.X.ToString (culture),
438 culture.TextInfo.ListSeparator, point.Y.ToString (culture));
442 private sealed class MyCultureInfo : CultureInfo
444 internal MyCultureInfo () : base ("en-US")
448 public override object GetFormat (Type formatType)
450 if (formatType == typeof (NumberFormatInfo)) {
451 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
453 nfi.NegativeSign = "myNegativeSign";
454 return NumberFormatInfo.ReadOnly (nfi);
456 return base.GetFormat (formatType);