2 // Tests for System.Drawing.ImageFormatConverter.cs
5 // Sanjay Gupta (gsanjay@novell.com)
7 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using NUnit.Framework;
32 using System.Drawing.Imaging;
33 using System.ComponentModel;
34 using System.Globalization;
35 using System.Security.Permissions;
37 namespace MonoTests.System.Drawing
40 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
41 public class ImageFormatConverterTest
44 ImageFormatConverter imgFmtConv;
45 ImageFormatConverter imgFmtConvFrmTD;
51 imageFmt = ImageFormat.Bmp;
52 imageFmtStr = imageFmt.ToString ();
54 imgFmtConv = new ImageFormatConverter();
55 imgFmtConvFrmTD = (ImageFormatConverter) TypeDescriptor.GetConverter (imageFmt);
59 public void TestCanConvertFrom ()
61 Assert.IsTrue (imgFmtConv.CanConvertFrom (typeof (String)), "CCF#1");
62 Assert.IsTrue (imgFmtConv.CanConvertFrom (null, typeof (String)), "CCF#1a");
63 Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (ImageFormat)), "CCF#2");
64 Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (Guid)), "CCF#3");
65 Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (Object)), "CCF#4");
66 Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (int)), "CCF#5");
68 Assert.IsTrue (imgFmtConvFrmTD.CanConvertFrom (typeof (String)), "CCF#1A");
69 Assert.IsTrue (imgFmtConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#1aA");
70 Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (ImageFormat)), "CCF#2A");
71 Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (Guid)), "CCF#3A");
72 Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#4A");
73 Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#5A");
78 public void TestCanConvertTo ()
80 Assert.IsTrue (imgFmtConv.CanConvertTo (typeof (String)), "CCT#1");
81 Assert.IsTrue (imgFmtConv.CanConvertTo (null, typeof (String)), "CCT#1a");
82 Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (ImageFormat)), "CCT#2");
83 Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (Guid)), "CCT#3");
84 Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (Object)), "CCT#4");
85 Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (int)), "CCT#5");
87 Assert.IsTrue (imgFmtConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
88 Assert.IsTrue (imgFmtConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
89 Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (ImageFormat)), "CCT#2A");
90 Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (Guid)), "CCT#3A");
91 Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#4A");
92 Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#5A");
96 public void TestConvertFrom ()
98 Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConv.ConvertFrom (null,
99 CultureInfo.InvariantCulture,
100 ImageFormat.Bmp.ToString ()), "CF#1");
103 imgFmtConv.ConvertFrom ("System.Drawing.String");
104 Assert.Fail ("CF#2: must throw NotSupportedException");
105 } catch (Exception e) {
106 Assert.IsTrue (e is NotSupportedException, "CF#2");
110 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
111 "System.Drawing.String");
112 Assert.Fail ("CF#2a: must throw NotSupportedException");
113 } catch (Exception e) {
114 Assert.IsTrue (e is NotSupportedException, "CF#2a");
118 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
120 Assert.Fail ("CF#3: must throw NotSupportedException");
121 } catch (Exception e) {
122 Assert.IsTrue (e is NotSupportedException, "CF#3");
126 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
127 ImageFormat.Bmp.Guid);
128 Assert.Fail ("CF#4: must throw NotSupportedException");
129 } catch (Exception e) {
130 Assert.IsTrue (e is NotSupportedException, "CF#4");
134 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
136 Assert.Fail ("CF#5: must throw NotSupportedException");
137 } catch (Exception e) {
138 Assert.IsTrue (e is NotSupportedException, "CF#5");
142 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture, 10);
143 Assert.Fail ("CF#6: must throw NotSupportedException");
144 } catch (Exception e) {
145 Assert.IsTrue (e is NotSupportedException, "CF#6");
149 Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
150 CultureInfo.InvariantCulture,
151 ImageFormat.Bmp.ToString ()), "CF#1A");
154 imgFmtConvFrmTD.ConvertFrom ("System.Drawing.String");
155 Assert.Fail ("CF#2A: must throw NotSupportedException");
156 } catch (Exception e) {
157 Assert.IsTrue (e is NotSupportedException, "CF#2A");
161 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
162 "System.Drawing.String");
163 Assert.Fail ("CF#2aA: must throw NotSupportedException");
164 } catch (Exception e) {
165 Assert.IsTrue (e is NotSupportedException, "CF#2aA");
169 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
171 Assert.Fail ("CF#3A: must throw NotSupportedException");
172 } catch (Exception e) {
173 Assert.IsTrue (e is NotSupportedException, "CF#3A");
177 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
178 ImageFormat.Bmp.Guid);
179 Assert.Fail ("CF#4A: must throw NotSupportedException");
180 } catch (Exception e) {
181 Assert.IsTrue (e is NotSupportedException, "CF#4A");
185 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
187 Assert.Fail ("CF#5A: must throw NotSupportedException");
188 } catch (Exception e) {
189 Assert.IsTrue (e is NotSupportedException, "CF#5A");
193 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, 10);
194 Assert.Fail ("CF#6A: must throw NotSupportedException");
195 } catch (Exception e) {
196 Assert.IsTrue (e is NotSupportedException, "CF#6A");
200 private ImageFormat ShortName (string s)
202 return (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, s);
206 public void ConvertFrom_ShortName ()
208 Assert.AreEqual (ImageFormat.Bmp, ShortName ("Bmp"), "Bmp");
209 Assert.AreEqual (ImageFormat.Emf, ShortName ("Emf"), "Emf");
210 Assert.AreEqual (ImageFormat.Exif, ShortName ("Exif"), "Exif");
211 Assert.AreEqual (ImageFormat.Gif, ShortName ("Gif"), "Gif");
212 Assert.AreEqual (ImageFormat.Tiff, ShortName ("Tiff"), "Tiff");
213 Assert.AreEqual (ImageFormat.Png, ShortName ("Png"), "Png");
214 Assert.AreEqual (ImageFormat.MemoryBmp, ShortName ("MemoryBmp"), "MemoryBmp");
215 Assert.AreEqual (ImageFormat.Icon, ShortName ("Icon"), "Icon");
216 Assert.AreEqual (ImageFormat.Jpeg, ShortName ("Jpeg"), "Jpeg");
217 Assert.AreEqual (ImageFormat.Wmf, ShortName ("Wmf"), "Wmf");
221 public void ConvertFrom_LongName ()
223 Assert.AreEqual (ImageFormat.Bmp, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
224 CultureInfo.InvariantCulture,
225 ImageFormat.Bmp.ToString ()), "CF#1A");
229 public void TestConvertTo ()
231 Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (null,
232 CultureInfo.InvariantCulture,
233 imageFmt, typeof (String)), "CT#1");
235 Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (imageFmt,
236 typeof (String)), "CT#1a");
239 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
240 imageFmt, typeof (ImageFormat));
241 Assert.Fail ("CT#2: must throw NotSupportedException");
242 } catch (Exception e) {
243 Assert.IsTrue (e is NotSupportedException, "CT#2");
247 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
248 imageFmt, typeof (Guid));
249 Assert.Fail ("CT#2a: must throw NotSupportedException");
250 } catch (Exception e) {
251 Assert.IsTrue (e is NotSupportedException, "CT#2a");
255 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
256 imageFmt, typeof (Object));
257 Assert.Fail ("CT#3: must throw NotSupportedException");
258 } catch (Exception e) {
259 Assert.IsTrue (e is NotSupportedException, "CT#3");
263 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
264 imageFmt, typeof (int));
265 Assert.Fail ("CT#4: must throw NotSupportedException");
266 } catch (Exception e) {
267 Assert.IsTrue (e is NotSupportedException, "CT#4");
271 Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (null,
272 CultureInfo.InvariantCulture,
273 imageFmt, typeof (String)), "CT#1A");
275 Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (imageFmt,
276 typeof (String)), "CT#1aA");
279 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
280 imageFmt, typeof (ImageFormat));
281 Assert.Fail ("CT#2A: must throw NotSupportedException");
282 } catch (Exception e) {
283 Assert.IsTrue (e is NotSupportedException, "CT#2A");
287 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
288 imageFmt, typeof (Guid));
289 Assert.Fail ("CT#2aA: must throw NotSupportedException");
290 } catch (Exception e) {
291 Assert.IsTrue (e is NotSupportedException, "CT#2aA");
295 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
296 imageFmt, typeof (Object));
297 Assert.Fail ("CT#3A: must throw NotSupportedException");
298 } catch (Exception e) {
299 Assert.IsTrue (e is NotSupportedException, "CT#3A");
303 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
304 imageFmt, typeof (int));
305 Assert.Fail ("CT#4A: must throw NotSupportedException");
306 } catch (Exception e) {
307 Assert.IsTrue (e is NotSupportedException, "CT#4A");
313 public void TestGetStandardValuesSupported ()
315 Assert.IsTrue (imgFmtConv.GetPropertiesSupported (), "GSVS#1");
316 Assert.IsTrue (imgFmtConv.GetPropertiesSupported (null), "GSVS#2");
320 public void TestGetStandardValues ()