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