2 // Tests for System.Drawing.RectangleConverter.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 RectangleConverterTest : Assertion
24 RectangleConverter rconv;
29 public void TearDown () {}
34 rect = new Rectangle (10, 10, 20, 30);
35 rectStr = rect.X + ", " + rect.Y + ", " + rect.Width + ", " + rect.Height;
37 rectneg = new Rectangle (-10, -10, 20, 30);
38 rectnegStr = rectneg.X + ", " + rectneg.Y + ", " + rectneg.Width + ", " + rectneg.Height;
40 rconv = (RectangleConverter) TypeDescriptor.GetConverter (rect);
44 public void TestCanConvertFrom ()
46 Assert ("CCF#1", rconv.CanConvertFrom (typeof (String)));
47 Assert ("CCF#1a", rconv.CanConvertFrom (null, typeof (String)));
48 Assert ("CCF#2", ! rconv.CanConvertFrom (null, typeof (Rectangle)));
49 Assert ("CCF#3", ! rconv.CanConvertFrom (null, typeof (RectangleF)));
50 Assert ("CCF#4", ! rconv.CanConvertFrom (null, typeof (Point)));
51 Assert ("CCF#5", ! rconv.CanConvertFrom (null, typeof (PointF)));
52 Assert ("CCF#6", ! rconv.CanConvertFrom (null, typeof (Size)));
53 Assert ("CCF#7", ! rconv.CanConvertFrom (null, typeof (SizeF)));
54 Assert ("CCF#8", ! rconv.CanConvertFrom (null, typeof (Object)));
55 Assert ("CCF#9", ! rconv.CanConvertFrom (null, typeof (int)));
59 public void TestCanConvertTo ()
61 Assert ("CCT#1", rconv.CanConvertTo (typeof (String)));
62 Assert ("CCT#1a", rconv.CanConvertTo (null, typeof (String)));
63 Assert ("CCT#2", ! rconv.CanConvertTo (null, typeof (Rectangle)));
64 Assert ("CCT#3", ! rconv.CanConvertTo (null, typeof (RectangleF)));
65 Assert ("CCT#4", ! rconv.CanConvertTo (null, typeof (Point)));
66 Assert ("CCT#5", ! rconv.CanConvertTo (null, typeof (PointF)));
67 Assert ("CCT#6", ! rconv.CanConvertTo (null, typeof (Size)));
68 Assert ("CCT#7", ! rconv.CanConvertTo (null, typeof (SizeF)));
69 Assert ("CCT#8", ! rconv.CanConvertTo (null, typeof (Object)));
70 Assert ("CCT#9", ! rconv.CanConvertTo (null, typeof (int)));
74 public void TestConvertFrom ()
76 AssertEquals ("CF#1", rect, (Rectangle) rconv.ConvertFrom (null,
77 CultureInfo.InvariantCulture,
79 AssertEquals ("CF#2", rectneg, (Rectangle) rconv.ConvertFrom (null,
80 CultureInfo.InvariantCulture,
84 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
86 Fail ("CF#3: must throw ArgumentException");
87 } catch (Exception e) {
88 Assert ("CF#3", e is ArgumentException);
92 rconv.ConvertFrom ("10");
93 Fail ("CF#3a: must throw ArgumentException");
94 } catch (Exception e) {
95 Assert ("CF#3a", e is ArgumentException);
99 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
101 Fail ("CF#4: must throw ArgumentException");
102 } catch (Exception e) {
103 Assert ("CF#4", e is ArgumentException);
107 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
109 Fail ("CF#5: must throw Exception");
114 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
115 new Rectangle (10, 10, 100, 100));
116 Fail ("CF#6: must throw NotSupportedException");
117 } catch (Exception e) {
118 Assert ("CF#6", e is NotSupportedException);
122 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
123 new RectangleF (10, 10, 100, 100));
124 Fail ("CF#7: must throw NotSupportedException");
125 } catch (Exception e) {
126 Assert ("CF#7", e is NotSupportedException);
130 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
132 Fail ("CF#8: must throw NotSupportedException");
133 } catch (Exception e) {
134 Assert ("CF#8", e is NotSupportedException);
138 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
139 new PointF (10, 10));
140 Fail ("CF#9: must throw NotSupportedException");
141 } catch (Exception e) {
142 Assert ("CF#9", e is NotSupportedException);
146 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
148 Fail ("CF#10: must throw NotSupportedException");
149 } catch (Exception e) {
150 Assert ("CF#10", e is NotSupportedException);
154 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
156 Fail ("CF#11: must throw NotSupportedException");
157 } catch (Exception e) {
158 Assert ("CF#11", e is NotSupportedException);
162 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
164 Fail ("CF#12: must throw NotSupportedException");
165 } catch (Exception e) {
166 Assert ("CF#12", e is NotSupportedException);
170 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 1001);
171 Fail ("CF#13: must throw NotSupportedException");
172 } catch (Exception e) {
173 Assert ("CF#13", e is NotSupportedException);
178 public void TestConvertTo ()
180 AssertEquals ("CT#1", rectStr, (String) rconv.ConvertTo (null,
181 CultureInfo.InvariantCulture,
182 rect, typeof (String)));
183 AssertEquals ("CT#2", rectnegStr, (String) rconv.ConvertTo (null,
184 CultureInfo.InvariantCulture,
185 rectneg, typeof (String)));
188 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
189 rect, typeof (Rectangle));
190 Fail ("CT#3: must throw NotSupportedException");
191 } catch (Exception e) {
192 Assert ("CT#3", e is NotSupportedException);
196 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
197 rect, typeof (RectangleF));
198 Fail ("CT#4: must throw NotSupportedException");
199 } catch (Exception e) {
200 Assert ("CT#4", e is NotSupportedException);
204 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
205 rect, typeof (Size));
206 Fail ("CT#5: must throw NotSupportedException");
207 } catch (Exception e) {
208 Assert ("CT#5", e is NotSupportedException);
212 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
213 rect, typeof (SizeF));
214 Fail ("CT#6: must throw NotSupportedException");
215 } catch (Exception e) {
216 Assert ("CT#6", e is NotSupportedException);
220 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
221 rect, typeof (Point));
222 Fail ("CT#7: must throw NotSupportedException");
223 } catch (Exception e) {
224 Assert ("CT#7", e is NotSupportedException);
228 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
229 rect, typeof (PointF));
230 Fail ("CT#8: must throw NotSupportedException");
231 } catch (Exception e) {
232 Assert ("CT#8", e is NotSupportedException);
236 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
237 rect, typeof (Object));
238 Fail ("CT#9: must throw NotSupportedException");
239 } catch (Exception e) {
240 Assert ("CT#9", e is NotSupportedException);
244 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
246 Fail ("CT#10: must throw NotSupportedException");
247 } catch (Exception e) {
248 Assert ("CT#10", e is NotSupportedException);
253 public void TestGetCreateInstanceSupported ()
255 Assert ("GCIS#1", rconv.GetCreateInstanceSupported ());
256 Assert ("GCIS#2", rconv.GetCreateInstanceSupported (null));
260 public void TestCreateInstance ()
262 Rectangle rectInstance;
264 Hashtable ht = new Hashtable ();
265 ht.Add ("X", 10); ht.Add ("Y", 10);
266 ht.Add ("Width", 20); ht.Add ("Height", 30);
268 rectInstance = (Rectangle) rconv.CreateInstance (ht);
269 AssertEquals ("CI#1", rect, rectInstance);
272 ht.Add ("X", -10); ht.Add ("Y", -10);
273 ht.Add ("Width", 20); ht.Add ("Height", 30);
275 rectInstance = (Rectangle) rconv.CreateInstance (null, ht);
276 AssertEquals ("CI#2", rectneg, rectInstance);
278 // Property names are case-sensitive. It should throw
279 // NullRefExc if any of the property names does not match
281 ht.Add ("x", -10); ht.Add ("Y", -10);
282 ht.Add ("Width", 20); ht.Add ("Height", 30);
284 rectInstance = (Rectangle) rconv.CreateInstance (null, ht);
285 Fail ("CI#3: must throw NullReferenceException");
286 } catch (Exception e) {
287 Assert ("CI#3", e is NullReferenceException);
292 public void TestGetPropertiesSupported ()
294 Assert ("GPS#1", rconv.GetPropertiesSupported ());
295 Assert ("GPS#2", rconv.GetPropertiesSupported (null));
299 [Ignore ("This test fails because of bug #58435")]
300 public void TestGetProperties ()
303 PropertyDescriptorCollection propsColl;
305 propsColl = rconv.GetProperties (rect);
306 AssertEquals ("GP1#1", 4, propsColl.Count);
307 AssertEquals ("GP1#2", rect.X, propsColl ["X"].GetValue (rect));
308 AssertEquals ("GP1#3", rect.Y, propsColl ["Y"].GetValue (rect));
309 AssertEquals ("GP1#4", rect.Width, propsColl ["Width"].GetValue (rect));
310 AssertEquals ("GP1#5", rect.Height, propsColl ["Height"].GetValue (rect));
312 propsColl = rconv.GetProperties (null, rectneg);
313 AssertEquals ("GP2#1", 4, propsColl.Count);
314 AssertEquals ("GP2#2", rectneg.X, propsColl ["X"].GetValue (rectneg));
315 AssertEquals ("GP2#3", rectneg.Y, propsColl ["Y"].GetValue (rectneg));
316 AssertEquals ("GP2#4", rectneg.Width, propsColl ["Width"].GetValue (rectneg));
317 AssertEquals ("GP2#5", rectneg.Height, propsColl ["Height"].GetValue (rectneg));
319 propsColl = rconv.GetProperties (null, rect, null);
320 AssertEquals ("GP3#1", 11, propsColl.Count);
321 AssertEquals ("GP3#2", rect.X, propsColl ["X"].GetValue (rect));
322 AssertEquals ("GP3#3", rect.Y, propsColl ["Y"].GetValue (rect));
323 AssertEquals ("GP3#4", rect.Width, propsColl ["Width"].GetValue (rect));
324 AssertEquals ("GP3#5", rect.Height, propsColl ["Height"].GetValue (rect));
326 AssertEquals ("GP3#6", rect.Top, propsColl ["Top"].GetValue (rect));
327 AssertEquals ("GP3#7", rect.Bottom, propsColl ["Bottom"].GetValue (rect));
328 AssertEquals ("GP3#8", rect.Left, propsColl ["Left"].GetValue (rect));
329 AssertEquals ("GP3#9", rect.Right, propsColl ["Right"].GetValue (rect));
330 AssertEquals ("GP3#10", rect.Location, propsColl ["Location"].GetValue (rect));
331 AssertEquals ("GP3#11", rect.Size, propsColl ["Size"].GetValue (rect));
332 AssertEquals ("GP3#12", rect.IsEmpty, propsColl ["IsEmpty"].GetValue (rect));
334 Type type = typeof (Rectangle);
335 attrs = Attribute.GetCustomAttributes (type, true);
336 propsColl = rconv.GetProperties (null, rect, attrs);
337 AssertEquals ("GP3#13", 0, propsColl.Count);