[COMCTL32_WINETEST] Sync with Wine Staging 1.9.11. CORE-11368
[reactos.git] / rostests / winetests / comctl32 / progress.c
1 /* Unit tests for the progress bar control.
2 *
3 * Copyright 2005 Michael Kaufmann
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include <stdarg.h>
21
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winuser.h"
26 #include "commctrl.h"
27
28 #include "wine/test.h"
29
30
31 static HWND hProgressParentWnd, hProgressWnd;
32 static const char progressTestClass[] = "ProgressBarTestClass";
33
34 static HWND create_progress(DWORD style)
35 {
36 return CreateWindowExA(0, PROGRESS_CLASSA, "", WS_VISIBLE | style,
37 0, 0, 100, 20, NULL, NULL, GetModuleHandleA(NULL), 0);
38 }
39
40 /* try to make sure pending X events have been processed before continuing */
41 static void flush_events(void)
42 {
43 MSG msg;
44 int diff = 100;
45 DWORD time = GetTickCount() + diff;
46
47 while (diff > 0)
48 {
49 if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min(10,diff), QS_ALLINPUT ) == WAIT_TIMEOUT) break;
50 while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessageA( &msg );
51 diff = time - GetTickCount();
52 }
53 }
54
55 static LRESULT CALLBACK progress_test_wnd_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
56 {
57 switch(msg) {
58
59 case WM_DESTROY:
60 PostQuitMessage(0);
61 break;
62
63 default:
64 return DefWindowProcA(hWnd, msg, wParam, lParam);
65 }
66
67 return 0L;
68 }
69
70 static WNDPROC progress_wndproc;
71 static BOOL erased;
72 static RECT last_paint_rect;
73
74 static LRESULT CALLBACK progress_subclass_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
75 {
76 if (msg == WM_PAINT)
77 {
78 GetUpdateRect(hWnd, &last_paint_rect, FALSE);
79 }
80 else if (msg == WM_ERASEBKGND)
81 {
82 erased = TRUE;
83 }
84 return CallWindowProcA(progress_wndproc, hWnd, msg, wParam, lParam);
85 }
86
87
88 static void update_window(HWND hWnd)
89 {
90 UpdateWindow(hWnd);
91 ok(!GetUpdateRect(hWnd, NULL, FALSE), "GetUpdateRect must return zero after UpdateWindow\n");
92 }
93
94
95 static void init(void)
96 {
97 HMODULE hComctl32;
98 BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
99 WNDCLASSA wc;
100 RECT rect;
101 BOOL ret;
102
103 hComctl32 = GetModuleHandleA("comctl32.dll");
104 pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
105 if (pInitCommonControlsEx)
106 {
107 INITCOMMONCONTROLSEX iccex;
108 iccex.dwSize = sizeof(iccex);
109 iccex.dwICC = ICC_PROGRESS_CLASS;
110 pInitCommonControlsEx(&iccex);
111 }
112 else
113 InitCommonControls();
114
115 wc.style = CS_HREDRAW | CS_VREDRAW;
116 wc.cbClsExtra = 0;
117 wc.cbWndExtra = 0;
118 wc.hInstance = GetModuleHandleA(NULL);
119 wc.hIcon = NULL;
120 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
121 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
122 wc.lpszMenuName = NULL;
123 wc.lpszClassName = progressTestClass;
124 wc.lpfnWndProc = progress_test_wnd_proc;
125 RegisterClassA(&wc);
126
127 SetRect(&rect, 0, 0, 400, 20);
128 ret = AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
129 ok(ret, "got %d\n", ret);
130
131 hProgressParentWnd = CreateWindowExA(0, progressTestClass, "Progress Bar Test", WS_OVERLAPPEDWINDOW,
132 CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, GetModuleHandleA(NULL), 0);
133 ok(hProgressParentWnd != NULL, "failed to create parent wnd\n");
134
135 GetClientRect(hProgressParentWnd, &rect);
136 hProgressWnd = CreateWindowExA(0, PROGRESS_CLASSA, "", WS_CHILD | WS_VISIBLE,
137 0, 0, rect.right, rect.bottom, hProgressParentWnd, NULL, GetModuleHandleA(NULL), 0);
138 ok(hProgressWnd != NULL, "failed to create parent wnd\n");
139 progress_wndproc = (WNDPROC)SetWindowLongPtrA(hProgressWnd, GWLP_WNDPROC, (LPARAM)progress_subclass_proc);
140
141 ShowWindow(hProgressParentWnd, SW_SHOWNORMAL);
142 ok(GetUpdateRect(hProgressParentWnd, NULL, FALSE), "GetUpdateRect: There should be a region that needs to be updated\n");
143 flush_events();
144 update_window(hProgressParentWnd);
145 }
146
147 static void cleanup(void)
148 {
149 MSG msg;
150
151 PostMessageA(hProgressParentWnd, WM_CLOSE, 0, 0);
152 while (GetMessageA(&msg,0,0,0)) {
153 TranslateMessage(&msg);
154 DispatchMessageA(&msg);
155 }
156
157 UnregisterClassA(progressTestClass, GetModuleHandleA(NULL));
158 }
159
160
161 /*
162 * Tests if a progress bar repaints itself immediately when it receives
163 * some specific messages.
164 */
165 static void test_redraw(void)
166 {
167 RECT client_rect;
168 LRESULT ret;
169
170 SendMessageA(hProgressWnd, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
171 SendMessageA(hProgressWnd, PBM_SETPOS, 10, 0);
172 SendMessageA(hProgressWnd, PBM_SETSTEP, 20, 0);
173 update_window(hProgressWnd);
174
175 /* PBM_SETPOS */
176 ok(SendMessageA(hProgressWnd, PBM_SETPOS, 50, 0) == 10, "PBM_SETPOS must return the previous position\n");
177 ok(!GetUpdateRect(hProgressWnd, NULL, FALSE), "PBM_SETPOS: The progress bar should be redrawn immediately\n");
178
179 /* PBM_DELTAPOS */
180 ok(SendMessageA(hProgressWnd, PBM_DELTAPOS, 15, 0) == 50, "PBM_DELTAPOS must return the previous position\n");
181 ok(!GetUpdateRect(hProgressWnd, NULL, FALSE), "PBM_DELTAPOS: The progress bar should be redrawn immediately\n");
182
183 /* PBM_SETPOS */
184 ok(SendMessageA(hProgressWnd, PBM_SETPOS, 80, 0) == 65, "PBM_SETPOS must return the previous position\n");
185 ok(!GetUpdateRect(hProgressWnd, NULL, FALSE), "PBM_SETPOS: The progress bar should be redrawn immediately\n");
186
187 /* PBM_STEPIT */
188 ok(SendMessageA(hProgressWnd, PBM_STEPIT, 0, 0) == 80, "PBM_STEPIT must return the previous position\n");
189 ok(!GetUpdateRect(hProgressWnd, NULL, FALSE), "PBM_STEPIT: The progress bar should be redrawn immediately\n");
190 ret = SendMessageA(hProgressWnd, PBM_GETPOS, 0, 0);
191 if (ret == 0)
192 win_skip("PBM_GETPOS needs comctl32 > 4.70\n");
193 else
194 ok(ret == 100, "PBM_GETPOS returned a wrong position : %d\n", (UINT)ret);
195
196 /* PBM_SETRANGE and PBM_SETRANGE32:
197 Usually the progress bar doesn't repaint itself immediately. If the
198 position is not in the new range, it does.
199 Don't test this, it may change in future Windows versions. */
200
201 SendMessageA(hProgressWnd, PBM_SETPOS, 0, 0);
202 update_window(hProgressWnd);
203
204 /* increase to 10 - no background erase required */
205 erased = FALSE;
206 SetRectEmpty(&last_paint_rect);
207 SendMessageA(hProgressWnd, PBM_SETPOS, 10, 0);
208 GetClientRect(hProgressWnd, &client_rect);
209 ok(EqualRect(&last_paint_rect, &client_rect),
210 "last_paint_rect was { %d, %d, %d, %d } instead of { %d, %d, %d, %d }\n",
211 last_paint_rect.left, last_paint_rect.top, last_paint_rect.right, last_paint_rect.bottom,
212 client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
213 update_window(hProgressWnd);
214 ok(!erased, "Progress bar shouldn't have erased the background\n");
215
216 /* decrease to 0 - background erase will be required */
217 erased = FALSE;
218 SetRectEmpty(&last_paint_rect);
219 SendMessageA(hProgressWnd, PBM_SETPOS, 0, 0);
220 GetClientRect(hProgressWnd, &client_rect);
221 ok(EqualRect(&last_paint_rect, &client_rect),
222 "last_paint_rect was { %d, %d, %d, %d } instead of { %d, %d, %d, %d }\n",
223 last_paint_rect.left, last_paint_rect.top, last_paint_rect.right, last_paint_rect.bottom,
224 client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
225 update_window(hProgressWnd);
226 ok(erased, "Progress bar should have erased the background\n");
227 }
228
229 static void test_setcolors(void)
230 {
231 HWND progress;
232 COLORREF clr;
233
234 progress = create_progress(PBS_SMOOTH);
235
236 clr = SendMessageA(progress, PBM_SETBARCOLOR, 0, 0);
237 ok(clr == CLR_DEFAULT, "got %x\n", clr);
238
239 clr = SendMessageA(progress, PBM_SETBARCOLOR, 0, RGB(0, 255, 0));
240 ok(clr == 0, "got %x\n", clr);
241
242 clr = SendMessageA(progress, PBM_SETBARCOLOR, 0, CLR_DEFAULT);
243 ok(clr == RGB(0, 255, 0), "got %x\n", clr);
244
245 clr = SendMessageA(progress, PBM_SETBKCOLOR, 0, 0);
246 ok(clr == CLR_DEFAULT, "got %x\n", clr);
247
248 clr = SendMessageA(progress, PBM_SETBKCOLOR, 0, RGB(255, 0, 0));
249 ok(clr == 0, "got %x\n", clr);
250
251 clr = SendMessageA(progress, PBM_SETBKCOLOR, 0, CLR_DEFAULT);
252 ok(clr == RGB(255, 0, 0), "got %x\n", clr);
253
254 DestroyWindow(progress);
255 }
256
257 START_TEST(progress)
258 {
259 init();
260
261 test_redraw();
262 test_setcolors();
263
264 cleanup();
265 }