1 /*
2  * Unit test suite for customlinecap
3  *
4  * Copyright (C) 2008 Nikolay Sivov
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "precomp.h"
22 
23 #define expect(expected, got) ok(got == expected, "Expected %.8x, got %.8x\n", expected, got)
24 #define expectf(expected, got) ok(got == expected, "Expected %.2f, got %.2f\n", expected, got)
25 
26 static void test_constructor_destructor(void)
27 {
28     GpCustomLineCap *custom;
29     GpPath *path, *path2;
30     GpStatus stat;
31 
32     stat = GdipCreatePath(FillModeAlternate, &path);
33     expect(Ok, stat);
34     stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
35     expect(Ok, stat);
36 
37     stat = GdipCreatePath(FillModeAlternate, &path2);
38     expect(Ok, stat);
39     stat = GdipAddPathRectangle(path2, 5.0, 5.0, 10.0, 10.0);
40     expect(Ok, stat);
41 
42     /* NULL args */
43     stat = GdipCreateCustomLineCap(NULL, NULL, LineCapFlat, 0.0, NULL);
44     expect(InvalidParameter, stat);
45     stat = GdipCreateCustomLineCap(path, NULL, LineCapFlat, 0.0, NULL);
46     expect(InvalidParameter, stat);
47     stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, NULL);
48     expect(InvalidParameter, stat);
49     stat = GdipCreateCustomLineCap(NULL, NULL, LineCapFlat, 0.0, &custom);
50     expect(InvalidParameter, stat);
51     stat = GdipDeleteCustomLineCap(NULL);
52     expect(InvalidParameter, stat);
53 
54     /* valid args */
55     stat = GdipCreateCustomLineCap(NULL, path2, LineCapFlat, 0.0, &custom);
56     expect(Ok, stat);
57     stat = GdipDeleteCustomLineCap(custom);
58     expect(Ok, stat);
59     /* it's strange but native returns NotImplemented on stroke == NULL */
60     custom = NULL;
61     stat = GdipCreateCustomLineCap(path, NULL, LineCapFlat, 10.0, &custom);
62     todo_wine expect(NotImplemented, stat);
63     todo_wine ok(custom == NULL, "Expected a failure on creation\n");
64     if(stat == Ok) GdipDeleteCustomLineCap(custom);
65 
66     GdipDeletePath(path2);
67     GdipDeletePath(path);
68 }
69 
70 static void test_linejoin(void)
71 {
72     GpCustomLineCap *custom;
73     GpPath *path;
74     GpLineJoin join;
75     GpStatus stat;
76 
77     stat = GdipCreatePath(FillModeAlternate, &path);
78     expect(Ok, stat);
79     stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
80     expect(Ok, stat);
81 
82     stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, &custom);
83     expect(Ok, stat);
84 
85     /* NULL args */
86     stat = GdipGetCustomLineCapStrokeJoin(NULL, NULL);
87     expect(InvalidParameter, stat);
88     stat = GdipGetCustomLineCapStrokeJoin(custom, NULL);
89     expect(InvalidParameter, stat);
90     stat = GdipGetCustomLineCapStrokeJoin(NULL, &join);
91     expect(InvalidParameter, stat);
92     stat = GdipSetCustomLineCapStrokeJoin(NULL, LineJoinBevel);
93     expect(InvalidParameter, stat);
94 
95     /* LineJoinMiter is default */
96     stat = GdipGetCustomLineCapStrokeJoin(custom, &join);
97     expect(Ok, stat);
98     expect(LineJoinMiter, join);
99 
100     /* set/get */
101     stat = GdipSetCustomLineCapStrokeJoin(custom, LineJoinBevel);
102     expect(Ok, stat);
103     stat = GdipGetCustomLineCapStrokeJoin(custom, &join);
104     expect(Ok, stat);
105     expect(LineJoinBevel, join);
106     stat = GdipSetCustomLineCapStrokeJoin(custom, LineJoinRound);
107     expect(Ok, stat);
108     stat = GdipGetCustomLineCapStrokeJoin(custom, &join);
109     expect(Ok, stat);
110     expect(LineJoinRound, join);
111     stat = GdipSetCustomLineCapStrokeJoin(custom, LineJoinMiterClipped);
112     expect(Ok, stat);
113     stat = GdipGetCustomLineCapStrokeJoin(custom, &join);
114     expect(Ok, stat);
115     expect(LineJoinMiterClipped, join);
116 
117     GdipDeleteCustomLineCap(custom);
118     GdipDeletePath(path);
119 }
120 
121 static void test_inset(void)
122 {
123     GpCustomLineCap *custom;
124     GpPath *path;
125     REAL inset;
126     GpStatus stat;
127 
128     stat = GdipCreatePath(FillModeAlternate, &path);
129     expect(Ok, stat);
130     stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
131     expect(Ok, stat);
132 
133     stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, &custom);
134     expect(Ok, stat);
135 
136     /* NULL args */
137     stat = GdipGetCustomLineCapBaseInset(NULL, NULL);
138     expect(InvalidParameter, stat);
139     stat = GdipGetCustomLineCapBaseInset(NULL, &inset);
140     expect(InvalidParameter, stat);
141     stat = GdipGetCustomLineCapBaseInset(custom, NULL);
142     expect(InvalidParameter, stat);
143     /* valid args */
144     inset = (REAL)0xdeadbeef;
145     stat = GdipGetCustomLineCapBaseInset(custom, &inset);
146     expect(Ok, stat);
147     expectf(0.0, inset);
148 
149     GdipDeleteCustomLineCap(custom);
150     GdipDeletePath(path);
151 }
152 
153 static void test_scale(void)
154 {
155     GpCustomLineCap *custom;
156     GpPath *path;
157     REAL scale;
158     GpStatus stat;
159 
160     stat = GdipCreatePath(FillModeAlternate, &path);
161     expect(Ok, stat);
162     stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
163     expect(Ok, stat);
164 
165     stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, &custom);
166     expect(Ok, stat);
167 
168     /* NULL args */
169     stat = GdipGetCustomLineCapWidthScale(NULL, NULL);
170     expect(InvalidParameter, stat);
171     stat = GdipGetCustomLineCapWidthScale(NULL, &scale);
172     expect(InvalidParameter, stat);
173     stat = GdipGetCustomLineCapWidthScale(custom, NULL);
174     expect(InvalidParameter, stat);
175 
176     stat = GdipSetCustomLineCapWidthScale(NULL, 2.0);
177     expect(InvalidParameter, stat);
178 
179     /* valid args: read default */
180     scale = (REAL)0xdeadbeef;
181     stat = GdipGetCustomLineCapWidthScale(custom, &scale);
182     expect(Ok, stat);
183     expectf(1.0, scale);
184 
185     /* set and read back some scale values: there is no limit for the scale */
186     stat = GdipSetCustomLineCapWidthScale(custom, 2.5);
187     expect(Ok, stat);
188     scale = (REAL)0xdeadbeef;
189     stat = GdipGetCustomLineCapWidthScale(custom, &scale);
190     expect(Ok, stat);
191     expectf(2.5, scale);
192 
193     stat = GdipSetCustomLineCapWidthScale(custom, 42.0);
194     expect(Ok, stat);
195     scale = (REAL)0xdeadbeef;
196     stat = GdipGetCustomLineCapWidthScale(custom, &scale);
197     expect(Ok, stat);
198     expectf(42.0, scale);
199 
200     stat = GdipSetCustomLineCapWidthScale(custom, 3000.0);
201     expect(Ok, stat);
202     scale = (REAL)0xdeadbeef;
203     stat = GdipGetCustomLineCapWidthScale(custom, &scale);
204     expect(Ok, stat);
205     expectf(3000.0, scale);
206 
207     stat = GdipSetCustomLineCapWidthScale(custom, 0.0);
208     expect(Ok, stat);
209     scale = (REAL)0xdeadbeef;
210     stat = GdipGetCustomLineCapWidthScale(custom, &scale);
211     expect(Ok, stat);
212     expectf(0.0, scale);
213 
214     GdipDeleteCustomLineCap(custom);
215     GdipDeletePath(path);
216 }
217 
218 static void test_create_adjustable_cap(void)
219 {
220     GpAdjustableArrowCap *cap;
221     REAL inset, scale;
222     GpLineJoin join;
223     GpStatus stat;
224     GpLineCap base;
225 
226     stat = GdipCreateAdjustableArrowCap(10.0, 10.0, TRUE, NULL);
227 todo_wine
228     ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);
229 
230     stat = GdipCreateAdjustableArrowCap(17.0, 15.0, TRUE, &cap);
231 todo_wine
232     ok(stat == Ok, "Failed to create adjustable cap, %d\n", stat);
233     if (stat != Ok)
234         return;
235 
236     stat = GdipGetAdjustableArrowCapMiddleInset(cap, NULL);
237     ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);
238 
239     stat = GdipGetAdjustableArrowCapMiddleInset(cap, &inset);
240     ok(stat == Ok, "Unexpected return code, %d\n", stat);
241     ok(inset == 0.0f, "Unexpected middle inset %f\n", inset);
242 
243     stat = GdipGetCustomLineCapBaseCap((GpCustomLineCap*)cap, &base);
244     ok(stat == Ok, "Unexpected return code, %d\n", stat);
245     ok(base == LineCapTriangle, "Unexpected base cap %d\n", base);
246 
247     stat = GdipSetCustomLineCapBaseCap((GpCustomLineCap*)cap, LineCapSquare);
248     ok(stat == Ok, "Unexpected return code, %d\n", stat);
249 
250     stat = GdipGetCustomLineCapBaseCap((GpCustomLineCap*)cap, &base);
251     ok(stat == Ok, "Unexpected return code, %d\n", stat);
252     ok(base == LineCapSquare, "Unexpected base cap %d\n", base);
253 
254     stat = GdipGetCustomLineCapBaseInset((GpCustomLineCap*)cap, &inset);
255     ok(stat == Ok, "Unexpected return code, %d\n", stat);
256 
257     stat = GdipGetCustomLineCapWidthScale((GpCustomLineCap*)cap, &scale);
258     ok(stat == Ok, "Unexpected return code, %d\n", stat);
259     ok(scale == 1.0f, "Unexpected width scale %f\n", scale);
260 
261     stat = GdipGetCustomLineCapStrokeJoin((GpCustomLineCap*)cap, &join);
262     ok(stat == Ok, "Unexpected return code, %d\n", stat);
263     ok(join == LineJoinMiter, "Unexpected stroke join %d\n", join);
264 
265     GdipDeleteCustomLineCap((GpCustomLineCap*)cap);
266 }
267 
268 static void test_captype(void)
269 {
270     GpAdjustableArrowCap *arrowcap;
271     GpCustomLineCap *custom;
272     CustomLineCapType type;
273     GpStatus stat;
274     GpPath *path;
275 
276     stat = GdipGetCustomLineCapType(NULL, NULL);
277     ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);
278 
279     type = 10;
280     stat = GdipGetCustomLineCapType(NULL, &type);
281     ok(stat == InvalidParameter, "Unexpected return code, %d\n", stat);
282     ok(type == 10, "Unexpected cap type, %d\n", type);
283 
284     /* default cap */
285     stat = GdipCreatePath(FillModeAlternate, &path);
286     ok(stat == Ok, "Failed to create path, %d\n", stat);
287     stat = GdipAddPathRectangle(path, 5.0, 5.0, 10.0, 10.0);
288     ok(stat == Ok, "AddPathRectangle failed, %d\n", stat);
289 
290     stat = GdipCreateCustomLineCap(NULL, path, LineCapFlat, 0.0, &custom);
291     ok(stat == Ok, "Failed to create cap, %d\n", stat);
292     stat = GdipGetCustomLineCapType(custom, &type);
293     ok(stat == Ok, "Failed to get cap type, %d\n", stat);
294     ok(type == CustomLineCapTypeDefault, "Unexpected cap type %d\n", stat);
295     GdipDeleteCustomLineCap(custom);
296     GdipDeletePath(path);
297 
298     /* arrow cap */
299     stat = GdipCreateAdjustableArrowCap(17.0, 15.0, TRUE, &arrowcap);
300 todo_wine
301     ok(stat == Ok, "Failed to create adjustable cap, %d\n", stat);
302     if (stat != Ok)
303         return;
304 
305     stat = GdipGetCustomLineCapType((GpCustomLineCap*)arrowcap, &type);
306     ok(stat == Ok, "Failed to get cap type, %d\n", stat);
307     ok(type == CustomLineCapTypeAdjustableArrow, "Unexpected cap type %d\n", stat);
308 
309     GdipDeleteCustomLineCap((GpCustomLineCap*)arrowcap);
310 }
311 
312 START_TEST(customlinecap)
313 {
314     struct GdiplusStartupInput gdiplusStartupInput;
315     ULONG_PTR gdiplusToken;
316     HMODULE hmsvcrt;
317     int (CDECL * _controlfp_s)(unsigned int *cur, unsigned int newval, unsigned int mask);
318 
319     /* Enable all FP exceptions except _EM_INEXACT, which gdi32 can trigger */
320     hmsvcrt = LoadLibraryA("msvcrt");
321     _controlfp_s = (void*)GetProcAddress(hmsvcrt, "_controlfp_s");
322     if (_controlfp_s) _controlfp_s(0, 0, 0x0008001e);
323 
324     gdiplusStartupInput.GdiplusVersion              = 1;
325     gdiplusStartupInput.DebugEventCallback          = NULL;
326     gdiplusStartupInput.SuppressBackgroundThread    = 0;
327     gdiplusStartupInput.SuppressExternalCodecs      = 0;
328 
329     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
330 
331     test_constructor_destructor();
332     test_linejoin();
333     test_inset();
334     test_scale();
335     test_create_adjustable_cap();
336     test_captype();
337 
338     GdiplusShutdown(gdiplusToken);
339 }
340