2007-03-14 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestImage.cs
1 //
2 // Image class testing unit
3 //
4 // Authors:
5 //      Jordi Mas i Hernàndez (jmas@softcatala.org>
6 //      Sebastien Pouliot  <sebastien@ximian.com>
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
31 using System;
32 using System.Drawing;
33 using System.Drawing.Imaging;
34 using System.IO;
35 using System.Reflection;
36 using System.Security.Permissions;
37 using System.Xml.Serialization;
38 using NUnit.Framework;
39
40 namespace MonoTests.System.Drawing{
41
42         [TestFixture]
43         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
44         public class ImageTest {
45
46                 private string fname;
47                 private bool callback;
48
49                 [TestFixtureSetUp]
50                 public void FixtureSetup ()
51                 {
52                         fname = Path.GetTempFileName ();
53                 }
54
55                 [TestFixtureTearDown]
56                 public void FixtureTearDown ()
57                 {
58                         try {
59                                 File.Delete (fname);
60                         }
61                         catch {
62                         }
63                 }
64
65                 [SetUp]
66                 public void SetUp ()
67                 {
68                         callback = false;
69                 }
70
71                 [Test]
72                 [ExpectedException (typeof (FileNotFoundException))]
73                 public void FileDoesNotExists ()
74                 {
75                         Image.FromFile ("FileDoesNotExists.jpg");
76                 }
77
78                 private bool CallbackTrue ()
79                 {
80                         callback = true;
81                         return true;
82                 }
83
84                 private bool CallbackFalse ()
85                 {
86                         callback = true;
87                         return false;
88                 }
89
90                 [Test]
91                 public void GetThumbnailImage_NullCallback_Tiff ()
92                 {
93                         using (Bitmap bmp = new Bitmap (10, 10)) {
94                                 // according to documentation a callback is mandatory
95                                 Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero);
96                                 Assert.AreEqual (5, tn.Height, "Height");
97                                 Assert.AreEqual (10, tn.Width, "Width");
98                                 Assert.IsFalse (callback, "Callback called");
99                                 tn.Save (fname, ImageFormat.Tiff);
100                         }
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (OutOfMemoryException))]
105                 public void GetThumbnailImage_Height_Zero ()
106                 {
107                         using (Bitmap bmp = new Bitmap (10, 10)) {
108                                 Image tn = bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
109                         }
110                 }
111
112                 [Test]
113                 [ExpectedException (typeof (OutOfMemoryException))]
114                 public void GetThumbnailImage_Width_Negative ()
115                 {
116                         using (Bitmap bmp = new Bitmap (10, 10)) {
117                                 Image tn = bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
118                         }
119                 }
120
121                 [Test]
122                 public void GetThumbnailImage_CallbackData_Invalid ()
123                 {
124                         using (Bitmap bmp = new Bitmap (10, 10)) {
125                                 // according to documentation IntPtr.Zero must be supplied as data
126                                 Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
127                                 Assert.AreEqual (5, tn.Height, "Height");
128                                 Assert.AreEqual (5, tn.Width, "Width");
129                                 Assert.IsFalse (callback, "Callback called");
130                                 tn.Save (fname, ImageFormat.Tiff);
131                         }
132                 }
133
134                 [Test]
135                 public void GetThumbnailImage_SameSize_Bmp ()
136                 {
137                         using (Bitmap bmp = new Bitmap (10, 10)) {
138                                 Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
139                                 Assert.AreEqual (10, tn.Height, "Height");
140                                 Assert.AreEqual (10, tn.Width, "Width");
141                                 Assert.IsFalse (callback, "Callback called");
142                                 tn.Save (fname, ImageFormat.Bmp);
143                         }
144                 }
145
146                 [Test]
147                 public void GetThumbnailImage_Smaller_Gif ()
148                 {
149                         using (Bitmap bmp = new Bitmap (10, 10)) {
150                                 Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
151                                 Assert.AreEqual (4, tn.Height, "Height");
152                                 Assert.AreEqual (4, tn.Width, "Width");
153                                 Assert.IsFalse (callback, "Callback called");
154                                 tn.Save (fname, ImageFormat.Gif);
155                         }
156                 }
157
158                 [Test]
159                 public void GetThumbnailImage_Bigger_Png ()
160                 {
161                         using (Bitmap bmp = new Bitmap (10, 10)) {
162                                 Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
163                                 Assert.AreEqual (40, tn.Height, "Height");
164                                 Assert.AreEqual (40, tn.Width, "Width");
165                                 Assert.IsFalse (callback, "Callback called");
166                                 tn.Save (fname, ImageFormat.Png);
167                         }
168                 }
169
170                 [Test]
171 #if !NET_2_0
172                 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
173 #endif
174                 public void Stream_Unlocked ()
175                 {
176                         try {
177                                 Image img = null;
178                                 using (MemoryStream ms = new MemoryStream ()) {
179                                         using (Bitmap bmp = new Bitmap (10, 10)) {
180                                                 bmp.Save (ms, ImageFormat.Png);
181                                         }
182                                         ms.Position = 0;
183                                         img = Image.FromStream (ms);
184                                 }
185                                 // stream isn't available anymore
186                                 ((Bitmap) img).MakeTransparent (Color.Transparent);
187                         }
188                         catch (OutOfMemoryException) {
189                                 int p = (int) Environment.OSVersion.Platform;
190                                 // libgdiplus (UNIX) doesn't lazy load the image so the
191                                 // stream may be freed (and this exception will never occur)
192                                 if ((p == 4) || (p == 128))
193                                         throw;
194                         }
195                 }
196
197                 [Test]
198 #if !NET_2_0
199                 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
200 #endif
201                 public void Stream_Locked ()
202                 {
203                         Image img = null;
204                         using (MemoryStream ms = new MemoryStream ()) {
205                                 using (Bitmap bmp = new Bitmap (10, 10)) {
206                                         bmp.Save (ms, ImageFormat.Png);
207                                 }
208                                 ms.Position = 0;
209                                 img = Image.FromStream (ms);
210                                 // stream is available
211                                 ((Bitmap) img).MakeTransparent (Color.Transparent);
212                         }
213                 }
214
215                 [Test]
216 #if NET_2_0
217                 [Category ("NotWorking")]       // http://bugzilla.ximian.com/show_bug.cgi?id=80558
218 #else
219                 [ExpectedException (typeof (InvalidOperationException))]
220 #endif
221                 public void XmlSerialize ()
222                 {
223                         new XmlSerializer (typeof (Image));
224                 }
225
226                 private void Wmf (Image img)
227                 {
228                         Assert.IsFalse (img is Bitmap, "Bitmap");
229                         Assert.IsTrue (img is Metafile, "Metafile");
230                         // as Image
231                         Assert.AreEqual (327683, img.Flags, "Flags");
232                         Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Wmf), "Wmf");
233 #if NET_2_0
234                         Assert.IsNull (img.Tag, "Tag");
235 #endif
236                 }
237
238                 [Test]
239                 public void FromFile_Metafile_Wmf ()
240                 {
241                         string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
242                         using (Image img = Image.FromFile (filename)) {
243                                 Wmf (img);
244                         }
245                 }
246
247                 [Test]
248                 public void FromStream_Metafile_Wmf ()
249                 {
250                         string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
251                         using (FileStream fs = File.OpenRead (filename)) {
252                                 using (Image img = Image.FromStream (fs)) {
253                                         Wmf (img);
254                                 }
255                         }
256                 }
257
258                 private void Emf (Image img)
259                 {
260                         Assert.IsFalse (img is Bitmap, "Bitmap");
261                         Assert.IsTrue (img is Metafile, "Metafile");
262                         // as Image
263                         Assert.AreEqual (327683, img.Flags, "Flags");
264                         Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf");
265 #if NET_2_0
266                         Assert.IsNull (img.Tag, "Tag");
267 #endif
268                 }
269
270                 [Test]
271                 public void FromFile_Metafile_Emf ()
272                 {
273                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
274                         using (Image img = Image.FromFile (filename)) {
275                                 Emf (img);
276                         }
277                 }
278
279                 [Test]
280                 public void FromStream_Metafile_Emf ()
281                 {
282                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
283                         using (FileStream fs = File.OpenRead (filename)) {
284                                 using (Image img = Image.FromStream (fs)) {
285                                         Emf (img);
286                                 }
287                         }
288                 }
289
290                 [Test]
291                 [ExpectedException (typeof (OutOfMemoryException))]
292                 public void FromFile_Invalid ()
293                 {
294                         string filename = Assembly.GetExecutingAssembly ().Location;
295                         Image.FromFile (filename);
296                 }
297
298                 [Test]
299                 [ExpectedException (typeof (ArgumentException))]
300                 public void FromStream_Invalid ()
301                 {
302                         string filename = Assembly.GetExecutingAssembly ().Location;
303                         using (FileStream fs = File.OpenRead (filename)) {
304                                 Image.FromStream (fs);
305                         }
306                 }
307         }
308 }