Merge pull request #273 from joncham/bug-getpid
[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) || (p == 6))
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                 [Test]
259                 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
260                 [ExpectedException (typeof (ArgumentException))]
261                 public void FromStream_Metafile_Wmf_NotOrigin ()
262                 {
263                         string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
264                         using (FileStream fs = File.OpenRead (filename)) {
265                                 fs.Position = fs.Length / 2;
266                                 Image.FromStream (fs);
267                         }
268                 }
269
270                 private void Emf (Image img)
271                 {
272                         Assert.IsFalse (img is Bitmap, "Bitmap");
273                         Assert.IsTrue (img is Metafile, "Metafile");
274                         // as Image
275                         Assert.AreEqual (327683, img.Flags, "Flags");
276                         Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf");
277 #if NET_2_0
278                         Assert.IsNull (img.Tag, "Tag");
279 #endif
280                 }
281
282                 [Test]
283                 public void FromFile_Metafile_Emf ()
284                 {
285                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
286                         using (Image img = Image.FromFile (filename)) {
287                                 Emf (img);
288                         }
289                 }
290
291                 [Test]
292                 public void FromStream_Metafile_Emf ()
293                 {
294                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
295                         using (FileStream fs = File.OpenRead (filename)) {
296                                 using (Image img = Image.FromStream (fs)) {
297                                         Emf (img);
298                                 }
299                         }
300                 }
301
302                 [Test]
303                 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
304                 [ExpectedException (typeof (ArgumentException))]
305                 public void FromStream_Metafile_Emf_NotOrigin ()
306                 {
307                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
308                         using (FileStream fs = File.OpenRead (filename)) {
309                                 fs.Position = fs.Length / 2;
310                                 Image.FromStream (fs);
311                         }
312                 }
313
314                 [Test]
315                 [ExpectedException (typeof (OutOfMemoryException))]
316                 public void FromFile_Invalid ()
317                 {
318                         string filename = Assembly.GetExecutingAssembly ().Location;
319                         Image.FromFile (filename);
320                 }
321
322                 [Test]
323                 [ExpectedException (typeof (ArgumentException))]
324                 public void FromStream_Invalid ()
325                 {
326                         string filename = Assembly.GetExecutingAssembly ().Location;
327                         using (FileStream fs = File.OpenRead (filename)) {
328                                 Image.FromStream (fs);
329                         }
330                 }
331
332                 private Bitmap GetBitmap ()
333                 {
334                         Bitmap bmp = new Bitmap (20, 10, PixelFormat.Format24bppRgb);
335                         using (Graphics g = Graphics.FromImage (bmp))
336                         {
337                                 Pen pen = new Pen (Color.Black, 3);
338                                 g.DrawRectangle (pen, 0, 0, 5, 10);
339                         }
340                         return bmp;
341                 }
342
343                 [Test]
344                 public void StreamSaveLoad ()
345                 {
346                         using (MemoryStream ms = new MemoryStream ()) {
347                                 using (Bitmap bmp = GetBitmap ()) {
348                                         Assert.AreEqual (0, ms.Position, "Position-1");
349                                         bmp.Save (ms, ImageFormat.Bmp);
350                                         Assert.IsTrue (ms.Position > 0, "Position-2");
351
352                                         ms.Position = ms.Length;
353                                         Assert.AreEqual (ms.Length, ms.Position, "Position-3");
354
355                                         Bitmap bmp2 = (Bitmap)Image.FromStream (ms);
356                                         Assert.IsTrue (ms.Position > 20, "Position-4");
357
358                                         Assert.IsTrue (bmp2.RawFormat.Equals (ImageFormat.Bmp), "Bmp");
359
360                                         Assert.AreEqual (bmp.GetPixel (0, 0), bmp2.GetPixel (0, 0), "0,0");
361                                         Assert.AreEqual (bmp.GetPixel (10, 0), bmp2.GetPixel (10, 0), "10,0");
362                                 }
363                         }
364                 }
365
366                 [Test]
367                 [ExpectedException (typeof (ArgumentException))]
368                 public void StreamJunkSaveLoad ()
369                 {
370                         using (MemoryStream ms = new MemoryStream ()) {
371                                 // junk
372                                 ms.WriteByte (0xff);
373                                 ms.WriteByte (0xef);
374                                 Assert.AreEqual (2, ms.Position, "Position-1");
375
376                                 using (Bitmap bmp = GetBitmap ()) {
377                                         bmp.Save (ms, ImageFormat.Bmp);
378                                         Assert.IsTrue (ms.Position > 2, "Position-2");
379                                         // exception here
380                                         Image.FromStream (ms);
381                                 }
382                         }
383                 }
384
385                 [Test]
386 #if !NET_2_0
387                 [ExpectedException (typeof (InvalidOperationException))]
388 #endif
389                 public void XmlSerialization ()
390                 {
391                         new XmlSerializer (typeof (Image));
392                 }
393         }
394 }