2007-01-15 Sebastien Pouliot <sebastien@ximian.com>
[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         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
43         public class ImageFormatConverterTest
44         {
45                 ImageFormat imageFmt;           
46                 ImageFormatConverter imgFmtConv;
47                 ImageFormatConverter imgFmtConvFrmTD;
48                 String imageFmtStr;
49
50                 [SetUp]
51                 public void SetUp ()            
52                 {
53                         imageFmt = ImageFormat.Bmp; 
54                         imageFmtStr = imageFmt.ToString ();
55                 
56                         imgFmtConv = new ImageFormatConverter();
57                         imgFmtConvFrmTD = (ImageFormatConverter) TypeDescriptor.GetConverter (imageFmt);                        
58                 }
59
60                 [Test]
61                 public void TestCanConvertFrom ()
62                 {
63                         Assert.IsTrue (imgFmtConv.CanConvertFrom (typeof (String)), "CCF#1");
64                         Assert.IsTrue (imgFmtConv.CanConvertFrom (null, typeof (String)), "CCF#1a");
65                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (ImageFormat)), "CCF#2");
66                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (Guid)), "CCF#3");
67                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (Object)), "CCF#4");
68                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (int)), "CCF#5");
69
70                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertFrom (typeof (String)), "CCF#1A");
71                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#1aA");
72                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (ImageFormat)), "CCF#2A");
73                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (Guid)), "CCF#3A");
74                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#4A");
75                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#5A");
76
77                 }
78
79                 [Test]
80                 public void TestCanConvertTo ()
81                 {
82                         Assert.IsTrue (imgFmtConv.CanConvertTo (typeof (String)), "CCT#1");
83                         Assert.IsTrue (imgFmtConv.CanConvertTo (null, typeof (String)), "CCT#1a");
84                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (ImageFormat)), "CCT#2");
85                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (Guid)), "CCT#3");
86                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (Object)), "CCT#4");
87                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (int)), "CCT#5");
88
89                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
90                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
91                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (ImageFormat)), "CCT#2A");
92                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (Guid)), "CCT#3A");
93                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#4A");
94                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#5A");
95                 }
96
97                 [Test]
98                 public void TestConvertFrom ()
99                 {
100                         Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConv.ConvertFrom (null,
101                                                                 CultureInfo.InvariantCulture,
102                                                                 ImageFormat.Bmp.ToString ()), "CF#1");
103                         
104                         try {
105                                 imgFmtConv.ConvertFrom ("System.Drawing.String");
106                                 Assert.Fail ("CF#2: must throw NotSupportedException");
107                         } catch (Exception e) {
108                                 Assert.IsTrue (e is NotSupportedException, "CF#2");
109                         }
110
111                         try {
112                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
113                                                    "System.Drawing.String");
114                                 Assert.Fail ("CF#2a: must throw NotSupportedException");
115                         } catch (Exception e) {
116                                 Assert.IsTrue (e is NotSupportedException, "CF#2a");
117                         }
118
119                         try {
120                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
121                                                    ImageFormat.Bmp);
122                                 Assert.Fail ("CF#3: must throw NotSupportedException");
123                         } catch (Exception e) {
124                                 Assert.IsTrue (e is NotSupportedException, "CF#3");
125                         }
126
127                         try {
128                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
129                                                    ImageFormat.Bmp.Guid);
130                                 Assert.Fail ("CF#4: must throw NotSupportedException");
131                         } catch (Exception e) {
132                                 Assert.IsTrue (e is NotSupportedException, "CF#4");
133                         }
134
135                         try {
136                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
137                                                    new Object ());
138                                 Assert.Fail ("CF#5: must throw NotSupportedException");
139                         } catch (Exception e) {
140                                 Assert.IsTrue (e is NotSupportedException, "CF#5");
141                         }
142
143                         try {
144                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture, 10);
145                                 Assert.Fail ("CF#6: must throw NotSupportedException");
146                         } catch (Exception e) {
147                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
148                         }
149
150                         
151                         Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
152                                                                 CultureInfo.InvariantCulture,
153                                                                 ImageFormat.Bmp.ToString ()), "CF#1A");
154                         
155                         try {
156                                 imgFmtConvFrmTD.ConvertFrom ("System.Drawing.String");
157                                 Assert.Fail ("CF#2A: must throw NotSupportedException");
158                         } catch (Exception e) {
159                                 Assert.IsTrue (e is NotSupportedException, "CF#2A");
160                         }
161
162                         try {
163                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
164                                                    "System.Drawing.String");
165                                 Assert.Fail ("CF#2aA: must throw NotSupportedException");
166                         } catch (Exception e) {
167                                 Assert.IsTrue (e is NotSupportedException, "CF#2aA");
168                         }
169
170                         try {
171                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
172                                                    ImageFormat.Bmp);
173                                 Assert.Fail ("CF#3A: must throw NotSupportedException");
174                         } catch (Exception e) {
175                                 Assert.IsTrue (e is NotSupportedException, "CF#3A");
176                         }
177
178                         try {
179                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
180                                                    ImageFormat.Bmp.Guid);
181                                 Assert.Fail ("CF#4A: must throw NotSupportedException");
182                         } catch (Exception e) {
183                                 Assert.IsTrue (e is NotSupportedException, "CF#4A");
184                         }
185
186                         try {
187                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
188                                                    new Object ());
189                                 Assert.Fail ("CF#5A: must throw NotSupportedException");
190                         } catch (Exception e) {
191                                 Assert.IsTrue (e is NotSupportedException, "CF#5A");
192                         }
193
194                         try {
195                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, 10);
196                                 Assert.Fail ("CF#6A: must throw NotSupportedException");
197                         } catch (Exception e) {
198                                 Assert.IsTrue (e is NotSupportedException, "CF#6A");
199                         }
200                 }
201
202                 private ImageFormat ShortName (string s)
203                 {
204                         return (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, s);
205                 }
206
207                 [Test]
208                 public void ConvertFrom_ShortName ()
209                 {
210                         Assert.AreEqual (ImageFormat.Bmp, ShortName ("Bmp"), "Bmp");
211                         Assert.AreEqual (ImageFormat.Emf, ShortName ("Emf"), "Emf");
212                         Assert.AreEqual (ImageFormat.Exif, ShortName ("Exif"), "Exif");
213                         Assert.AreEqual (ImageFormat.Gif, ShortName ("Gif"), "Gif");
214                         Assert.AreEqual (ImageFormat.Tiff, ShortName ("Tiff"), "Tiff");
215                         Assert.AreEqual (ImageFormat.Png, ShortName ("Png"), "Png");
216                         Assert.AreEqual (ImageFormat.MemoryBmp, ShortName ("MemoryBmp"), "MemoryBmp");
217                         Assert.AreEqual (ImageFormat.Icon, ShortName ("Icon"), "Icon");
218                         Assert.AreEqual (ImageFormat.Jpeg, ShortName ("Jpeg"), "Jpeg");
219                         Assert.AreEqual (ImageFormat.Wmf, ShortName ("Wmf"), "Wmf");
220                 }
221
222                 private void LongName (ImageFormat f, string s)
223                 {
224                         Assert.AreEqual (f, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
225                                 CultureInfo.InvariantCulture, f.ToString ()), s);
226                 }
227
228                 [Test]
229                 [NUnit.Framework.Category ("NotWorking")]
230                 public void ConvertFrom_LongName ()
231                 {
232                         LongName (ImageFormat.Bmp, "Bmp");
233                         LongName (ImageFormat.Emf, "Emf");
234                         LongName (ImageFormat.Exif, "Exif");
235                         LongName (ImageFormat.Gif, "Gif");
236                         LongName (ImageFormat.Tiff, "Tiff");
237                         LongName (ImageFormat.Png, "Png");
238                         LongName (ImageFormat.MemoryBmp, "MemoryBmp");
239                         LongName (ImageFormat.Icon, "Icon");
240                         LongName (ImageFormat.Jpeg, "Jpeg");
241                         LongName (ImageFormat.Wmf, "Wmf");
242                 }
243
244                 [Test]
245                 public void TestConvertTo ()
246                 {
247                         Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (null,
248                                                                 CultureInfo.InvariantCulture,
249                                                                 imageFmt, typeof (String)), "CT#1");
250
251                         Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (imageFmt, 
252                                                                         typeof (String)), "CT#1a");
253                                                         
254                         try {
255                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture, 
256                                                  imageFmt, typeof (ImageFormat));
257                                 Assert.Fail ("CT#2: must throw NotSupportedException");
258                         } catch (Exception e) {
259                                 Assert.IsTrue (e is NotSupportedException, "CT#2");
260                         }
261
262                         try {
263                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
264                                                  imageFmt, typeof (Guid));
265                                 Assert.Fail ("CT#2a: must throw NotSupportedException");
266                         } catch (Exception e) {
267                                 Assert.IsTrue (e is NotSupportedException, "CT#2a");
268                         }
269
270                         try {
271                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
272                                                  imageFmt, typeof (Object));
273                                 Assert.Fail ("CT#3: must throw NotSupportedException");
274                         } catch (Exception e) {
275                                 Assert.IsTrue (e is NotSupportedException, "CT#3");
276                         }
277
278                         try {
279                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
280                                                  imageFmt, typeof (int));
281                                 Assert.Fail ("CT#4: must throw NotSupportedException");
282                         } catch (Exception e) {
283                                 Assert.IsTrue (e is NotSupportedException, "CT#4");
284                         }
285
286
287                         Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (null,
288                                                                 CultureInfo.InvariantCulture,
289                                                                 imageFmt, typeof (String)), "CT#1A");
290
291                         Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (imageFmt, 
292                                                                         typeof (String)), "CT#1aA");
293                                                         
294                         try {
295                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, 
296                                                  imageFmt, typeof (ImageFormat));
297                                 Assert.Fail ("CT#2A: must throw NotSupportedException");
298                         } catch (Exception e) {
299                                 Assert.IsTrue (e is NotSupportedException, "CT#2A");
300                         }
301
302                         try {
303                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
304                                                  imageFmt, typeof (Guid));
305                                 Assert.Fail ("CT#2aA: must throw NotSupportedException");
306                         } catch (Exception e) {
307                                 Assert.IsTrue (e is NotSupportedException, "CT#2aA");
308                         }
309
310                         try {
311                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
312                                                  imageFmt, typeof (Object));
313                                 Assert.Fail ("CT#3A: must throw NotSupportedException");
314                         } catch (Exception e) {
315                                 Assert.IsTrue (e is NotSupportedException, "CT#3A");
316                         }
317
318                         try {
319                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
320                                                  imageFmt, typeof (int));
321                                 Assert.Fail ("CT#4A: must throw NotSupportedException");
322                         } catch (Exception e) {
323                                 Assert.IsTrue (e is NotSupportedException, "CT#4A");
324                         }
325                 }
326
327                 [Test]
328                 public void GetStandardValuesSupported ()
329                 {
330                         Assert.IsTrue (imgFmtConv.GetStandardValuesSupported (), "GetStandardValuesSupported()");
331                         Assert.IsTrue (imgFmtConv.GetStandardValuesSupported (null), "GetStandardValuesSupported(null)");
332                 }
333
334                 private void CheckStandardValues (string msg, ICollection c)
335                 {
336                         bool memorybmp = false;
337                         bool bmp = false;
338                         bool emf = false;
339                         bool wmf = false;
340                         bool gif = false;
341                         bool jpeg = false;
342                         bool png = false;
343                         bool tiff = false;
344                         bool exif = false;
345                         bool icon = false;
346
347                         foreach (ImageFormat iformat in c) {
348                                 switch (iformat.Guid.ToString ()) {
349                                 case "b96b3caa-0728-11d3-9d7b-0000f81ef32e":
350                                         memorybmp = true;
351                                         break;
352                                 case "b96b3cab-0728-11d3-9d7b-0000f81ef32e":
353                                         bmp = true;
354                                         break;
355                                 case "b96b3cac-0728-11d3-9d7b-0000f81ef32e":
356                                         emf = true;
357                                         break;
358                                 case "b96b3cad-0728-11d3-9d7b-0000f81ef32e":
359                                         wmf = true;
360                                         break;
361                                 case "b96b3cb0-0728-11d3-9d7b-0000f81ef32e":
362                                         gif = true;
363                                         break;
364                                 case "b96b3cae-0728-11d3-9d7b-0000f81ef32e":
365                                         jpeg = true;
366                                         break;
367                                 case "b96b3caf-0728-11d3-9d7b-0000f81ef32e":
368                                         png = true;
369                                         break;
370                                 case "b96b3cb1-0728-11d3-9d7b-0000f81ef32e":
371                                         tiff = true;
372                                         break;
373                                 case "b96b3cb2-0728-11d3-9d7b-0000f81ef32e":
374                                         exif = true;
375                                         break;
376                                 case "b96b3cb5-0728-11d3-9d7b-0000f81ef32e":
377                                         icon = true;
378                                         break;
379                                 default:
380                                         Assert.Fail ("Unknown GUID {0}", iformat.Guid.ToString ());
381                                         break;
382                                 }
383                         }
384                         Assert.IsTrue (memorybmp, "MemoryBMP");
385                         Assert.IsTrue (bmp, "Bmp");
386                         Assert.IsTrue (emf, "Emf");
387                         Assert.IsTrue (wmf, "Wmf");
388                         Assert.IsTrue (gif, "Gif");
389                         Assert.IsTrue (jpeg, "Jpeg");
390                         Assert.IsTrue (png, "Png");
391                         Assert.IsTrue (tiff, "Tiff");
392                         Assert.IsTrue (exif, "Exif");
393                         Assert.IsTrue (icon, "Icon");
394                 }
395
396                 [Test]
397                 public void GetStandardValues ()
398                 {
399                         CheckStandardValues ("(empty)", imgFmtConv.GetStandardValues ());
400                         CheckStandardValues ("(null)", imgFmtConv.GetStandardValues (null));
401                 }
402         }
403 }