[COMCTL32_APITEST} -Add some more tests and fix build.
[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", width, 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
57 void Test_Imagelist()
58 {
59 HWND hwnd1;
60 BOOL ret;
61 BUTTON_IMAGELIST imlData;
62
63 hwnd1 = CreateWindowW(L"Button", L"Test2", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
64 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
65
66 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
67 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
68 ok (imlData.himl == 0, "Expected 0 himl\n");
69 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
70 ok_rect(imlData.margin, 0, 0, 0, 0);
71
72 SetRect(&imlData.margin, 0,0,0,1);
73 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
74 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
75
76 imlData.himl = (HIMAGELIST)0xdead;
77 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
78 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
79
80 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
81 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
82 ok (imlData.himl == (HIMAGELIST)0xdead, "Expected 0 himl\n");
83 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
84 ok_rect(imlData.margin, 0, 0, 0, 1);
85
86 imlData.himl = 0;
87 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
88 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
89
90 DestroyWindow(hwnd1);
91 }
92
93 void Test_GetIdealSizeNoThemes()
94 {
95 HWND hwnd1;
96 BOOL ret;
97 SIZE s, textent;
98 HFONT font;
99 HDC hdc;
100 HANDLE hbmp;
101 HIMAGELIST himl;
102 BUTTON_IMAGELIST imlData;
103 RECT rc;
104 LOGFONTW lf;
105
106 hwnd1 = CreateWindowW(L"Button", L" ", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
107 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
108 SetWindowTheme(hwnd1, L"", L"");
109
110 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
111 hdc = GetDC(hwnd1);
112 SelectObject(hdc, font);
113 GetTextExtentPoint32W(hdc, L" ", 1, &textent);
114
115 memset(&s, 0, sizeof(s));
116 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
117 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
118 ok_size(s, textent.cx + 5 + 2,
119 textent.cy + 7 + 2); /* the last +2 is the text margin */
120
121 DestroyWindow(hwnd1);
122
123
124
125
126 hwnd1 = CreateWindowW(L"Button", L"", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
127 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
128 SetWindowTheme(hwnd1, L"", L"");
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 (s.cx > 80, "Expected big cx\n");
134 ok (s.cy > 80, "Expected big cy\n");
135
136 s.cx = 1;
137 s.cy = 1;
138 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
139 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
140 ok (s.cx > 80, "Expected big cx\n");
141 ok (s.cy > 80, "Expected big cy\n");
142
143 hbmp = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), IMAGE_BITMAP, 0, 0, 0);
144 ok (hbmp != 0, "Expected LoadImage to succeed\n");
145
146 SendMessageW(hwnd1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
147
148 memset(&s, 0, sizeof(s));
149 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
150 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
151 ok (s.cx > 80, "Expected big cx\n");
152 ok (s.cy > 80, "Expected big cy\n");
153
154 himl = ImageList_LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), 1, 1, 0, IMAGE_BITMAP, 0);
155 ok (himl != 0, "Expected ImageList_LoadImage to succeed\n");
156
157 memset(&imlData, 0, sizeof(imlData));
158 imlData.himl = himl;
159 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
160 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
161
162 memset(&s, 0, sizeof(s));
163 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
164 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
165 ok (s.cx > 80, "Expected big cx\n");
166 ok (s.cy > 80, "Expected big cy\n");
167
168 DestroyWindow(hwnd1);
169
170
171
172
173 hwnd1 = CreateWindowW(L"Button", L" ", BS_BITMAP , 10, 10, 100, 100, 0, NULL, NULL, NULL);
174 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
175 SetWindowTheme(hwnd1, L"", L"");
176
177 SendMessageW(hwnd1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
178
179 memset(&s, 0, sizeof(s));
180 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
181 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
182
183 /* In xp and 2k3 the image is ignored, in vista+ its width is added to the text width */
184 ok_size(s, textent.cx + 5 + 2,
185 textent.cy + 7 + 2); /* the last +2 is the text margin */
186
187 DestroyWindow(hwnd1);
188
189
190
191 hwnd1 = CreateWindowW(L"Button", L" ", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
192 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
193 SetWindowTheme(hwnd1, L"", L"");
194
195 SetRect(&rc, 0,0,0,0);
196 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
197 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
198
199 memset(&s, 0, sizeof(s));
200 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
201 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
202 ok_size(s, textent.cx + 5,
203 textent.cy + 7);
204
205 SetRect(&rc, 50,50,50,50);
206 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
207 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
208
209 memset(&s, 0, sizeof(s));
210 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
211 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
212 ok_size(s, textent.cx + 5 + 100,
213 textent.cy + 7 + 100);
214
215 SetRect(&rc, 1,1,1,1);
216 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
217 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
218
219 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
220 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
221
222 memset(&s, 0, sizeof(s));
223 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
224 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
225 ok_size(s, textent.cx + 5 + 2 + 1, /* we get an extra pixel due to the iml */
226 textent.cy + 7 + 2);
227
228 s.cx = 1;
229 s.cy = 1;
230 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
231 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
232 ok_size(s, textent.cx + 5 + 2 + 1,
233 textent.cy + 7 + 2);
234
235 s.cx = 100;
236 s.cy = 100;
237 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
238 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
239 ok_size(s, textent.cx + 5 + 2 + 1,
240 textent.cy + 7 + 2);
241
242 SetRect(&imlData.margin, 1,1,1,1);
243 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
244 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
245
246 memset(&s, 0, sizeof(s));
247 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
248 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
249 /* expected width = text width + hardcoded value + text margins + image width + image margins */
250 ok_size(s, textent.cx + 5 + 2 + 1 + 2,
251 textent.cy + 7 + 2);
252
253 SetRect(&imlData.margin, 50,50,50,50);
254 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
255 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
256
257 memset(&s, 0, sizeof(s));
258 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
259 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
260 /* image + its margins is so big that the height is dictated by them */
261 ok_size(s, textent.cx + 5 + 2 + 1 + 100, (LONG)101);
262
263 DestroyWindow(hwnd1);
264
265
266
267
268
269
270 hwnd1 = CreateWindowW(L"Button", L"Start", BS_VCENTER, 0, 0, 0, 0, 0, NULL, NULL, NULL);
271 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
272 SetWindowTheme(hwnd1, L"", L"");
273
274 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
275 hdc = GetDC(hwnd1);
276 SelectObject(hdc, font);
277 GetTextExtentPoint32W(hdc, L"Start", 5, &textent);
278
279 SetRect(&rc, 0,0,0,0);
280 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
281 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
282
283 memset(&s, 0, sizeof(s));
284 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
285 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
286 ok_size(s, textent.cx + 5, textent.cy + 7);
287
288 DestroyWindow(hwnd1);
289
290
291
292
293 /* Test again with some real text to see if the formula is correct */
294 hwnd1 = CreateWindowW(L"Button", L"Some test text", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
295 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
296 SetWindowTheme(hwnd1, L"", L"");
297
298 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
299 hdc = GetDC(hwnd1);
300 SelectObject(hdc, font);
301 GetTextExtentPoint32W(hdc, L"Some test text", 14, &textent);
302
303 memset(&s, 0, sizeof(s));
304 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
305 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
306 ok_size(s, textent.cx + 5 + 2, /* the last +2 is the text margin */
307 textent.cy + 7 + 2);
308
309 /* The hardcoded values are independent of the margin */
310 lf.lfHeight = 200;
311 lf.lfWidth = 200;
312 lf.lfWeight = FW_BOLD;
313 wcscpy(lf.lfFaceName, L"Arial");
314 font = CreateFontIndirectW(&lf);
315 ok(font != NULL, "\n");
316 SendMessageW(hwnd1, WM_SETFONT, (WPARAM)font, FALSE);
317
318 SelectObject(hdc, font);
319 GetTextExtentPoint32W(hdc, L"Some test text", 14, &textent);
320
321 memset(&s, 0, sizeof(s));
322 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
323 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
324 ok_size(s, textent.cx + 5 + 2, /* the last +2 is the text margin */
325 textent.cy + 7 + 2);
326
327 DestroyWindow(hwnd1);
328
329
330
331
332 }
333
334 START_TEST(button)
335 {
336 LoadLibraryW(L"comctl32.dll"); /* same as statically linking to comctl32 and doing InitCommonControls */
337 Test_TextMargin();
338 Test_Imagelist();
339 Test_GetIdealSizeNoThemes();
340 }
341