(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Drawing / Test / System.Drawing / TestImageConverter.cs
blobfb5a9b0441a9bc7247ff42e5261cae38a1390d5a
1 //
2 // Tests for System.Drawing.ImageConverter.cs
3 //
4 // Author:
5 // Sanjay Gupta (gsanjay@novell.com)
6 //
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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;
33 using System;
34 using System.Drawing;
35 using System.Drawing.Imaging;
36 using System.Collections;
37 using System.ComponentModel;
38 using System.Globalization;
39 using System.IO;
41 namespace MonoTests.System.Drawing
43 [TestFixture]
44 public class ImageConverterTest
46 Image image;
47 ImageConverter imgConv;
48 ImageConverter imgConvFrmTD;
49 String imageStr;
50 byte [] imageBytes;
52 [TearDown]
53 public void TearDown () {}
55 [SetUp]
56 public void SetUp ()
58 image = Image.FromFile (TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp"));
59 imageStr = image.ToString ();
61 imgConv = new ImageConverter();
62 imgConvFrmTD = (ImageConverter) TypeDescriptor.GetConverter (image);
64 Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/almogaver24bits1.bmp"), FileMode.Open);
65 int length = (int) stream.Length;
66 imageBytes = new byte [length];
68 try {
69 if (stream.Read (imageBytes, 0, length) != length)
70 Assert.Fail ("SU#1: Read Failure");
71 } catch (Exception e) {
72 Assert.Fail ("SU#2 Exception thrown while reading. Exception is: "+e.Message);
73 } finally {
74 stream.Close ();
77 stream.Close ();
81 [Test]
82 public void TestCanConvertFrom ()
84 Assert.IsTrue (imgConv.CanConvertFrom (typeof (byte [])), "CCF#1");
85 Assert.IsTrue (imgConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
86 Assert.IsTrue (imgConv.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1b");
87 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (String)), "CCF#2");
88 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
89 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
90 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
91 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
92 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
93 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
94 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (int)), "CCF#9");
95 Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
97 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
98 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
99 Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1bA");
100 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
101 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
102 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
103 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
104 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
105 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
106 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
107 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
108 Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
112 [Test]
113 public void TestCanConvertTo ()
115 Assert.IsTrue (imgConv.CanConvertTo (typeof (String)), "CCT#1");
116 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (String)), "CCT#1a");
117 Assert.IsTrue (imgConv.CanConvertTo (null, imageStr.GetType ()), "CCT#1b");
118 Assert.IsTrue (imgConv.CanConvertTo (typeof (byte [])), "CCT#2");
119 Assert.IsTrue (imgConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
120 Assert.IsTrue (imgConv.CanConvertTo (null, imageBytes.GetType ()), "CCT#2b");
121 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
122 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Point)), "CCT#4");
123 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
124 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Size)), "CCT#6");
125 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
126 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Object)), "CCT#8");
127 Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (int)), "CCT#9");
129 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
130 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
131 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageStr.GetType ()), "CCT#1bA");
132 Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
133 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
134 Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageBytes.GetType ()), "CCT#2bA");
135 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
136 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
137 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
138 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
139 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
140 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
141 Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
145 [Test]
146 public void TestConvertFrom ()
148 Image newImage = (Image) imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
150 Assert.AreEqual (image.Height, newImage.Height, "CF#1");
151 Assert.AreEqual (image.Width, newImage.Width, "CF#1a");
153 try {
154 imgConv.ConvertFrom ("System.Drawing.String");
155 Assert.Fail ("CF#2: must throw NotSupportedException");
156 } catch (Exception e) {
157 Assert.IsTrue (e is NotSupportedException, "CF#2");
160 try {
161 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
162 "System.Drawing.String");
163 Assert.Fail ("CF#2a: must throw NotSupportedException");
164 } catch (Exception e) {
165 Assert.IsTrue (e is NotSupportedException, "CF#2a");
168 try {
169 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
170 new Bitmap (20, 20));
171 Assert.Fail ("CF#3: must throw NotSupportedException");
172 } catch (Exception e) {
173 Assert.IsTrue (e is NotSupportedException, "CF#3");
176 try {
177 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
178 new Point (10, 10));
179 Assert.Fail ("CF#4: must throw NotSupportedException");
180 } catch (Exception e) {
181 Assert.IsTrue (e is NotSupportedException, "CF#4");
184 try {
185 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
186 new SizeF (10, 10));
187 Assert.Fail ("CF#5: must throw NotSupportedException");
188 } catch (Exception e) {
189 Assert.IsTrue (e is NotSupportedException, "CF#5");
192 try {
193 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
194 new Object ());
195 Assert.Fail ("CF#6: must throw NotSupportedException");
196 } catch (Exception e) {
197 Assert.IsTrue (e is NotSupportedException, "CF#6");
201 newImage = (Image) imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
203 Assert.AreEqual (image.Height, newImage.Height, "CF#1A");
204 Assert.AreEqual (image.Width, newImage.Width, "CF#1aA");
207 try {
208 imgConvFrmTD.ConvertFrom ("System.Drawing.String");
209 Assert.Fail ("CF#2A: must throw NotSupportedException");
210 } catch (Exception e) {
211 Assert.IsTrue (e is NotSupportedException, "CF#2A");
214 try {
215 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
216 "System.Drawing.String");
217 Assert.Fail ("CF#2aA: must throw NotSupportedException");
218 } catch (Exception e) {
219 Assert.IsTrue (e is NotSupportedException, "CF#2aA");
222 try {
223 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
224 new Bitmap (20, 20));
225 Assert.Fail ("CF#3A: must throw NotSupportedException");
226 } catch (Exception e) {
227 Assert.IsTrue (e is NotSupportedException, "CF#3A");
230 try {
231 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
232 new Point (10, 10));
233 Assert.Fail ("CF#4A: must throw NotSupportedException");
234 } catch (Exception e) {
235 Assert.IsTrue (e is NotSupportedException, "CF#4A");
238 try {
239 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
240 new SizeF (10, 10));
241 Assert.Fail ("CF#5A: must throw NotSupportedException");
242 } catch (Exception e) {
243 Assert.IsTrue (e is NotSupportedException, "CF#5A");
246 try {
247 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
248 new Object ());
249 Assert.Fail ("CF#6A: must throw NotSupportedException");
250 } catch (Exception e) {
251 Assert.IsTrue (e is NotSupportedException, "CF#6A");
256 [Test]
257 public void TestConvertTo ()
259 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (null,
260 CultureInfo.InvariantCulture,
261 image, typeof (String)), "CT#1");
263 Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (image,
264 typeof (String)), "CT#1a");
266 /*byte [] newImageBytes = (byte []) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
267 image, imageBytes.GetType ());
269 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2");
271 newImageBytes = (byte []) imgConv.ConvertTo (image, imageBytes.GetType ());
273 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2a");
275 try {
276 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
277 image, typeof (Rectangle));
278 Assert.Fail ("CT#3: must throw NotSupportedException");
279 } catch (Exception e) {
280 Assert.IsTrue (e is NotSupportedException, "CT#3");
283 try {
284 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
285 image, image.GetType ());
286 Assert.Fail ("CT#4: must throw NotSupportedException");
287 } catch (Exception e) {
288 Assert.IsTrue (e is NotSupportedException, "CT#4");
291 try {
292 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
293 image, typeof (Size));
294 Assert.Fail ("CT#5: must throw NotSupportedException");
295 } catch (Exception e) {
296 Assert.IsTrue (e is NotSupportedException, "CT#5");
299 try {
300 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
301 image, typeof (Bitmap));
302 Assert.Fail ("CT#6: must throw NotSupportedException");
303 } catch (Exception e) {
304 Assert.IsTrue (e is NotSupportedException, "CT#6");
307 try {
308 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
309 image, typeof (Point));
310 Assert.Fail ("CT#7: must throw NotSupportedException");
311 } catch (Exception e) {
312 Assert.IsTrue (e is NotSupportedException, "CT#7");
315 try {
316 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
317 image, typeof (Metafile));
318 Assert.Fail ("CT#8: must throw NotSupportedException");
319 } catch (Exception e) {
320 Assert.IsTrue (e is NotSupportedException, "CT#8");
323 try {
324 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
325 image, typeof (Object));
326 Assert.Fail ("CT#9: must throw NotSupportedException");
327 } catch (Exception e) {
328 Assert.IsTrue (e is NotSupportedException, "CT#9");
331 try {
332 imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
333 image, typeof (int));
334 Assert.Fail ("CT#10: must throw NotSupportedException");
335 } catch (Exception e) {
336 Assert.IsTrue (e is NotSupportedException, "CT#10");
340 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (null,
341 CultureInfo.InvariantCulture,
342 image, typeof (String)), "CT#1A");
344 Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (image,
345 typeof (String)), "CT#1aA");
347 /*newImageBytes = (byte []) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
348 image, imageBytes.GetType ());
350 Assert.AreEqual ( imageBytes.Length, newImageBytes.Length, "CT#2A");
352 newImageBytes = (byte []) imgConvFrmTD.ConvertTo (image, imageBytes.GetType ());
354 Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2aA");
356 try {
357 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
358 image, typeof (Rectangle));
359 Assert.Fail ("CT#3A: must throw NotSupportedException");
360 } catch (Exception e) {
361 Assert.IsTrue (e is NotSupportedException, "CT#3A");
364 try {
365 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
366 image, image.GetType ());
367 Assert.Fail ("CT#4A: must throw NotSupportedException");
368 } catch (Exception e) {
369 Assert.IsTrue (e is NotSupportedException, "CT#4A");
372 try {
373 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
374 image, typeof (Size));
375 Assert.Fail ("CT#5A: must throw NotSupportedException");
376 } catch (Exception e) {
377 Assert.IsTrue (e is NotSupportedException, "CT#5A");
380 try {
381 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
382 image, typeof (Bitmap));
383 Assert.Fail ("CT#6A: must throw NotSupportedException");
384 } catch (Exception e) {
385 Assert.IsTrue (e is NotSupportedException, "CT#6A");
388 try {
389 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
390 image, typeof (Point));
391 Assert.Fail ("CT#7A: must throw NotSupportedException");
392 } catch (Exception e) {
393 Assert.IsTrue (e is NotSupportedException, "CT#7A");
396 try {
397 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
398 image, typeof (Metafile));
399 Assert.Fail ("CT#8A: must throw NotSupportedException");
400 } catch (Exception e) {
401 Assert.IsTrue (e is NotSupportedException, "CT#8A");
404 try {
405 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
406 image, typeof (Object));
407 Assert.Fail ("CT#9A: must throw NotSupportedException");
408 } catch (Exception e) {
409 Assert.IsTrue (e is NotSupportedException, "CT#9A");
412 try {
413 imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
414 image, typeof (int));
415 Assert.Fail ("CT#10A: must throw NotSupportedException");
416 } catch (Exception e) {
417 Assert.IsTrue (e is NotSupportedException, "CT#10A");
422 [Test]
423 public void TestGetPropertiesSupported ()
425 Assert.IsTrue (imgConv.GetPropertiesSupported (), "GPS#1");
426 Assert.IsTrue (imgConv.GetPropertiesSupported (null), "GPS#2");
429 [Test]
430 [Ignore ("This test fails because of bug #58435")]
431 public void TestGetProperties ()
433 PropertyDescriptorCollection propsColl;
435 propsColl = imgConv.GetProperties (null, image, null);
436 Assert.AreEqual (13, propsColl.Count, "GP1#1");
438 propsColl = imgConv.GetProperties (null, image);
439 Assert.AreEqual (6, propsColl.Count, "GP1#2");
441 propsColl = imgConv.GetProperties (image);
442 Assert.AreEqual (6, propsColl.Count, "GP1#3");
444 propsColl = TypeDescriptor.GetProperties (typeof (Image));
445 Assert.AreEqual (13, propsColl.Count, "GP1#4");
447 propsColl = imgConvFrmTD.GetProperties (null, image, null);
448 Assert.AreEqual (13, propsColl.Count, "GP1#1A");
450 propsColl = imgConvFrmTD.GetProperties (null, image);
451 Assert.AreEqual (6, propsColl.Count, "GP1#2A");
453 propsColl = imgConvFrmTD.GetProperties (image);
454 Assert.AreEqual (6, propsColl.Count, "GP1#3A");