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
23 #define _WIN32_WINNT 0x0501 /* For SetWindowSubclass/etc */
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
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[] = {
57 static const struct message Sub_DeletedTest[] = {
63 static const struct message Sub_AfterDeletedTest[] = {
68 static const struct message Sub_OldAfterNewTest[] = {
78 static const struct message Sub_MixTest[] = {
86 static const struct message Sub_MixAndNestTest[] = {
98 static const struct message Sub_MixNestDelTest[] = {
109 static const struct message Sub_MixDelPrevTest[] = {
117 static void add_message(const struct message *msg)
122 sequence = HeapAlloc( GetProcessHeap(), 0, sequence_size * sizeof (struct message) );
124 if (sequence_cnt == sequence_size)
127 sequence = HeapReAlloc( GetProcessHeap(), 0, sequence, sequence_size * sizeof (struct message) );
131 sequence[sequence_cnt].wParam = msg->wParam;
132 sequence[sequence_cnt].procnum = msg->procnum;
137 static void flush_sequence(void)
139 HeapFree(GetProcessHeap(), 0, sequence);
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);
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%x got 0x%x\n",
160 context, expected->procnum, expected->wParam, actual->wParam);
164 ok(!expected->procnum, "Received fewer messages than expected\n");
165 ok(!actual->procnum, "Received more messages than expected\n");
169 static LRESULT WINAPI WndProc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
173 if(message == WM_USER) {
178 return DefWindowProc(hwnd, message, wParam, lParam);
182 static WNDPROC origProc3;
183 static LRESULT WINAPI WndProc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
187 if(message == WM_USER) {
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)
199 if(message == WM_USER) {
201 msg.procnum = uldSubclass;
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);
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");
263 static BOOL RegisterWindowClasses(void)
268 cls.lpfnWndProc = WndProc1;
271 cls.hInstance = GetModuleHandleA(0);
274 cls.hbrBackground = NULL;
275 cls.lpszMenuName = NULL;
276 cls.lpszClassName = "TestSubclass";
277 if(!RegisterClassA(&cls)) return FALSE;
286 hdll = GetModuleHandleA("comctl32.dll");
288 /* Functions have to be loaded by ordinal. Only XP and W2K3 export
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)
298 if(!RegisterWindowClasses()) assert(0);