ee9f5de9ecfcc1d164c0be752a03b34fe74634e2
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestSizeConverter.cs
1 //
2 // Tests for System.Drawing.SizeConverter.cs 
3 //
4 // Author:
5 //      Ravindra (rkumar@novell.com)
6 //
7 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.Drawing;
13 using System.Collections;
14 using System.ComponentModel;
15 using System.Globalization;
16
17 namespace MonoTests.System.Drawing
18 {
19         [TestFixture]   
20         public class SizeConverterTest : Assertion
21         {
22                 Size sz;
23                 Size szneg;
24                 SizeConverter szconv;
25                 String szStr;
26                 String sznegStr;
27
28                 [TearDown]
29                 public void TearDown () {}
30
31                 [SetUp]
32                 public void SetUp ()            
33                 {
34                         sz = new Size (10, 20);
35                         szStr = sz.Width + ", " + sz.Height;
36
37                         szneg = new Size (-20, -30);
38                         sznegStr = szneg.Width + ", " + szneg.Height;
39
40                         szconv = (SizeConverter) TypeDescriptor.GetConverter (sz);
41                 }
42
43                 [Test]
44                 public void TestCanConvertFrom ()
45                 {
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)));
56                 }
57
58                 [Test]
59                 public void TestCanConvertTo ()
60                 {
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)));
71                 }
72
73                 [Test]
74                 public void TestConvertFrom ()
75                 {
76                         AssertEquals ("CF#1", sz, (Size) szconv.ConvertFrom (null,
77                                                                 CultureInfo.InvariantCulture,
78                                                                 "10, 20"));
79                         AssertEquals ("CF#2", szneg, (Size) szconv.ConvertFrom (null,
80                                                                 CultureInfo.InvariantCulture,
81                                                                 "-20, -30"));
82
83                         try {
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);
88                         }
89
90                         try {
91                                 szconv.ConvertFrom ("10");
92                                 Fail ("CF#3a: must throw ArgumentException");
93                         } catch (Exception e) {
94                                 Assert ("CF#3a", e is ArgumentException);
95                         }
96
97                         try {
98                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
99                                                     "1, 1, 1");
100                                 Fail ("CF#4: must throw ArgumentException");
101                         } catch (Exception e) {
102                                 Assert ("CF#4", e is ArgumentException);
103                         }
104
105                         try {
106                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
107                                                     "*1, 1");
108                                 Fail ("CF#5: must throw Exception");
109                         } catch {
110                         }
111
112                         try {
113                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
114                                                     new Point (10, 10));
115                                 Fail ("CF#6: must throw NotSupportedException");
116                         } catch (Exception e) {
117                                 Assert ("CF#6", e is NotSupportedException);
118                         }
119
120                         try {
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);
126                         }
127
128                         try {
129                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
130                                                     new Size (10, 10));
131                                 Fail ("CF#8: must throw NotSupportedException");
132                         } catch (Exception e) {
133                                 Assert ("CF#8", e is NotSupportedException);
134                         }
135
136                         try {
137                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
138                                                     new SizeF (10, 10));
139                                 Fail ("CF#9: must throw NotSupportedException");
140                         } catch (Exception e) {
141                                 Assert ("CF#9", e is NotSupportedException);
142                         }
143
144                         try {
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);
149                         }
150                 }
151
152                 [Test]
153                 public void TestConvertTo ()
154                 {
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, 
160                                                         typeof (String)));
161
162                         try {
163                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
164                                                   typeof (Size));
165                                 Fail ("CT#3: must throw NotSupportedException");
166                         } catch (Exception e) {
167                                 Assert ("CT#3", e is NotSupportedException);
168                         }
169
170                         try {
171                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
172                                                   typeof (SizeF));
173                                 Fail ("CT#4: must throw NotSupportedException");
174                         } catch (Exception e) {
175                                 Assert ("CT#4", e is NotSupportedException);
176                         }
177
178                         try {
179                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
180                                                   typeof (Point));
181                                 Fail ("CT#5: must throw NotSupportedException");
182                         } catch (Exception e) {
183                                 Assert ("CT#5", e is NotSupportedException);
184                         }
185
186                         try {
187                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
188                                                   typeof (PointF));
189                                 Fail ("CT#6: must throw NotSupportedException");
190                         } catch (Exception e) {
191                                 Assert ("CT#6", e is NotSupportedException);
192                         }
193
194                         try {
195                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
196                                                   typeof (int));
197                                 Fail ("CT#7: must throw NotSupportedException");
198                         } catch (Exception e) {
199                                 Assert ("CT#7", e is NotSupportedException);
200                         }
201                 }
202
203                 [Test]
204                 public void TestGetCreateInstanceSupported ()
205                 {
206                         Assert ("GCIS#1", szconv.GetCreateInstanceSupported ());
207                         Assert ("GCIS#2", szconv.GetCreateInstanceSupported (null));
208                 }
209
210                 [Test]
211                 public void TestCreateInstance ()
212                 {
213                         Size szInstance;
214
215                         Hashtable ht = new Hashtable ();
216                         ht.Add ("Width", 10); ht.Add ("Height", 20);
217
218                         szInstance = (Size) szconv.CreateInstance (ht);
219                         AssertEquals ("CI#1", sz, szInstance);
220
221                         ht.Clear ();
222                         ht.Add ("Width", -20); ht.Add ("Height", -30);
223
224                         szInstance = (Size) szconv.CreateInstance (null, ht);
225                         AssertEquals ("CI#2", szneg, szInstance);
226
227                         // Property names are case-sensitive. It should throw 
228                         // NullRefExc if any of the property names does not match
229                         ht.Clear ();
230                         ht.Add ("width", 20); ht.Add ("Height", 30);
231                         try {
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);
236                         }
237                 }
238
239                 [Test]
240                 public void TestGetPropertiesSupported ()
241                 {
242                         Assert ("GPS#1", szconv.GetPropertiesSupported ());
243                         Assert ("GPS#2", szconv.GetPropertiesSupported (null));
244                 }
245
246                 [Test]
247                 public void TestGetProperties ()
248                 {
249                         Attribute [] attrs;
250                         PropertyDescriptorCollection propsColl;
251
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));
256
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));
261
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));
267
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);
272                 }
273         }
274 }