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
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
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
[] = {
56 static const struct message Sub_DeletedTest
[] = {
62 static const struct message Sub_AfterDeletedTest
[] = {
67 static const struct message Sub_OldAfterNewTest
[] = {
77 static const struct message Sub_MixTest
[] = {
85 static const struct message Sub_MixAndNestTest
[] = {
97 static const struct message Sub_MixNestDelTest
[] = {
108 static const struct message Sub_MixDelPrevTest
[] = {
116 static void add_message(const struct message
*msg
)
121 sequence
= heap_alloc( sequence_size
* sizeof (struct message
) );
123 if (sequence_cnt
== sequence_size
)
126 sequence
= heap_realloc( sequence
, sequence_size
* sizeof (struct message
) );
130 sequence
[sequence_cnt
].wParam
= msg
->wParam
;
131 sequence
[sequence_cnt
].procnum
= msg
->procnum
;
136 static void flush_sequence(void)
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
);
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
);
163 ok(!expected
->procnum
, "Received fewer messages than expected\n");
164 ok(!actual
->procnum
, "Received more messages than expected\n");
168 static LRESULT WINAPI
wnd_proc_1(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
172 if(message
== WM_USER
) {
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
)
186 if(message
== WM_USER
) {
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
)
198 if(message
== WM_USER
) {
200 msg
.procnum
= uldSubclass
;
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)
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);
280 static BOOL
register_window_classes(void)
286 cls
.lpfnWndProc
= wnd_proc_1
;
289 cls
.hInstance
= GetModuleHandleA(0);
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");
301 static BOOL
init_function_pointers(void)
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
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);
318 if(!pSetWindowSubclass
|| !pRemoveWindowSubclass
|| !pDefSubclassProc
)
320 win_skip("SetWindowSubclass and friends are not available\n");
324 /* test named exports */
325 ptr
= GetProcAddress(hmod
, "SetWindowSubclass");
326 ok(broken(ptr
== 0) || ptr
!= 0, "expected named export for SetWindowSubclass\n");
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 */
343 if(!init_function_pointers()) return;
345 if(!register_window_classes()) return;