Properly skip known crasher in gdi32:metafile test.
[reactos.git] / rostests / winetests / gdi32 / clipping.c
1 /*
2 * Unit test suite for clipping
3 *
4 * Copyright 2005 Huw Davies
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 "wine/test.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25
26 static void test_GetRandomRgn(void)
27 {
28 HWND hwnd = CreateWindowExA(0,"BUTTON","test",WS_VISIBLE|WS_POPUP,0,0,100,100,GetDesktopWindow(),0,0,0);
29 HDC hdc;
30 HRGN hrgn = CreateRectRgn(0, 0, 0, 0);
31 int ret;
32 RECT rc, rc2;
33 RECT ret_rc, window_rc;
34
35 ok( hwnd != 0, "CreateWindow failed\n" );
36
37 SetRect(&window_rc, 400, 300, 500, 400);
38 MoveWindow(hwnd, window_rc.left, window_rc.top, window_rc.right - window_rc.left, window_rc.bottom - window_rc.top, FALSE);
39 hdc = GetDC(hwnd);
40
41 ret = GetRandomRgn(hdc, hrgn, 1);
42 ok(ret == 0, "GetRandomRgn rets %d\n", ret);
43 ret = GetRandomRgn(hdc, hrgn, 2);
44 ok(ret == 0, "GetRandomRgn rets %d\n", ret);
45 ret = GetRandomRgn(hdc, hrgn, 3);
46 ok(ret == 0, "GetRandomRgn rets %d\n", ret);
47
48 /* Set a clip region */
49 SetRect(&rc, 20, 20, 80, 80);
50 IntersectClipRect(hdc, rc.left, rc.top, rc.right, rc.bottom);
51
52 ret = GetRandomRgn(hdc, hrgn, 1);
53 ok(ret != 0, "GetRandomRgn rets %d\n", ret);
54 GetRgnBox(hrgn, &ret_rc);
55 ok(EqualRect(&rc, &ret_rc), "GetRandomRgn %d,%d - %d,%d\n",
56 ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom);
57
58 ret = GetRandomRgn(hdc, hrgn, 2);
59 ok(ret == 0, "GetRandomRgn rets %d\n", ret);
60
61 ret = GetRandomRgn(hdc, hrgn, 3);
62 ok(ret != 0, "GetRandomRgn rets %d\n", ret);
63 GetRgnBox(hrgn, &ret_rc);
64 ok(EqualRect(&rc, &ret_rc), "GetRandomRgn %d,%d - %d,%d\n",
65 ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom);
66
67 /* Move the clip to the meta and clear the clip */
68 SetMetaRgn(hdc);
69
70 ret = GetRandomRgn(hdc, hrgn, 1);
71 ok(ret == 0, "GetRandomRgn rets %d\n", ret);
72 ret = GetRandomRgn(hdc, hrgn, 2);
73 ok(ret != 0, "GetRandomRgn rets %d\n", ret);
74 GetRgnBox(hrgn, &ret_rc);
75 ok(EqualRect(&rc, &ret_rc), "GetRandomRgn %d,%d - %d,%d\n",
76 ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom);
77
78 ret = GetRandomRgn(hdc, hrgn, 3);
79 ok(ret != 0, "GetRandomRgn rets %d\n", ret);
80 GetRgnBox(hrgn, &ret_rc);
81 ok(EqualRect(&rc, &ret_rc), "GetRandomRgn %d,%d - %d,%d\n",
82 ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom);
83
84 /* Set a new clip (still got the meta) */
85 SetRect(&rc2, 10, 30, 70, 90);
86 IntersectClipRect(hdc, rc2.left, rc2.top, rc2.right, rc2.bottom);
87
88 ret = GetRandomRgn(hdc, hrgn, 1);
89 ok(ret != 0, "GetRandomRgn rets %d\n", ret);
90 GetRgnBox(hrgn, &ret_rc);
91 ok(EqualRect(&rc2, &ret_rc), "GetRandomRgn %d,%d - %d,%d\n",
92 ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom);
93
94 ret = GetRandomRgn(hdc, hrgn, 2);
95 ok(ret != 0, "GetRandomRgn rets %d\n", ret);
96 GetRgnBox(hrgn, &ret_rc);
97 ok(EqualRect(&rc, &ret_rc), "GetRandomRgn %d,%d - %d,%d\n",
98 ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom);
99
100 IntersectRect(&rc2, &rc, &rc2);
101
102 ret = GetRandomRgn(hdc, hrgn, 3);
103 ok(ret != 0, "GetRandomRgn rets %d\n", ret);
104 GetRgnBox(hrgn, &ret_rc);
105 ok(EqualRect(&rc2, &ret_rc), "GetRandomRgn %d,%d - %d,%d\n",
106 ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom);
107
108
109 ret = GetRandomRgn(hdc, hrgn, SYSRGN);
110 ok(ret != 0, "GetRandomRgn rets %d\n", ret);
111 GetRgnBox(hrgn, &ret_rc);
112 if(GetVersion() & 0x80000000)
113 OffsetRect(&window_rc, -window_rc.left, -window_rc.top);
114 ok(EqualRect(&window_rc, &ret_rc) ||
115 broken(IsRectEmpty(&ret_rc)), /* win95 */
116 "GetRandomRgn %d,%d - %d,%d\n",
117 ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom);
118
119 DeleteObject(hrgn);
120 ReleaseDC(hwnd, hdc);
121 DestroyWindow(hwnd);
122 }
123
124 static void verify_region(HRGN hrgn, const RECT *rc)
125 {
126 union
127 {
128 RGNDATA data;
129 char buf[sizeof(RGNDATAHEADER) + sizeof(RECT)];
130 } rgn;
131 const RECT *rect;
132 DWORD ret;
133
134 ret = GetRegionData(hrgn, 0, NULL);
135 if (IsRectEmpty(rc))
136 ok(ret == sizeof(rgn.data.rdh), "expected sizeof(rdh), got %u\n", ret);
137 else
138 ok(ret == sizeof(rgn.data.rdh) + sizeof(RECT), "expected sizeof(rgn), got %u\n", ret);
139
140 if (!ret) return;
141
142 ret = GetRegionData(hrgn, sizeof(rgn), &rgn.data);
143 if (IsRectEmpty(rc))
144 ok(ret == sizeof(rgn.data.rdh), "expected sizeof(rdh), got %u\n", ret);
145 else
146 ok(ret == sizeof(rgn.data.rdh) + sizeof(RECT), "expected sizeof(rgn), got %u\n", ret);
147
148 trace("size %u, type %u, count %u, rgn size %u, bound (%d,%d-%d,%d)\n",
149 rgn.data.rdh.dwSize, rgn.data.rdh.iType,
150 rgn.data.rdh.nCount, rgn.data.rdh.nRgnSize,
151 rgn.data.rdh.rcBound.left, rgn.data.rdh.rcBound.top,
152 rgn.data.rdh.rcBound.right, rgn.data.rdh.rcBound.bottom);
153 if (rgn.data.rdh.nCount != 0)
154 {
155 rect = (const RECT *)rgn.data.Buffer;
156 trace("rect (%d,%d-%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
157 ok(EqualRect(rect, rc), "rects don't match\n");
158 }
159
160 ok(rgn.data.rdh.dwSize == sizeof(rgn.data.rdh), "expected sizeof(rdh), got %u\n", rgn.data.rdh.dwSize);
161 ok(rgn.data.rdh.iType == RDH_RECTANGLES, "expected RDH_RECTANGLES, got %u\n", rgn.data.rdh.iType);
162 if (IsRectEmpty(rc))
163 {
164 ok(rgn.data.rdh.nCount == 0, "expected 0, got %u\n", rgn.data.rdh.nCount);
165 ok(rgn.data.rdh.nRgnSize == 0 ||
166 broken(rgn.data.rdh.nRgnSize == 168), /* NT4 */
167 "expected 0, got %u\n", rgn.data.rdh.nRgnSize);
168 }
169 else
170 {
171 ok(rgn.data.rdh.nCount == 1, "expected 1, got %u\n", rgn.data.rdh.nCount);
172 ok(rgn.data.rdh.nRgnSize == sizeof(RECT) ||
173 broken(rgn.data.rdh.nRgnSize == 168), /* NT4 */
174 "expected sizeof(RECT), got %u\n", rgn.data.rdh.nRgnSize);
175 }
176 ok(EqualRect(&rgn.data.rdh.rcBound, rc), "rects don't match\n");
177 }
178
179 static void test_ExtCreateRegion(void)
180 {
181 static const RECT empty_rect;
182 static const RECT rc = { 111, 222, 333, 444 };
183 static const RECT rc_xformed = { 76, 151, 187, 262 };
184 union
185 {
186 RGNDATA data;
187 char buf[sizeof(RGNDATAHEADER) + sizeof(RECT)];
188 } rgn;
189 HRGN hrgn;
190 XFORM xform;
191
192 if (0) /* crashes under Win9x */
193 {
194 SetLastError(0xdeadbeef);
195 hrgn = ExtCreateRegion(NULL, 0, NULL);
196 ok(!hrgn, "ExtCreateRegion should fail\n");
197 ok(GetLastError() == ERROR_INVALID_PARAMETER, "ERROR_INVALID_PARAMETER, got %u\n", GetLastError());
198 }
199
200 rgn.data.rdh.dwSize = 0;
201 rgn.data.rdh.iType = 0;
202 rgn.data.rdh.nCount = 0;
203 rgn.data.rdh.nRgnSize = 0;
204 SetRectEmpty(&rgn.data.rdh.rcBound);
205 memcpy(rgn.data.Buffer, &rc, sizeof(rc));
206
207 SetLastError(0xdeadbeef);
208 hrgn = ExtCreateRegion(NULL, sizeof(rgn), &rgn.data);
209 ok(!hrgn, "ExtCreateRegion should fail\n");
210 ok(GetLastError() == 0xdeadbeef, "0xdeadbeef, got %u\n", GetLastError());
211
212 rgn.data.rdh.dwSize = sizeof(rgn.data.rdh) - 1;
213
214 SetLastError(0xdeadbeef);
215 hrgn = ExtCreateRegion(NULL, sizeof(rgn), &rgn.data);
216 ok(!hrgn, "ExtCreateRegion should fail\n");
217 ok(GetLastError() == 0xdeadbeef, "0xdeadbeef, got %u\n", GetLastError());
218
219 /* although XP doesn't care about the type Win9x does */
220 rgn.data.rdh.iType = RDH_RECTANGLES;
221 rgn.data.rdh.dwSize = sizeof(rgn.data.rdh);
222
223 SetLastError(0xdeadbeef);
224 hrgn = ExtCreateRegion(NULL, sizeof(rgn), &rgn.data);
225 ok(hrgn != 0, "ExtCreateRegion error %u\n", GetLastError());
226 verify_region(hrgn, &empty_rect);
227 DeleteObject(hrgn);
228
229 rgn.data.rdh.nCount = 1;
230 SetRectEmpty(&rgn.data.rdh.rcBound);
231 memcpy(rgn.data.Buffer, &rc, sizeof(rc));
232
233 SetLastError(0xdeadbeef);
234 hrgn = ExtCreateRegion(NULL, sizeof(rgn), &rgn.data);
235 ok(hrgn != 0, "ExtCreateRegion error %u\n", GetLastError());
236 verify_region(hrgn, &rc);
237 DeleteObject(hrgn);
238
239 rgn.data.rdh.dwSize = sizeof(rgn.data.rdh) + 1;
240
241 SetLastError(0xdeadbeef);
242 hrgn = ExtCreateRegion(NULL, 1, &rgn.data);
243 ok(hrgn != 0 ||
244 broken(GetLastError() == 0xdeadbeef), /* NT4 */
245 "ExtCreateRegion error %u\n", GetLastError());
246 if(hrgn)
247 {
248 verify_region(hrgn, &rc);
249 DeleteObject(hrgn);
250 }
251
252 xform.eM11 = 0.5; /* 50% width */
253 xform.eM12 = 0.0;
254 xform.eM21 = 0.0;
255 xform.eM22 = 0.5; /* 50% height */
256 xform.eDx = 20.0;
257 xform.eDy = 40.0;
258
259 rgn.data.rdh.dwSize = sizeof(rgn.data.rdh);
260
261 SetLastError(0xdeadbeef);
262 hrgn = ExtCreateRegion(&xform, sizeof(rgn), &rgn.data);
263 ok(hrgn != 0, "ExtCreateRegion error %u/%x\n", GetLastError(), GetLastError());
264 verify_region(hrgn, &rc_xformed);
265 DeleteObject(hrgn);
266 }
267
268 static void test_GetClipRgn(void)
269 {
270 HDC hdc;
271 HRGN hrgn, hrgn2, hrgn3, hrgn4;
272 int ret;
273
274 /* Test calling GetClipRgn with NULL device context and region handles. */
275 ret = GetClipRgn(NULL, NULL);
276 ok(ret == -1, "Expected GetClipRgn to return -1, got %d\n", ret);
277
278 hdc = GetDC(NULL);
279 ok(hdc != NULL, "Expected GetDC to return a valid device context handle\n");
280
281 /* Test calling GetClipRgn with a valid device context and NULL region. */
282 ret = GetClipRgn(hdc, NULL);
283 ok(ret == 0 ||
284 ret == -1 /* Win9x */,
285 "Expected GetClipRgn to return 0, got %d\n", ret);
286
287 /* Initialize the test regions. */
288 hrgn = CreateRectRgn(100, 100, 100, 100);
289 ok(hrgn != NULL,
290 "Expected CreateRectRgn to return a handle to a new rectangular region\n");
291
292 hrgn2 = CreateRectRgn(1, 2, 3, 4);
293 ok(hrgn2 != NULL,
294 "Expected CreateRectRgn to return a handle to a new rectangular region\n");
295
296 hrgn3 = CreateRectRgn(1, 2, 3, 4);
297 ok(hrgn3 != NULL,
298 "Expected CreateRectRgn to return a handle to a new rectangular region\n");
299
300 hrgn4 = CreateRectRgn(1, 2, 3, 4);
301 ok(hrgn4 != NULL,
302 "Expected CreateRectRgn to return a handle to a new rectangular region\n");
303
304 /* Try getting a clipping region from the device context
305 * when the device context's clipping region isn't set. */
306 ret = GetClipRgn(hdc, hrgn2);
307 ok(ret == 0, "Expected GetClipRgn to return 0, got %d\n", ret);
308
309 /* The region passed to GetClipRgn should be unchanged. */
310 ret = EqualRgn(hrgn2, hrgn3);
311 ok(ret == 1,
312 "Expected EqualRgn to compare the two regions as equal, got %d\n", ret);
313
314 /* Try setting and getting back a clipping region. */
315 ret = SelectClipRgn(hdc, hrgn);
316 ok(ret == NULLREGION,
317 "Expected SelectClipRgn to return NULLREGION, got %d\n", ret);
318
319 /* Passing a NULL region handle when the device context
320 * has a clipping region results in an error. */
321 ret = GetClipRgn(hdc, NULL);
322 ok(ret == -1, "Expected GetClipRgn to return -1, got %d\n", ret);
323
324 ret = GetClipRgn(hdc, hrgn2);
325 ok(ret == 1, "Expected GetClipRgn to return 1, got %d\n", ret);
326
327 ret = EqualRgn(hrgn, hrgn2);
328 ok(ret == 1,
329 "Expected EqualRgn to compare the two regions as equal, got %d\n", ret);
330
331 /* Try unsetting and then query the clipping region. */
332 ret = SelectClipRgn(hdc, NULL);
333 ok(ret == SIMPLEREGION,
334 "Expected SelectClipRgn to return SIMPLEREGION, got %d\n", ret);
335
336 ret = GetClipRgn(hdc, NULL);
337 ok(ret == 0 ||
338 ret == -1 /* Win9x */,
339 "Expected GetClipRgn to return 0, got %d\n", ret);
340
341 ret = GetClipRgn(hdc, hrgn3);
342 ok(ret == 0, "Expected GetClipRgn to return 0, got %d\n", ret);
343
344 ret = EqualRgn(hrgn3, hrgn4);
345 ok(ret == 1,
346 "Expected EqualRgn to compare the two regions as equal, got %d\n", ret);
347
348 DeleteObject(hrgn4);
349 DeleteObject(hrgn3);
350 DeleteObject(hrgn2);
351 DeleteObject(hrgn);
352 ReleaseDC(NULL, hdc);
353 }
354
355 START_TEST(clipping)
356 {
357 test_GetRandomRgn();
358 test_ExtCreateRegion();
359 test_GetClipRgn();
360 }