2 // Image class testing unit
5 // Jordi Mas i Hernàndez (jmas@softcatala.org>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2005 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2005-2007 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Drawing.Imaging;
35 using System.Security.Permissions;
36 using System.Xml.Serialization;
37 using NUnit.Framework;
39 namespace MonoTests.System.Drawing{
42 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
43 public class ImageTest {
46 private bool callback;
49 public void FixtureSetup ()
51 fname = Path.GetTempFileName ();
55 public void FixtureTearDown ()
71 [ExpectedException (typeof (FileNotFoundException))]
72 public void FileDoesNotExists ()
74 Image.FromFile ("FileDoesNotExists.jpg");
77 private bool CallbackTrue ()
83 private bool CallbackFalse ()
90 public void GetThumbnailImage_NullCallback_Tiff ()
92 using (Bitmap bmp = new Bitmap (10, 10)) {
93 // according to documentation a callback is mandatory
94 Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero);
95 Assert.AreEqual (5, tn.Height, "Height");
96 Assert.AreEqual (10, tn.Width, "Width");
97 Assert.IsFalse (callback, "Callback called");
98 tn.Save (fname, ImageFormat.Tiff);
103 [ExpectedException (typeof (OutOfMemoryException))]
104 public void GetThumbnailImage_Height_Zero ()
106 using (Bitmap bmp = new Bitmap (10, 10)) {
107 Image tn = bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
112 [ExpectedException (typeof (OutOfMemoryException))]
113 public void GetThumbnailImage_Width_Negative ()
115 using (Bitmap bmp = new Bitmap (10, 10)) {
116 Image tn = bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
121 public void GetThumbnailImage_CallbackData_Invalid ()
123 using (Bitmap bmp = new Bitmap (10, 10)) {
124 // according to documentation IntPtr.Zero must be supplied as data
125 Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
126 Assert.AreEqual (5, tn.Height, "Height");
127 Assert.AreEqual (5, tn.Width, "Width");
128 Assert.IsFalse (callback, "Callback called");
129 tn.Save (fname, ImageFormat.Tiff);
134 public void GetThumbnailImage_SameSize_Bmp ()
136 using (Bitmap bmp = new Bitmap (10, 10)) {
137 Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
138 Assert.AreEqual (10, tn.Height, "Height");
139 Assert.AreEqual (10, tn.Width, "Width");
140 Assert.IsFalse (callback, "Callback called");
141 tn.Save (fname, ImageFormat.Bmp);
146 public void GetThumbnailImage_Smaller_Gif ()
148 using (Bitmap bmp = new Bitmap (10, 10)) {
149 Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
150 Assert.AreEqual (4, tn.Height, "Height");
151 Assert.AreEqual (4, tn.Width, "Width");
152 Assert.IsFalse (callback, "Callback called");
153 tn.Save (fname, ImageFormat.Gif);
158 public void GetThumbnailImage_Bigger_Png ()
160 using (Bitmap bmp = new Bitmap (10, 10)) {
161 Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
162 Assert.AreEqual (40, tn.Height, "Height");
163 Assert.AreEqual (40, tn.Width, "Width");
164 Assert.IsFalse (callback, "Callback called");
165 tn.Save (fname, ImageFormat.Png);
171 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
173 public void Stream_Unlocked ()
177 using (MemoryStream ms = new MemoryStream ()) {
178 using (Bitmap bmp = new Bitmap (10, 10)) {
179 bmp.Save (ms, ImageFormat.Png);
182 img = Image.FromStream (ms);
184 // stream isn't available anymore
185 ((Bitmap) img).MakeTransparent (Color.Transparent);
187 catch (OutOfMemoryException) {
188 int p = (int) Environment.OSVersion.Platform;
189 // libgdiplus (UNIX) doesn't lazy load the image so the
190 // stream may be freed (and this exception will never occur)
191 if ((p == 4) || (p == 128))
198 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
200 public void Stream_Locked ()
203 using (MemoryStream ms = new MemoryStream ()) {
204 using (Bitmap bmp = new Bitmap (10, 10)) {
205 bmp.Save (ms, ImageFormat.Png);
208 img = Image.FromStream (ms);
209 // stream is available
210 ((Bitmap) img).MakeTransparent (Color.Transparent);
216 [Category ("NotWorking")] // http://bugzilla.ximian.com/show_bug.cgi?id=80558
218 [ExpectedException (typeof (InvalidOperationException))]
220 public void XmlSerialize ()
222 new XmlSerializer (typeof (Image));