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 
47 static int sequence_cnt, sequence_size;
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     }
125     if (sequence_cnt == sequence_size)
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 {
140     heap_free(sequence);
141     sequence = NULL;
142     sequence_cnt = sequence_size = 0;
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 
170 static LRESULT WINAPI wnd_proc_1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
171 {
172     struct message msg;
173 
174     if(message == WM_USER) {
175         msg.wParam = wParam;
176         msg.procnum = 1;
177         add_message(&msg);
178     }
179     return DefWindowProcA(hwnd, message, wParam, lParam);
180 }
181 
182 
183 static WNDPROC orig_proc_3;
184 static LRESULT WINAPI wnd_proc_3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
185 {
186     struct message msg;
187 
188     if(message == WM_USER) {
189         msg.wParam = wParam;
190         msg.procnum = 3;
191         add_message(&msg);
192     }
193     return CallWindowProcA(orig_proc_3, hwnd, message, wParam, lParam);
194 }
195 
196 static LRESULT WINAPI wnd_proc_sub(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uldSubclass, DWORD_PTR dwRefData)
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)
213                 SendMessageA(hwnd, WM_USER, wParam+1, 0);
214         }
215     }
216     return pDefSubclassProc(hwnd, message, wParam, lParam);
217 }
218 
219 static void test_subclass(void)
220 {
221     HWND hwnd = CreateWindowExA(0, "TestSubclass", "Test subclass", WS_OVERLAPPEDWINDOW,
222                            100, 100, 200, 200, 0, 0, 0, NULL);
223     ok(hwnd != NULL, "failed to create test subclass wnd\n");
224 
225     pSetWindowSubclass(hwnd, wnd_proc_sub, 2, 0);
226     SendMessageA(hwnd, WM_USER, 1, 0);
227     SendMessageA(hwnd, WM_USER, 2, 0);
228     ok_sequence(Sub_BasicTest, "Basic");
229 
230     pSetWindowSubclass(hwnd, wnd_proc_sub, 2, DELETE_SELF);
231     SendMessageA(hwnd, WM_USER, 1, 1);
232     ok_sequence(Sub_DeletedTest, "Deleted");
233 
234     SendMessageA(hwnd, WM_USER, 1, 0);
235     ok_sequence(Sub_AfterDeletedTest, "After Deleted");
236 
237     pSetWindowSubclass(hwnd, wnd_proc_sub, 2, 0);
238     orig_proc_3 = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC, (LONG_PTR)wnd_proc_3);
239     SendMessageA(hwnd, WM_USER, 1, 0);
240     SendMessageA(hwnd, WM_USER, 2, 0);
241     ok_sequence(Sub_OldAfterNewTest, "Old after New");
242 
243     pSetWindowSubclass(hwnd, wnd_proc_sub, 4, 0);
244     SendMessageA(hwnd, WM_USER, 1, 0);
245     ok_sequence(Sub_MixTest, "Mix");
246 
247     /* Now the fun starts */
248     pSetWindowSubclass(hwnd, wnd_proc_sub, 4, SEND_NEST);
249     SendMessageA(hwnd, WM_USER, 1, 1);
250     ok_sequence(Sub_MixAndNestTest, "Mix and nest");
251 
252     pSetWindowSubclass(hwnd, wnd_proc_sub, 4, SEND_NEST | DELETE_SELF);
253     SendMessageA(hwnd, WM_USER, 1, 1);
254     ok_sequence(Sub_MixNestDelTest, "Mix, nest, del");
255 
256     pSetWindowSubclass(hwnd, wnd_proc_sub, 4, 0);
257     pSetWindowSubclass(hwnd, wnd_proc_sub, 5, DELETE_PREV);
258     SendMessageA(hwnd, WM_USER, 1, 1);
259     ok_sequence(Sub_MixDelPrevTest, "Mix and del prev");
260 
261     DestroyWindow(hwnd);
262 }
263 
264 static BOOL register_window_classes(void)
265 {
266     WNDCLASSA cls;
267     ATOM atom;
268 
269     cls.style = 0;
270     cls.lpfnWndProc = wnd_proc_1;
271     cls.cbClsExtra = 0;
272     cls.cbWndExtra = 0;
273     cls.hInstance = GetModuleHandleA(0);
274     cls.hIcon = 0;
275     cls.hCursor = NULL;
276     cls.hbrBackground = NULL;
277     cls.lpszMenuName = NULL;
278     cls.lpszClassName = "TestSubclass";
279     atom = RegisterClassA(&cls);
280     ok(atom, "failed to register test class\n");
281 
282     return atom != 0;
283 }
284 
285 static BOOL init_function_pointers(void)
286 {
287     HMODULE hmod;
288     void *ptr;
289 
290     hmod = LoadLibraryA("comctl32.dll");
291     ok(hmod != NULL, "got %p\n", hmod);
292 
293     /* Functions have to be loaded by ordinal. Only XP and W2K3 export
294      * them by name.
295      */
296 #define MAKEFUNC_ORD(f, ord) (p##f = (void*)GetProcAddress(hmod, (LPSTR)(ord)))
297     MAKEFUNC_ORD(SetWindowSubclass, 410);
298     MAKEFUNC_ORD(RemoveWindowSubclass, 412);
299     MAKEFUNC_ORD(DefSubclassProc, 413);
300 #undef MAKEFUNC_ORD
301 
302     if(!pSetWindowSubclass || !pRemoveWindowSubclass || !pDefSubclassProc)
303     {
304         win_skip("SetWindowSubclass and friends are not available\n");
305         return FALSE;
306     }
307 
308     /* test named exports */
309     ptr = GetProcAddress(hmod, "SetWindowSubclass");
310     ok(broken(ptr == 0) || ptr != 0, "expected named export for SetWindowSubclass\n");
311     if(ptr)
312     {
313 #define TESTNAMED(f) \
314     ptr = (void*)GetProcAddress(hmod, #f); \
315     ok(ptr != 0, "expected named export for " #f "\n");
316         TESTNAMED(RemoveWindowSubclass);
317         TESTNAMED(DefSubclassProc);
318         /* GetWindowSubclass exported for V6 only */
319 #undef TESTNAMED
320     }
321 
322     return TRUE;
323 }
324 
325 START_TEST(subclass)
326 {
327     if(!init_function_pointers()) return;
328 
329     if(!register_window_classes()) return;
330 
331     test_subclass();
332 }
333