2005-09-16 Sebastien Pouliot <sebastien@ximian.com>
[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
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
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.
29 //
30
31
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;
40 using System.Security.Permissions;
41
42 namespace MonoTests.System.Drawing
43 {
44         [TestFixture]
45         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
46         public class ImageConverterTest 
47         {
48                 Image image;            
49                 ImageConverter imgConv;
50                 ImageConverter imgConvFrmTD;
51                 String imageStr;
52                 byte [] imageBytes;
53
54                 [TearDown]
55                 public void TearDown () {}
56
57                 [SetUp]
58                 public void SetUp ()            
59                 {
60                         image = Image.FromFile (TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp"));
61                         imageStr = image.ToString ();
62                 
63                         imgConv = new ImageConverter();
64                         imgConvFrmTD = (ImageConverter) TypeDescriptor.GetConverter (image);
65                         
66                         Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/almogaver24bits1.bmp"), FileMode.Open);
67                         int length = (int) stream.Length;
68                         imageBytes = new byte [length];
69                         
70                         try {
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);
75                         } finally {
76                                 stream.Close ();
77                         }
78                 
79                         stream.Close ();
80
81                 }
82
83                 [Test]
84                 public void TestCanConvertFrom ()
85                 {
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");
98
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");
111
112                 }
113
114                 [Test]
115                 public void TestCanConvertTo ()
116                 {
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");
130
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");
144
145                 }
146
147                 [Test]
148                 public void TestConvertFrom ()
149                 {
150                         Image newImage = (Image) imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
151                         
152                         Assert.AreEqual (image.Height, newImage.Height, "CF#1");
153                         Assert.AreEqual (image.Width, newImage.Width, "CF#1a");
154                         
155                         try {
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");
160                         }
161
162                         try {
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");
168                         }
169
170                         try {
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");
176                         }
177
178                         try {
179                                 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
180                                                    new Point (10, 10));
181                                 Assert.Fail ("CF#4: must throw NotSupportedException");
182                         } catch (Exception e) {
183                                 Assert.IsTrue (e is NotSupportedException, "CF#4");
184                         }
185
186                         try {
187                                 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
188                                                    new SizeF (10, 10));
189                                 Assert.Fail ("CF#5: must throw NotSupportedException");
190                         } catch (Exception e) {
191                                 Assert.IsTrue (e is NotSupportedException, "CF#5");
192                         }
193
194                         try {
195                                 imgConv.ConvertFrom (null, CultureInfo.InvariantCulture,
196                                                    new Object ());
197                                 Assert.Fail ("CF#6: must throw NotSupportedException");
198                         } catch (Exception e) {
199                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
200                         }
201
202
203                         newImage = (Image) imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
204
205                         Assert.AreEqual (image.Height, newImage.Height, "CF#1A");
206                         Assert.AreEqual (image.Width, newImage.Width, "CF#1aA");
207                         
208                         
209                         try {
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");
214                         }
215
216                         try {
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");
222                         }
223
224                         try {
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");
230                         }
231
232                         try {
233                                 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
234                                                    new Point (10, 10));
235                                 Assert.Fail ("CF#4A: must throw NotSupportedException");
236                         } catch (Exception e) {
237                                 Assert.IsTrue (e is NotSupportedException, "CF#4A");
238                         }
239
240                         try {
241                                 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
242                                                    new SizeF (10, 10));
243                                 Assert.Fail ("CF#5A: must throw NotSupportedException");
244                         } catch (Exception e) {
245                                 Assert.IsTrue (e is NotSupportedException, "CF#5A");
246                         }
247
248                         try {
249                                 imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
250                                                    new Object ());
251                                 Assert.Fail ("CF#6A: must throw NotSupportedException");
252                         } catch (Exception e) {
253                                 Assert.IsTrue (e is NotSupportedException, "CF#6A");
254                         }
255
256                 }
257
258                 [Test]
259                 public void TestConvertTo ()
260                 {
261                         Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (null,
262                                                                 CultureInfo.InvariantCulture,
263                                                                 image, typeof (String)), "CT#1");
264
265                         Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (image, 
266                                                                         typeof (String)), "CT#1a");
267                                 
268                         /*byte [] newImageBytes = (byte []) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
269                                                                                         image, imageBytes.GetType ());
270
271                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2");
272
273                         newImageBytes = (byte []) imgConv.ConvertTo (image, imageBytes.GetType ());
274                         
275                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2a");
276                         
277                         try {
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");
283                         }
284
285                         try {
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");
291                         }
292
293                         try {
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");
299                         }
300
301                         try {
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");
307                         }
308
309                         try {
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");
315                         }
316
317                         try {
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");
323                         }
324
325                         try {
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");
331                         }
332
333                         try {
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");
339                         }
340                         */
341                         
342                         Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (null,
343                                                                 CultureInfo.InvariantCulture,
344                                                                 image, typeof (String)), "CT#1A");
345
346                         Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (image, 
347                                                                         typeof (String)), "CT#1aA");
348                                 
349                         /*newImageBytes = (byte []) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
350                                                                                         image, imageBytes.GetType ());
351
352                         Assert.AreEqual ( imageBytes.Length, newImageBytes.Length, "CT#2A");
353
354                         newImageBytes = (byte []) imgConvFrmTD.ConvertTo (image, imageBytes.GetType ());
355                         
356                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2aA");
357                         
358                         try {
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");
364                         }
365
366                         try {
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");
372                         }
373
374                         try {
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");
380                         }
381
382                         try {
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");
388                         }
389
390                         try {
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");
396                         }
397
398                         try {
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");
404                         }
405
406                         try {
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");
412                         }
413
414                         try {
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");
420                         }*/
421                 }
422
423                 
424                 [Test]
425                 public void TestGetPropertiesSupported ()
426                 {
427                         Assert.IsTrue (imgConv.GetPropertiesSupported (), "GPS#1");
428                         Assert.IsTrue (imgConv.GetPropertiesSupported (null), "GPS#2");
429                 }
430
431                 [Test]
432                 public void TestGetProperties ()
433                 {
434                         PropertyDescriptorCollection propsColl;
435
436                         propsColl = imgConv.GetProperties (null, image, null);
437                         Assert.AreEqual (13, propsColl.Count, "GP1#1");
438                         
439                         propsColl = imgConv.GetProperties (null, image);
440                         Assert.AreEqual (6, propsColl.Count, "GP1#2");
441
442                         propsColl = imgConv.GetProperties (image);
443                         Assert.AreEqual (6, propsColl.Count, "GP1#3");
444
445                         propsColl = TypeDescriptor.GetProperties (typeof (Image));
446                         Assert.AreEqual (13, propsColl.Count, "GP1#4");
447                         
448                         propsColl = imgConvFrmTD.GetProperties (null, image, null);
449                         Assert.AreEqual (13, propsColl.Count, "GP1#1A");
450                         
451                         propsColl = imgConvFrmTD.GetProperties (null, image);
452                         Assert.AreEqual (6, propsColl.Count, "GP1#2A");
453
454                         propsColl = imgConvFrmTD.GetProperties (image);
455                         Assert.AreEqual (6, propsColl.Count, "GP1#3A");
456                         
457                 }
458         }
459 }