f15d7f161cff6fb964b4fdeae6d5dc6de5fd9f0f
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / GDIPlusTest.cs
1 //
2 // Direct GDI+ API unit tests
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Drawing;
31 using System.Drawing.Drawing2D;
32 using System.Drawing.Imaging;
33 using System.Runtime.InteropServices;
34 using NUnit.Framework;
35
36 namespace MonoTests.System.Drawing {
37
38         // copied from Mono's System.Drawing.dll gdiEnums.cs
39         internal enum Status {
40                 Ok = 0,
41                 GenericError = 1,
42                 InvalidParameter = 2,
43                 OutOfMemory = 3,
44                 ObjectBusy = 4,
45                 InsufficientBuffer = 5,
46                 NotImplemented = 6,
47                 Win32Error = 7,
48                 WrongState = 8,
49                 Aborted = 9,
50                 FileNotFound = 10,
51                 ValueOverflow = 11,
52                 AccessDenied = 12,
53                 UnknownImageFormat = 13,
54                 FontFamilyNotFound = 14,
55                 FontStyleNotFound = 15,
56                 NotTrueTypeFont = 16,
57                 UnsupportedGdiplusVersion = 17,
58                 GdiplusNotInitialized = 18,
59                 PropertyNotFound = 19,
60                 PropertyNotSupported = 20,
61                 ProfileNotFound = 21
62         }
63
64         [TestFixture]
65         public class GDIPlusTest {
66
67                 // FontFamily
68
69                 [DllImport ("gdiplus.dll", CharSet=CharSet.Auto)]
70                 internal static extern Status GdipCreateFontFamilyFromName (
71                         [MarshalAs (UnmanagedType.LPWStr)] string fName, IntPtr collection, out IntPtr fontFamily);
72
73                 [DllImport ("gdiplus.dll")]
74                 internal static extern Status GdipGetGenericFontFamilySerif (out IntPtr fontFamily);
75
76                 [DllImport ("gdiplus.dll")]
77                 internal static extern Status GdipDeleteFontFamily (IntPtr fontfamily);
78
79                 [Test]
80                 public void DeleteFontFamily ()
81                 {
82                         // invalid image pointer (null)
83                         Assert.AreEqual (Status.InvalidParameter, GdipDeleteFontFamily (IntPtr.Zero), "null");
84
85                         IntPtr font_family;
86                         GdipCreateFontFamilyFromName ("Arial", IntPtr.Zero, out font_family);
87                         Assert.IsTrue (font_family != IntPtr.Zero, "GdipCreateFontFamilyFromName");
88                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "first");
89                 }
90
91                 [Test]
92                 [Category ("NotWorking")]
93                 public void DeleteFontFamily_DoubleDispose ()
94                 {
95                         IntPtr font_family;
96                         GdipGetGenericFontFamilySerif (out font_family);
97                         // first dispose
98                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "first");
99                         // second dispose
100                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "second");
101                 }
102
103                 // Bitmap
104
105                 [DllImport ("gdiplus.dll")]
106                 internal static extern Status GdipCreateBitmapFromScan0 (int width, int height, int stride, PixelFormat format, IntPtr scan0, out IntPtr bmp);
107
108                 [Test]
109                 public void CreateBitmapFromScan0 ()
110                 {
111                         IntPtr bmp;
112                         Assert.AreEqual (Status.InvalidParameter, GdipCreateBitmapFromScan0 (-1, 10, 10, PixelFormat.Format32bppArgb, IntPtr.Zero, out bmp), "negative width");
113                 }
114
115                 // GraphicsPath
116
117                 [DllImport ("gdiplus.dll")]
118                 internal static extern Status GdipCreatePath (FillMode brushMode, out IntPtr path);
119
120                 [DllImport ("gdiplus.dll")]
121                 internal static extern Status GdipGetPointCount (IntPtr path, out int count);
122
123                 [DllImport ("gdiplus.dll")]
124                 internal static extern Status GdipGetPathPoints (IntPtr path, [Out] PointF[] points, int count);
125
126                 [DllImport ("gdiplus.dll")]
127                 internal static extern Status GdipGetPathTypes (IntPtr path, [Out] byte[] types, int count);
128
129                 [Test]
130                 public void GetPointCount_Zero ()
131                 {
132                         IntPtr path;
133                         Assert.AreEqual (Status.Ok, GdipCreatePath (FillMode.Alternate, out path), "GdipCreatePath");
134                         Assert.IsTrue (path != IntPtr.Zero, "Handle");
135
136                         int count;
137                         Assert.AreEqual (Status.Ok, GdipGetPointCount (path, out count), "GdipGetPointCount");
138                         Assert.AreEqual (0, count, "Count");
139
140                         PointF[] points = new PointF[count];
141                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathPoints (path, points, count), "GdipGetPathPoints");
142                         // can't get the points if the count is zero!
143
144                         byte[] types = new byte[count];
145                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathTypes (path, types, count), "GdipGetPathTypes");
146                         // can't get the types if the count is zero!
147                 }
148
149                 // Image
150
151                 [DllImport ("gdiplus.dll")]
152                 internal static extern Status GdipDisposeImage (IntPtr image);
153
154                 [Test]
155                 public void DisposeImage ()
156                 {
157                         // invalid image pointer (null)
158                         Assert.AreEqual (Status.InvalidParameter, GdipDisposeImage (IntPtr.Zero), "null");
159
160                         IntPtr image;
161                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
162                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
163                 }
164
165                 [Test]
166                 [Category ("NotWorking")]
167                 public void DisposeImage_Dual ()
168                 {
169                         IntPtr image;
170                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
171                         // first dispose
172                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
173                         // second dispose
174                         Assert.AreEqual (Status.ObjectBusy, GdipDisposeImage (image), "second");
175                 }
176
177
178                 [DllImport ("gdiplus.dll")]
179                 internal static extern Status GdipGetImageThumbnail (IntPtr image, uint width, uint height, out IntPtr thumbImage, IntPtr callback, IntPtr callBackData);
180
181                 [Test]
182                 [Category ("NotWorking")] // libgdiplus doesn't implement GdipGetImageThumbnail (it is done inside S.D)
183                 public void GetImageThumbnail ()
184                 {
185                         IntPtr ptr;
186
187                         // invalid image pointer (null)
188                         Assert.AreEqual (Status.InvalidParameter, GdipGetImageThumbnail (IntPtr.Zero, 10, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
189
190                         IntPtr image;
191                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
192                         try {
193                                 // invalid width (0)
194                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 0, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
195                                 // invalid width (negative)
196                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 0x8000000, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
197                                 // invalid height (0)
198                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 10, 0, out ptr, IntPtr.Zero, IntPtr.Zero));
199                                 // invalid height (negative)
200                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 10, 0x8000000, out ptr, IntPtr.Zero, IntPtr.Zero));
201                         }
202                         finally {
203                                 GdipDisposeImage (image);
204                         }
205                 }
206
207                 // Region
208
209                 [DllImport ("gdiplus.dll")]
210                 static internal extern Status GdipCreateRegionRgnData (byte[] data, int size, out IntPtr region);
211
212                 [Test]
213                 public void CreateRegionRgnData ()
214                 {
215                         IntPtr region;
216                         Assert.AreEqual (Status.InvalidParameter, GdipCreateRegionRgnData (null, 0, out region));
217
218                         byte[] data = new byte[0];
219                         Assert.AreEqual (Status.GenericError, GdipCreateRegionRgnData (data, 0, out region));
220                 }
221         }
222 }