* roottypes.cs: Rename from tree.cs.
[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, 2006 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 NUnit.Framework;
37
38 namespace MonoTests.System.Drawing{
39
40         [TestFixture]
41         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42         public class ImageTest {
43
44                 private string fname;
45                 private bool callback;
46
47                 [TestFixtureSetUp]
48                 public void FixtureSetup ()
49                 {
50                         fname = Path.GetTempFileName ();
51                 }
52
53                 [TestFixtureTearDown]
54                 public void FixtureTearDown ()
55                 {
56                         try {
57                                 File.Delete (fname);
58                         }
59                         catch {
60                         }
61                 }
62
63                 [SetUp]
64                 public void SetUp ()
65                 {
66                         callback = false;
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (FileNotFoundException))]
71                 public void FileDoesNotExists ()
72                 {
73                         Image.FromFile ("FileDoesNotExists.jpg");
74                 }
75
76                 private bool CallbackTrue ()
77                 {
78                         callback = true;
79                         return true;
80                 }
81
82                 private bool CallbackFalse ()
83                 {
84                         callback = true;
85                         return false;
86                 }
87
88                 [Test]
89                 public void GetThumbnailImage_NullCallback_Tiff ()
90                 {
91                         using (Bitmap bmp = new Bitmap (10, 10)) {
92                                 // according to documentation a callback is mandatory
93                                 Image tn = bmp.GetThumbnailImage (10, 5, null, IntPtr.Zero);
94                                 Assert.AreEqual (5, tn.Height, "Height");
95                                 Assert.AreEqual (10, tn.Width, "Width");
96                                 Assert.IsFalse (callback, "Callback called");
97                                 tn.Save (fname, ImageFormat.Tiff);
98                         }
99                 }
100
101                 [Test]
102                 [ExpectedException (typeof (OutOfMemoryException))]
103                 public void GetThumbnailImage_Height_Zero ()
104                 {
105                         using (Bitmap bmp = new Bitmap (10, 10)) {
106                                 Image tn = bmp.GetThumbnailImage (5, 0, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
107                         }
108                 }
109
110                 [Test]
111                 [ExpectedException (typeof (OutOfMemoryException))]
112                 public void GetThumbnailImage_Width_Negative ()
113                 {
114                         using (Bitmap bmp = new Bitmap (10, 10)) {
115                                 Image tn = bmp.GetThumbnailImage (-5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
116                         }
117                 }
118
119                 [Test]
120                 public void GetThumbnailImage_CallbackData_Invalid ()
121                 {
122                         using (Bitmap bmp = new Bitmap (10, 10)) {
123                                 // according to documentation IntPtr.Zero must be supplied as data
124                                 Image tn = bmp.GetThumbnailImage (5, 5, new Image.GetThumbnailImageAbort (CallbackFalse), (IntPtr)Int32.MaxValue);
125                                 Assert.AreEqual (5, tn.Height, "Height");
126                                 Assert.AreEqual (5, tn.Width, "Width");
127                                 Assert.IsFalse (callback, "Callback called");
128                                 tn.Save (fname, ImageFormat.Tiff);
129                         }
130                 }
131
132                 [Test]
133                 public void GetThumbnailImage_SameSize_Bmp ()
134                 {
135                         using (Bitmap bmp = new Bitmap (10, 10)) {
136                                 Image tn = bmp.GetThumbnailImage (10, 10, new Image.GetThumbnailImageAbort (CallbackFalse), IntPtr.Zero);
137                                 Assert.AreEqual (10, tn.Height, "Height");
138                                 Assert.AreEqual (10, tn.Width, "Width");
139                                 Assert.IsFalse (callback, "Callback called");
140                                 tn.Save (fname, ImageFormat.Bmp);
141                         }
142                 }
143
144                 [Test]
145                 public void GetThumbnailImage_Smaller_Gif ()
146                 {
147                         using (Bitmap bmp = new Bitmap (10, 10)) {
148                                 Image tn = bmp.GetThumbnailImage (4, 4, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
149                                 Assert.AreEqual (4, tn.Height, "Height");
150                                 Assert.AreEqual (4, tn.Width, "Width");
151                                 Assert.IsFalse (callback, "Callback called");
152                                 tn.Save (fname, ImageFormat.Gif);
153                         }
154                 }
155
156                 [Test]
157                 public void GetThumbnailImage_Bigger_Png ()
158                 {
159                         using (Bitmap bmp = new Bitmap (10, 10)) {
160                                 Image tn = bmp.GetThumbnailImage (40, 40, new Image.GetThumbnailImageAbort (CallbackTrue), IntPtr.Zero);
161                                 Assert.AreEqual (40, tn.Height, "Height");
162                                 Assert.AreEqual (40, tn.Width, "Width");
163                                 Assert.IsFalse (callback, "Callback called");
164                                 tn.Save (fname, ImageFormat.Png);
165                         }
166                 }
167         }
168 }