[GDI32_APITEST]
[reactos.git] / rostests / apitests / gdi32 / GetRandomRgn.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for GetRandomRgn
5 * PROGRAMMERS: Timo Kreuzer
6 */
7
8 #include <apitest.h>
9
10 #include <stdio.h>
11 #include <wingdi.h>
12 #include <winuser.h>
13
14 #define CLIPRGN 1
15 #define METARGN 2
16 #define APIRGN 3
17 #define SYSRGN 4
18 #define RGN5 5
19
20 HWND ghwnd;
21 HDC ghdcWindow;
22
23 void Test_GetRandomRgn_Params()
24 {
25 HDC hdc;
26 HRGN hrgn;
27 INT ret;
28
29 hdc = CreateCompatibleDC(0);
30 if (!hdc)
31 {
32 printf("Coun't create a dc\n");
33 return;
34 }
35
36 hrgn = CreateRectRgn(11, 17, 23, 42);
37 if (!hrgn)
38 {
39 printf("Coun't create a region\n");
40 return;
41 }
42
43 SetLastError(0xbadbad00);
44 ret = GetRandomRgn(NULL, NULL, 0);
45 ok_int(ret, -1);
46 ok((GetLastError() == 0xbadbad00) || (GetLastError() == ERROR_INVALID_HANDLE), "wrong error: %ld\n", GetLastError());
47
48 SetLastError(0xbadbad00);
49 ret = GetRandomRgn(NULL, NULL, CLIPRGN);
50 ok_int(ret, -1);
51 ok((GetLastError() == 0xbadbad00) || (GetLastError() == ERROR_INVALID_HANDLE), "wrong error: %ld\n", GetLastError());
52
53 SetLastError(0xbadbad00);
54 ret = GetRandomRgn(NULL, hrgn, 0);
55 ok_int(ret, -1);
56 ok((GetLastError() == 0xbadbad00) || (GetLastError() == ERROR_INVALID_HANDLE), "wrong error: %ld\n", GetLastError());
57
58 SetLastError(0xbadbad00);
59 ret = GetRandomRgn(NULL, hrgn, CLIPRGN);
60 ok_int(ret, -1);
61 ok((GetLastError() == 0xbadbad00) || (GetLastError() == ERROR_INVALID_HANDLE), "wrong error: %ld\n", GetLastError());
62
63 SetLastError(0xbadbad00);
64 ret = GetRandomRgn(hdc, NULL, 0);
65 ok_int(ret, 0);
66 ok_long(GetLastError(), 0xbadbad00);
67
68 SetLastError(0xbadbad00);
69 ret = GetRandomRgn(hdc, NULL, CLIPRGN);
70 ok_int(ret, 0);
71 ok_long(GetLastError(), 0xbadbad00);
72
73 SetLastError(0xbadbad00);
74 ret = GetRandomRgn(hdc, hrgn, 0);
75 ok_int(ret, 0);
76 ok_long(GetLastError(), 0xbadbad00);
77 #if 0 // this is vista+
78 SetLastError(0xbadbad00);
79 ret = GetRandomRgn(hdc, hrgn, 5);
80 ok_int(ret, 1);
81 ok_long(GetLastError(), 0xbadbad00);
82 #endif
83 SetLastError(0xbadbad00);
84 ret = GetRandomRgn(hdc, hrgn, 6);
85 ok_int(ret, 0);
86 ok_long(GetLastError(), 0xbadbad00);
87
88 SetLastError(0xbadbad00);
89 ret = GetRandomRgn(hdc, hrgn, 27);
90 ok_int(ret, 0);
91 ok_long(GetLastError(), 0xbadbad00);
92
93 SetLastError(0xbadbad00);
94 ret = GetRandomRgn(hdc, hrgn, -1);
95 ok_int(ret, 0);
96 ok_long(GetLastError(), 0xbadbad00);
97
98 SetLastError(0xbadbad00);
99 ret = GetRandomRgn(hdc, hrgn, CLIPRGN);
100 ok_int(ret, 0);
101 ok_long(GetLastError(), 0xbadbad00);
102
103 SetLastError(0xbadbad00);
104 ret = GetRandomRgn((HDC)0x123, hrgn, CLIPRGN);
105 ok_int(ret, -1);
106 ok((GetLastError() == 0xbadbad00) || (GetLastError() == ERROR_INVALID_HANDLE), "wrong error: %ld\n", GetLastError());
107
108 DeleteObject(hrgn);
109 DeleteDC(hdc);
110 }
111
112 void Test_GetRandomRgn_CLIPRGN()
113 {
114 HDC hdc;
115 HRGN hrgn1, hrgn2;
116 INT ret;
117 RECT rect;
118
119 hrgn1 = CreateRectRgn(11, 17, 23, 42);
120 if (!hrgn1)
121 {
122 printf("Coun't create a region\n");
123 return;
124 }
125
126 hdc = CreateCompatibleDC(0);
127 if (!hdc)
128 {
129 printf("Coun't create a dc\n");
130 return;
131 }
132
133 ret = GetRandomRgn(hdc, hrgn1, CLIPRGN);
134 ok_int(ret, 0);
135 GetRgnBox(hrgn1, &rect);
136 ok_long(rect.left, 11);
137 ok_long(rect.top, 17);
138 ok_long(rect.right, 23);
139 ok_long(rect.bottom, 42);
140
141 hrgn2 = CreateRectRgn(1, 2, 3, 4);
142 SelectClipRgn(hdc, hrgn2);
143 DeleteObject(hrgn2);
144 ret = GetRandomRgn(hdc, hrgn1, CLIPRGN);
145 ok_int(ret, 1);
146 GetRgnBox(hrgn1, &rect);
147 ok_long(rect.left, 1);
148 ok_long(rect.top, 2);
149 ok_long(rect.right, 3);
150 ok_long(rect.bottom, 4);
151
152 hrgn2 = CreateRectRgn(2, 3, 4, 5);
153 SelectClipRgn(ghdcWindow, hrgn2);
154 DeleteObject(hrgn2);
155 ret = GetRandomRgn(ghdcWindow, hrgn1, CLIPRGN);
156 ok_int(ret, 1);
157 GetRgnBox(hrgn1, &rect);
158 ok_long(rect.left, 2);
159 ok_long(rect.top, 3);
160 ok_long(rect.right, 4);
161 ok_long(rect.bottom, 5);
162
163 MoveWindow(ghwnd, 200, 400, 100, 100, 0);
164
165 ret = GetRandomRgn(ghdcWindow, hrgn1, CLIPRGN);
166 ok_int(ret, 1);
167 GetRgnBox(hrgn1, &rect);
168 ok_long(rect.left, 2);
169 ok_long(rect.top, 3);
170 ok_long(rect.right, 4);
171 ok_long(rect.bottom, 5);
172
173
174 DeleteObject(hrgn1);
175 DeleteDC(hdc);
176 }
177
178 void Test_GetRandomRgn_METARGN()
179 {
180 }
181
182 void Test_GetRandomRgn_APIRGN()
183 {
184 }
185
186 void Test_GetRandomRgn_SYSRGN()
187 {
188 HDC hdc;
189 HRGN hrgn1, hrgn2;
190 INT ret;
191 RECT rect, rect2;
192 HBITMAP hbmp;
193
194 hrgn1 = CreateRectRgn(11, 17, 23, 42);
195 if (!hrgn1)
196 {
197 printf("Coun't create a region\n");
198 return;
199 }
200
201 hdc = CreateCompatibleDC(0);
202 if (!hdc)
203 {
204 printf("Coun't create a dc\n");
205 return;
206 }
207
208 ret = GetRandomRgn(hdc, hrgn1, SYSRGN);
209 ok_int(ret, 1);
210 GetRgnBox(hrgn1, &rect);
211 ok_long(rect.left, 0);
212 ok_long(rect.top, 0);
213 ok_long(rect.right, 1);
214 ok_long(rect.bottom, 1);
215
216 hrgn2 = CreateRectRgn(1, 2, 3, 4);
217 SelectClipRgn(hdc, hrgn2);
218 DeleteObject(hrgn2);
219 ret = GetRandomRgn(hdc, hrgn1, SYSRGN);
220 ok_int(ret, 1);
221 GetRgnBox(hrgn1, &rect);
222 ok_long(rect.left, 0);
223 ok_long(rect.top, 0);
224 ok_long(rect.right, 1);
225 ok_long(rect.bottom, 1);
226
227 hbmp = CreateCompatibleBitmap(hdc, 4, 7);
228 SelectObject(hdc, hbmp);
229 ret = GetRandomRgn(hdc, hrgn1, SYSRGN);
230 ok_int(ret, 1);
231 GetRgnBox(hrgn1, &rect);
232 ok_long(rect.left, 0);
233 ok_long(rect.top, 0);
234 ok_long(rect.right, 4);
235 ok_long(rect.bottom, 7);
236 DeleteObject(hbmp);
237
238 MoveWindow(ghwnd, 100, 100, 100, 100, 0);
239 ret = GetRandomRgn(ghdcWindow, hrgn1, SYSRGN);
240 ok_int(ret, 1);
241 GetRgnBox(hrgn1, &rect);
242 DPtoLP(ghdcWindow, (LPPOINT)&rect, 2);
243 #if 0 // FIXME: this needs calculation
244 ok_long(rect.left, 104);
245 ok_long(rect.top, 124);
246 ok_long(rect.right, 209);
247 ok_long(rect.bottom, 196);
248 #endif
249
250 MoveWindow(ghwnd, 200, 400, 200, 200, 0);
251
252 ret = GetRandomRgn(ghdcWindow, hrgn1, SYSRGN);
253 ok_int(ret, 1);
254 GetRgnBox(hrgn1, &rect2);
255 DPtoLP(ghdcWindow, (LPPOINT)&rect2, 2);
256 #if 0 // FIXME: this needs calculation
257 ok_long(rect2.left, rect.left + 100);
258 ok_long(rect2.top, rect.top + 300);
259 ok_long(rect2.right, rect.right + 200 - 13);
260 ok_long(rect2.bottom, rect.bottom + 400);
261 #endif
262
263 DeleteObject(hrgn1);
264 DeleteDC(hdc);
265
266 }
267
268 void Test_GetRandomRgn_RGN5()
269 {
270 HDC hdc;
271 HRGN hrgn1, hrgn2;
272 INT ret;
273 RECT rect, rect2;
274 HBITMAP hbmp;
275 DBG_UNREFERENCED_LOCAL_VARIABLE(hrgn2);
276 DBG_UNREFERENCED_LOCAL_VARIABLE(rect2);
277
278 hrgn1 = CreateRectRgn(11, 17, 23, 42);
279 if (!hrgn1)
280 {
281 printf("Coun't create a region\n");
282 return;
283 }
284
285 hdc = CreateCompatibleDC(0);
286 if (!hdc)
287 {
288 printf("Coun't create a dc\n");
289 return;
290 }
291 #if 0 // this is vista+
292 ret = GetRandomRgn(hdc, hrgn1, RGN5);
293 ok_int(ret, 1);
294 GetRgnBox(hrgn1, &rect);
295 ok_long(rect.left, 0);
296 ok_long(rect.top, 0);
297 ok_long(rect.right, 1);
298 ok_long(rect.bottom, 1);
299
300 hrgn2 = CreateRectRgn(1, 2, 3, 4);
301 SelectClipRgn(hdc, hrgn2);
302 DeleteObject(hrgn2);
303 ret = GetRandomRgn(hdc, hrgn1, RGN5);
304 ok_int(ret, 1);
305 GetRgnBox(hrgn1, &rect);
306 ok_long(rect.left, 0);
307 ok_long(rect.top, 0);
308 ok_long(rect.right, 1);
309 ok_long(rect.bottom, 1);
310 #endif
311
312 hbmp = CreateCompatibleBitmap(hdc, 4, 7);
313 SelectObject(hdc, hbmp);
314 ret = GetRandomRgn(hdc, hrgn1, SYSRGN);
315 ok_int(ret, 1);
316 GetRgnBox(hrgn1, &rect);
317 ok_long(rect.left, 0);
318 ok_long(rect.top, 0);
319 ok_long(rect.right, 4);
320 ok_long(rect.bottom, 7);
321 DeleteObject(hbmp);
322
323 #if 0 // this is vista+
324 MoveWindow(ghwnd, 100, 100, 100, 100, 0);
325 ret = GetRandomRgn(ghdcWindow, hrgn1, RGN5);
326 ok_int(ret, 1);
327 GetRgnBox(hrgn1, &rect);
328 DPtoLP(ghdcWindow, (LPPOINT)&rect, 2);
329 ok_long(rect.left, 104);
330 ok_long(rect.top, 124);
331 ok_long(rect.right, 209);
332 ok_long(rect.bottom, 196);
333
334 MoveWindow(ghwnd, 200, 400, 200, 200, 0);
335
336 ret = GetRandomRgn(ghdcWindow, hrgn1, RGN5);
337 ok_int(ret, 1);
338 GetRgnBox(hrgn1, &rect2);
339 DPtoLP(ghdcWindow, (LPPOINT)&rect2, 2);
340 ok_long(rect2.left, rect.left + 100);
341 ok_long(rect2.top, rect.top + 300);
342 ok_long(rect2.right, rect.right + 200 - 13);
343 ok_long(rect2.bottom, rect.bottom + 400);
344 #endif
345
346 DeleteObject(hrgn1);
347 DeleteDC(hdc);
348 }
349
350 START_TEST(GetRandomRgn)
351 {
352
353 /* Create a window */
354 ghwnd = CreateWindowW(L"BUTTON", L"TestWindow", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
355 100, 100, 100, 100, NULL, NULL, 0, 0);
356 ghdcWindow = GetDC(ghwnd);
357 if (!ghdcWindow)
358 {
359 printf("No window dc\n");
360 return;
361 }
362
363 Test_GetRandomRgn_Params();
364 Test_GetRandomRgn_CLIPRGN();
365 Test_GetRandomRgn_METARGN();
366 Test_GetRandomRgn_APIRGN();
367 Test_GetRandomRgn_SYSRGN();
368 Test_GetRandomRgn_RGN5();
369
370 }
371