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,
65 public class GDIPlusTest {
69 [DllImport ("gdiplus.dll", CharSet=CharSet.Auto)]
70 internal static extern Status GdipCreateFontFamilyFromName (
71 [MarshalAs (UnmanagedType.LPWStr)] string fName, IntPtr collection, out IntPtr fontFamily);
73 [DllImport ("gdiplus.dll")]
74 internal static extern Status GdipGetGenericFontFamilySerif (out IntPtr fontFamily);
76 [DllImport ("gdiplus.dll")]
77 internal static extern Status GdipDeleteFontFamily (IntPtr fontfamily);
80 public void DeleteFontFamily ()
82 // invalid image pointer (null)
83 Assert.AreEqual (Status.InvalidParameter, GdipDeleteFontFamily (IntPtr.Zero), "null");
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");
92 [Category ("NotWorking")]
93 public void DeleteFontFamily_DoubleDispose ()
96 GdipGetGenericFontFamilySerif (out font_family);
98 Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "first");
100 Assert.AreEqual (Status.Ok, GdipDeleteFontFamily (font_family), "second");
105 [DllImport ("gdiplus.dll")]
106 internal static extern Status GdipCreateBitmapFromScan0 (int width, int height, int stride, PixelFormat format, IntPtr scan0, out IntPtr bmp);
109 public void CreateBitmapFromScan0 ()
112 Assert.AreEqual (Status.InvalidParameter, GdipCreateBitmapFromScan0 (-1, 10, 10, PixelFormat.Format32bppArgb, IntPtr.Zero, out bmp), "negative width");
117 [DllImport ("gdiplus.dll")]
118 static internal extern Status GdipDeleteBrush (IntPtr brush);
121 public void DeleteBrush ()
123 Assert.AreEqual (Status.InvalidParameter, GdipDeleteBrush (IntPtr.Zero), "GdipDeleteBrush");
128 [DllImport ("gdiplus.dll")]
129 internal static extern Status GdipCreatePath (FillMode brushMode, out IntPtr path);
131 [DllImport ("gdiplus.dll")]
132 internal static extern Status GdipGetPointCount (IntPtr path, out int count);
134 [DllImport ("gdiplus.dll")]
135 internal static extern Status GdipGetPathPoints (IntPtr path, [Out] PointF[] points, int count);
137 [DllImport ("gdiplus.dll")]
138 internal static extern Status GdipGetPathTypes (IntPtr path, [Out] byte[] types, int count);
141 public void GetPointCount_Zero ()
144 Assert.AreEqual (Status.Ok, GdipCreatePath (FillMode.Alternate, out path), "GdipCreatePath");
145 Assert.IsTrue (path != IntPtr.Zero, "Handle");
148 Assert.AreEqual (Status.Ok, GdipGetPointCount (path, out count), "GdipGetPointCount");
149 Assert.AreEqual (0, count, "Count");
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!
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!
162 [DllImport ("gdiplus.dll")]
163 internal static extern Status GdipDisposeImage (IntPtr image);
166 public void DisposeImage ()
168 // invalid image pointer (null)
169 Assert.AreEqual (Status.InvalidParameter, GdipDisposeImage (IntPtr.Zero), "null");
172 GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
173 Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
177 [Category ("NotWorking")]
178 public void DisposeImage_Dual ()
181 GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
183 Assert.AreEqual (Status.Ok, GdipDisposeImage (image), "first");
185 Assert.AreEqual (Status.ObjectBusy, GdipDisposeImage (image), "second");
189 [DllImport ("gdiplus.dll")]
190 internal static extern Status GdipGetImageThumbnail (IntPtr image, uint width, uint height, out IntPtr thumbImage, IntPtr callback, IntPtr callBackData);
193 [Category ("NotWorking")] // libgdiplus doesn't implement GdipGetImageThumbnail (it is done inside S.D)
194 public void GetImageThumbnail ()
198 // invalid image pointer (null)
199 Assert.AreEqual (Status.InvalidParameter, GdipGetImageThumbnail (IntPtr.Zero, 10, 10, out ptr, IntPtr.Zero, IntPtr.Zero));
202 GdipCreateBitmapFromScan0 (10, 10, 0, PixelFormat.Format32bppArgb, IntPtr.Zero, out image);
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));
214 GdipDisposeImage (image);
220 [DllImport ("gdiplus.dll")]
221 static internal extern Status GdipCreatePathGradient (PointF[] points, int count, WrapMode wrapMode, out IntPtr brush);
223 [DllImport ("gdiplus.dll")]
224 static internal extern Status GdipGetPathGradientBlendCount (IntPtr brush, out int count);
226 [DllImport ("gdiplus.dll")]
227 static internal extern Status GdipGetPathGradientPresetBlend (IntPtr brush, int[] blend, float[] positions, int count);
231 public void CreatePathGradient ()
233 PointF[] points = null;
235 Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "null");
237 points = new PointF [0];
238 Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 0, WrapMode.Clamp, out brush), "empty");
240 points = new PointF[1];
241 Assert.AreEqual (Status.OutOfMemory, GdipCreatePathGradient (points, 1, WrapMode.Clamp, out brush), "one");
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");
247 Assert.AreEqual (Status.Ok, GdipGetPathGradientBlendCount (brush, out count), "GdipGetPathGradientBlendCount");
248 Assert.AreEqual (1, count, "blend count");
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!
255 Assert.AreEqual (Status.Ok, GdipDeleteBrush (brush), "GdipDeleteBrush");
261 [DllImport ("gdiplus.dll")]
262 static internal extern Status GdipCreateRegionRgnData (byte[] data, int size, out IntPtr region);
265 public void CreateRegionRgnData ()
268 Assert.AreEqual (Status.InvalidParameter, GdipCreateRegionRgnData (null, 0, out region));
270 byte[] data = new byte[0];
271 Assert.AreEqual (Status.GenericError, GdipCreateRegionRgnData (data, 0, out region));