[Cleanup] Removed TARGET_JVM
[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                 public void TestCanConvertFrom ()
85                 {
86                         Assert.IsTrue (icoConv.CanConvertFrom (typeof (byte [])), "CCF#1");
87                         Assert.IsTrue (icoConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
88                         Assert.IsTrue (icoConv.CanConvertFrom (null, iconBytes.GetType ()), "CCF#1b");
89                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (String)), "CCF#2");
90                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
91                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
92                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
93                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
94                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
95                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
96                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (int)), "CCF#9");
97                         Assert.IsTrue (! icoConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
98
99                         Assert.IsTrue (icoConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
100                         Assert.IsTrue (icoConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
101                         Assert.IsTrue (icoConvFrmTD.CanConvertFrom (null, iconBytes.GetType ()), "CCF#1bA");
102                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
103                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
104                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
105                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
106                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
107                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
108                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
109                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
110                         Assert.IsTrue (! icoConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
111
112                 }
113
114                 [Test]
115                 public void TestCanConvertTo ()
116                 {
117                         Assert.IsTrue (icoConv.CanConvertTo (typeof (String)), "CCT#1");
118                         Assert.IsTrue (icoConv.CanConvertTo (null, typeof (String)), "CCT#1a");
119                         Assert.IsTrue (icoConv.CanConvertTo (null, iconStr.GetType ()), "CCT#1b");
120                         Assert.IsTrue (icoConv.CanConvertTo (typeof (byte [])), "CCT#2");
121                         Assert.IsTrue (icoConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
122                         Assert.IsTrue (icoConv.CanConvertTo (null, iconBytes.GetType ()), "CCT#2b");
123                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
124                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Point)), "CCT#4");
125                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
126                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Size)), "CCT#6");
127                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
128                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (Object)), "CCT#8");
129                         Assert.IsTrue (! icoConv.CanConvertTo (null, typeof (int)), "CCT#9");
130
131                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
132                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
133                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, iconStr.GetType ()), "CCT#1bA");
134                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
135                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
136                         Assert.IsTrue (icoConvFrmTD.CanConvertTo (null, iconBytes.GetType ()), "CCT#2bA");
137                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
138                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
139                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
140                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
141                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
142                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
143                         Assert.IsTrue (! icoConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
144
145                 }
146
147                 [Test]
148                 public void TestConvertFrom ()
149                 {
150                         Icon newIcon = (Icon) icoConv.ConvertFrom (null, CultureInfo.InvariantCulture, iconBytes);
151
152                         Assert.AreEqual (icon.Height, newIcon.Height, "CF#1");
153                         Assert.AreEqual (icon.Width, newIcon.Width, "CF#1a" );
154                         
155                         try {
156                                 icoConv.ConvertFrom ("System.Drawing.String");
157                                 Assert.Fail ("CF#2: must throw NotSupportedException");
158                         } catch (Exception e) {
159                                 Assert.IsTrue (e is NotSupportedException, "CF#2");
160                         }
161
162                         try {
163                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
164                                                    "System.Drawing.String");
165                                 Assert.Fail ("CF#2a: must throw NotSupportedException");
166                         } catch (Exception e) {
167                                 Assert.IsTrue (e is NotSupportedException, "CF#2a");
168                         }
169
170                         try {
171                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
172                                                    new Bitmap (20, 20));
173                                 Assert.Fail ("CF#3: must throw NotSupportedException");
174                         } catch (Exception e) {
175                                 Assert.IsTrue (e is NotSupportedException, "CF#3");
176                         }
177
178                         try {
179                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
180                                                    new Point (10, 10));
181                                 Assert.Fail ("CF#4: must throw NotSupportedException");
182                         } catch (Exception e) {
183                                 Assert.IsTrue (e is NotSupportedException, "CF#4");
184                         }
185
186                         try {
187                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
188                                                    new SizeF (10, 10));
189                                 Assert.Fail ("CF#5: must throw NotSupportedException");
190                         } catch (Exception e) {
191                                 Assert.IsTrue (e is NotSupportedException, "CF#5");
192                         }
193
194                         try {
195                                 icoConv.ConvertFrom (null, CultureInfo.InvariantCulture,
196                                                    new Object ());
197                                 Assert.Fail ("CF#6: must throw NotSupportedException");
198                         } catch (Exception e) {
199                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
200                         }
201
202
203                         newIcon = (Icon) icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, iconBytes);
204
205                         Assert.AreEqual (icon.Height, newIcon.Height, "CF#1A");
206                         Assert.AreEqual (icon.Width, newIcon.Width, "CF#1Aa");
207                         
208                         try {
209                                 icoConvFrmTD.ConvertFrom ("System.Drawing.String");
210                                 Assert.Fail ("CF#2A: must throw NotSupportedException");
211                         } catch (Exception e) {
212                                 Assert.IsTrue (e is NotSupportedException, "CF#2A");
213                         }
214
215                         try {
216                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
217                                                    "System.Drawing.String");
218                                 Assert.Fail ("CF#2aA: must throw NotSupportedException");
219                         } catch (Exception e) {
220                                 Assert.IsTrue (e is NotSupportedException, "CF#2aA");
221                         }
222
223                         try {
224                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
225                                                    new Bitmap (20, 20));
226                                 Assert.Fail ("CF#3A: must throw NotSupportedException");
227                         } catch (Exception e) {
228                                 Assert.IsTrue (e is NotSupportedException, "CF#3A");
229                         }
230
231                         try {
232                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
233                                                    new Point (10, 10));
234                                 Assert.Fail ("CF#4A: must throw NotSupportedException");
235                         } catch (Exception e) {
236                                 Assert.IsTrue (e is NotSupportedException, "CF#4A");
237                         }
238
239                         try {
240                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
241                                                    new SizeF (10, 10));
242                                 Assert.Fail ("CF#5A: must throw NotSupportedException");
243                         } catch (Exception e) {
244                                 Assert.IsTrue (e is NotSupportedException, "CF#5A");
245                         }
246
247                         try {
248                                 icoConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
249                                                    new Object ());
250                                 Assert.Fail ("CF#6A: must throw NotSupportedException");
251                         } catch (Exception e) {
252                                 Assert.IsTrue (e is NotSupportedException, "CF#6A");
253                         }
254
255                 }
256
257                 [Test]
258                 public void TestConvertTo ()
259                 {
260                         Assert.AreEqual (iconStr, (String) icoConv.ConvertTo (null,
261                                                                 CultureInfo.InvariantCulture,
262                                                                 icon, typeof (String)), "CT#1");
263
264                         Assert.AreEqual (iconStr, (String) icoConv.ConvertTo (icon, 
265                                                                         typeof (String)), "CT#1a");
266                                 
267                         /*byte [] newIconBytes = (byte []) icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
268                                                                                         icon, iconBytes.GetType ());
269                 
270                         Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2");
271
272                         newIconBytes = (byte []) icoConv.ConvertTo (icon, iconBytes.GetType ());
273                         
274                         Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2a");
275
276                                                 
277                         try {
278                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture, 
279                                                  icon, typeof (Rectangle));
280                                 Assert.Fail ("CT#3: must throw NotSupportedException");
281                         } catch (Exception e) {
282                                 Assert.IsTrue ( e is NotSupportedException, "CT#3");
283                         }
284
285                         try {
286                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
287                                                  icon, icon.GetType ());
288                                 Assert.Fail ("CT#4: must throw NotSupportedException");
289                         } catch (Exception e) {
290                                 Assert.IsTrue (e is NotSupportedException, "CT#4");
291                         }
292
293                         try {
294                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
295                                                  icon, typeof (Size));
296                                 Assert.Fail ("CT#5: must throw NotSupportedException");
297                         } catch (Exception e) {
298                                 Assert.IsTrue (e is NotSupportedException, "CT#5");
299                         }
300
301                         try {
302                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
303                                                  icon, typeof (Bitmap));
304                                 Assert.Fail ("CT#6: must throw NotSupportedException");
305                         } catch (Exception e) {
306                                 Assert.IsTrue ( e is NotSupportedException, "CT#6");
307                         }
308
309                         try {
310                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
311                                                  icon, typeof (Point));
312                                 Assert.Fail ("CT#7: must throw NotSupportedException");
313                         } catch (Exception e) {
314                                 Assert.IsTrue (e is NotSupportedException, "CT#7");
315                         }
316
317                         try {
318                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
319                                                  icon, typeof (Metafile));
320                                 Assert.Fail ("CT#8: must throw NotSupportedException");
321                         } catch (Exception e) {
322                                 Assert.IsTrue (e is NotSupportedException, "CT#8");
323                         }
324
325                         try {
326                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
327                                                  icon, typeof (Object));
328                                 Assert.Fail ("CT#9: must throw NotSupportedException");
329                         } catch (Exception e) {
330                                 Assert.IsTrue (e is NotSupportedException, "CT#9");
331                         }
332
333                         try {
334                                 icoConv.ConvertTo (null, CultureInfo.InvariantCulture,
335                                                  icon, typeof (int));
336                                 Assert.Fail ("CT#10: must throw NotSupportedException");
337                         } catch (Exception e) {
338                                 Assert.IsTrue (e is NotSupportedException, "CT#10");
339                         }*/
340
341
342                         Assert.AreEqual (iconStr, (String) icoConvFrmTD.ConvertTo (null,
343                                                                 CultureInfo.InvariantCulture,
344                                                                 icon, typeof (String)), "CT#1A");
345
346                         Assert.AreEqual (iconStr, (String) icoConvFrmTD.ConvertTo (icon, 
347                                                                         typeof (String)), "CT#1aA");
348                                 
349                         /*newIconBytes = (byte []) icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
350                                                                                         icon, iconBytes.GetType ());
351                 
352                         Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2A");
353
354                         newIconBytes = (byte []) icoConvFrmTD.ConvertTo (icon, iconBytes.GetType ());
355                         
356                         Assert.AreEqual (iconBytes.Length, newIconBytes.Length, "CT#2aA");
357                         
358                         try {
359                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, 
360                                                  icon, typeof (Rectangle));
361                                 Assert.Fail ("CT#3A: must throw NotSupportedException");
362                         } catch (Exception e) {
363                                 Assert.IsTrue (e is NotSupportedException, "CT#3A");
364                         }
365
366                         try {
367                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
368                                                  icon, icon.GetType ());
369                                 Assert.Fail ("CT#4A: must throw NotSupportedException");
370                         } catch (Exception e) {
371                                 Assert.IsTrue (e is NotSupportedException, "CT#4A");
372                         }
373
374                         try {
375                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
376                                                  icon, typeof (Size));
377                                 Assert.Fail ("CT#5A: must throw NotSupportedException");
378                         } catch (Exception e) {
379                                 Assert.IsTrue (e is NotSupportedException, "CT#5A");
380                         }
381
382                         try {
383                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
384                                                  icon, typeof (Bitmap));
385                                 Assert.Fail ("CT#6A: must throw NotSupportedException");
386                         } catch (Exception e) {
387                                 Assert.IsTrue (e is NotSupportedException, "CT#6A");
388                         }
389
390                         try {
391                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
392                                                  icon, typeof (Point));
393                                 Assert.Fail ("CT#7A: must throw NotSupportedException");
394                         } catch (Exception e) {
395                                 Assert.IsTrue (e is NotSupportedException, "CT#7A");
396                         }
397
398                         try {
399                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
400                                                  icon, typeof (Metafile));
401                                 Assert.Fail ("CT#8A: must throw NotSupportedException");
402                         } catch (Exception e) {
403                                 Assert.IsTrue (e is NotSupportedException, "CT#8A");
404                         }
405
406                         try {
407                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
408                                                  icon, typeof (Object));
409                                 Assert.Fail ("CT#9A: must throw NotSupportedException");
410                         } catch (Exception e) {
411                                 Assert.IsTrue (e is NotSupportedException, "CT#9A");
412                         }
413
414                         try {
415                                 icoConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
416                                                  icon, typeof (int));
417                                 Assert.Fail ("CT#10A: must throw NotSupportedException");
418                         } catch (Exception e) {
419                                 Assert.IsTrue (e is NotSupportedException, "CT#10A");
420                         }*/
421
422                         Assert.AreEqual ("(none)", (string) icoConv.ConvertTo (null, typeof (string)), "CT#2");
423                 }
424                                 
425         }
426 }