ReactOS 0.4.15-dev-8131-g4988de4
subclass.c
Go to the documentation of this file.
1/* Unit tests for subclassed windows.
2 *
3 * Copyright 2004 Kevin Koltzau
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/heap.h"
30#include "wine/test.h"
31
32static BOOL (WINAPI *pSetWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR);
33static BOOL (WINAPI *pRemoveWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR);
34static LRESULT (WINAPI *pDefSubclassProc)(HWND, UINT, WPARAM, LPARAM);
35
36#define SEND_NEST 0x01
37#define DELETE_SELF 0x02
38#define DELETE_PREV 0x04
39
40struct message {
41 int procnum; /* WndProc id message is expected from */
42 WPARAM wParam; /* expected value of wParam */
43};
44
46static struct message* sequence;
47
48static const struct message Sub_BasicTest[] = {
49 { 2, 1 },
50 { 1, 1 },
51 { 2, 2 },
52 { 1, 2 },
53 { 0 }
54};
55
56static const struct message Sub_DeletedTest[] = {
57 { 2, 1 },
58 { 1, 1 },
59 { 0 }
60};
61
62static const struct message Sub_AfterDeletedTest[] = {
63 { 1, 1 },
64 { 0 }
65};
66
67static 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 }
75};
76
77static const struct message Sub_MixTest[] = {
78 { 3, 1 },
79 { 4, 1 },
80 { 2, 1 },
81 { 1, 1 },
82 { 0 }
83};
84
85static 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 }
95};
96
97static 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 }
106};
107
108static const struct message Sub_MixDelPrevTest[] = {
109 { 3, 1 },
110 { 5, 1 },
111 { 2, 1 },
112 { 1, 1 },
113 { 0 }
114};
115
116static void add_message(const struct message *msg)
117{
118 if (!sequence)
119 {
120 sequence_size = 10;
121 sequence = heap_alloc( sequence_size * sizeof (struct message) );
122 }
124 {
125 sequence_size *= 2;
126 sequence = heap_realloc( sequence, sequence_size * sizeof (struct message) );
127 }
129
130 sequence[sequence_cnt].wParam = msg->wParam;
131 sequence[sequence_cnt].procnum = msg->procnum;
132
133 sequence_cnt++;
134}
135
136static void flush_sequence(void)
137{
139 sequence = NULL;
141}
142
143static void ok_sequence(const struct message *expected, const char *context)
144{
145 static const struct message end_of_sequence = { 0, 0 };
146 const struct message *actual;
147
148 add_message(&end_of_sequence);
149
150 actual = sequence;
151
152 while(expected->procnum && actual->procnum)
153 {
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++;
162 }
163 ok(!expected->procnum, "Received fewer messages than expected\n");
164 ok(!actual->procnum, "Received more messages than expected\n");
166}
167
169{
170 struct message msg;
171
172 if(message == WM_USER) {
173 msg.wParam = wParam;
174 msg.procnum = 1;
176 }
178}
179
180
183{
184 struct message msg;
185
186 if(message == WM_USER) {
187 msg.wParam = wParam;
188 msg.procnum = 3;
190 }
192}
193
195{
196 struct message msg;
197
198 if(message == WM_USER) {
199 msg.wParam = wParam;
200 msg.procnum = uldSubclass;
202
203 if(lParam) {
204 if(dwRefData & DELETE_SELF) {
205 pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass);
206 pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass);
207 }
209 pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass-1);
210 if(dwRefData & SEND_NEST)
212 }
213 }
214 return pDefSubclassProc(hwnd, message, wParam, lParam);
215}
216
217static void test_subclass(void)
218{
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");
223
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");
229
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");
234
235 SendMessageA(hwnd, WM_USER, 1, 0);
236 ok_sequence(Sub_AfterDeletedTest, "After Deleted");
237
238 ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 2, 0);
239 ok(ret == TRUE, "Expected TRUE\n");
241 SendMessageA(hwnd, WM_USER, 1, 0);
242 SendMessageA(hwnd, WM_USER, 2, 0);
243 ok_sequence(Sub_OldAfterNewTest, "Old after New");
244
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");
249
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");
255
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");
260
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");
267
268 ret = pSetWindowSubclass(NULL, wnd_proc_sub, 1, 0);
269 ok(ret == FALSE, "Expected FALSE\n");
270
271 ret = pSetWindowSubclass(hwnd, NULL, 1, 0);
272 ok(ret == FALSE, "Expected FALSE\n");
273
274 pRemoveWindowSubclass(hwnd, wnd_proc_sub, 2);
275 pRemoveWindowSubclass(hwnd, wnd_proc_sub, 5);
276
278}
279
281{
282 WNDCLASSA cls;
283 ATOM atom;
284
285 cls.style = 0;
287 cls.cbClsExtra = 0;
288 cls.cbWndExtra = 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");
297
298 return atom != 0;
299}
300
302{
304 void *ptr;
305
306 hmod = LoadLibraryA("comctl32.dll");
307 ok(hmod != NULL, "got %p\n", hmod);
308
309 /* Functions have to be loaded by ordinal. Only XP and W2K3 export
310 * them by name.
311 */
312#define MAKEFUNC_ORD(f, ord) (p##f = (void*)GetProcAddress(hmod, (LPSTR)(ord)))
316#undef MAKEFUNC_ORD
317
318 if(!pSetWindowSubclass || !pRemoveWindowSubclass || !pDefSubclassProc)
319 {
320 win_skip("SetWindowSubclass and friends are not available\n");
321 return FALSE;
322 }
323
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)
328 {
329#define TESTNAMED(f) \
330 ptr = (void*)GetProcAddress(hmod, #f); \
331 ok(ptr != 0, "expected named export for " #f "\n");
334 /* GetWindowSubclass exported for V6 only */
335#undef TESTNAMED
336 }
337
338 return TRUE;
339}
340
341START_TEST(subclass)
342{
343 if(!init_function_pointers()) return;
344
345 if(!register_window_classes()) return;
346
348}
#define add_message(msg)
Definition: SystemMenu.c:98
#define ok_sequence(exp, contx, todo)
Definition: SystemMenu.c:275
#define broken(x)
Definition: _sntprintf.h:21
static void * heap_alloc(size_t len)
Definition: appwiz.h:66
static BOOL heap_free(void *mem)
Definition: appwiz.h:76
static void * heap_realloc(void *mem, size_t len)
Definition: appwiz.h:71
#define ok(value,...)
Definition: atltest.h:57
#define START_TEST(x)
Definition: atltest.h:75
#define msg(x)
Definition: auth_time.c:54
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
BOOL WINAPI SetWindowSubclass(HWND hWnd, SUBCLASSPROC pfnSubclass, UINT_PTR uIDSubclass, DWORD_PTR dwRef)
Definition: commctrl.c:1261
BOOL WINAPI RemoveWindowSubclass(HWND hWnd, SUBCLASSPROC pfnSubclass, UINT_PTR uID)
Definition: commctrl.c:1390
LRESULT WINAPI DefSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: commctrl.c:1496
WORD ATOM
Definition: dimm.idl:113
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
HANDLE HWND
Definition: compat.h:19
#define GetProcAddress(x, y)
Definition: compat.h:753
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
#define assert(x)
Definition: debug.h:53
unsigned int BOOL
Definition: ntddk_ex.h:94
static PVOID ptr
Definition: dispmode.c:27
static PEXPLICIT_ACCESSW *static HMODULE hmod
Definition: security.c:143
BOOL expected
Definition: store.c:2063
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
unsigned __int3264 UINT_PTR
Definition: mstsclib_h.h:274
unsigned int UINT
Definition: ndis.h:50
#define BOOL
Definition: nt_native.h:43
#define LRESULT
Definition: ole.h:14
#define WS_OVERLAPPEDWINDOW
Definition: pedump.c:637
_In_ SUBCLASSPROC _In_ UINT_PTR _In_ DWORD_PTR dwRefData
Definition: commctrl.h:5058
#define win_skip
Definition: test.h:163
HBRUSH hbrBackground
Definition: winuser.h:3170
HICON hIcon
Definition: winuser.h:3168
HINSTANCE hInstance
Definition: winuser.h:3167
HCURSOR hCursor
Definition: winuser.h:3169
int cbWndExtra
Definition: winuser.h:3166
UINT style
Definition: winuser.h:3163
LPCSTR lpszMenuName
Definition: winuser.h:3171
LPCSTR lpszClassName
Definition: winuser.h:3172
WNDPROC lpfnWndProc
Definition: winuser.h:3164
int cbClsExtra
Definition: winuser.h:3165
Definition: http.c:7252
Definition: tftpd.h:60
int procnum
Definition: subclass.c:41
#define GWLP_WNDPROC
Definition: treelist.c:66
uint32_t DWORD_PTR
Definition: typedefs.h:65
int ret
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
#define WINAPI
Definition: msvc.h:6
static void test_subclass(void)
Definition: subclass.c:217
static struct message * sequence
Definition: subclass.c:46
static BOOL init_function_pointers(void)
Definition: subclass.c:301
static const struct message Sub_MixDelPrevTest[]
Definition: subclass.c:108
static const struct message Sub_BasicTest[]
Definition: subclass.c:48
static const struct message Sub_MixTest[]
Definition: subclass.c:77
static LPARAM
Definition: subclass.c:34
static SUBCLASSPROC
Definition: subclass.c:32
static DWORD_PTR
Definition: subclass.c:32
#define TESTNAMED(f)
static const struct message Sub_MixNestDelTest[]
Definition: subclass.c:97
#define DELETE_PREV
Definition: subclass.c:38
static int sequence_size
Definition: subclass.c:45
static void flush_sequence(void)
Definition: subclass.c:136
static WPARAM
Definition: subclass.c:34
#define MAKEFUNC_ORD(f, ord)
static const struct message Sub_DeletedTest[]
Definition: subclass.c:56
#define DELETE_SELF
Definition: subclass.c:37
static LRESULT WINAPI wnd_proc_1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Definition: subclass.c:168
static int sequence_cnt
Definition: subclass.c:45
static UINT_PTR
Definition: subclass.c:32
static WNDPROC orig_proc_3
Definition: subclass.c:181
static const struct message Sub_OldAfterNewTest[]
Definition: subclass.c:67
#define SEND_NEST
Definition: subclass.c:36
static const struct message Sub_AfterDeletedTest[]
Definition: subclass.c:62
static LRESULT WINAPI wnd_proc_3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Definition: subclass.c:182
static BOOL register_window_classes(void)
Definition: subclass.c:280
static LRESULT WINAPI wnd_proc_sub(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uldSubclass, DWORD_PTR dwRefData)
Definition: subclass.c:194
static const struct message Sub_MixAndNestTest[]
Definition: subclass.c:85
static UINT
Definition: subclass.c:34
#define SetWindowLongPtrA
Definition: winuser.h:5345
HWND WINAPI CreateWindowExA(_In_ DWORD dwExStyle, _In_opt_ LPCSTR lpClassName, _In_opt_ LPCSTR lpWindowName, _In_ DWORD dwStyle, _In_ int X, _In_ int Y, _In_ int nWidth, _In_ int nHeight, _In_opt_ HWND hWndParent, _In_opt_ HMENU hMenu, _In_opt_ HINSTANCE hInstance, _In_opt_ LPVOID lpParam)
LRESULT WINAPI DefWindowProcA(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
LRESULT WINAPI SendMessageA(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
ATOM WINAPI RegisterClassA(_In_ CONST WNDCLASSA *)
#define WM_USER
Definition: winuser.h:1895
LRESULT(CALLBACK * WNDPROC)(HWND, UINT, WPARAM, LPARAM)
Definition: winuser.h:2906
BOOL WINAPI DestroyWindow(_In_ HWND)
LRESULT WINAPI CallWindowProcA(_In_ WNDPROC, _In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)