comctl32: Allow a little slop in the status bar metric tests.
[wine] / dlls / comctl32 / tests / status.c
1 /* Unit test suite for status control.
2  *
3  * Copyright 2007 Google (Lei Zhang)
4  * Copyright 2007 Alex Arazi
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <windows.h>
23 #include <commctrl.h>
24
25 #include "wine/test.h"
26
27 #define SUBCLASS_NAME "MyStatusBar"
28
29 #define expect(expected,got) ok (expected == got,"Expected %d, got %d\n",expected,got)
30 #define expect_rect(_left,_top,_right,_bottom,got) do { \
31         RECT exp = {abs(got.left - _left), abs(got.top - _top), \
32                     abs(got.right - _right), abs(got.bottom - _bottom)}; \
33         ok(exp.left <= 2 && exp.top <= 2 && exp.right <= 2 && exp.bottom <= 2, \
34            "Expected rect {%d,%d, %d,%d}, got {%d,%d, %d,%d}\n", \
35            _left, _top, _right, _bottom, \
36            (got).left, (got).top, (got).right, (got).bottom); } while (0)
37
38 static HINSTANCE hinst;
39 static WNDPROC g_status_wndproc;
40 static RECT g_rcCreated;
41 static HWND g_hMainWnd;
42 static int g_wmsize_count = 0;
43
44 static HWND create_status_control(DWORD style, DWORD exstyle)
45 {
46     HWND hWndStatus;
47
48     /* make the control */
49     hWndStatus = CreateWindowEx(exstyle, STATUSCLASSNAME, NULL, style,
50         /* placement */
51         0, 0, 300, 20,
52         /* parent, etc */
53         NULL, NULL, hinst, NULL);
54     assert (hWndStatus);
55     return hWndStatus;
56 }
57
58 static LRESULT WINAPI create_test_wndproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
59 {
60     LRESULT ret;
61
62     if (msg == WM_CREATE)
63     {
64         CREATESTRUCT *cs = (CREATESTRUCT *)lParam;
65         ret = CallWindowProc(g_status_wndproc, hwnd, msg, wParam, lParam);
66         GetWindowRect(hwnd, &g_rcCreated);
67         MapWindowPoints(HWND_DESKTOP, g_hMainWnd, (LPPOINT)&g_rcCreated, 2);
68         ok(cs->x == g_rcCreated.left, "CREATESTRUCT.x modified\n");
69         ok(cs->y == g_rcCreated.top, "CREATESTRUCT.y modified\n");
70     } else if (msg == WM_SIZE)
71     {
72         g_wmsize_count++;
73         ret = CallWindowProc(g_status_wndproc, hwnd, msg, wParam, lParam);
74     }
75     else
76         ret = CallWindowProc(g_status_wndproc, hwnd, msg, wParam, lParam);
77
78     return ret;
79 }
80
81 static void register_subclass()
82 {
83     WNDCLASSEX cls;
84
85     cls.cbSize = sizeof(WNDCLASSEX);
86     GetClassInfoEx(NULL, STATUSCLASSNAME, &cls);
87     g_status_wndproc = cls.lpfnWndProc;
88     cls.lpfnWndProc = create_test_wndproc;
89     cls.lpszClassName = SUBCLASS_NAME;
90     cls.hInstance = NULL;
91     ok(RegisterClassEx(&cls), "RegisterClassEx failed\n");
92 }
93
94 static void test_create()
95 {
96     RECT rc;
97     HWND hwnd;
98
99     ok((hwnd = CreateWindowA(SUBCLASS_NAME, "", WS_CHILD|WS_VISIBLE|SBARS_SIZEGRIP, 0, 0, 100, 100,
100         g_hMainWnd, NULL, NULL, 0)) != NULL, "CreateWindowA failed\n");
101     MapWindowPoints(HWND_DESKTOP, g_hMainWnd, (LPPOINT)&rc, 2);
102     GetWindowRect(hwnd, &rc);
103     MapWindowPoints(HWND_DESKTOP, g_hMainWnd, (LPPOINT)&rc, 2);
104     expect_rect(0, 0, 100, 100, g_rcCreated);
105     expect(0, rc.left);
106     expect(672, rc.right);
107     expect(226, rc.bottom);
108     /* we don't check rc.top as this may depend on user font settings */
109     DestroyWindow(hwnd);
110 }
111
112 static int CALLBACK check_height_font_enumproc(ENUMLOGFONTEX *enumlf, NEWTEXTMETRICEX *ntm, DWORD type, LPARAM lParam)
113 {
114     HWND hwndStatus = (HWND)lParam;
115     HDC hdc = GetDC(NULL);
116     static const int sizes[] = {8, 9, 10, 12, 16, 22, 28, 36, 48, 72};
117     int i;
118
119     trace("Font %s\n", enumlf->elfFullName);
120     for (i = 0; i < sizeof(sizes)/sizeof(sizes[0]); i++)
121     {
122         HFONT hFont;
123         TEXTMETRIC tm;
124         HFONT hCtrlFont;
125         HFONT hOldFont;
126         RECT rcCtrl;
127
128         enumlf->elfLogFont.lfHeight = sizes[i];
129         hFont = CreateFontIndirect(&enumlf->elfLogFont);
130         hCtrlFont = (HFONT)SendMessage(hwndStatus, WM_SETFONT, (WPARAM)hFont, TRUE);
131         hOldFont = SelectObject(hdc, hFont);
132
133         GetClientRect(hwndStatus, &rcCtrl);
134         GetTextMetrics(hdc, &tm);
135         expect(max(tm.tmHeight + (tm.tmInternalLeading ? tm.tmInternalLeading : 2) + 4, 20), rcCtrl.bottom);
136
137         SelectObject(hdc, hOldFont);
138         SendMessage(hwndStatus, WM_SETFONT, (WPARAM)hCtrlFont, TRUE);
139         DeleteObject(hFont);
140     }
141     ReleaseDC(NULL, hdc);
142     return 1;
143 }
144
145 static int CALLBACK check_height_family_enumproc(ENUMLOGFONTEX *enumlf, NEWTEXTMETRICEX *ntm, DWORD type, LPARAM lParam)
146 {
147     HDC hdc = GetDC(NULL);
148     enumlf->elfLogFont.lfHeight = 0;
149     EnumFontFamiliesEx(hdc, &enumlf->elfLogFont, (FONTENUMPROC)check_height_font_enumproc, lParam, 0);
150     ReleaseDC(NULL, hdc);
151     return 1;
152 }
153
154 static void test_height(void)
155 {
156     LOGFONT lf;
157     HFONT hFont, hFontSm;
158     RECT rc1, rc2;
159     HWND hwndStatus = CreateWindow(SUBCLASS_NAME, NULL, WS_CHILD|WS_VISIBLE,
160         0, 0, 300, 20, g_hMainWnd, NULL, NULL, NULL);
161     HDC hdc;
162
163     GetClientRect(hwndStatus, &rc1);
164     hFont = CreateFont(32, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
165         OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, "Tahoma");
166
167     g_wmsize_count = 0;
168     SendMessage(hwndStatus, WM_SETFONT, (WPARAM)hFont, TRUE);
169     ok(g_wmsize_count > 0, "WM_SETFONT should issue WM_SIZE\n");
170
171     GetClientRect(hwndStatus, &rc2);
172     expect_rect(0, 0, 672, 42, rc2); /* GetTextMetrics returns invalid tmInternalLeading for this font */
173
174     g_wmsize_count = 0;
175     SendMessage(hwndStatus, WM_SETFONT, (WPARAM)hFont, TRUE);
176     ok(g_wmsize_count > 0, "WM_SETFONT should issue WM_SIZE\n");
177
178     GetClientRect(hwndStatus, &rc2);
179     expect_rect(0, 0, 672, 42, rc2);
180
181     /* minheight < fontsize - no effects*/
182     SendMessage(hwndStatus, SB_SETMINHEIGHT, 12, 0);
183     SendMessage(hwndStatus, WM_SIZE, 0, 0);
184     GetClientRect(hwndStatus, &rc2);
185     expect_rect(0, 0, 672, 42, rc2);
186
187     /* minheight > fontsize - has an effect after WM_SIZE */
188     SendMessage(hwndStatus, SB_SETMINHEIGHT, 60, 0);
189     GetClientRect(hwndStatus, &rc2);
190     expect_rect(0, 0, 672, 42, rc2);
191     SendMessage(hwndStatus, WM_SIZE, 0, 0);
192     GetClientRect(hwndStatus, &rc2);
193     expect_rect(0, 0, 672, 62, rc2);
194
195     /* font changed to smaller than minheight - has an effect */
196     SendMessage(hwndStatus, SB_SETMINHEIGHT, 30, 0);
197     expect_rect(0, 0, 672, 62, rc2);
198     SendMessage(hwndStatus, WM_SIZE, 0, 0);
199     GetClientRect(hwndStatus, &rc2);
200     expect_rect(0, 0, 672, 42, rc2);
201     hFontSm = CreateFont(9, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
202         OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, "Tahoma");
203     SendMessage(hwndStatus, WM_SETFONT, (WPARAM)hFontSm, TRUE);
204     GetClientRect(hwndStatus, &rc2);
205     expect_rect(0, 0, 672, 32, rc2);
206
207     /* test the height formula */
208     ZeroMemory(&lf, sizeof(lf));
209     SendMessage(hwndStatus, SB_SETMINHEIGHT, 0, 0);
210     hdc = GetDC(NULL);
211     trace("dpi=%d\n", GetDeviceCaps(hdc, LOGPIXELSY));
212     EnumFontFamiliesEx(hdc, &lf, (FONTENUMPROC)check_height_family_enumproc, (LPARAM)hwndStatus, 0);
213     ReleaseDC(NULL, hdc);
214
215     DestroyWindow(hwndStatus);
216     DeleteObject(hFont);
217     DeleteObject(hFontSm);
218 }
219
220 static void test_status_control(void)
221 {
222     HWND hWndStatus;
223     int r;
224     int nParts[] = {50, 150, -1};
225     int checkParts[] = {0, 0, 0};
226     int borders[] = {0, 0, 0};
227     RECT rc;
228     CHAR charArray[20];
229     HICON hIcon;
230
231     hWndStatus = create_status_control(WS_VISIBLE, 0);
232
233     /* Divide into parts and set text */
234     r = SendMessage(hWndStatus, SB_SETPARTS, 3, (LPARAM)nParts);
235     expect(TRUE,r);
236     r = SendMessage(hWndStatus, SB_SETTEXT, 0, (LPARAM)"First");
237     expect(TRUE,r);
238     r = SendMessage(hWndStatus, SB_SETTEXT, 1, (LPARAM)"Second");
239     expect(TRUE,r);
240     r = SendMessage(hWndStatus, SB_SETTEXT, 2, (LPARAM)"Third");
241     expect(TRUE,r);
242
243     /* Get RECT Information */
244     r = SendMessage(hWndStatus, SB_GETRECT, 0, (LPARAM)&rc);
245     expect(TRUE,r);
246     expect(2,rc.top);
247     /* The rc.bottom test is system dependent
248     expect(22,rc.bottom); */
249     expect(0,rc.left);
250     expect(50,rc.right);
251     r = SendMessage(hWndStatus, SB_GETRECT, -1, (LPARAM)&rc);
252     expect(FALSE,r);
253     r = SendMessage(hWndStatus, SB_GETRECT, 3, (LPARAM)&rc);
254     expect(FALSE,r);
255     /* Get text length and text */
256     r = SendMessage(hWndStatus, SB_GETTEXTLENGTH, 2, 0);
257     expect(5,LOWORD(r));
258     expect(0,HIWORD(r));
259     r = SendMessage(hWndStatus, SB_GETTEXT, 2, (LPARAM) charArray);
260     ok(strcmp(charArray,"Third") == 0, "Expected Third, got %s\n", charArray);
261     expect(5,LOWORD(r));
262     expect(0,HIWORD(r));
263
264     /* Get parts and borders */
265     r = SendMessage(hWndStatus, SB_GETPARTS, 3, (LPARAM)checkParts);
266     ok(r == 3, "Expected 3, got %d\n", r);
267     expect(50,checkParts[0]);
268     expect(150,checkParts[1]);
269     expect(-1,checkParts[2]);
270     r = SendMessage(hWndStatus, SB_GETBORDERS, 0, (LPARAM)borders);
271     ok(r == TRUE, "Expected TRUE, got %d\n", r);
272     expect(0,borders[0]);
273     expect(2,borders[1]);
274     expect(2,borders[2]);
275
276     /* Test resetting text with different characters */
277     r = SendMessage(hWndStatus, SB_SETTEXT, 0, (LPARAM)"First@Again");
278     expect(TRUE,r);
279     r = SendMessage(hWndStatus, SB_SETTEXT, 1, (LPARAM)"InvalidChars\\7\7");
280         expect(TRUE,r);
281     r = SendMessage(hWndStatus, SB_SETTEXT, 2, (LPARAM)"InvalidChars\\n\n");
282         expect(TRUE,r);
283
284     /* Get text again */
285     r = SendMessage(hWndStatus, SB_GETTEXT, 0, (LPARAM) charArray);
286     ok(strcmp(charArray,"First@Again") == 0, "Expected First@Again, got %s\n", charArray);
287     expect(11,LOWORD(r));
288     expect(0,HIWORD(r));
289     r = SendMessage(hWndStatus, SB_GETTEXT, 1, (LPARAM) charArray);
290     todo_wine
291     {
292         ok(strcmp(charArray,"InvalidChars\\7 ") == 0, "Expected InvalidChars\\7 , got %s\n", charArray);
293     }
294     expect(15,LOWORD(r));
295     expect(0,HIWORD(r));
296     r = SendMessage(hWndStatus, SB_GETTEXT, 2, (LPARAM) charArray);
297     todo_wine
298     {
299         ok(strcmp(charArray,"InvalidChars\\n ") == 0, "Expected InvalidChars\\n , got %s\n", charArray);
300     }
301     expect(15,LOWORD(r));
302     expect(0,HIWORD(r));
303
304     /* Set background color */
305     r = SendMessage(hWndStatus, SB_SETBKCOLOR , 0, RGB(255,0,0));
306     expect(CLR_DEFAULT,r);
307     r = SendMessage(hWndStatus, SB_SETBKCOLOR , 0, CLR_DEFAULT);
308     expect(RGB(255,0,0),r);
309
310     /* Add an icon to the status bar */
311     hIcon = LoadIcon(NULL, IDI_QUESTION);
312     r = SendMessage(hWndStatus, SB_SETICON, 1, (LPARAM) NULL);
313     ok(r != 0, "Expected non-zero, got %d\n", r);
314     r = SendMessage(hWndStatus, SB_SETICON, 1, (LPARAM) hIcon);
315     ok(r != 0, "Expected non-zero, got %d\n", r);
316     r = SendMessage(hWndStatus, SB_SETICON, 1, (LPARAM) NULL);
317     ok(r != 0, "Expected non-zero, got %d\n", r);
318
319     /* Set the Unicode format */
320     r = SendMessage(hWndStatus, SB_SETUNICODEFORMAT, FALSE, 0);
321     r = SendMessage(hWndStatus, SB_GETUNICODEFORMAT, 0, 0);
322     expect(FALSE,r);
323     r = SendMessage(hWndStatus, SB_SETUNICODEFORMAT, TRUE, 0);
324     expect(FALSE,r);
325     r = SendMessage(hWndStatus, SB_GETUNICODEFORMAT, 0, 0);
326     expect(TRUE,r);
327
328     /* Reset number of parts */
329     r = SendMessage(hWndStatus, SB_SETPARTS, 2, (LPARAM)nParts);
330     expect(TRUE,r);
331
332     /* Set the minimum height and get rectangle information again */
333     SendMessage(hWndStatus, SB_SETMINHEIGHT, 50, (LPARAM) 0);
334     r = SendMessage(hWndStatus, WM_SIZE, 0, (LPARAM) 0);
335     expect(0,r);
336     r = SendMessage(hWndStatus, SB_GETRECT, 0, (LPARAM)&rc);
337     expect(TRUE,r);
338     expect(2,rc.top);
339     /* The rc.bottom test is system dependent
340     expect(22,rc.bottom); */
341     expect(0,rc.left);
342     expect(50,rc.right);
343     r = SendMessage(hWndStatus, SB_GETRECT, -1, (LPARAM)&rc);
344     expect(FALSE,r);
345     r = SendMessage(hWndStatus, SB_GETRECT, 3, (LPARAM)&rc);
346     expect(FALSE,r);
347
348     /* Set the ToolTip text */
349     todo_wine
350     {
351         SendMessage(hWndStatus, SB_SETTIPTEXT, 0,(LPARAM) "Tooltip Text");
352         SendMessage(hWndStatus, SB_GETTIPTEXT, MAKEWPARAM (0, 20),(LPARAM) charArray);
353         ok(strcmp(charArray,"Tooltip Text") == 0, "Expected Tooltip Text, got %s\n", charArray);
354     }
355
356     /* Make simple */
357     SendMessage(hWndStatus, SB_SIMPLE, TRUE, 0);
358     r = SendMessage(hWndStatus, SB_ISSIMPLE, 0, 0);
359     expect(TRUE,r);
360
361     DestroyWindow(hWndStatus);
362 }
363
364 START_TEST(status)
365 {
366     hinst = GetModuleHandleA(NULL);
367
368     g_hMainWnd = CreateWindowExA(0, "static", "", WS_OVERLAPPEDWINDOW,
369       CW_USEDEFAULT, CW_USEDEFAULT, 672+2*GetSystemMetrics(SM_CXSIZEFRAME),
370       226+GetSystemMetrics(SM_CYCAPTION)+2*GetSystemMetrics(SM_CYSIZEFRAME),
371       NULL, NULL, GetModuleHandleA(NULL), 0);
372
373     InitCommonControls();
374
375     register_subclass();
376
377     test_status_control();
378     test_create();
379     test_height();
380 }