[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestImageConverter.cs
1 //
2 // Tests for System.Drawing.ImageConverter.cs 
3 //
4 // Authors:
5 //      Sanjay Gupta (gsanjay@novell.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30
31 using NUnit.Framework;
32 using System;
33 using System.Drawing;
34 using System.Drawing.Imaging;
35 using System.Collections;
36 using System.ComponentModel;
37 using System.Globalization;
38 using System.IO;
39 using System.Security.Permissions;
40
41 namespace MonoTests.System.Drawing
42 {
43         [TestFixture]
44         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
45         public class ImageConverterTest 
46         {
47                 Image image;            
48                 ImageConverter imgConv;
49                 ImageConverter imgConvFrmTD;
50                 String imageStr;
51                 byte [] imageBytes;
52
53                 [SetUp]
54                 public void SetUp ()            
55                 {
56                         image = Image.FromFile (TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp"));
57                         imageStr = image.ToString ();
58                 
59                         imgConv = new ImageConverter();
60                         imgConvFrmTD = (ImageConverter) TypeDescriptor.GetConverter (image);
61                         
62                         Stream stream = new FileStream (TestBitmap.getInFile ("bitmaps/almogaver24bits1.bmp"), FileMode.Open);
63                         int length = (int) stream.Length;
64                         imageBytes = new byte [length];
65                         
66                         try {
67                                 if (stream.Read (imageBytes, 0, length) != length)
68                                         Assert.Fail ("SU#1: Read Failure"); 
69                         } catch (Exception e) {
70                                 Assert.Fail ("SU#2 Exception thrown while reading. Exception is: "+e.Message);
71                         } finally {
72                                 stream.Close ();
73                         }
74                 
75                         stream.Close ();
76
77                 }
78
79                 [Test]
80                 public void TestCanConvertFrom ()
81                 {
82                         Assert.IsTrue (imgConv.CanConvertFrom (typeof (byte [])), "CCF#1");
83                         Assert.IsTrue (imgConv.CanConvertFrom (null, typeof (byte [])), "CCF#1a");
84                         Assert.IsTrue (imgConv.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1b");
85                         Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (String)), "CCF#2");
86                         Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Rectangle)), "CCF#3");
87                         Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Point)), "CCF#4");
88                         Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (PointF)), "CCF#5");
89                         Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Size)), "CCF#6");
90                         Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (SizeF)), "CCF#7");
91                         Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Object)), "CCF#8");
92                         Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (int)), "CCF#9");
93                         Assert.IsTrue (! imgConv.CanConvertFrom (null, typeof (Metafile)), "CCF#10");
94
95                         Assert.IsTrue (imgConvFrmTD.CanConvertFrom (typeof (byte [])), "CCF#1A");
96                         Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, typeof (byte [])), "CCF#1aA");
97                         Assert.IsTrue (imgConvFrmTD.CanConvertFrom (null, imageBytes.GetType ()), "CCF#1bA");
98                         Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#2A");
99                         Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Rectangle)), "CCF#3A");
100                         Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Point)), "CCF#4A");
101                         Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (PointF)), "CCF#5A");
102                         Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Size)), "CCF#6A");
103                         Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (SizeF)), "CCF#7A");
104                         Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#8A");
105                         Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#9A");
106                         Assert.IsTrue (! imgConvFrmTD.CanConvertFrom (null, typeof (Metafile)), "CCF#10A");
107
108                 }
109
110                 [Test]
111                 public void TestCanConvertTo ()
112                 {
113                         Assert.IsTrue (imgConv.CanConvertTo (typeof (String)), "CCT#1");
114                         Assert.IsTrue (imgConv.CanConvertTo (null, typeof (String)), "CCT#1a");
115                         Assert.IsTrue (imgConv.CanConvertTo (null, imageStr.GetType ()), "CCT#1b");
116                         Assert.IsTrue (imgConv.CanConvertTo (typeof (byte [])), "CCT#2");
117                         Assert.IsTrue (imgConv.CanConvertTo (null, typeof (byte [])), "CCT#2a");
118                         Assert.IsTrue (imgConv.CanConvertTo (null, imageBytes.GetType ()), "CCT#2b");
119                         Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Rectangle)), "CCT#3");
120                         Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Point)), "CCT#4");
121                         Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (PointF)), "CCT#5");
122                         Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Size)), "CCT#6");
123                         Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (SizeF)), "CCT#7");
124                         Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (Object)), "CCT#8");
125                         Assert.IsTrue (! imgConv.CanConvertTo (null, typeof (int)), "CCT#9");
126
127                         Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
128                         Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
129                         Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageStr.GetType ()), "CCT#1bA");
130                         Assert.IsTrue (imgConvFrmTD.CanConvertTo (typeof (byte [])), "CCT#2A");
131                         Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, typeof (byte [])), "CCT#2aA");
132                         Assert.IsTrue (imgConvFrmTD.CanConvertTo (null, imageBytes.GetType ()), "CCT#2bA");
133                         Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Rectangle)), "CCT#3A");
134                         Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Point)), "CCT#4A");
135                         Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (PointF)), "CCT#5A");
136                         Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Size)), "CCT#6A");
137                         Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (SizeF)), "CCT#7A");
138                         Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#8A");
139                         Assert.IsTrue (! imgConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#9A");
140
141                 }
142
143                 [Test]
144                 public void ConvertFrom ()
145                 {
146                         Image newImage = (Image) imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
147                         
148                         Assert.AreEqual (image.Height, newImage.Height, "CF#1");
149                         Assert.AreEqual (image.Width, newImage.Width, "CF#1a");
150
151                         Assert.AreEqual ("(none)", imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, null, typeof (string)), "Null/Empty");
152
153                         newImage = (Image) imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, imageBytes);
154
155                         Assert.AreEqual (image.Height, newImage.Height, "CF#1A");
156                         Assert.AreEqual (image.Width, newImage.Width, "CF#1aA");
157                 }
158                 
159                 [Test]
160                 [ExpectedException (typeof (NotSupportedException))]
161                 public void ConvertFrom_BadString ()
162                 {
163                         imgConv.ConvertFrom ("System.Drawing.String");
164                 }
165
166                 [Test]
167                 [ExpectedException (typeof (NotSupportedException))]
168                 public void ConvertFrom_BadString_WithCulture ()
169                 {
170                         imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, "System.Drawing.String");
171                 }
172
173                 [Test]
174                 [ExpectedException (typeof (NotSupportedException))]
175                 public void ConvertFrom_Bitmap ()
176                 {
177                         imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new Bitmap (20, 20));
178                 }
179
180                 [Test]
181                 [ExpectedException (typeof (NotSupportedException))]
182                 public void ConvertFrom_Point ()
183                 {
184                         imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new Point (10, 10));
185                 }
186
187                 [Test]
188                 [ExpectedException (typeof (NotSupportedException))]
189                 public void ConvertFrom_SizeF ()
190                 {
191                         imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new SizeF (10, 10));
192                 }
193
194                 [Test]
195                 [ExpectedException (typeof (NotSupportedException))]
196                 public void ConvertFrom_Object ()
197                 {
198                         imgConv.ConvertFrom (null, CultureInfo.InvariantCulture, new Object ());
199                 }
200
201                 [Test]
202                 [ExpectedException (typeof (NotSupportedException))]
203                 public void TypeDescriptor_ConvertFrom_BadString ()
204                 {
205                         imgConvFrmTD.ConvertFrom ("System.Drawing.String");
206                 }
207
208                 [Test]
209                 [ExpectedException (typeof (NotSupportedException))]
210                 public void TypeDescriptor_ConvertFrom_BadString_Culture ()
211                 {
212                         imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, "System.Drawing.String");
213                 }
214
215                 [Test]
216                 [ExpectedException (typeof (NotSupportedException))]
217                 public void TypeDescriptor_ConvertFrom_Bitmap ()
218                 {
219                         imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new Bitmap (20, 20));
220                 }
221
222                 [Test]
223                 [ExpectedException (typeof (NotSupportedException))]
224                 public void TypeDescriptor_ConvertFrom_Point ()
225                 {
226                         imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new Point (10, 10));
227                 }
228
229                 [Test]
230                 [ExpectedException (typeof (NotSupportedException))]
231                 public void TypeDescriptor_ConvertFrom_SizeF ()
232                 {
233                         imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new SizeF (10, 10));
234                 }
235
236                 [Test]
237                 [ExpectedException (typeof (NotSupportedException))]
238                 public void TypeDescriptor_ConvertFrom_Object ()
239                 {
240                         imgConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, new Object ());
241                 }
242
243                 [Test]
244                 public void ConvertTo ()
245                 {
246                         Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
247                                 image, typeof (string)), "CT#1");
248
249                         Assert.AreEqual (imageStr, (String) imgConv.ConvertTo (image, typeof (string)), "CT#1a");
250
251                         Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
252                                 image, typeof (string)), "CT#1A");
253
254                         Assert.AreEqual (imageStr, (String) imgConvFrmTD.ConvertTo (image, typeof (string)), "CT#1aA");
255                 }
256                                 
257                 [Test]
258                 [NUnit.Framework.Category ("NotWorking")]
259                 public void ConvertTo_ByteArray ()
260                 {
261                         byte[] newImageBytes = (byte[]) imgConv.ConvertTo (null, CultureInfo.InvariantCulture,
262                                 image, imageBytes.GetType ());
263
264                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2");
265
266                         newImageBytes = (byte[]) imgConv.ConvertTo (image, imageBytes.GetType ());
267                         
268                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2a");
269
270                         newImageBytes = (byte[]) imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
271                                 image, imageBytes.GetType ());
272
273                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2A");
274
275                         newImageBytes = (byte[]) imgConvFrmTD.ConvertTo (image, imageBytes.GetType ());
276                         
277                         Assert.AreEqual (imageBytes.Length, newImageBytes.Length, "CT#2aA");
278                 }
279
280                 [Test]
281                 [ExpectedException (typeof (NotSupportedException))]
282                 public void ConvertTo_Rectangle ()
283                 {
284                         imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Rectangle));
285                 }
286
287                 [Test]
288                 [ExpectedException (typeof (NotSupportedException))]
289                 public void ConvertTo_Image ()
290                 {
291                         imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, image.GetType ());
292                 }
293
294                 [Test]
295                 [ExpectedException (typeof (NotSupportedException))]
296                 public void ConvertTo_Size ()
297                 {
298                         imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Size));
299                 }
300
301                 [Test]
302                 [ExpectedException (typeof (NotSupportedException))]
303                 public void ConvertTo_Bitmap ()
304                 {
305                         imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Bitmap));
306                 }
307
308                 [Test]
309                 [ExpectedException (typeof (NotSupportedException))]
310                 public void ConvertTo_Point ()
311                 {
312                         imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Point));
313                 }
314
315                 [Test]
316                 [ExpectedException (typeof (NotSupportedException))]
317                 public void ConvertTo_Metafile ()
318                 {
319                         imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Metafile));
320                 }
321
322                 [Test]
323                 [ExpectedException (typeof (NotSupportedException))]
324                 public void ConvertTo_Object ()
325                 {
326                         imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Object));
327                 }
328
329                 [Test]
330                 [ExpectedException (typeof (NotSupportedException))]
331                 public void ConvertTo_Int ()
332                 {
333                         imgConv.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (int));
334                 }
335
336                 [Test]
337                 [ExpectedException (typeof (NotSupportedException))]
338                 public void TypeDescriptor_ConvertTo_Rectangle ()
339                 {
340                         imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Rectangle));
341                 }
342
343                 [Test]
344                 [ExpectedException (typeof (NotSupportedException))]
345                 public void TypeDescriptor_ConvertTo_Image ()
346                 {
347                         imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, image.GetType ());
348                 }
349
350                 [Test]
351                 [ExpectedException (typeof (NotSupportedException))]
352                 public void TypeDescriptor_ConvertTo_Size ()
353                 {
354                         imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Size));
355                 }
356
357                 [Test]
358                 [ExpectedException (typeof (NotSupportedException))]
359                 public void TypeDescriptor_ConvertTo_Bitmap ()
360                 {
361                         imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Bitmap));
362                 }
363
364                 [Test]
365                 [ExpectedException (typeof (NotSupportedException))]
366                 public void TypeDescriptor_ConvertTo_Point ()
367                 {
368                         imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Point));
369                 }
370
371                 [Test]
372                 [ExpectedException (typeof (NotSupportedException))]
373                 public void TypeDescriptor_ConvertTo_Metafile ()
374                 {
375                         imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Metafile));
376                 }
377
378                 [Test]
379                 [ExpectedException (typeof (NotSupportedException))]
380                 public void TypeDescriptor_ConvertTo_Object ()
381                 {
382                         imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (Object));
383                 }
384
385                 [Test]
386                 [ExpectedException (typeof (NotSupportedException))]
387                 public void TypeDescriptor_ConvertTo_Int ()
388                 {
389                         imgConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, image, typeof (int));
390                 }
391
392                 [Test]
393                 public void TestGetPropertiesSupported ()
394                 {
395                         Assert.IsTrue (imgConv.GetPropertiesSupported (), "GPS#1");
396                         Assert.IsTrue (imgConv.GetPropertiesSupported (null), "GPS#2");
397                 }
398
399                 [Test]
400                 public void TestGetProperties ()
401                 {
402                         int basecount = 1;
403                         PropertyDescriptorCollection propsColl;
404
405                         propsColl = imgConv.GetProperties (null, image, null);
406                         Assert.AreEqual (13 + basecount, propsColl.Count, "GP1#1");
407                         
408                         propsColl = imgConv.GetProperties (null, image);
409                         Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#2");
410
411                         propsColl = imgConv.GetProperties (image);
412                         Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#3");
413
414                         propsColl = TypeDescriptor.GetProperties (typeof (Image));
415                         Assert.AreEqual (13 + basecount, propsColl.Count, "GP1#4");
416                         
417                         propsColl = imgConvFrmTD.GetProperties (null, image, null);
418                         Assert.AreEqual (13 + basecount, propsColl.Count, "GP1#1A");
419                         
420                         propsColl = imgConvFrmTD.GetProperties (null, image);
421                         Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#2A");
422
423                         propsColl = imgConvFrmTD.GetProperties (image);
424                         Assert.AreEqual (6 + basecount, propsColl.Count, "GP1#3A");
425                 }
426         }
427 }