user32: Add more window style tests, make them pass under Wine.
[wine] / dlls / user32 / tests / scroll.c
1 /*
2  * Unit tests for scrollbar
3  *
4  * Copyright 2008 Lyutin Anatoly (Etersoft)
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 <stdarg.h>
23 #include <stdio.h>
24 #include <windows.h>
25
26 #include "wine/test.h"
27
28 static HWND hScroll, hMainWnd;
29 static BOOL bThemeActive = FALSE;
30
31 static LRESULT CALLBACK MyWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
32 {
33     switch(msg)
34     {
35
36     case WM_CREATE:
37     {
38         hScroll = CreateWindowA( "SCROLLBAR", "", WS_CHILD | WS_VISIBLE, 0, 0, 120, 100, hWnd, (HMENU)100, GetModuleHandleA(0), 0 );
39
40         return 0;
41     }
42     case WM_DESTROY:
43         PostQuitMessage(0);
44         break;
45     case WM_HSCROLL:
46     case WM_VSCROLL:
47         /* stop tracking */
48         ReleaseCapture();
49         return 0;
50     default:
51         return DefWindowProcA(hWnd, msg, wParam, lParam);
52     }
53     return 0;
54 }
55 static void scrollbar_test_track(void)
56 {
57     /* test that scrollbar tracking is terminated when
58      * the control looses mouse capture */
59     SendMessage( hScroll, WM_LBUTTONDOWN, 0, MAKELPARAM( 1, 1));
60     /* a normal return from the sendmessage */
61     /* not normal for instance by closing the windws */
62     ok( IsWindow( hScroll), "Scrollbar has gone!\n");
63 }
64
65 static void scrollbar_test1(void)
66 {
67     BOOL ret;
68
69     ret = EnableScrollBar( hScroll, SB_CTL, ESB_DISABLE_BOTH );
70     ok( ret, "The scrollbar should be disabled.\n" );
71     ok( !IsWindowEnabled( hScroll ), "The scrollbar window should be disabled.\n" );
72
73     ret = EnableScrollBar( hScroll, SB_CTL, ESB_ENABLE_BOTH );
74     ok( ret, "The scrollbar should be enabled.\n" );
75     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
76
77     /* test buttons separately */
78     ret = EnableScrollBar( hScroll, SB_CTL, ESB_DISABLE_LTUP );
79     ok( ret, "The scrollbar LTUP button should be disabled.\n" );
80     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
81     ret = EnableScrollBar( hScroll, SB_CTL, ESB_ENABLE_BOTH );
82     ok( ret, "The scrollbar should be enabled.\n" );
83     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
84
85     ret = EnableScrollBar( hScroll, SB_CTL, ESB_DISABLE_RTDN );
86     ok( ret, "The scrollbar RTDN button should be disabled.\n" );
87     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
88     ret = EnableScrollBar( hScroll, SB_CTL, ESB_ENABLE_BOTH );
89     ok( ret, "The scrollbar should be enabled.\n" );
90     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
91 }
92
93 static void scrollbar_test2(void)
94 {
95     int ret;
96
97     trace("The scrollbar is disabled.\n");
98
99     EnableWindow( hScroll, FALSE );
100     ok( !IsWindowEnabled( hScroll ), "The scroll should be disabled.\n" );
101
102     ret = SetScrollPos( hScroll, SB_CTL, 30, TRUE);
103     ok( !ret, "The position should not be set.\n" );
104
105     ret = GetScrollPos( hScroll, SB_CTL);
106     ok( !ret, "The position should be equal to zero\n");
107
108     ret = SetScrollRange( hScroll, SB_CTL, 0, 100, TRUE );
109     ok( ret, "The range should be set.\n" );
110
111     ret = SetScrollPos( hScroll, SB_CTL, 30, TRUE);
112     ok( !ret , "The position should not be set.\n" );
113
114     ret = GetScrollPos( hScroll, SB_CTL);
115     ok( ret == 30, "The position should be set!!!\n");
116
117     trace("The scrollbar is enabled.\n");
118
119     EnableWindow( hScroll, TRUE );
120     ok( IsWindowEnabled( hScroll ), "The scroll should be enabled.\n" );
121
122     ret = SetScrollPos( hScroll, SB_CTL, 30, TRUE);
123     ok( ret == 30, "The position should be set.\n" );
124
125     ret = GetScrollPos( hScroll, SB_CTL);
126     ok( ret == 30, "The position should not be equal to zero\n");
127
128     ret = SetScrollRange( hScroll, SB_CTL, 0, 100, TRUE );
129     ok( ret, "The range should be set.\n" );
130
131     ret = SetScrollPos( hScroll, SB_CTL, 30, TRUE);
132     ok( ret == 30, "The position should be set.\n" );
133
134     ret = GetScrollPos( hScroll, SB_CTL);
135     ok( ret == 30, "The position should not be equal to zero\n");
136 }
137
138 static void scrollbar_test3(void)
139 {
140     BOOL    ret;
141
142     ret = ShowScrollBar( hScroll, SB_CTL, FALSE );
143     ok( ret, "The ShowScrollBar() should not failed.\n" );
144     ok( !IsWindowVisible( hScroll ), "The scrollbar window should not be visible\n" );
145
146     ret = ShowScrollBar( hScroll, SB_CTL, TRUE );
147     ok( ret, "The ShowScrollBar() should not failed.\n" );
148     ok( !IsWindowVisible( hScroll ), "The scrollbar window should be visible\n" );
149
150     ret = ShowScrollBar( NULL, SB_CTL, TRUE );
151     ok( !ret, "The ShowScrollBar() should failed.\n" );
152
153 }
154
155 static void scrollbar_test4(void)
156 {
157     BOOL ret;
158     SCROLLBARINFO sbi;
159     RECT rect;
160     BOOL (WINAPI *pGetScrollBarInfo)(HWND, LONG, LPSCROLLBARINFO);
161
162     pGetScrollBarInfo = (void*)GetProcAddress(GetModuleHandleA("user32.dll"), "GetScrollBarInfo");
163     if (!pGetScrollBarInfo)
164     {
165         win_skip("GetScrollBarInfo is not available\n");
166         return;
167     }
168
169     /* Test GetScrollBarInfo to make sure it returns rcScrollBar in screen
170      * coordinates. */
171     sbi.cbSize = sizeof(sbi);
172     ret = pGetScrollBarInfo( hScroll, OBJID_CLIENT, &sbi);
173     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
174     GetWindowRect( hScroll, &rect );
175     ok( ret, "The GetWindowRect() call should not fail.\n" );
176     ok( !(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)),
177         "unexpected rgstate(0x%x)\n", sbi.rgstate[0]);
178     ok( EqualRect(&rect, &sbi.rcScrollBar),
179         "WindowRect(%d, %d, %d, %d) != rcScrollBar(%d, %d, %d, %d)\n",
180         rect.top, rect.left, rect.bottom, rect.right,
181         sbi.rcScrollBar.top, sbi.rcScrollBar.left,
182         sbi.rcScrollBar.bottom, sbi.rcScrollBar.right );
183
184     /* Test windows horizontal and vertical scrollbar to make sure rcScrollBar
185      * is still returned in screen coordinates by moving the window, and
186      * making sure that it shifts the rcScrollBar value. */
187     ShowWindow( hMainWnd, SW_SHOW );
188     sbi.cbSize = sizeof(sbi);
189     ret = pGetScrollBarInfo( hMainWnd, OBJID_HSCROLL, &sbi);
190     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
191     GetWindowRect( hMainWnd, &rect );
192     ok( ret, "The GetWindowRect() call should not fail.\n" );
193     MoveWindow( hMainWnd, rect.left+5, rect.top+5,
194                 rect.right-rect.left, rect.bottom-rect.top, TRUE );
195     rect = sbi.rcScrollBar;
196     OffsetRect(&rect, 5, 5);
197     ret = pGetScrollBarInfo( hMainWnd, OBJID_HSCROLL, &sbi);
198     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
199     ok( EqualRect(&rect, &sbi.rcScrollBar),
200         "PreviousRect(%d, %d, %d, %d) != CurrentRect(%d, %d, %d, %d)\n",
201         rect.top, rect.left, rect.bottom, rect.right,
202         sbi.rcScrollBar.top, sbi.rcScrollBar.left,
203         sbi.rcScrollBar.bottom, sbi.rcScrollBar.right );
204
205     sbi.cbSize = sizeof(sbi);
206     ret = pGetScrollBarInfo( hMainWnd, OBJID_VSCROLL, &sbi);
207     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
208     GetWindowRect( hMainWnd, &rect );
209     ok( ret, "The GetWindowRect() call should not fail.\n" );
210     MoveWindow( hMainWnd, rect.left+5, rect.top+5,
211                 rect.right-rect.left, rect.bottom-rect.top, TRUE );
212     rect = sbi.rcScrollBar;
213     OffsetRect(&rect, 5, 5);
214     ret = pGetScrollBarInfo( hMainWnd, OBJID_VSCROLL, &sbi);
215     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
216     ok( EqualRect(&rect, &sbi.rcScrollBar),
217         "PreviousRect(%d, %d, %d, %d) != CurrentRect(%d, %d, %d, %d)\n",
218         rect.top, rect.left, rect.bottom, rect.right,
219         sbi.rcScrollBar.top, sbi.rcScrollBar.left,
220         sbi.rcScrollBar.bottom, sbi.rcScrollBar.right );
221 }
222
223 /* some tests designed to show that Horizontal and Vertical
224  * window scroll bar info are not created independently */
225 static void scrollbar_test_default( DWORD style)
226 {
227     INT min, max, ret;
228     DWORD winstyle;
229     HWND hwnd;
230     SCROLLINFO si = { sizeof( SCROLLINFO), SIF_TRACKPOS };
231
232     hwnd = CreateWindowExA( 0, "static", "", WS_POPUP | style,
233                 0, 0, 10, 10, 0, 0, 0, NULL);
234     assert( hwnd != 0);
235
236     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
237     ok( ret ||
238             broken( !ret) /* Win 9x/ME */ , "GetScrollRange failed.\n");
239     /* range is 0,0 if there are no H or V scroll bars. 0,100 otherwise */
240     if( !( style & ( WS_VSCROLL | WS_HSCROLL)))
241         ok( min == 0 && max == 0,
242                 "Scroll bar range is %d,%d. Expected 0,0. Style %08x\n", min, max, style);
243     else
244 todo_wine
245         ok(( min == 0 && max == 100) ||
246                 broken( min == 0 && max == 0), /* Win 9x/ME */
247                 "Scroll bar range is %d,%d. Expected 0,100. Style %08x\n", min, max, style);
248     ret = GetScrollRange( hwnd, SB_HORZ, &min, &max);
249     ok( ret ||
250             broken( !ret) /* Win 9x/ME */ , "GetScrollRange failed.\n");
251     /* range is 0,0 if there are no H or V scroll bars. 0,100 otherwise */
252     if( !( style & ( WS_VSCROLL | WS_HSCROLL)))
253         ok( min == 0 && max == 0,
254                 "Scroll bar range is %d,%d. Expected 0,0. Style %08x\n", min, max, style);
255     else
256 todo_wine
257         ok(( min == 0 && max == 100) ||
258                 broken( min == 0 && max == 0), /* Win 9x/ME */
259                 "Scroll bar range is %d,%d. Expected 0,100. Style %08x\n", min, max, style);
260     /* test GetScrollInfo, vist for vertical SB */
261     ret = GetScrollInfo( hwnd, SB_VERT, &si);
262     /* should fail if no H or V scroll bar styles are present. Succeed otherwise */
263     if( !( style & ( WS_VSCROLL | WS_HSCROLL)))
264         ok( !ret, "GetScrollInfo succeeded unexpectedly. Style is %08x\n", style);
265     else
266 todo_wine
267         ok( ret ||
268                 broken( !ret), /* Win 9x/ME */
269                 "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
270     /* Same for Horizontal SB */
271     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
272     /* should fail if no H or V scroll bar styles are present. Succeed otherwise */
273     if( !( style & ( WS_VSCROLL | WS_HSCROLL)))
274         ok( !ret, "GetScrollInfo succeeded unexpectedly. Style is %08x\n", style);
275     else
276 todo_wine
277         ok( ret ||
278                 broken( !ret), /* Win 9x/ME */
279                 "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
280     /* now set the Vertical Scroll range to something that could be the default value it
281      * already has */;
282     ret = SetScrollRange( hwnd, SB_VERT, 0, 100, FALSE);
283     ok( ret, "SetScrollRange failed.\n");
284     /* and request the Horizontal range */
285     ret = GetScrollRange( hwnd, SB_HORZ, &min, &max);
286     ok( ret, "GetScrollRange failed.\n");
287     /* now the range should be 0,100 in ALL cases */
288     ok( min == 0 && max == 100,
289             "Scroll bar range is %d,%d. Expected 0,100. Style %08x\n", min, max, style);
290     /* See what is different now for GetScrollRange */
291     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
292     /* should succeed in ALL cases */
293     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
294     ret = GetScrollInfo( hwnd, SB_VERT, &si);
295     /* should succeed in ALL cases */
296     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
297     /* report the windows style */
298     winstyle = GetWindowLongA( hwnd, GWL_STYLE );
299     /* WS_VSCROLL added to the window style */
300     if( !(style & WS_VSCROLL))
301     {
302         if (bThemeActive || style != WS_HSCROLL)
303 todo_wine
304             ok( (winstyle & (WS_HSCROLL|WS_VSCROLL)) == ( style | WS_VSCROLL),
305                     "unexpected style change %08x expected %08x\n",
306                     (winstyle & (WS_HSCROLL|WS_VSCROLL)), style | WS_VSCROLL);
307         else
308             ok( (winstyle & (WS_HSCROLL|WS_VSCROLL)) == style ||
309                     broken((winstyle & (WS_HSCROLL|WS_VSCROLL)) == (WS_HSCROLL|WS_VSCROLL)), /* Win 9x/ME */
310                     "unexpected style change %08x expected %08x\n",
311                     (winstyle & (WS_HSCROLL|WS_VSCROLL)), style);
312     }
313     /* do the test again with H and V reversed.
314      * Start with a clean window */
315     DestroyWindow( hwnd);
316     hwnd = CreateWindowExA( 0, "static", "", WS_POPUP | style,
317                 0, 0, 10, 10, 0, 0, 0, NULL);
318     assert( hwnd != 0);
319     /* Set Horizontal Scroll range to something that could be the default value it
320      * already has */;
321     ret = SetScrollRange( hwnd, SB_HORZ, 0, 100, FALSE);
322     ok( ret, "SetScrollRange failed.\n");
323     /* and request the Vertical range */
324     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
325     ok( ret, "GetScrollRange failed.\n");
326     /* now the range should be 0,100 in ALL cases */
327     ok( min == 0 && max == 100,
328             "Scroll bar range is %d,%d. Expected 0,100. Style %08x\n", min, max, style);
329     /* See what is different now for GetScrollRange */
330     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
331     /* should succeed in ALL cases */
332     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
333     ret = GetScrollInfo( hwnd, SB_VERT, &si);
334     /* should succeed in ALL cases */
335     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
336     /* report the windows style */
337     winstyle = GetWindowLongA( hwnd, GWL_STYLE );
338     /* WS_HSCROLL added to the window style */
339     if( !(style & WS_HSCROLL))
340     {
341         if (bThemeActive || style != WS_VSCROLL)
342 todo_wine
343             ok( (winstyle & (WS_HSCROLL|WS_VSCROLL)) == ( style | WS_HSCROLL),
344                     "unexpected style change %08x expected %08x\n",
345                     (winstyle & (WS_HSCROLL|WS_VSCROLL)), style | WS_HSCROLL);
346         else
347             ok( (winstyle & (WS_HSCROLL|WS_VSCROLL)) == style ||
348                     broken((winstyle & (WS_HSCROLL|WS_VSCROLL)) == (WS_HSCROLL|WS_VSCROLL)), /* Win 9x/ME */
349                     "unexpected style change %08x expected %08x\n",
350                     (winstyle & (WS_HSCROLL|WS_VSCROLL)), style);
351     }
352     /* Slightly change the test to use SetScrollInfo
353      * Start with a clean window */
354     DestroyWindow( hwnd);
355     hwnd = CreateWindowExA( 0, "static", "", WS_POPUP | style,
356                 0, 0, 10, 10, 0, 0, 0, NULL);
357     assert( hwnd != 0);
358     /* set Horizontal position with SetScrollInfo */
359     si.nPos = 0;
360     si.nMin = 11;
361     si.nMax = 22;
362     si.fMask |= SIF_RANGE;
363     ret = SetScrollInfo( hwnd, SB_HORZ, &si, FALSE);
364     ok( ret, "SetScrollInfo failed. Style is %08x\n", style);
365     /* and request the Vertical range */
366     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
367     ok( ret, "GetScrollRange failed.\n");
368     /* now the range should be 0,100 in ALL cases */
369     ok( min == 0 && max == 100,
370             "Scroll bar range is %d,%d. Expected 0,100. Style %08x\n", min, max, style);
371     /* See what is different now for GetScrollRange */
372     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
373     /* should succeed in ALL cases */
374     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
375     ret = GetScrollInfo( hwnd, SB_VERT, &si);
376     /* should succeed in ALL cases */
377     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
378     /* also test if the window scroll bars are enabled */
379     ret = EnableScrollBar( hwnd, SB_VERT, ESB_ENABLE_BOTH);
380     ok( !ret, "Vertical window scroll bar was not enabled\n");
381     ret = EnableScrollBar( hwnd, SB_HORZ, ESB_ENABLE_BOTH);
382     ok( !ret, "Horizontal window scroll bar was not enabled\n");
383     DestroyWindow( hwnd);
384     /* finally, check if adding a WS_[HV]SColl style of a  window makes the scroll info
385      * available */
386     if( style & (WS_HSCROLL | WS_VSCROLL)) return;/* only test if not yet set */
387     /* Start with a clean window */
388     DestroyWindow( hwnd);
389     hwnd = CreateWindowExA( 0, "static", "", WS_POPUP ,
390                 0, 0, 10, 10, 0, 0, 0, NULL);
391     assert( hwnd != 0);
392     ret = GetScrollInfo( hwnd, SB_VERT, &si);
393     /* should fail */
394     ok( !ret, "GetScrollInfo succeeded unexpectedly. Style is %08x\n", style);
395     /* add scroll styles */
396     winstyle = GetWindowLongA( hwnd, GWL_STYLE );
397     SetWindowLongW( hwnd, GWL_STYLE, winstyle | WS_VSCROLL | WS_HSCROLL);
398     ret = GetScrollInfo( hwnd, SB_VERT, &si);
399     /* should still fail */
400     ok( !ret, "GetScrollInfo succeeded unexpectedly. Style is %08x\n", style);
401     /* clean up */
402     DestroyWindow( hwnd);
403 }
404
405 START_TEST ( scroll )
406 {
407     WNDCLASSA wc;
408     HMODULE hUxtheme;
409     BOOL (WINAPI * pIsThemeActive)(VOID);
410
411     wc.style = CS_HREDRAW | CS_VREDRAW;
412     wc.cbClsExtra = 0;
413     wc.cbWndExtra = 0;
414     wc.hInstance = GetModuleHandleA(NULL);
415     wc.hIcon = NULL;
416     wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
417     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
418     wc.lpszMenuName = NULL;
419     wc.lpszClassName = "MyTestWnd";
420     wc.lpfnWndProc = MyWndProc;
421     RegisterClassA(&wc);
422
423     hMainWnd = CreateWindowExA( 0, "MyTestWnd", "Scroll",
424       WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,
425       CW_USEDEFAULT, CW_USEDEFAULT, 100, 100, NULL, NULL, GetModuleHandleA(NULL), 0 );
426
427     ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n");
428     if (!hMainWnd) return;
429
430     assert( hScroll );
431
432     scrollbar_test1();
433     scrollbar_test2();
434     scrollbar_test3();
435     scrollbar_test4();
436     scrollbar_test_track();
437
438     /* Some test results vary depending of theming being active or not */
439     hUxtheme = LoadLibraryA("uxtheme.dll");
440     if (hUxtheme)
441     {
442         pIsThemeActive = (void*)GetProcAddress(hUxtheme, "IsThemeActive");
443         if (pIsThemeActive)
444             bThemeActive = pIsThemeActive();
445         FreeLibrary(hUxtheme);
446     }
447
448     scrollbar_test_default( 0);
449     scrollbar_test_default( WS_HSCROLL);
450     scrollbar_test_default( WS_VSCROLL);
451     scrollbar_test_default( WS_HSCROLL | WS_VSCROLL);
452
453     DestroyWindow(hScroll);
454     DestroyWindow(hMainWnd);
455 }