[Cleanup] Removed TARGET_JVM
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / TestImageCodecInfo.cs
1 //
2 // ImageCodecInfo class testing unit
3 //
4 // Authors:
5 //      Jordi Mas i Hernàndez (jordi@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2004 Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004-2007 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 using System;
32 using System.Drawing;
33 using System.Drawing.Imaging;
34 using NUnit.Framework;
35 using System.Collections;
36 using System.Security.Permissions;
37 using System.Text.RegularExpressions;
38
39 namespace MonoTests.System.Drawing.Imaging {
40
41         [TestFixture]
42         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
43         public class ImageCodecInfoTest {
44
45                 Hashtable decoders;
46                 Hashtable encoders;
47
48                 ImageCodecInfo GetEncoder (Guid clsid)
49                 {
50                         return (ImageCodecInfo) encoders [clsid];
51                 }
52
53                 ImageCodecInfo GetDecoder (Guid clsid) {
54                         return (ImageCodecInfo) decoders [clsid];
55                 }
56
57                 [TestFixtureSetUp]
58                 public void FixtureGetReady()           
59                 {
60                         decoders = new Hashtable ();
61                         encoders = new Hashtable ();
62
63                         foreach (ImageCodecInfo decoder in ImageCodecInfo.GetImageDecoders ())
64                                 decoders[decoder.Clsid] = decoder;
65                 
66                         foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders ())
67                                 encoders[encoder.Clsid] = encoder;
68                 }
69
70                 static void Check (ImageCodecInfo e, ImageCodecInfo d, Guid FormatID, string CodecName, string DllName,
71                         string FilenameExtension, ImageCodecFlags Flags, string FormatDescription,
72                         string MimeType, int Version, int signatureLength, string mask, string pattern, string pattern2)
73                 {
74                         Regex extRegex = new Regex (@"^(\*\.\w+(;(\*\.\w+))*;)?"+
75                                 Regex.Escape (FilenameExtension)+@"(;\*\.\w+(;(\*\.\w+))*)?$",
76                                 RegexOptions.IgnoreCase | RegexOptions.Singleline);
77
78                         if (e != null) {
79                                 Assert.AreEqual (FormatID, e.FormatID, "Encoder.FormatID");
80                                 Assert.IsTrue (e.CodecName.IndexOf (CodecName)>=0,
81                                         "Encoder.CodecName contains "+CodecName);
82                                 Assert.AreEqual (DllName, e.DllName, "Encoder.DllName");
83                                 Assert.IsTrue (extRegex.IsMatch (e.FilenameExtension),
84                                         "Encoder.FilenameExtension is a right list with "+FilenameExtension);
85                                 Assert.AreEqual (Flags, e.Flags, "Encoder.Flags");
86                                 Assert.IsTrue (e.FormatDescription.IndexOf (FormatDescription)>=0,
87                                         "Encoder.FormatDescription contains "+FormatDescription);
88                                 Assert.IsTrue (e.MimeType.IndexOf (MimeType)>=0,
89                                         "Encoder.MimeType contains "+MimeType);
90
91                                 Assert.AreEqual (signatureLength, e.SignatureMasks.Length, "Encoder.SignatureMasks.Length");
92                                 for (int i = 0; i < signatureLength; i++) {
93                                         Assert.AreEqual (mask, BitConverter.ToString (e.SignatureMasks[i]), String.Format ("Encoder.SignatureMasks[{0}]", i));
94                                 }
95                                 Assert.AreEqual (signatureLength, e.SignaturePatterns.Length, "Encoder.SignaturePatterns.Length");
96                                 Assert.AreEqual (pattern, BitConverter.ToString (e.SignaturePatterns[0]), "Encoder.SignaturePatterns[0]");
97                                 if (pattern2 != null)
98                                         Assert.AreEqual (pattern2, BitConverter.ToString (e.SignaturePatterns[1]), "Encoder.SignaturePatterns[1]");
99                         }
100                         if (d != null) {
101                                 Assert.AreEqual (FormatID, d.FormatID, "Decoder.FormatID");
102                                 Assert.IsTrue (d.CodecName.IndexOf (CodecName)>=0,
103                                         "Decoder.CodecName contains "+CodecName);
104                                 Assert.AreEqual (DllName, d.DllName, "Decoder.DllName");
105                                 Assert.IsTrue (extRegex.IsMatch (d.FilenameExtension),
106                                         "Decoder.FilenameExtension is a right list with "+FilenameExtension);
107                                 Assert.AreEqual (Flags, d.Flags, "Decoder.Flags");
108                                 Assert.IsTrue (d.FormatDescription.IndexOf (FormatDescription)>=0,
109                                         "Decoder.FormatDescription contains "+FormatDescription);
110                                 Assert.IsTrue (d.MimeType.IndexOf (MimeType)>=0,
111                                         "Decoder.MimeType contains "+MimeType);
112
113                                 Assert.AreEqual (signatureLength, d.SignatureMasks.Length, "Decoder.SignatureMasks.Length");
114                                 for (int i = 0; i < signatureLength; i++) {
115                                         Assert.AreEqual (mask, BitConverter.ToString (d.SignatureMasks[i]), String.Format ("Decoder.SignatureMasks[{0}]", i));
116                                 }
117                                 Assert.AreEqual (signatureLength, d.SignaturePatterns.Length, "Decoder.SignaturePatterns.Length");
118                                 Assert.AreEqual (pattern, BitConverter.ToString (d.SignaturePatterns[0]), "Decoder.SignaturePatterns[0]");
119                                 if (pattern2 != null)
120                                         Assert.AreEqual (pattern2, BitConverter.ToString (d.SignaturePatterns[1]), "Decoder.SignaturePatterns[1]");
121                         }
122                 }
123
124                 [Test]
125                 public void Decoders ()
126                 {
127                         Assert.AreEqual (8, decoders.Count, "Count");
128                         foreach (DictionaryEntry de in decoders) {
129                                 string guid = de.Key.ToString ();
130                                 switch (guid) {
131                                 case "557cf402-1a04-11d3-9a73-0000f81ef32e": // GIF
132                                 case "557cf403-1a04-11d3-9a73-0000f81ef32e": // EMF
133                                 case "557cf400-1a04-11d3-9a73-0000f81ef32e": // BMP/DIB/RLE
134                                 case "557cf401-1a04-11d3-9a73-0000f81ef32e": // JPG,JPEG,JPE,JFIF
135                                 case "557cf406-1a04-11d3-9a73-0000f81ef32e": // PNG
136                                 case "557cf407-1a04-11d3-9a73-0000f81ef32e": // ICO
137                                 case "557cf404-1a04-11d3-9a73-0000f81ef32e": // WMF
138                                 case "557cf405-1a04-11d3-9a73-0000f81ef32e": // TIF,TIFF
139                                         break;
140                                 default:
141                                         Assert.Ignore ("Unknown decoder " + guid);
142                                         break;
143                                 }
144                         }
145                 }
146
147                 [Test]
148                 public void Encoders ()
149                 {
150                         Assert.AreEqual (5, encoders.Count, "Count");
151                         foreach (DictionaryEntry de in encoders) {
152                                 string guid = de.Key.ToString ();
153                                 switch (guid) {
154                                 case "557cf402-1a04-11d3-9a73-0000f81ef32e": // GIF
155                                 case "557cf400-1a04-11d3-9a73-0000f81ef32e": // BMP/DIB/RLE
156                                 case "557cf401-1a04-11d3-9a73-0000f81ef32e": // JPG,JPEG,JPE,JFIF
157                                 case "557cf406-1a04-11d3-9a73-0000f81ef32e": // PNG
158                                 case "557cf405-1a04-11d3-9a73-0000f81ef32e": // TIF,TIFF
159                                         break;
160                                 default:
161                                         Assert.Ignore ("Unknown encoder " + guid);
162                                         break;
163                                 }
164                         }
165                 }
166
167                 [Test]
168                 public void BMPCodec()
169                 {
170                         Guid g = new Guid ("557cf400-1a04-11d3-9a73-0000f81ef32e");
171                         Check (GetEncoder (g), GetDecoder (g), ImageFormat.Bmp.Guid,
172                                 "BMP", null, "*.BMP",
173                                 ImageCodecFlags.Builtin | ImageCodecFlags.Encoder | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
174                                 "BMP", "image/bmp", 1, 1, "FF-FF", "42-4D", null);
175                 }
176
177                 [Test]
178                 public void GifCodec()
179                 {
180                         Guid g = new Guid ("557cf402-1a04-11d3-9a73-0000f81ef32e");
181                         Check (GetEncoder (g), GetDecoder (g), ImageFormat.Gif.Guid,
182                                 "GIF", null, "*.GIF",
183                                 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
184                                 "GIF", "image/gif", 1, 2, "FF-FF-FF-FF-FF-FF", "47-49-46-38-39-61", "47-49-46-38-37-61");
185                 }
186                 
187                 [Test]
188                 public void JpegCodec()
189                 {
190                         Guid g = new Guid ("557cf401-1a04-11d3-9a73-0000f81ef32e");
191                         Check (GetEncoder (g), GetDecoder (g), ImageFormat.Jpeg.Guid,
192                                 "JPEG", null, "*.JPG",
193                                 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
194                                 "JPEG", "image/jpeg", 1, 1, "FF-FF", "FF-D8", null);
195                 }
196
197                 [Test]
198                 public void PngCodec()
199                 {
200                         Guid g = new Guid ("557cf406-1a04-11d3-9a73-0000f81ef32e");
201                         Check (GetEncoder (g), GetDecoder (g), ImageFormat.Png.Guid,
202                                 "PNG", null, "*.PNG",
203                                 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
204                                 "PNG", "image/png", 1, 1, "FF-FF-FF-FF-FF-FF-FF-FF", "89-50-4E-47-0D-0A-1A-0A", null);
205                 }
206
207                 [Test]
208                 public void TiffCodec ()
209                 {
210                         Guid g = new Guid ("557cf405-1a04-11d3-9a73-0000f81ef32e");
211                         Check (GetEncoder (g), GetDecoder (g), ImageFormat.Tiff.Guid,
212                                 "TIFF", null, "*.TIF;*.TIFF",
213                                 ImageCodecFlags.Builtin | ImageCodecFlags.Encoder | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
214                                 "TIFF", "image/tiff", 1, 2, "FF-FF", "49-49", "4D-4D");
215                 }
216
217                 [Test]
218                 public void IconCodec_Encoder ()
219                 {
220                         Guid g = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
221                         Assert.IsNull (GetEncoder (g), "Encoder");
222                 }
223
224                 [Test]
225                 public void IconCodec_Decoder ()
226                 {
227                         Guid g = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
228                         Check (null, GetDecoder (g), ImageFormat.Icon.Guid,
229                                 "ICO", null, "*.ICO",
230                                 ImageCodecFlags.Builtin | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
231                                 "ICO", "image/x-icon", 1, 1, "FF-FF-FF-FF", "00-00-01-00", null);
232                 }
233
234                 [Test]
235                 public void EmfCodec_Encoder ()
236                 {
237                         Guid g = new Guid ("557cf403-1a04-11d3-9a73-0000f81ef32e");
238                         Assert.IsNull (GetEncoder (g), "Encoder");
239                 }
240
241                 [Test]
242                 public void EmfCodec_Decoder ()
243                 {
244                         Guid g = new Guid ("557cf403-1a04-11d3-9a73-0000f81ef32e");
245                         Check (null, GetDecoder (g), ImageFormat.Emf.Guid,
246                                 "EMF", null, "*.EMF",
247                                 ImageCodecFlags.Builtin | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
248                                 "EMF", "image/x-emf", 1, 1, "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-FF-FF-FF-FF",
249                                 "00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-20-45-4D-46", null);
250                 }
251
252                 [Test]
253                 public void WmfCodec_Encoder ()
254                 {
255                         Guid g = new Guid ("557cf404-1a04-11d3-9a73-0000f81ef32e");
256                         Assert.IsNull (GetEncoder (g), "Encoder");
257                 }
258
259                 [Test]
260                 public void WmfCodec_Decoder ()
261                 {
262                         Guid g = new Guid ("557cf404-1a04-11d3-9a73-0000f81ef32e");
263                         Check (null, GetDecoder (g), ImageFormat.Wmf.Guid,
264                                 "WMF", null, "*.WMF",
265                                 ImageCodecFlags.Builtin | ImageCodecFlags.Decoder | ImageCodecFlags.SupportBitmap,
266                                 "WMF", "image/x-wmf", 1, 1, "FF-FF-FF-FF", "D7-CD-C6-9A", null);
267                 }
268         }
269 }