merge -r 58060:58217
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Imaging / TestImageCodecInfo.cs
1 //
2 // ImageCodecInfo class testing unit
3 //
4 // Author:
5 //
6 //       Jordi Mas i Hernàndez (jordi@ximian.com)
7 //
8 // (C) 2004 Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 using System;
34 using System.Drawing;
35 using System.Drawing.Imaging;
36 using NUnit.Framework;
37 using System.Collections;
38 using System.Security.Permissions;
39 using System.Text.RegularExpressions;
40
41 namespace MonoTests.System.Drawing
42 {
43
44         [TestFixture]
45         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
46         public class TestImageCodecInfo 
47         {
48                 Hashtable decoders;
49                 Hashtable encoders;
50
51                 ImageCodecInfo GetEncoder (Guid clsid)
52                 {
53                         return (ImageCodecInfo) encoders [clsid];
54                 }
55
56                 ImageCodecInfo GetDecoder (Guid clsid) {
57                         return (ImageCodecInfo) decoders [clsid];
58                 }
59
60                 [TestFixtureSetUp]
61                 public void FixtureGetReady()           
62                 {
63                         ImageCodecInfo [] arrEnc  = ImageCodecInfo.GetImageDecoders ();
64                         ImageCodecInfo [] arrDec = ImageCodecInfo.GetImageEncoders ();
65                         decoders = new Hashtable ();
66                         encoders = new Hashtable ();
67
68                         foreach (ImageCodecInfo decoder in arrDec)
69                                 decoders[decoder.Clsid] = decoder;
70                 
71                         foreach (ImageCodecInfo encoder in arrEnc)
72                                 encoders[encoder.Clsid] = encoder;
73                 }
74
75                 static void Check (ImageCodecInfo e, ImageCodecInfo d, Guid FormatID, string CodecName, string DllName,
76                         string FilenameExtension, ImageCodecFlags Flags, string FormatDescription,
77                         string MimeType, int Version)
78                 {
79                         Regex extRegex = new Regex (@"^(\*\.\w+(;(\*\.\w+))*;)?"+
80                                 Regex.Escape (FilenameExtension)+@"(;\*\.\w+(;(\*\.\w+))*)?$",
81                                 RegexOptions.IgnoreCase | RegexOptions.Singleline);
82
83                         if (e != null) {
84                                 Assert.AreEqual (FormatID, e.FormatID, "Encoder.FormatID");
85                                 Assert.IsTrue (e.CodecName.IndexOf (CodecName)>=0,
86                                         "Encoder.CodecName contains "+CodecName);
87                                 Assert.AreEqual (DllName, e.DllName, "Encoder.DllName");
88                                 Assert.IsTrue (extRegex.IsMatch (e.FilenameExtension),
89                                         "Encoder.FilenameExtension is a right list with "+FilenameExtension);
90                                 Assert.AreEqual (Flags, e.Flags, "Encoder.Flags");
91                                 Assert.IsTrue (e.FormatDescription.IndexOf (FormatDescription)>=0,
92                                         "Encoder.FormatDescription contains "+FormatDescription);
93                                 Assert.IsTrue (e.MimeType.IndexOf (MimeType)>=0,
94                                         "Encoder.MimeType contains "+MimeType);
95                         }
96                         if (d != null) {
97                                 Assert.AreEqual (FormatID, d.FormatID, "Decoder.FormatID");
98                                 Assert.IsTrue (d.CodecName.IndexOf (CodecName)>=0,
99                                         "Decoder.CodecName contains "+CodecName);
100                                 Assert.AreEqual (DllName, d.DllName, "Decoder.DllName");
101                                 Assert.IsTrue (extRegex.IsMatch (d.FilenameExtension),
102                                         "Decoder.FilenameExtension is a right list with "+FilenameExtension);
103                                 Assert.AreEqual (Flags, d.Flags, "Decoder.Flags");
104                                 Assert.IsTrue (d.FormatDescription.IndexOf (FormatDescription)>=0,
105                                         "Decoder.FormatDescription contains "+FormatDescription);
106                                 Assert.IsTrue (d.MimeType.IndexOf (MimeType)>=0,
107                                         "Decoder.MimeType contains "+MimeType);
108                         }
109                         /*
110                         if (SignatureMasks == null) {
111                                 Assert.AreEqual (null, e.SignatureMasks, "Encoder.SignatureMasks");
112                                 Assert.AreEqual (null, d.SignatureMasks, "Decoder.SignatureMasks");
113                         }
114                         else {
115                                 Assert.AreEqual (SignatureMasks.Length, e.SignatureMasks.Length, "Encoder.SignatureMasks.Length");
116                                 Assert.AreEqual (SignatureMasks.Length, d.SignatureMasks.Length, "Decoder.SignatureMasks.Length");
117                                 for (int i = 0; i < SignatureMasks.Length; i++) {
118                                         Assert.AreEqual (SignatureMasks[i].Length, e.SignatureMasks[i].Length,
119                                                 "Encoder.SignatureMasks["+i.ToString ()+"].Length");
120                                         Assert.AreEqual (SignatureMasks[i].Length, d.SignatureMasks[i].Length,
121                                                 "Decoder.SignatureMasks["+i.ToString ()+"].Length");
122                                         for (int j = 0; j < SignatureMasks[i].Length; j++) {
123                                                 Assert.AreEqual (SignatureMasks[i][j], e.SignatureMasks[i][j],
124                                                         "Encoder.SignatureMasks["+i.ToString ()+"]["+j.ToString ()+"]");
125                                                 Assert.AreEqual (SignatureMasks[i][j], d.SignatureMasks[i][j],
126                                                         "Decoder.SignatureMasks["+i.ToString ()+"]["+j.ToString ()+"]");
127                                         }
128                                 }
129                         }
130                         */
131                         
132                 }
133
134                 [Test]
135                 public void BMPCodec()
136                 {
137                         Guid g = new Guid ("557cf400-1a04-11d3-9a73-0000f81ef32e");
138                         Check (GetEncoder (g), GetDecoder (g), ImageFormat.Bmp.Guid,
139                                 "BMP", null, "*.BMP",
140                                 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
141                                 "BMP", "image/bmp", 1);
142                 }
143
144                 [Test]
145                 public void GifCodec()
146                 {
147                         Guid g = new Guid ("557cf402-1a04-11d3-9a73-0000f81ef32e");
148                         Check (GetEncoder (g), GetDecoder (g), ImageFormat.Gif.Guid,
149                                 "GIF", null, "*.GIF",
150                                 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
151                                 "GIF", "image/gif", 1);
152                 }
153                 
154                 [Test]
155                 public void JpegCodec()
156                 {
157                         Guid g = new Guid ("557cf401-1a04-11d3-9a73-0000f81ef32e");
158                         Check (GetEncoder (g), GetDecoder (g), ImageFormat.Jpeg.Guid,
159                                 "JPEG", null, "*.JPG",
160                                 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
161                                 "JPEG", "image/jpeg", 1);
162                 }
163
164                 [Test]
165                 public void PngCodec()
166                 {
167                         Guid g = new Guid ("557cf406-1a04-11d3-9a73-0000f81ef32e");
168                         Check (GetEncoder (g), GetDecoder (g), ImageFormat.Png.Guid,
169                                 "PNG", null, "*.PNG",
170                                 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
171                                 "PNG", "image/png", 1);
172                 }
173                 [Test]
174                 public void IconCodec() {
175                         Guid g = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
176                         Check (null, GetDecoder (g), ImageFormat.Bmp.Guid,
177                                 "ICO", null, "*.ICO",
178                                 ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.SupportBitmap,
179                                 "ICO", "image/x-icon", 1);
180                 }
181
182         }
183 }