mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / comctl32 / tests / subclass.c
blobb2b47319b16428da49d0c21d7f4b22d423f8bb7f
1 /* Unit tests for subclassed windows.
3 * Copyright 2004 Kevin Koltzau
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.
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.
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
20 #include <assert.h>
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "commctrl.h"
29 #include "wine/heap.h"
30 #include "wine/test.h"
32 static BOOL (WINAPI *pSetWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR);
33 static BOOL (WINAPI *pRemoveWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR);
34 static LRESULT (WINAPI *pDefSubclassProc)(HWND, UINT, WPARAM, LPARAM);
36 #define SEND_NEST 0x01
37 #define DELETE_SELF 0x02
38 #define DELETE_PREV 0x04
40 struct message {
41 int procnum; /* WndProc id message is expected from */
42 WPARAM wParam; /* expected value of wParam */
45 static int sequence_cnt, sequence_size;
46 static struct message* sequence;
48 static const struct message Sub_BasicTest[] = {
49 { 2, 1 },
50 { 1, 1 },
51 { 2, 2 },
52 { 1, 2 },
53 { 0 }
56 static const struct message Sub_DeletedTest[] = {
57 { 2, 1 },
58 { 1, 1 },
59 { 0 }
62 static const struct message Sub_AfterDeletedTest[] = {
63 { 1, 1 },
64 { 0 }
67 static const struct message Sub_OldAfterNewTest[] = {
68 { 3, 1 },
69 { 2, 1 },
70 { 1, 1 },
71 { 3, 2 },
72 { 2, 2 },
73 { 1, 2 },
74 { 0 }
77 static const struct message Sub_MixTest[] = {
78 { 3, 1 },
79 { 4, 1 },
80 { 2, 1 },
81 { 1, 1 },
82 { 0 }
85 static const struct message Sub_MixAndNestTest[] = {
86 { 3, 1 },
87 { 4, 1 },
88 { 3, 2 },
89 { 4, 2 },
90 { 2, 2 },
91 { 1, 2 },
92 { 2, 1 },
93 { 1, 1 },
94 { 0 }
97 static const struct message Sub_MixNestDelTest[] = {
98 { 3, 1 },
99 { 4, 1 },
100 { 3, 2 },
101 { 2, 2 },
102 { 1, 2 },
103 { 2, 1 },
104 { 1, 1 },
105 { 0 }
108 static const struct message Sub_MixDelPrevTest[] = {
109 { 3, 1 },
110 { 5, 1 },
111 { 2, 1 },
112 { 1, 1 },
113 { 0 }
116 static void add_message(const struct message *msg)
118 if (!sequence)
120 sequence_size = 10;
121 sequence = heap_alloc( sequence_size * sizeof (struct message) );
123 if (sequence_cnt == sequence_size)
125 sequence_size *= 2;
126 sequence = heap_realloc( sequence, sequence_size * sizeof (struct message) );
128 assert(sequence);
130 sequence[sequence_cnt].wParam = msg->wParam;
131 sequence[sequence_cnt].procnum = msg->procnum;
133 sequence_cnt++;
136 static void flush_sequence(void)
138 heap_free(sequence);
139 sequence = NULL;
140 sequence_cnt = sequence_size = 0;
143 static void ok_sequence(const struct message *expected, const char *context)
145 static const struct message end_of_sequence = { 0, 0 };
146 const struct message *actual;
148 add_message(&end_of_sequence);
150 actual = sequence;
152 while(expected->procnum && actual->procnum)
154 ok(expected->procnum == actual->procnum,
155 "%s: the procnum %d was expected, but got procnum %d instead\n",
156 context, expected->procnum, actual->procnum);
157 ok(expected->wParam == actual->wParam,
158 "%s: in procnum %d expecting wParam 0x%lx got 0x%lx\n",
159 context, expected->procnum, expected->wParam, actual->wParam);
160 expected++;
161 actual++;
163 ok(!expected->procnum, "Received fewer messages than expected\n");
164 ok(!actual->procnum, "Received more messages than expected\n");
165 flush_sequence();
168 static LRESULT WINAPI wnd_proc_1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
170 struct message msg;
172 if(message == WM_USER) {
173 msg.wParam = wParam;
174 msg.procnum = 1;
175 add_message(&msg);
177 return DefWindowProcA(hwnd, message, wParam, lParam);
181 static WNDPROC orig_proc_3;
182 static LRESULT WINAPI wnd_proc_3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
184 struct message msg;
186 if(message == WM_USER) {
187 msg.wParam = wParam;
188 msg.procnum = 3;
189 add_message(&msg);
191 return CallWindowProcA(orig_proc_3, hwnd, message, wParam, lParam);
194 static LRESULT WINAPI wnd_proc_sub(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uldSubclass, DWORD_PTR dwRefData)
196 struct message msg;
198 if(message == WM_USER) {
199 msg.wParam = wParam;
200 msg.procnum = uldSubclass;
201 add_message(&msg);
203 if(lParam) {
204 if(dwRefData & DELETE_SELF) {
205 pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass);
206 pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass);
208 if(dwRefData & DELETE_PREV)
209 pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass-1);
210 if(dwRefData & SEND_NEST)
211 SendMessageA(hwnd, WM_USER, wParam+1, 0);
214 return pDefSubclassProc(hwnd, message, wParam, lParam);
217 static void test_subclass(void)
219 BOOL ret;
220 HWND hwnd = CreateWindowExA(0, "TestSubclass", "Test subclass", WS_OVERLAPPEDWINDOW,
221 100, 100, 200, 200, 0, 0, 0, NULL);
222 ok(hwnd != NULL, "failed to create test subclass wnd\n");
224 ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 2, 0);
225 ok(ret == TRUE, "Expected TRUE\n");
226 SendMessageA(hwnd, WM_USER, 1, 0);
227 SendMessageA(hwnd, WM_USER, 2, 0);
228 ok_sequence(Sub_BasicTest, "Basic");
230 ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 2, DELETE_SELF);
231 ok(ret == TRUE, "Expected TRUE\n");
232 SendMessageA(hwnd, WM_USER, 1, 1);
233 ok_sequence(Sub_DeletedTest, "Deleted");
235 SendMessageA(hwnd, WM_USER, 1, 0);
236 ok_sequence(Sub_AfterDeletedTest, "After Deleted");
238 ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 2, 0);
239 ok(ret == TRUE, "Expected TRUE\n");
240 orig_proc_3 = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)wnd_proc_3);
241 SendMessageA(hwnd, WM_USER, 1, 0);
242 SendMessageA(hwnd, WM_USER, 2, 0);
243 ok_sequence(Sub_OldAfterNewTest, "Old after New");
245 ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 4, 0);
246 ok(ret == TRUE, "Expected TRUE\n");
247 SendMessageA(hwnd, WM_USER, 1, 0);
248 ok_sequence(Sub_MixTest, "Mix");
250 /* Now the fun starts */
251 ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 4, SEND_NEST);
252 ok(ret == TRUE, "Expected TRUE\n");
253 SendMessageA(hwnd, WM_USER, 1, 1);
254 ok_sequence(Sub_MixAndNestTest, "Mix and nest");
256 ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 4, SEND_NEST | DELETE_SELF);
257 ok(ret == TRUE, "Expected TRUE\n");
258 SendMessageA(hwnd, WM_USER, 1, 1);
259 ok_sequence(Sub_MixNestDelTest, "Mix, nest, del");
261 ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 4, 0);
262 ok(ret == TRUE, "Expected TRUE\n");
263 ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 5, DELETE_PREV);
264 ok(ret == TRUE, "Expected TRUE\n");
265 SendMessageA(hwnd, WM_USER, 1, 1);
266 ok_sequence(Sub_MixDelPrevTest, "Mix and del prev");
268 ret = pSetWindowSubclass(NULL, wnd_proc_sub, 1, 0);
269 ok(ret == FALSE, "Expected FALSE\n");
271 ret = pSetWindowSubclass(hwnd, NULL, 1, 0);
272 ok(ret == FALSE, "Expected FALSE\n");
274 pRemoveWindowSubclass(hwnd, wnd_proc_sub, 2);
275 pRemoveWindowSubclass(hwnd, wnd_proc_sub, 5);
277 DestroyWindow(hwnd);
280 static BOOL register_window_classes(void)
282 WNDCLASSA cls;
283 ATOM atom;
285 cls.style = 0;
286 cls.lpfnWndProc = wnd_proc_1;
287 cls.cbClsExtra = 0;
288 cls.cbWndExtra = 0;
289 cls.hInstance = GetModuleHandleA(0);
290 cls.hIcon = 0;
291 cls.hCursor = NULL;
292 cls.hbrBackground = NULL;
293 cls.lpszMenuName = NULL;
294 cls.lpszClassName = "TestSubclass";
295 atom = RegisterClassA(&cls);
296 ok(atom, "failed to register test class\n");
298 return atom != 0;
301 static BOOL init_function_pointers(void)
303 HMODULE hmod;
304 void *ptr;
306 hmod = LoadLibraryA("comctl32.dll");
307 ok(hmod != NULL, "got %p\n", hmod);
309 /* Functions have to be loaded by ordinal. Only XP and W2K3 export
310 * them by name.
312 #define MAKEFUNC_ORD(f, ord) (p##f = (void*)GetProcAddress(hmod, (LPSTR)(ord)))
313 MAKEFUNC_ORD(SetWindowSubclass, 410);
314 MAKEFUNC_ORD(RemoveWindowSubclass, 412);
315 MAKEFUNC_ORD(DefSubclassProc, 413);
316 #undef MAKEFUNC_ORD
318 if(!pSetWindowSubclass || !pRemoveWindowSubclass || !pDefSubclassProc)
320 win_skip("SetWindowSubclass and friends are not available\n");
321 return FALSE;
324 /* test named exports */
325 ptr = GetProcAddress(hmod, "SetWindowSubclass");
326 ok(broken(ptr == 0) || ptr != 0, "expected named export for SetWindowSubclass\n");
327 if(ptr)
329 #define TESTNAMED(f) \
330 ptr = (void*)GetProcAddress(hmod, #f); \
331 ok(ptr != 0, "expected named export for " #f "\n");
332 TESTNAMED(RemoveWindowSubclass);
333 TESTNAMED(DefSubclassProc);
334 /* GetWindowSubclass exported for V6 only */
335 #undef TESTNAMED
338 return TRUE;
341 START_TEST(subclass)
343 if(!init_function_pointers()) return;
345 if(!register_window_classes()) return;
347 test_subclass();