Make System.Drawing unit tests use Assert.Throws instead of [ExpectedException] ...
[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                 public void FileDoesNotExists ()
73                 {
74                         Assert.Throws<FileNotFoundException> (() => Image.FromFile ("FileDoesNotExists.jpg"));
75                 }
76
77                 private bool CallbackTrue ()
78                 {
79                         callback = true;
80                         return true;
81                 }
82
83                 private bool CallbackFalse ()
84                 {
85                         callback = true;
86                         return false;
87                 }
88
89                 [Test]
90                 public void GetThumbnailImage_NullCallback_Tiff ()
91                 {
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);
99                         }
100                 }
101
102                 [Test]
103                 public void GetThumbnailImage_Height_Zero ()
104                 {
105                         using (Bitmap bmp = new Bitmap (10, 10)) {
106                                 Assert.Throws<OutOfMemoryException> (() => bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero));
107                         }
108                 }
109
110                 [Test]
111                 public void GetThumbnailImage_Width_Negative ()
112                 {
113                         using (Bitmap bmp = new Bitmap (10, 10)) {
114                                 Assert.Throws<OutOfMemoryException> (() => bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero));
115                         }
116                 }
117
118                 [Test]
119                 public void GetThumbnailImage_CallbackData_Invalid ()
120                 {
121                         using (Bitmap bmp = new Bitmap (10, 10)) {
122                                 // according to documentation IntPtr.Zero must be supplied as data
123                                 Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
124                                 Assert.AreEqual (5, tn.Height, "Height");
125                                 Assert.AreEqual (5, tn.Width, "Width");
126                                 Assert.IsFalse (callback, "Callback called");
127                                 tn.Save (fname, ImageFormat.Tiff);
128                         }
129                 }
130
131                 [Test]
132                 public void GetThumbnailImage_SameSize_Bmp ()
133                 {
134                         using (Bitmap bmp = new Bitmap (10, 10)) {
135                                 Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
136                                 Assert.AreEqual (10, tn.Height, "Height");
137                                 Assert.AreEqual (10, tn.Width, "Width");
138                                 Assert.IsFalse (callback, "Callback called");
139                                 tn.Save (fname, ImageFormat.Bmp);
140                         }
141                 }
142
143                 [Test]
144                 public void GetThumbnailImage_Smaller_Gif ()
145                 {
146                         using (Bitmap bmp = new Bitmap (10, 10)) {
147                                 Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
148                                 Assert.AreEqual (4, tn.Height, "Height");
149                                 Assert.AreEqual (4, tn.Width, "Width");
150                                 Assert.IsFalse (callback, "Callback called");
151                                 tn.Save (fname, ImageFormat.Gif);
152                         }
153                 }
154
155                 [Test]
156                 public void GetThumbnailImage_Bigger_Png ()
157                 {
158                         using (Bitmap bmp = new Bitmap (10, 10)) {
159                                 Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
160                                 Assert.AreEqual (40, tn.Height, "Height");
161                                 Assert.AreEqual (40, tn.Width, "Width");
162                                 Assert.IsFalse (callback, "Callback called");
163                                 tn.Save (fname, ImageFormat.Png);
164                         }
165                 }
166
167                 [Test]
168                 public void Stream_Unlocked ()
169                 {
170                         try {
171                                 Image img = null;
172                                 using (MemoryStream ms = new MemoryStream ()) {
173                                         using (Bitmap bmp = new Bitmap (10, 10)) {
174                                                 bmp.Save (ms, ImageFormat.Png);
175                                         }
176                                         ms.Position = 0;
177                                         img = Image.FromStream (ms);
178                                 }
179                                 // stream isn't available anymore
180                                 ((Bitmap) img).MakeTransparent (Color.Transparent);
181                         }
182                         catch (OutOfMemoryException) {
183                                 int p = (int) Environment.OSVersion.Platform;
184                                 // libgdiplus (UNIX) doesn't lazy load the image so the
185                                 // stream may be freed (and this exception will never occur)
186                                 if ((p == 4) || (p == 128) || (p == 6))
187                                         throw;
188                         }
189                 }
190
191                 [Test]
192                 public void Stream_Locked ()
193                 {
194                         Image img = null;
195                         using (MemoryStream ms = new MemoryStream ()) {
196                                 using (Bitmap bmp = new Bitmap (10, 10)) {
197                                         bmp.Save (ms, ImageFormat.Png);
198                                 }
199                                 ms.Position = 0;
200                                 img = Image.FromStream (ms);
201                                 // stream is available
202                                 ((Bitmap) img).MakeTransparent (Color.Transparent);
203                         }
204                 }
205
206                 [Test]
207                 [Category ("NotWorking")]       // http://bugzilla.ximian.com/show_bug.cgi?id=80558
208                 public void XmlSerialize ()
209                 {
210                         new XmlSerializer (typeof (Image));
211                 }
212
213                 private void Wmf (Image img)
214                 {
215                         Assert.IsFalse (img is Bitmap, "Bitmap");
216                         Assert.IsTrue (img is Metafile, "Metafile");
217                         // as Image
218                         Assert.AreEqual (327683, img.Flags, "Flags");
219                         Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Wmf), "Wmf");
220                         Assert.IsNull (img.Tag, "Tag");
221                 }
222
223                 [Test]
224                 public void FromFile_Metafile_Wmf ()
225                 {
226                         string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
227                         using (Image img = Image.FromFile (filename)) {
228                                 Wmf (img);
229                         }
230                 }
231
232                 [Test]
233                 public void FromStream_Metafile_Wmf ()
234                 {
235                         string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
236                         using (FileStream fs = File.OpenRead (filename)) {
237                                 using (Image img = Image.FromStream (fs)) {
238                                         Wmf (img);
239                                 }
240                         }
241                 }
242
243                 [Test]
244                 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
245                 public void FromStream_Metafile_Wmf_NotOrigin ()
246                 {
247                         string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
248                         using (FileStream fs = File.OpenRead (filename)) {
249                                 fs.Position = fs.Length / 2;
250                                 Assert.Throws<ArgumentException> (() => Image.FromStream (fs));
251                         }
252                 }
253
254                 private void Emf (Image img)
255                 {
256                         Assert.IsFalse (img is Bitmap, "Bitmap");
257                         Assert.IsTrue (img is Metafile, "Metafile");
258                         // as Image
259                         Assert.AreEqual (327683, img.Flags, "Flags");
260                         Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf");
261                         Assert.IsNull (img.Tag, "Tag");
262                 }
263
264                 [Test]
265                 public void FromFile_Metafile_Emf ()
266                 {
267                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
268                         using (Image img = Image.FromFile (filename)) {
269                                 Emf (img);
270                         }
271                 }
272
273                 [Test]
274                 public void FromStream_Metafile_Emf ()
275                 {
276                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
277                         using (FileStream fs = File.OpenRead (filename)) {
278                                 using (Image img = Image.FromStream (fs)) {
279                                         Emf (img);
280                                 }
281                         }
282                 }
283
284                 [Test]
285                 [Category ("NotWorking")] // https://bugzilla.novell.com/show_bug.cgi?id=338779
286                 public void FromStream_Metafile_Emf_NotOrigin ()
287                 {
288                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
289                         using (FileStream fs = File.OpenRead (filename)) {
290                                 fs.Position = fs.Length / 2;
291                                 Assert.Throws<ArgumentException> (() => Image.FromStream (fs));
292                         }
293                 }
294
295                 [Test]
296                 public void FromFile_Invalid ()
297                 {
298                         string filename = Assembly.GetExecutingAssembly ().Location;
299                         Assert.Throws<OutOfMemoryException> (() => Image.FromFile (filename));
300                 }
301
302                 [Test]
303                 public void FromStream_Invalid ()
304                 {
305                         string filename = Assembly.GetExecutingAssembly ().Location;
306                         using (FileStream fs = File.OpenRead (filename)) {
307                                 Assert.Throws<ArgumentException> (() => Image.FromStream (fs));
308                         }
309                 }
310
311                 private Bitmap GetBitmap ()
312                 {
313                         Bitmap bmp = new Bitmap (20, 10, PixelFormat.Format24bppRgb);
314                         using (Graphics g = Graphics.FromImage (bmp))
315                         {
316                                 Pen pen = new Pen (Color.Black, 3);
317                                 g.DrawRectangle (pen, 0, 0, 5, 10);
318                         }
319                         return bmp;
320                 }
321
322                 [Test]
323                 public void StreamSaveLoad ()
324                 {
325                         using (MemoryStream ms = new MemoryStream ()) {
326                                 using (Bitmap bmp = GetBitmap ()) {
327                                         Assert.AreEqual (0, ms.Position, "Position-1");
328                                         bmp.Save (ms, ImageFormat.Bmp);
329                                         Assert.IsTrue (ms.Position > 0, "Position-2");
330
331                                         ms.Position = ms.Length;
332                                         Assert.AreEqual (ms.Length, ms.Position, "Position-3");
333
334                                         Bitmap bmp2 = (Bitmap)Image.FromStream (ms);
335                                         Assert.IsTrue (ms.Position > 20, "Position-4");
336
337                                         Assert.IsTrue (bmp2.RawFormat.Equals (ImageFormat.Bmp), "Bmp");
338
339                                         Assert.AreEqual (bmp.GetPixel (0, 0), bmp2.GetPixel (0, 0), "0,0");
340                                         Assert.AreEqual (bmp.GetPixel (10, 0), bmp2.GetPixel (10, 0), "10,0");
341                                 }
342                         }
343                 }
344
345                 [Test]
346                 public void StreamJunkSaveLoad ()
347                 {
348                         using (MemoryStream ms = new MemoryStream ()) {
349                                 // junk
350                                 ms.WriteByte (0xff);
351                                 ms.WriteByte (0xef);
352                                 Assert.AreEqual (2, ms.Position, "Position-1");
353
354                                 using (Bitmap bmp = GetBitmap ()) {
355                                         bmp.Save (ms, ImageFormat.Bmp);
356                                         Assert.IsTrue (ms.Position > 2, "Position-2");
357                                         // exception here
358                                         Assert.Throws<ArgumentException> (() => Image.FromStream (ms));
359                                 }
360                         }
361                 }
362
363                 [Test]
364                 public void XmlSerialization ()
365                 {
366                         new XmlSerializer (typeof (Image));
367                 }
368         }
369 }