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 NUnit.Framework;
35 using System.Collections;
36 using System.ComponentModel;
37 using System.Globalization;
39 namespace MonoTests.System.Drawing
42 public class SizeConverterTest : Assertion
51 public void TearDown () {}
56 sz = new Size (10, 20);
57 szStr = sz.Width + ", " + sz.Height;
59 szneg = new Size (-20, -30);
60 sznegStr = szneg.Width + ", " + szneg.Height;
62 szconv = (SizeConverter) TypeDescriptor.GetConverter (sz);
66 public void TestCanConvertFrom ()
68 Assert ("CCF#1", szconv.CanConvertFrom (typeof (String)));
69 Assert ("CCF#1a", szconv.CanConvertFrom (null, typeof (String)));
70 Assert ("CCF#2", ! szconv.CanConvertFrom (null, typeof (Rectangle)));
71 Assert ("CCF#3", ! szconv.CanConvertFrom (null, typeof (RectangleF)));
72 Assert ("CCF#4", ! szconv.CanConvertFrom (null, typeof (Point)));
73 Assert ("CCF#5", ! szconv.CanConvertFrom (null, typeof (PointF)));
74 Assert ("CCF#6", ! szconv.CanConvertFrom (null, typeof (Size)));
75 Assert ("CCF#7", ! szconv.CanConvertFrom (null, typeof (SizeF)));
76 Assert ("CCF#8", ! szconv.CanConvertFrom (null, typeof (Object)));
77 Assert ("CCF#9", ! szconv.CanConvertFrom (null, typeof (int)));
81 public void TestCanConvertTo ()
83 Assert ("CCT#1", szconv.CanConvertTo (typeof (String)));
84 Assert ("CCT#1a", szconv.CanConvertTo (null, typeof (String)));
85 Assert ("CCT#2", ! szconv.CanConvertTo (null, typeof (Rectangle)));
86 Assert ("CCT#3", ! szconv.CanConvertTo (null, typeof (RectangleF)));
87 Assert ("CCT#4", ! szconv.CanConvertTo (null, typeof (Point)));
88 Assert ("CCT#5", ! szconv.CanConvertTo (null, typeof (PointF)));
89 Assert ("CCT#6", ! szconv.CanConvertTo (null, typeof (Size)));
90 Assert ("CCT#7", ! szconv.CanConvertTo (null, typeof (SizeF)));
91 Assert ("CCT#8", ! szconv.CanConvertTo (null, typeof (Object)));
92 Assert ("CCT#9", ! szconv.CanConvertTo (null, typeof (int)));
96 public void TestConvertFrom ()
98 AssertEquals ("CF#1", sz, (Size) szconv.ConvertFrom (null,
99 CultureInfo.InvariantCulture,
101 AssertEquals ("CF#2", szneg, (Size) szconv.ConvertFrom (null,
102 CultureInfo.InvariantCulture,
106 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, "10");
107 Fail ("CF#3: must throw ArgumentException");
108 } catch (Exception e) {
109 Assert ("CF#3", e is ArgumentException);
113 szconv.ConvertFrom ("10");
114 Fail ("CF#3a: must throw ArgumentException");
115 } catch (Exception e) {
116 Assert ("CF#3a", e is ArgumentException);
120 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
122 Fail ("CF#4: must throw ArgumentException");
123 } catch (Exception e) {
124 Assert ("CF#4", e is ArgumentException);
128 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
130 Fail ("CF#5: must throw Exception");
135 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
137 Fail ("CF#6: must throw NotSupportedException");
138 } catch (Exception e) {
139 Assert ("CF#6", e is NotSupportedException);
143 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
144 new PointF (10, 10));
145 Fail ("CF#7: must throw NotSupportedException");
146 } catch (Exception e) {
147 Assert ("CF#7", e is NotSupportedException);
151 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
153 Fail ("CF#8: must throw NotSupportedException");
154 } catch (Exception e) {
155 Assert ("CF#8", e is NotSupportedException);
159 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
161 Fail ("CF#9: must throw NotSupportedException");
162 } catch (Exception e) {
163 Assert ("CF#9", e is NotSupportedException);
167 szconv.ConvertFrom (null, CultureInfo.InvariantCulture, 0x10);
168 Fail ("CF#10: must throw NotSupportedException");
169 } catch (Exception e) {
170 Assert ("CF#10", e is NotSupportedException);
175 public void TestConvertTo ()
177 AssertEquals ("CT#1", szStr, (String) szconv.ConvertTo (null,
178 CultureInfo.InvariantCulture,
179 sz, typeof (String)));
180 AssertEquals ("CT#2", sznegStr, (String) szconv.ConvertTo (null,
181 CultureInfo.InvariantCulture, szneg,
185 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
187 Fail ("CT#3: must throw NotSupportedException");
188 } catch (Exception e) {
189 Assert ("CT#3", e is NotSupportedException);
193 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
195 Fail ("CT#4: must throw NotSupportedException");
196 } catch (Exception e) {
197 Assert ("CT#4", e is NotSupportedException);
201 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
203 Fail ("CT#5: must throw NotSupportedException");
204 } catch (Exception e) {
205 Assert ("CT#5", e is NotSupportedException);
209 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
211 Fail ("CT#6: must throw NotSupportedException");
212 } catch (Exception e) {
213 Assert ("CT#6", e is NotSupportedException);
217 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
219 Fail ("CT#7: must throw NotSupportedException");
220 } catch (Exception e) {
221 Assert ("CT#7", e is NotSupportedException);
226 public void TestGetCreateInstanceSupported ()
228 Assert ("GCIS#1", szconv.GetCreateInstanceSupported ());
229 Assert ("GCIS#2", szconv.GetCreateInstanceSupported (null));
233 public void TestCreateInstance ()
237 Hashtable ht = new Hashtable ();
238 ht.Add ("Width", 10); ht.Add ("Height", 20);
240 szInstance = (Size) szconv.CreateInstance (ht);
241 AssertEquals ("CI#1", sz, szInstance);
244 ht.Add ("Width", -20); ht.Add ("Height", -30);
246 szInstance = (Size) szconv.CreateInstance (null, ht);
247 AssertEquals ("CI#2", szneg, szInstance);
249 // Property names are case-sensitive. It should throw
250 // NullRefExc if any of the property names does not match
252 ht.Add ("width", 20); ht.Add ("Height", 30);
254 szInstance = (Size) szconv.CreateInstance (null, ht);
255 Fail ("CI#3: must throw NullReferenceException");
256 } catch (Exception e) {
257 Assert ("CI#3", e is NullReferenceException);
262 public void TestGetPropertiesSupported ()
264 Assert ("GPS#1", szconv.GetPropertiesSupported ());
265 Assert ("GPS#2", szconv.GetPropertiesSupported (null));
269 [Ignore ("This test fails because of bug #58435")]
270 public void TestGetProperties ()
273 PropertyDescriptorCollection propsColl;
275 propsColl = szconv.GetProperties (sz);
276 AssertEquals ("GP1#1", 2, propsColl.Count);
277 AssertEquals ("GP1#2", sz.Width, propsColl ["Width"].GetValue (sz));
278 AssertEquals ("GP1#3", sz.Height, propsColl ["Height"].GetValue (sz));
280 propsColl = szconv.GetProperties (null, szneg);
281 AssertEquals ("GP2#1", 2, propsColl.Count);
282 AssertEquals ("GP2#2", szneg.Width, propsColl ["Width"].GetValue (szneg));
283 AssertEquals ("GP2#3", szneg.Height, propsColl ["Height"].GetValue (szneg));
285 propsColl = szconv.GetProperties (null, sz, null);
286 AssertEquals ("GP3#1", 3, propsColl.Count);
287 AssertEquals ("GP3#2", sz.Width, propsColl ["Width"].GetValue (sz));
288 AssertEquals ("GP3#3", sz.Height, propsColl ["Height"].GetValue (sz));
289 AssertEquals ("GP3#4", sz.IsEmpty, propsColl ["IsEmpty"].GetValue (sz));
291 Type type = typeof (Size);
292 attrs = Attribute.GetCustomAttributes (type, true);
293 propsColl = szconv.GetProperties (null, sz, attrs);
294 AssertEquals ("GP3#5", 0, propsColl.Count);