2 // Tests for System.Drawing.ImageConverter.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.
31 using NUnit.Framework;
34 using System.Drawing.Imaging;
35 using System.Collections;
36 using System.ComponentModel;
37 using System.Globalization;
39 using System.Security.Permissions;
41 namespace MonoTests.System.Drawing
44 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
45 public class ImageConverterTest
48 ImageConverter imgConv;
49 ImageConverter imgConvFrmTD;
56 image = Image.FromFile (TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp"));
57 imageStr = image.ToString ();
59 imgConv = new ImageConverter();
60 imgConvFrmTD = (ImageConverter) TypeDescriptor.GetConverter (image);
62 Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/almogaver24bits1.bmp"), FileMode.Open);
63 int length = (int) stream.Length;
64 imageBytes = new byte [length];
67 if (stream.Read (imageBytes, 0, length) != length)
68 Assert.Fail ("SU#1: Read Failure");
69 } catch (Exception e) {
70 Assert.Fail ("SU#2 Exception thrown while reading. Exception is: "+e.Message);
80 public void TestCanConvertFrom ()
82 Assert.IsTrue (imgConv.CanConvertFrom (typeof (byte [])), "CCF#1");
83 Assert.IsTrue (imgConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
84 Assert.IsTrue (imgConv.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1b");
85 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (String)), "CCF#2");
86 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
87 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
88 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
89 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
90 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
91 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
92 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (int)), "CCF#9");
93 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
95 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
96 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
97 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1bA");
98 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
99 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
100 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
101 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
102 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
103 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
104 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
105 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
106 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
111 public void TestCanConvertTo ()
113 Assert.IsTrue (imgConv.CanConvertTo (typeof (String)), "CCT#1");
114 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (String)), "CCT#1a");
115 Assert.IsTrue (imgConv.CanConvertTo (null, imageStr.GetType ()), "CCT#1b");
116 Assert.IsTrue (imgConv.CanConvertTo (typeof (byte [])), "CCT#2");
117 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
118 Assert.IsTrue (imgConv.CanConvertTo (null, imageBytes.GetType ()), "CCT#2b");
119 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
120 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Point)), "CCT#4");
121 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
122 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Size)), "CCT#6");
123 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
124 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Object)), "CCT#8");
125 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (int)), "CCT#9");
127 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
128 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
129 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageStr.GetType ()), "CCT#1bA");
130 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
131 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
132 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageBytes.GetType ()), "CCT#2bA");
133 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
134 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
135 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
136 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
137 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
138 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
139 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
144 public void ConvertFrom ()
146 Image newImage = (Image) imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
148 Assert.AreEqual (image.Height, newImage.Height, "CF#1");
149 Assert.AreEqual (image.Width, newImage.Width, "CF#1a");
151 Assert.AreEqual ("(none)", imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, null, typeof (string)), "Null/Empty");
153 newImage = (Image) imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
155 Assert.AreEqual (image.Height, newImage.Height, "CF#1A");
156 Assert.AreEqual (image.Width, newImage.Width, "CF#1aA");
160 [ExpectedException (typeof (NotSupportedException))]
161 public void ConvertFrom_BadString ()
163 imgConv.ConvertFrom ("System.Drawing.String");
167 [ExpectedException (typeof (NotSupportedException))]
168 public void ConvertFrom_BadString_WithCulture ()
170 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, "System.Drawing.String");
174 [ExpectedException (typeof (NotSupportedException))]
175 public void ConvertFrom_Bitmap ()
177 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new Bitmap (20, 20));
181 [ExpectedException (typeof (NotSupportedException))]
182 public void ConvertFrom_Point ()
184 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new Point (10, 10));
188 [ExpectedException (typeof (NotSupportedException))]
189 public void ConvertFrom_SizeF ()
191 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new SizeF (10, 10));
195 [ExpectedException (typeof (NotSupportedException))]
196 public void ConvertFrom_Object ()
198 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new Object ());
202 [ExpectedException (typeof (NotSupportedException))]
203 public void TypeDescriptor_ConvertFrom_BadString ()
205 imgConvFrmTD.ConvertFrom ("System.Drawing.String");
209 [ExpectedException (typeof (NotSupportedException))]
210 public void TypeDescriptor_ConvertFrom_BadString_Culture ()
212 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, "System.Drawing.String");
216 [ExpectedException (typeof (NotSupportedException))]
217 public void TypeDescriptor_ConvertFrom_Bitmap ()
219 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new Bitmap (20, 20));
223 [ExpectedException (typeof (NotSupportedException))]
224 public void TypeDescriptor_ConvertFrom_Point ()
226 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new Point (10, 10));
230 [ExpectedException (typeof (NotSupportedException))]
231 public void TypeDescriptor_ConvertFrom_SizeF ()
233 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new SizeF (10, 10));
237 [ExpectedException (typeof (NotSupportedException))]
238 public void TypeDescriptor_ConvertFrom_Object ()
240 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new Object ());
244 public void ConvertTo ()
246 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
247 image, typeof (string)), "CT#1");
249 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (image, typeof (string)), "CT#1a");
251 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
252 image, typeof (string)), "CT#1A");
254 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (image, typeof (string)), "CT#1aA");
258 [NUnit.Framework.Category ("NotWorking")]
259 public void ConvertTo_ByteArray ()
261 byte[] newImageBytes = (byte[]) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
262 image, imageBytes.GetType ());
264 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2");
266 newImageBytes = (byte[]) imgConv.ConvertTo (image, imageBytes.GetType ());
268 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2a");
270 newImageBytes = (byte[]) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
271 image, imageBytes.GetType ());
273 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2A");
275 newImageBytes = (byte[]) imgConvFrmTD.ConvertTo (image, imageBytes.GetType ());
277 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2aA");
281 [ExpectedException (typeof (NotSupportedException))]
282 public void ConvertTo_Rectangle ()
284 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Rectangle));
288 [ExpectedException (typeof (NotSupportedException))]
289 public void ConvertTo_Image ()
291 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, image.GetType ());
295 [ExpectedException (typeof (NotSupportedException))]
296 public void ConvertTo_Size ()
298 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Size));
302 [ExpectedException (typeof (NotSupportedException))]
303 public void ConvertTo_Bitmap ()
305 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Bitmap));
309 [ExpectedException (typeof (NotSupportedException))]
310 public void ConvertTo_Point ()
312 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Point));
316 [ExpectedException (typeof (NotSupportedException))]
317 public void ConvertTo_Metafile ()
319 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Metafile));
323 [ExpectedException (typeof (NotSupportedException))]
324 public void ConvertTo_Object ()
326 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Object));
330 [ExpectedException (typeof (NotSupportedException))]
331 public void ConvertTo_Int ()
333 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (int));
337 [ExpectedException (typeof (NotSupportedException))]
338 public void TypeDescriptor_ConvertTo_Rectangle ()
340 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Rectangle));
344 [ExpectedException (typeof (NotSupportedException))]
345 public void TypeDescriptor_ConvertTo_Image ()
347 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, image.GetType ());
351 [ExpectedException (typeof (NotSupportedException))]
352 public void TypeDescriptor_ConvertTo_Size ()
354 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Size));
358 [ExpectedException (typeof (NotSupportedException))]
359 public void TypeDescriptor_ConvertTo_Bitmap ()
361 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Bitmap));
365 [ExpectedException (typeof (NotSupportedException))]
366 public void TypeDescriptor_ConvertTo_Point ()
368 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Point));
372 [ExpectedException (typeof (NotSupportedException))]
373 public void TypeDescriptor_ConvertTo_Metafile ()
375 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Metafile));
379 [ExpectedException (typeof (NotSupportedException))]
380 public void TypeDescriptor_ConvertTo_Object ()
382 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Object));
386 [ExpectedException (typeof (NotSupportedException))]
387 public void TypeDescriptor_ConvertTo_Int ()
389 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (int));
393 public void TestGetPropertiesSupported ()
395 Assert.IsTrue (imgConv.GetPropertiesSupported (), "GPS#1");
396 Assert.IsTrue (imgConv.GetPropertiesSupported (null), "GPS#2");
400 public void TestGetProperties ()
403 PropertyDescriptorCollection propsColl;
405 propsColl = imgConv.GetProperties (null, image, null);
406 Assert.AreEqual (13 + basecount, propsColl.Count, "GP1#1");
408 propsColl = imgConv.GetProperties (null, image);
409 Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#2");
411 propsColl = imgConv.GetProperties (image);
412 Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#3");
414 propsColl = TypeDescriptor.GetProperties (typeof (Image));
415 Assert.AreEqual (13 + basecount, propsColl.Count, "GP1#4");
417 propsColl = imgConvFrmTD.GetProperties (null, image, null);
418 Assert.AreEqual (13 + basecount, propsColl.Count, "GP1#1A");
420 propsColl = imgConvFrmTD.GetProperties (null, image);
421 Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#2A");
423 propsColl = imgConvFrmTD.GetProperties (image);
424 Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#3A");