TestImageCodecInfo.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // ImageCodecInfo class testing unit
  3. //
  4. // Author:
  5. // Jordi Mas i Hernàndez ([email protected])
  6. //
  7. // (C) 2004 Ximian, Inc. http://www.ximian.com
  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. using System;
  30. using System.Drawing;
  31. using System.Drawing.Imaging;
  32. using NUnit.Framework;
  33. using System.Collections;
  34. using System.Security.Permissions;
  35. using System.Text.RegularExpressions;
  36. namespace MonoTests.System.Drawing.Imaging {
  37. [TestFixture]
  38. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  39. public class ImageCodecInfoTest {
  40. Hashtable decoders;
  41. Hashtable encoders;
  42. ImageCodecInfo GetEncoder (Guid clsid)
  43. {
  44. return (ImageCodecInfo) encoders [clsid];
  45. }
  46. ImageCodecInfo GetDecoder (Guid clsid) {
  47. return (ImageCodecInfo) decoders [clsid];
  48. }
  49. [TestFixtureSetUp]
  50. public void FixtureGetReady()
  51. {
  52. ImageCodecInfo [] arrEnc = ImageCodecInfo.GetImageDecoders ();
  53. ImageCodecInfo [] arrDec = ImageCodecInfo.GetImageEncoders ();
  54. decoders = new Hashtable ();
  55. encoders = new Hashtable ();
  56. foreach (ImageCodecInfo decoder in arrDec)
  57. decoders[decoder.Clsid] = decoder;
  58. foreach (ImageCodecInfo encoder in arrEnc)
  59. encoders[encoder.Clsid] = encoder;
  60. }
  61. static void Check (ImageCodecInfo e, ImageCodecInfo d, Guid FormatID, string CodecName, string DllName,
  62. string FilenameExtension, ImageCodecFlags Flags, string FormatDescription,
  63. string MimeType, int Version)
  64. {
  65. Regex extRegex = new Regex (@"^(\*\.\w+(;(\*\.\w+))*;)?"+
  66. Regex.Escape (FilenameExtension)+@"(;\*\.\w+(;(\*\.\w+))*)?$",
  67. RegexOptions.IgnoreCase | RegexOptions.Singleline);
  68. if (e != null) {
  69. Assert.AreEqual (FormatID, e.FormatID, "Encoder.FormatID");
  70. Assert.IsTrue (e.CodecName.IndexOf (CodecName)>=0,
  71. "Encoder.CodecName contains "+CodecName);
  72. Assert.AreEqual (DllName, e.DllName, "Encoder.DllName");
  73. Assert.IsTrue (extRegex.IsMatch (e.FilenameExtension),
  74. "Encoder.FilenameExtension is a right list with "+FilenameExtension);
  75. Assert.AreEqual (Flags, e.Flags, "Encoder.Flags");
  76. Assert.IsTrue (e.FormatDescription.IndexOf (FormatDescription)>=0,
  77. "Encoder.FormatDescription contains "+FormatDescription);
  78. Assert.IsTrue (e.MimeType.IndexOf (MimeType)>=0,
  79. "Encoder.MimeType contains "+MimeType);
  80. }
  81. if (d != null) {
  82. Assert.AreEqual (FormatID, d.FormatID, "Decoder.FormatID");
  83. Assert.IsTrue (d.CodecName.IndexOf (CodecName)>=0,
  84. "Decoder.CodecName contains "+CodecName);
  85. Assert.AreEqual (DllName, d.DllName, "Decoder.DllName");
  86. Assert.IsTrue (extRegex.IsMatch (d.FilenameExtension),
  87. "Decoder.FilenameExtension is a right list with "+FilenameExtension);
  88. Assert.AreEqual (Flags, d.Flags, "Decoder.Flags");
  89. Assert.IsTrue (d.FormatDescription.IndexOf (FormatDescription)>=0,
  90. "Decoder.FormatDescription contains "+FormatDescription);
  91. Assert.IsTrue (d.MimeType.IndexOf (MimeType)>=0,
  92. "Decoder.MimeType contains "+MimeType);
  93. }
  94. /*
  95. if (SignatureMasks == null) {
  96. Assert.AreEqual (null, e.SignatureMasks, "Encoder.SignatureMasks");
  97. Assert.AreEqual (null, d.SignatureMasks, "Decoder.SignatureMasks");
  98. }
  99. else {
  100. Assert.AreEqual (SignatureMasks.Length, e.SignatureMasks.Length, "Encoder.SignatureMasks.Length");
  101. Assert.AreEqual (SignatureMasks.Length, d.SignatureMasks.Length, "Decoder.SignatureMasks.Length");
  102. for (int i = 0; i < SignatureMasks.Length; i++) {
  103. Assert.AreEqual (SignatureMasks[i].Length, e.SignatureMasks[i].Length,
  104. "Encoder.SignatureMasks["+i.ToString ()+"].Length");
  105. Assert.AreEqual (SignatureMasks[i].Length, d.SignatureMasks[i].Length,
  106. "Decoder.SignatureMasks["+i.ToString ()+"].Length");
  107. for (int j = 0; j < SignatureMasks[i].Length; j++) {
  108. Assert.AreEqual (SignatureMasks[i][j], e.SignatureMasks[i][j],
  109. "Encoder.SignatureMasks["+i.ToString ()+"]["+j.ToString ()+"]");
  110. Assert.AreEqual (SignatureMasks[i][j], d.SignatureMasks[i][j],
  111. "Decoder.SignatureMasks["+i.ToString ()+"]["+j.ToString ()+"]");
  112. }
  113. }
  114. }
  115. */
  116. }
  117. [Test]
  118. #if TARGET_JVM
  119. [Category ("NotWorking")]
  120. #endif
  121. public void BMPCodec()
  122. {
  123. Guid g = new Guid ("557cf400-1a04-11d3-9a73-0000f81ef32e");
  124. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Bmp.Guid,
  125. "BMP", null, "*.BMP",
  126. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  127. "BMP", "image/bmp", 1);
  128. }
  129. [Test]
  130. #if TARGET_JVM
  131. [Category ("NotWorking")]
  132. #endif
  133. public void GifCodec()
  134. {
  135. Guid g = new Guid ("557cf402-1a04-11d3-9a73-0000f81ef32e");
  136. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Gif.Guid,
  137. "GIF", null, "*.GIF",
  138. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  139. "GIF", "image/gif", 1);
  140. }
  141. [Test]
  142. #if TARGET_JVM
  143. [Category ("NotWorking")]
  144. #endif
  145. public void JpegCodec()
  146. {
  147. Guid g = new Guid ("557cf401-1a04-11d3-9a73-0000f81ef32e");
  148. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Jpeg.Guid,
  149. "JPEG", null, "*.JPG",
  150. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  151. "JPEG", "image/jpeg", 1);
  152. }
  153. [Test]
  154. #if TARGET_JVM
  155. [Category ("NotWorking")]
  156. #endif
  157. public void PngCodec()
  158. {
  159. Guid g = new Guid ("557cf406-1a04-11d3-9a73-0000f81ef32e");
  160. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Png.Guid,
  161. "PNG", null, "*.PNG",
  162. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  163. "PNG", "image/png", 1);
  164. }
  165. [Test]
  166. public void IconCodec() {
  167. Guid g = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
  168. Check (null, GetDecoder (g), ImageFormat.Bmp.Guid,
  169. "ICO", null, "*.ICO",
  170. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.SupportBitmap,
  171. "ICO", "image/x-icon", 1);
  172. }
  173. }
  174. }