99c0841dbea31fd0974bae252ff6d2eedb61d25d
[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
25 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
26 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
27 ok_rect(rc, 1, 1, 1, 1);
28
29 SetRect(&rc, 0,0,0,0);
30 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
31 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
32
33 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
34 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
35 ok_rect(rc, 0, 0, 0, 0);
36
37 SetRect(&rc, -1,-1,-1,-1);
38 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
39 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
40
41 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
42 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
43 ok_rect(rc, -1, -1, -1, -1);
44
45 SetRect(&rc, 1000,1000,1000,1000);
46 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
47 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
48
49 ret = SendMessageW(hwnd1, BCM_GETTEXTMARGIN, 0, (LPARAM)&rc);
50 ok (ret == TRUE, "Expected BCM_GETTEXTMARGIN to succeed\n");
51 ok_rect(rc, 1000, 1000, 1000, 1000);
52
53 DestroyWindow(hwnd1);
54 }
55
56 void Test_Imagelist()
57 {
58 HWND hwnd1;
59 BOOL ret;
60 BUTTON_IMAGELIST imlData;
61
62 hwnd1 = CreateWindowW(L"Button", L"Test2", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
63 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
64
65 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
66 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
67 ok (imlData.himl == 0, "Expected 0 himl\n");
68 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
69 ok_rect(imlData.margin, 0, 0, 0, 0);
70
71 SetRect(&imlData.margin, 0,0,0,1);
72 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
73 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
74
75 imlData.himl = (HIMAGELIST)0xdead;
76 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
77 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n");
78
79 ret = SendMessageW(hwnd1, BCM_GETIMAGELIST, 0, (LPARAM)&imlData);
80 ok (ret == TRUE, "Expected BCM_GETIMAGELIST to succeed\n");
81 ok (imlData.himl == (HIMAGELIST)0xdead, "Expected 0 himl\n");
82 ok (imlData.uAlign == 0, "Expected 0 uAlign\n");
83 ok_rect(imlData.margin, 0, 0, 0, 1);
84
85 imlData.himl = 0;
86 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
87 ok (ret == FALSE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
88
89 DestroyWindow(hwnd1);
90 }
91
92 void Test_GetIdealSizeNoThemes()
93 {
94 HWND hwnd1;
95 BOOL ret;
96 SIZE s, textent;
97 HFONT font;
98 HDC hdc;
99 WINDOWINFO wi;
100 HANDLE hbmp;
101 HIMAGELIST himl;
102 BUTTON_IMAGELIST imlData;
103 RECT rc;
104
105 hwnd1 = CreateWindowW(L"Button", L" ", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
106 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
107 SetWindowTheme(hwnd1, L"", L"");
108
109 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
110 hdc = GetDC(hwnd1);
111 SelectObject(hdc, font);
112 GetTextExtentPoint32W(hdc, L" ", 1, &textent);
113
114 memset(&wi, 0, sizeof(wi));
115 GetWindowInfo(hwnd1, &wi);
116
117 memset(&s, 0, sizeof(s));
118 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
119 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
120 ok_size(s, textent.cx + 2 * wi.cxWindowBorders - 1 + 2,
121 textent.cy + 2 * wi.cyWindowBorders + 1 + 2); /* the last +2 is the text margin */
122
123 DestroyWindow(hwnd1);
124
125 hwnd1 = CreateWindowW(L"Button", L"", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
126 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
127 SetWindowTheme(hwnd1, L"", L"");
128
129 memset(&s, 0, sizeof(s));
130 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
131 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
132 ok (s.cx > 80, "Expected big cx\n");
133 ok (s.cy > 80, "Expected big cy\n");
134
135 s.cx = 1;
136 s.cy = 1;
137 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
138 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
139 ok (s.cx > 80, "Expected big cx\n");
140 ok (s.cy > 80, "Expected big cy\n");
141
142 hbmp = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), IMAGE_BITMAP, 0, 0, 0);
143 ok (hbmp != 0, "Expected LoadImage to succeed\n");
144
145 SendMessageW(hwnd1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
146
147 memset(&s, 0, sizeof(s));
148 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
149 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
150 ok (s.cx > 80, "Expected big cx\n");
151 ok (s.cy > 80, "Expected big cy\n");
152
153 himl = ImageList_LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(5), 1, 1, 0, IMAGE_BITMAP, 0);
154 ok (himl != 0, "Expected ImageList_LoadImage to succeed\n");
155
156 memset(&imlData, 0, sizeof(imlData));
157 imlData.himl = himl;
158 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
159 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
160
161 memset(&s, 0, sizeof(s));
162 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
163 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
164 ok (s.cx > 80, "Expected big cx\n");
165 ok (s.cy > 80, "Expected big cy\n");
166
167 DestroyWindow(hwnd1);
168
169 hwnd1 = CreateWindowW(L"Button", L" ", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
170 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
171 SetWindowTheme(hwnd1, L"", L"");
172
173 SetRect(&rc, 0,0,0,0);
174 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
175 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
176
177 memset(&s, 0, sizeof(s));
178 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
179 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
180 ok_size(s, textent.cx + 2 * wi.cxWindowBorders - 1,
181 textent.cy + 2 * wi.cyWindowBorders + 1);
182
183 SetRect(&rc, 50,50,50,50);
184 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
185 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
186
187 memset(&s, 0, sizeof(s));
188 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
189 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
190 ok_size(s, textent.cx + 2 * wi.cxWindowBorders - 1 + 100,
191 textent.cy + 2 * wi.cyWindowBorders + 1 + 100);
192
193 SetRect(&rc, 0,0,0,0);
194 ret = SendMessageW(hwnd1, BCM_SETTEXTMARGIN, 0, (LPARAM)&rc);
195 ok (ret == TRUE, "Expected BCM_SETTEXTMARGIN to succeed\n");
196
197 ret = SendMessageW(hwnd1, BCM_SETIMAGELIST, 0, (LPARAM)&imlData);
198 ok (ret == TRUE, "Expected BCM_SETIMAGELIST to fail\n"); /* This works in win10 */
199
200 memset(&s, 0, sizeof(s));
201 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
202 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
203 ok_size(s, textent.cx + 2 * wi.cxWindowBorders, /* we get an extra pixel due to the iml */
204 textent.cy + 2 * wi.cyWindowBorders + 1);
205
206 s.cx = 1;
207 s.cy = 1;
208 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
209 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
210 ok_size(s, textent.cx + 2 * wi.cxWindowBorders,
211 textent.cy + 2 * wi.cyWindowBorders + 1);
212
213 s.cx = 100;
214 s.cy = 100;
215 ret = SendMessageW(hwnd1, BCM_GETIDEALSIZE, 0, (LPARAM)&s);
216 ok (ret == TRUE, "Expected BCM_GETIDEALSIZE to succeed\n");
217 ok_size(s, textent.cx + 2 * wi.cxWindowBorders,
218 textent.cy + 2 * wi.cyWindowBorders + 1);
219
220 DestroyWindow(hwnd1);
221
222 /* Test again with some real text to see if the formula is correct */
223 hwnd1 = CreateWindowW(L"Button", L"Some test text", 0, 10, 10, 100, 100, 0, NULL, NULL, NULL);
224 ok (hwnd1 != NULL, "Expected CreateWindowW to succeed\n");
225 SetWindowTheme(hwnd1, L"", L"");
226
227 font = (HFONT)SendMessageW(hwnd1, WM_GETFONT, 0, 0);
228 hdc = GetDC(hwnd1);
229 SelectObject(hdc, font);
230 GetTextExtentPoint32W(hdc, L"Some test text", 14, &textent);
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 + 2 * wi.cxWindowBorders - 1 + 2, /* the last +2 is the text margin */
236 textent.cy + 2 * wi.cyWindowBorders + 1 + 2);
237
238 DestroyWindow(hwnd1);
239 }
240
241 START_TEST(button)
242 {
243 LoadLibraryW(L"comctl32.dll"); /* same as statically linking to comctl32 and doing InitCommonControls */
244 Test_TextMargin();
245 Test_Imagelist();
246 Test_GetIdealSizeNoThemes();
247 }
248