urlmon: Don't create stgmed_obj for binding to object.
[wine] / dlls / comctl32 / tests / 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 <assert.h>
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "commctrl.h" 
28
29 #include "wine/test.h"
30
31
32 static HWND hProgressParentWnd, hProgressWnd;
33 static const char progressTestClass[] = "ProgressBarTestClass";
34
35
36 /* try to make sure pending X events have been processed before continuing */
37 static void flush_events(void)
38 {
39     MSG msg;
40     int diff = 100;
41     DWORD time = GetTickCount() + diff;
42
43     while (diff > 0)
44     {
45         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min(10,diff), QS_ALLINPUT ) == WAIT_TIMEOUT) break;
46         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
47         diff = time - GetTickCount();
48     }
49 }
50
51 static LRESULT CALLBACK ProgressTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
52 {
53     switch(msg) {
54
55     case WM_DESTROY:
56         PostQuitMessage(0);
57         break;
58   
59     default:
60         return DefWindowProcA(hWnd, msg, wParam, lParam);
61     }
62     
63     return 0L;
64 }
65
66 static WNDPROC progress_wndproc;
67 static BOOL erased;
68 static RECT last_paint_rect;
69
70 static LRESULT CALLBACK ProgressSubclassProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
71 {
72     if (msg == WM_PAINT)
73     {
74         GetUpdateRect(hWnd, &last_paint_rect, FALSE);
75     }
76     else if (msg == WM_ERASEBKGND)
77     {
78         erased = TRUE;
79     }
80     return CallWindowProc(progress_wndproc, hWnd, msg, wParam, lParam);
81 }
82
83
84 static void update_window(HWND hWnd)
85 {
86     UpdateWindow(hWnd);
87     ok(!GetUpdateRect(hWnd, NULL, FALSE), "GetUpdateRect must return zero after UpdateWindow\n");    
88 }
89
90
91 static void init(void)
92 {
93     WNDCLASSA wc;
94     RECT rect;
95     
96     InitCommonControls();
97   
98     wc.style = CS_HREDRAW | CS_VREDRAW;
99     wc.cbClsExtra = 0;
100     wc.cbWndExtra = 0;
101     wc.hInstance = GetModuleHandleA(NULL);
102     wc.hIcon = NULL;
103     wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
104     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
105     wc.lpszMenuName = NULL;
106     wc.lpszClassName = progressTestClass;
107     wc.lpfnWndProc = ProgressTestWndProc;
108     RegisterClassA(&wc);
109     
110     rect.left = 0;
111     rect.top = 0;
112     rect.right = 400;
113     rect.bottom = 20;
114     assert(AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE));
115     
116     hProgressParentWnd = CreateWindowExA(0, progressTestClass, "Progress Bar Test", WS_OVERLAPPEDWINDOW,
117       CW_USEDEFAULT, CW_USEDEFAULT, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, GetModuleHandleA(NULL), 0);
118     assert(hProgressParentWnd != NULL);
119
120     GetClientRect(hProgressParentWnd, &rect);
121     hProgressWnd = CreateWindowEx(0, PROGRESS_CLASS, "", WS_CHILD | WS_VISIBLE,
122       0, 0, rect.right, rect.bottom, hProgressParentWnd, NULL, GetModuleHandleA(NULL), 0);
123     assert(hProgressWnd != NULL);
124     progress_wndproc = (WNDPROC)SetWindowLongPtr(hProgressWnd, GWLP_WNDPROC, (LPARAM)ProgressSubclassProc);
125     
126     ShowWindow(hProgressParentWnd, SW_SHOWNORMAL);
127     ok(GetUpdateRect(hProgressParentWnd, NULL, FALSE), "GetUpdateRect: There should be a region that needs to be updated\n");
128     flush_events();
129     update_window(hProgressParentWnd);    
130 }
131
132
133 static void cleanup(void)
134 {
135     MSG msg;
136     
137     PostMessageA(hProgressParentWnd, WM_CLOSE, 0, 0);
138     while (GetMessageA(&msg,0,0,0)) {
139         TranslateMessage(&msg);
140         DispatchMessageA(&msg);
141     }
142     
143     UnregisterClassA(progressTestClass, GetModuleHandleA(NULL));
144 }
145
146
147 /*
148  * Tests if a progress bar repaints itself immediately when it receives
149  * some specific messages.
150  */
151 static void test_redraw(void)
152 {
153     RECT client_rect;
154
155     SendMessageA(hProgressWnd, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
156     SendMessageA(hProgressWnd, PBM_SETPOS, 10, 0);
157     SendMessageA(hProgressWnd, PBM_SETSTEP, 20, 0);
158     update_window(hProgressWnd);
159
160     /* PBM_SETPOS */
161     ok(SendMessageA(hProgressWnd, PBM_SETPOS, 50, 0) == 10, "PBM_SETPOS must return the previous position\n");
162     ok(!GetUpdateRect(hProgressWnd, NULL, FALSE), "PBM_SETPOS: The progress bar should be redrawn immediately\n");
163     
164     /* PBM_DELTAPOS */
165     ok(SendMessageA(hProgressWnd, PBM_DELTAPOS, 15, 0) == 50, "PBM_DELTAPOS must return the previous position\n");
166     ok(!GetUpdateRect(hProgressWnd, NULL, FALSE), "PBM_DELTAPOS: The progress bar should be redrawn immediately\n");
167     
168     /* PBM_SETPOS */
169     ok(SendMessageA(hProgressWnd, PBM_SETPOS, 80, 0) == 65, "PBM_SETPOS must return the previous position\n");
170     ok(!GetUpdateRect(hProgressWnd, NULL, FALSE), "PBM_SETPOS: The progress bar should be redrawn immediately\n");
171     
172     /* PBM_STEPIT */
173     ok(SendMessageA(hProgressWnd, PBM_STEPIT, 0, 0) == 80, "PBM_STEPIT must return the previous position\n");
174     ok(!GetUpdateRect(hProgressWnd, NULL, FALSE), "PBM_STEPIT: The progress bar should be redrawn immediately\n");
175     ok((UINT)SendMessageA(hProgressWnd, PBM_GETPOS, 0, 0) == 100, "PBM_GETPOS returned a wrong position\n");
176     
177     /* PBM_SETRANGE and PBM_SETRANGE32:
178     Usually the progress bar doesn't repaint itself immediately. If the
179     position is not in the new range, it does.
180     Don't test this, it may change in future Windows versions. */
181
182     SendMessage(hProgressWnd, PBM_SETPOS, 0, 0);
183     update_window(hProgressWnd);
184
185     /* increase to 10 - no background erase required */
186     erased = FALSE;
187     SetRectEmpty(&last_paint_rect);
188     SendMessage(hProgressWnd, PBM_SETPOS, 10, 0);
189     GetClientRect(hProgressWnd, &client_rect);
190     ok(EqualRect(&last_paint_rect, &client_rect),
191        "last_paint_rect was { %d, %d, %d, %d } instead of { %d, %d, %d, %d }\n",
192        last_paint_rect.left, last_paint_rect.top, last_paint_rect.right, last_paint_rect.bottom,
193        client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
194     update_window(hProgressWnd);
195     ok(!erased, "Progress bar shouldn't have erased the background\n");
196
197     /* decrease to 0 - background erase will be required */
198     erased = FALSE;
199     SetRectEmpty(&last_paint_rect);
200     SendMessage(hProgressWnd, PBM_SETPOS, 0, 0);
201     GetClientRect(hProgressWnd, &client_rect);
202     ok(EqualRect(&last_paint_rect, &client_rect),
203        "last_paint_rect was { %d, %d, %d, %d } instead of { %d, %d, %d, %d }\n",
204        last_paint_rect.left, last_paint_rect.top, last_paint_rect.right, last_paint_rect.bottom,
205        client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);
206     update_window(hProgressWnd);
207     ok(erased, "Progress bar should have erased the background\n");
208 }
209
210
211 START_TEST(progress)
212 {
213     init();
214     
215     test_redraw();
216     
217     cleanup();
218 }