e2f4d74e5cca94ccfda85208e5ee0f59e3706f8a
[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
78 SetLastError(0xbadbad00);
79 ret = GetRandomRgn(hdc, hrgn, 5);
80 ok_int(ret, 1);
81 ok_long(GetLastError(), 0xbadbad00);
82
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 ok_long(rect.left, 104);
244 ok_long(rect.top, 124);
245 ok_long(rect.right, 209);
246 ok_long(rect.bottom, 196);
247
248 MoveWindow(ghwnd, 200, 400, 200, 200, 0);
249
250 ret = GetRandomRgn(ghdcWindow, hrgn1, SYSRGN);
251 ok_int(ret, 1);
252 GetRgnBox(hrgn1, &rect2);
253 DPtoLP(ghdcWindow, (LPPOINT)&rect2, 2);
254 ok_long(rect2.left, rect.left + 100);
255 ok_long(rect2.top, rect.top + 300);
256 ok_long(rect2.right, rect.right + 200 - 13);
257 ok_long(rect2.bottom, rect.bottom + 400);
258
259
260 DeleteObject(hrgn1);
261 DeleteDC(hdc);
262
263 }
264
265 void Test_GetRandomRgn_RGN5()
266 {
267 HDC hdc;
268 HRGN hrgn1, hrgn2;
269 INT ret;
270 RECT rect, rect2;
271 HBITMAP hbmp;
272
273 hrgn1 = CreateRectRgn(11, 17, 23, 42);
274 if (!hrgn1)
275 {
276 printf("Coun't create a region\n");
277 return;
278 }
279
280 hdc = CreateCompatibleDC(0);
281 if (!hdc)
282 {
283 printf("Coun't create a dc\n");
284 return;
285 }
286
287 ret = GetRandomRgn(hdc, hrgn1, RGN5);
288 ok_int(ret, 1);
289 GetRgnBox(hrgn1, &rect);
290 ok_long(rect.left, 0);
291 ok_long(rect.top, 0);
292 ok_long(rect.right, 1);
293 ok_long(rect.bottom, 1);
294
295 hrgn2 = CreateRectRgn(1, 2, 3, 4);
296 SelectClipRgn(hdc, hrgn2);
297 DeleteObject(hrgn2);
298 ret = GetRandomRgn(hdc, hrgn1, RGN5);
299 ok_int(ret, 1);
300 GetRgnBox(hrgn1, &rect);
301 ok_long(rect.left, 0);
302 ok_long(rect.top, 0);
303 ok_long(rect.right, 1);
304 ok_long(rect.bottom, 1);
305
306 hbmp = CreateCompatibleBitmap(hdc, 4, 7);
307 SelectObject(hdc, hbmp);
308 ret = GetRandomRgn(hdc, hrgn1, SYSRGN);
309 ok_int(ret, 1);
310 GetRgnBox(hrgn1, &rect);
311 ok_long(rect.left, 0);
312 ok_long(rect.top, 0);
313 ok_long(rect.right, 4);
314 ok_long(rect.bottom, 7);
315 DeleteObject(hbmp);
316
317 MoveWindow(ghwnd, 100, 100, 100, 100, 0);
318 ret = GetRandomRgn(ghdcWindow, hrgn1, RGN5);
319 ok_int(ret, 1);
320 GetRgnBox(hrgn1, &rect);
321 DPtoLP(ghdcWindow, (LPPOINT)&rect, 2);
322 ok_long(rect.left, 104);
323 ok_long(rect.top, 124);
324 ok_long(rect.right, 209);
325 ok_long(rect.bottom, 196);
326
327 MoveWindow(ghwnd, 200, 400, 200, 200, 0);
328
329 ret = GetRandomRgn(ghdcWindow, hrgn1, RGN5);
330 ok_int(ret, 1);
331 GetRgnBox(hrgn1, &rect2);
332 DPtoLP(ghdcWindow, (LPPOINT)&rect2, 2);
333 ok_long(rect2.left, rect.left + 100);
334 ok_long(rect2.top, rect.top + 300);
335 ok_long(rect2.right, rect.right + 200 - 13);
336 ok_long(rect2.bottom, rect.bottom + 400);
337
338
339 DeleteObject(hrgn1);
340 DeleteDC(hdc);
341 }
342
343 START_TEST(GetRandomRgn)
344 {
345
346 /* Create a window */
347 ghwnd = CreateWindowW(L"BUTTON", L"TestWindow", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
348 100, 100, 100, 100, NULL, NULL, 0, 0);
349 ghdcWindow = GetDC(ghwnd);
350 if (!ghdcWindow)
351 {
352 printf("No window dc\n");
353 return;
354 }
355
356 Test_GetRandomRgn_Params();
357 Test_GetRandomRgn_CLIPRGN();
358 Test_GetRandomRgn_METARGN();
359 Test_GetRandomRgn_APIRGN();
360 Test_GetRandomRgn_SYSRGN();
361 Test_GetRandomRgn_RGN5();
362
363 }
364