2 // Direct GDI+ API unit tests
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Drawing.Drawing2D;
32 using System.Drawing.Imaging;
33 using System.Runtime.InteropServices;
34 using NUnit.Framework;
36 namespace MonoTests.System.Drawing {
38 // copied from Mono's System.Drawing.dll gdiEnums.cs
39 internal enum Status {
45 InsufficientBuffer = 5,
53 UnknownImageFormat = 13,
54 FontFamilyNotFound = 14,
55 FontStyleNotFound = 15,
57 UnsupportedGdiplusVersion = 17,
58 GdiplusNotInitialized = 18,
59 PropertyNotFound = 19,
60 PropertyNotSupported = 20,
64 // copied from Mono's System.Drawing.dll gdiEnums.cs
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;
95 public class GDIPlusTest {
99 [DllImport ("gdiplus.dll", CharSet=CharSet.Auto)]
100 internal static extern Status GdipCreateFontFamilyFromName (
101 [MarshalAs (UnmanagedType.LPWStr)] string fName, IntPtr collection, out IntPtr fontFamily);
103 [DllImport ("gdiplus.dll")]
104 internal static extern Status GdipGetGenericFontFamilySerif (out IntPtr fontFamily);
106 [DllImport ("gdiplus.dll")]
107 internal static extern Status GdipDeleteFontFamily (IntPtr fontfamily);
110 public void DeleteFontFamily ()
112 // invalid image pointer (null)
113 Assert.AreEqual (Status.InvalidParameter, GdipDeleteFontFamily (IntPtr.Zero), "null");
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");
122 [Category ("NotWorking")]
123 public void DeleteFontFamily_DoubleDispose ()
126 GdipGetGenericFontFamilySerif (out font_family);
128 Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "first");
130 Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "second");
135 [DllImport ("gdiplus.dll")]
136 internal static extern Status GdipCreateFont (IntPtr fontFamily, float emSize, FontStyle style, GraphicsUnit unit, out IntPtr font);
138 [DllImport ("gdiplus.dll", CharSet = CharSet.Auto)]
139 internal static extern Status GdipGetLogFont (IntPtr font, IntPtr graphics, [MarshalAs (UnmanagedType.AsAny), Out] object logfontA);
141 [DllImport ("gdiplus.dll")]
142 internal static extern Status GdipDeleteFont (IntPtr font);
145 public void CreateFont ()
148 GdipCreateFontFamilyFromName ("Arial", IntPtr.Zero, out family);
149 Assert.IsTrue (family != IntPtr.Zero, "family");
152 Assert.AreEqual (Status.Ok, GdipCreateFont (family, 10f, FontStyle.Regular, GraphicsUnit.Point, out font), "GdipCreateFont");
153 Assert.IsTrue (font != IntPtr.Zero, "font");
155 LOGFONT lf = new LOGFONT ();
157 Assert.AreEqual (Status.InvalidParameter, GdipGetLogFont (font, IntPtr.Zero, (object) lf), "GdipGetLogFont-null-graphics");
158 Assert.AreEqual (0, lf.lfCharSet, "lfCharSet-null-graphics");
161 GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
162 Assert.IsTrue (image != IntPtr.Zero, "image");
165 GdipGetImageGraphicsContext (image, out graphics);
166 Assert.IsTrue (graphics != IntPtr.Zero, "graphics");
169 Assert.AreEqual (Status.InvalidParameter, GdipGetLogFont (IntPtr.Zero, graphics, (object) lf), "GdipGetLogFont-null");
170 Assert.AreEqual (0, lf.lfCharSet, "lfCharSet-null");
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
177 Assert.AreEqual (Status.Ok, GdipDeleteFont (font), "GdipDeleteFont");
178 Assert.AreEqual (Status.InvalidParameter, GdipDeleteFont (IntPtr.Zero), "GdipDeleteFont-null");
180 Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (family), "GdipDeleteFontFamily");
181 Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "GdipDisposeImage");
182 Assert.AreEqual (Status.Ok, GdipDeleteGraphics (graphics), "GdipDeleteGraphics");
187 [DllImport ("gdiplus.dll")]
188 internal static extern Status GdipCreateBitmapFromScan0 (int width, int height, int stride, PixelFormat format, IntPtr scan0, out IntPtr bmp);
191 public void CreateBitmapFromScan0 ()
194 Assert.AreEqual (Status.InvalidParameter, GdipCreateBitmapFromScan0 (-1, 10, 10, PixelFormat.Format32bppArgb, IntPtr.Zero, out bmp), "negative width");
199 [DllImport ("gdiplus.dll")]
200 static internal extern Status GdipDeleteBrush (IntPtr brush);
203 public void DeleteBrush ()
205 Assert.AreEqual (Status.InvalidParameter, GdipDeleteBrush (IntPtr.Zero), "GdipDeleteBrush");
210 [DllImport ("gdiplus.dll")]
211 internal static extern Status GdipGetImageGraphicsContext (IntPtr image, out IntPtr graphics);
213 [DllImport ("gdiplus.dll")]
214 static internal extern Status GdipDeleteGraphics (IntPtr graphics);
217 public void Graphics ()
220 Assert.AreEqual (Status.InvalidParameter, GdipGetImageGraphicsContext (IntPtr.Zero, out graphics), "GdipGetImageGraphicsContext");
223 GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
224 Assert.IsTrue (image != IntPtr.Zero, "image");
226 Assert.AreEqual (Status.Ok, GdipGetImageGraphicsContext (image, out graphics), "GdipGetImageGraphicsContext");
227 Assert.IsTrue (graphics != IntPtr.Zero, "graphics");
229 Assert.AreEqual (Status.Ok, GdipDeleteGraphics (graphics), "GdipDeleteGraphics");
230 Assert.AreEqual (Status.InvalidParameter, GdipDeleteGraphics (IntPtr.Zero), "GdipDeleteGraphics-null");
232 Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "GdipDisposeImage");
237 [DllImport ("gdiplus.dll")]
238 internal static extern Status GdipCreatePath (FillMode brushMode, out IntPtr path);
240 [DllImport ("gdiplus.dll")]
241 internal static extern Status GdipGetPointCount (IntPtr path, out int count);
243 [DllImport ("gdiplus.dll")]
244 internal static extern Status GdipGetPathPoints (IntPtr path, [Out] PointF[] points, int count);
246 [DllImport ("gdiplus.dll")]
247 internal static extern Status GdipGetPathTypes (IntPtr path, [Out] byte[] types, int count);
249 [DllImport ("gdiplus.dll")]
250 internal static extern Status GdipDeletePath (IntPtr path);
253 public void GetPointCount_Zero ()
256 Assert.AreEqual (Status.Ok, GdipCreatePath (FillMode.Alternate, out path), "GdipCreatePath");
257 Assert.IsTrue (path != IntPtr.Zero, "Handle");
260 Assert.AreEqual (Status.Ok, GdipGetPointCount (path, out count), "GdipGetPointCount");
261 Assert.AreEqual (0, count, "Count");
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!
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!
271 Assert.AreEqual (Status.Ok, GdipDeletePath (path), "GdipDeletePath");
272 Assert.AreEqual (Status.InvalidParameter, GdipDeletePath (IntPtr.Zero), "GdipDeletePath-null");
277 [DllImport ("gdiplus.dll")]
278 internal static extern Status GdipDisposeImage (IntPtr image);
281 public void DisposeImage ()
283 // invalid image pointer (null)
284 Assert.AreEqual (Status.InvalidParameter, GdipDisposeImage (IntPtr.Zero), "null");
287 GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
288 Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
292 [Category ("NotWorking")]
293 public void DisposeImage_Dual ()
296 GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
298 Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
300 Assert.AreEqual (Status.ObjectBusy, GdipDisposeImage (image), "second");
304 [DllImport ("gdiplus.dll")]
305 internal static extern Status GdipGetImageThumbnail (IntPtr image, uint width, uint height, out IntPtr thumbImage, IntPtr callback, IntPtr callBackData);
308 [Category ("NotWorking")] // libgdiplus doesn't implement GdipGetImageThumbnail (it is done inside S.D)
309 public void GetImageThumbnail ()
313 // invalid image pointer (null)
314 Assert.AreEqual (Status.InvalidParameter, GdipGetImageThumbnail (IntPtr.Zero, 10, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
317 GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
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));
329 GdipDisposeImage (image);
335 [DllImport ("gdiplus.dll")]
336 static internal extern Status GdipCreatePathGradient (PointF[] points, int count, WrapMode wrapMode, out IntPtr brush);
338 [DllImport ("gdiplus.dll")]
339 static internal extern Status GdipGetPathGradientBlendCount (IntPtr brush, out int count);
341 [DllImport ("gdiplus.dll")]
342 static internal extern Status GdipGetPathGradientPresetBlend (IntPtr brush, int[] blend, float[] positions, int count);
346 public void CreatePathGradient ()
348 PointF[] points = null;
350 Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "null");
352 points = new PointF [0];
353 Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "empty");
355 points = new PointF[1];
356 Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 1, WrapMode.Clamp, out brush), "one");
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");
362 Assert.AreEqual (Status.Ok, GdipGetPathGradientBlendCount (brush, out count), "GdipGetPathGradientBlendCount");
363 Assert.AreEqual (1, count, "blend count");
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!
370 Assert.AreEqual (Status.Ok, GdipDeleteBrush (brush), "GdipDeleteBrush");
375 [DllImport ("gdiplus.dll")]
376 internal static extern Status GdipCreatePen1 (int argb, float width, Unit unit, out IntPtr pen);
378 [DllImport ("gdiplus.dll")]
379 internal static extern Status GdipGetPenDashStyle (IntPtr pen, out DashStyle dashStyle);
381 [DllImport ("gdiplus.dll")]
382 internal static extern Status GdipSetPenDashStyle (IntPtr pen, DashStyle dashStyle);
384 [DllImport ("gdiplus.dll")]
385 internal static extern Status GdipGetPenDashCount (IntPtr pen, out int count);
387 [DllImport ("gdiplus.dll")]
388 internal static extern Status GdipGetPenDashArray (IntPtr pen, float[] dash, int count);
390 [DllImport ("gdiplus.dll")]
391 internal static extern Status GdipDeletePen (IntPtr pen);
394 public void CreatePen ()
397 Assert.AreEqual (Status.Ok, GdipCreatePen1 (0, 0f, Unit.UnitWorld, out pen), "GdipCreatePen1");
398 Assert.IsTrue (pen != IntPtr.Zero, "pen");
401 Assert.AreEqual (Status.Ok, GdipGetPenDashStyle (pen, out ds), "GdipGetPenDashStyle");
402 Assert.AreEqual (Status.InvalidParameter, GdipGetPenDashStyle (IntPtr.Zero, out ds), "GdipGetPenDashStyle-null");
404 ds = DashStyle.Custom;
405 Assert.AreEqual (Status.Ok, GdipSetPenDashStyle (pen, ds), "GdipSetPenDashStyle");
406 Assert.AreEqual (Status.InvalidParameter, GdipSetPenDashStyle (IntPtr.Zero, ds), "GdipSetPenDashStyle-null");
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");
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");
418 Assert.AreEqual (Status.Ok, GdipDeletePen (pen), "GdipDeletePen");
419 Assert.AreEqual (Status.InvalidParameter, GdipDeletePen (IntPtr.Zero), "GdipDeletePen-null");
424 [DllImport ("gdiplus.dll")]
425 static internal extern Status GdipCreateRegionRgnData (byte[] data, int size, out IntPtr region);
428 public void CreateRegionRgnData ()
431 Assert.AreEqual (Status.InvalidParameter, GdipCreateRegionRgnData (null, 0, out region));
433 byte[] data = new byte[0];
434 Assert.AreEqual (Status.GenericError, GdipCreateRegionRgnData (data, 0, out region));