2006-08-04 Sebastien Pouliot <sebastien@ximian.com>
[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                 // Brush
116
117                 [DllImport ("gdiplus.dll")]
118                 static internal extern Status GdipDeleteBrush (IntPtr brush);
119
120                 [Test]
121                 public void DeleteBrush ()
122                 {
123                         Assert.AreEqual (Status.InvalidParameter, GdipDeleteBrush (IntPtr.Zero), "GdipDeleteBrush");
124                 }
125
126                 // GraphicsPath
127
128                 [DllImport ("gdiplus.dll")]
129                 internal static extern Status GdipCreatePath (FillMode brushMode, out IntPtr path);
130
131                 [DllImport ("gdiplus.dll")]
132                 internal static extern Status GdipGetPointCount (IntPtr path, out int count);
133
134                 [DllImport ("gdiplus.dll")]
135                 internal static extern Status GdipGetPathPoints (IntPtr path, [Out] PointF[] points, int count);
136
137                 [DllImport ("gdiplus.dll")]
138                 internal static extern Status GdipGetPathTypes (IntPtr path, [Out] byte[] types, int count);
139
140                 [Test]
141                 public void GetPointCount_Zero ()
142                 {
143                         IntPtr path;
144                         Assert.AreEqual (Status.Ok, GdipCreatePath (FillMode.Alternate, out path), "GdipCreatePath");
145                         Assert.IsTrue (path != IntPtr.Zero, "Handle");
146
147                         int count;
148                         Assert.AreEqual (Status.Ok, GdipGetPointCount (path, out count), "GdipGetPointCount");
149                         Assert.AreEqual (0, count, "Count");
150
151                         PointF[] points = new PointF[count];
152                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathPoints (path, points, count), "GdipGetPathPoints");
153                         // can't get the points if the count is zero!
154
155                         byte[] types = new byte[count];
156                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathTypes (path, types, count), "GdipGetPathTypes");
157                         // can't get the types if the count is zero!
158                 }
159
160                 // Image
161
162                 [DllImport ("gdiplus.dll")]
163                 internal static extern Status GdipDisposeImage (IntPtr image);
164
165                 [Test]
166                 public void DisposeImage ()
167                 {
168                         // invalid image pointer (null)
169                         Assert.AreEqual (Status.InvalidParameter, GdipDisposeImage (IntPtr.Zero), "null");
170
171                         IntPtr image;
172                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
173                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
174                 }
175
176                 [Test]
177                 [Category ("NotWorking")]
178                 public void DisposeImage_Dual ()
179                 {
180                         IntPtr image;
181                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
182                         // first dispose
183                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
184                         // second dispose
185                         Assert.AreEqual (Status.ObjectBusy, GdipDisposeImage (image), "second");
186                 }
187
188
189                 [DllImport ("gdiplus.dll")]
190                 internal static extern Status GdipGetImageThumbnail (IntPtr image, uint width, uint height, out IntPtr thumbImage, IntPtr callback, IntPtr callBackData);
191
192                 [Test]
193                 [Category ("NotWorking")] // libgdiplus doesn't implement GdipGetImageThumbnail (it is done inside S.D)
194                 public void GetImageThumbnail ()
195                 {
196                         IntPtr ptr;
197
198                         // invalid image pointer (null)
199                         Assert.AreEqual (Status.InvalidParameter, GdipGetImageThumbnail (IntPtr.Zero, 10, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
200
201                         IntPtr image;
202                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
203                         try {
204                                 // invalid width (0)
205                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 0, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
206                                 // invalid width (negative)
207                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 0x8000000, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
208                                 // invalid height (0)
209                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 10, 0, out ptr, IntPtr.Zero, IntPtr.Zero));
210                                 // invalid height (negative)
211                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 10, 0x8000000, out ptr, IntPtr.Zero, IntPtr.Zero));
212                         }
213                         finally {
214                                 GdipDisposeImage (image);
215                         }
216                 }
217
218                 // PathGradientBrush
219
220                 [DllImport ("gdiplus.dll")]
221                 static internal extern Status GdipCreatePathGradient (PointF[] points, int count, WrapMode wrapMode, out IntPtr brush);
222
223                 [DllImport ("gdiplus.dll")]
224                 static internal extern Status GdipGetPathGradientBlendCount (IntPtr brush, out int count);
225
226                 [DllImport ("gdiplus.dll")]
227                 static internal extern Status GdipGetPathGradientPresetBlend (IntPtr brush, int[] blend, float[] positions, int count);
228
229
230                 [Test]
231                 public void CreatePathGradient ()
232                 {
233                         PointF[] points = null;
234                         IntPtr brush;
235                         Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "null");
236
237                         points = new PointF [0];
238                         Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "empty");
239
240                         points = new PointF[1];
241                         Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 1, WrapMode.Clamp, out brush), "one");
242
243                         points = new PointF[2] { new PointF (1, 2), new PointF (20, 30) };
244                         Assert.AreEqual (Status.Ok, GdipCreatePathGradient (points, 2, WrapMode.Clamp, out brush), "two");
245
246                         int count;
247                         Assert.AreEqual (Status.Ok, GdipGetPathGradientBlendCount (brush, out count), "GdipGetPathGradientBlendCount");
248                         Assert.AreEqual (1, count, "blend count");
249
250                         int[] colors = new int[count];
251                         float[] positions = new float[count];
252                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathGradientPresetBlend (brush, colors, positions, count), "GdipGetPathGradientBlend");
253                         // can't call that for 1 count!
254
255                         Assert.AreEqual (Status.Ok, GdipDeleteBrush (brush), "GdipDeleteBrush");
256                 }
257
258
259                 // Region
260
261                 [DllImport ("gdiplus.dll")]
262                 static internal extern Status GdipCreateRegionRgnData (byte[] data, int size, out IntPtr region);
263
264                 [Test]
265                 public void CreateRegionRgnData ()
266                 {
267                         IntPtr region;
268                         Assert.AreEqual (Status.InvalidParameter, GdipCreateRegionRgnData (null, 0, out region));
269
270                         byte[] data = new byte[0];
271                         Assert.AreEqual (Status.GenericError, GdipCreateRegionRgnData (data, 0, out region));
272                 }
273         }
274 }