2 // Tests for System.Drawing.ImageConverter.cs
5 // Sanjay Gupta (gsanjay@novell.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
35 using System.Drawing.Imaging;
36 using System.Collections;
37 using System.ComponentModel;
38 using System.Globalization;
40 using System.Security.Permissions;
42 namespace MonoTests.System.Drawing
45 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
46 public class ImageConverterTest
49 ImageConverter imgConv;
50 ImageConverter imgConvFrmTD;
55 public void TearDown () {}
60 image = Image.FromFile (TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp"));
61 imageStr = image.ToString ();
63 imgConv = new ImageConverter();
64 imgConvFrmTD = (ImageConverter) TypeDescriptor.GetConverter (image);
66 Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/almogaver24bits1.bmp"), FileMode.Open);
67 int length = (int) stream.Length;
68 imageBytes = new byte [length];
71 if (stream.Read (imageBytes, 0, length) != length)
72 Assert.Fail ("SU#1: Read Failure");
73 } catch (Exception e) {
74 Assert.Fail ("SU#2 Exception thrown while reading. Exception is: "+e.Message);
84 public void TestCanConvertFrom ()
86 Assert.IsTrue (imgConv.CanConvertFrom (typeof (byte [])), "CCF#1");
87 Assert.IsTrue (imgConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
88 Assert.IsTrue (imgConv.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1b");
89 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (String)), "CCF#2");
90 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
91 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
92 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
93 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
94 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
95 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
96 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (int)), "CCF#9");
97 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
99 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
100 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
101 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1bA");
102 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
103 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
104 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
105 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
106 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
107 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
108 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
109 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
110 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
115 public void TestCanConvertTo ()
117 Assert.IsTrue (imgConv.CanConvertTo (typeof (String)), "CCT#1");
118 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (String)), "CCT#1a");
119 Assert.IsTrue (imgConv.CanConvertTo (null, imageStr.GetType ()), "CCT#1b");
120 Assert.IsTrue (imgConv.CanConvertTo (typeof (byte [])), "CCT#2");
121 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
122 Assert.IsTrue (imgConv.CanConvertTo (null, imageBytes.GetType ()), "CCT#2b");
123 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
124 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Point)), "CCT#4");
125 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
126 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Size)), "CCT#6");
127 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
128 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Object)), "CCT#8");
129 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (int)), "CCT#9");
131 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
132 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
133 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageStr.GetType ()), "CCT#1bA");
134 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
135 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
136 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageBytes.GetType ()), "CCT#2bA");
137 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
138 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
139 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
140 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
141 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
142 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
143 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
148 public void TestConvertFrom ()
150 Image newImage = (Image) imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
152 Assert.AreEqual (image.Height, newImage.Height, "CF#1");
153 Assert.AreEqual (image.Width, newImage.Width, "CF#1a");
156 imgConv.ConvertFrom ("System.Drawing.String");
157 Assert.Fail ("CF#2: must throw NotSupportedException");
158 } catch (Exception e) {
159 Assert.IsTrue (e is NotSupportedException, "CF#2");
163 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
164 "System.Drawing.String");
165 Assert.Fail ("CF#2a: must throw NotSupportedException");
166 } catch (Exception e) {
167 Assert.IsTrue (e is NotSupportedException, "CF#2a");
171 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
172 new Bitmap (20, 20));
173 Assert.Fail ("CF#3: must throw NotSupportedException");
174 } catch (Exception e) {
175 Assert.IsTrue (e is NotSupportedException, "CF#3");
179 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
181 Assert.Fail ("CF#4: must throw NotSupportedException");
182 } catch (Exception e) {
183 Assert.IsTrue (e is NotSupportedException, "CF#4");
187 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
189 Assert.Fail ("CF#5: must throw NotSupportedException");
190 } catch (Exception e) {
191 Assert.IsTrue (e is NotSupportedException, "CF#5");
195 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
197 Assert.Fail ("CF#6: must throw NotSupportedException");
198 } catch (Exception e) {
199 Assert.IsTrue (e is NotSupportedException, "CF#6");
203 newImage = (Image) imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
205 Assert.AreEqual (image.Height, newImage.Height, "CF#1A");
206 Assert.AreEqual (image.Width, newImage.Width, "CF#1aA");
210 imgConvFrmTD.ConvertFrom ("System.Drawing.String");
211 Assert.Fail ("CF#2A: must throw NotSupportedException");
212 } catch (Exception e) {
213 Assert.IsTrue (e is NotSupportedException, "CF#2A");
217 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
218 "System.Drawing.String");
219 Assert.Fail ("CF#2aA: must throw NotSupportedException");
220 } catch (Exception e) {
221 Assert.IsTrue (e is NotSupportedException, "CF#2aA");
225 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
226 new Bitmap (20, 20));
227 Assert.Fail ("CF#3A: must throw NotSupportedException");
228 } catch (Exception e) {
229 Assert.IsTrue (e is NotSupportedException, "CF#3A");
233 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
235 Assert.Fail ("CF#4A: must throw NotSupportedException");
236 } catch (Exception e) {
237 Assert.IsTrue (e is NotSupportedException, "CF#4A");
241 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
243 Assert.Fail ("CF#5A: must throw NotSupportedException");
244 } catch (Exception e) {
245 Assert.IsTrue (e is NotSupportedException, "CF#5A");
249 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
251 Assert.Fail ("CF#6A: must throw NotSupportedException");
252 } catch (Exception e) {
253 Assert.IsTrue (e is NotSupportedException, "CF#6A");
259 public void TestConvertTo ()
261 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (null,
262 CultureInfo.InvariantCulture,
263 image, typeof (String)), "CT#1");
265 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (image,
266 typeof (String)), "CT#1a");
268 /*byte [] newImageBytes = (byte []) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
269 image, imageBytes.GetType ());
271 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2");
273 newImageBytes = (byte []) imgConv.ConvertTo (image, imageBytes.GetType ());
275 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2a");
278 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
279 image, typeof (Rectangle));
280 Assert.Fail ("CT#3: must throw NotSupportedException");
281 } catch (Exception e) {
282 Assert.IsTrue (e is NotSupportedException, "CT#3");
286 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
287 image, image.GetType ());
288 Assert.Fail ("CT#4: must throw NotSupportedException");
289 } catch (Exception e) {
290 Assert.IsTrue (e is NotSupportedException, "CT#4");
294 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
295 image, typeof (Size));
296 Assert.Fail ("CT#5: must throw NotSupportedException");
297 } catch (Exception e) {
298 Assert.IsTrue (e is NotSupportedException, "CT#5");
302 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
303 image, typeof (Bitmap));
304 Assert.Fail ("CT#6: must throw NotSupportedException");
305 } catch (Exception e) {
306 Assert.IsTrue (e is NotSupportedException, "CT#6");
310 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
311 image, typeof (Point));
312 Assert.Fail ("CT#7: must throw NotSupportedException");
313 } catch (Exception e) {
314 Assert.IsTrue (e is NotSupportedException, "CT#7");
318 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
319 image, typeof (Metafile));
320 Assert.Fail ("CT#8: must throw NotSupportedException");
321 } catch (Exception e) {
322 Assert.IsTrue (e is NotSupportedException, "CT#8");
326 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
327 image, typeof (Object));
328 Assert.Fail ("CT#9: must throw NotSupportedException");
329 } catch (Exception e) {
330 Assert.IsTrue (e is NotSupportedException, "CT#9");
334 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
335 image, typeof (int));
336 Assert.Fail ("CT#10: must throw NotSupportedException");
337 } catch (Exception e) {
338 Assert.IsTrue (e is NotSupportedException, "CT#10");
342 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (null,
343 CultureInfo.InvariantCulture,
344 image, typeof (String)), "CT#1A");
346 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (image,
347 typeof (String)), "CT#1aA");
349 /*newImageBytes = (byte []) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
350 image, imageBytes.GetType ());
352 Assert.AreEqual ( imageBytes.Length, newImageBytes.Length, "CT#2A");
354 newImageBytes = (byte []) imgConvFrmTD.ConvertTo (image, imageBytes.GetType ());
356 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2aA");
359 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
360 image, typeof (Rectangle));
361 Assert.Fail ("CT#3A: must throw NotSupportedException");
362 } catch (Exception e) {
363 Assert.IsTrue (e is NotSupportedException, "CT#3A");
367 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
368 image, image.GetType ());
369 Assert.Fail ("CT#4A: must throw NotSupportedException");
370 } catch (Exception e) {
371 Assert.IsTrue (e is NotSupportedException, "CT#4A");
375 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
376 image, typeof (Size));
377 Assert.Fail ("CT#5A: must throw NotSupportedException");
378 } catch (Exception e) {
379 Assert.IsTrue (e is NotSupportedException, "CT#5A");
383 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
384 image, typeof (Bitmap));
385 Assert.Fail ("CT#6A: must throw NotSupportedException");
386 } catch (Exception e) {
387 Assert.IsTrue (e is NotSupportedException, "CT#6A");
391 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
392 image, typeof (Point));
393 Assert.Fail ("CT#7A: must throw NotSupportedException");
394 } catch (Exception e) {
395 Assert.IsTrue (e is NotSupportedException, "CT#7A");
399 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
400 image, typeof (Metafile));
401 Assert.Fail ("CT#8A: must throw NotSupportedException");
402 } catch (Exception e) {
403 Assert.IsTrue (e is NotSupportedException, "CT#8A");
407 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
408 image, typeof (Object));
409 Assert.Fail ("CT#9A: must throw NotSupportedException");
410 } catch (Exception e) {
411 Assert.IsTrue (e is NotSupportedException, "CT#9A");
415 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
416 image, typeof (int));
417 Assert.Fail ("CT#10A: must throw NotSupportedException");
418 } catch (Exception e) {
419 Assert.IsTrue (e is NotSupportedException, "CT#10A");
425 public void TestGetPropertiesSupported ()
427 Assert.IsTrue (imgConv.GetPropertiesSupported (), "GPS#1");
428 Assert.IsTrue (imgConv.GetPropertiesSupported (null), "GPS#2");
432 public void TestGetProperties ()
434 PropertyDescriptorCollection propsColl;
436 propsColl = imgConv.GetProperties (null, image, null);
437 Assert.AreEqual (13, propsColl.Count, "GP1#1");
439 propsColl = imgConv.GetProperties (null, image);
440 Assert.AreEqual (6, propsColl.Count, "GP1#2");
442 propsColl = imgConv.GetProperties (image);
443 Assert.AreEqual (6, propsColl.Count, "GP1#3");
445 propsColl = TypeDescriptor.GetProperties (typeof (Image));
446 Assert.AreEqual (13, propsColl.Count, "GP1#4");
448 propsColl = imgConvFrmTD.GetProperties (null, image, null);
449 Assert.AreEqual (13, propsColl.Count, "GP1#1A");
451 propsColl = imgConvFrmTD.GetProperties (null, image);
452 Assert.AreEqual (6, propsColl.Count, "GP1#2A");
454 propsColl = imgConvFrmTD.GetProperties (image);
455 Assert.AreEqual (6, propsColl.Count, "GP1#3A");