ReactOS  0.4.14-dev-614-gbfd8a84
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 #define _WIN32_WINNT 0x0501 /* For SetWindowSubclass/etc */
21 
22 #include <assert.h>
23 #include <stdarg.h>
24 
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "commctrl.h"
30 
31 #include "wine/heap.h"
32 #include "wine/test.h"
33 
34 static BOOL (WINAPI *pSetWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR);
35 static BOOL (WINAPI *pRemoveWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR);
36 static LRESULT (WINAPI *pDefSubclassProc)(HWND, UINT, WPARAM, LPARAM);
37 
38 #define SEND_NEST 0x01
39 #define DELETE_SELF 0x02
40 #define DELETE_PREV 0x04
41 
42 struct message {
43  int procnum; /* WndProc id message is expected from */
44  WPARAM wParam; /* expected value of wParam */
45 };
46 
48 static struct message* sequence;
49 
50 static const struct message Sub_BasicTest[] = {
51  { 2, 1 },
52  { 1, 1 },
53  { 2, 2 },
54  { 1, 2 },
55  { 0 }
56 };
57 
58 static const struct message Sub_DeletedTest[] = {
59  { 2, 1 },
60  { 1, 1 },
61  { 0 }
62 };
63 
64 static const struct message Sub_AfterDeletedTest[] = {
65  { 1, 1 },
66  { 0 }
67 };
68 
69 static const struct message Sub_OldAfterNewTest[] = {
70  { 3, 1 },
71  { 2, 1 },
72  { 1, 1 },
73  { 3, 2 },
74  { 2, 2 },
75  { 1, 2 },
76  { 0 }
77 };
78 
79 static const struct message Sub_MixTest[] = {
80  { 3, 1 },
81  { 4, 1 },
82  { 2, 1 },
83  { 1, 1 },
84  { 0 }
85 };
86 
87 static const struct message Sub_MixAndNestTest[] = {
88  { 3, 1 },
89  { 4, 1 },
90  { 3, 2 },
91  { 4, 2 },
92  { 2, 2 },
93  { 1, 2 },
94  { 2, 1 },
95  { 1, 1 },
96  { 0 }
97 };
98 
99 static const struct message Sub_MixNestDelTest[] = {
100  { 3, 1 },
101  { 4, 1 },
102  { 3, 2 },
103  { 2, 2 },
104  { 1, 2 },
105  { 2, 1 },
106  { 1, 1 },
107  { 0 }
108 };
109 
110 static const struct message Sub_MixDelPrevTest[] = {
111  { 3, 1 },
112  { 5, 1 },
113  { 2, 1 },
114  { 1, 1 },
115  { 0 }
116 };
117 
118 static void add_message(const struct message *msg)
119 {
120  if (!sequence)
121  {
122  sequence_size = 10;
123  sequence = heap_alloc( sequence_size * sizeof (struct message) );
124  }
126  {
127  sequence_size *= 2;
128  sequence = heap_realloc( sequence, sequence_size * sizeof (struct message) );
129  }
130  assert(sequence);
131 
132  sequence[sequence_cnt].wParam = msg->wParam;
133  sequence[sequence_cnt].procnum = msg->procnum;
134 
135  sequence_cnt++;
136 }
137 
138 static void flush_sequence(void)
139 {
141  sequence = NULL;
143 }
144 
145 static void ok_sequence(const struct message *expected, const char *context)
146 {
147  static const struct message end_of_sequence = { 0, 0 };
148  const struct message *actual;
149 
150  add_message(&end_of_sequence);
151 
152  actual = sequence;
153 
154  while(expected->procnum && actual->procnum)
155  {
156  ok(expected->procnum == actual->procnum,
157  "%s: the procnum %d was expected, but got procnum %d instead\n",
158  context, expected->procnum, actual->procnum);
159  ok(expected->wParam == actual->wParam,
160  "%s: in procnum %d expecting wParam 0x%lx got 0x%lx\n",
161  context, expected->procnum, expected->wParam, actual->wParam);
162  expected++;
163  actual++;
164  }
165  ok(!expected->procnum, "Received fewer messages than expected\n");
166  ok(!actual->procnum, "Received more messages than expected\n");
167  flush_sequence();
168 }
169 
171 {
172  struct message msg;
173 
174  if(message == WM_USER) {
175  msg.wParam = wParam;
176  msg.procnum = 1;
177  add_message(&msg);
178  }
180 }
181 
182 
185 {
186  struct message msg;
187 
188  if(message == WM_USER) {
189  msg.wParam = wParam;
190  msg.procnum = 3;
191  add_message(&msg);
192  }
194 }
195 
197 {
198  struct message msg;
199 
200  if(message == WM_USER) {
201  msg.wParam = wParam;
202  msg.procnum = uldSubclass;
203  add_message(&msg);
204 
205  if(lParam) {
206  if(dwRefData & DELETE_SELF) {
207  pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass);
208  pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass);
209  }
210  if(dwRefData & DELETE_PREV)
211  pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass-1);
212  if(dwRefData & SEND_NEST)
214  }
215  }
216  return pDefSubclassProc(hwnd, message, wParam, lParam);
217 }
218 
219 static void test_subclass(void)
220 {
221  BOOL ret;
222  HWND hwnd = CreateWindowExA(0, "TestSubclass", "Test subclass", WS_OVERLAPPEDWINDOW,
223  100, 100, 200, 200, 0, 0, 0, NULL);
224  ok(hwnd != NULL, "failed to create test subclass wnd\n");
225 
226  ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 2, 0);
227  ok(ret == TRUE, "Expected TRUE\n");
228  SendMessageA(hwnd, WM_USER, 1, 0);
229  SendMessageA(hwnd, WM_USER, 2, 0);
230  ok_sequence(Sub_BasicTest, "Basic");
231 
232  ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 2, DELETE_SELF);
233  ok(ret == TRUE, "Expected TRUE\n");
234  SendMessageA(hwnd, WM_USER, 1, 1);
235  ok_sequence(Sub_DeletedTest, "Deleted");
236 
237  SendMessageA(hwnd, WM_USER, 1, 0);
238  ok_sequence(Sub_AfterDeletedTest, "After Deleted");
239 
240  ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 2, 0);
241  ok(ret == TRUE, "Expected TRUE\n");
243  SendMessageA(hwnd, WM_USER, 1, 0);
244  SendMessageA(hwnd, WM_USER, 2, 0);
245  ok_sequence(Sub_OldAfterNewTest, "Old after New");
246 
247  ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 4, 0);
248  ok(ret == TRUE, "Expected TRUE\n");
249  SendMessageA(hwnd, WM_USER, 1, 0);
250  ok_sequence(Sub_MixTest, "Mix");
251 
252  /* Now the fun starts */
253  ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 4, SEND_NEST);
254  ok(ret == TRUE, "Expected TRUE\n");
255  SendMessageA(hwnd, WM_USER, 1, 1);
256  ok_sequence(Sub_MixAndNestTest, "Mix and nest");
257 
258  ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 4, SEND_NEST | DELETE_SELF);
259  ok(ret == TRUE, "Expected TRUE\n");
260  SendMessageA(hwnd, WM_USER, 1, 1);
261  ok_sequence(Sub_MixNestDelTest, "Mix, nest, del");
262 
263  ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 4, 0);
264  ok(ret == TRUE, "Expected TRUE\n");
265  ret = pSetWindowSubclass(hwnd, wnd_proc_sub, 5, DELETE_PREV);
266  ok(ret == TRUE, "Expected TRUE\n");
267  SendMessageA(hwnd, WM_USER, 1, 1);
268  ok_sequence(Sub_MixDelPrevTest, "Mix and del prev");
269 
270  ret = pSetWindowSubclass(NULL, wnd_proc_sub, 1, 0);
271  ok(ret == FALSE, "Expected FALSE\n");
272 
273  ret = pSetWindowSubclass(hwnd, NULL, 1, 0);
274  ok(ret == FALSE, "Expected FALSE\n");
275 
276  pRemoveWindowSubclass(hwnd, wnd_proc_sub, 2);
277  pRemoveWindowSubclass(hwnd, wnd_proc_sub, 5);
278 
280 }
281 
283 {
284  WNDCLASSA cls;
285  ATOM atom;
286 
287  cls.style = 0;
288  cls.lpfnWndProc = wnd_proc_1;
289  cls.cbClsExtra = 0;
290  cls.cbWndExtra = 0;
291  cls.hInstance = GetModuleHandleA(0);
292  cls.hIcon = 0;
293  cls.hCursor = NULL;
294  cls.hbrBackground = NULL;
295  cls.lpszMenuName = NULL;
296  cls.lpszClassName = "TestSubclass";
297  atom = RegisterClassA(&cls);
298  ok(atom, "failed to register test class\n");
299 
300  return atom != 0;
301 }
302 
304 {
305  HMODULE hmod;
306  void *ptr;
307 
308  hmod = LoadLibraryA("comctl32.dll");
309  ok(hmod != NULL, "got %p\n", hmod);
310 
311  /* Functions have to be loaded by ordinal. Only XP and W2K3 export
312  * them by name.
313  */
314 #define MAKEFUNC_ORD(f, ord) (p##f = (void*)GetProcAddress(hmod, (LPSTR)(ord)))
318 #undef MAKEFUNC_ORD
319 
320  if(!pSetWindowSubclass || !pRemoveWindowSubclass || !pDefSubclassProc)
321  {
322  win_skip("SetWindowSubclass and friends are not available\n");
323  return FALSE;
324  }
325 
326  /* test named exports */
327  ptr = GetProcAddress(hmod, "SetWindowSubclass");
328  ok(broken(ptr == 0) || ptr != 0, "expected named export for SetWindowSubclass\n");
329  if(ptr)
330  {
331 #define TESTNAMED(f) \
332  ptr = (void*)GetProcAddress(hmod, #f); \
333  ok(ptr != 0, "expected named export for " #f "\n");
336  /* GetWindowSubclass exported for V6 only */
337 #undef TESTNAMED
338  }
339 
340  return TRUE;
341 }
342 
343 START_TEST(subclass)
344 {
345  if(!init_function_pointers()) return;
346 
347  if(!register_window_classes()) return;
348 
349  test_subclass();
350 }
static LRESULT WINAPI wnd_proc_1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Definition: subclass.c:170
START_TEST(subclass)
Definition: subclass.c:343
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
static UINT_PTR
Definition: subclass.c:34
Definition: tftpd.h:59
#define TRUE
Definition: types.h:120
static WPARAM
Definition: subclass.c:36
static SUBCLASSPROC
Definition: subclass.c:34
static const struct message Sub_OldAfterNewTest[]
Definition: subclass.c:69
Definition: http.c:7098
#define TESTNAMED(f)
WORD ATOM
Definition: dimm.idl:113
static LRESULT WINAPI wnd_proc_3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Definition: subclass.c:184
static const struct message Sub_BasicTest[]
Definition: subclass.c:50
LRESULT WINAPI SendMessageA(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
int procnum
Definition: subclass.c:43
_In_ SUBCLASSPROC _In_ UINT_PTR _In_ DWORD_PTR dwRefData
Definition: commctrl.h:5052
HANDLE HWND
Definition: compat.h:13
static const struct message Sub_MixAndNestTest[]
Definition: subclass.c:87
int cbClsExtra
Definition: winuser.h:3140
#define assert(x)
Definition: debug.h:53
LPCSTR lpszMenuName
Definition: winuser.h:3146
UINT_PTR WPARAM
Definition: windef.h:207
HBRUSH hbrBackground
Definition: winuser.h:3145
BOOL WINAPI DestroyWindow(_In_ HWND)
static void test_subclass(void)
Definition: subclass.c:219
static void * heap_realloc(void *mem, size_t len)
Definition: appwiz.h:70
WPARAM wParam
Definition: combotst.c:138
static void * heap_alloc(size_t len)
Definition: appwiz.h:65
LRESULT WINAPI CallWindowProcA(_In_ WNDPROC, _In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
static void add_message(const struct message *msg)
Definition: subclass.c:118
int cbWndExtra
Definition: winuser.h:3141
static const struct message Sub_MixDelPrevTest[]
Definition: subclass.c:110
static BOOL init_function_pointers(void)
Definition: subclass.c:303
unsigned int BOOL
Definition: ntddk_ex.h:94
#define SEND_NEST
Definition: subclass.c:38
HINSTANCE WINAPI DECLSPEC_HOTPATCH LoadLibraryA(LPCSTR lpLibFileName)
Definition: loader.c:111
static PVOID ptr
Definition: dispmode.c:27
HCURSOR hCursor
Definition: winuser.h:3144
#define MAKEFUNC_ORD(f, ord)
static BOOL register_window_classes(void)
Definition: subclass.c:282
static LRESULT(WINAPI *pDefSubclassProc)(HWND
smooth NULL
Definition: ftsmooth.c:416
LONG_PTR LPARAM
Definition: windef.h:208
LRESULT WINAPI DefSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Definition: commctrl.c:1496
static const struct message Sub_MixTest[]
Definition: subclass.c:79
BOOL WINAPI SetWindowSubclass(HWND hWnd, SUBCLASSPROC pfnSubclass, UINT_PTR uIDSubclass, DWORD_PTR dwRef)
Definition: commctrl.c:1261
static UINT
Definition: subclass.c:36
static int sequence_size
Definition: subclass.c:47
static void ok_sequence(const struct message *expected, const char *context)
Definition: subclass.c:145
#define WINAPI
Definition: msvc.h:6
static const struct message Sub_AfterDeletedTest[]
Definition: subclass.c:64
unsigned __int3264 UINT_PTR
Definition: mstsclib_h.h:274
static LRESULT WINAPI wnd_proc_sub(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uldSubclass, DWORD_PTR dwRefData)
Definition: subclass.c:196
int ret
#define DELETE_SELF
Definition: subclass.c:39
static struct message * sequence
Definition: subclass.c:48
static BOOL(WINAPI *pSetWindowSubclass)(HWND
#define SetWindowLongPtrA
Definition: winuser.h:5320
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:821
LRESULT WINAPI DefWindowProcA(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
LPCSTR lpszClassName
Definition: winuser.h:3147
uint32_t DWORD_PTR
Definition: typedefs.h:63
#define WM_USER
Definition: winuser.h:1877
#define GWLP_WNDPROC
Definition: treelist.c:66
#define broken(x)
Definition: _sntprintf.h:21
static PEXPLICIT_ACCESSW *static HMODULE hmod
Definition: security.c:141
UINT style
Definition: winuser.h:3138
LRESULT(CALLBACK * WNDPROC)(HWND, UINT, WPARAM, LPARAM)
Definition: winuser.h:2881
BOOL WINAPI RemoveWindowSubclass(HWND hWnd, SUBCLASSPROC pfnSubclass, UINT_PTR uID)
Definition: commctrl.c:1390
HINSTANCE hInstance
Definition: winuser.h:3142
#define ok(value,...)
Definition: atltest.h:57
static LPARAM
Definition: subclass.c:36
static WNDPROC orig_proc_3
Definition: subclass.c:183
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
static const struct message Sub_DeletedTest[]
Definition: subclass.c:58
unsigned int UINT
Definition: ndis.h:50
static int sequence_cnt
Definition: subclass.c:47
WNDPROC lpfnWndProc
Definition: winuser.h:3139
#define msg(x)
Definition: auth_time.c:54
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)
#define WS_OVERLAPPEDWINDOW
Definition: pedump.c:637
static DWORD_PTR
Definition: subclass.c:34
#define GetProcAddress(x, y)
Definition: compat.h:418
#define DELETE_PREV
Definition: subclass.c:40
LONG_PTR LRESULT
Definition: windef.h:209
#define win_skip
Definition: test.h:150
static const struct message Sub_MixNestDelTest[]
Definition: subclass.c:99
LPARAM lParam
Definition: combotst.c:139
ATOM WINAPI RegisterClassA(_In_ CONST WNDCLASSA *)
BOOL expected
Definition: store.c:2063
static void flush_sequence(void)
Definition: subclass.c:138
static BOOL heap_free(void *mem)
Definition: appwiz.h:75
HICON hIcon
Definition: winuser.h:3143