2006-08-07 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         [StructLayout (LayoutKind.Sequential, CharSet = CharSet.Auto)]
76         public class LOGFONT {
77                 public int lfHeight = 0;
78                 public int lfWidth = 0;
79                 public int lfEscapement = 0;
80                 public int lfOrientation = 0;
81                 public int lfWeight = 0;
82                 public byte lfItalic = 0;
83                 public byte lfUnderline = 0;
84                 public byte lfStrikeOut = 0;
85                 public byte lfCharSet = 0;
86                 public byte lfOutPrecision = 0;
87                 public byte lfClipPrecision = 0;
88                 public byte lfQuality = 0;
89                 public byte lfPitchAndFamily = 0;
90                 [MarshalAs (UnmanagedType.ByValTStr, SizeConst = 32)]
91                 public string lfFaceName = null;
92         }
93
94         [TestFixture]
95         public class GDIPlusTest {
96
97                 // FontFamily
98
99                 [DllImport ("gdiplus.dll", CharSet=CharSet.Auto)]
100                 internal static extern Status GdipCreateFontFamilyFromName (
101                         [MarshalAs (UnmanagedType.LPWStr)] string fName, IntPtr collection, out IntPtr fontFamily);
102
103                 [DllImport ("gdiplus.dll")]
104                 internal static extern Status GdipGetGenericFontFamilySerif (out IntPtr fontFamily);
105
106                 [DllImport ("gdiplus.dll")]
107                 internal static extern Status GdipDeleteFontFamily (IntPtr fontfamily);
108
109                 [Test]
110                 public void DeleteFontFamily ()
111                 {
112                         // invalid image pointer (null)
113                         Assert.AreEqual (Status.InvalidParameter, GdipDeleteFontFamily (IntPtr.Zero), "null");
114
115                         IntPtr font_family;
116                         GdipCreateFontFamilyFromName ("Arial", IntPtr.Zero, out font_family);
117                         Assert.IsTrue (font_family != IntPtr.Zero, "GdipCreateFontFamilyFromName");
118                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "first");
119                 }
120
121                 [Test]
122                 [Category ("NotWorking")]
123                 public void DeleteFontFamily_DoubleDispose ()
124                 {
125                         IntPtr font_family;
126                         GdipGetGenericFontFamilySerif (out font_family);
127                         // first dispose
128                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "first");
129                         // second dispose
130                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "second");
131                 }
132
133                 // Font
134
135                 [DllImport ("gdiplus.dll")]
136                 internal static extern Status GdipCreateFont (IntPtr fontFamily, float emSize, FontStyle style, GraphicsUnit unit, out IntPtr font);
137
138                 [DllImport ("gdiplus.dll", CharSet = CharSet.Auto)]
139                 internal static extern Status GdipGetLogFont (IntPtr font, IntPtr graphics, [MarshalAs (UnmanagedType.AsAny), Out] object logfontA);
140
141                 [DllImport ("gdiplus.dll")]
142                 internal static extern Status GdipDeleteFont (IntPtr font);             
143
144                 [Test]
145                 public void CreateFont ()
146                 {
147                         IntPtr family;
148                         GdipCreateFontFamilyFromName ("Arial", IntPtr.Zero, out family);
149                         Assert.IsTrue (family != IntPtr.Zero, "family");
150
151                         IntPtr font;
152                         Assert.AreEqual (Status.Ok, GdipCreateFont (family, 10f, FontStyle.Regular, GraphicsUnit.Point, out font), "GdipCreateFont");
153                         Assert.IsTrue (font != IntPtr.Zero, "font");
154
155                         LOGFONT lf = new LOGFONT ();
156                         lf.lfCharSet = 1;
157                         Assert.AreEqual (Status.InvalidParameter, GdipGetLogFont (font, IntPtr.Zero, (object) lf), "GdipGetLogFont-null-graphics");
158                         Assert.AreEqual (0, lf.lfCharSet, "lfCharSet-null-graphics");
159
160                         IntPtr image;
161                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
162                         Assert.IsTrue (image != IntPtr.Zero, "image");
163
164                         IntPtr graphics;
165                         GdipGetImageGraphicsContext (image, out graphics);
166                         Assert.IsTrue (graphics != IntPtr.Zero, "graphics");
167
168                         lf.lfCharSet = 1;
169                         Assert.AreEqual (Status.InvalidParameter, GdipGetLogFont (IntPtr.Zero, graphics, (object) lf), "GdipGetLogFont-null");
170                         Assert.AreEqual (0, lf.lfCharSet, "lfCharSet-null");
171
172                         lf.lfCharSet = 1;
173                         Assert.AreEqual (Status.Ok, GdipGetLogFont (font, graphics, (object) lf), "GdipGetLogFont");
174                         Assert.AreEqual (0, lf.lfCharSet, "lfCharSet");
175                         // strangely this is 1 in the managed side
176
177                         Assert.AreEqual (Status.Ok, GdipDeleteFont (font), "GdipDeleteFont");
178                         Assert.AreEqual (Status.InvalidParameter, GdipDeleteFont (IntPtr.Zero), "GdipDeleteFont-null");
179
180                         Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (family), "GdipDeleteFontFamily");
181                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "GdipDisposeImage");
182                         Assert.AreEqual (Status.Ok, GdipDeleteGraphics (graphics), "GdipDeleteGraphics");
183                 }
184
185                 // Bitmap
186
187                 [DllImport ("gdiplus.dll")]
188                 internal static extern Status GdipCreateBitmapFromScan0 (int width, int height, int stride, PixelFormat format, IntPtr scan0, out IntPtr bmp);
189
190                 [Test]
191                 public void CreateBitmapFromScan0 ()
192                 {
193                         IntPtr bmp;
194                         Assert.AreEqual (Status.InvalidParameter, GdipCreateBitmapFromScan0 (-1, 10, 10, PixelFormat.Format32bppArgb, IntPtr.Zero, out bmp), "negative width");
195                 }
196
197                 // Brush
198
199                 [DllImport ("gdiplus.dll")]
200                 static internal extern Status GdipDeleteBrush (IntPtr brush);
201
202                 [Test]
203                 public void DeleteBrush ()
204                 {
205                         Assert.AreEqual (Status.InvalidParameter, GdipDeleteBrush (IntPtr.Zero), "GdipDeleteBrush");
206                 }
207
208                 // Graphics
209
210                 [DllImport ("gdiplus.dll")]
211                 internal static extern Status GdipGetImageGraphicsContext (IntPtr image, out IntPtr graphics);
212
213                 [DllImport ("gdiplus.dll")]
214                 static internal extern Status GdipDeleteGraphics (IntPtr graphics);
215
216                 [Test]
217                 public void Graphics ()
218                 {
219                         IntPtr graphics;
220                         Assert.AreEqual (Status.InvalidParameter, GdipGetImageGraphicsContext (IntPtr.Zero, out graphics), "GdipGetImageGraphicsContext");
221
222                         IntPtr image;
223                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
224                         Assert.IsTrue (image != IntPtr.Zero, "image");
225
226                         Assert.AreEqual (Status.Ok, GdipGetImageGraphicsContext (image, out graphics), "GdipGetImageGraphicsContext");
227                         Assert.IsTrue (graphics != IntPtr.Zero, "graphics");
228
229                         Assert.AreEqual (Status.Ok, GdipDeleteGraphics (graphics), "GdipDeleteGraphics");
230                         Assert.AreEqual (Status.InvalidParameter, GdipDeleteGraphics (IntPtr.Zero), "GdipDeleteGraphics-null");
231
232                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "GdipDisposeImage");
233                 }
234
235                 // GraphicsPath
236
237                 [DllImport ("gdiplus.dll")]
238                 internal static extern Status GdipCreatePath (FillMode brushMode, out IntPtr path);
239
240                 [DllImport ("gdiplus.dll")]
241                 internal static extern Status GdipGetPointCount (IntPtr path, out int count);
242
243                 [DllImport ("gdiplus.dll")]
244                 internal static extern Status GdipGetPathPoints (IntPtr path, [Out] PointF[] points, int count);
245
246                 [DllImport ("gdiplus.dll")]
247                 internal static extern Status GdipGetPathTypes (IntPtr path, [Out] byte[] types, int count);
248
249                 [DllImport ("gdiplus.dll")]                
250                 internal static extern Status GdipDeletePath (IntPtr path);
251
252                 [Test]
253                 public void GetPointCount_Zero ()
254                 {
255                         IntPtr path;
256                         Assert.AreEqual (Status.Ok, GdipCreatePath (FillMode.Alternate, out path), "GdipCreatePath");
257                         Assert.IsTrue (path != IntPtr.Zero, "Handle");
258
259                         int count;
260                         Assert.AreEqual (Status.Ok, GdipGetPointCount (path, out count), "GdipGetPointCount");
261                         Assert.AreEqual (0, count, "Count");
262
263                         PointF[] points = new PointF[count];
264                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathPoints (path, points, count), "GdipGetPathPoints");
265                         // can't get the points if the count is zero!
266
267                         byte[] types = new byte[count];
268                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathTypes (path, types, count), "GdipGetPathTypes");
269                         // can't get the types if the count is zero!
270
271                         Assert.AreEqual (Status.Ok, GdipDeletePath (path), "GdipDeletePath");
272                         Assert.AreEqual (Status.InvalidParameter, GdipDeletePath (IntPtr.Zero), "GdipDeletePath-null");
273                 }
274
275                 // Image
276
277                 [DllImport ("gdiplus.dll")]
278                 internal static extern Status GdipDisposeImage (IntPtr image);
279
280                 [Test]
281                 public void DisposeImage ()
282                 {
283                         // invalid image pointer (null)
284                         Assert.AreEqual (Status.InvalidParameter, GdipDisposeImage (IntPtr.Zero), "null");
285
286                         IntPtr image;
287                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
288                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
289                 }
290
291                 [Test]
292                 [Category ("NotWorking")]
293                 public void DisposeImage_Dual ()
294                 {
295                         IntPtr image;
296                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
297                         // first dispose
298                         Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
299                         // second dispose
300                         Assert.AreEqual (Status.ObjectBusy, GdipDisposeImage (image), "second");
301                 }
302
303
304                 [DllImport ("gdiplus.dll")]
305                 internal static extern Status GdipGetImageThumbnail (IntPtr image, uint width, uint height, out IntPtr thumbImage, IntPtr callback, IntPtr callBackData);
306
307                 [Test]
308                 [Category ("NotWorking")] // libgdiplus doesn't implement GdipGetImageThumbnail (it is done inside S.D)
309                 public void GetImageThumbnail ()
310                 {
311                         IntPtr ptr;
312
313                         // invalid image pointer (null)
314                         Assert.AreEqual (Status.InvalidParameter, GdipGetImageThumbnail (IntPtr.Zero, 10, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
315
316                         IntPtr image;
317                         GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
318                         try {
319                                 // invalid width (0)
320                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 0, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
321                                 // invalid width (negative)
322                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 0x8000000, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
323                                 // invalid height (0)
324                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 10, 0, out ptr, IntPtr.Zero, IntPtr.Zero));
325                                 // invalid height (negative)
326                                 Assert.AreEqual (Status.OutOfMemory, GdipGetImageThumbnail (image, 10, 0x8000000, out ptr, IntPtr.Zero, IntPtr.Zero));
327                         }
328                         finally {
329                                 GdipDisposeImage (image);
330                         }
331                 }
332
333                 // PathGradientBrush
334
335                 [DllImport ("gdiplus.dll")]
336                 static internal extern Status GdipCreatePathGradient (PointF[] points, int count, WrapMode wrapMode, out IntPtr brush);
337
338                 [DllImport ("gdiplus.dll")]
339                 static internal extern Status GdipGetPathGradientBlendCount (IntPtr brush, out int count);
340
341                 [DllImport ("gdiplus.dll")]
342                 static internal extern Status GdipGetPathGradientPresetBlend (IntPtr brush, int[] blend, float[] positions, int count);
343
344
345                 [Test]
346                 public void CreatePathGradient ()
347                 {
348                         PointF[] points = null;
349                         IntPtr brush;
350                         Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "null");
351
352                         points = new PointF [0];
353                         Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "empty");
354
355                         points = new PointF[1];
356                         Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 1, WrapMode.Clamp, out brush), "one");
357
358                         points = new PointF[2] { new PointF (1, 2), new PointF (20, 30) };
359                         Assert.AreEqual (Status.Ok, GdipCreatePathGradient (points, 2, WrapMode.Clamp, out brush), "two");
360
361                         int count;
362                         Assert.AreEqual (Status.Ok, GdipGetPathGradientBlendCount (brush, out count), "GdipGetPathGradientBlendCount");
363                         Assert.AreEqual (1, count, "blend count");
364
365                         int[] colors = new int[count];
366                         float[] positions = new float[count];
367                         Assert.AreEqual (Status.InvalidParameter, GdipGetPathGradientPresetBlend (brush, colors, positions, count), "GdipGetPathGradientBlend");
368                         // can't call that for 1 count!
369
370                         Assert.AreEqual (Status.Ok, GdipDeleteBrush (brush), "GdipDeleteBrush");
371                 }
372
373                 // Pen
374
375                 [DllImport ("gdiplus.dll")]
376                 internal static extern Status GdipCreatePen1 (int argb, float width, Unit unit, out IntPtr pen);
377
378                 [DllImport ("gdiplus.dll")]
379                 internal static extern Status GdipGetPenDashStyle (IntPtr pen, out DashStyle dashStyle);
380
381                 [DllImport ("gdiplus.dll")]
382                 internal static extern Status GdipSetPenDashStyle (IntPtr pen, DashStyle dashStyle);
383
384                 [DllImport ("gdiplus.dll")]
385                 internal static extern Status GdipGetPenDashCount (IntPtr pen, out int count);
386
387                 [DllImport ("gdiplus.dll")]
388                 internal static extern Status GdipGetPenDashArray (IntPtr pen, float[] dash, int count);
389
390                 [DllImport ("gdiplus.dll")]
391                 internal static extern Status GdipDeletePen (IntPtr pen);
392
393                 [Test]
394                 public void CreatePen ()
395                 {
396                         IntPtr pen;
397                         Assert.AreEqual (Status.Ok, GdipCreatePen1 (0, 0f, Unit.UnitWorld, out pen), "GdipCreatePen1");
398                         Assert.IsTrue (pen != IntPtr.Zero, "pen");
399
400                         DashStyle ds;
401                         Assert.AreEqual (Status.Ok, GdipGetPenDashStyle (pen, out ds), "GdipGetPenDashStyle");
402                         Assert.AreEqual (Status.InvalidParameter, GdipGetPenDashStyle (IntPtr.Zero, out ds), "GdipGetPenDashStyle-null");
403
404                         ds = DashStyle.Custom;
405                         Assert.AreEqual (Status.Ok, GdipSetPenDashStyle (pen, ds), "GdipSetPenDashStyle");
406                         Assert.AreEqual (Status.InvalidParameter, GdipSetPenDashStyle (IntPtr.Zero, ds), "GdipSetPenDashStyle-null");
407
408                         int count;
409                         Assert.AreEqual (Status.Ok, GdipGetPenDashCount (pen, out count), "GdipGetPenDashCount");
410                         Assert.AreEqual (Status.InvalidParameter, GdipGetPenDashCount (IntPtr.Zero, out count), "GdipGetPenDashCount-null");
411                         Assert.AreEqual (0, count, "count");
412
413                         float[] dash = new float[count];
414                         Assert.AreEqual (Status.OutOfMemory, GdipGetPenDashArray (pen, dash, count), "GdipGetPenDashArray");
415                         Assert.AreEqual (Status.InvalidParameter, GdipGetPenDashArray (IntPtr.Zero, dash, count), "GdipGetPenDashArray-null-pen");
416                         Assert.AreEqual (Status.InvalidParameter, GdipGetPenDashArray (pen, null, count), "GdipGetPenDashArray-null-dash");
417
418                         Assert.AreEqual (Status.Ok, GdipDeletePen (pen), "GdipDeletePen");
419                         Assert.AreEqual (Status.InvalidParameter, GdipDeletePen (IntPtr.Zero), "GdipDeletePen-null");
420                 }
421
422                 // Region
423
424                 [DllImport ("gdiplus.dll")]
425                 static internal extern Status GdipCreateRegionRgnData (byte[] data, int size, out IntPtr region);
426
427                 [Test]
428                 public void CreateRegionRgnData ()
429                 {
430                         IntPtr region;
431                         Assert.AreEqual (Status.InvalidParameter, GdipCreateRegionRgnData (null, 0, out region));
432
433                         byte[] data = new byte[0];
434                         Assert.AreEqual (Status.GenericError, GdipCreateRegionRgnData (data, 0, out region));
435                 }
436         }
437 }