* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestImageFormatConverter.cs
1 //
2 // Tests for System.Drawing.ImageFormatConverter.cs 
3 //
4 // Author:
5 //      Sanjay Gupta (gsanjay@novell.com)
6 //
7 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30 using System;
31 using System.Drawing;
32 using System.Drawing.Imaging;
33 using System.ComponentModel;
34 using System.Globalization;
35 using System.Security.Permissions;
36
37 namespace MonoTests.System.Drawing
38 {
39         [TestFixture]
40         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
41         public class ImageFormatConverterTest
42         {
43                 ImageFormat imageFmt;           
44                 ImageFormatConverter imgFmtConv;
45                 ImageFormatConverter imgFmtConvFrmTD;
46                 String imageFmtStr;
47
48                 [SetUp]
49                 public void SetUp ()            
50                 {
51                         imageFmt = ImageFormat.Bmp; 
52                         imageFmtStr = imageFmt.ToString ();
53                 
54                         imgFmtConv = new ImageFormatConverter();
55                         imgFmtConvFrmTD = (ImageFormatConverter) TypeDescriptor.GetConverter (imageFmt);                        
56                 }
57
58                 [Test]
59                 public void TestCanConvertFrom ()
60                 {
61                         Assert.IsTrue (imgFmtConv.CanConvertFrom (typeof (String)), "CCF#1");
62                         Assert.IsTrue (imgFmtConv.CanConvertFrom (null, typeof (String)), "CCF#1a");
63                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (ImageFormat)), "CCF#2");
64                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (Guid)), "CCF#3");
65                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (Object)), "CCF#4");
66                         Assert.IsTrue (! imgFmtConv.CanConvertFrom (null, typeof (int)), "CCF#5");
67
68                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertFrom (typeof (String)), "CCF#1A");
69                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertFrom (null, typeof (String)), "CCF#1aA");
70                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (ImageFormat)), "CCF#2A");
71                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (Guid)), "CCF#3A");
72                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (Object)), "CCF#4A");
73                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertFrom (null, typeof (int)), "CCF#5A");
74
75                 }
76
77                 [Test]
78                 public void TestCanConvertTo ()
79                 {
80                         Assert.IsTrue (imgFmtConv.CanConvertTo (typeof (String)), "CCT#1");
81                         Assert.IsTrue (imgFmtConv.CanConvertTo (null, typeof (String)), "CCT#1a");
82                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (ImageFormat)), "CCT#2");
83                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (Guid)), "CCT#3");
84                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (Object)), "CCT#4");
85                         Assert.IsTrue (! imgFmtConv.CanConvertTo (null, typeof (int)), "CCT#5");
86
87                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertTo (typeof (String)), "CCT#1A");
88                         Assert.IsTrue (imgFmtConvFrmTD.CanConvertTo (null, typeof (String)), "CCT#1aA");
89                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (ImageFormat)), "CCT#2A");
90                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (Guid)), "CCT#3A");
91                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (Object)), "CCT#4A");
92                         Assert.IsTrue (! imgFmtConvFrmTD.CanConvertTo (null, typeof (int)), "CCT#5A");
93                 }
94
95                 [Test]
96                 public void TestConvertFrom ()
97                 {
98                         Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConv.ConvertFrom (null,
99                                                                 CultureInfo.InvariantCulture,
100                                                                 ImageFormat.Bmp.ToString ()), "CF#1");
101                         
102                         try {
103                                 imgFmtConv.ConvertFrom ("System.Drawing.String");
104                                 Assert.Fail ("CF#2: must throw NotSupportedException");
105                         } catch (Exception e) {
106                                 Assert.IsTrue (e is NotSupportedException, "CF#2");
107                         }
108
109                         try {
110                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
111                                                    "System.Drawing.String");
112                                 Assert.Fail ("CF#2a: must throw NotSupportedException");
113                         } catch (Exception e) {
114                                 Assert.IsTrue (e is NotSupportedException, "CF#2a");
115                         }
116
117                         try {
118                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
119                                                    ImageFormat.Bmp);
120                                 Assert.Fail ("CF#3: must throw NotSupportedException");
121                         } catch (Exception e) {
122                                 Assert.IsTrue (e is NotSupportedException, "CF#3");
123                         }
124
125                         try {
126                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
127                                                    ImageFormat.Bmp.Guid);
128                                 Assert.Fail ("CF#4: must throw NotSupportedException");
129                         } catch (Exception e) {
130                                 Assert.IsTrue (e is NotSupportedException, "CF#4");
131                         }
132
133                         try {
134                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture,
135                                                    new Object ());
136                                 Assert.Fail ("CF#5: must throw NotSupportedException");
137                         } catch (Exception e) {
138                                 Assert.IsTrue (e is NotSupportedException, "CF#5");
139                         }
140
141                         try {
142                                 imgFmtConv.ConvertFrom (null, CultureInfo.InvariantCulture, 10);
143                                 Assert.Fail ("CF#6: must throw NotSupportedException");
144                         } catch (Exception e) {
145                                 Assert.IsTrue (e is NotSupportedException, "CF#6");
146                         }
147
148                         
149                         Assert.AreEqual (imageFmt, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
150                                                                 CultureInfo.InvariantCulture,
151                                                                 ImageFormat.Bmp.ToString ()), "CF#1A");
152                         
153                         try {
154                                 imgFmtConvFrmTD.ConvertFrom ("System.Drawing.String");
155                                 Assert.Fail ("CF#2A: must throw NotSupportedException");
156                         } catch (Exception e) {
157                                 Assert.IsTrue (e is NotSupportedException, "CF#2A");
158                         }
159
160                         try {
161                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
162                                                    "System.Drawing.String");
163                                 Assert.Fail ("CF#2aA: must throw NotSupportedException");
164                         } catch (Exception e) {
165                                 Assert.IsTrue (e is NotSupportedException, "CF#2aA");
166                         }
167
168                         try {
169                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
170                                                    ImageFormat.Bmp);
171                                 Assert.Fail ("CF#3A: must throw NotSupportedException");
172                         } catch (Exception e) {
173                                 Assert.IsTrue (e is NotSupportedException, "CF#3A");
174                         }
175
176                         try {
177                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
178                                                    ImageFormat.Bmp.Guid);
179                                 Assert.Fail ("CF#4A: must throw NotSupportedException");
180                         } catch (Exception e) {
181                                 Assert.IsTrue (e is NotSupportedException, "CF#4A");
182                         }
183
184                         try {
185                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture,
186                                                    new Object ());
187                                 Assert.Fail ("CF#5A: must throw NotSupportedException");
188                         } catch (Exception e) {
189                                 Assert.IsTrue (e is NotSupportedException, "CF#5A");
190                         }
191
192                         try {
193                                 imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, 10);
194                                 Assert.Fail ("CF#6A: must throw NotSupportedException");
195                         } catch (Exception e) {
196                                 Assert.IsTrue (e is NotSupportedException, "CF#6A");
197                         }
198                 }
199
200                 private ImageFormat ShortName (string s)
201                 {
202                         return (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null, CultureInfo.InvariantCulture, s);
203                 }
204
205                 [Test]
206                 public void ConvertFrom_ShortName ()
207                 {
208                         Assert.AreEqual (ImageFormat.Bmp, ShortName ("Bmp"), "Bmp");
209                         Assert.AreEqual (ImageFormat.Emf, ShortName ("Emf"), "Emf");
210                         Assert.AreEqual (ImageFormat.Exif, ShortName ("Exif"), "Exif");
211                         Assert.AreEqual (ImageFormat.Gif, ShortName ("Gif"), "Gif");
212                         Assert.AreEqual (ImageFormat.Tiff, ShortName ("Tiff"), "Tiff");
213                         Assert.AreEqual (ImageFormat.Png, ShortName ("Png"), "Png");
214                         Assert.AreEqual (ImageFormat.MemoryBmp, ShortName ("MemoryBmp"), "MemoryBmp");
215                         Assert.AreEqual (ImageFormat.Icon, ShortName ("Icon"), "Icon");
216                         Assert.AreEqual (ImageFormat.Jpeg, ShortName ("Jpeg"), "Jpeg");
217                         Assert.AreEqual (ImageFormat.Wmf, ShortName ("Wmf"), "Wmf");
218                 }
219
220                 [Test]
221                 public void ConvertFrom_LongName ()
222                 {
223                         Assert.AreEqual (ImageFormat.Bmp, (ImageFormat) imgFmtConvFrmTD.ConvertFrom (null,
224                                                                 CultureInfo.InvariantCulture,
225                                                                 ImageFormat.Bmp.ToString ()), "CF#1A");
226                 }
227
228                 [Test]
229                 public void TestConvertTo ()
230                 {
231                         Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (null,
232                                                                 CultureInfo.InvariantCulture,
233                                                                 imageFmt, typeof (String)), "CT#1");
234
235                         Assert.AreEqual (imageFmtStr, (String) imgFmtConv.ConvertTo (imageFmt, 
236                                                                         typeof (String)), "CT#1a");
237                                                         
238                         try {
239                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture, 
240                                                  imageFmt, typeof (ImageFormat));
241                                 Assert.Fail ("CT#2: must throw NotSupportedException");
242                         } catch (Exception e) {
243                                 Assert.IsTrue (e is NotSupportedException, "CT#2");
244                         }
245
246                         try {
247                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
248                                                  imageFmt, typeof (Guid));
249                                 Assert.Fail ("CT#2a: must throw NotSupportedException");
250                         } catch (Exception e) {
251                                 Assert.IsTrue (e is NotSupportedException, "CT#2a");
252                         }
253
254                         try {
255                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
256                                                  imageFmt, typeof (Object));
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                                 imgFmtConv.ConvertTo (null, CultureInfo.InvariantCulture,
264                                                  imageFmt, typeof (int));
265                                 Assert.Fail ("CT#4: must throw NotSupportedException");
266                         } catch (Exception e) {
267                                 Assert.IsTrue (e is NotSupportedException, "CT#4");
268                         }
269
270
271                         Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (null,
272                                                                 CultureInfo.InvariantCulture,
273                                                                 imageFmt, typeof (String)), "CT#1A");
274
275                         Assert.AreEqual (imageFmtStr, (String) imgFmtConvFrmTD.ConvertTo (imageFmt, 
276                                                                         typeof (String)), "CT#1aA");
277                                                         
278                         try {
279                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture, 
280                                                  imageFmt, typeof (ImageFormat));
281                                 Assert.Fail ("CT#2A: must throw NotSupportedException");
282                         } catch (Exception e) {
283                                 Assert.IsTrue (e is NotSupportedException, "CT#2A");
284                         }
285
286                         try {
287                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
288                                                  imageFmt, typeof (Guid));
289                                 Assert.Fail ("CT#2aA: must throw NotSupportedException");
290                         } catch (Exception e) {
291                                 Assert.IsTrue (e is NotSupportedException, "CT#2aA");
292                         }
293
294                         try {
295                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
296                                                  imageFmt, typeof (Object));
297                                 Assert.Fail ("CT#3A: must throw NotSupportedException");
298                         } catch (Exception e) {
299                                 Assert.IsTrue (e is NotSupportedException, "CT#3A");
300                         }
301
302                         try {
303                                 imgFmtConvFrmTD.ConvertTo (null, CultureInfo.InvariantCulture,
304                                                  imageFmt, typeof (int));
305                                 Assert.Fail ("CT#4A: must throw NotSupportedException");
306                         } catch (Exception e) {
307                                 Assert.IsTrue (e is NotSupportedException, "CT#4A");
308                         }
309                 }
310
311                 
312                 /*[Test]
313                 public void TestGetStandardValuesSupported ()
314                 {
315                         Assert.IsTrue (imgFmtConv.GetPropertiesSupported (), "GSVS#1");
316                         Assert.IsTrue (imgFmtConv.GetPropertiesSupported (null), "GSVS#2");
317                 }
318
319                 [Test]
320                 public void TestGetStandardValues ()
321                 {                       
322                         //MONO TODO                     
323                 }*/
324         }
325 }