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);
81 [NUnit.Framework.Category ("NotWorking")]
83 public void TestCanConvertFrom ()
85 Assert.IsTrue (imgConv.CanConvertFrom (typeof (byte [])), "CCF#1");
86 Assert.IsTrue (imgConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
87 Assert.IsTrue (imgConv.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1b");
88 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (String)), "CCF#2");
89 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
90 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
91 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
92 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
93 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
94 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
95 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (int)), "CCF#9");
96 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
98 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
99 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
100 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1bA");
101 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
102 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
103 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
104 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
105 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
106 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
107 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
108 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
109 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
115 [NUnit.Framework.Category ("NotWorking")]
117 public void TestCanConvertTo ()
119 Assert.IsTrue (imgConv.CanConvertTo (typeof (String)), "CCT#1");
120 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (String)), "CCT#1a");
121 Assert.IsTrue (imgConv.CanConvertTo (null, imageStr.GetType ()), "CCT#1b");
122 Assert.IsTrue (imgConv.CanConvertTo (typeof (byte [])), "CCT#2");
123 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
124 Assert.IsTrue (imgConv.CanConvertTo (null, imageBytes.GetType ()), "CCT#2b");
125 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
126 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Point)), "CCT#4");
127 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
128 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Size)), "CCT#6");
129 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
130 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Object)), "CCT#8");
131 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (int)), "CCT#9");
133 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
134 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
135 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageStr.GetType ()), "CCT#1bA");
136 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
137 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
138 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageBytes.GetType ()), "CCT#2bA");
139 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
140 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
141 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
142 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
143 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
144 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
145 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
151 [NUnit.Framework.Category ("NotWorking")]
153 public void ConvertFrom ()
155 Image newImage = (Image) imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
157 Assert.AreEqual (image.Height, newImage.Height, "CF#1");
158 Assert.AreEqual (image.Width, newImage.Width, "CF#1a");
160 Assert.AreEqual ("(none)", imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, null, typeof (string)), "Null/Empty");
162 newImage = (Image) imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
164 Assert.AreEqual (image.Height, newImage.Height, "CF#1A");
165 Assert.AreEqual (image.Width, newImage.Width, "CF#1aA");
170 [NUnit.Framework.Category ("NotWorking")]
172 [ExpectedException (typeof (NotSupportedException))]
173 public void ConvertFrom_BadString ()
175 imgConv.ConvertFrom ("System.Drawing.String");
180 [NUnit.Framework.Category ("NotWorking")]
182 [ExpectedException (typeof (NotSupportedException))]
183 public void ConvertFrom_BadString_WithCulture ()
185 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, "System.Drawing.String");
190 [NUnit.Framework.Category ("NotWorking")]
192 [ExpectedException (typeof (NotSupportedException))]
193 public void ConvertFrom_Bitmap ()
195 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new Bitmap (20, 20));
200 [NUnit.Framework.Category ("NotWorking")]
202 [ExpectedException (typeof (NotSupportedException))]
203 public void ConvertFrom_Point ()
205 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new Point (10, 10));
210 [NUnit.Framework.Category ("NotWorking")]
212 [ExpectedException (typeof (NotSupportedException))]
213 public void ConvertFrom_SizeF ()
215 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new SizeF (10, 10));
220 [NUnit.Framework.Category ("NotWorking")]
222 [ExpectedException (typeof (NotSupportedException))]
223 public void ConvertFrom_Object ()
225 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new Object ());
230 [NUnit.Framework.Category ("NotWorking")]
232 [ExpectedException (typeof (NotSupportedException))]
233 public void TypeDescriptor_ConvertFrom_BadString ()
235 imgConvFrmTD.ConvertFrom ("System.Drawing.String");
240 [NUnit.Framework.Category ("NotWorking")]
242 [ExpectedException (typeof (NotSupportedException))]
243 public void TypeDescriptor_ConvertFrom_BadString_Culture ()
245 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, "System.Drawing.String");
250 [NUnit.Framework.Category ("NotWorking")]
252 [ExpectedException (typeof (NotSupportedException))]
253 public void TypeDescriptor_ConvertFrom_Bitmap ()
255 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new Bitmap (20, 20));
260 [NUnit.Framework.Category ("NotWorking")]
262 [ExpectedException (typeof (NotSupportedException))]
263 public void TypeDescriptor_ConvertFrom_Point ()
265 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new Point (10, 10));
270 [NUnit.Framework.Category ("NotWorking")]
272 [ExpectedException (typeof (NotSupportedException))]
273 public void TypeDescriptor_ConvertFrom_SizeF ()
275 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new SizeF (10, 10));
280 [NUnit.Framework.Category ("NotWorking")]
282 [ExpectedException (typeof (NotSupportedException))]
283 public void TypeDescriptor_ConvertFrom_Object ()
285 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new Object ());
290 [NUnit.Framework.Category ("NotWorking")]
292 public void ConvertTo ()
294 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
295 image, typeof (string)), "CT#1");
297 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (image, typeof (string)), "CT#1a");
299 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
300 image, typeof (string)), "CT#1A");
302 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (image, typeof (string)), "CT#1aA");
306 [NUnit.Framework.Category ("NotWorking")]
307 public void ConvertTo_ByteArray ()
309 byte[] newImageBytes = (byte[]) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
310 image, imageBytes.GetType ());
312 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2");
314 newImageBytes = (byte[]) imgConv.ConvertTo (image, imageBytes.GetType ());
316 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2a");
318 newImageBytes = (byte[]) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
319 image, imageBytes.GetType ());
321 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2A");
323 newImageBytes = (byte[]) imgConvFrmTD.ConvertTo (image, imageBytes.GetType ());
325 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2aA");
330 [NUnit.Framework.Category ("NotWorking")]
332 [ExpectedException (typeof (NotSupportedException))]
333 public void ConvertTo_Rectangle ()
335 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Rectangle));
340 [NUnit.Framework.Category ("NotWorking")]
342 [ExpectedException (typeof (NotSupportedException))]
343 public void ConvertTo_Image ()
345 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, image.GetType ());
350 [NUnit.Framework.Category ("NotWorking")]
352 [ExpectedException (typeof (NotSupportedException))]
353 public void ConvertTo_Size ()
355 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Size));
360 [NUnit.Framework.Category ("NotWorking")]
362 [ExpectedException (typeof (NotSupportedException))]
363 public void ConvertTo_Bitmap ()
365 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Bitmap));
370 [NUnit.Framework.Category ("NotWorking")]
372 [ExpectedException (typeof (NotSupportedException))]
373 public void ConvertTo_Point ()
375 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Point));
380 [NUnit.Framework.Category ("NotWorking")]
382 [ExpectedException (typeof (NotSupportedException))]
383 public void ConvertTo_Metafile ()
385 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Metafile));
390 [NUnit.Framework.Category ("NotWorking")]
392 [ExpectedException (typeof (NotSupportedException))]
393 public void ConvertTo_Object ()
395 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Object));
400 [NUnit.Framework.Category ("NotWorking")]
402 [ExpectedException (typeof (NotSupportedException))]
403 public void ConvertTo_Int ()
405 imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (int));
410 [NUnit.Framework.Category ("NotWorking")]
412 [ExpectedException (typeof (NotSupportedException))]
413 public void TypeDescriptor_ConvertTo_Rectangle ()
415 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Rectangle));
420 [NUnit.Framework.Category ("NotWorking")]
422 [ExpectedException (typeof (NotSupportedException))]
423 public void TypeDescriptor_ConvertTo_Image ()
425 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, image.GetType ());
430 [NUnit.Framework.Category ("NotWorking")]
432 [ExpectedException (typeof (NotSupportedException))]
433 public void TypeDescriptor_ConvertTo_Size ()
435 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Size));
440 [NUnit.Framework.Category ("NotWorking")]
442 [ExpectedException (typeof (NotSupportedException))]
443 public void TypeDescriptor_ConvertTo_Bitmap ()
445 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Bitmap));
450 [NUnit.Framework.Category ("NotWorking")]
452 [ExpectedException (typeof (NotSupportedException))]
453 public void TypeDescriptor_ConvertTo_Point ()
455 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Point));
460 [NUnit.Framework.Category ("NotWorking")]
462 [ExpectedException (typeof (NotSupportedException))]
463 public void TypeDescriptor_ConvertTo_Metafile ()
465 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Metafile));
470 [NUnit.Framework.Category ("NotWorking")]
472 [ExpectedException (typeof (NotSupportedException))]
473 public void TypeDescriptor_ConvertTo_Object ()
475 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Object));
480 [NUnit.Framework.Category ("NotWorking")]
482 [ExpectedException (typeof (NotSupportedException))]
483 public void TypeDescriptor_ConvertTo_Int ()
485 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (int));
490 [NUnit.Framework.Category ("NotWorking")]
492 public void TestGetPropertiesSupported ()
494 Assert.IsTrue (imgConv.GetPropertiesSupported (), "GPS#1");
495 Assert.IsTrue (imgConv.GetPropertiesSupported (null), "GPS#2");
500 [NUnit.Framework.Category ("NotWorking")]
502 public void TestGetProperties ()
509 PropertyDescriptorCollection propsColl;
511 propsColl = imgConv.GetProperties (null, image, null);
512 Assert.AreEqual (13 + basecount, propsColl.Count, "GP1#1");
514 propsColl = imgConv.GetProperties (null, image);
515 Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#2");
517 propsColl = imgConv.GetProperties (image);
518 Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#3");
520 propsColl = TypeDescriptor.GetProperties (typeof (Image));
521 Assert.AreEqual (13 + basecount, propsColl.Count, "GP1#4");
523 propsColl = imgConvFrmTD.GetProperties (null, image, null);
524 Assert.AreEqual (13 + basecount, propsColl.Count, "GP1#1A");
526 propsColl = imgConvFrmTD.GetProperties (null, image);
527 Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#2A");
529 propsColl = imgConvFrmTD.GetProperties (image);
530 Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#3A");