2007-01-15 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestImage.cs
index 610f103623bcad9e6af078eef0cee6935698615c..6349648ed558e4b11e4be0e1df87b2d37f12f2c7 100644 (file)
@@ -2,11 +2,11 @@
 // Image class testing unit
 //
 // Authors:
-//     Jordi Mas i Hernàndez (jmas@softcatala.org>
+//     Jordi Mas i Hernàndez (jmas@softcatala.org>
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2005 Ximian, Inc.  http://www.ximian.com
-// Copyright (C) 2005, 2006 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2005-2007 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -32,7 +32,6 @@ using System;
 using System.Drawing;
 using System.Drawing.Imaging;
 using System.IO;
-//using System.Runtime.InteropServices;
 using System.Security.Permissions;
 using NUnit.Framework;
 
@@ -43,6 +42,7 @@ namespace MonoTests.System.Drawing{
        public class ImageTest {
 
                private string fname;
+               private bool callback;
 
                [TestFixtureSetUp]
                public void FixtureSetup ()
@@ -60,6 +60,12 @@ namespace MonoTests.System.Drawing{
                        }
                }
 
+               [SetUp]
+               public void SetUp ()
+               {
+                       callback = false;
+               }
+
                [Test]
                [ExpectedException (typeof (FileNotFoundException))]
                public void FileDoesNotExists ()
@@ -69,11 +75,13 @@ namespace MonoTests.System.Drawing{
 
                private bool CallbackTrue ()
                {
+                       callback = true;
                        return true;
                }
 
                private bool CallbackFalse ()
                {
+                       callback = true;
                        return false;
                }
 
@@ -85,6 +93,7 @@ namespace MonoTests.System.Drawing{
                                Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero);
                                Assert.AreEqual (5, tn.Height, "Height");
                                Assert.AreEqual (10, tn.Width, "Width");
+                               Assert.IsFalse (callback, "Callback called");
                                tn.Save (fname, ImageFormat.Tiff);
                        }
                }
@@ -115,6 +124,7 @@ namespace MonoTests.System.Drawing{
                                Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
                                Assert.AreEqual (5, tn.Height, "Height");
                                Assert.AreEqual (5, tn.Width, "Width");
+                               Assert.IsFalse (callback, "Callback called");
                                tn.Save (fname, ImageFormat.Tiff);
                        }
                }
@@ -126,6 +136,7 @@ namespace MonoTests.System.Drawing{
                                Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
                                Assert.AreEqual (10, tn.Height, "Height");
                                Assert.AreEqual (10, tn.Width, "Width");
+                               Assert.IsFalse (callback, "Callback called");
                                tn.Save (fname, ImageFormat.Bmp);
                        }
                }
@@ -137,6 +148,7 @@ namespace MonoTests.System.Drawing{
                                Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
                                Assert.AreEqual (4, tn.Height, "Height");
                                Assert.AreEqual (4, tn.Width, "Width");
+                               Assert.IsFalse (callback, "Callback called");
                                tn.Save (fname, ImageFormat.Gif);
                        }
                }
@@ -148,8 +160,48 @@ namespace MonoTests.System.Drawing{
                                Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
                                Assert.AreEqual (40, tn.Height, "Height");
                                Assert.AreEqual (40, tn.Width, "Width");
+                               Assert.IsFalse (callback, "Callback called");
                                tn.Save (fname, ImageFormat.Png);
                        }
                }
+
+               [Test]
+               public void Stream_Unlocked ()
+               {
+                       try {
+                               Image img = null;
+                               using (MemoryStream ms = new MemoryStream ()) {
+                                       using (Bitmap bmp = new Bitmap (10, 10)) {
+                                               bmp.Save (ms, ImageFormat.Png);
+                                       }
+                                       ms.Position = 0;
+                                       img = Image.FromStream (ms);
+                               }
+                               // stream isn't available anymore
+                               ((Bitmap) img).MakeTransparent (Color.Transparent);
+                       }
+                       catch (OutOfMemoryException) {
+                               int p = (int) Environment.OSVersion.Platform;
+                               // libgdiplus (UNIX) doesn't lazy load the image so the
+                               // stream may be freed (and this exception will never occur)
+                               if ((p == 4) || (p == 128))
+                                       throw;
+                       }
+               }
+
+               [Test]
+               public void Stream_Locked ()
+               {
+                       Image img = null;
+                       using (MemoryStream ms = new MemoryStream ()) {
+                               using (Bitmap bmp = new Bitmap (10, 10)) {
+                                       bmp.Save (ms, ImageFormat.Png);
+                               }
+                               ms.Position = 0;
+                               img = Image.FromStream (ms);
+                               // stream is available
+                               ((Bitmap) img).MakeTransparent (Color.Transparent);
+                       }
+               }
        }
 }