2 // Tests for System.Drawing.ImageConverter.cs
5 // Sanjay Gupta (gsanjay@novell.com)
7 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
10 using NUnit.Framework;
13 using System.Drawing.Imaging;
14 using System.Collections;
15 using System.ComponentModel;
16 using System.Globalization;
19 namespace MonoTests.System.Drawing
22 public class ImageConverterTest
25 ImageConverter imgConv;
26 ImageConverter imgConvFrmTD;
31 public void TearDown () {}
36 image = Image.FromFile (TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp"));
37 imageStr = image.ToString ();
39 imgConv = new ImageConverter();
40 imgConvFrmTD = (ImageConverter) TypeDescriptor.GetConverter (image);
42 Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/almogaver24bits1.bmp"), FileMode.Open);
43 int length = (int) stream.Length;
44 imageBytes = new byte [length];
47 if (stream.Read (imageBytes, 0, length) != length)
48 Assert.Fail ("SU#1: Read Failure");
49 } catch (Exception e) {
50 Assert.Fail ("SU#2 Exception thrown while reading. Exception is: "+e.Message);
60 public void TestCanConvertFrom ()
62 Assert.IsTrue (imgConv.CanConvertFrom (typeof (byte [])), "CCF#1");
63 Assert.IsTrue (imgConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
64 Assert.IsTrue (imgConv.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1b");
65 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (String)), "CCF#2");
66 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
67 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
68 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
69 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
70 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
71 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
72 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (int)), "CCF#9");
73 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
75 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
76 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
77 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1bA");
78 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
79 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
80 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
81 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
82 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
83 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
84 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
85 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
86 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
91 public void TestCanConvertTo ()
93 Assert.IsTrue (imgConv.CanConvertTo (typeof (String)), "CCT#1");
94 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (String)), "CCT#1a");
95 Assert.IsTrue (imgConv.CanConvertTo (null, imageStr.GetType ()), "CCT#1b");
96 Assert.IsTrue (imgConv.CanConvertTo (typeof (byte [])), "CCT#2");
97 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
98 Assert.IsTrue (imgConv.CanConvertTo (null, imageBytes.GetType ()), "CCT#2b");
99 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
100 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Point)), "CCT#4");
101 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
102 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Size)), "CCT#6");
103 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
104 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Object)), "CCT#8");
105 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (int)), "CCT#9");
107 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
108 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
109 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageStr.GetType ()), "CCT#1bA");
110 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
111 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
112 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageBytes.GetType ()), "CCT#2bA");
113 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
114 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
115 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
116 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
117 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
118 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
119 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
124 public void TestConvertFrom ()
126 Image newImage = (Image) imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
128 Assert.AreEqual (image.Height, newImage.Height, "CF#1");
129 Assert.AreEqual (image.Width, newImage.Width, "CF#1a");
132 imgConv.ConvertFrom ("System.Drawing.String");
133 Assert.Fail ("CF#2: must throw NotSupportedException");
134 } catch (Exception e) {
135 Assert.IsTrue (e is NotSupportedException, "CF#2");
139 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
140 "System.Drawing.String");
141 Assert.Fail ("CF#2a: must throw NotSupportedException");
142 } catch (Exception e) {
143 Assert.IsTrue (e is NotSupportedException, "CF#2a");
147 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
148 new Bitmap (20, 20));
149 Assert.Fail ("CF#3: must throw NotSupportedException");
150 } catch (Exception e) {
151 Assert.IsTrue (e is NotSupportedException, "CF#3");
155 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
157 Assert.Fail ("CF#4: must throw NotSupportedException");
158 } catch (Exception e) {
159 Assert.IsTrue (e is NotSupportedException, "CF#4");
163 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
165 Assert.Fail ("CF#5: must throw NotSupportedException");
166 } catch (Exception e) {
167 Assert.IsTrue (e is NotSupportedException, "CF#5");
171 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
173 Assert.Fail ("CF#6: must throw NotSupportedException");
174 } catch (Exception e) {
175 Assert.IsTrue (e is NotSupportedException, "CF#6");
179 newImage = (Image) imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
181 Assert.AreEqual (image.Height, newImage.Height, "CF#1A");
182 Assert.AreEqual (image.Width, newImage.Width, "CF#1aA");
186 imgConvFrmTD.ConvertFrom ("System.Drawing.String");
187 Assert.Fail ("CF#2A: must throw NotSupportedException");
188 } catch (Exception e) {
189 Assert.IsTrue (e is NotSupportedException, "CF#2A");
193 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
194 "System.Drawing.String");
195 Assert.Fail ("CF#2aA: must throw NotSupportedException");
196 } catch (Exception e) {
197 Assert.IsTrue (e is NotSupportedException, "CF#2aA");
201 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
202 new Bitmap (20, 20));
203 Assert.Fail ("CF#3A: must throw NotSupportedException");
204 } catch (Exception e) {
205 Assert.IsTrue (e is NotSupportedException, "CF#3A");
209 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
211 Assert.Fail ("CF#4A: must throw NotSupportedException");
212 } catch (Exception e) {
213 Assert.IsTrue (e is NotSupportedException, "CF#4A");
217 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
219 Assert.Fail ("CF#5A: must throw NotSupportedException");
220 } catch (Exception e) {
221 Assert.IsTrue (e is NotSupportedException, "CF#5A");
225 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
227 Assert.Fail ("CF#6A: must throw NotSupportedException");
228 } catch (Exception e) {
229 Assert.IsTrue (e is NotSupportedException, "CF#6A");
235 public void TestConvertTo ()
237 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (null,
238 CultureInfo.InvariantCulture,
239 image, typeof (String)), "CT#1");
241 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (image,
242 typeof (String)), "CT#1a");
244 byte [] newImageBytes = (byte []) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
245 image, imageBytes.GetType ());
247 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2");
249 newImageBytes = (byte []) imgConv.ConvertTo (image, imageBytes.GetType ());
251 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2a");
255 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
256 image, typeof (Rectangle));
257 Assert.Fail ("CT#3: must throw NotSupportedException");
258 } catch (Exception e) {
259 Assert.IsTrue (e is NotSupportedException, "CT#3");
263 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
264 image, image.GetType ());
265 Assert.Fail ("CT#4: must throw NotSupportedException");
266 } catch (Exception e) {
267 Assert.IsTrue (e is NotSupportedException, "CT#4");
271 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
272 image, typeof (Size));
273 Assert.Fail ("CT#5: must throw NotSupportedException");
274 } catch (Exception e) {
275 Assert.IsTrue (e is NotSupportedException, "CT#5");
279 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
280 image, typeof (Bitmap));
281 Assert.Fail ("CT#6: must throw NotSupportedException");
282 } catch (Exception e) {
283 Assert.IsTrue (e is NotSupportedException, "CT#6");
287 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
288 image, typeof (Point));
289 Assert.Fail ("CT#7: must throw NotSupportedException");
290 } catch (Exception e) {
291 Assert.IsTrue (e is NotSupportedException, "CT#7");
295 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
296 image, typeof (Metafile));
297 Assert.Fail ("CT#8: must throw NotSupportedException");
298 } catch (Exception e) {
299 Assert.IsTrue (e is NotSupportedException, "CT#8");
303 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
304 image, typeof (Object));
305 Assert.Fail ("CT#9: must throw NotSupportedException");
306 } catch (Exception e) {
307 Assert.IsTrue (e is NotSupportedException, "CT#9");
311 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
312 image, typeof (int));
313 Assert.Fail ("CT#10: must throw NotSupportedException");
314 } catch (Exception e) {
315 Assert.IsTrue (e is NotSupportedException, "CT#10");
319 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (null,
320 CultureInfo.InvariantCulture,
321 image, typeof (String)), "CT#1A");
323 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (image,
324 typeof (String)), "CT#1aA");
326 newImageBytes = (byte []) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
327 image, imageBytes.GetType ());
329 Assert.AreEqual ( imageBytes.Length, newImageBytes.Length, "CT#2A");
331 newImageBytes = (byte []) imgConvFrmTD.ConvertTo (image, imageBytes.GetType ());
333 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2aA");
336 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
337 image, typeof (Rectangle));
338 Assert.Fail ("CT#3A: must throw NotSupportedException");
339 } catch (Exception e) {
340 Assert.IsTrue (e is NotSupportedException, "CT#3A");
344 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
345 image, image.GetType ());
346 Assert.Fail ("CT#4A: must throw NotSupportedException");
347 } catch (Exception e) {
348 Assert.IsTrue (e is NotSupportedException, "CT#4A");
352 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
353 image, typeof (Size));
354 Assert.Fail ("CT#5A: must throw NotSupportedException");
355 } catch (Exception e) {
356 Assert.IsTrue (e is NotSupportedException, "CT#5A");
360 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
361 image, typeof (Bitmap));
362 Assert.Fail ("CT#6A: must throw NotSupportedException");
363 } catch (Exception e) {
364 Assert.IsTrue (e is NotSupportedException, "CT#6A");
368 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
369 image, typeof (Point));
370 Assert.Fail ("CT#7A: must throw NotSupportedException");
371 } catch (Exception e) {
372 Assert.IsTrue (e is NotSupportedException, "CT#7A");
376 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
377 image, typeof (Metafile));
378 Assert.Fail ("CT#8A: must throw NotSupportedException");
379 } catch (Exception e) {
380 Assert.IsTrue (e is NotSupportedException, "CT#8A");
384 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
385 image, typeof (Object));
386 Assert.Fail ("CT#9A: must throw NotSupportedException");
387 } catch (Exception e) {
388 Assert.IsTrue (e is NotSupportedException, "CT#9A");
392 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
393 image, typeof (int));
394 Assert.Fail ("CT#10A: must throw NotSupportedException");
395 } catch (Exception e) {
396 Assert.IsTrue (e is NotSupportedException, "CT#10A");
402 public void TestGetPropertiesSupported ()
404 Assert.IsTrue (imgConv.GetPropertiesSupported (), "GPS#1");
405 Assert.IsTrue (imgConv.GetPropertiesSupported (null), "GPS#2");
409 [Ignore ("This test fails because of bug #58435")]
410 public void TestGetProperties ()
412 PropertyDescriptorCollection propsColl;
414 propsColl = imgConv.GetProperties (null, image, null);
415 Assert.AreEqual (13, propsColl.Count, "GP1#1");
417 propsColl = imgConv.GetProperties (null, image);
418 Assert.AreEqual (6, propsColl.Count, "GP1#2");
420 propsColl = imgConv.GetProperties (image);
421 Assert.AreEqual (6, propsColl.Count, "GP1#3");
423 propsColl = TypeDescriptor.GetProperties (typeof (Image));
424 Assert.AreEqual (13, propsColl.Count, "GP1#4");
426 propsColl = imgConvFrmTD.GetProperties (null, image, null);
427 Assert.AreEqual (13, propsColl.Count, "GP1#1A");
429 propsColl = imgConvFrmTD.GetProperties (null, image);
430 Assert.AreEqual (6, propsColl.Count, "GP1#2A");
432 propsColl = imgConvFrmTD.GetProperties (image);
433 Assert.AreEqual (6, propsColl.Count, "GP1#3A");