1 /* Unit test suite for window classes.
3 * Copyright 2002 Mike McCormack
4 * Copyright 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* To get CS_DROPSHADOW with the MSVC headers */
22 #define _WIN32_WINNT 0x0501
29 #include "wine/test.h"
36 #define NUMCLASSWORDS 4
38 static LRESULT WINAPI ClassTest_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
40 return DefWindowProcW (hWnd, msg, wParam, lParam);
43 /***********************************************************************
45 static void ClassTest(HINSTANCE hInstance, BOOL global)
48 WCHAR className[] = {'T','e','s','t','C','l','a','s','s',0};
49 WCHAR winName[] = {'W','i','n','C','l','a','s','s','T','e','s','t',0};
56 cls.style = CS_HREDRAW | CS_VREDRAW | (global?CS_GLOBALCLASS:0);
57 cls.lpfnWndProc = ClassTest_WndProc;
58 cls.cbClsExtra = NUMCLASSWORDS*sizeof(DWORD);
60 cls.hInstance = hInstance;
61 cls.hIcon = LoadIconW (0, (LPWSTR)IDI_APPLICATION);
62 cls.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
63 cls.hbrBackground = GetStockObject (WHITE_BRUSH);
65 cls.lpszClassName = className;
67 classatom=RegisterClassW(&cls);
68 if (!classatom && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
70 ok(classatom, "failed to register class\n");
72 ok(!RegisterClassW (&cls),
73 "RegisterClass of the same class should fail for the second time\n");
76 hTestWnd = CreateWindowW (className, winName,
77 WS_OVERLAPPEDWINDOW + WS_HSCROLL + WS_VSCROLL,
78 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0,
81 ok(hTestWnd!=0, "Failed to create window\n");
83 /* test initial values of valid classwords */
84 for(i=0; i<NUMCLASSWORDS; i++)
87 ok(!GetClassLongW(hTestWnd,i*sizeof (DWORD)),
88 "GetClassLongW initial value nonzero!\n");
90 "GetClassLongW failed!\n");
95 * GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD))
96 * does not fail on Win 98, though MSDN says it should
99 GetClassLongW(hTestWnd, NUMCLASSWORDS*sizeof(DWORD));
101 "GetClassLongW() with invalid offset did not fail\n");
104 /* set values of valid class words */
105 for(i=0; i<NUMCLASSWORDS; i++)
108 ok(!SetClassLongW(hTestWnd,i*sizeof(DWORD),i+1),
109 "GetClassLongW(%ld) initial value nonzero!\n",i*sizeof(DWORD));
111 "SetClassLongW(%ld) failed!\n",i*sizeof(DWORD));
114 /* test values of valid classwords that we set */
115 for(i=0; i<NUMCLASSWORDS; i++)
118 ok( (i+1) == GetClassLongW(hTestWnd,i*sizeof (DWORD)),
119 "GetClassLongW value doesn't match what was set!\n");
121 "GetClassLongW failed!\n");
124 /* check GetClassName */
125 i = GetClassNameW(hTestWnd, str, sizeof(str));
126 ok(i == lstrlenW(className),
127 "GetClassName returned incorrect length\n");
128 ok(!lstrcmpW(className,str),
129 "GetClassName returned incorrect name for this window's class\n");
131 /* check GetClassInfo with our hInstance */
132 if((test_atom = GetClassInfoW(hInstance, str, &wc)))
134 ok(test_atom == classatom,
135 "class atom did not match\n");
136 ok(wc.cbClsExtra == cls.cbClsExtra,
137 "cbClsExtra did not match\n");
138 ok(wc.cbWndExtra == cls.cbWndExtra,
139 "cbWndExtra did not match\n");
140 ok(wc.hbrBackground == cls.hbrBackground,
141 "hbrBackground did not match\n");
142 ok(wc.hCursor== cls.hCursor,
143 "hCursor did not match\n");
144 ok(wc.hInstance== cls.hInstance,
145 "hInstance did not match\n");
148 ok(FALSE,"GetClassInfo (hinstance) failed!\n");
150 /* check GetClassInfo with zero hInstance */
153 if((test_atom = GetClassInfoW(0, str, &wc)))
155 ok(test_atom == classatom,
156 "class atom did not match %x != %x\n", test_atom, classatom);
157 ok(wc.cbClsExtra == cls.cbClsExtra,
158 "cbClsExtra did not match %x!=%x\n",wc.cbClsExtra,cls.cbClsExtra);
159 ok(wc.cbWndExtra == cls.cbWndExtra,
160 "cbWndExtra did not match %x!=%x\n",wc.cbWndExtra,cls.cbWndExtra);
161 ok(wc.hbrBackground == cls.hbrBackground,
162 "hbrBackground did not match %p!=%p\n",wc.hbrBackground,cls.hbrBackground);
163 ok(wc.hCursor== cls.hCursor,
164 "hCursor did not match %p!=%p\n",wc.hCursor,cls.hCursor);
166 "hInstance not zero for global class %p\n",wc.hInstance);
169 ok(FALSE,"GetClassInfo (0) failed for global class!\n");
173 ok(!GetClassInfoW(0, str, &wc),
174 "GetClassInfo (0) succeeded for local class!\n");
177 ok(!UnregisterClassW(className, hInstance),
178 "Unregister class succeeded with window existing\n");
180 ok(DestroyWindow(hTestWnd),
181 "DestroyWindow() failed!\n");
183 ok(UnregisterClassW(className, hInstance),
184 "UnregisterClass() failed\n");
189 static void check_style( const char *name, int must_exist, UINT style, UINT ignore )
193 if (GetClassInfo( 0, name, &wc ))
195 ok( !(~wc.style & style & ~ignore), "System class %s is missing bits %x (%08x/%08x)\n",
196 name, ~wc.style & style, wc.style, style );
197 ok( !(wc.style & ~style), "System class %s has extra bits %x (%08x/%08x)\n",
198 name, wc.style & ~style, wc.style, style );
201 ok( !must_exist, "System class %s does not exist\n", name );
204 /* test styles of system classes */
205 static void test_styles(void)
207 /* check style bits */
208 check_style( "Button", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
209 check_style( "ComboBox", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
210 check_style( "Edit", 1, CS_PARENTDC | CS_DBLCLKS, 0 );
211 check_style( "ListBox", 1, CS_PARENTDC | CS_DBLCLKS, CS_PARENTDC /*FIXME*/ );
212 check_style( "MDIClient", 1, 0, 0 );
213 check_style( "ScrollBar", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, 0 );
214 check_style( "Static", 1, CS_PARENTDC | CS_DBLCLKS, 0 );
215 check_style( "ComboLBox", 1, CS_SAVEBITS | CS_DBLCLKS, 0 );
216 check_style( "DDEMLEvent", 0, 0, 0 );
217 check_style( "Message", 0, 0, 0 );
218 check_style( "#32768", 1, CS_DROPSHADOW | CS_SAVEBITS | CS_DBLCLKS, CS_DROPSHADOW ); /* menu */
219 check_style( "#32769", 1, CS_DBLCLKS, 0 ); /* desktop */
220 check_style( "#32770", 1, CS_SAVEBITS | CS_DBLCLKS, 0 ); /* dialog */
221 todo_wine { check_style( "#32771", 1, CS_SAVEBITS | CS_HREDRAW | CS_VREDRAW, 0 ); } /* task switch */
222 check_style( "#32772", 1, 0, 0 ); /* icon title */
225 static void check_class(HINSTANCE inst, const char *name, const char *menu_name)
228 UINT atom = GetClassInfo(inst,name,&wc);
229 ok( atom, "Class %s %p not found\n", name, inst );
232 if (wc.lpszMenuName && menu_name)
233 ok( !strcmp( menu_name, wc.lpszMenuName ), "Wrong name %s/%s for class %s %p\n",
234 wc.lpszMenuName, menu_name, name, inst );
236 ok( !menu_name == !wc.lpszMenuName, "Wrong name %p/%p for class %s %p\n",
237 wc.lpszMenuName, menu_name, name, inst );
241 static void check_instance( const char *name, HINSTANCE inst, HINSTANCE info_inst, HINSTANCE gcl_inst )
246 ok( GetClassInfo( inst, name, &wc ), "Couldn't find class %s inst %p\n", name, inst );
247 ok( wc.hInstance == info_inst, "Wrong info instance %p/%p for class %s\n",
248 wc.hInstance, info_inst, name );
249 hwnd = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, inst, 0 );
250 ok( hwnd != NULL, "Couldn't create window for class %s inst %p\n", name, inst );
251 ok( (HINSTANCE)GetClassLong( hwnd, GCL_HMODULE ) == gcl_inst,
252 "Wrong GCL instance %p/%p for class %s\n",
253 (HINSTANCE)GetClassLong( hwnd, GCL_HMODULE ), gcl_inst, name );
257 /* test various instance parameters */
258 static void test_instances(void)
262 const char *name = "__test__";
263 HINSTANCE kernel32 = GetModuleHandleA("kernel32");
264 HINSTANCE user32 = GetModuleHandleA("user32");
265 HINSTANCE main_module = GetModuleHandleA(NULL);
267 memset( &cls, 0, sizeof(cls) );
268 cls.style = CS_HREDRAW | CS_VREDRAW;
269 cls.lpfnWndProc = ClassTest_WndProc;
272 cls.lpszClassName = name;
274 cls.lpszMenuName = "main_module";
275 cls.hInstance = main_module;
276 ok( RegisterClassA( &cls ), "Failed to register local class for main module\n" );
277 check_class( main_module, name, "main_module" );
278 check_instance( name, main_module, main_module, main_module );
280 cls.lpszMenuName = "kernel32";
281 cls.hInstance = kernel32;
282 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
283 check_class( kernel32, name, "kernel32" );
284 check_class( main_module, name, "main_module" );
285 check_instance( name, kernel32, kernel32, kernel32 );
286 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
288 /* setting global flag doesn't change status of class */
289 hwnd = CreateWindowExA( 0, name, "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
290 SetClassLongA( hwnd, GCL_STYLE, CS_GLOBALCLASS );
291 cls.lpszMenuName = "kernel32";
292 cls.hInstance = kernel32;
293 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
294 check_class( kernel32, name, "kernel32" );
295 check_class( main_module, name, "main_module" );
296 check_instance( name, kernel32, kernel32, kernel32 );
297 check_instance( name, main_module, main_module, main_module );
298 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
300 /* changing the instance doesn't make it global */
301 SetClassLongA( hwnd, GCL_HMODULE, 0 );
302 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
303 check_class( kernel32, name, "kernel32" );
304 check_instance( name, kernel32, kernel32, kernel32 );
305 ok( !GetClassInfo( 0, name, &wc ), "Class found with null instance\n" );
306 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
308 /* GetClassInfo with instance 0 finds user32 instance */
309 SetClassLongA( hwnd, GCL_HMODULE, (LONG)user32 );
310 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
311 check_class( kernel32, name, "kernel32" );
312 check_class( user32, name, "main_module" );
313 check_class( 0, name, "main_module" );
314 check_instance( name, kernel32, kernel32, kernel32 );
315 check_instance( name, user32, 0, user32 );
316 check_instance( name, 0, 0, kernel32 );
317 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
319 SetClassLongA( hwnd, GCL_HMODULE, 0x12345678 );
320 ok( RegisterClassA( &cls ), "Failed to register local class for kernel32\n" );
321 check_class( kernel32, name, "kernel32" );
322 check_class( (HINSTANCE)0x12345678, name, "main_module" );
323 check_instance( name, kernel32, kernel32, kernel32 );
324 check_instance( name, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0x12345678 );
325 ok( !GetClassInfo( 0, name, &wc ), "Class found with null instance\n" );
327 /* creating a window with instance 0 uses the first class found */
328 cls.hInstance = (HINSTANCE)0xdeadbeef;
329 cls.lpszMenuName = "deadbeef";
331 ok( RegisterClassA( &cls ), "Failed to register local class for deadbeef\n" );
332 hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
333 ok( GetClassLong( hwnd2, GCL_HMODULE ) == 0xdeadbeef,
334 "Didn't get deadbeef class for null instance\n" );
335 DestroyWindow( hwnd2 );
336 ok( UnregisterClassA( name, (HINSTANCE)0xdeadbeef ), "Unregister failed for deadbeef\n" );
338 hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
339 ok( (HINSTANCE)GetClassLong( hwnd2, GCL_HMODULE ) == kernel32,
340 "Didn't get kernel32 class for null instance\n" );
341 DestroyWindow( hwnd2 );
343 ok( UnregisterClassA( name, kernel32 ), "Unregister failed for kernel32\n" );
345 hwnd2 = CreateWindowExA( 0, name, "test_window", 0, 0, 0, 0, 0, 0, 0, NULL, 0 );
346 ok( GetClassLong( hwnd2, GCL_HMODULE ) == 0x12345678,
347 "Didn't get 12345678 class for null instance\n" );
348 DestroyWindow( hwnd2 );
350 SetClassLongA( hwnd, GCL_HMODULE, (LONG)main_module );
351 DestroyWindow( hwnd );
353 /* null handle means the same thing as main module */
354 cls.lpszMenuName = "null";
356 ok( !RegisterClassA( &cls ), "Succeeded registering local class for null instance\n" );
357 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
358 ok( UnregisterClassA( name, main_module ), "Unregister failed for main module\n" );
360 ok( RegisterClassA( &cls ), "Failed to register local class for null instance\n" );
361 /* must be found with main module handle */
362 check_class( main_module, name, "null" );
363 check_instance( name, main_module, main_module, main_module );
364 ok( !GetClassInfo( 0, name, &wc ), "Class found with null instance\n" );
365 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %ld\n", GetLastError() );
366 ok( UnregisterClassA( name, 0 ), "Unregister failed for null instance\n" );
368 /* registering for user32 always fails */
369 cls.lpszMenuName = "user32";
370 cls.hInstance = user32;
371 ok( !RegisterClassA( &cls ), "Succeeded registering local class for user32\n" );
372 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error code %ld\n", GetLastError() );
373 cls.style |= CS_GLOBALCLASS;
374 ok( !RegisterClassA( &cls ), "Succeeded registering global class for user32\n" );
375 ok( GetLastError() == ERROR_INVALID_PARAMETER, "Wrong error code %ld\n", GetLastError() );
377 /* unregister is OK though */
378 cls.hInstance = main_module;
379 ok( RegisterClassA( &cls ), "Failed to register global class for main module\n" );
380 ok( UnregisterClassA( name, user32 ), "Unregister failed for user32\n" );
382 /* instance doesn't matter for global class */
383 cls.style |= CS_GLOBALCLASS;
384 cls.lpszMenuName = "main_module";
385 cls.hInstance = main_module;
386 ok( RegisterClassA( &cls ), "Failed to register global class for main module\n" );
387 cls.lpszMenuName = "kernel32";
388 cls.hInstance = kernel32;
389 ok( !RegisterClassA( &cls ), "Succeeded registering local class for kernel32\n" );
390 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
391 /* even if global flag is cleared */
392 hwnd = CreateWindowExA( 0, name, "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
393 SetClassLongA( hwnd, GCL_STYLE, 0 );
394 ok( !RegisterClassA( &cls ), "Succeeded registering local class for kernel32\n" );
395 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
397 check_class( main_module, name, "main_module" );
398 check_class( kernel32, name, "main_module" );
399 check_class( 0, name, "main_module" );
400 check_class( (HINSTANCE)0x12345678, name, "main_module" );
401 check_instance( name, main_module, main_module, main_module );
402 check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, main_module );
404 /* changing the instance for global class doesn't make much difference */
405 SetClassLongA( hwnd, GCL_HMODULE, 0xdeadbeef );
406 check_instance( name, main_module, main_module, (HINSTANCE)0xdeadbeef );
407 check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef );
409 DestroyWindow( hwnd );
410 ok( UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister failed for main module global\n" );
411 ok( !UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister succeeded the second time\n" );
412 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %ld\n", GetLastError() );
414 cls.hInstance = (HINSTANCE)0x12345678;
415 ok( RegisterClassA( &cls ), "Failed to register global class for dummy instance\n" );
416 check_instance( name, main_module, main_module, (HINSTANCE)0x12345678 );
417 check_instance( name, (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, (HINSTANCE)0x12345678 );
418 ok( UnregisterClassA( name, (HINSTANCE)0x87654321 ), "Unregister failed for main module global\n" );
420 /* check system classes */
422 /* we cannot register a global class with the name of a system class */
423 cls.style |= CS_GLOBALCLASS;
424 cls.lpszMenuName = "button_main_module";
425 cls.lpszClassName = "BUTTON";
426 cls.hInstance = main_module;
427 ok( !RegisterClassA( &cls ), "Succeeded registering global button class for main module\n" );
428 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
429 cls.hInstance = kernel32;
430 ok( !RegisterClassA( &cls ), "Succeeded registering global button class for kernel32\n" );
431 ok( GetLastError() == ERROR_CLASS_ALREADY_EXISTS, "Wrong error code %ld\n", GetLastError() );
433 /* local class is OK however */
434 cls.style &= ~CS_GLOBALCLASS;
435 cls.lpszMenuName = "button_main_module";
436 cls.hInstance = main_module;
437 ok( RegisterClassA( &cls ), "Failed to register local button class for main module\n" );
438 check_class( main_module, "BUTTON", "button_main_module" );
439 cls.lpszMenuName = "button_kernel32";
440 cls.hInstance = kernel32;
441 ok( RegisterClassA( &cls ), "Failed to register local button class for kernel32\n" );
442 check_class( kernel32, "BUTTON", "button_kernel32" );
443 check_class( main_module, "BUTTON", "button_main_module" );
444 ok( UnregisterClassA( "BUTTON", kernel32 ), "Unregister failed for kernel32 button\n" );
445 ok( UnregisterClassA( "BUTTON", main_module ), "Unregister failed for main module button\n" );
446 /* GetClassInfo sets instance to passed value for global classes */
447 check_instance( "BUTTON", 0, 0, user32 );
448 check_instance( "BUTTON", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
449 check_instance( "BUTTON", user32, 0, user32 );
451 /* we can unregister system classes */
452 ok( GetClassInfo( 0, "BUTTON", &wc ), "Button class not found with null instance\n" );
453 ok( GetClassInfo( kernel32, "BUTTON", &wc ), "Button class not found with kernel32\n" );
454 ok( UnregisterClass( "BUTTON", (HINSTANCE)0x12345678 ), "Failed to unregister button\n" );
455 ok( !UnregisterClass( "BUTTON", (HINSTANCE)0x87654321 ), "Unregistered button a second time\n" );
456 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %ld\n", GetLastError() );
457 ok( !GetClassInfo( 0, "BUTTON", &wc ), "Button still exists\n" );
458 ok( GetLastError() == ERROR_CLASS_DOES_NOT_EXIST, "Wrong error code %ld\n", GetLastError() );
460 /* we can change the instance of a system class */
461 check_instance( "EDIT", (HINSTANCE)0xdeadbeef, (HINSTANCE)0xdeadbeef, user32 );
462 hwnd = CreateWindowExA( 0, "EDIT", "test", 0, 0, 0, 0, 0, 0, 0, main_module, 0 );
463 SetClassLongA( hwnd, GCL_HMODULE, 0xdeadbeef );
464 check_instance( "EDIT", (HINSTANCE)0x12345678, (HINSTANCE)0x12345678, (HINSTANCE)0xdeadbeef );
469 HANDLE hInstance = GetModuleHandleA( NULL );
471 ClassTest(hInstance,FALSE);
472 ClassTest(hInstance,TRUE);