2 // Tests for System.Drawing.SizeConverter.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 SizeConverterTest
51 String szStrInvariant;
52 String sznegStrInvariant;
57 sz = new Size (10, 20);
58 szStrInvariant = sz.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + sz.Height;
60 szneg = new Size (-20, -30);
61 sznegStrInvariant = szneg.Width + CultureInfo.InvariantCulture.TextInfo.ListSeparator + " " + szneg.Height;
63 szconv = (SizeConverter) TypeDescriptor.GetConverter (sz);
68 [NUnit.Framework.Category ("NotWorking")]
70 public void TestCanConvertFrom ()
72 Assert.IsTrue (szconv.CanConvertFrom (typeof (String)), "CCF#1");
73 Assert.IsTrue (szconv.CanConvertFrom (null, typeof (String)), "CCF#2");
74 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
75 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#4");
76 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Point)), "CCF#5");
77 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (PointF)), "CCF#6");
78 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Size)), "CCF#7");
79 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (SizeF)), "CCF#8");
80 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Object)), "CCF#9");
81 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (int)), "CCF#10");
82 Assert.IsTrue (szconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#11");
86 public void TestCanConvertTo ()
88 Assert.IsTrue (szconv.CanConvertTo (typeof (String)), "CCT#1");
89 Assert.IsTrue (szconv.CanConvertTo (null, typeof (String)), "CCT#2");
90 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
91 Assert.IsFalse (szconv.CanConvertTo (null, typeof (RectangleF)), "CCT#4");
92 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Point)), "CCT#5");
93 Assert.IsFalse (szconv.CanConvertTo (null, typeof (PointF)), "CCT#6");
94 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Size)), "CCT#7");
95 Assert.IsFalse (szconv.CanConvertTo (null, typeof (SizeF)), "CCT#8");
96 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Object)), "CCT#9");
97 Assert.IsFalse (szconv.CanConvertTo (null, typeof (int)), "CCT#10");
102 [NUnit.Framework.Category ("NotWorking")]
104 public void TestConvertFrom ()
106 Assert.AreEqual (sz, (Size) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
108 Assert.AreEqual (szneg, (Size) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
109 "-20, -30"), "CF#2");
112 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10");
113 Assert.Fail ("CF#3: must throw ArgumentException");
114 } catch (Exception e) {
115 Assert.IsTrue (e is ArgumentException, "CF#3");
119 szconv.ConvertFrom ("10");
120 Assert.Fail ("CF#3a: must throw ArgumentException");
121 } catch (Exception e) {
122 Assert.IsTrue (e is ArgumentException, "CF#3a");
126 szconv.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 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
136 Assert.Fail ("CF#5-1: 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 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
146 Assert.Fail ("CF#6: must throw NotSupportedException");
147 } catch (Exception e) {
148 Assert.IsTrue (e is NotSupportedException, "CF#6");
152 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
153 new PointF (10, 10));
154 Assert.Fail ("CF#7: must throw NotSupportedException");
155 } catch (Exception e) {
156 Assert.IsTrue (e is NotSupportedException, "CF#7");
160 szconv.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 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
170 Assert.Fail ("CF#9: must throw NotSupportedException");
171 } catch (Exception e) {
172 Assert.IsTrue (e is NotSupportedException, "CF#9");
176 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
177 Assert.Fail ("CF#10: must throw NotSupportedException");
178 } catch (Exception e) {
179 Assert.IsTrue (e is NotSupportedException, "CF#10");
184 public void TestConvertTo ()
186 Assert.AreEqual (szStrInvariant, (String) szconv.ConvertTo (null,
187 CultureInfo.InvariantCulture, sz, typeof (String)), "CT#1");
188 Assert.AreEqual (sznegStrInvariant, (String) szconv.ConvertTo (null,
189 CultureInfo.InvariantCulture, szneg, typeof (String)), "CT#2");
192 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
194 Assert.Fail ("CT#3: must throw NotSupportedException");
195 } catch (Exception e) {
196 Assert.IsTrue (e is NotSupportedException, "CT#3");
200 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
202 Assert.Fail ("CT#4: must throw NotSupportedException");
203 } catch (Exception e) {
204 Assert.IsTrue (e is NotSupportedException, "CT#4");
208 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
210 Assert.Fail ("CT#5: must throw NotSupportedException");
211 } catch (Exception e) {
212 Assert.IsTrue (e is NotSupportedException, "CT#5");
216 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
218 Assert.Fail ("CT#6: must throw NotSupportedException");
219 } catch (Exception e) {
220 Assert.IsTrue (e is NotSupportedException, "CT#6");
224 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
226 Assert.Fail ("CT#7: must throw NotSupportedException");
227 } catch (Exception e) {
228 Assert.IsTrue (e is NotSupportedException, "CT#7");
233 public void TestGetCreateInstanceSupported ()
235 Assert.IsTrue (szconv.GetCreateInstanceSupported (), "GCIS#1");
236 Assert.IsTrue (szconv.GetCreateInstanceSupported (null), "GCIS#2");
240 public void TestCreateInstance ()
244 Hashtable ht = new Hashtable ();
245 ht.Add ("Width", 10); ht.Add ("Height", 20);
247 szInstance = (Size) szconv.CreateInstance (ht);
248 Assert.AreEqual (sz, szInstance, "CI#1");
251 ht.Add ("Width", -20); ht.Add ("Height", -30);
253 szInstance = (Size) szconv.CreateInstance (null, ht);
254 Assert.AreEqual (szneg, szInstance, "CI#2");
259 [ExpectedException (typeof (ArgumentException))]
261 [ExpectedException (typeof (NullReferenceException))]
263 public void TestCreateInstance_CaseSensitive ()
265 Hashtable ht = new Hashtable ();
266 ht.Add ("width", 20);
267 ht.Add ("Height", 30);
268 szconv.CreateInstance (null, ht);
272 public void TestGetPropertiesSupported ()
274 Assert.IsTrue (szconv.GetPropertiesSupported (), "GPS#1");
275 Assert.IsTrue (szconv.GetPropertiesSupported (null), "GPS#2");
280 [NUnit.Framework.Category ("NotWorking")]
282 public void TestGetProperties ()
285 PropertyDescriptorCollection propsColl;
287 propsColl = szconv.GetProperties (sz);
288 Assert.AreEqual (2, propsColl.Count, "GP1#1");
289 Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP1#2");
290 Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP1#3");
292 propsColl = szconv.GetProperties (null, szneg);
293 Assert.AreEqual (2, propsColl.Count, "GP2#1");
294 Assert.AreEqual (szneg.Width, propsColl["Width"].GetValue (szneg), "GP2#2");
295 Assert.AreEqual (szneg.Height, propsColl["Height"].GetValue (szneg), "GP2#3");
297 propsColl = szconv.GetProperties (null, sz, null);
298 Assert.AreEqual (3, propsColl.Count, "GP3#1");
299 Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP3#2");
300 Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP3#3");
301 Assert.AreEqual (sz.IsEmpty, propsColl["IsEmpty"].GetValue (sz), "GP3#4");
303 Type type = typeof (Size);
304 attrs = Attribute.GetCustomAttributes (type, true);
305 propsColl = szconv.GetProperties (null, sz, attrs);
306 Assert.AreEqual (0, propsColl.Count, "GP3#5");
310 public void ConvertFromInvariantString_string ()
312 Assert.AreEqual (sz, szconv.ConvertFromInvariantString (szStrInvariant),
314 Assert.AreEqual (szneg, szconv.ConvertFromInvariantString (sznegStrInvariant),
319 [ExpectedException (typeof (ArgumentException))]
320 public void ConvertFromInvariantString_string_exc_1 ()
322 szconv.ConvertFromInvariantString ("1, 2, 3");
327 [NUnit.Framework.Category ("NotWorking")]
329 public void ConvertFromInvariantString_string_exc_2 ()
332 szconv.ConvertFromInvariantString ("hello");
334 } catch (Exception ex) {
335 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
336 Assert.IsNotNull (ex.InnerException, "#3");
337 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
343 [NUnit.Framework.Category ("NotWorking")]
345 public void ConvertFromString_string ()
347 // save current culture
348 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
351 PerformConvertFromStringTest (new CultureInfo ("en-US"));
352 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
353 PerformConvertFromStringTest (new MyCultureInfo ());
355 // restore original culture
356 Thread.CurrentThread.CurrentCulture = currentCulture;
361 [ExpectedException (typeof (ArgumentException))]
362 public void ConvertFromString_string_exc_1 ()
364 CultureInfo culture = CultureInfo.CurrentCulture;
365 szconv.ConvertFromString (string.Format(culture,
366 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator));
371 [NUnit.Framework.Category ("NotWorking")]
373 public void ConvertFromString_string_exc_2 ()
376 szconv.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 (szStrInvariant, szconv.ConvertToInvariantString (sz),
390 Assert.AreEqual (sznegStrInvariant, szconv.ConvertToInvariantString (szneg),
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 (szconv.GetStandardValuesSupported ());
417 public void GetStandardValues ()
419 Assert.IsNull (szconv.GetStandardValues ());
423 public void GetStandardValuesExclusive ()
425 Assert.IsFalse (szconv.GetStandardValuesExclusive ());
428 private void PerformConvertFromStringTest (CultureInfo culture)
430 // set current culture
431 Thread.CurrentThread.CurrentCulture = culture;
434 Assert.AreEqual (sz, szconv.ConvertFromString (CreateSizeString (culture, sz)),
435 "CFSS#1-" + culture.Name);
436 Assert.AreEqual (szneg, szconv.ConvertFromString (CreateSizeString (culture, szneg)),
437 "CFSS#2-" + culture.Name);
440 private void PerformConvertToStringTest (CultureInfo culture)
442 // set current culture
443 Thread.CurrentThread.CurrentCulture = culture;
446 Assert.AreEqual (CreateSizeString (culture, sz), szconv.ConvertToString (sz),
447 "CFISS#1-" + culture.Name);
448 Assert.AreEqual (CreateSizeString (culture, szneg), szconv.ConvertToString (szneg),
449 "CFISS#2-" + culture.Name);
452 private static string CreateSizeString (Size size)
454 return CreateSizeString (CultureInfo.CurrentCulture, size);
457 private static string CreateSizeString (CultureInfo culture, Size size)
459 return string.Format ("{0}{1} {2}", size.Width.ToString (culture),
460 culture.TextInfo.ListSeparator, size.Height.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);