ChangeLog: Updated ChangeLog
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestImageConverter.cs
1 //
2 // Tests for System.Drawing.ImageConverter.cs 
3 //
4 // Author:
5 //      Sanjay Gupta (gsanjay@novell.com)
6 //
7 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.Drawing;
13 using System.Drawing.Imaging;
14 using System.Collections;
15 using System.ComponentModel;
16 using System.Globalization;
17 using System.IO;
18
19 namespace MonoTests.System.Drawing
20 {
21         [TestFixture]   
22         public class ImageConverterTest 
23         {
24                 Image image;            
25                 ImageConverter imgConv;
26                 ImageConverter imgConvFrmTD;
27                 String imageStr;
28                 byte [] imageBytes;
29
30                 [TearDown]
31                 public void TearDown () {}
32
33                 [SetUp]
34                 public void SetUp ()            
35                 {
36                         image = Image.FromFile (TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp"));
37                         imageStr = image.ToString ();
38                 
39                         imgConv = new ImageConverter();
40                         imgConvFrmTD = (ImageConverter) TypeDescriptor.GetConverter (image);
41                         
42                         Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/almogaver24bits1.bmp"), FileMode.Open);
43                         int length = (int) stream.Length;
44                         imageBytes = new byte [length];
45                         
46                         try {
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);
51                         } finally {
52                                 stream.Close ();
53                         }
54                 
55                         stream.Close ();
56
57                 }
58
59                 [Test]
60                 public void TestCanConvertFrom ()
61                 {
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");
74
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");
87
88                 }
89
90                 [Test]
91                 public void TestCanConvertTo ()
92                 {
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");
106
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");
120
121                 }
122
123                 [Test]
124                 public void TestConvertFrom ()
125                 {
126                         Image newImage = (Image) imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
127                         
128                         Assert.AreEqual (image.Height, newImage.Height, "CF#1");
129                         Assert.AreEqual (image.Width, newImage.Width, "CF#1a");
130                         
131                         try {
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");
136                         }
137
138                         try {
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");
144                         }
145
146                         try {
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");
152                         }
153
154                         try {
155                                 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
156                                                    new Point (10, 10));
157                                 Assert.Fail ("CF#4: must throw NotSupportedException");
158                         } catch (Exception e) {
159                                 Assert.IsTrue (e is NotSupportedException, "CF#4");
160                         }
161
162                         try {
163                                 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
164                                                    new SizeF (10, 10));
165                                 Assert.Fail ("CF#5: must throw NotSupportedException");
166                         } catch (Exception e) {
167                                 Assert.IsTrue (e is NotSupportedException, "CF#5");
168                         }
169
170                         try {
171                                 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
172                                                    new Object ());
173                                 Assert.Fail ("CF#6: must throw NotSupportedException");
174                         } catch (Exception e) {
175                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
176                         }
177
178
179                         newImage = (Image) imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
180
181                         Assert.AreEqual (image.Height, newImage.Height, "CF#1A");
182                         Assert.AreEqual (image.Width, newImage.Width, "CF#1aA");
183                         
184                         
185                         try {
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");
190                         }
191
192                         try {
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");
198                         }
199
200                         try {
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");
206                         }
207
208                         try {
209                                 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
210                                                    new Point (10, 10));
211                                 Assert.Fail ("CF#4A: must throw NotSupportedException");
212                         } catch (Exception e) {
213                                 Assert.IsTrue (e is NotSupportedException, "CF#4A");
214                         }
215
216                         try {
217                                 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
218                                                    new SizeF (10, 10));
219                                 Assert.Fail ("CF#5A: must throw NotSupportedException");
220                         } catch (Exception e) {
221                                 Assert.IsTrue (e is NotSupportedException, "CF#5A");
222                         }
223
224                         try {
225                                 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
226                                                    new Object ());
227                                 Assert.Fail ("CF#6A: must throw NotSupportedException");
228                         } catch (Exception e) {
229                                 Assert.IsTrue (e is NotSupportedException, "CF#6A");
230                         }
231
232                 }
233
234                 [Test]
235                 public void TestConvertTo ()
236                 {
237                         Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (null,
238                                                                 CultureInfo.InvariantCulture,
239                                                                 image, typeof (String)), "CT#1");
240
241                         Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (image, 
242                                                                         typeof (String)), "CT#1a");
243                                 
244                         byte [] newImageBytes = (byte []) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
245                                                                                         image, imageBytes.GetType ());
246
247                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2");
248
249                         newImageBytes = (byte []) imgConv.ConvertTo (image, imageBytes.GetType ());
250                         
251                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2a");
252
253                         
254                         try {
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");
260                         }
261
262                         try {
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");
268                         }
269
270                         try {
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");
276                         }
277
278                         try {
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");
284                         }
285
286                         try {
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");
292                         }
293
294                         try {
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");
300                         }
301
302                         try {
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");
308                         }
309
310                         try {
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");
316                         }
317
318
319                         Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (null,
320                                                                 CultureInfo.InvariantCulture,
321                                                                 image, typeof (String)), "CT#1A");
322
323                         Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (image, 
324                                                                         typeof (String)), "CT#1aA");
325                                 
326                         newImageBytes = (byte []) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
327                                                                                         image, imageBytes.GetType ());
328
329                         Assert.AreEqual ( imageBytes.Length, newImageBytes.Length, "CT#2A");
330
331                         newImageBytes = (byte []) imgConvFrmTD.ConvertTo (image, imageBytes.GetType ());
332                         
333                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2aA");
334                         
335                         try {
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");
341                         }
342
343                         try {
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");
349                         }
350
351                         try {
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");
357                         }
358
359                         try {
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");
365                         }
366
367                         try {
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");
373                         }
374
375                         try {
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");
381                         }
382
383                         try {
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");
389                         }
390
391                         try {
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");
397                         }
398                 }
399
400                 
401                 [Test]
402                 public void TestGetPropertiesSupported ()
403                 {
404                         Assert.IsTrue (imgConv.GetPropertiesSupported (), "GPS#1");
405                         Assert.IsTrue (imgConv.GetPropertiesSupported (null), "GPS#2");
406                 }
407
408                 [Test]
409                 [Ignore ("This test fails because of bug #58435")]
410                 public void TestGetProperties ()
411                 {
412                         PropertyDescriptorCollection propsColl;
413
414                         propsColl = imgConv.GetProperties (null, image, null);
415                         Assert.AreEqual (13, propsColl.Count, "GP1#1");
416                         
417                         propsColl = imgConv.GetProperties (null, image);
418                         Assert.AreEqual (6, propsColl.Count, "GP1#2");
419
420                         propsColl = imgConv.GetProperties (image);
421                         Assert.AreEqual (6, propsColl.Count, "GP1#3");
422
423                         propsColl = TypeDescriptor.GetProperties (typeof (Image));
424                         Assert.AreEqual (13, propsColl.Count, "GP1#4");
425                         
426                         propsColl = imgConvFrmTD.GetProperties (null, image, null);
427                         Assert.AreEqual (13, propsColl.Count, "GP1#1A");
428                         
429                         propsColl = imgConvFrmTD.GetProperties (null, image);
430                         Assert.AreEqual (6, propsColl.Count, "GP1#2A");
431
432                         propsColl = imgConvFrmTD.GetProperties (image);
433                         Assert.AreEqual (6, propsColl.Count, "GP1#3A");
434                         
435                 }
436         }
437 }