user32/tests: Fix a couple of dimension checks on W2k3 and Vista.
[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
30 static LRESULT CALLBACK MyWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
31 {
32     switch(msg)
33     {
34
35     case WM_CREATE:
36     {
37         hScroll = CreateWindowA( "SCROLLBAR", "", WS_CHILD | WS_VISIBLE, 0, 0, 120, 100, hWnd, (HMENU)100, GetModuleHandleA(0), 0 );
38
39         return 0;
40     }
41     case WM_DESTROY:
42         PostQuitMessage(0);
43         break;
44
45     default:
46         return DefWindowProcA(hWnd, msg, wParam, lParam);
47     }
48     return 0;
49 }
50
51 static void scrollbar_test1(void)
52 {
53     BOOL ret;
54
55     ret = EnableScrollBar( hScroll, SB_CTL, ESB_DISABLE_BOTH );
56     ok( ret, "The scrollbar should be disabled.\n" );
57     ok( !IsWindowEnabled( hScroll ), "The scrollbar window should be disabled.\n" );
58
59     ret = EnableScrollBar( hScroll, SB_CTL, ESB_ENABLE_BOTH );
60     ok( ret, "The scrollbar should be enabled.\n" );
61     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
62
63     /* test buttons separately */
64     ret = EnableScrollBar( hScroll, SB_CTL, ESB_DISABLE_LTUP );
65     ok( ret, "The scrollbar LTUP button should be disabled.\n" );
66     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
67     ret = EnableScrollBar( hScroll, SB_CTL, ESB_ENABLE_BOTH );
68     ok( ret, "The scrollbar should be enabled.\n" );
69     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
70
71     ret = EnableScrollBar( hScroll, SB_CTL, ESB_DISABLE_RTDN );
72     ok( ret, "The scrollbar RTDN button should be disabled.\n" );
73     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
74     ret = EnableScrollBar( hScroll, SB_CTL, ESB_ENABLE_BOTH );
75     ok( ret, "The scrollbar should be enabled.\n" );
76     ok( IsWindowEnabled( hScroll ), "The scrollbar window should be enabled.\n" );
77 }
78
79 static void scrollbar_test2(void)
80 {
81     int ret;
82
83     trace("The scrollbar is disabled.\n");
84
85     EnableWindow( hScroll, FALSE );
86     ok( !IsWindowEnabled( hScroll ), "The scroll should be disabled.\n" );
87
88     ret = SetScrollPos( hScroll, SB_CTL, 30, TRUE);
89     ok( !ret, "The position should not be set.\n" );
90
91     ret = GetScrollPos( hScroll, SB_CTL);
92     ok( !ret, "The position should be equal to zero\n");
93
94     ret = SetScrollRange( hScroll, SB_CTL, 0, 100, TRUE );
95     ok( ret, "The range should be set.\n" );
96
97     ret = SetScrollPos( hScroll, SB_CTL, 30, TRUE);
98     ok( !ret , "The position should not be set.\n" );
99
100     ret = GetScrollPos( hScroll, SB_CTL);
101     ok( ret == 30, "The position should be set!!!\n");
102
103     trace("The scrollbar is enabled.\n");
104
105     EnableWindow( hScroll, TRUE );
106     ok( IsWindowEnabled( hScroll ), "The scroll should be enabled.\n" );
107
108     ret = SetScrollPos( hScroll, SB_CTL, 30, TRUE);
109     ok( ret == 30, "The position should be set.\n" );
110
111     ret = GetScrollPos( hScroll, SB_CTL);
112     ok( ret == 30, "The position should not be equal to zero\n");
113
114     ret = SetScrollRange( hScroll, SB_CTL, 0, 100, TRUE );
115     ok( ret, "The range should be set.\n" );
116
117     ret = SetScrollPos( hScroll, SB_CTL, 30, TRUE);
118     ok( ret == 30, "The position should be set.\n" );
119
120     ret = GetScrollPos( hScroll, SB_CTL);
121     ok( ret == 30, "The position should not be equal to zero\n");
122 }
123
124 static void scrollbar_test3(void)
125 {
126     BOOL    ret;
127
128     ret = ShowScrollBar( hScroll, SB_CTL, FALSE );
129     ok( ret, "The ShowScrollBar() should not failed.\n" );
130     ok( !IsWindowVisible( hScroll ), "The scrollbar window should not be visible\n" );
131
132     ret = ShowScrollBar( hScroll, SB_CTL, TRUE );
133     ok( ret, "The ShowScrollBar() should not failed.\n" );
134     ok( !IsWindowVisible( hScroll ), "The scrollbar window should be visible\n" );
135
136     ret = ShowScrollBar( NULL, SB_CTL, TRUE );
137     ok( !ret, "The ShowScrollBar() should failed.\n" );
138
139 }
140
141 static void scrollbar_test4(void)
142 {
143     BOOL ret;
144     SCROLLBARINFO sbi;
145     RECT rect;
146     BOOL (WINAPI *pGetScrollBarInfo)(HWND, LONG, LPSCROLLBARINFO);
147
148     pGetScrollBarInfo = (void*)GetProcAddress(GetModuleHandleA("user32.dll"), "GetScrollBarInfo");
149     if (!pGetScrollBarInfo)
150     {
151         win_skip("GetScrollBarInfo is not available\n");
152         return;
153     }
154
155     /* Test GetScrollBarInfo to make sure it returns rcScrollBar in screen
156      * coordinates. */
157     sbi.cbSize = sizeof(sbi);
158     ret = pGetScrollBarInfo( hScroll, OBJID_CLIENT, &sbi);
159     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
160     GetWindowRect( hScroll, &rect );
161     ok( ret, "The GetWindowRect() call should not fail.\n" );
162     ok( !(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)),
163         "unexpected rgstate(0x%x)\n", sbi.rgstate[0]);
164     ok( EqualRect(&rect, &sbi.rcScrollBar),
165         "WindowRect(%d, %d, %d, %d) != rcScrollBar(%d, %d, %d, %d)\n",
166         rect.top, rect.left, rect.bottom, rect.right,
167         sbi.rcScrollBar.top, sbi.rcScrollBar.left,
168         sbi.rcScrollBar.bottom, sbi.rcScrollBar.right );
169
170     /* Test windows horizontal and vertical scrollbar to make sure rcScrollBar
171      * is still returned in screen coordinates by moving the window, and
172      * making sure that it shifts the rcScrollBar value. */
173     ShowWindow( hMainWnd, SW_SHOW );
174     sbi.cbSize = sizeof(sbi);
175     ret = pGetScrollBarInfo( hMainWnd, OBJID_HSCROLL, &sbi);
176     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
177     GetWindowRect( hMainWnd, &rect );
178     ok( ret, "The GetWindowRect() call should not fail.\n" );
179     MoveWindow( hMainWnd, rect.left+5, rect.top+5,
180                 rect.right-rect.left, rect.bottom-rect.top, TRUE );
181     rect = sbi.rcScrollBar;
182     OffsetRect(&rect, 5, 5);
183     ret = pGetScrollBarInfo( hMainWnd, OBJID_HSCROLL, &sbi);
184     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
185     ok( EqualRect(&rect, &sbi.rcScrollBar),
186         "PreviousRect(%d, %d, %d, %d) != CurrentRect(%d, %d, %d, %d)\n",
187         rect.top, rect.left, rect.bottom, rect.right,
188         sbi.rcScrollBar.top, sbi.rcScrollBar.left,
189         sbi.rcScrollBar.bottom, sbi.rcScrollBar.right );
190
191     sbi.cbSize = sizeof(sbi);
192     ret = pGetScrollBarInfo( hMainWnd, OBJID_VSCROLL, &sbi);
193     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
194     GetWindowRect( hMainWnd, &rect );
195     ok( ret, "The GetWindowRect() call should not fail.\n" );
196     MoveWindow( hMainWnd, rect.left+5, rect.top+5,
197                 rect.right-rect.left, rect.bottom-rect.top, TRUE );
198     rect = sbi.rcScrollBar;
199     OffsetRect(&rect, 5, 5);
200     ret = pGetScrollBarInfo( hMainWnd, OBJID_VSCROLL, &sbi);
201     ok( ret, "The GetScrollBarInfo() call should not fail.\n" );
202     ok( EqualRect(&rect, &sbi.rcScrollBar),
203         "PreviousRect(%d, %d, %d, %d) != CurrentRect(%d, %d, %d, %d)\n",
204         rect.top, rect.left, rect.bottom, rect.right,
205         sbi.rcScrollBar.top, sbi.rcScrollBar.left,
206         sbi.rcScrollBar.bottom, sbi.rcScrollBar.right );
207 }
208
209 START_TEST ( scroll )
210 {
211     WNDCLASSA wc;
212
213     wc.style = CS_HREDRAW | CS_VREDRAW;
214     wc.cbClsExtra = 0;
215     wc.cbWndExtra = 0;
216     wc.hInstance = GetModuleHandleA(NULL);
217     wc.hIcon = NULL;
218     wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
219     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
220     wc.lpszMenuName = NULL;
221     wc.lpszClassName = "MyTestWnd";
222     wc.lpfnWndProc = MyWndProc;
223     RegisterClassA(&wc);
224
225     hMainWnd = CreateWindowExA( 0, "MyTestWnd", "Scroll",
226       WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL,
227       CW_USEDEFAULT, CW_USEDEFAULT, 100, 100, NULL, NULL, GetModuleHandleA(NULL), 0 );
228
229     if ( !ok( hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n" ) )
230         return;
231
232     assert( hScroll );
233
234     scrollbar_test1();
235     scrollbar_test2();
236     scrollbar_test3();
237     scrollbar_test4();
238
239     DestroyWindow(hScroll);
240     DestroyWindow(hMainWnd);
241 }