2 // Tests for System.Drawing.ImageFormatConverter.cs
5 // Sanjay Gupta (gsanjay@novell.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
32 using System.Collections;
33 using System.ComponentModel;
35 using System.Drawing.Imaging;
36 using System.Globalization;
37 using System.Security.Permissions;
39 namespace MonoTests.System.Drawing
42 public class ImageFormatConverterTest
45 ImageFormatConverter imgFmtConv;
46 ImageFormatConverter imgFmtConvFrmTD;
52 imageFmt = ImageFormat.Bmp;
53 imageFmtStr = imageFmt.ToString ();
55 imgFmtConv = new ImageFormatConverter();
56 imgFmtConvFrmTD = (ImageFormatConverter) TypeDescriptor.GetConverter (imageFmt);
60 public void TestCanConvertFrom ()
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");
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");
79 public void TestCanConvertTo ()
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");
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");
97 public void TestConvertFrom ()
99 Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConv.ConvertFrom (null,
100 CultureInfo.InvariantCulture,
101 ImageFormat.Bmp.ToString ()), "CF#1");
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");
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");
119 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
121 Assert.Fail ("CF#3: must throw NotSupportedException");
122 } catch (Exception e) {
123 Assert.IsTrue (e is NotSupportedException, "CF#3");
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");
135 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
137 Assert.Fail ("CF#5: must throw NotSupportedException");
138 } catch (Exception e) {
139 Assert.IsTrue (e is NotSupportedException, "CF#5");
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");
150 Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
151 CultureInfo.InvariantCulture,
152 ImageFormat.Bmp.ToString ()), "CF#1A");
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");
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");
170 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
172 Assert.Fail ("CF#3A: must throw NotSupportedException");
173 } catch (Exception e) {
174 Assert.IsTrue (e is NotSupportedException, "CF#3A");
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");
186 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
188 Assert.Fail ("CF#5A: must throw NotSupportedException");
189 } catch (Exception e) {
190 Assert.IsTrue (e is NotSupportedException, "CF#5A");
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");
201 private ImageFormat ShortName (string s)
203 return (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, s);
207 public void ConvertFrom_ShortName ()
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");
221 private void LongName (ImageFormat f, string s)
223 Assert.AreEqual (f, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
224 CultureInfo.InvariantCulture, f.ToString ()), s);
228 [NUnit.Framework.Category ("NotWorking")]
229 public void ConvertFrom_LongName ()
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");
244 public void TestConvertTo ()
246 Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (null,
247 CultureInfo.InvariantCulture,
248 imageFmt, typeof (String)), "CT#1");
250 Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (imageFmt,
251 typeof (String)), "CT#1a");
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");
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");
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");
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");
286 Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (null,
287 CultureInfo.InvariantCulture,
288 imageFmt, typeof (String)), "CT#1A");
290 Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (imageFmt,
291 typeof (String)), "CT#1aA");
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");
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");
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");
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");
327 public void GetStandardValuesSupported ()
329 Assert.IsTrue (imgFmtConv.GetStandardValuesSupported (), "GetStandardValuesSupported()");
330 Assert.IsTrue (imgFmtConv.GetStandardValuesSupported (null), "GetStandardValuesSupported(null)");
333 private void CheckStandardValues (string msg, ICollection c)
335 bool memorybmp = false;
346 foreach (ImageFormat iformat in c) {
347 switch (iformat.Guid.ToString ()) {
348 case "b96b3caa-0728-11d3-9d7b-0000f81ef32e":
351 case "b96b3cab-0728-11d3-9d7b-0000f81ef32e":
354 case "b96b3cac-0728-11d3-9d7b-0000f81ef32e":
357 case "b96b3cad-0728-11d3-9d7b-0000f81ef32e":
360 case "b96b3cb0-0728-11d3-9d7b-0000f81ef32e":
363 case "b96b3cae-0728-11d3-9d7b-0000f81ef32e":
366 case "b96b3caf-0728-11d3-9d7b-0000f81ef32e":
369 case "b96b3cb1-0728-11d3-9d7b-0000f81ef32e":
372 case "b96b3cb2-0728-11d3-9d7b-0000f81ef32e":
375 case "b96b3cb5-0728-11d3-9d7b-0000f81ef32e":
379 Assert.Fail ("Unknown GUID {0}", iformat.Guid.ToString ());
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");
396 public void GetStandardValues ()
398 CheckStandardValues ("(empty)", imgFmtConv.GetStandardValues ());
399 CheckStandardValues ("(null)", imgFmtConv.GetStandardValues (null));