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);
67 public void TestCanConvertFrom ()
69 Assert.IsTrue (szconv.CanConvertFrom (typeof (String)), "CCF#1");
70 Assert.IsTrue (szconv.CanConvertFrom (null, typeof (String)), "CCF#2");
71 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
72 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (RectangleF)), "CCF#4");
73 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Point)), "CCF#5");
74 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (PointF)), "CCF#6");
75 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Size)), "CCF#7");
76 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (SizeF)), "CCF#8");
77 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (Object)), "CCF#9");
78 Assert.IsFalse (szconv.CanConvertFrom (null, typeof (int)), "CCF#10");
79 Assert.IsTrue (szconv.CanConvertFrom (null, typeof (InstanceDescriptor)), "CCF#11");
83 public void TestCanConvertTo ()
85 Assert.IsTrue (szconv.CanConvertTo (typeof (String)), "CCT#1");
86 Assert.IsTrue (szconv.CanConvertTo (null, typeof (String)), "CCT#2");
87 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
88 Assert.IsFalse (szconv.CanConvertTo (null, typeof (RectangleF)), "CCT#4");
89 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Point)), "CCT#5");
90 Assert.IsFalse (szconv.CanConvertTo (null, typeof (PointF)), "CCT#6");
91 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Size)), "CCT#7");
92 Assert.IsFalse (szconv.CanConvertTo (null, typeof (SizeF)), "CCT#8");
93 Assert.IsFalse (szconv.CanConvertTo (null, typeof (Object)), "CCT#9");
94 Assert.IsFalse (szconv.CanConvertTo (null, typeof (int)), "CCT#10");
98 public void TestConvertFrom ()
100 Assert.AreEqual (sz, (Size) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
102 Assert.AreEqual (szneg, (Size) szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
103 "-20, -30"), "CF#2");
106 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10");
107 Assert.Fail ("CF#3: must throw ArgumentException");
108 } catch (Exception e) {
109 Assert.IsTrue (e is ArgumentException, "CF#3");
113 szconv.ConvertFrom ("10");
114 Assert.Fail ("CF#3a: must throw ArgumentException");
115 } catch (Exception e) {
116 Assert.IsTrue (e is ArgumentException, "CF#3a");
120 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
122 Assert.Fail ("CF#4: must throw ArgumentException");
123 } catch (Exception e) {
124 Assert.IsTrue (e is ArgumentException, "CF#4");
128 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
130 Assert.Fail ("CF#5-1: must throw Exception");
131 } catch (Exception ex) {
132 Assert.AreEqual (typeof (Exception), ex.GetType (), "CF#5-2");
133 Assert.IsNotNull (ex.InnerException, "CF#5-3");
134 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "CF#5-4");
138 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
140 Assert.Fail ("CF#6: must throw NotSupportedException");
141 } catch (Exception e) {
142 Assert.IsTrue (e is NotSupportedException, "CF#6");
146 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
147 new PointF (10, 10));
148 Assert.Fail ("CF#7: must throw NotSupportedException");
149 } catch (Exception e) {
150 Assert.IsTrue (e is NotSupportedException, "CF#7");
154 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
156 Assert.Fail ("CF#8: must throw NotSupportedException");
157 } catch (Exception e) {
158 Assert.IsTrue (e is NotSupportedException, "CF#8");
162 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
164 Assert.Fail ("CF#9: must throw NotSupportedException");
165 } catch (Exception e) {
166 Assert.IsTrue (e is NotSupportedException, "CF#9");
170 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
171 Assert.Fail ("CF#10: must throw NotSupportedException");
172 } catch (Exception e) {
173 Assert.IsTrue (e is NotSupportedException, "CF#10");
178 public void TestConvertTo ()
180 Assert.AreEqual (szStrInvariant, (String) szconv.ConvertTo (null,
181 CultureInfo.InvariantCulture, sz, typeof (String)), "CT#1");
182 Assert.AreEqual (sznegStrInvariant, (String) szconv.ConvertTo (null,
183 CultureInfo.InvariantCulture, szneg, typeof (String)), "CT#2");
186 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
188 Assert.Fail ("CT#3: must throw NotSupportedException");
189 } catch (Exception e) {
190 Assert.IsTrue (e is NotSupportedException, "CT#3");
194 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
196 Assert.Fail ("CT#4: must throw NotSupportedException");
197 } catch (Exception e) {
198 Assert.IsTrue (e is NotSupportedException, "CT#4");
202 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
204 Assert.Fail ("CT#5: must throw NotSupportedException");
205 } catch (Exception e) {
206 Assert.IsTrue (e is NotSupportedException, "CT#5");
210 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
212 Assert.Fail ("CT#6: must throw NotSupportedException");
213 } catch (Exception e) {
214 Assert.IsTrue (e is NotSupportedException, "CT#6");
218 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
220 Assert.Fail ("CT#7: must throw NotSupportedException");
221 } catch (Exception e) {
222 Assert.IsTrue (e is NotSupportedException, "CT#7");
227 szconv.ConvertTo (null, null, sz, typeof (string));
228 } catch (NullReferenceException e) {
229 Assert.Fail ("CT#8: must not throw NullReferenceException");
234 public void TestGetCreateInstanceSupported ()
236 Assert.IsTrue (szconv.GetCreateInstanceSupported (), "GCIS#1");
237 Assert.IsTrue (szconv.GetCreateInstanceSupported (null), "GCIS#2");
241 public void TestCreateInstance ()
245 Hashtable ht = new Hashtable ();
246 ht.Add ("Width", 10); ht.Add ("Height", 20);
248 szInstance = (Size) szconv.CreateInstance (ht);
249 Assert.AreEqual (sz, szInstance, "CI#1");
252 ht.Add ("Width", -20); ht.Add ("Height", -30);
254 szInstance = (Size) szconv.CreateInstance (null, ht);
255 Assert.AreEqual (szneg, szInstance, "CI#2");
259 [ExpectedException (typeof (ArgumentException))]
260 public void TestCreateInstance_CaseSensitive ()
262 Hashtable ht = new Hashtable ();
263 ht.Add ("width", 20);
264 ht.Add ("Height", 30);
265 szconv.CreateInstance (null, ht);
269 public void TestGetPropertiesSupported ()
271 Assert.IsTrue (szconv.GetPropertiesSupported (), "GPS#1");
272 Assert.IsTrue (szconv.GetPropertiesSupported (null), "GPS#2");
276 public void TestGetProperties ()
279 PropertyDescriptorCollection propsColl;
281 propsColl = szconv.GetProperties (sz);
282 Assert.AreEqual (2, propsColl.Count, "GP1#1");
283 Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP1#2");
284 Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP1#3");
286 propsColl = szconv.GetProperties (null, szneg);
287 Assert.AreEqual (2, propsColl.Count, "GP2#1");
288 Assert.AreEqual (szneg.Width, propsColl["Width"].GetValue (szneg), "GP2#2");
289 Assert.AreEqual (szneg.Height, propsColl["Height"].GetValue (szneg), "GP2#3");
291 propsColl = szconv.GetProperties (null, sz, null);
292 Assert.AreEqual (3, propsColl.Count, "GP3#1");
293 Assert.AreEqual (sz.Width, propsColl["Width"].GetValue (sz), "GP3#2");
294 Assert.AreEqual (sz.Height, propsColl["Height"].GetValue (sz), "GP3#3");
295 Assert.AreEqual (sz.IsEmpty, propsColl["IsEmpty"].GetValue (sz), "GP3#4");
297 Type type = typeof (Size);
298 attrs = Attribute.GetCustomAttributes (type, true);
299 propsColl = szconv.GetProperties (null, sz, attrs);
300 Assert.AreEqual (0, propsColl.Count, "GP3#5");
304 public void ConvertFromInvariantString_string ()
306 Assert.AreEqual (sz, szconv.ConvertFromInvariantString (szStrInvariant),
308 Assert.AreEqual (szneg, szconv.ConvertFromInvariantString (sznegStrInvariant),
313 [ExpectedException (typeof (ArgumentException))]
314 public void ConvertFromInvariantString_string_exc_1 ()
316 szconv.ConvertFromInvariantString ("1, 2, 3");
320 public void ConvertFromInvariantString_string_exc_2 ()
323 szconv.ConvertFromInvariantString ("hello");
325 } catch (Exception ex) {
326 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
327 Assert.IsNotNull (ex.InnerException, "#3");
328 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
333 public void ConvertFromString_string ()
335 // save current culture
336 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
339 PerformConvertFromStringTest (new CultureInfo ("en-US"));
340 PerformConvertFromStringTest (new CultureInfo ("nl-BE"));
341 PerformConvertFromStringTest (new MyCultureInfo ());
343 // restore original culture
344 Thread.CurrentThread.CurrentCulture = currentCulture;
349 [ExpectedException (typeof (ArgumentException))]
350 public void ConvertFromString_string_exc_1 ()
352 CultureInfo culture = CultureInfo.CurrentCulture;
353 szconv.ConvertFromString (string.Format(culture,
354 "1{0} 2{0} 3{0} 4{0} 5", culture.TextInfo.ListSeparator));
358 public void ConvertFromString_string_exc_2 ()
361 szconv.ConvertFromString ("hello");
363 } catch (Exception ex) {
364 Assert.AreEqual (typeof (Exception), ex.GetType (), "#2");
365 Assert.IsNotNull (ex.InnerException, "#3");
366 Assert.AreEqual (typeof (FormatException), ex.InnerException.GetType (), "#3");
371 public void ConvertToInvariantString_string ()
373 Assert.AreEqual (szStrInvariant, szconv.ConvertToInvariantString (sz),
375 Assert.AreEqual (sznegStrInvariant, szconv.ConvertToInvariantString (szneg),
380 public void ConvertToString_string ()
382 // save current culture
383 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
386 PerformConvertToStringTest (new CultureInfo ("en-US"));
387 PerformConvertToStringTest (new CultureInfo ("nl-BE"));
388 PerformConvertToStringTest (new MyCultureInfo ());
390 // restore original culture
391 Thread.CurrentThread.CurrentCulture = currentCulture;
396 public void GetStandardValuesSupported ()
398 Assert.IsFalse (szconv.GetStandardValuesSupported ());
402 public void GetStandardValues ()
404 Assert.IsNull (szconv.GetStandardValues ());
408 public void GetStandardValuesExclusive ()
410 Assert.IsFalse (szconv.GetStandardValuesExclusive ());
413 private void PerformConvertFromStringTest (CultureInfo culture)
415 // set current culture
416 Thread.CurrentThread.CurrentCulture = culture;
419 Assert.AreEqual (sz, szconv.ConvertFromString (CreateSizeString (culture, sz)),
420 "CFSS#1-" + culture.Name);
421 Assert.AreEqual (szneg, szconv.ConvertFromString (CreateSizeString (culture, szneg)),
422 "CFSS#2-" + culture.Name);
425 private void PerformConvertToStringTest (CultureInfo culture)
427 // set current culture
428 Thread.CurrentThread.CurrentCulture = culture;
431 Assert.AreEqual (CreateSizeString (culture, sz), szconv.ConvertToString (sz),
432 "CFISS#1-" + culture.Name);
433 Assert.AreEqual (CreateSizeString (culture, szneg), szconv.ConvertToString (szneg),
434 "CFISS#2-" + culture.Name);
437 private static string CreateSizeString (Size size)
439 return CreateSizeString (CultureInfo.CurrentCulture, size);
442 private static string CreateSizeString (CultureInfo culture, Size size)
444 return string.Format ("{0}{1} {2}", size.Width.ToString (culture),
445 culture.TextInfo.ListSeparator, size.Height.ToString (culture));
449 private sealed class MyCultureInfo : CultureInfo
451 internal MyCultureInfo () : base ("en-US")
455 public override object GetFormat (Type formatType)
457 if (formatType == typeof (NumberFormatInfo)) {
458 NumberFormatInfo nfi = (NumberFormatInfo) ((NumberFormatInfo) base.GetFormat (formatType)).Clone ();
460 nfi.NegativeSign = "myNegativeSign";
461 return NumberFormatInfo.ReadOnly (nfi);
463 return base.GetFormat (formatType);