push 337eb2e2d902d84a5d689451984c5832d7e04fc4
[wine/hacks.git] / dlls / comctl32 / tests / subclass.c
blobc225f726fe724d38b15aaccc3f1fee0c38fb75bd
1 /* Unit tests for subclassed windows.
3 * Copyright 2004 Kevin Koltzau
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.
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.
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
20 #define _WIN32_WINNT 0x0501 /* For SetWindowSubclass/etc */
22 #include <assert.h>
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "commctrl.h"
31 #include "wine/test.h"
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);
37 #define SEND_NEST 0x01
38 #define DELETE_SELF 0x02
39 #define DELETE_PREV 0x04
41 struct message {
42 int procnum; /* WndProc id message is expected from */
43 WPARAM wParam; /* expected value of wParam */
46 static int sequence_cnt, sequence_size;
47 static struct message* sequence;
49 static const struct message Sub_BasicTest[] = {
50 { 2, 1 },
51 { 1, 1 },
52 { 2, 2 },
53 { 1, 2 },
54 { 0 }
57 static const struct message Sub_DeletedTest[] = {
58 { 2, 1 },
59 { 1, 1 },
60 { 0 }
63 static const struct message Sub_AfterDeletedTest[] = {
64 { 1, 1 },
65 { 0 }
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 }
78 static const struct message Sub_MixTest[] = {
79 { 3, 1 },
80 { 4, 1 },
81 { 2, 1 },
82 { 1, 1 },
83 { 0 }
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 }
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 }
109 static const struct message Sub_MixDelPrevTest[] = {
110 { 3, 1 },
111 { 5, 1 },
112 { 2, 1 },
113 { 1, 1 },
114 { 0 }
117 static void add_message(const struct message *msg)
119 if (!sequence)
121 sequence_size = 10;
122 sequence = HeapAlloc( GetProcessHeap(), 0, sequence_size * sizeof (struct message) );
124 if (sequence_cnt == sequence_size)
126 sequence_size *= 2;
127 sequence = HeapReAlloc( GetProcessHeap(), 0, sequence, sequence_size * sizeof (struct message) );
129 assert(sequence);
131 sequence[sequence_cnt].wParam = msg->wParam;
132 sequence[sequence_cnt].procnum = msg->procnum;
134 sequence_cnt++;
137 static void flush_sequence(void)
139 HeapFree(GetProcessHeap(), 0, sequence);
140 sequence = 0;
141 sequence_cnt = sequence_size = 0;
144 static void ok_sequence(const struct message *expected, const char *context)
146 static const struct message end_of_sequence = { 0, 0 };
147 const struct message *actual;
149 add_message(&end_of_sequence);
151 actual = sequence;
153 while(expected->procnum && actual->procnum)
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++;
164 ok(!expected->procnum, "Received fewer messages than expected\n");
165 ok(!actual->procnum, "Received more messages than expected\n");
166 flush_sequence();
169 static LRESULT WINAPI WndProc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
171 struct message msg;
173 if(message == WM_USER) {
174 msg.wParam = wParam;
175 msg.procnum = 1;
176 add_message(&msg);
178 return DefWindowProc(hwnd, message, wParam, lParam);
182 static WNDPROC origProc3;
183 static LRESULT WINAPI WndProc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
185 struct message msg;
187 if(message == WM_USER) {
188 msg.wParam = wParam;
189 msg.procnum = 3;
190 add_message(&msg);
192 return CallWindowProc(origProc3, hwnd, message, wParam, lParam);
195 static LRESULT WINAPI WndProcSub(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uldSubclass, DWORD_PTR dwRefData)
197 struct message msg;
199 if(message == WM_USER) {
200 msg.wParam = wParam;
201 msg.procnum = uldSubclass;
202 add_message(&msg);
204 if(lParam) {
205 if(dwRefData & DELETE_SELF) {
206 pRemoveWindowSubclass(hwnd, WndProcSub, uldSubclass);
207 pRemoveWindowSubclass(hwnd, WndProcSub, uldSubclass);
209 if(dwRefData & DELETE_PREV)
210 pRemoveWindowSubclass(hwnd, WndProcSub, uldSubclass-1);
211 if(dwRefData & SEND_NEST)
212 SendMessage(hwnd, WM_USER, wParam+1, 0);
215 return pDefSubclassProc(hwnd, message, wParam, lParam);
218 static void test_subclass(void)
220 HWND hwnd = CreateWindowExA(0, "TestSubclass", "Test subclass", WS_OVERLAPPEDWINDOW,
221 100, 100, 200, 200, 0, 0, 0, NULL);
222 assert(hwnd);
224 pSetWindowSubclass(hwnd, WndProcSub, 2, 0);
225 SendMessage(hwnd, WM_USER, 1, 0);
226 SendMessage(hwnd, WM_USER, 2, 0);
227 ok_sequence(Sub_BasicTest, "Basic");
229 pSetWindowSubclass(hwnd, WndProcSub, 2, DELETE_SELF);
230 SendMessage(hwnd, WM_USER, 1, 1);
231 ok_sequence(Sub_DeletedTest, "Deleted");
233 SendMessage(hwnd, WM_USER, 1, 0);
234 ok_sequence(Sub_AfterDeletedTest, "After Deleted");
236 pSetWindowSubclass(hwnd, WndProcSub, 2, 0);
237 origProc3 = (WNDPROC)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG)WndProc3);
238 SendMessage(hwnd, WM_USER, 1, 0);
239 SendMessage(hwnd, WM_USER, 2, 0);
240 ok_sequence(Sub_OldAfterNewTest, "Old after New");
242 pSetWindowSubclass(hwnd, WndProcSub, 4, 0);
243 SendMessage(hwnd, WM_USER, 1, 0);
244 ok_sequence(Sub_MixTest, "Mix");
246 /* Now the fun starts */
247 pSetWindowSubclass(hwnd, WndProcSub, 4, SEND_NEST);
248 SendMessage(hwnd, WM_USER, 1, 1);
249 ok_sequence(Sub_MixAndNestTest, "Mix and nest");
251 pSetWindowSubclass(hwnd, WndProcSub, 4, SEND_NEST | DELETE_SELF);
252 SendMessage(hwnd, WM_USER, 1, 1);
253 ok_sequence(Sub_MixNestDelTest, "Mix, nest, del");
255 pSetWindowSubclass(hwnd, WndProcSub, 4, 0);
256 pSetWindowSubclass(hwnd, WndProcSub, 5, DELETE_PREV);
257 SendMessage(hwnd, WM_USER, 1, 1);
258 ok_sequence(Sub_MixDelPrevTest, "Mix and del prev");
260 DestroyWindow(hwnd);
263 static BOOL RegisterWindowClasses(void)
265 WNDCLASSA cls;
267 cls.style = 0;
268 cls.lpfnWndProc = WndProc1;
269 cls.cbClsExtra = 0;
270 cls.cbWndExtra = 0;
271 cls.hInstance = GetModuleHandleA(0);
272 cls.hIcon = 0;
273 cls.hCursor = NULL;
274 cls.hbrBackground = NULL;
275 cls.lpszMenuName = NULL;
276 cls.lpszClassName = "TestSubclass";
277 if(!RegisterClassA(&cls)) return FALSE;
279 return TRUE;
282 START_TEST(subclass)
284 HMODULE hdll;
286 hdll = GetModuleHandleA("comctl32.dll");
287 assert(hdll);
288 /* Functions have to be loaded by ordinal. Only XP and W2K3 export
289 * them by name.
291 pSetWindowSubclass = (void*)GetProcAddress(hdll, (LPSTR)410);
292 pRemoveWindowSubclass = (void*)GetProcAddress(hdll, (LPSTR)412);
293 pDefSubclassProc = (void*)GetProcAddress(hdll, (LPSTR)413);
295 if(!pSetWindowSubclass || !pRemoveWindowSubclass || !pDefSubclassProc)
296 return;
298 if(!RegisterWindowClasses()) assert(0);
300 test_subclass();