2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31
32 using NUnit.Framework;
33 using System;
34 using System.Drawing;
35 using System.Collections;
36 using System.ComponentModel;
37 using System.Globalization;
38
39 namespace MonoTests.System.Drawing
40 {
41         [TestFixture]   
42         public class SizeConverterTest : Assertion
43         {
44                 Size sz;
45                 Size szneg;
46                 SizeConverter szconv;
47                 String szStr;
48                 String sznegStr;
49
50                 [TearDown]
51                 public void TearDown () {}
52
53                 [SetUp]
54                 public void SetUp ()            
55                 {
56                         sz = new Size (10, 20);
57                         szStr = sz.Width + ", " + sz.Height;
58
59                         szneg = new Size (-20, -30);
60                         sznegStr = szneg.Width + ", " + szneg.Height;
61
62                         szconv = (SizeConverter) TypeDescriptor.GetConverter (sz);
63                 }
64
65                 [Test]
66                 public void TestCanConvertFrom ()
67                 {
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)));
78                 }
79
80                 [Test]
81                 public void TestCanConvertTo ()
82                 {
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)));
93                 }
94
95                 [Test]
96                 public void TestConvertFrom ()
97                 {
98                         AssertEquals ("CF#1", sz, (Size) szconv.ConvertFrom (null,
99                                                                 CultureInfo.InvariantCulture,
100                                                                 "10, 20"));
101                         AssertEquals ("CF#2", szneg, (Size) szconv.ConvertFrom (null,
102                                                                 CultureInfo.InvariantCulture,
103                                                                 "-20, -30"));
104
105                         try {
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);
110                         }
111
112                         try {
113                                 szconv.ConvertFrom ("10");
114                                 Fail ("CF#3a: must throw ArgumentException");
115                         } catch (Exception e) {
116                                 Assert ("CF#3a", e is ArgumentException);
117                         }
118
119                         try {
120                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
121                                                     "1, 1, 1");
122                                 Fail ("CF#4: must throw ArgumentException");
123                         } catch (Exception e) {
124                                 Assert ("CF#4", e is ArgumentException);
125                         }
126
127                         try {
128                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
129                                                     "*1, 1");
130                                 Fail ("CF#5: must throw Exception");
131                         } catch {
132                         }
133
134                         try {
135                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
136                                                     new Point (10, 10));
137                                 Fail ("CF#6: must throw NotSupportedException");
138                         } catch (Exception e) {
139                                 Assert ("CF#6", e is NotSupportedException);
140                         }
141
142                         try {
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);
148                         }
149
150                         try {
151                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
152                                                     new Size (10, 10));
153                                 Fail ("CF#8: must throw NotSupportedException");
154                         } catch (Exception e) {
155                                 Assert ("CF#8", e is NotSupportedException);
156                         }
157
158                         try {
159                                 szconv.ConvertFrom (null, CultureInfo.InvariantCulture,
160                                                     new SizeF (10, 10));
161                                 Fail ("CF#9: must throw NotSupportedException");
162                         } catch (Exception e) {
163                                 Assert ("CF#9", e is NotSupportedException);
164                         }
165
166                         try {
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);
171                         }
172                 }
173
174                 [Test]
175                 public void TestConvertTo ()
176                 {
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, 
182                                                         typeof (String)));
183
184                         try {
185                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
186                                                   typeof (Size));
187                                 Fail ("CT#3: must throw NotSupportedException");
188                         } catch (Exception e) {
189                                 Assert ("CT#3", e is NotSupportedException);
190                         }
191
192                         try {
193                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
194                                                   typeof (SizeF));
195                                 Fail ("CT#4: must throw NotSupportedException");
196                         } catch (Exception e) {
197                                 Assert ("CT#4", e is NotSupportedException);
198                         }
199
200                         try {
201                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
202                                                   typeof (Point));
203                                 Fail ("CT#5: must throw NotSupportedException");
204                         } catch (Exception e) {
205                                 Assert ("CT#5", e is NotSupportedException);
206                         }
207
208                         try {
209                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
210                                                   typeof (PointF));
211                                 Fail ("CT#6: must throw NotSupportedException");
212                         } catch (Exception e) {
213                                 Assert ("CT#6", e is NotSupportedException);
214                         }
215
216                         try {
217                                 szconv.ConvertTo (null, CultureInfo.InvariantCulture, sz,
218                                                   typeof (int));
219                                 Fail ("CT#7: must throw NotSupportedException");
220                         } catch (Exception e) {
221                                 Assert ("CT#7", e is NotSupportedException);
222                         }
223                 }
224
225                 [Test]
226                 public void TestGetCreateInstanceSupported ()
227                 {
228                         Assert ("GCIS#1", szconv.GetCreateInstanceSupported ());
229                         Assert ("GCIS#2", szconv.GetCreateInstanceSupported (null));
230                 }
231
232                 [Test]
233                 public void TestCreateInstance ()
234                 {
235                         Size szInstance;
236
237                         Hashtable ht = new Hashtable ();
238                         ht.Add ("Width", 10); ht.Add ("Height", 20);
239
240                         szInstance = (Size) szconv.CreateInstance (ht);
241                         AssertEquals ("CI#1", sz, szInstance);
242
243                         ht.Clear ();
244                         ht.Add ("Width", -20); ht.Add ("Height", -30);
245
246                         szInstance = (Size) szconv.CreateInstance (null, ht);
247                         AssertEquals ("CI#2", szneg, szInstance);
248
249                         // Property names are case-sensitive. It should throw 
250                         // NullRefExc if any of the property names does not match
251                         ht.Clear ();
252                         ht.Add ("width", 20); ht.Add ("Height", 30);
253                         try {
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);
258                         }
259                 }
260
261                 [Test]
262                 public void TestGetPropertiesSupported ()
263                 {
264                         Assert ("GPS#1", szconv.GetPropertiesSupported ());
265                         Assert ("GPS#2", szconv.GetPropertiesSupported (null));
266                 }
267
268                 [Test]
269                 [Ignore ("This test fails because of bug #58435")]
270                 public void TestGetProperties ()
271                 {
272                         Attribute [] attrs;
273                         PropertyDescriptorCollection propsColl;
274
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));
279
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));
284
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));
290
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);
295                 }
296         }
297 }