TestImageCodecInfo.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.Text.RegularExpressions;
  38. namespace MonoTests.System.Drawing
  39. {
  40. [TestFixture]
  41. public class TestImageCodecInfo
  42. {
  43. Hashtable decoders;
  44. Hashtable encoders;
  45. ImageCodecInfo GetEncoder (Guid clsid)
  46. {
  47. return (ImageCodecInfo) encoders [clsid];
  48. }
  49. ImageCodecInfo GetDecoder (Guid clsid) {
  50. return (ImageCodecInfo) decoders [clsid];
  51. }
  52. [TestFixtureSetUp]
  53. public void FixtureGetReady()
  54. {
  55. ImageCodecInfo [] arrEnc = ImageCodecInfo.GetImageDecoders ();
  56. ImageCodecInfo [] arrDec = ImageCodecInfo.GetImageEncoders ();
  57. decoders = new Hashtable ();
  58. encoders = new Hashtable ();
  59. foreach (ImageCodecInfo decoder in arrDec)
  60. decoders[decoder.Clsid] = decoder;
  61. foreach (ImageCodecInfo encoder in arrEnc)
  62. encoders[encoder.Clsid] = encoder;
  63. }
  64. static void Check (ImageCodecInfo e, ImageCodecInfo d, Guid FormatID, string CodecName, string DllName,
  65. string FilenameExtension, ImageCodecFlags Flags, string FormatDescription,
  66. string MimeType, int Version)
  67. {
  68. Regex extRegex = new Regex (@"^(\*\.\w+(;(\*\.\w+))*;)?"+
  69. Regex.Escape (FilenameExtension)+@"(;\*\.\w+(;(\*\.\w+))*)?$",
  70. RegexOptions.IgnoreCase | RegexOptions.Singleline);
  71. if (e != null) {
  72. Assert.AreEqual (FormatID, e.FormatID, "Encoder.FormatID");
  73. Assert.IsTrue (e.CodecName.IndexOf (CodecName)>=0,
  74. "Encoder.CodecName contains "+CodecName);
  75. Assert.AreEqual (DllName, e.DllName, "Encoder.DllName");
  76. Assert.IsTrue (extRegex.IsMatch (e.FilenameExtension),
  77. "Encoder.FilenameExtension is a right list with "+FilenameExtension);
  78. Assert.AreEqual (Flags, e.Flags, "Encoder.Flags");
  79. Assert.IsTrue (e.FormatDescription.IndexOf (FormatDescription)>=0,
  80. "Encoder.FormatDescription contains "+FormatDescription);
  81. Assert.IsTrue (e.MimeType.IndexOf (MimeType)>=0,
  82. "Encoder.MimeType contains "+MimeType);
  83. }
  84. if (d != null) {
  85. Assert.AreEqual (FormatID, d.FormatID, "Decoder.FormatID");
  86. Assert.IsTrue (d.CodecName.IndexOf (CodecName)>=0,
  87. "Decoder.CodecName contains "+CodecName);
  88. Assert.AreEqual (DllName, d.DllName, "Decoder.DllName");
  89. Assert.IsTrue (extRegex.IsMatch (d.FilenameExtension),
  90. "Decoder.FilenameExtension is a right list with "+FilenameExtension);
  91. Assert.AreEqual (Flags, d.Flags, "Decoder.Flags");
  92. Assert.IsTrue (d.FormatDescription.IndexOf (FormatDescription)>=0,
  93. "Decoder.FormatDescription contains "+FormatDescription);
  94. Assert.IsTrue (d.MimeType.IndexOf (MimeType)>=0,
  95. "Decoder.MimeType contains "+MimeType);
  96. }
  97. /*
  98. if (SignatureMasks == null) {
  99. Assert.AreEqual (null, e.SignatureMasks, "Encoder.SignatureMasks");
  100. Assert.AreEqual (null, d.SignatureMasks, "Decoder.SignatureMasks");
  101. }
  102. else {
  103. Assert.AreEqual (SignatureMasks.Length, e.SignatureMasks.Length, "Encoder.SignatureMasks.Length");
  104. Assert.AreEqual (SignatureMasks.Length, d.SignatureMasks.Length, "Decoder.SignatureMasks.Length");
  105. for (int i = 0; i < SignatureMasks.Length; i++) {
  106. Assert.AreEqual (SignatureMasks[i].Length, e.SignatureMasks[i].Length,
  107. "Encoder.SignatureMasks["+i.ToString ()+"].Length");
  108. Assert.AreEqual (SignatureMasks[i].Length, d.SignatureMasks[i].Length,
  109. "Decoder.SignatureMasks["+i.ToString ()+"].Length");
  110. for (int j = 0; j < SignatureMasks[i].Length; j++) {
  111. Assert.AreEqual (SignatureMasks[i][j], e.SignatureMasks[i][j],
  112. "Encoder.SignatureMasks["+i.ToString ()+"]["+j.ToString ()+"]");
  113. Assert.AreEqual (SignatureMasks[i][j], d.SignatureMasks[i][j],
  114. "Decoder.SignatureMasks["+i.ToString ()+"]["+j.ToString ()+"]");
  115. }
  116. }
  117. }
  118. */
  119. }
  120. [Test]
  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. public void GifCodec()
  131. {
  132. Guid g = new Guid ("557cf402-1a04-11d3-9a73-0000f81ef32e");
  133. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Gif.Guid,
  134. "GIF", null, "*.GIF",
  135. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  136. "GIF", "image/gif", 1);
  137. }
  138. [Test]
  139. public void JpegCodec()
  140. {
  141. Guid g = new Guid ("557cf401-1a04-11d3-9a73-0000f81ef32e");
  142. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Jpeg.Guid,
  143. "JPEG", null, "*.JPG",
  144. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  145. "JPEG", "image/jpeg", 1);
  146. }
  147. [Test]
  148. public void PngCodec()
  149. {
  150. Guid g = new Guid ("557cf406-1a04-11d3-9a73-0000f81ef32e");
  151. Check (GetEncoder (g), GetDecoder (g), ImageFormat.Png.Guid,
  152. "PNG", null, "*.PNG",
  153. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.Decoder|ImageCodecFlags.SupportBitmap,
  154. "PNG", "image/png", 1);
  155. }
  156. [Test]
  157. public void IconCodec() {
  158. Guid g = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
  159. Check (null, GetDecoder (g), ImageFormat.Bmp.Guid,
  160. "ICO", null, "*.ICO",
  161. ImageCodecFlags.Builtin|ImageCodecFlags.Encoder|ImageCodecFlags.SupportBitmap,
  162. "ICO", "image/x-icon", 1);
  163. }
  164. }
  165. }