* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestIconConverter.cs
1 //
2 // Tests for System.Drawing.IconConverter.cs 
3 //
4 // Author:
5 //      Sanjay Gupta (gsanjay@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.Drawing.Imaging;
36 using System.Collections;
37 using System.ComponentModel;
38 using System.Globalization;
39 using System.IO;
40 using System.Security.Permissions;
41
42 namespace MonoTests.System.Drawing
43 {
44         [TestFixture]
45         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
46         public class IconConverterTest
47         {
48                 Icon icon;              
49                 IconConverter icoConv;
50                 IconConverter icoConvFrmTD;
51                 String iconStr;
52                 byte [] iconBytes;
53
54                 [TearDown]
55                 public void TearDown () {}
56
57                 [SetUp]
58                 public void SetUp ()            
59                 {
60                         icon = new Icon (TestBitmap.getInFile ("bitmaps/VisualPng.ico"));
61                         iconStr = icon.ToString ();
62                 
63                         icoConv = new IconConverter();
64                         icoConvFrmTD = (IconConverter) TypeDescriptor.GetConverter (icon);
65                         
66                         Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/VisualPng1.ico"), FileMode.Open);
67                         int length = (int) stream.Length;
68                         iconBytes = new byte [length];
69                         
70                         try {
71                                 if (stream.Read (iconBytes, 0, length) != length)
72                                         Assert.Fail ("SU#1: Read Failure"); 
73                         } catch (Exception e) {
74                                 Assert.Fail ("SU#2 Exception thrown while reading. Exception is: "+e.Message);
75                         } finally {
76                                 stream.Close ();
77                         }
78                 
79                         stream.Close ();
80
81                 }
82
83                 [Test]
84 #if TARGET_JVM
85                 [NUnit.Framework.Category ("NotWorking")]
86 #endif
87                 public void TestCanConvertFrom ()
88                 {
89                         Assert.IsTrue (icoConv.CanConvertFrom (typeof (byte [])), "CCF#1");
90                         Assert.IsTrue (icoConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
91                         Assert.IsTrue (icoConv.CanConvertFrom (null, iconBytes.GetType ()), "CCF#1b");
92                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (String)), "CCF#2");
93                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
94                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
95                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
96                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
97                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
98                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
99                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (int)), "CCF#9");
100                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
101
102                         Assert.IsTrue (icoConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
103                         Assert.IsTrue (icoConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
104                         Assert.IsTrue (icoConvFrmTD.CanConvertFrom (null, iconBytes.GetType ()), "CCF#1bA");
105                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
106                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
107                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
108                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
109                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
110                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
111                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
112                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
113                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
114
115                 }
116
117                 [Test]
118 #if TARGET_JVM
119                 [NUnit.Framework.Category ("NotWorking")]
120 #endif
121                 public void TestCanConvertTo ()
122                 {
123                         Assert.IsTrue (icoConv.CanConvertTo (typeof (String)), "CCT#1");
124                         Assert.IsTrue (icoConv.CanConvertTo (null, typeof (String)), "CCT#1a");
125                         Assert.IsTrue (icoConv.CanConvertTo (null, iconStr.GetType ()), "CCT#1b");
126                         Assert.IsTrue (icoConv.CanConvertTo (typeof (byte [])), "CCT#2");
127                         Assert.IsTrue (icoConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
128                         Assert.IsTrue (icoConv.CanConvertTo (null, iconBytes.GetType ()), "CCT#2b");
129                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
130                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Point)), "CCT#4");
131                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
132                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Size)), "CCT#6");
133                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
134                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Object)), "CCT#8");
135                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (int)), "CCT#9");
136
137                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
138                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
139                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, iconStr.GetType ()), "CCT#1bA");
140                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
141                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
142                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, iconBytes.GetType ()), "CCT#2bA");
143                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
144                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
145                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
146                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
147                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
148                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
149                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
150
151                 }
152
153                 [Test]
154 #if TARGET_JVM
155                 [NUnit.Framework.Category ("NotWorking")]
156 #endif
157                 public void TestConvertFrom ()
158                 {
159                         Icon newIcon = (Icon) icoConv.ConvertFrom (null, CultureInfo.InvariantCulture, iconBytes);
160
161                         Assert.AreEqual (icon.Height, newIcon.Height, "CF#1");
162                         Assert.AreEqual (icon.Width, newIcon.Width, "CF#1a" );
163                         
164                         try {
165                                 icoConv.ConvertFrom ("System.Drawing.String");
166                                 Assert.Fail ("CF#2: must throw NotSupportedException");
167                         } catch (Exception e) {
168                                 Assert.IsTrue (e is NotSupportedException, "CF#2");
169                         }
170
171                         try {
172                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
173                                                    "System.Drawing.String");
174                                 Assert.Fail ("CF#2a: must throw NotSupportedException");
175                         } catch (Exception e) {
176                                 Assert.IsTrue (e is NotSupportedException, "CF#2a");
177                         }
178
179                         try {
180                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
181                                                    new Bitmap (20, 20));
182                                 Assert.Fail ("CF#3: must throw NotSupportedException");
183                         } catch (Exception e) {
184                                 Assert.IsTrue (e is NotSupportedException, "CF#3");
185                         }
186
187                         try {
188                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
189                                                    new Point (10, 10));
190                                 Assert.Fail ("CF#4: must throw NotSupportedException");
191                         } catch (Exception e) {
192                                 Assert.IsTrue (e is NotSupportedException, "CF#4");
193                         }
194
195                         try {
196                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
197                                                    new SizeF (10, 10));
198                                 Assert.Fail ("CF#5: must throw NotSupportedException");
199                         } catch (Exception e) {
200                                 Assert.IsTrue (e is NotSupportedException, "CF#5");
201                         }
202
203                         try {
204                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
205                                                    new Object ());
206                                 Assert.Fail ("CF#6: must throw NotSupportedException");
207                         } catch (Exception e) {
208                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
209                         }
210
211
212                         newIcon = (Icon) icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, iconBytes);
213
214                         Assert.AreEqual (icon.Height, newIcon.Height, "CF#1A");
215                         Assert.AreEqual (icon.Width, newIcon.Width, "CF#1Aa");
216                         
217                         try {
218                                 icoConvFrmTD.ConvertFrom ("System.Drawing.String");
219                                 Assert.Fail ("CF#2A: must throw NotSupportedException");
220                         } catch (Exception e) {
221                                 Assert.IsTrue (e is NotSupportedException, "CF#2A");
222                         }
223
224                         try {
225                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
226                                                    "System.Drawing.String");
227                                 Assert.Fail ("CF#2aA: must throw NotSupportedException");
228                         } catch (Exception e) {
229                                 Assert.IsTrue (e is NotSupportedException, "CF#2aA");
230                         }
231
232                         try {
233                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
234                                                    new Bitmap (20, 20));
235                                 Assert.Fail ("CF#3A: must throw NotSupportedException");
236                         } catch (Exception e) {
237                                 Assert.IsTrue (e is NotSupportedException, "CF#3A");
238                         }
239
240                         try {
241                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
242                                                    new Point (10, 10));
243                                 Assert.Fail ("CF#4A: must throw NotSupportedException");
244                         } catch (Exception e) {
245                                 Assert.IsTrue (e is NotSupportedException, "CF#4A");
246                         }
247
248                         try {
249                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
250                                                    new SizeF (10, 10));
251                                 Assert.Fail ("CF#5A: must throw NotSupportedException");
252                         } catch (Exception e) {
253                                 Assert.IsTrue (e is NotSupportedException, "CF#5A");
254                         }
255
256                         try {
257                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
258                                                    new Object ());
259                                 Assert.Fail ("CF#6A: must throw NotSupportedException");
260                         } catch (Exception e) {
261                                 Assert.IsTrue (e is NotSupportedException, "CF#6A");
262                         }
263
264                 }
265
266                 [Test]
267 #if TARGET_JVM
268                 [NUnit.Framework.Category ("NotWorking")]
269 #endif
270                 public void TestConvertTo ()
271                 {
272                         Assert.AreEqual (iconStr, (String) icoConv.ConvertTo (null,
273                                                                 CultureInfo.InvariantCulture,
274                                                                 icon, typeof (String)), "CT#1");
275
276                         Assert.AreEqual (iconStr, (String) icoConv.ConvertTo (icon, 
277                                                                         typeof (String)), "CT#1a");
278                                 
279                         /*byte [] newIconBytes = (byte []) icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
280                                                                                         icon, iconBytes.GetType ());
281                 
282                         Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2");
283
284                         newIconBytes = (byte []) icoConv.ConvertTo (icon, iconBytes.GetType ());
285                         
286                         Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2a");
287
288                                                 
289                         try {
290                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture, 
291                                                  icon, typeof (Rectangle));
292                                 Assert.Fail ("CT#3: must throw NotSupportedException");
293                         } catch (Exception e) {
294                                 Assert.IsTrue ( e is NotSupportedException, "CT#3");
295                         }
296
297                         try {
298                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
299                                                  icon, icon.GetType ());
300                                 Assert.Fail ("CT#4: must throw NotSupportedException");
301                         } catch (Exception e) {
302                                 Assert.IsTrue (e is NotSupportedException, "CT#4");
303                         }
304
305                         try {
306                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
307                                                  icon, typeof (Size));
308                                 Assert.Fail ("CT#5: must throw NotSupportedException");
309                         } catch (Exception e) {
310                                 Assert.IsTrue (e is NotSupportedException, "CT#5");
311                         }
312
313                         try {
314                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
315                                                  icon, typeof (Bitmap));
316                                 Assert.Fail ("CT#6: must throw NotSupportedException");
317                         } catch (Exception e) {
318                                 Assert.IsTrue ( e is NotSupportedException, "CT#6");
319                         }
320
321                         try {
322                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
323                                                  icon, typeof (Point));
324                                 Assert.Fail ("CT#7: must throw NotSupportedException");
325                         } catch (Exception e) {
326                                 Assert.IsTrue (e is NotSupportedException, "CT#7");
327                         }
328
329                         try {
330                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
331                                                  icon, typeof (Metafile));
332                                 Assert.Fail ("CT#8: must throw NotSupportedException");
333                         } catch (Exception e) {
334                                 Assert.IsTrue (e is NotSupportedException, "CT#8");
335                         }
336
337                         try {
338                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
339                                                  icon, typeof (Object));
340                                 Assert.Fail ("CT#9: must throw NotSupportedException");
341                         } catch (Exception e) {
342                                 Assert.IsTrue (e is NotSupportedException, "CT#9");
343                         }
344
345                         try {
346                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
347                                                  icon, typeof (int));
348                                 Assert.Fail ("CT#10: must throw NotSupportedException");
349                         } catch (Exception e) {
350                                 Assert.IsTrue (e is NotSupportedException, "CT#10");
351                         }*/
352
353
354                         Assert.AreEqual (iconStr, (String) icoConvFrmTD.ConvertTo (null,
355                                                                 CultureInfo.InvariantCulture,
356                                                                 icon, typeof (String)), "CT#1A");
357
358                         Assert.AreEqual (iconStr, (String) icoConvFrmTD.ConvertTo (icon, 
359                                                                         typeof (String)), "CT#1aA");
360                                 
361                         /*newIconBytes = (byte []) icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
362                                                                                         icon, iconBytes.GetType ());
363                 
364                         Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2A");
365
366                         newIconBytes = (byte []) icoConvFrmTD.ConvertTo (icon, iconBytes.GetType ());
367                         
368                         Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2aA");
369                         
370                         try {
371                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, 
372                                                  icon, typeof (Rectangle));
373                                 Assert.Fail ("CT#3A: must throw NotSupportedException");
374                         } catch (Exception e) {
375                                 Assert.IsTrue (e is NotSupportedException, "CT#3A");
376                         }
377
378                         try {
379                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
380                                                  icon, icon.GetType ());
381                                 Assert.Fail ("CT#4A: must throw NotSupportedException");
382                         } catch (Exception e) {
383                                 Assert.IsTrue (e is NotSupportedException, "CT#4A");
384                         }
385
386                         try {
387                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
388                                                  icon, typeof (Size));
389                                 Assert.Fail ("CT#5A: must throw NotSupportedException");
390                         } catch (Exception e) {
391                                 Assert.IsTrue (e is NotSupportedException, "CT#5A");
392                         }
393
394                         try {
395                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
396                                                  icon, typeof (Bitmap));
397                                 Assert.Fail ("CT#6A: must throw NotSupportedException");
398                         } catch (Exception e) {
399                                 Assert.IsTrue (e is NotSupportedException, "CT#6A");
400                         }
401
402                         try {
403                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
404                                                  icon, typeof (Point));
405                                 Assert.Fail ("CT#7A: must throw NotSupportedException");
406                         } catch (Exception e) {
407                                 Assert.IsTrue (e is NotSupportedException, "CT#7A");
408                         }
409
410                         try {
411                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
412                                                  icon, typeof (Metafile));
413                                 Assert.Fail ("CT#8A: must throw NotSupportedException");
414                         } catch (Exception e) {
415                                 Assert.IsTrue (e is NotSupportedException, "CT#8A");
416                         }
417
418                         try {
419                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
420                                                  icon, typeof (Object));
421                                 Assert.Fail ("CT#9A: must throw NotSupportedException");
422                         } catch (Exception e) {
423                                 Assert.IsTrue (e is NotSupportedException, "CT#9A");
424                         }
425
426                         try {
427                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
428                                                  icon, typeof (int));
429                                 Assert.Fail ("CT#10A: must throw NotSupportedException");
430                         } catch (Exception e) {
431                                 Assert.IsTrue (e is NotSupportedException, "CT#10A");
432                         }*/
433                 }
434                                 
435         }
436 }