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/test.h"
32 
33 static BOOL (WINAPI *pSetWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR);
34 static BOOL (WINAPI *pRemoveWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR);
35 static LRESULT (WINAPI *pDefSubclassProc)(HWND, UINT, WPARAM, LPARAM);
36 
37 #define SEND_NEST   0x01
38 #define DELETE_SELF 0x02
39 #define DELETE_PREV 0x04
40 
41 struct message {
42     int procnum;           /* WndProc id message is expected from */
43     WPARAM wParam;         /* expected value of wParam */
44 };
45 
46 static int sequence_cnt, sequence_size;
47 static struct message* sequence;
48 
49 static const struct message Sub_BasicTest[] = {
50     { 2, 1 },
51     { 1, 1 },
52     { 2, 2 },
53     { 1, 2 },
54     { 0 }
55 };
56 
57 static const struct message Sub_DeletedTest[] = {
58     { 2, 1 },
59     { 1, 1 },
60     { 0 }
61 };
62 
63 static const struct message Sub_AfterDeletedTest[] = {
64     { 1, 1 },
65     { 0 }
66 };
67 
68 static const struct message Sub_OldAfterNewTest[] = {
69     { 3, 1 },
70     { 2, 1 },
71     { 1, 1 },
72     { 3, 2 },
73     { 2, 2 },
74     { 1, 2 },
75     { 0 }
76 };
77 
78 static const struct message Sub_MixTest[] = {
79     { 3, 1 },
80     { 4, 1 },
81     { 2, 1 },
82     { 1, 1 },
83     { 0 }
84 };
85 
86 static const struct message Sub_MixAndNestTest[] = {
87     { 3, 1 },
88     { 4, 1 },
89     { 3, 2 },
90     { 4, 2 },
91     { 2, 2 },
92     { 1, 2 },
93     { 2, 1 },
94     { 1, 1 },
95     { 0 }
96 };
97 
98 static const struct message Sub_MixNestDelTest[] = {
99     { 3, 1 },
100     { 4, 1 },
101     { 3, 2 },
102     { 2, 2 },
103     { 1, 2 },
104     { 2, 1 },
105     { 1, 1 },
106     { 0 }
107 };
108 
109 static const struct message Sub_MixDelPrevTest[] = {
110     { 3, 1 },
111     { 5, 1 },
112     { 2, 1 },
113     { 1, 1 },
114     { 0 }
115 };
116 
117 static void add_message(const struct message *msg)
118 {
119     if (!sequence)
120     {
121         sequence_size = 10;
122         sequence = HeapAlloc( GetProcessHeap(), 0, sequence_size * sizeof (struct message) );
123     }
124     if (sequence_cnt == sequence_size)
125     {
126         sequence_size *= 2;
127         sequence = HeapReAlloc( GetProcessHeap(), 0, sequence, sequence_size * sizeof (struct message) );
128     }
129     assert(sequence);
130 
131     sequence[sequence_cnt].wParam  = msg->wParam;
132     sequence[sequence_cnt].procnum = msg->procnum;
133 
134     sequence_cnt++;
135 }
136 
137 static void flush_sequence(void)
138 {
139     HeapFree(GetProcessHeap(), 0, sequence);
140     sequence = 0;
141     sequence_cnt = sequence_size = 0;
142 }
143 
144 static void ok_sequence(const struct message *expected, const char *context)
145 {
146     static const struct message end_of_sequence = { 0, 0 };
147     const struct message *actual;
148 
149     add_message(&end_of_sequence);
150 
151     actual = sequence;
152 
153     while(expected->procnum && actual->procnum)
154     {
155         ok(expected->procnum == actual->procnum,
156             "%s: the procnum %d was expected, but got procnum %d instead\n",
157             context, expected->procnum, actual->procnum);
158         ok(expected->wParam == actual->wParam,
159             "%s: in procnum %d expecting wParam 0x%lx got 0x%lx\n",
160             context, expected->procnum, expected->wParam, actual->wParam);
161         expected++;
162         actual++;
163     }
164     ok(!expected->procnum, "Received fewer messages than expected\n");
165     ok(!actual->procnum, "Received more messages than expected\n");
166     flush_sequence();
167 }
168 
169 static LRESULT WINAPI wnd_proc_1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
170 {
171     struct message msg;
172 
173     if(message == WM_USER) {
174         msg.wParam = wParam;
175         msg.procnum = 1;
176         add_message(&msg);
177     }
178     return DefWindowProcA(hwnd, message, wParam, lParam);
179 }
180 
181 
182 static WNDPROC orig_proc_3;
183 static LRESULT WINAPI wnd_proc_3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
184 {
185     struct message msg;
186 
187     if(message == WM_USER) {
188         msg.wParam = wParam;
189         msg.procnum = 3;
190         add_message(&msg);
191     }
192     return CallWindowProcA(orig_proc_3, hwnd, message, wParam, lParam);
193 }
194 
195 static LRESULT WINAPI wnd_proc_sub(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uldSubclass, DWORD_PTR dwRefData)
196 {
197     struct message msg;
198 
199     if(message == WM_USER) {
200         msg.wParam = wParam;
201         msg.procnum = uldSubclass;
202         add_message(&msg);
203 
204         if(lParam) {
205             if(dwRefData & DELETE_SELF) {
206                 pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass);
207                 pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass);
208             }
209             if(dwRefData & DELETE_PREV)
210                 pRemoveWindowSubclass(hwnd, wnd_proc_sub, uldSubclass-1);
211             if(dwRefData & SEND_NEST)
212                 SendMessageA(hwnd, WM_USER, wParam+1, 0);
213         }
214     }
215     return pDefSubclassProc(hwnd, message, wParam, lParam);
216 }
217 
218 static void test_subclass(void)
219 {
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     pSetWindowSubclass(hwnd, wnd_proc_sub, 2, 0);
225     SendMessageA(hwnd, WM_USER, 1, 0);
226     SendMessageA(hwnd, WM_USER, 2, 0);
227     ok_sequence(Sub_BasicTest, "Basic");
228 
229     pSetWindowSubclass(hwnd, wnd_proc_sub, 2, DELETE_SELF);
230     SendMessageA(hwnd, WM_USER, 1, 1);
231     ok_sequence(Sub_DeletedTest, "Deleted");
232 
233     SendMessageA(hwnd, WM_USER, 1, 0);
234     ok_sequence(Sub_AfterDeletedTest, "After Deleted");
235 
236     pSetWindowSubclass(hwnd, wnd_proc_sub, 2, 0);
237     orig_proc_3 = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)wnd_proc_3);
238     SendMessageA(hwnd, WM_USER, 1, 0);
239     SendMessageA(hwnd, WM_USER, 2, 0);
240     ok_sequence(Sub_OldAfterNewTest, "Old after New");
241 
242     pSetWindowSubclass(hwnd, wnd_proc_sub, 4, 0);
243     SendMessageA(hwnd, WM_USER, 1, 0);
244     ok_sequence(Sub_MixTest, "Mix");
245 
246     /* Now the fun starts */
247     pSetWindowSubclass(hwnd, wnd_proc_sub, 4, SEND_NEST);
248     SendMessageA(hwnd, WM_USER, 1, 1);
249     ok_sequence(Sub_MixAndNestTest, "Mix and nest");
250 
251     pSetWindowSubclass(hwnd, wnd_proc_sub, 4, SEND_NEST | DELETE_SELF);
252     SendMessageA(hwnd, WM_USER, 1, 1);
253     ok_sequence(Sub_MixNestDelTest, "Mix, nest, del");
254 
255     pSetWindowSubclass(hwnd, wnd_proc_sub, 4, 0);
256     pSetWindowSubclass(hwnd, wnd_proc_sub, 5, DELETE_PREV);
257     SendMessageA(hwnd, WM_USER, 1, 1);
258     ok_sequence(Sub_MixDelPrevTest, "Mix and del prev");
259 
260     DestroyWindow(hwnd);
261 }
262 
263 static BOOL register_window_classes(void)
264 {
265     WNDCLASSA cls;
266     ATOM atom;
267 
268     cls.style = 0;
269     cls.lpfnWndProc = wnd_proc_1;
270     cls.cbClsExtra = 0;
271     cls.cbWndExtra = 0;
272     cls.hInstance = GetModuleHandleA(0);
273     cls.hIcon = 0;
274     cls.hCursor = NULL;
275     cls.hbrBackground = NULL;
276     cls.lpszMenuName = NULL;
277     cls.lpszClassName = "TestSubclass";
278     atom = RegisterClassA(&cls);
279     ok(atom, "failed to register test class\n");
280 
281     return atom != 0;
282 }
283 
284 static BOOL init_function_pointers(void)
285 {
286     HMODULE hmod;
287     void *ptr;
288 
289     hmod = GetModuleHandleA("comctl32.dll");
290     ok(hmod != NULL, "got %p\n", hmod);
291 
292     /* Functions have to be loaded by ordinal. Only XP and W2K3 export
293      * them by name.
294      */
295 #define MAKEFUNC_ORD(f, ord) (p##f = (void*)GetProcAddress(hmod, (LPSTR)(ord)))
296     MAKEFUNC_ORD(SetWindowSubclass, 410);
297     MAKEFUNC_ORD(RemoveWindowSubclass, 412);
298     MAKEFUNC_ORD(DefSubclassProc, 413);
299 #undef MAKEFUNC_ORD
300 
301     if(!pSetWindowSubclass || !pRemoveWindowSubclass || !pDefSubclassProc)
302     {
303         win_skip("SetWindowSubclass and friends are not available\n");
304         return FALSE;
305     }
306 
307     /* test named exports */
308     ptr = GetProcAddress(hmod, "SetWindowSubclass");
309     ok(broken(ptr == 0) || ptr != 0, "expected named export for SetWindowSubclass\n");
310     if(ptr)
311     {
312 #define TESTNAMED(f) \
313     ptr = (void*)GetProcAddress(hmod, #f); \
314     ok(ptr != 0, "expected named export for " #f "\n");
315         TESTNAMED(RemoveWindowSubclass);
316         TESTNAMED(DefSubclassProc);
317         /* GetWindowSubclass exported for V6 only */
318 #undef TESTNAMED
319     }
320 
321     return TRUE;
322 }
323 
324 START_TEST(subclass)
325 {
326     if(!init_function_pointers()) return;
327 
328     if(!register_window_classes()) return;
329 
330     test_subclass();
331 }
332