Remove CAS from System.Drawing unit tests
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestImageFormatConverter.cs
1 //
2 // Tests for System.Drawing.ImageFormatConverter.cs 
3 //
4 // Authors:
5 //      Sanjay Gupta (gsanjay@novell.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Drawing;
35 using System.Drawing.Imaging;
36 using System.Globalization;
37 using System.Security.Permissions;
38
39 namespace MonoTests.System.Drawing
40 {
41         [TestFixture]
42         public class ImageFormatConverterTest
43         {
44                 ImageFormat imageFmt;           
45                 ImageFormatConverter imgFmtConv;
46                 ImageFormatConverter imgFmtConvFrmTD;
47                 String imageFmtStr;
48
49                 [SetUp]
50                 public void SetUp ()            
51                 {
52                         imageFmt = ImageFormat.Bmp; 
53                         imageFmtStr = imageFmt.ToString ();
54                 
55                         imgFmtConv = new ImageFormatConverter();
56                         imgFmtConvFrmTD = (ImageFormatConverter) TypeDescriptor.GetConverter (imageFmt);                        
57                 }
58
59                 [Test]
60                 public void TestCanConvertFrom ()
61                 {
62                         Assert.IsTrue (imgFmtConv.CanConvertFrom (typeof (String)), "CCF#1");
63                         Assert.IsTrue (imgFmtConv.CanConvertFrom (null, typeof (String)), "CCF#1a");
64                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (ImageFormat)), "CCF#2");
65                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (Guid)), "CCF#3");
66                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (Object)), "CCF#4");
67                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (int)), "CCF#5");
68
69                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertFrom (typeof (String)), "CCF#1A");
70                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#1aA");
71                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (ImageFormat)), "CCF#2A");
72                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (Guid)), "CCF#3A");
73                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#4A");
74                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#5A");
75
76                 }
77
78                 [Test]
79                 public void TestCanConvertTo ()
80                 {
81                         Assert.IsTrue (imgFmtConv.CanConvertTo (typeof (String)), "CCT#1");
82                         Assert.IsTrue (imgFmtConv.CanConvertTo (null, typeof (String)), "CCT#1a");
83                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (ImageFormat)), "CCT#2");
84                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (Guid)), "CCT#3");
85                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (Object)), "CCT#4");
86                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (int)), "CCT#5");
87
88                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
89                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
90                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (ImageFormat)), "CCT#2A");
91                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (Guid)), "CCT#3A");
92                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#4A");
93                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#5A");
94                 }
95
96                 [Test]
97                 public void TestConvertFrom ()
98                 {
99                         Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConv.ConvertFrom (null,
100                                                                 CultureInfo.InvariantCulture,
101                                                                 ImageFormat.Bmp.ToString ()), "CF#1");
102                         
103                         try {
104                                 imgFmtConv.ConvertFrom ("System.Drawing.String");
105                                 Assert.Fail ("CF#2: must throw NotSupportedException");
106                         } catch (Exception e) {
107                                 Assert.IsTrue (e is NotSupportedException, "CF#2");
108                         }
109
110                         try {
111                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
112                                                    "System.Drawing.String");
113                                 Assert.Fail ("CF#2a: must throw NotSupportedException");
114                         } catch (Exception e) {
115                                 Assert.IsTrue (e is NotSupportedException, "CF#2a");
116                         }
117
118                         try {
119                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
120                                                    ImageFormat.Bmp);
121                                 Assert.Fail ("CF#3: must throw NotSupportedException");
122                         } catch (Exception e) {
123                                 Assert.IsTrue (e is NotSupportedException, "CF#3");
124                         }
125
126                         try {
127                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
128                                                    ImageFormat.Bmp.Guid);
129                                 Assert.Fail ("CF#4: must throw NotSupportedException");
130                         } catch (Exception e) {
131                                 Assert.IsTrue (e is NotSupportedException, "CF#4");
132                         }
133
134                         try {
135                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
136                                                    new Object ());
137                                 Assert.Fail ("CF#5: must throw NotSupportedException");
138                         } catch (Exception e) {
139                                 Assert.IsTrue (e is NotSupportedException, "CF#5");
140                         }
141
142                         try {
143                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture, 10);
144                                 Assert.Fail ("CF#6: must throw NotSupportedException");
145                         } catch (Exception e) {
146                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
147                         }
148
149                         
150                         Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
151                                                                 CultureInfo.InvariantCulture,
152                                                                 ImageFormat.Bmp.ToString ()), "CF#1A");
153                         
154                         try {
155                                 imgFmtConvFrmTD.ConvertFrom ("System.Drawing.String");
156                                 Assert.Fail ("CF#2A: must throw NotSupportedException");
157                         } catch (Exception e) {
158                                 Assert.IsTrue (e is NotSupportedException, "CF#2A");
159                         }
160
161                         try {
162                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
163                                                    "System.Drawing.String");
164                                 Assert.Fail ("CF#2aA: must throw NotSupportedException");
165                         } catch (Exception e) {
166                                 Assert.IsTrue (e is NotSupportedException, "CF#2aA");
167                         }
168
169                         try {
170                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
171                                                    ImageFormat.Bmp);
172                                 Assert.Fail ("CF#3A: must throw NotSupportedException");
173                         } catch (Exception e) {
174                                 Assert.IsTrue (e is NotSupportedException, "CF#3A");
175                         }
176
177                         try {
178                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
179                                                    ImageFormat.Bmp.Guid);
180                                 Assert.Fail ("CF#4A: must throw NotSupportedException");
181                         } catch (Exception e) {
182                                 Assert.IsTrue (e is NotSupportedException, "CF#4A");
183                         }
184
185                         try {
186                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
187                                                    new Object ());
188                                 Assert.Fail ("CF#5A: must throw NotSupportedException");
189                         } catch (Exception e) {
190                                 Assert.IsTrue (e is NotSupportedException, "CF#5A");
191                         }
192
193                         try {
194                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, 10);
195                                 Assert.Fail ("CF#6A: must throw NotSupportedException");
196                         } catch (Exception e) {
197                                 Assert.IsTrue (e is NotSupportedException, "CF#6A");
198                         }
199                 }
200
201                 private ImageFormat ShortName (string s)
202                 {
203                         return (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, s);
204                 }
205
206                 [Test]
207                 public void ConvertFrom_ShortName ()
208                 {
209                         Assert.AreEqual (ImageFormat.Bmp, ShortName ("Bmp"), "Bmp");
210                         Assert.AreEqual (ImageFormat.Emf, ShortName ("Emf"), "Emf");
211                         Assert.AreEqual (ImageFormat.Exif, ShortName ("Exif"), "Exif");
212                         Assert.AreEqual (ImageFormat.Gif, ShortName ("Gif"), "Gif");
213                         Assert.AreEqual (ImageFormat.Tiff, ShortName ("Tiff"), "Tiff");
214                         Assert.AreEqual (ImageFormat.Png, ShortName ("Png"), "Png");
215                         Assert.AreEqual (ImageFormat.MemoryBmp, ShortName ("MemoryBmp"), "MemoryBmp");
216                         Assert.AreEqual (ImageFormat.Icon, ShortName ("Icon"), "Icon");
217                         Assert.AreEqual (ImageFormat.Jpeg, ShortName ("Jpeg"), "Jpeg");
218                         Assert.AreEqual (ImageFormat.Wmf, ShortName ("Wmf"), "Wmf");
219                 }
220
221                 private void LongName (ImageFormat f, string s)
222                 {
223                         Assert.AreEqual (f, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
224                                 CultureInfo.InvariantCulture, f.ToString ()), s);
225                 }
226
227                 [Test]
228                 [NUnit.Framework.Category ("NotWorking")]
229                 public void ConvertFrom_LongName ()
230                 {
231                         LongName (ImageFormat.Bmp, "Bmp");
232                         LongName (ImageFormat.Emf, "Emf");
233                         LongName (ImageFormat.Exif, "Exif");
234                         LongName (ImageFormat.Gif, "Gif");
235                         LongName (ImageFormat.Tiff, "Tiff");
236                         LongName (ImageFormat.Png, "Png");
237                         LongName (ImageFormat.MemoryBmp, "MemoryBmp");
238                         LongName (ImageFormat.Icon, "Icon");
239                         LongName (ImageFormat.Jpeg, "Jpeg");
240                         LongName (ImageFormat.Wmf, "Wmf");
241                 }
242
243                 [Test]
244                 public void TestConvertTo ()
245                 {
246                         Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (null,
247                                                                 CultureInfo.InvariantCulture,
248                                                                 imageFmt, typeof (String)), "CT#1");
249
250                         Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (imageFmt, 
251                                                                         typeof (String)), "CT#1a");
252                                                         
253                         try {
254                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture, 
255                                                  imageFmt, typeof (ImageFormat));
256                                 Assert.Fail ("CT#2: must throw NotSupportedException");
257                         } catch (Exception e) {
258                                 Assert.IsTrue (e is NotSupportedException, "CT#2");
259                         }
260
261                         try {
262                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
263                                                  imageFmt, typeof (Guid));
264                                 Assert.Fail ("CT#2a: must throw NotSupportedException");
265                         } catch (Exception e) {
266                                 Assert.IsTrue (e is NotSupportedException, "CT#2a");
267                         }
268
269                         try {
270                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
271                                                  imageFmt, typeof (Object));
272                                 Assert.Fail ("CT#3: must throw NotSupportedException");
273                         } catch (Exception e) {
274                                 Assert.IsTrue (e is NotSupportedException, "CT#3");
275                         }
276
277                         try {
278                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
279                                                  imageFmt, typeof (int));
280                                 Assert.Fail ("CT#4: must throw NotSupportedException");
281                         } catch (Exception e) {
282                                 Assert.IsTrue (e is NotSupportedException, "CT#4");
283                         }
284
285
286                         Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (null,
287                                                                 CultureInfo.InvariantCulture,
288                                                                 imageFmt, typeof (String)), "CT#1A");
289
290                         Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (imageFmt, 
291                                                                         typeof (String)), "CT#1aA");
292                                                         
293                         try {
294                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, 
295                                                  imageFmt, typeof (ImageFormat));
296                                 Assert.Fail ("CT#2A: must throw NotSupportedException");
297                         } catch (Exception e) {
298                                 Assert.IsTrue (e is NotSupportedException, "CT#2A");
299                         }
300
301                         try {
302                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
303                                                  imageFmt, typeof (Guid));
304                                 Assert.Fail ("CT#2aA: must throw NotSupportedException");
305                         } catch (Exception e) {
306                                 Assert.IsTrue (e is NotSupportedException, "CT#2aA");
307                         }
308
309                         try {
310                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
311                                                  imageFmt, typeof (Object));
312                                 Assert.Fail ("CT#3A: must throw NotSupportedException");
313                         } catch (Exception e) {
314                                 Assert.IsTrue (e is NotSupportedException, "CT#3A");
315                         }
316
317                         try {
318                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
319                                                  imageFmt, typeof (int));
320                                 Assert.Fail ("CT#4A: must throw NotSupportedException");
321                         } catch (Exception e) {
322                                 Assert.IsTrue (e is NotSupportedException, "CT#4A");
323                         }
324                 }
325
326                 [Test]
327                 public void GetStandardValuesSupported ()
328                 {
329                         Assert.IsTrue (imgFmtConv.GetStandardValuesSupported (), "GetStandardValuesSupported()");
330                         Assert.IsTrue (imgFmtConv.GetStandardValuesSupported (null), "GetStandardValuesSupported(null)");
331                 }
332
333                 private void CheckStandardValues (string msg, ICollection c)
334                 {
335                         bool memorybmp = false;
336                         bool bmp = false;
337                         bool emf = false;
338                         bool wmf = false;
339                         bool gif = false;
340                         bool jpeg = false;
341                         bool png = false;
342                         bool tiff = false;
343                         bool exif = false;
344                         bool icon = false;
345
346                         foreach (ImageFormat iformat in c) {
347                                 switch (iformat.Guid.ToString ()) {
348                                 case "b96b3caa-0728-11d3-9d7b-0000f81ef32e":
349                                         memorybmp = true;
350                                         break;
351                                 case "b96b3cab-0728-11d3-9d7b-0000f81ef32e":
352                                         bmp = true;
353                                         break;
354                                 case "b96b3cac-0728-11d3-9d7b-0000f81ef32e":
355                                         emf = true;
356                                         break;
357                                 case "b96b3cad-0728-11d3-9d7b-0000f81ef32e":
358                                         wmf = true;
359                                         break;
360                                 case "b96b3cb0-0728-11d3-9d7b-0000f81ef32e":
361                                         gif = true;
362                                         break;
363                                 case "b96b3cae-0728-11d3-9d7b-0000f81ef32e":
364                                         jpeg = true;
365                                         break;
366                                 case "b96b3caf-0728-11d3-9d7b-0000f81ef32e":
367                                         png = true;
368                                         break;
369                                 case "b96b3cb1-0728-11d3-9d7b-0000f81ef32e":
370                                         tiff = true;
371                                         break;
372                                 case "b96b3cb2-0728-11d3-9d7b-0000f81ef32e":
373                                         exif = true;
374                                         break;
375                                 case "b96b3cb5-0728-11d3-9d7b-0000f81ef32e":
376                                         icon = true;
377                                         break;
378                                 default:
379                                         Assert.Fail ("Unknown GUID {0}", iformat.Guid.ToString ());
380                                         break;
381                                 }
382                         }
383                         Assert.IsTrue (memorybmp, "MemoryBMP");
384                         Assert.IsTrue (bmp, "Bmp");
385                         Assert.IsTrue (emf, "Emf");
386                         Assert.IsTrue (wmf, "Wmf");
387                         Assert.IsTrue (gif, "Gif");
388                         Assert.IsTrue (jpeg, "Jpeg");
389                         Assert.IsTrue (png, "Png");
390                         Assert.IsTrue (tiff, "Tiff");
391                         Assert.IsTrue (exif, "Exif");
392                         Assert.IsTrue (icon, "Icon");
393                 }
394
395                 [Test]
396                 public void GetStandardValues ()
397                 {
398                         CheckStandardValues ("(empty)", imgFmtConv.GetStandardValues ());
399                         CheckStandardValues ("(null)", imgFmtConv.GetStandardValues (null));
400                 }
401         }
402 }