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 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
43 public class ImageFormatConverterTest
46 ImageFormatConverter imgFmtConv;
47 ImageFormatConverter imgFmtConvFrmTD;
53 imageFmt = ImageFormat.Bmp;
54 imageFmtStr = imageFmt.ToString ();
56 imgFmtConv = new ImageFormatConverter();
57 imgFmtConvFrmTD = (ImageFormatConverter) TypeDescriptor.GetConverter (imageFmt);
61 public void TestCanConvertFrom ()
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");
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");
80 public void TestCanConvertTo ()
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");
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");
98 public void TestConvertFrom ()
100 Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConv.ConvertFrom (null,
101 CultureInfo.InvariantCulture,
102 ImageFormat.Bmp.ToString ()), "CF#1");
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");
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");
120 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
122 Assert.Fail ("CF#3: must throw NotSupportedException");
123 } catch (Exception e) {
124 Assert.IsTrue (e is NotSupportedException, "CF#3");
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");
136 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
138 Assert.Fail ("CF#5: must throw NotSupportedException");
139 } catch (Exception e) {
140 Assert.IsTrue (e is NotSupportedException, "CF#5");
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");
151 Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
152 CultureInfo.InvariantCulture,
153 ImageFormat.Bmp.ToString ()), "CF#1A");
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");
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");
171 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
173 Assert.Fail ("CF#3A: must throw NotSupportedException");
174 } catch (Exception e) {
175 Assert.IsTrue (e is NotSupportedException, "CF#3A");
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");
187 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
189 Assert.Fail ("CF#5A: must throw NotSupportedException");
190 } catch (Exception e) {
191 Assert.IsTrue (e is NotSupportedException, "CF#5A");
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");
202 private ImageFormat ShortName (string s)
204 return (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, s);
208 public void ConvertFrom_ShortName ()
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");
222 private void LongName (ImageFormat f, string s)
224 Assert.AreEqual (f, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
225 CultureInfo.InvariantCulture, f.ToString ()), s);
229 [NUnit.Framework.Category ("NotWorking")]
230 public void ConvertFrom_LongName ()
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");
245 public void TestConvertTo ()
247 Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (null,
248 CultureInfo.InvariantCulture,
249 imageFmt, typeof (String)), "CT#1");
251 Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (imageFmt,
252 typeof (String)), "CT#1a");
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");
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");
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");
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");
287 Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (null,
288 CultureInfo.InvariantCulture,
289 imageFmt, typeof (String)), "CT#1A");
291 Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (imageFmt,
292 typeof (String)), "CT#1aA");
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");
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");
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");
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");
328 public void GetStandardValuesSupported ()
330 Assert.IsTrue (imgFmtConv.GetStandardValuesSupported (), "GetStandardValuesSupported()");
331 Assert.IsTrue (imgFmtConv.GetStandardValuesSupported (null), "GetStandardValuesSupported(null)");
334 private void CheckStandardValues (string msg, ICollection c)
336 bool memorybmp = false;
347 foreach (ImageFormat iformat in c) {
348 switch (iformat.Guid.ToString ()) {
349 case "b96b3caa-0728-11d3-9d7b-0000f81ef32e":
352 case "b96b3cab-0728-11d3-9d7b-0000f81ef32e":
355 case "b96b3cac-0728-11d3-9d7b-0000f81ef32e":
358 case "b96b3cad-0728-11d3-9d7b-0000f81ef32e":
361 case "b96b3cb0-0728-11d3-9d7b-0000f81ef32e":
364 case "b96b3cae-0728-11d3-9d7b-0000f81ef32e":
367 case "b96b3caf-0728-11d3-9d7b-0000f81ef32e":
370 case "b96b3cb1-0728-11d3-9d7b-0000f81ef32e":
373 case "b96b3cb2-0728-11d3-9d7b-0000f81ef32e":
376 case "b96b3cb5-0728-11d3-9d7b-0000f81ef32e":
380 Assert.Fail ("Unknown GUID {0}", iformat.Guid.ToString ());
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");
397 public void GetStandardValues ()
399 CheckStandardValues ("(empty)", imgFmtConv.GetStandardValues ());
400 CheckStandardValues ("(null)", imgFmtConv.GetStandardValues (null));