2 // Tests for System.Drawing.RectangleConverter.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.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.ComponentModel;
34 using System.ComponentModel.Design.Serialization;
36 using System.Globalization;
37 using System.Security.Permissions;
38 using System.Threading;
40 using NUnit.Framework;
42 namespace MonoTests.System.Drawing
45 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
46 public class RectangleConverterTest
50 RectangleConverter rconv;
51 String rectStrInvariant;
52 String rectnegStrInvariant;
57 rect = new Rectangle (10, 10, 20, 30);
58 rectStrInvariant = rect.X + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " +
59 rect.Y + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " +
60 rect.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " +
63 rectneg = new Rectangle (-10, -10, 20, 30);
64 rectnegStrInvariant = rectneg.X + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " "
65 + rectneg.Y + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " +
66 rectneg.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + rectneg.Height;
68 rconv = (RectangleConverter) TypeDescriptor.GetConverter (rect);
72 public void TestCanConvertFrom ()
74 Assert.IsTrue (rconv.CanConvertFrom (typeof (String)), "CCF#1");
75 Assert.IsTrue (rconv.CanConvertFrom (null, typeof (String)), "CCF#2");
76 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
77 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#4");
78 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Point)), "CCF#5");
79 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (PointF)), "CCF#6");
80 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Size)), "CCF#7");
81 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (SizeF)), "CCF#8");
82 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Object)), "CCF#9");
83 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (int)), "CCF#10");
84 Assert.IsTrue (rconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#11");
88 public void TestCanConvertTo ()
90 Assert.IsTrue (rconv.CanConvertTo (typeof (String)), "CCT#1");
91 Assert.IsTrue (rconv.CanConvertTo (null, typeof (String)), "CCT#2");
92 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
93 Assert.IsFalse (rconv.CanConvertTo (null, typeof (RectangleF)), "CCT#4");
94 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Point)), "CCT#5");
95 Assert.IsFalse (rconv.CanConvertTo (null, typeof (PointF)), "CCT#6");
96 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Size)), "CCT#7");
97 Assert.IsFalse (rconv.CanConvertTo (null, typeof (SizeF)), "CCT#8");
98 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Object)), "CCT#9");
99 Assert.IsFalse (rconv.CanConvertTo (null, typeof (int)), "CCT#10");
103 public void TestConvertFrom ()
105 Assert.AreEqual (rect, (Rectangle) rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
106 "10, 10, 20, 30"), "CF#1");
107 Assert.AreEqual (rectneg, (Rectangle) rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
108 "-10, -10, 20, 30"), "CF#2");
111 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
113 Assert.Fail ("CF#3: must throw ArgumentException");
114 } catch (Exception e) {
115 Assert.IsTrue (e is ArgumentException, "CF#3");
119 rconv.ConvertFrom ("10");
120 Assert.Fail ("CF#3a: must throw ArgumentException");
121 } catch (Exception e) {
122 Assert.IsTrue (e is ArgumentException, "CF#3a");
126 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
128 Assert.Fail ("CF#4: must throw ArgumentException");
129 } catch (Exception e) {
130 Assert.IsTrue (e is ArgumentException, "CF#4");
134 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
136 Assert.Fail ("CF#5: must throw Exception");
137 } catch (Exception ex) {
138 Assert.AreEqual (typeof (Exception), ex.GetType (), "CF#5-2");
139 Assert.IsNotNull (ex.InnerException, "CF#5-3");
140 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "CF#5-4");
144 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
145 new Rectangle (10, 10, 100, 100));
146 Assert.Fail ("CF#6: must throw NotSupportedException");
147 } catch (Exception e) {
148 Assert.IsTrue (e is NotSupportedException, "CF#6");
152 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
153 new RectangleF (10, 10, 100, 100));
154 Assert.Fail ("CF#7: must throw NotSupportedException");
155 } catch (Exception e) {
156 Assert.IsTrue (e is NotSupportedException, "CF#7");
160 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
162 Assert.Fail ("CF#8: must throw NotSupportedException");
163 } catch (Exception e) {
164 Assert.IsTrue (e is NotSupportedException, "CF#8");
168 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
169 new PointF (10, 10));
170 Assert.Fail ("CF#9: must throw NotSupportedException");
171 } catch (Exception e) {
172 Assert.IsTrue (e is NotSupportedException, "CF#9");
176 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
178 Assert.Fail ("CF#10: must throw NotSupportedException");
179 } catch (Exception e) {
180 Assert.IsTrue (e is NotSupportedException, "CF#10");
184 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
186 Assert.Fail ("CF#11: must throw NotSupportedException");
187 } catch (Exception e) {
188 Assert.IsTrue (e is NotSupportedException, "CF#11");
192 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
194 Assert.Fail ("CF#12: must throw NotSupportedException");
195 } catch (Exception e) {
196 Assert.IsTrue (e is NotSupportedException, "CF#12");
200 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 1001);
201 Assert.Fail ("CF#13: must throw NotSupportedException");
202 } catch (Exception e) {
203 Assert.IsTrue (e is NotSupportedException, "CF#13");
208 public void TestConvertTo ()
210 Assert.AreEqual (rectStrInvariant, (String) rconv.ConvertTo (null,
211 CultureInfo.InvariantCulture, rect, typeof (String)), "CT#1");
212 Assert.AreEqual (rectnegStrInvariant, (String) rconv.ConvertTo (null,
213 CultureInfo.InvariantCulture, rectneg, typeof (String)), "CT#2");
216 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
217 rect, typeof (Rectangle));
218 Assert.Fail ("CT#3: must throw NotSupportedException");
219 } catch (Exception e) {
220 Assert.IsTrue (e is NotSupportedException, "CT#3");
224 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
225 rect, typeof (RectangleF));
226 Assert.Fail ("CT#4: must throw NotSupportedException");
227 } catch (Exception e) {
228 Assert.IsTrue (e is NotSupportedException, "CT#4");
232 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
233 rect, typeof (Size));
234 Assert.Fail ("CT#5: must throw NotSupportedException");
235 } catch (Exception e) {
236 Assert.IsTrue (e is NotSupportedException, "CT#5");
240 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
241 rect, typeof (SizeF));
242 Assert.Fail ("CT#6: must throw NotSupportedException");
243 } catch (Exception e) {
244 Assert.IsTrue (e is NotSupportedException, "CT#6");
248 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
249 rect, typeof (Point));
250 Assert.Fail ("CT#7: must throw NotSupportedException");
251 } catch (Exception e) {
252 Assert.IsTrue (e is NotSupportedException, "CT#7");
256 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
257 rect, typeof (PointF));
258 Assert.Fail ("CT#8: must throw NotSupportedException");
259 } catch (Exception e) {
260 Assert.IsTrue (e is NotSupportedException, "CT#8");
264 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
265 rect, typeof (Object));
266 Assert.Fail ("CT#9: must throw NotSupportedException");
267 } catch (Exception e) {
268 Assert.IsTrue (e is NotSupportedException, "CT#9");
272 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
274 Assert.Fail ("CT#10: must throw NotSupportedException");
275 } catch (Exception e) {
276 Assert.IsTrue (e is NotSupportedException, "CT#10");
281 public void TestGetCreateInstanceSupported ()
283 Assert.IsTrue (rconv.GetCreateInstanceSupported (), "GCIS#1");
284 Assert.IsTrue (rconv.GetCreateInstanceSupported (null), "GCIS#2");
288 public void TestCreateInstance ()
290 Rectangle rectInstance;
292 Hashtable ht = new Hashtable ();
293 ht.Add ("X", 10); ht.Add ("Y", 10);
294 ht.Add ("Width", 20); ht.Add ("Height", 30);
296 rectInstance = (Rectangle) rconv.CreateInstance (ht);
297 Assert.AreEqual (rect, rectInstance, "CI#1");
300 ht.Add ("X", -10); ht.Add ("Y", -10);
301 ht.Add ("Width", 20); ht.Add ("Height", 30);
303 rectInstance = (Rectangle) rconv.CreateInstance (null, ht);
304 Assert.AreEqual (rectneg, rectInstance, "CI#2");
308 public void TestCreateInstance_CaseSensitive ()
310 Hashtable ht = new Hashtable ();
313 ht.Add ("Width", 20);
314 ht.Add ("Height", 30);
315 Assert.Throws<ArgumentException> (() => rconv.CreateInstance (null, ht));
319 public void TestGetPropertiesSupported ()
321 Assert.IsTrue (rconv.GetPropertiesSupported (), "GPS#1");
322 Assert.IsTrue (rconv.GetPropertiesSupported (null), "GPS#2");
326 public void TestGetProperties ()
329 PropertyDescriptorCollection propsColl;
331 propsColl = rconv.GetProperties (rect);
332 Assert.AreEqual (4, propsColl.Count, "GP1#1");
333 Assert.AreEqual (rect.X, propsColl["X"].GetValue (rect), "GP1#2");
334 Assert.AreEqual (rect.Y, propsColl["Y"].GetValue (rect), "GP1#3");
335 Assert.AreEqual (rect.Width, propsColl["Width"].GetValue (rect), "GP1#4");
336 Assert.AreEqual (rect.Height, propsColl["Height"].GetValue (rect), "GP1#5");
338 propsColl = rconv.GetProperties (null, rectneg);
339 Assert.AreEqual (4, propsColl.Count, "GP2#1");
340 Assert.AreEqual (rectneg.X, propsColl["X"].GetValue (rectneg), "GP2#2");
341 Assert.AreEqual (rectneg.Y, propsColl["Y"].GetValue (rectneg), "GP2#3");
342 Assert.AreEqual (rectneg.Width, propsColl["Width"].GetValue (rectneg), "GP2#4");
343 Assert.AreEqual (rectneg.Height, propsColl["Height"].GetValue (rectneg), "GP2#5");
345 propsColl = rconv.GetProperties (null, rect, null);
346 Assert.AreEqual (11, propsColl.Count, "GP3#1");
347 Assert.AreEqual (rect.X, propsColl["X"].GetValue (rect), "GP3#2");
348 Assert.AreEqual (rect.Y, propsColl["Y"].GetValue (rect), "GP3#3");
349 Assert.AreEqual (rect.Width, propsColl["Width"].GetValue (rect), "GP3#4");
350 Assert.AreEqual (rect.Height, propsColl["Height"].GetValue (rect), "GP3#5");
352 Assert.AreEqual (rect.Top, propsColl["Top"].GetValue (rect), "GP3#6");
353 Assert.AreEqual (rect.Bottom, propsColl["Bottom"].GetValue (rect), "GP3#7");
354 Assert.AreEqual (rect.Left, propsColl["Left"].GetValue (rect), "GP3#8");
355 Assert.AreEqual (rect.Right, propsColl["Right"].GetValue (rect), "GP3#9");
356 Assert.AreEqual (rect.Location, propsColl["Location"].GetValue (rect), "GP3#10");
357 Assert.AreEqual (rect.Size, propsColl["Size"].GetValue (rect), "GP3#11");
358 Assert.AreEqual (rect.IsEmpty, propsColl["IsEmpty"].GetValue (rect), "GP3#12");
360 Type type = typeof (Rectangle);
361 attrs = Attribute.GetCustomAttributes (type, true);
362 propsColl = rconv.GetProperties (null, rect, attrs);
363 Assert.AreEqual (0, propsColl.Count, "GP3#13");
367 public void ConvertFromInvariantString_string ()
369 Assert.AreEqual (rect, rconv.ConvertFromInvariantString (rectStrInvariant),
371 Assert.AreEqual (rectneg, rconv.ConvertFromInvariantString (rectnegStrInvariant),
376 public void ConvertFromInvariantString_string_exc_1 ()
378 Assert.Throws<ArgumentException> (() => rconv.ConvertFromInvariantString ("1, 2, 3"));
382 public void ConvertFromInvariantString_string_exc_2 ()
385 rconv.ConvertFromInvariantString ("hello");
387 } catch (Exception ex) {
388 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
389 Assert.IsNotNull (ex.InnerException, "#3");
390 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
395 public void ConvertFromString_string ()
397 // save current culture
398 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
401 PerformConvertFromStringTest (new CultureInfo ("en-US"));
402 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
403 PerformConvertFromStringTest (new MyCultureInfo ());
405 // restore original culture
406 Thread.CurrentThread.CurrentCulture = currentCulture;
411 public void ConvertFromString_string_exc_1 ()
413 CultureInfo culture = CultureInfo.CurrentCulture;
414 Assert.Throws<ArgumentException> (() => rconv.ConvertFromString (string.Format(culture,
415 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator)));
419 public void ConvertFromString_string_exc_2 ()
422 rconv.ConvertFromString ("hello");
424 } catch (Exception ex) {
425 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
426 Assert.IsNotNull (ex.InnerException, "#3");
427 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
432 public void ConvertToInvariantString_string ()
434 Assert.AreEqual (rectStrInvariant, rconv.ConvertToInvariantString (rect),
436 Assert.AreEqual (rectnegStrInvariant, rconv.ConvertToInvariantString (rectneg),
441 public void ConvertToString_string () {
442 // save current culture
443 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
446 PerformConvertToStringTest (new CultureInfo ("en-US"));
447 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
448 PerformConvertToStringTest (new MyCultureInfo ());
450 // restore original culture
451 Thread.CurrentThread.CurrentCulture = currentCulture;
456 public void GetStandardValuesSupported ()
458 Assert.IsFalse (rconv.GetStandardValuesSupported ());
462 public void GetStandardValues ()
464 Assert.IsNull (rconv.GetStandardValues ());
468 public void GetStandardValuesExclusive ()
470 Assert.IsFalse (rconv.GetStandardValuesExclusive ());
473 private void PerformConvertFromStringTest (CultureInfo culture)
475 // set current culture
476 Thread.CurrentThread.CurrentCulture = culture;
479 Assert.AreEqual (rect, rconv.ConvertFromString (CreateRectangleString (rect)),
480 "CFSS#1-" + culture.Name);
481 Assert.AreEqual (rectneg, rconv.ConvertFromString (CreateRectangleString (rectneg)),
482 "CFSS#2-" + culture.Name);
485 private void PerformConvertToStringTest (CultureInfo culture)
487 // set current culture
488 Thread.CurrentThread.CurrentCulture = culture;
491 Assert.AreEqual (CreateRectangleString (rect), rconv.ConvertToString (rect),
492 "CFISS#1-" + culture.Name);
493 Assert.AreEqual (CreateRectangleString (rectneg), rconv.ConvertToString (rectneg),
494 "CFISS#2-" + culture.Name);
497 private static string CreateRectangleString (Rectangle rectangle)
499 return CreateRectangleString (CultureInfo.CurrentCulture, rectangle);
502 private static string CreateRectangleString (CultureInfo culture, Rectangle rectangle)
504 return string.Format ("{0}{1} {2}{1} {3}{1} {4}", rectangle.X.ToString (culture),
505 culture.TextInfo.ListSeparator, rectangle.Y.ToString (culture),
506 rectangle.Width.ToString (culture), rectangle.Height.ToString (culture));
510 private sealed class MyCultureInfo : CultureInfo
512 internal MyCultureInfo ()
517 public override object GetFormat (Type formatType)
519 if (formatType == typeof (NumberFormatInfo)) {
520 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
522 nfi.NegativeSign = "myNegativeSign";
523 return NumberFormatInfo.ReadOnly (nfi);
525 return base.GetFormat (formatType);