2 // Tests for System.Drawing.SizeConverter.cs
5 // Ravindra (rkumar@novell.com)
7 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
10 using NUnit.Framework;
13 using System.Collections;
14 using System.ComponentModel;
15 using System.Globalization;
17 namespace MonoTests.System.Drawing
20 public class SizeConverterTest : Assertion
29 public void TearDown () {}
34 sz = new Size (10, 20);
35 szStr = sz.Width + ", " + sz.Height;
37 szneg = new Size (-20, -30);
38 sznegStr = szneg.Width + ", " + szneg.Height;
40 szconv = (SizeConverter) TypeDescriptor.GetConverter (sz);
44 public void TestCanConvertFrom ()
46 Assert ("CCF#1", szconv.CanConvertFrom (typeof (String)));
47 Assert ("CCF#1a", szconv.CanConvertFrom (null, typeof (String)));
48 Assert ("CCF#2", ! szconv.CanConvertFrom (null, typeof (Rectangle)));
49 Assert ("CCF#3", ! szconv.CanConvertFrom (null, typeof (RectangleF)));
50 Assert ("CCF#4", ! szconv.CanConvertFrom (null, typeof (Point)));
51 Assert ("CCF#5", ! szconv.CanConvertFrom (null, typeof (PointF)));
52 Assert ("CCF#6", ! szconv.CanConvertFrom (null, typeof (Size)));
53 Assert ("CCF#7", ! szconv.CanConvertFrom (null, typeof (SizeF)));
54 Assert ("CCF#8", ! szconv.CanConvertFrom (null, typeof (Object)));
55 Assert ("CCF#9", ! szconv.CanConvertFrom (null, typeof (int)));
59 public void TestCanConvertTo ()
61 Assert ("CCT#1", szconv.CanConvertTo (typeof (String)));
62 Assert ("CCT#1a", szconv.CanConvertTo (null, typeof (String)));
63 Assert ("CCT#2", ! szconv.CanConvertTo (null, typeof (Rectangle)));
64 Assert ("CCT#3", ! szconv.CanConvertTo (null, typeof (RectangleF)));
65 Assert ("CCT#4", ! szconv.CanConvertTo (null, typeof (Point)));
66 Assert ("CCT#5", ! szconv.CanConvertTo (null, typeof (PointF)));
67 Assert ("CCT#6", ! szconv.CanConvertTo (null, typeof (Size)));
68 Assert ("CCT#7", ! szconv.CanConvertTo (null, typeof (SizeF)));
69 Assert ("CCT#8", ! szconv.CanConvertTo (null, typeof (Object)));
70 Assert ("CCT#9", ! szconv.CanConvertTo (null, typeof (int)));
74 public void TestConvertFrom ()
76 AssertEquals ("CF#1", sz, (Size) szconv.ConvertFrom (null,
77 CultureInfo.InvariantCulture,
79 AssertEquals ("CF#2", szneg, (Size) szconv.ConvertFrom (null,
80 CultureInfo.InvariantCulture,
84 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10");
85 Fail ("CF#3: must throw ArgumentException");
86 } catch (Exception e) {
87 Assert ("CF#3", e is ArgumentException);
91 szconv.ConvertFrom ("10");
92 Fail ("CF#3a: must throw ArgumentException");
93 } catch (Exception e) {
94 Assert ("CF#3a", e is ArgumentException);
98 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
100 Fail ("CF#4: must throw ArgumentException");
101 } catch (Exception e) {
102 Assert ("CF#4", e is ArgumentException);
106 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
108 Fail ("CF#5: must throw Exception");
113 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
115 Fail ("CF#6: must throw NotSupportedException");
116 } catch (Exception e) {
117 Assert ("CF#6", e is NotSupportedException);
121 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
122 new PointF (10, 10));
123 Fail ("CF#7: must throw NotSupportedException");
124 } catch (Exception e) {
125 Assert ("CF#7", e is NotSupportedException);
129 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
131 Fail ("CF#8: must throw NotSupportedException");
132 } catch (Exception e) {
133 Assert ("CF#8", e is NotSupportedException);
137 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
139 Fail ("CF#9: must throw NotSupportedException");
140 } catch (Exception e) {
141 Assert ("CF#9", e is NotSupportedException);
145 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
146 Fail ("CF#10: must throw NotSupportedException");
147 } catch (Exception e) {
148 Assert ("CF#10", e is NotSupportedException);
153 public void TestConvertTo ()
155 AssertEquals ("CT#1", szStr, (String) szconv.ConvertTo (null,
156 CultureInfo.InvariantCulture,
157 sz, typeof (String)));
158 AssertEquals ("CT#2", sznegStr, (String) szconv.ConvertTo (null,
159 CultureInfo.InvariantCulture, szneg,
163 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
165 Fail ("CT#3: must throw NotSupportedException");
166 } catch (Exception e) {
167 Assert ("CT#3", e is NotSupportedException);
171 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
173 Fail ("CT#4: must throw NotSupportedException");
174 } catch (Exception e) {
175 Assert ("CT#4", e is NotSupportedException);
179 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
181 Fail ("CT#5: must throw NotSupportedException");
182 } catch (Exception e) {
183 Assert ("CT#5", e is NotSupportedException);
187 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
189 Fail ("CT#6: must throw NotSupportedException");
190 } catch (Exception e) {
191 Assert ("CT#6", e is NotSupportedException);
195 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
197 Fail ("CT#7: must throw NotSupportedException");
198 } catch (Exception e) {
199 Assert ("CT#7", e is NotSupportedException);
204 public void TestGetCreateInstanceSupported ()
206 Assert ("GCIS#1", szconv.GetCreateInstanceSupported ());
207 Assert ("GCIS#2", szconv.GetCreateInstanceSupported (null));
211 public void TestCreateInstance ()
215 Hashtable ht = new Hashtable ();
216 ht.Add ("Width", 10); ht.Add ("Height", 20);
218 szInstance = (Size) szconv.CreateInstance (ht);
219 AssertEquals ("CI#1", sz, szInstance);
222 ht.Add ("Width", -20); ht.Add ("Height", -30);
224 szInstance = (Size) szconv.CreateInstance (null, ht);
225 AssertEquals ("CI#2", szneg, szInstance);
227 // Property names are case-sensitive. It should throw
228 // NullRefExc if any of the property names does not match
230 ht.Add ("width", 20); ht.Add ("Height", 30);
232 szInstance = (Size) szconv.CreateInstance (null, ht);
233 Fail ("CI#3: must throw NullReferenceException");
234 } catch (Exception e) {
235 Assert ("CI#3", e is NullReferenceException);
240 public void TestGetPropertiesSupported ()
242 Assert ("GPS#1", szconv.GetPropertiesSupported ());
243 Assert ("GPS#2", szconv.GetPropertiesSupported (null));
247 public void TestGetProperties ()
250 PropertyDescriptorCollection propsColl;
252 propsColl = szconv.GetProperties (sz);
253 AssertEquals ("GP1#1", 2, propsColl.Count);
254 AssertEquals ("GP1#2", sz.Width, propsColl ["Width"].GetValue (sz));
255 AssertEquals ("GP1#3", sz.Height, propsColl ["Height"].GetValue (sz));
257 propsColl = szconv.GetProperties (null, szneg);
258 AssertEquals ("GP2#1", 2, propsColl.Count);
259 AssertEquals ("GP2#2", szneg.Width, propsColl ["Width"].GetValue (szneg));
260 AssertEquals ("GP2#3", szneg.Height, propsColl ["Height"].GetValue (szneg));
262 propsColl = szconv.GetProperties (null, sz, null);
263 AssertEquals ("GP3#1", 3, propsColl.Count);
264 AssertEquals ("GP3#2", sz.Width, propsColl ["Width"].GetValue (sz));
265 AssertEquals ("GP3#3", sz.Height, propsColl ["Height"].GetValue (sz));
266 AssertEquals ("GP3#4", sz.IsEmpty, propsColl ["IsEmpty"].GetValue (sz));
268 Type type = typeof (Size);
269 attrs = Attribute.GetCustomAttributes (type, true);
270 propsColl = szconv.GetProperties (null, sz, attrs);
271 AssertEquals ("GP3#5", 0, propsColl.Count);