Mark tests as not working under TARGET_JVM
[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.Security.Permissions;
36 using System.Xml.Serialization;
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Drawing{
40
41         [TestFixture]
42         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
43         public class ImageTest {
44
45                 private string fname;
46                 private bool callback;
47
48                 [TestFixtureSetUp]
49                 public void FixtureSetup ()
50                 {
51                         fname = Path.GetTempFileName ();
52                 }
53
54                 [TestFixtureTearDown]
55                 public void FixtureTearDown ()
56                 {
57                         try {
58                                 File.Delete (fname);
59                         }
60                         catch {
61                         }
62                 }
63
64                 [SetUp]
65                 public void SetUp ()
66                 {
67                         callback = false;
68                 }
69
70                 [Test]
71                 [ExpectedException (typeof (FileNotFoundException))]
72                 public void FileDoesNotExists ()
73                 {
74                         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                 [ExpectedException (typeof (OutOfMemoryException))]
104                 public void GetThumbnailImage_Height_Zero ()
105                 {
106                         using (Bitmap bmp = new Bitmap (10, 10)) {
107                                 Image tn = bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
108                         }
109                 }
110
111                 [Test]
112                 [ExpectedException (typeof (OutOfMemoryException))]
113                 public void GetThumbnailImage_Width_Negative ()
114                 {
115                         using (Bitmap bmp = new Bitmap (10, 10)) {
116                                 Image tn = bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
117                         }
118                 }
119
120                 [Test]
121                 public void GetThumbnailImage_CallbackData_Invalid ()
122                 {
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);
130                         }
131                 }
132
133                 [Test]
134                 public void GetThumbnailImage_SameSize_Bmp ()
135                 {
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);
142                         }
143                 }
144
145                 [Test]
146                 public void GetThumbnailImage_Smaller_Gif ()
147                 {
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);
154                         }
155                 }
156
157                 [Test]
158                 public void GetThumbnailImage_Bigger_Png ()
159                 {
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);
166                         }
167                 }
168
169                 [Test]
170 #if !NET_2_0
171                 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
172 #endif
173                 public void Stream_Unlocked ()
174                 {
175                         try {
176                                 Image img = null;
177                                 using (MemoryStream ms = new MemoryStream ()) {
178                                         using (Bitmap bmp = new Bitmap (10, 10)) {
179                                                 bmp.Save (ms, ImageFormat.Png);
180                                         }
181                                         ms.Position = 0;
182                                         img = Image.FromStream (ms);
183                                 }
184                                 // stream isn't available anymore
185                                 ((Bitmap) img).MakeTransparent (Color.Transparent);
186                         }
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))
192                                         throw;
193                         }
194                 }
195
196                 [Test]
197 #if !NET_2_0
198                 [Category ("NotDotNet")] // MS 1.x throws an ArgumentNullException in this case
199 #endif
200                 public void Stream_Locked ()
201                 {
202                         Image img = null;
203                         using (MemoryStream ms = new MemoryStream ()) {
204                                 using (Bitmap bmp = new Bitmap (10, 10)) {
205                                         bmp.Save (ms, ImageFormat.Png);
206                                 }
207                                 ms.Position = 0;
208                                 img = Image.FromStream (ms);
209                                 // stream is available
210                                 ((Bitmap) img).MakeTransparent (Color.Transparent);
211                         }
212                 }
213
214                 [Test]
215 #if NET_2_0
216                 [Category ("NotWorking")]       // http://bugzilla.ximian.com/show_bug.cgi?id=80558
217 #else
218                 [ExpectedException (typeof (InvalidOperationException))]
219 #endif
220                 public void XmlSerialize ()
221                 {
222                         new XmlSerializer (typeof (Image));
223                 }
224
225                 private void Wmf (Image img)
226                 {
227                         Assert.IsFalse (img is Bitmap, "Bitmap");
228                         Assert.IsTrue (img is Metafile, "Metafile");
229                         // as Image
230                         Assert.AreEqual (327683, img.Flags, "Flags");
231                         Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Wmf), "Wmf");
232 #if NET_2_0
233                         Assert.IsNull (img.Tag, "Tag");
234 #endif
235                 }
236
237                 [Test]
238                 public void FromFile_Metafile_Wmf ()
239                 {
240                         string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
241                         using (Image img = Image.FromFile (filename)) {
242                                 Wmf (img);
243                         }
244                 }
245
246                 [Test]
247                 public void FromStream_Metafile_Wmf ()
248                 {
249                         string filename = TestBitmap.getInFile ("bitmaps/telescope_01.wmf");
250                         using (FileStream fs = File.OpenRead (filename)) {
251                                 using (Image img = Image.FromStream (fs)) {
252                                         Wmf (img);
253                                 }
254                         }
255                 }
256
257                 private void Emf (Image img)
258                 {
259                         Assert.IsFalse (img is Bitmap, "Bitmap");
260                         Assert.IsTrue (img is Metafile, "Metafile");
261                         // as Image
262                         Assert.AreEqual (327683, img.Flags, "Flags");
263                         Assert.IsTrue (img.RawFormat.Equals (ImageFormat.Emf), "Emf");
264 #if NET_2_0
265                         Assert.IsNull (img.Tag, "Tag");
266 #endif
267                 }
268
269                 [Test]
270                 public void FromFile_Metafile_Emf ()
271                 {
272                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
273                         using (Image img = Image.FromFile (filename)) {
274                                 Emf (img);
275                         }
276                 }
277
278                 [Test]
279                 public void FromStream_Metafile_Emf ()
280                 {
281                         string filename = TestBitmap.getInFile ("bitmaps/milkmateya01.emf");
282                         using (FileStream fs = File.OpenRead (filename)) {
283                                 using (Image img = Image.FromStream (fs)) {
284                                         Emf (img);
285                                 }
286                         }
287                 }
288         }
289 }