MetafileTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //
  2. // Metafile class unit tests
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Drawing;
  30. using System.Drawing.Imaging;
  31. using System.IO;
  32. using System.Runtime.InteropServices;
  33. using System.Security.Permissions;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Drawing.Imaging {
  36. [TestFixture]
  37. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  38. public class MetafileTest {
  39. public const string Bitmap = "bitmaps/non-inverted.bmp";
  40. public const string WmfPlaceable = "bitmaps/telescope_01.wmf";
  41. public const string Emf = "bitmaps/milkmateya01.emf";
  42. // Get the input directory depending on the runtime
  43. static public string getInFile (string file)
  44. {
  45. string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
  46. if (!File.Exists (sRslt))
  47. sRslt = "Test/System.Drawing/" + file;
  48. return sRslt;
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentException))]
  52. public void Metafile_Stream_Null ()
  53. {
  54. new Metafile ((Stream)null);
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentNullException))]
  58. public void Metafile_String_Null ()
  59. {
  60. new Metafile ((string) null);
  61. }
  62. [Test]
  63. [ExpectedException (typeof (ArgumentException))]
  64. public void Metafile_String_Empty ()
  65. {
  66. new Metafile (String.Empty);
  67. }
  68. [Test]
  69. [ExpectedException (typeof (ExternalException))]
  70. public void Metafile_String_FileDoesNotExists ()
  71. {
  72. string filename = getInFile ("telescope_02.wmf");
  73. new Metafile (filename);
  74. }
  75. [Test]
  76. public void Metafile_String ()
  77. {
  78. string filename = getInFile (WmfPlaceable);
  79. Metafile mf = new Metafile (filename);
  80. Metafile clone = (Metafile) mf.Clone ();
  81. }
  82. [Test]
  83. [ExpectedException (typeof (ExternalException))]
  84. public void GetMetafileHeader_Bitmap ()
  85. {
  86. new Metafile (getInFile (Bitmap));
  87. }
  88. static public void Check_MetaHeader_WmfPlaceable (MetaHeader mh)
  89. {
  90. Assert.AreEqual (9, mh.HeaderSize, "HeaderSize");
  91. Assert.AreEqual (98, mh.MaxRecord, "MaxRecord");
  92. Assert.AreEqual (3, mh.NoObjects, "NoObjects");
  93. Assert.AreEqual (0, mh.NoParameters, "NoParameters");
  94. Assert.AreEqual (1737, mh.Size, "Size");
  95. Assert.AreEqual (1, mh.Type, "Type");
  96. Assert.AreEqual (0x300, mh.Version, "Version");
  97. }
  98. public static void Check_MetafileHeader_WmfPlaceable (MetafileHeader header)
  99. {
  100. Assert.AreEqual (MetafileType.WmfPlaceable, header.Type, "Type");
  101. Assert.AreEqual (0x300, header.Version, "Version");
  102. // filesize - 22, which happens to be the size (22) of a PLACEABLEMETAHEADER struct
  103. Assert.AreEqual (3474, header.MetafileSize, "MetafileSize");
  104. Assert.AreEqual (-30, header.Bounds.X, "Bounds.X");
  105. Assert.AreEqual (-40, header.Bounds.Y, "Bounds.Y");
  106. Assert.AreEqual (3096, header.Bounds.Width, "Bounds.Width");
  107. Assert.AreEqual (4127, header.Bounds.Height, "Bounds.Height");
  108. Assert.AreEqual (606, header.DpiX, "DpiX");
  109. Assert.AreEqual (606, header.DpiY, "DpiY");
  110. Assert.AreEqual (0, header.EmfPlusHeaderSize, "EmfPlusHeaderSize");
  111. Assert.AreEqual (0, header.LogicalDpiX, "LogicalDpiX");
  112. Assert.AreEqual (0, header.LogicalDpiY, "LogicalDpiY");
  113. Assert.IsNotNull (header.WmfHeader, "WmfHeader");
  114. Check_MetaHeader_WmfPlaceable (header.WmfHeader);
  115. Assert.IsFalse (header.IsDisplay (), "IsDisplay");
  116. Assert.IsFalse (header.IsEmf (), "IsEmf");
  117. Assert.IsFalse (header.IsEmfOrEmfPlus (), "IsEmfOrEmfPlus");
  118. Assert.IsFalse (header.IsEmfPlus (), "IsEmfPlus");
  119. Assert.IsFalse (header.IsEmfPlusDual (), "IsEmfPlusDual");
  120. Assert.IsFalse (header.IsEmfPlusOnly (), "IsEmfPlusOnly");
  121. Assert.IsTrue (header.IsWmf (), "IsWmf");
  122. Assert.IsTrue (header.IsWmfPlaceable (), "IsWmfPlaceable");
  123. }
  124. [Test]
  125. public void GetMetafileHeader_WmfPlaceable ()
  126. {
  127. using (Metafile mf = new Metafile (getInFile (WmfPlaceable))) {
  128. MetafileHeader header1 = mf.GetMetafileHeader ();
  129. Check_MetafileHeader_WmfPlaceable (header1);
  130. MetafileHeader header2 = mf.GetMetafileHeader ();
  131. Assert.IsFalse (Object.ReferenceEquals (header1, header2), "Same object");
  132. }
  133. }
  134. [Test]
  135. public void GetMetafileHeader_FromFile_WmfPlaceable ()
  136. {
  137. using (Metafile mf = new Metafile (getInFile (WmfPlaceable))) {
  138. MetafileHeader header1 = mf.GetMetafileHeader ();
  139. Check_MetafileHeader_WmfPlaceable (header1);
  140. MetaHeader mh1 = header1.WmfHeader;
  141. Check_MetaHeader_WmfPlaceable (mh1);
  142. MetaHeader mh2 = mf.GetMetafileHeader ().WmfHeader;
  143. Assert.IsFalse (Object.ReferenceEquals (mh1, mh2), "Same object");
  144. }
  145. }
  146. [Test]
  147. public void GetMetafileHeader_FromFileStream_WmfPlaceable ()
  148. {
  149. using (FileStream fs = File.OpenRead (getInFile (WmfPlaceable))) {
  150. using (Metafile mf = new Metafile (fs)) {
  151. MetafileHeader header1 = mf.GetMetafileHeader ();
  152. Check_MetafileHeader_WmfPlaceable (header1);
  153. MetaHeader mh1 = header1.WmfHeader;
  154. Check_MetaHeader_WmfPlaceable (mh1);
  155. MetaHeader mh2 = mf.GetMetafileHeader ().WmfHeader;
  156. Assert.IsFalse (Object.ReferenceEquals (mh1, mh2), "Same object");
  157. }
  158. }
  159. }
  160. [Test]
  161. public void GetMetafileHeader_FromMemoryStream_WmfPlaceable ()
  162. {
  163. MemoryStream ms;
  164. string filename = getInFile (WmfPlaceable);
  165. using (FileStream fs = File.OpenRead (filename)) {
  166. byte[] data = new byte[fs.Length];
  167. fs.Read (data, 0, data.Length);
  168. ms = new MemoryStream (data);
  169. }
  170. using (Metafile mf = new Metafile (ms)) {
  171. MetafileHeader header1 = mf.GetMetafileHeader ();
  172. Check_MetafileHeader_WmfPlaceable (header1);
  173. MetaHeader mh1 = header1.WmfHeader;
  174. Check_MetaHeader_WmfPlaceable (mh1);
  175. MetaHeader mh2 = mf.GetMetafileHeader ().WmfHeader;
  176. Assert.IsFalse (Object.ReferenceEquals (mh1, mh2), "Same object");
  177. }
  178. ms.Close ();
  179. }
  180. public static void Check_MetafileHeader_Emf (MetafileHeader header)
  181. {
  182. Assert.AreEqual (MetafileType.Emf, header.Type, "Type");
  183. Assert.AreEqual (65536, header.Version, "Version");
  184. // extactly the filesize
  185. Assert.AreEqual (20456, header.MetafileSize, "MetafileSize");
  186. Assert.AreEqual (0, header.Bounds.X, "Bounds.X");
  187. Assert.AreEqual (0, header.Bounds.Y, "Bounds.Y");
  188. #if false
  189. Assert.AreEqual (759, header.Bounds.Width, "Bounds.Width");
  190. Assert.AreEqual (1073, header.Bounds.Height, "Bounds.Height");
  191. Assert.AreEqual (96f, header.DpiX, 0.5f, "DpiX");
  192. Assert.AreEqual (96f, header.DpiY, 0.5f, "DpiY");
  193. Assert.AreEqual (6619188, header.EmfPlusHeaderSize, "EmfPlusHeaderSize");
  194. Assert.AreEqual (3670064, header.LogicalDpiX, "LogicalDpiX");
  195. Assert.AreEqual (3670064, header.LogicalDpiY, "LogicalDpiY");
  196. #endif
  197. try {
  198. Assert.IsNotNull (header.WmfHeader, "WmfHeader");
  199. Assert.Fail ("WmfHeader didn't throw an ArgumentException");
  200. }
  201. catch (ArgumentException) {
  202. }
  203. catch (Exception e) {
  204. Assert.Fail ("WmfHeader didn't throw an ArgumentException but: {0}.", e.ToString ());
  205. }
  206. Assert.IsFalse (header.IsDisplay (), "IsDisplay");
  207. Assert.IsTrue (header.IsEmf (), "IsEmf");
  208. Assert.IsTrue (header.IsEmfOrEmfPlus (), "IsEmfOrEmfPlus");
  209. Assert.IsFalse (header.IsEmfPlus (), "IsEmfPlus");
  210. Assert.IsFalse (header.IsEmfPlusDual (), "IsEmfPlusDual");
  211. Assert.IsFalse (header.IsEmfPlusOnly (), "IsEmfPlusOnly");
  212. Assert.IsFalse (header.IsWmf (), "IsWmf");
  213. Assert.IsFalse (header.IsWmfPlaceable (), "IsWmfPlaceable");
  214. }
  215. [Test]
  216. public void GetMetafileHeader_FromFile_Emf ()
  217. {
  218. using (Metafile mf = new Metafile (getInFile (Emf))) {
  219. MetafileHeader header1 = mf.GetMetafileHeader ();
  220. Check_MetafileHeader_Emf (header1);
  221. }
  222. }
  223. [Test]
  224. public void GetMetafileHeader_FromFileStream_Emf ()
  225. {
  226. using (FileStream fs = File.OpenRead (getInFile (Emf))) {
  227. using (Metafile mf = new Metafile (fs)) {
  228. MetafileHeader header1 = mf.GetMetafileHeader ();
  229. Check_MetafileHeader_Emf (header1);
  230. }
  231. }
  232. }
  233. [Test]
  234. public void GetMetafileHeader_FromMemoryStream_Emf ()
  235. {
  236. MemoryStream ms;
  237. string filename = getInFile (Emf);
  238. using (FileStream fs = File.OpenRead (filename)) {
  239. byte[] data = new byte[fs.Length];
  240. fs.Read (data, 0, data.Length);
  241. ms = new MemoryStream (data);
  242. }
  243. using (Metafile mf = new Metafile (ms)) {
  244. MetafileHeader header1 = mf.GetMetafileHeader ();
  245. Check_MetafileHeader_Emf (header1);
  246. }
  247. ms.Close ();
  248. }
  249. [Test]
  250. [ExpectedException (typeof (NullReferenceException))]
  251. public void Static_GetMetafileHeader_Stream_Null ()
  252. {
  253. MetafileHeader header = Metafile.GetMetafileHeader ((Stream)null);
  254. }
  255. [Test]
  256. public void Static_GetMetafileHeader_Stream ()
  257. {
  258. string filename = getInFile (WmfPlaceable);
  259. using (FileStream fs = File.OpenRead (filename)) {
  260. MetafileHeader header = Metafile.GetMetafileHeader (fs);
  261. Check_MetafileHeader_WmfPlaceable (header);
  262. }
  263. }
  264. [Test]
  265. [ExpectedException (typeof (ArgumentNullException))]
  266. public void Static_GetMetafileHeader_Filename_Null ()
  267. {
  268. MetafileHeader header = Metafile.GetMetafileHeader ((string) null);
  269. }
  270. [Test]
  271. public void Static_GetMetafileHeader_Filename ()
  272. {
  273. string filename = getInFile (WmfPlaceable);
  274. MetafileHeader header = Metafile.GetMetafileHeader (filename);
  275. Check_MetafileHeader_WmfPlaceable (header);
  276. }
  277. }
  278. [TestFixture]
  279. public class MetafileFulltrustTest {
  280. [Test]
  281. [ExpectedException (typeof (ArgumentException))]
  282. public void Static_GetMetafileHeader_IntPtr_Zero ()
  283. {
  284. Metafile.GetMetafileHeader (IntPtr.Zero);
  285. }
  286. [Test]
  287. [ExpectedException (typeof (ArgumentException))]
  288. public void Static_GetMetafileHeader_IntPtr ()
  289. {
  290. string filename = MetafileTest.getInFile (MetafileTest.WmfPlaceable);
  291. using (Metafile mf = new Metafile (filename)) {
  292. IntPtr hemf = mf.GetHenhmetafile ();
  293. Assert.IsTrue (hemf != IntPtr.Zero, "GetHenhmetafile");
  294. Metafile.GetMetafileHeader (hemf);
  295. }
  296. }
  297. }
  298. }