TestImage.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. //
  2. // Image class testing unit
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernàndez ([email protected]>
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2005 Ximian, Inc. http://www.ximian.com
  9. // Copyright (C) 2005-2007 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Drawing;
  32. using System.Drawing.Imaging;
  33. using System.IO;
  34. using System.Reflection;
  35. using System.Security.Permissions;
  36. using System.Xml.Serialization;
  37. using NUnit.Framework;
  38. namespace MonoTests.System.Drawing{
  39. [TestFixture]
  40. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  41. public class ImageTest {
  42. private string fname;
  43. private bool callback;
  44. [TestFixtureSetUp]
  45. public void FixtureSetup ()
  46. {
  47. fname = Path.GetTempFileName ();
  48. }
  49. [TestFixtureTearDown]
  50. public void FixtureTearDown ()
  51. {
  52. try {
  53. File.Delete (fname);
  54. }
  55. catch {
  56. }
  57. }
  58. [SetUp]
  59. public void SetUp ()
  60. {
  61. callback = false;
  62. }
  63. [Test]
  64. [ExpectedException (typeof (FileNotFoundException))]
  65. public void FileDoesNotExists ()
  66. {
  67. Image.FromFile ("FileDoesNotExists.jpg");
  68. }
  69. private bool CallbackTrue ()
  70. {
  71. callback = true;
  72. return true;
  73. }
  74. private bool CallbackFalse ()
  75. {
  76. callback = true;
  77. return false;
  78. }
  79. [Test]
  80. public void GetThumbnailImage_NullCallback_Tiff ()
  81. {
  82. using (Bitmap bmp = new Bitmap (10, 10)) {
  83. // according to documentation a callback is mandatory
  84. Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero);
  85. Assert.AreEqual (5, tn.Height, "Height");
  86. Assert.AreEqual (10, tn.Width, "Width");
  87. Assert.IsFalse (callback, "Callback called");
  88. tn.Save (fname, ImageFormat.Tiff);
  89. }
  90. }
  91. [Test]
  92. [ExpectedException (typeof (OutOfMemoryException))]
  93. public void GetThumbnailImage_Height_Zero ()
  94. {
  95. using (Bitmap bmp = new Bitmap (10, 10)) {
  96. Image tn = bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
  97. }
  98. }
  99. [Test]
  100. [ExpectedException (typeof (OutOfMemoryException))]
  101. public void GetThumbnailImage_Width_Negative ()
  102. {
  103. using (Bitmap bmp = new Bitmap (10, 10)) {
  104. Image tn = bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
  105. }
  106. }
  107. [Test]
  108. public void GetThumbnailImage_CallbackData_Invalid ()
  109. {
  110. using (Bitmap bmp = new Bitmap (10, 10)) {
  111. // according to documentation IntPtr.Zero must be supplied as data
  112. Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
  113. Assert.AreEqual (5, tn.Height, "Height");
  114. Assert.AreEqual (5, tn.Width, "Width");
  115. Assert.IsFalse (callback, "Callback called");
  116. tn.Save (fname, ImageFormat.Tiff);
  117. }
  118. }
  119. [Test]
  120. public void GetThumbnailImage_SameSize_Bmp ()
  121. {
  122. using (Bitmap bmp = new Bitmap (10, 10)) {
  123. Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
  124. Assert.AreEqual (10, tn.Height, "Height");
  125. Assert.AreEqual (10, tn.Width, "Width");
  126. Assert.IsFalse (callback, "Callback called");
  127. tn.Save (fname, ImageFormat.Bmp);
  128. }
  129. }
  130. [Test]
  131. public void GetThumbnailImage_Smaller_Gif ()
  132. {
  133. using (Bitmap bmp = new Bitmap (10, 10)) {
  134. Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
  135. Assert.AreEqual (4, tn.Height, "Height");
  136. Assert.AreEqual (4, tn.Width, "Width");
  137. Assert.IsFalse (callback, "Callback called");
  138. tn.Save (fname, ImageFormat.Gif);
  139. }
  140. }
  141. [Test]
  142. public void GetThumbnailImage_Bigger_Png ()
  143. {
  144. using (Bitmap bmp = new Bitmap (10, 10)) {
  145. Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
  146. Assert.AreEqual (40, tn.Height, "Height");
  147. Assert.AreEqual (40, tn.Width, "Width");
  148. Assert.IsFalse (callback, "Callback called");
  149. tn.Save (fname, ImageFormat.Png);
  150. }
  151. }
  152. [Test]
  153. #if !NET_2_0
  154. [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
  155. #endif
  156. public void Stream_Unlocked ()
  157. {
  158. try {
  159. Image img = null;
  160. using (MemoryStream ms = new MemoryStream ()) {
  161. using (Bitmap bmp = new Bitmap (10, 10)) {
  162. bmp.Save (ms, ImageFormat.Png);
  163. }
  164. ms.Position = 0;
  165. img = Image.FromStream (ms);
  166. }
  167. // stream isn't available anymore
  168. ((Bitmap) img).MakeTransparent (Color.Transparent);
  169. }
  170. catch (OutOfMemoryException) {
  171. int p = (int) Environment.OSVersion.Platform;
  172. // libgdiplus (UNIX) doesn't lazy load the image so the
  173. // stream may be freed (and this exception will never occur)
  174. if ((p == 4) || (p == 128) || (p == 6))
  175. throw;
  176. }
  177. }
  178. [Test]
  179. #if !NET_2_0
  180. [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
  181. #endif
  182. public void Stream_Locked ()
  183. {
  184. Image img = null;
  185. using (MemoryStream ms = new MemoryStream ()) {
  186. using (Bitmap bmp = new Bitmap (10, 10)) {
  187. bmp.Save (ms, ImageFormat.Png);
  188. }
  189. ms.Position = 0;
  190. img = Image.FromStream (ms);
  191. // stream is available
  192. ((Bitmap) img).MakeTransparent (Color.Transparent);
  193. }
  194. }
  195. [Test]
  196. #if NET_2_0
  197. [Category ("NotWorking")] // http://bugzilla.ximian.com/show_bug.cgi?id=80558
  198. #else
  199. [ExpectedException (typeof (InvalidOperationException))]
  200. #endif
  201. public void XmlSerialize ()
  202. {
  203. new XmlSerializer (typeof (Image));
  204. }
  205. private void Wmf (Image img)
  206. {
  207. Assert.IsFalse (img is Bitmap, "Bitmap");
  208. Assert.IsTrue (img is Metafile, "Metafile");
  209. // as Image
  210. Assert.AreEqual (327683, img.Flags, "Flags");
  211. Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Wmf), "Wmf");
  212. #if NET_2_0
  213. Assert.IsNull (img.Tag, "Tag");
  214. #endif
  215. }
  216. [Test]
  217. public void FromFile_Metafile_Wmf ()
  218. {
  219. string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
  220. using (Image img = Image.FromFile (filename)) {
  221. Wmf (img);
  222. }
  223. }
  224. [Test]
  225. public void FromStream_Metafile_Wmf ()
  226. {
  227. string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
  228. using (FileStream fs = File.OpenRead (filename)) {
  229. using (Image img = Image.FromStream (fs)) {
  230. Wmf (img);
  231. }
  232. }
  233. }
  234. [Test]
  235. [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
  236. [ExpectedException (typeof (ArgumentException))]
  237. public void FromStream_Metafile_Wmf_NotOrigin ()
  238. {
  239. string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
  240. using (FileStream fs = File.OpenRead (filename)) {
  241. fs.Position = fs.Length / 2;
  242. Image.FromStream (fs);
  243. }
  244. }
  245. private void Emf (Image img)
  246. {
  247. Assert.IsFalse (img is Bitmap, "Bitmap");
  248. Assert.IsTrue (img is Metafile, "Metafile");
  249. // as Image
  250. Assert.AreEqual (327683, img.Flags, "Flags");
  251. Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf");
  252. #if NET_2_0
  253. Assert.IsNull (img.Tag, "Tag");
  254. #endif
  255. }
  256. [Test]
  257. public void FromFile_Metafile_Emf ()
  258. {
  259. string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
  260. using (Image img = Image.FromFile (filename)) {
  261. Emf (img);
  262. }
  263. }
  264. [Test]
  265. public void FromStream_Metafile_Emf ()
  266. {
  267. string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
  268. using (FileStream fs = File.OpenRead (filename)) {
  269. using (Image img = Image.FromStream (fs)) {
  270. Emf (img);
  271. }
  272. }
  273. }
  274. [Test]
  275. [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
  276. [ExpectedException (typeof (ArgumentException))]
  277. public void FromStream_Metafile_Emf_NotOrigin ()
  278. {
  279. string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
  280. using (FileStream fs = File.OpenRead (filename)) {
  281. fs.Position = fs.Length / 2;
  282. Image.FromStream (fs);
  283. }
  284. }
  285. [Test]
  286. [ExpectedException (typeof (OutOfMemoryException))]
  287. public void FromFile_Invalid ()
  288. {
  289. string filename = Assembly.GetExecutingAssembly ().Location;
  290. Image.FromFile (filename);
  291. }
  292. [Test]
  293. [ExpectedException (typeof (ArgumentException))]
  294. public void FromStream_Invalid ()
  295. {
  296. string filename = Assembly.GetExecutingAssembly ().Location;
  297. using (FileStream fs = File.OpenRead (filename)) {
  298. Image.FromStream (fs);
  299. }
  300. }
  301. private Bitmap GetBitmap ()
  302. {
  303. Bitmap bmp = new Bitmap (20, 10, PixelFormat.Format24bppRgb);
  304. using (Graphics g = Graphics.FromImage (bmp))
  305. {
  306. Pen pen = new Pen (Color.Black, 3);
  307. g.DrawRectangle (pen, 0, 0, 5, 10);
  308. }
  309. return bmp;
  310. }
  311. [Test]
  312. public void StreamSaveLoad ()
  313. {
  314. using (MemoryStream ms = new MemoryStream ()) {
  315. using (Bitmap bmp = GetBitmap ()) {
  316. Assert.AreEqual (0, ms.Position, "Position-1");
  317. bmp.Save (ms, ImageFormat.Bmp);
  318. Assert.IsTrue (ms.Position > 0, "Position-2");
  319. ms.Position = ms.Length;
  320. Assert.AreEqual (ms.Length, ms.Position, "Position-3");
  321. Bitmap bmp2 = (Bitmap)Image.FromStream (ms);
  322. Assert.IsTrue (ms.Position > 20, "Position-4");
  323. Assert.IsTrue (bmp2.RawFormat.Equals (ImageFormat.Bmp), "Bmp");
  324. Assert.AreEqual (bmp.GetPixel (0, 0), bmp2.GetPixel (0, 0), "0,0");
  325. Assert.AreEqual (bmp.GetPixel (10, 0), bmp2.GetPixel (10, 0), "10,0");
  326. }
  327. }
  328. }
  329. [Test]
  330. [ExpectedException (typeof (ArgumentException))]
  331. public void StreamJunkSaveLoad ()
  332. {
  333. using (MemoryStream ms = new MemoryStream ()) {
  334. // junk
  335. ms.WriteByte (0xff);
  336. ms.WriteByte (0xef);
  337. Assert.AreEqual (2, ms.Position, "Position-1");
  338. using (Bitmap bmp = GetBitmap ()) {
  339. bmp.Save (ms, ImageFormat.Bmp);
  340. Assert.IsTrue (ms.Position > 2, "Position-2");
  341. // exception here
  342. Image.FromStream (ms);
  343. }
  344. }
  345. }
  346. [Test]
  347. #if !NET_2_0
  348. [ExpectedException (typeof (InvalidOperationException))]
  349. #endif
  350. public void XmlSerialization ()
  351. {
  352. new XmlSerializer (typeof (Image));
  353. }
  354. }
  355. }