rename tests to dllname_winetest to make it less trouble on exception to change build...
[reactos.git] / reactos / regtests / winetests / comctl32 / subclass.c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <assert.h>
21 #include <stdarg.h>
22
23 #define _WIN32_WINNT 0x0501 /* For SetWindowSubclass/etc */
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%x got 0x%x\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 WndProc1(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 DefWindowProc(hwnd, message, wParam, lParam);
179 }
180
181
182 static WNDPROC origProc3;
183 static LRESULT WINAPI WndProc3(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 CallWindowProc(origProc3, hwnd, message, wParam, lParam);
193 }
194
195 static LRESULT WINAPI WndProcSub(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, WndProcSub, uldSubclass);
207 pRemoveWindowSubclass(hwnd, WndProcSub, uldSubclass);
208 }
209 if(dwRefData & DELETE_PREV)
210 pRemoveWindowSubclass(hwnd, WndProcSub, uldSubclass-1);
211 if(dwRefData & SEND_NEST)
212 SendMessage(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 assert(hwnd);
223
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");
228
229 pSetWindowSubclass(hwnd, WndProcSub, 2, DELETE_SELF);
230 SendMessage(hwnd, WM_USER, 1, 1);
231 ok_sequence(Sub_DeletedTest, "Deleted");
232
233 SendMessage(hwnd, WM_USER, 1, 0);
234 ok_sequence(Sub_AfterDeletedTest, "After Deleted");
235
236 pSetWindowSubclass(hwnd, WndProcSub, 2, 0);
237 origProc3 = (WNDPROC)SetWindowLong(hwnd, GWL_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");
241
242 pSetWindowSubclass(hwnd, WndProcSub, 4, 0);
243 SendMessage(hwnd, WM_USER, 1, 0);
244 ok_sequence(Sub_MixTest, "Mix");
245
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");
250
251 pSetWindowSubclass(hwnd, WndProcSub, 4, SEND_NEST | DELETE_SELF);
252 SendMessage(hwnd, WM_USER, 1, 1);
253 ok_sequence(Sub_MixNestDelTest, "Mix, nest, del");
254
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");
259
260 DestroyWindow(hwnd);
261 }
262
263 static BOOL RegisterWindowClasses(void)
264 {
265 WNDCLASSA cls;
266
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;
278
279 return TRUE;
280 }
281
282 START_TEST(subclass)
283 {
284 HMODULE hdll;
285
286 hdll = GetModuleHandleA("comctl32.dll");
287 assert(hdll);
288 pSetWindowSubclass = (void*)GetProcAddress(hdll, "SetWindowSubclass");
289 pRemoveWindowSubclass = (void*)GetProcAddress(hdll, "RemoveWindowSubclass");
290 pDefSubclassProc = (void*)GetProcAddress(hdll, "DefSubclassProc");
291
292 if(!pSetWindowSubclass || !pRemoveWindowSubclass || !pDefSubclassProc)
293 return;
294
295 if(!RegisterWindowClasses()) assert(0);
296
297 test_subclass();
298 }