TestImageCodecInfo.cs 6.8 KB

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