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);
73 [NUnit.Framework.Category ("NotWorking")]
75 public void TestCanConvertFrom ()
77 Assert.IsTrue (rconv.CanConvertFrom (typeof (String)), "CCF#1");
78 Assert.IsTrue (rconv.CanConvertFrom (null, typeof (String)), "CCF#2");
79 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
80 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#4");
81 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Point)), "CCF#5");
82 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (PointF)), "CCF#6");
83 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Size)), "CCF#7");
84 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (SizeF)), "CCF#8");
85 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (Object)), "CCF#9");
86 Assert.IsFalse (rconv.CanConvertFrom (null, typeof (int)), "CCF#10");
87 Assert.IsTrue (rconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#11");
91 public void TestCanConvertTo ()
93 Assert.IsTrue (rconv.CanConvertTo (typeof (String)), "CCT#1");
94 Assert.IsTrue (rconv.CanConvertTo (null, typeof (String)), "CCT#2");
95 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
96 Assert.IsFalse (rconv.CanConvertTo (null, typeof (RectangleF)), "CCT#4");
97 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Point)), "CCT#5");
98 Assert.IsFalse (rconv.CanConvertTo (null, typeof (PointF)), "CCT#6");
99 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Size)), "CCT#7");
100 Assert.IsFalse (rconv.CanConvertTo (null, typeof (SizeF)), "CCT#8");
101 Assert.IsFalse (rconv.CanConvertTo (null, typeof (Object)), "CCT#9");
102 Assert.IsFalse (rconv.CanConvertTo (null, typeof (int)), "CCT#10");
107 [NUnit.Framework.Category ("NotWorking")]
109 public void TestConvertFrom ()
111 Assert.AreEqual (rect, (Rectangle) rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
112 "10, 10, 20, 30"), "CF#1");
113 Assert.AreEqual (rectneg, (Rectangle) rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
114 "-10, -10, 20, 30"), "CF#2");
117 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
119 Assert.Fail ("CF#3: must throw ArgumentException");
120 } catch (Exception e) {
121 Assert.IsTrue (e is ArgumentException, "CF#3");
125 rconv.ConvertFrom ("10");
126 Assert.Fail ("CF#3a: must throw ArgumentException");
127 } catch (Exception e) {
128 Assert.IsTrue (e is ArgumentException, "CF#3a");
132 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
134 Assert.Fail ("CF#4: must throw ArgumentException");
135 } catch (Exception e) {
136 Assert.IsTrue (e is ArgumentException, "CF#4");
140 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
142 Assert.Fail ("CF#5: must throw Exception");
143 } catch (Exception ex) {
144 Assert.AreEqual (typeof (Exception), ex.GetType (), "CF#5-2");
145 Assert.IsNotNull (ex.InnerException, "CF#5-3");
146 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "CF#5-4");
150 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
151 new Rectangle (10, 10, 100, 100));
152 Assert.Fail ("CF#6: must throw NotSupportedException");
153 } catch (Exception e) {
154 Assert.IsTrue (e is NotSupportedException, "CF#6");
158 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
159 new RectangleF (10, 10, 100, 100));
160 Assert.Fail ("CF#7: must throw NotSupportedException");
161 } catch (Exception e) {
162 Assert.IsTrue (e is NotSupportedException, "CF#7");
166 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
168 Assert.Fail ("CF#8: must throw NotSupportedException");
169 } catch (Exception e) {
170 Assert.IsTrue (e is NotSupportedException, "CF#8");
174 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
175 new PointF (10, 10));
176 Assert.Fail ("CF#9: must throw NotSupportedException");
177 } catch (Exception e) {
178 Assert.IsTrue (e is NotSupportedException, "CF#9");
182 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
184 Assert.Fail ("CF#10: must throw NotSupportedException");
185 } catch (Exception e) {
186 Assert.IsTrue (e is NotSupportedException, "CF#10");
190 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
192 Assert.Fail ("CF#11: must throw NotSupportedException");
193 } catch (Exception e) {
194 Assert.IsTrue (e is NotSupportedException, "CF#11");
198 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
200 Assert.Fail ("CF#12: must throw NotSupportedException");
201 } catch (Exception e) {
202 Assert.IsTrue (e is NotSupportedException, "CF#12");
206 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 1001);
207 Assert.Fail ("CF#13: must throw NotSupportedException");
208 } catch (Exception e) {
209 Assert.IsTrue (e is NotSupportedException, "CF#13");
214 public void TestConvertTo ()
216 Assert.AreEqual (rectStrInvariant, (String) rconv.ConvertTo (null,
217 CultureInfo.InvariantCulture, rect, typeof (String)), "CT#1");
218 Assert.AreEqual (rectnegStrInvariant, (String) rconv.ConvertTo (null,
219 CultureInfo.InvariantCulture, rectneg, typeof (String)), "CT#2");
222 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
223 rect, typeof (Rectangle));
224 Assert.Fail ("CT#3: must throw NotSupportedException");
225 } catch (Exception e) {
226 Assert.IsTrue (e is NotSupportedException, "CT#3");
230 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
231 rect, typeof (RectangleF));
232 Assert.Fail ("CT#4: must throw NotSupportedException");
233 } catch (Exception e) {
234 Assert.IsTrue (e is NotSupportedException, "CT#4");
238 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
239 rect, typeof (Size));
240 Assert.Fail ("CT#5: must throw NotSupportedException");
241 } catch (Exception e) {
242 Assert.IsTrue (e is NotSupportedException, "CT#5");
246 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
247 rect, typeof (SizeF));
248 Assert.Fail ("CT#6: must throw NotSupportedException");
249 } catch (Exception e) {
250 Assert.IsTrue (e is NotSupportedException, "CT#6");
254 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
255 rect, typeof (Point));
256 Assert.Fail ("CT#7: must throw NotSupportedException");
257 } catch (Exception e) {
258 Assert.IsTrue (e is NotSupportedException, "CT#7");
262 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
263 rect, typeof (PointF));
264 Assert.Fail ("CT#8: must throw NotSupportedException");
265 } catch (Exception e) {
266 Assert.IsTrue (e is NotSupportedException, "CT#8");
270 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
271 rect, typeof (Object));
272 Assert.Fail ("CT#9: must throw NotSupportedException");
273 } catch (Exception e) {
274 Assert.IsTrue (e is NotSupportedException, "CT#9");
278 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
280 Assert.Fail ("CT#10: must throw NotSupportedException");
281 } catch (Exception e) {
282 Assert.IsTrue (e is NotSupportedException, "CT#10");
287 public void TestGetCreateInstanceSupported ()
289 Assert.IsTrue (rconv.GetCreateInstanceSupported (), "GCIS#1");
290 Assert.IsTrue (rconv.GetCreateInstanceSupported (null), "GCIS#2");
294 public void TestCreateInstance ()
296 Rectangle rectInstance;
298 Hashtable ht = new Hashtable ();
299 ht.Add ("X", 10); ht.Add ("Y", 10);
300 ht.Add ("Width", 20); ht.Add ("Height", 30);
302 rectInstance = (Rectangle) rconv.CreateInstance (ht);
303 Assert.AreEqual (rect, rectInstance, "CI#1");
306 ht.Add ("X", -10); ht.Add ("Y", -10);
307 ht.Add ("Width", 20); ht.Add ("Height", 30);
309 rectInstance = (Rectangle) rconv.CreateInstance (null, ht);
310 Assert.AreEqual (rectneg, rectInstance, "CI#2");
315 [ExpectedException (typeof (ArgumentException))]
317 [ExpectedException (typeof (NullReferenceException))]
319 public void TestCreateInstance_CaseSensitive ()
321 Hashtable ht = new Hashtable ();
324 ht.Add ("Width", 20);
325 ht.Add ("Height", 30);
326 rconv.CreateInstance (null, ht);
330 public void TestGetPropertiesSupported ()
332 Assert.IsTrue (rconv.GetPropertiesSupported (), "GPS#1");
333 Assert.IsTrue (rconv.GetPropertiesSupported (null), "GPS#2");
338 [NUnit.Framework.Category ("NotWorking")]
340 public void TestGetProperties ()
343 PropertyDescriptorCollection propsColl;
345 propsColl = rconv.GetProperties (rect);
346 Assert.AreEqual (4, propsColl.Count, "GP1#1");
347 Assert.AreEqual (rect.X, propsColl["X"].GetValue (rect), "GP1#2");
348 Assert.AreEqual (rect.Y, propsColl["Y"].GetValue (rect), "GP1#3");
349 Assert.AreEqual (rect.Width, propsColl["Width"].GetValue (rect), "GP1#4");
350 Assert.AreEqual (rect.Height, propsColl["Height"].GetValue (rect), "GP1#5");
352 propsColl = rconv.GetProperties (null, rectneg);
353 Assert.AreEqual (4, propsColl.Count, "GP2#1");
354 Assert.AreEqual (rectneg.X, propsColl["X"].GetValue (rectneg), "GP2#2");
355 Assert.AreEqual (rectneg.Y, propsColl["Y"].GetValue (rectneg), "GP2#3");
356 Assert.AreEqual (rectneg.Width, propsColl["Width"].GetValue (rectneg), "GP2#4");
357 Assert.AreEqual (rectneg.Height, propsColl["Height"].GetValue (rectneg), "GP2#5");
359 propsColl = rconv.GetProperties (null, rect, null);
360 Assert.AreEqual (11, propsColl.Count, "GP3#1");
361 Assert.AreEqual (rect.X, propsColl["X"].GetValue (rect), "GP3#2");
362 Assert.AreEqual (rect.Y, propsColl["Y"].GetValue (rect), "GP3#3");
363 Assert.AreEqual (rect.Width, propsColl["Width"].GetValue (rect), "GP3#4");
364 Assert.AreEqual (rect.Height, propsColl["Height"].GetValue (rect), "GP3#5");
366 Assert.AreEqual (rect.Top, propsColl["Top"].GetValue (rect), "GP3#6");
367 Assert.AreEqual (rect.Bottom, propsColl["Bottom"].GetValue (rect), "GP3#7");
368 Assert.AreEqual (rect.Left, propsColl["Left"].GetValue (rect), "GP3#8");
369 Assert.AreEqual (rect.Right, propsColl["Right"].GetValue (rect), "GP3#9");
370 Assert.AreEqual (rect.Location, propsColl["Location"].GetValue (rect), "GP3#10");
371 Assert.AreEqual (rect.Size, propsColl["Size"].GetValue (rect), "GP3#11");
372 Assert.AreEqual (rect.IsEmpty, propsColl["IsEmpty"].GetValue (rect), "GP3#12");
374 Type type = typeof (Rectangle);
375 attrs = Attribute.GetCustomAttributes (type, true);
376 propsColl = rconv.GetProperties (null, rect, attrs);
377 Assert.AreEqual (0, propsColl.Count, "GP3#13");
381 public void ConvertFromInvariantString_string ()
383 Assert.AreEqual (rect, rconv.ConvertFromInvariantString (rectStrInvariant),
385 Assert.AreEqual (rectneg, rconv.ConvertFromInvariantString (rectnegStrInvariant),
390 [ExpectedException (typeof (ArgumentException))]
391 public void ConvertFromInvariantString_string_exc_1 ()
393 rconv.ConvertFromInvariantString ("1, 2, 3");
398 [NUnit.Framework.Category ("NotWorking")]
400 public void ConvertFromInvariantString_string_exc_2 ()
403 rconv.ConvertFromInvariantString ("hello");
405 } catch (Exception ex) {
406 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
407 Assert.IsNotNull (ex.InnerException, "#3");
408 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
414 [NUnit.Framework.Category ("NotWorking")]
416 public void ConvertFromString_string ()
418 // save current culture
419 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
422 PerformConvertFromStringTest (new CultureInfo ("en-US"));
423 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
424 PerformConvertFromStringTest (new MyCultureInfo ());
426 // restore original culture
427 Thread.CurrentThread.CurrentCulture = currentCulture;
432 [ExpectedException (typeof (ArgumentException))]
433 public void ConvertFromString_string_exc_1 ()
435 CultureInfo culture = CultureInfo.CurrentCulture;
436 rconv.ConvertFromString (string.Format(culture,
437 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator));
442 [NUnit.Framework.Category ("NotWorking")]
444 public void ConvertFromString_string_exc_2 ()
447 rconv.ConvertFromString ("hello");
449 } catch (Exception ex) {
450 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
451 Assert.IsNotNull (ex.InnerException, "#3");
452 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
457 public void ConvertToInvariantString_string ()
459 Assert.AreEqual (rectStrInvariant, rconv.ConvertToInvariantString (rect),
461 Assert.AreEqual (rectnegStrInvariant, rconv.ConvertToInvariantString (rectneg),
466 public void ConvertToString_string () {
467 // save current culture
468 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
471 PerformConvertToStringTest (new CultureInfo ("en-US"));
472 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
473 PerformConvertToStringTest (new MyCultureInfo ());
475 // restore original culture
476 Thread.CurrentThread.CurrentCulture = currentCulture;
481 public void GetStandardValuesSupported ()
483 Assert.IsFalse (rconv.GetStandardValuesSupported ());
487 public void GetStandardValues ()
489 Assert.IsNull (rconv.GetStandardValues ());
493 public void GetStandardValuesExclusive ()
495 Assert.IsFalse (rconv.GetStandardValuesExclusive ());
498 private void PerformConvertFromStringTest (CultureInfo culture)
500 // set current culture
501 Thread.CurrentThread.CurrentCulture = culture;
504 Assert.AreEqual (rect, rconv.ConvertFromString (CreateRectangleString (rect)),
505 "CFSS#1-" + culture.Name);
506 Assert.AreEqual (rectneg, rconv.ConvertFromString (CreateRectangleString (rectneg)),
507 "CFSS#2-" + culture.Name);
510 private void PerformConvertToStringTest (CultureInfo culture)
512 // set current culture
513 Thread.CurrentThread.CurrentCulture = culture;
516 Assert.AreEqual (CreateRectangleString (rect), rconv.ConvertToString (rect),
517 "CFISS#1-" + culture.Name);
518 Assert.AreEqual (CreateRectangleString (rectneg), rconv.ConvertToString (rectneg),
519 "CFISS#2-" + culture.Name);
522 private static string CreateRectangleString (Rectangle rectangle)
524 return CreateRectangleString (CultureInfo.CurrentCulture, rectangle);
527 private static string CreateRectangleString (CultureInfo culture, Rectangle rectangle)
529 return string.Format ("{0}{1} {2}{1} {3}{1} {4}", rectangle.X.ToString (culture),
530 culture.TextInfo.ListSeparator, rectangle.Y.ToString (culture),
531 rectangle.Width.ToString (culture), rectangle.Height.ToString (culture));
535 private sealed class MyCultureInfo : CultureInfo
537 internal MyCultureInfo ()
542 public override object GetFormat (Type formatType)
544 if (formatType == typeof (NumberFormatInfo)) {
545 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
547 nfi.NegativeSign = "myNegativeSign";
548 return NumberFormatInfo.ReadOnly (nfi);
550 return base.GetFormat (formatType);