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