ChangeLog: Updated ChangeLog.
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestRectangleConverter.cs
1 //
2 // Tests for System.Drawing.RectangleConverter.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 RectangleConverterTest : Assertion
21         {
22                 Rectangle rect;
23                 Rectangle rectneg;
24                 RectangleConverter rconv;
25                 String rectStr;
26                 String rectnegStr;
27
28                 [TearDown]
29                 public void TearDown () {}
30
31                 [SetUp]
32                 public void SetUp ()            
33                 {
34                         rect = new Rectangle (10, 10, 20, 30);
35                         rectStr = rect.X + ", " + rect.Y + ", " + rect.Width + ", " + rect.Height;
36
37                         rectneg = new Rectangle (-10, -10, 20, 30);
38                         rectnegStr = rectneg.X + ", " + rectneg.Y + ", " + rectneg.Width + ", " + rectneg.Height;
39
40                         rconv = (RectangleConverter) TypeDescriptor.GetConverter (rect);
41                 }
42
43                 [Test]
44                 public void TestCanConvertFrom ()
45                 {
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)));
56                 }
57
58                 [Test]
59                 public void TestCanConvertTo ()
60                 {
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)));
71                 }
72
73                 [Test]
74                 public void TestConvertFrom ()
75                 {
76                         AssertEquals ("CF#1", rect, (Rectangle) rconv.ConvertFrom (null,
77                                                                 CultureInfo.InvariantCulture,
78                                                                 "10, 10, 20, 30"));
79                         AssertEquals ("CF#2", rectneg, (Rectangle) rconv.ConvertFrom (null,
80                                                                 CultureInfo.InvariantCulture,
81                                                                 "-10, -10, 20, 30"));
82
83                         try {
84                                 rconv.ConvertFrom (null, CultureInfo.InvariantCulture, 
85                                                    "10, 10");
86                                 Fail ("CF#3: must throw ArgumentException");
87                         } catch (Exception e) {
88                                 Assert ("CF#3", e is ArgumentException);
89                         }
90
91                         try {
92                                 rconv.ConvertFrom ("10");
93                                 Fail ("CF#3a: must throw ArgumentException");
94                         } catch (Exception e) {
95                                 Assert ("CF#3a", e is ArgumentException);
96                         }
97
98                         try {
99                                 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
100                                                    "1, 1, 1, 1, 1");
101                                 Fail ("CF#4: must throw ArgumentException");
102                         } catch (Exception e) {
103                                 Assert ("CF#4", e is ArgumentException);
104                         }
105
106                         try {
107                                 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
108                                                    "*1, 1, 1, 1");
109                                 Fail ("CF#5: must throw Exception");
110                         } catch {
111                         }
112
113                         try {
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);
119                         }
120
121                         try {
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);
127                         }
128
129                         try {
130                                 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
131                                                    new Point (10, 10));
132                                 Fail ("CF#8: must throw NotSupportedException");
133                         } catch (Exception e) {
134                                 Assert ("CF#8", e is NotSupportedException);
135                         }
136
137                         try {
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);
143                         }
144
145                         try {
146                                 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
147                                                    new Size (10, 10));
148                                 Fail ("CF#10: must throw NotSupportedException");
149                         } catch (Exception e) {
150                                 Assert ("CF#10", e is NotSupportedException);
151                         }
152
153                         try {
154                                 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
155                                                    new SizeF (10, 10));
156                                 Fail ("CF#11: must throw NotSupportedException");
157                         } catch (Exception e) {
158                                 Assert ("CF#11", e is NotSupportedException);
159                         }
160
161                         try {
162                                 rconv.ConvertFrom (null, CultureInfo.InvariantCulture,
163                                                    new Object ());
164                                 Fail ("CF#12: must throw NotSupportedException");
165                         } catch (Exception e) {
166                                 Assert ("CF#12", e is NotSupportedException);
167                         }
168
169                         try {
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);
174                         }
175                 }
176
177                 [Test]
178                 public void TestConvertTo ()
179                 {
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)));
186
187                         try {
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);
193                         }
194
195                         try {
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);
201                         }
202
203                         try {
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);
209                         }
210
211                         try {
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);
217                         }
218
219                         try {
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);
225                         }
226
227                         try {
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);
233                         }
234
235                         try {
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);
241                         }
242
243                         try {
244                                 rconv.ConvertTo (null, CultureInfo.InvariantCulture,
245                                                  rect, typeof (int));
246                                 Fail ("CT#10: must throw NotSupportedException");
247                         } catch (Exception e) {
248                                 Assert ("CT#10", e is NotSupportedException);
249                         }
250                 }
251
252                 [Test]
253                 public void TestGetCreateInstanceSupported ()
254                 {
255                         Assert ("GCIS#1", rconv.GetCreateInstanceSupported ());
256                         Assert ("GCIS#2", rconv.GetCreateInstanceSupported (null));
257                 }
258
259                 [Test]
260                 public void TestCreateInstance ()
261                 {
262                         Rectangle rectInstance;
263
264                         Hashtable ht = new Hashtable ();
265                         ht.Add ("X", 10); ht.Add ("Y", 10);
266                         ht.Add ("Width", 20); ht.Add ("Height", 30);
267
268                         rectInstance = (Rectangle) rconv.CreateInstance (ht);
269                         AssertEquals ("CI#1", rect, rectInstance);
270
271                         ht.Clear ();
272                         ht.Add ("X", -10); ht.Add ("Y", -10);
273                         ht.Add ("Width", 20); ht.Add ("Height", 30);
274
275                         rectInstance = (Rectangle) rconv.CreateInstance (null, ht);
276                         AssertEquals ("CI#2", rectneg, rectInstance);
277
278                         // Property names are case-sensitive. It should throw 
279                         // NullRefExc if any of the property names does not match
280                         ht.Clear ();
281                         ht.Add ("x", -10); ht.Add ("Y", -10);
282                         ht.Add ("Width", 20); ht.Add ("Height", 30);
283                         try {
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);
288                         }
289                 }
290
291                 [Test]
292                 public void TestGetPropertiesSupported ()
293                 {
294                         Assert ("GPS#1", rconv.GetPropertiesSupported ());
295                         Assert ("GPS#2", rconv.GetPropertiesSupported (null));
296                 }
297
298                 [Test]
299                 [Ignore ("This test fails because of bug #58435")]
300                 public void TestGetProperties ()
301                 {
302                         Attribute [] attrs;
303                         PropertyDescriptorCollection propsColl;
304
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));
311
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));
318
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));
325
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));
333
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);
338                 }
339         }
340 }