user32: Correct dialog focus behavior.
[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/%08x\n", winstyle, style);
306         else
307             ok( (winstyle & (WS_HSCROLL|WS_VSCROLL)) == style,
308                 "unexpected style change %08x/%08x\n", winstyle, style);
309     }
310     /* do the test again with H and V reversed.
311      * Start with a clean window */
312     DestroyWindow( hwnd);
313     hwnd = CreateWindowExA( 0, "static", "", WS_POPUP | style,
314                 0, 0, 10, 10, 0, 0, 0, NULL);
315     assert( hwnd != 0);
316     /* Set Horizontal Scroll range to something that could be the default value it
317      * already has */;
318     ret = SetScrollRange( hwnd, SB_HORZ, 0, 100, FALSE);
319     ok( ret, "SetScrollRange failed.\n");
320     /* and request the Vertical range */
321     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
322     ok( ret, "GetScrollRange failed.\n");
323     /* now the range should be 0,100 in ALL cases */
324     ok( min == 0 && max == 100,
325             "Scroll bar range is %d,%d. Expected 0,100. Style %08x\n", min, max, style);
326     /* See what is different now for GetScrollRange */
327     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
328     /* should succeed in ALL cases */
329     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
330     ret = GetScrollInfo( hwnd, SB_VERT, &si);
331     /* should succeed in ALL cases */
332     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
333     /* report the windows style */
334     winstyle = GetWindowLongA( hwnd, GWL_STYLE );
335     /* WS_HSCROLL added to the window style */
336     if( !(style & WS_HSCROLL))
337     {
338         if (bThemeActive || style != WS_VSCROLL)
339 todo_wine
340             ok( (winstyle & (WS_HSCROLL|WS_VSCROLL)) == ( style | WS_HSCROLL),
341                 "unexpected style change %08x/%08x\n", winstyle, style);
342         else
343             ok( (winstyle & (WS_HSCROLL|WS_VSCROLL)) == style,
344                 "unexpected style change %08x/%08x\n", winstyle, style);
345     }
346     /* Slightly change the test to use SetScrollInfo
347      * Start with a clean window */
348     DestroyWindow( hwnd);
349     hwnd = CreateWindowExA( 0, "static", "", WS_POPUP | style,
350                 0, 0, 10, 10, 0, 0, 0, NULL);
351     assert( hwnd != 0);
352     /* set Horizontal position with SetScrollInfo */
353     si.nPos = 0;
354     si.nMin = 11;
355     si.nMax = 22;
356     si.fMask |= SIF_RANGE;
357     ret = SetScrollInfo( hwnd, SB_HORZ, &si, FALSE);
358     ok( ret, "SetScrollInfo failed. Style is %08x\n", style);
359     /* and request the Vertical range */
360     ret = GetScrollRange( hwnd, SB_VERT, &min, &max);
361     ok( ret, "GetScrollRange failed.\n");
362     /* now the range should be 0,100 in ALL cases */
363     ok( min == 0 && max == 100,
364             "Scroll bar range is %d,%d. Expected 0,100. Style %08x\n", min, max, style);
365     /* See what is different now for GetScrollRange */
366     ret = GetScrollInfo( hwnd, SB_HORZ, &si);
367     /* should succeed in ALL cases */
368     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
369     ret = GetScrollInfo( hwnd, SB_VERT, &si);
370     /* should succeed in ALL cases */
371     ok( ret, "GetScrollInfo failed unexpectedly. Style is %08x\n", style);
372     /* also test if the window scroll bars are enabled */
373     ret = EnableScrollBar( hwnd, SB_VERT, ESB_ENABLE_BOTH);
374     ok( !ret, "Vertical window scroll bar was not enabled\n");
375     ret = EnableScrollBar( hwnd, SB_HORZ, ESB_ENABLE_BOTH);
376     ok( !ret, "Horizontal window scroll bar was not enabled\n");
377     DestroyWindow( hwnd);
378     /* finally, check if adding a WS_[HV]SCROLL style of a window makes the scroll info
379      * available */
380     if( style & (WS_HSCROLL | WS_VSCROLL)) return;/* only test if not yet set */
381     /* Start with a clean window */
382     DestroyWindow( hwnd);
383     hwnd = CreateWindowExA( 0, "static", "", WS_POPUP ,
384                 0, 0, 10, 10, 0, 0, 0, NULL);
385     assert( hwnd != 0);
386     ret = GetScrollInfo( hwnd, SB_VERT, &si);
387     /* should fail */
388     ok( !ret, "GetScrollInfo succeeded unexpectedly. Style is %08x\n", style);
389     /* add scroll styles */
390     winstyle = GetWindowLongA( hwnd, GWL_STYLE );
391     SetWindowLongW( hwnd, GWL_STYLE, winstyle | WS_VSCROLL | WS_HSCROLL);
392     ret = GetScrollInfo( hwnd, SB_VERT, &si);
393     /* should still fail */
394     ok( !ret, "GetScrollInfo succeeded unexpectedly. Style is %08x\n", style);
395     /* clean up */
396     DestroyWindow( hwnd);
397 }
398
399 START_TEST ( scroll )
400 {
401     WNDCLASSA wc;
402     HMODULE hUxtheme;
403     BOOL (WINAPI * pIsThemeActive)(VOID);
404
405     wc.style = CS_HREDRAW | CS_VREDRAW;
406     wc.cbClsExtra = 0;
407     wc.cbWndExtra = 0;
408     wc.hInstance = GetModuleHandleA(NULL);
409     wc.hIcon = NULL;
410     wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
411     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
412     wc.lpszMenuName = NULL;
413     wc.lpszClassName = "MyTestWnd";
414     wc.lpfnWndProc = MyWndProc;
415     RegisterClassA(&wc);
416
417     hMainWnd = CreateWindowExA( 0, "MyTestWnd", "Scroll",
418       WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,
419       CW_USEDEFAULT, CW_USEDEFAULT, 100, 100, NULL, NULL, GetModuleHandleA(NULL), 0 );
420
421     ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n");
422     if (!hMainWnd) return;
423
424     assert( hScroll );
425
426     scrollbar_test1();
427     scrollbar_test2();
428     scrollbar_test3();
429     scrollbar_test4();
430     scrollbar_test_track();
431
432     /* Some test results vary depending of theming being active or not */
433     hUxtheme = LoadLibraryA("uxtheme.dll");
434     if (hUxtheme)
435     {
436         pIsThemeActive = (void*)GetProcAddress(hUxtheme, "IsThemeActive");
437         if (pIsThemeActive)
438             bThemeActive = pIsThemeActive();
439         FreeLibrary(hUxtheme);
440     }
441
442     scrollbar_test_default( 0);
443     scrollbar_test_default( WS_HSCROLL);
444     scrollbar_test_default( WS_VSCROLL);
445     scrollbar_test_default( WS_HSCROLL | WS_VSCROLL);
446
447     DestroyWindow(hScroll);
448     DestroyWindow(hMainWnd);
449 }