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         // copied from Mono's System.Drawing.dll gdiEnums.cs
65         internal enum Unit {
66                 UnitWorld = 0,
67                 UnitDisplay = 1,
68                 UnitPixel = 2,
69                 UnitPoint = 3,
70                 UnitInch = 4,
71                 UnitDocument = 5,
72                 UnitMillimeter = 6
73         }
74
75         [TestFixture]
76         public class GDIPlusTest {
77
78                 // FontFamily
79
80                 [DllImport ("gdiplus.dll", CharSet=CharSet.Auto)]
81                 internal static extern Status GdipCreateFontFamilyFromName (
82                         [MarshalAs (UnmanagedType.LPWStr)] string fName, IntPtr collection, out IntPtr fontFamily);
83
84                 [DllImport ("gdiplus.dll")]
85                 internal static extern Status GdipGetGenericFontFamilySerif (out IntPtr fontFamily);
86
87                 [DllImport ("gdiplus.dll")]
88                 internal static extern Status GdipDeleteFontFamily (IntPtr fontfamily);
89
90                 [Test]
91                 public void DeleteFontFamily ()
92                 {
93                         // invalid image pointer (null)
94                         Assert.AreEqual (Status.InvalidParameter, GdipDeleteFontFamily (IntPtr.Zero), "null");
95
96                         IntPtr font_family;
97                         GdipCreateFontFamilyFromName ("Arial", IntPtr.Zero, out font_family);
98                         Assert.IsTrue (font_family != IntPtr.Zero, "GdipCreateFontFamilyFromName");
99                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "first");
100                 }
101
102                 [Test]
103                 [Category ("NotWorking")]
104                 public void DeleteFontFamily_DoubleDispose ()
105                 {
106                         IntPtr font_family;
107                         GdipGetGenericFontFamilySerif (out font_family);
108                         // first dispose
109                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "first");
110                         // second dispose
111                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "second");
112                 }
113
114                 // Bitmap
115
116                 [DllImport ("gdiplus.dll")]
117                 internal static extern Status GdipCreateBitmapFromScan0 (int width, int height, int stride, PixelFormat format, IntPtr scan0, out IntPtr bmp);
118
119                 [Test]
120                 public void CreateBitmapFromScan0 ()
121                 {
122                         IntPtr bmp;
123                         Assert.AreEqual (Status.InvalidParameter, GdipCreateBitmapFromScan0 (-1, 10, 10, PixelFormat.Format32bppArgb, IntPtr.Zero, out bmp), "negative width");
124                 }
125
126                 // Brush
127
128                 [DllImport ("gdiplus.dll")]
129                 static internal extern Status GdipDeleteBrush (IntPtr brush);
130
131                 [Test]
132                 public void DeleteBrush ()
133                 {
134                         Assert.AreEqual (Status.InvalidParameter, GdipDeleteBrush (IntPtr.Zero), "GdipDeleteBrush");
135                 }
136
137                 // GraphicsPath
138
139                 [DllImport ("gdiplus.dll")]
140                 internal static extern Status GdipCreatePath (FillMode brushMode, out IntPtr path);
141
142                 [DllImport ("gdiplus.dll")]
143                 internal static extern Status GdipGetPointCount (IntPtr path, out int count);
144
145                 [DllImport ("gdiplus.dll")]
146                 internal static extern Status GdipGetPathPoints (IntPtr path, [Out] PointF[] points, int count);
147
148                 [DllImport ("gdiplus.dll")]
149                 internal static extern Status GdipGetPathTypes (IntPtr path, [Out] byte[] types, int count);
150
151                 [Test]
152                 public void GetPointCount_Zero ()
153                 {
154                         IntPtr path;
155                         Assert.AreEqual (Status.Ok, GdipCreatePath (FillMode.Alternate, out path), "GdipCreatePath");
156                         Assert.IsTrue (path != IntPtr.Zero, "Handle");
157
158                         int count;
159                         Assert.AreEqual (Status.Ok, GdipGetPointCount (path, out count), "GdipGetPointCount");
160                         Assert.AreEqual (0, count, "Count");
161
162                         PointF[] points = new PointF[count];
163                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathPoints (path, points, count), "GdipGetPathPoints");
164                         // can't get the points if the count is zero!
165
166                         byte[] types = new byte[count];
167                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathTypes (path, types, count), "GdipGetPathTypes");
168                         // can't get the types if the count is zero!
169                 }
170
171                 // Image
172
173                 [DllImport ("gdiplus.dll")]
174                 internal static extern Status GdipDisposeImage (IntPtr image);
175
176                 [Test]
177                 public void DisposeImage ()
178                 {
179                         // invalid image pointer (null)
180                         Assert.AreEqual (Status.InvalidParameter, GdipDisposeImage (IntPtr.Zero), "null");
181
182                         IntPtr image;
183                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
184                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
185                 }
186
187                 [Test]
188                 [Category ("NotWorking")]
189                 public void DisposeImage_Dual ()
190                 {
191                         IntPtr image;
192                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
193                         // first dispose
194                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
195                         // second dispose
196                         Assert.AreEqual (Status.ObjectBusy, GdipDisposeImage (image), "second");
197                 }
198
199
200                 [DllImport ("gdiplus.dll")]
201                 internal static extern Status GdipGetImageThumbnail (IntPtr image, uint width, uint height, out IntPtr thumbImage, IntPtr callback, IntPtr callBackData);
202
203                 [Test]
204                 [Category ("NotWorking")] // libgdiplus doesn't implement GdipGetImageThumbnail (it is done inside S.D)
205                 public void GetImageThumbnail ()
206                 {
207                         IntPtr ptr;
208
209                         // invalid image pointer (null)
210                         Assert.AreEqual (Status.InvalidParameter, GdipGetImageThumbnail (IntPtr.Zero, 10, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
211
212                         IntPtr image;
213                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
214                         try {
215                                 // invalid width (0)
216                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 0, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
217                                 // invalid width (negative)
218                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 0x8000000, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
219                                 // invalid height (0)
220                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 10, 0, out ptr, IntPtr.Zero, IntPtr.Zero));
221                                 // invalid height (negative)
222                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 10, 0x8000000, out ptr, IntPtr.Zero, IntPtr.Zero));
223                         }
224                         finally {
225                                 GdipDisposeImage (image);
226                         }
227                 }
228
229                 // PathGradientBrush
230
231                 [DllImport ("gdiplus.dll")]
232                 static internal extern Status GdipCreatePathGradient (PointF[] points, int count, WrapMode wrapMode, out IntPtr brush);
233
234                 [DllImport ("gdiplus.dll")]
235                 static internal extern Status GdipGetPathGradientBlendCount (IntPtr brush, out int count);
236
237                 [DllImport ("gdiplus.dll")]
238                 static internal extern Status GdipGetPathGradientPresetBlend (IntPtr brush, int[] blend, float[] positions, int count);
239
240
241                 [Test]
242                 public void CreatePathGradient ()
243                 {
244                         PointF[] points = null;
245                         IntPtr brush;
246                         Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "null");
247
248                         points = new PointF [0];
249                         Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "empty");
250
251                         points = new PointF[1];
252                         Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 1, WrapMode.Clamp, out brush), "one");
253
254                         points = new PointF[2] { new PointF (1, 2), new PointF (20, 30) };
255                         Assert.AreEqual (Status.Ok, GdipCreatePathGradient (points, 2, WrapMode.Clamp, out brush), "two");
256
257                         int count;
258                         Assert.AreEqual (Status.Ok, GdipGetPathGradientBlendCount (brush, out count), "GdipGetPathGradientBlendCount");
259                         Assert.AreEqual (1, count, "blend count");
260
261                         int[] colors = new int[count];
262                         float[] positions = new float[count];
263                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathGradientPresetBlend (brush, colors, positions, count), "GdipGetPathGradientBlend");
264                         // can't call that for 1 count!
265
266                         Assert.AreEqual (Status.Ok, GdipDeleteBrush (brush), "GdipDeleteBrush");
267                 }
268
269                 // Pen
270
271                 [DllImport ("gdiplus.dll")]
272                 internal static extern Status GdipCreatePen1 (int argb, float width, Unit unit, out IntPtr pen);
273
274                 [DllImport ("gdiplus.dll")]
275                 internal static extern Status GdipGetPenDashStyle (IntPtr pen, out DashStyle dashStyle);
276
277                 [DllImport ("gdiplus.dll")]
278                 internal static extern Status GdipSetPenDashStyle (IntPtr pen, DashStyle dashStyle);
279
280                 [DllImport ("gdiplus.dll")]
281                 internal static extern Status GdipGetPenDashCount (IntPtr pen, out int count);
282
283                 [DllImport ("gdiplus.dll")]
284                 internal static extern Status GdipGetPenDashArray (IntPtr pen, float[] dash, int count);
285
286                 [DllImport ("gdiplus.dll")]
287                 internal static extern Status GdipDeletePen (IntPtr pen);
288
289                 [Test]
290                 public void CreatePen ()
291                 {
292                         IntPtr pen;
293                         Assert.AreEqual (Status.Ok, GdipCreatePen1 (0, 0f, Unit.UnitWorld, out pen), "GdipCreatePen1");
294                         Assert.IsTrue (pen != IntPtr.Zero, "pen");
295
296                         DashStyle ds;
297                         Assert.AreEqual (Status.Ok, GdipGetPenDashStyle (pen, out ds), "GdipGetPenDashStyle");
298                         Assert.AreEqual (Status.InvalidParameter, GdipGetPenDashStyle (IntPtr.Zero, out ds), "GdipGetPenDashStyle-null");
299
300                         ds = DashStyle.Custom;
301                         Assert.AreEqual (Status.Ok, GdipSetPenDashStyle (pen, ds), "GdipSetPenDashStyle");
302                         Assert.AreEqual (Status.InvalidParameter, GdipSetPenDashStyle (IntPtr.Zero, ds), "GdipSetPenDashStyle-null");
303
304                         int count;
305                         Assert.AreEqual (Status.Ok, GdipGetPenDashCount (pen, out count), "GdipGetPenDashCount");
306                         Assert.AreEqual (Status.InvalidParameter, GdipGetPenDashCount (IntPtr.Zero, out count), "GdipGetPenDashCount-null");
307                         Assert.AreEqual (0, count, "count");
308
309                         float[] dash = new float[count];
310                         Assert.AreEqual (Status.OutOfMemory, GdipGetPenDashArray (pen, dash, count), "GdipGetPenDashArray");
311                         Assert.AreEqual (Status.InvalidParameter, GdipGetPenDashArray (IntPtr.Zero, dash, count), "GdipGetPenDashArray-null");
312
313                         Assert.AreEqual (Status.Ok, GdipDeletePen (pen), "GdipDeletePen");
314                         Assert.AreEqual (Status.InvalidParameter, GdipDeletePen (IntPtr.Zero), "GdipDeletePen-null");
315                 }
316
317                 // Region
318
319                 [DllImport ("gdiplus.dll")]
320                 static internal extern Status GdipCreateRegionRgnData (byte[] data, int size, out IntPtr region);
321
322                 [Test]
323                 public void CreateRegionRgnData ()
324                 {
325                         IntPtr region;
326                         Assert.AreEqual (Status.InvalidParameter, GdipCreateRegionRgnData (null, 0, out region));
327
328                         byte[] data = new byte[0];
329                         Assert.AreEqual (Status.GenericError, GdipCreateRegionRgnData (data, 0, out region));
330                 }
331         }
332 }