[COMCTL32_APITEST]: Simplify tests to not depend on internals of CreateWindow (creati...
[reactos.git] / rostests / apitests / comctl32 / button.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for Button window class v6
5 * PROGRAMMERS: Giannis Adamopoulos
6 */
7
8 #include "wine/test.h"
9 #include <windows.h>
10 #include <commctrl.h>
11 #include <uxtheme.h>
12
13 #define ok_rect(rc, l,r,t,b) ok((rc.left == (l)) && (rc.right == (r)) && (rc.top == (t)) && (rc.bottom == (b)), "Wrong rect. expected %d, %d, %d, %d got %ld, %ld, %ld, %ld\n", l,t,r,b, rc.left, rc.top, rc.right, rc.bottom)
14 #define ok_size(s, width, height) ok((s.cx == (width) && s.cy == (height)), "Expected size (%lu,%lu) got (%lu,%lu)\n", (LONG)width, (LONG)height, s.cx, s.cy)
15
16 void Test_TextMargin()
17 {
18 RECT rc;
19 BOOL ret;
20 HWND hwnd1;
21
22 hwnd1 = CreateWindowW(L"Button", L"Test1", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
23 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
24 SetWindowTheme(hwnd1, L"", L"");
25
26 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
27 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
28 ok_rect(rc, 1, 1, 1, 1);
29
30 SetRect(&rc, 0,0,0,0);
31 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
32 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
33
34 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
35 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
36 ok_rect(rc, 0, 0, 0, 0);
37
38 SetRect(&rc, -1,-1,-1,-1);
39 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
40 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
41
42 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
43 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
44 ok_rect(rc, -1, -1, -1, -1);
45
46 SetRect(&rc, 1000,1000,1000,1000);
47 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
48 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
49
50 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
51 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
52 ok_rect(rc, 1000, 1000, 1000, 1000);
53
54 DestroyWindow(hwnd1);
55
56 hwnd1 = CreateWindowW(L"Button", L"Test1", BS_DEFPUSHBUTTON, 10, 10, 100, 100, 0, NULL, NULL, NULL);
57 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
58 SetWindowTheme(hwnd1, L"", L"");
59
60 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
61 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
62 ok_rect(rc, 1, 1, 1, 1);
63
64 DestroyWindow(hwnd1);
65
66 }
67
68 void Test_Imagelist()
69 {
70 HWND hwnd1;
71 BOOL ret;
72 BUTTON_IMAGELIST imlData;
73
74 hwnd1 = CreateWindowW(L"Button", L"Test2", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
75 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
76
77 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
78 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
79 ok (imlData.himl == 0, "Expected 0 himl\n");
80 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
81 ok_rect(imlData.margin, 0, 0, 0, 0);
82
83 SetRect(&imlData.margin, 0,0,0,1);
84 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
85 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
86
87 imlData.himl = (HIMAGELIST)0xdead;
88 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
89 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
90
91 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
92 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
93 ok (imlData.himl == (HIMAGELIST)0xdead, "Expected 0 himl\n");
94 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
95 ok_rect(imlData.margin, 0, 0, 0, 1);
96
97 imlData.himl = 0;
98 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
99 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
100
101 DestroyWindow(hwnd1);
102 }
103
104 void Test_GetIdealSizeNoThemes()
105 {
106 HWND hwnd1, hwnd2;
107 BOOL ret;
108 SIZE s, textent;
109 HFONT font;
110 HDC hdc;
111 HANDLE hbmp;
112 HIMAGELIST himl;
113 BUTTON_IMAGELIST imlData;
114 RECT rc;
115 LOGFONTW lf;
116 DWORD i;
117
118 hwnd2 = CreateWindowW(L"Static", L"", 0, 0, 0, 100, 100, 0, NULL, NULL, NULL);
119 ok (hwnd2 != NULL, "Expected CreateWindowW to succeed\n");
120
121 hwnd1 = CreateWindowW(L"Button", L" ", WS_CHILD, 10, 10, 100, 100, hwnd2, NULL, NULL, NULL);
122 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
123 SetWindowTheme(hwnd1, L"", L"");
124
125 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
126 hdc = GetDC(hwnd1);
127 SelectObject(hdc, font);
128 GetTextExtentPoint32W(hdc, L" ", 1, &textent);
129
130 memset(&s, 0, sizeof(s));
131 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
132 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
133 ok_size(s, textent.cx + 5 + 2,
134 textent.cy + 7 + 2); /* the last +2 is the text margin */
135
136 DestroyWindow(hwnd1);
137
138
139 hwnd1 = CreateWindowW(L"Button", L" ", BS_USERBUTTON | WS_CHILD, 10, 10, 100, 100, hwnd2, NULL, NULL, NULL);
140 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
141 SetWindowTheme(hwnd1, L"", L"");
142
143 memset(&s, 0, sizeof(s));
144 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
145 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
146 ok_size(s, textent.cx + 5 + 2,
147 textent.cy + 7 + 2); /* the last +2 is the text margin */
148
149 DestroyWindow(hwnd1);
150
151
152
153 hwnd1 = CreateWindowW(L"Button", L"", WS_CHILD, 10, 10, 100, 100, hwnd2, NULL, NULL, NULL);
154 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
155 SetWindowTheme(hwnd1, L"", L"");
156
157 s.cx = 1;
158 s.cy = 1;
159 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
160 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
161 ok_size(s, 100, 100);
162
163 hbmp = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), IMAGE_BITMAP, 0, 0, 0);
164 ok (hbmp != 0, "Expected LoadImage to succeed\n");
165
166 SendMessageW(hwnd1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
167
168 memset(&s, 0, sizeof(s));
169 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
170 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
171 ok_size(s, 100, 100);
172
173 himl = ImageList_LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), 1, 1, 0, IMAGE_BITMAP, 0);
174 ok (himl != 0, "Expected ImageList_LoadImage to succeed\n");
175
176 memset(&imlData, 0, sizeof(imlData));
177 imlData.himl = himl;
178 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
179 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
180
181 memset(&s, 0, sizeof(s));
182 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
183 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
184 ok_size(s, 100, 100);
185
186 DestroyWindow(hwnd1);
187
188
189
190
191
192 hwnd1 = CreateWindowW(L"Button", L"", WS_CHILD, 10, 10, 5, 5, hwnd2, NULL, NULL, NULL);
193 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
194 SetWindowTheme(hwnd1, L"", L"");
195
196 memset(&s, 0, sizeof(s));
197 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
198 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
199 ok_size(s, 5, 5);
200
201 DestroyWindow(hwnd1);
202
203
204
205
206 hwnd1 = CreateWindowW(L"Button", L" ", BS_BITMAP | WS_CHILD, 10, 10, 100, 100, hwnd2, NULL, NULL, NULL);
207 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
208 SetWindowTheme(hwnd1, L"", L"");
209
210 SendMessageW(hwnd1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
211
212 memset(&s, 0, sizeof(s));
213 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
214 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
215
216 /* In xp and 2k3 the image is ignored, in vista+ its width is added to the text width */
217 ok_size(s, textent.cx + 5 + 2,
218 textent.cy + 7 + 2); /* the last +2 is the text margin */
219
220 DestroyWindow(hwnd1);
221
222
223
224 hwnd1 = CreateWindowW(L"Button", L" ", WS_CHILD, 10, 10, 100, 100, hwnd2, NULL, NULL, NULL);
225 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
226 SetWindowTheme(hwnd1, L"", L"");
227
228 SetRect(&rc, 0,0,0,0);
229 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
230 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
231
232 memset(&s, 0, sizeof(s));
233 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
234 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
235 ok_size(s, textent.cx + 5,
236 textent.cy + 7);
237
238 SetRect(&rc, 50,50,50,50);
239 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
240 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
241
242 memset(&s, 0, sizeof(s));
243 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
244 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
245 ok_size(s, textent.cx + 5 + 100,
246 textent.cy + 7 + 100);
247
248 SetRect(&rc, 1,1,1,1);
249 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
250 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
251
252 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
253 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
254
255 memset(&s, 0, sizeof(s));
256 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
257 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
258 ok_size(s, textent.cx + 5 + 2 + 1, /* we get an extra pixel due to the iml */
259 textent.cy + 7 + 2);
260
261 s.cx = 1;
262 s.cy = 1;
263 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
264 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
265 ok_size(s, textent.cx + 5 + 2 + 1,
266 textent.cy + 7 + 2);
267
268 s.cx = 100;
269 s.cy = 100;
270 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
271 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
272 ok_size(s, textent.cx + 5 + 2 + 1,
273 textent.cy + 7 + 2);
274
275 SetRect(&imlData.margin, 1,1,1,1);
276 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
277 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
278
279 memset(&s, 0, sizeof(s));
280 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
281 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
282 /* expected width = text width + hardcoded value + text margins + image width + image margins */
283 ok_size(s, textent.cx + 5 + 2 + 1 + 2,
284 textent.cy + 7 + 2);
285
286 SetRect(&imlData.margin, 50,50,50,50);
287 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
288 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
289
290 memset(&s, 0, sizeof(s));
291 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
292 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
293 /* image + its margins is so big that the height is dictated by them */
294 ok_size(s, textent.cx + 5 + 2 + 1 + 100, (LONG)101);
295
296 DestroyWindow(hwnd1);
297
298
299
300
301
302
303 hwnd1 = CreateWindowW(L"Button", L"Start", BS_VCENTER | WS_CHILD, 0, 0, 0, 0, hwnd2, NULL, NULL, NULL);
304 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
305 SetWindowTheme(hwnd1, L"", L"");
306
307 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
308 hdc = GetDC(hwnd1);
309 SelectObject(hdc, font);
310 GetTextExtentPoint32W(hdc, L"Start", 5, &textent);
311
312 SetRect(&rc, 0,0,0,0);
313 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
314 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
315
316 memset(&s, 0, sizeof(s));
317 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
318 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
319 ok_size(s, textent.cx + 5, textent.cy + 7);
320
321 DestroyWindow(hwnd1);
322
323
324
325
326 /* Test again with some real text to see if the formula is correct */
327 hwnd1 = CreateWindowW(L"Button", L"Some test text", WS_CHILD, 10, 10, 100, 100, hwnd2, NULL, NULL, NULL);
328 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
329 SetWindowTheme(hwnd1, L"", L"");
330
331 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
332 hdc = GetDC(hwnd1);
333 SelectObject(hdc, font);
334 GetTextExtentPoint32W(hdc, L"Some test text", 14, &textent);
335
336 memset(&s, 0, sizeof(s));
337 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
338 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
339 ok_size(s, textent.cx + 5 + 2, /* the last +2 is the text margin */
340 textent.cy + 7 + 2);
341
342 /* The hardcoded values are independent of the margin */
343 lf.lfHeight = 200;
344 lf.lfWidth = 200;
345 lf.lfWeight = FW_BOLD;
346 wcscpy(lf.lfFaceName, L"Arial");
347 font = CreateFontIndirectW(&lf);
348 ok(font != NULL, "\n");
349 SendMessageW(hwnd1, WM_SETFONT, (WPARAM)font, FALSE);
350
351 SelectObject(hdc, font);
352 GetTextExtentPoint32W(hdc, L"Some test text", 14, &textent);
353
354 memset(&s, 0, sizeof(s));
355 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
356 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
357 ok_size(s, textent.cx + 5 + 2, /* the last +2 is the text margin */
358 textent.cy + 7 + 2);
359
360 DestroyWindow(hwnd1);
361
362 for (i = BS_PUSHBUTTON; i <= BS_OWNERDRAW; i++)
363 {
364 if (i == BS_USERBUTTON)
365 continue;
366
367 if (i >= BS_CHECKBOX)
368 {
369 hwnd1 = CreateWindowW(L"Button", L" ", i|WS_CHILD, 0, 0, 72, 72, hwnd2, NULL, NULL, NULL);
370 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
371 memset(&s, 0, sizeof(s));
372 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
373 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
374 ok_size(s, 72, 72);
375
376 SetWindowTheme(hwnd1, L"", L"");
377 memset(&s, 0, sizeof(s));
378 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
379 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
380 ok_size(s, 72, 72);
381 DestroyWindow(hwnd1);
382
383 hwnd1 = CreateWindowW(L"Button", L" ", i|WS_CHILD, 0, 0, 12, 12, hwnd2, NULL, NULL, NULL);
384 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
385 memset(&s, 0, sizeof(s));
386 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
387 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
388 ok_size(s, 12, 12);
389 DestroyWindow(hwnd1);
390 }
391
392 hwnd1 = CreateWindowW(L"Button", L"", i|WS_CHILD, 0, 0, 72, 72, hwnd2, NULL, NULL, NULL);
393 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
394 memset(&s, 0, sizeof(s));
395 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
396 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
397 ok_size(s, 72, 72);
398 DestroyWindow(hwnd1);
399
400 hwnd1 = CreateWindowW(L"Button", L"", i|WS_CHILD, 0, 0, 150, 72, hwnd2, NULL, NULL, NULL);
401 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
402 memset(&s, 0, sizeof(s));
403 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
404 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
405 ok_size(s, 150, 72);
406 DestroyWindow(hwnd1);
407 }
408 DestroyWindow(hwnd2);
409 }
410
411 START_TEST(button)
412 {
413 LoadLibraryW(L"comctl32.dll"); /* same as statically linking to comctl32 and doing InitCommonControls */
414 Test_TextMargin();
415 Test_Imagelist();
416 Test_GetIdealSizeNoThemes();
417 }
418