8b0781d1fcc4cb6f34bfd47f2c2de71b51b6275c
[reactos.git] / rostests / winetests / comctl32 / misc.c
1 /*
2 * Misc tests
3 *
4 * Copyright 2006 Paul Vriens
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 //#include <stdio.h>
22 //#include <windows.h>
23
24 #include <wine/test.h>
25 #include <wingdi.h>
26 #include <winuser.h>
27 #include <commctrl.h>
28 #include "v6util.h"
29
30 static PVOID (WINAPI * pAlloc)(LONG);
31 static PVOID (WINAPI * pReAlloc)(PVOID, LONG);
32 static BOOL (WINAPI * pFree)(PVOID);
33 static LONG (WINAPI * pGetSize)(PVOID);
34
35 static INT (WINAPI * pStr_GetPtrA)(LPCSTR, LPSTR, INT);
36 static BOOL (WINAPI * pStr_SetPtrA)(LPSTR, LPCSTR);
37 static INT (WINAPI * pStr_GetPtrW)(LPCWSTR, LPWSTR, INT);
38 static BOOL (WINAPI * pStr_SetPtrW)(LPWSTR, LPCWSTR);
39
40 static HMODULE hComctl32 = 0;
41
42 static char testicon_data[] =
43 {
44 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x02, 0x00, 0x00, 0x01, 0x00,
45 0x20, 0x00, 0x40, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x28, 0x00,
46 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00,
47 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x12, 0x0b,
48 0x00, 0x00, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0xde, 0xde, 0xde, 0xff, 0xde, 0xde, 0xde, 0xff, 0xde, 0xde,
50 0xde, 0xff, 0xde, 0xde, 0xde, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00
52 };
53
54 #define COMCTL32_GET_PROC(ordinal, func) \
55 p ## func = (void*)GetProcAddress(hComctl32, (LPSTR)ordinal); \
56 if(!p ## func) { \
57 trace("GetProcAddress(%d)(%s) failed\n", ordinal, #func); \
58 FreeLibrary(hComctl32); \
59 }
60
61 static BOOL InitFunctionPtrs(void)
62 {
63 hComctl32 = LoadLibraryA("comctl32.dll");
64
65 if(!hComctl32)
66 {
67 trace("Could not load comctl32.dll\n");
68 return FALSE;
69 }
70
71 COMCTL32_GET_PROC(71, Alloc);
72 COMCTL32_GET_PROC(72, ReAlloc);
73 COMCTL32_GET_PROC(73, Free);
74 COMCTL32_GET_PROC(74, GetSize);
75
76 COMCTL32_GET_PROC(233, Str_GetPtrA)
77 COMCTL32_GET_PROC(234, Str_SetPtrA)
78 COMCTL32_GET_PROC(235, Str_GetPtrW)
79 COMCTL32_GET_PROC(236, Str_SetPtrW)
80
81 return TRUE;
82 }
83
84 static void test_GetPtrAW(void)
85 {
86 if (pStr_GetPtrA)
87 {
88 static const char source[] = "Just a source string";
89 static const char desttest[] = "Just a destination string";
90 static char dest[MAX_PATH];
91 int sourcelen;
92 int destsize = MAX_PATH;
93 int count;
94
95 sourcelen = strlen(source) + 1;
96
97 count = pStr_GetPtrA(NULL, NULL, 0);
98 ok (count == 0, "Expected count to be 0, it was %d\n", count);
99
100 if (0)
101 {
102 /* Crashes on W98, NT4, W2K, XP, W2K3
103 * Our implementation also crashes and we should probably leave
104 * it like that.
105 */
106 count = pStr_GetPtrA(NULL, NULL, destsize);
107 trace("count : %d\n", count);
108 }
109
110 count = pStr_GetPtrA(source, NULL, 0);
111 ok (count == sourcelen ||
112 broken(count == sourcelen - 1), /* win9x */
113 "Expected count to be %d, it was %d\n", sourcelen, count);
114
115 strcpy(dest, desttest);
116 count = pStr_GetPtrA(source, dest, 0);
117 ok (count == sourcelen ||
118 broken(count == 0), /* win9x */
119 "Expected count to be %d, it was %d\n", sourcelen, count);
120 ok (!lstrcmpA(dest, desttest) ||
121 broken(!lstrcmpA(dest, "")), /* Win7 */
122 "Expected destination to not have changed\n");
123
124 count = pStr_GetPtrA(source, NULL, destsize);
125 ok (count == sourcelen ||
126 broken(count == sourcelen - 1), /* win9x */
127 "Expected count to be %d, it was %d\n", sourcelen, count);
128
129 count = pStr_GetPtrA(source, dest, destsize);
130 ok (count == sourcelen ||
131 broken(count == sourcelen - 1), /* win9x */
132 "Expected count to be %d, it was %d\n", sourcelen, count);
133 ok (!lstrcmpA(source, dest), "Expected source and destination to be the same\n");
134
135 strcpy(dest, desttest);
136 count = pStr_GetPtrA(NULL, dest, destsize);
137 ok (count == 0, "Expected count to be 0, it was %d\n", count);
138 ok (dest[0] == '\0', "Expected destination to be cut-off and 0 terminated\n");
139
140 destsize = 15;
141 count = pStr_GetPtrA(source, dest, destsize);
142 ok (count == 15 ||
143 broken(count == 14), /* win9x */
144 "Expected count to be 15, it was %d\n", count);
145 ok (!memcmp(source, dest, 14), "Expected first part of source and destination to be the same\n");
146 ok (dest[14] == '\0', "Expected destination to be cut-off and 0 terminated\n");
147 }
148 }
149
150 static void test_Alloc(void)
151 {
152 PCHAR p;
153 BOOL res;
154 DWORD size, min;
155
156 /* allocate size 0 */
157 p = pAlloc(0);
158 ok(p != NULL, "Expected non-NULL ptr\n");
159
160 /* get the minimum size */
161 min = pGetSize(p);
162
163 /* free the block */
164 res = pFree(p);
165 ok(res == TRUE, "Expected TRUE, got %d\n", res);
166
167 /* allocate size 1 */
168 p = pAlloc(1);
169 ok(p != NULL, "Expected non-NULL ptr\n");
170
171 /* get the allocated size */
172 size = pGetSize(p);
173 ok(size == 1 ||
174 broken(size == min), /* win9x */
175 "Expected 1, got %d\n", size);
176
177 /* reallocate the block */
178 p = pReAlloc(p, 2);
179 ok(p != NULL, "Expected non-NULL ptr\n");
180
181 /* get the new size */
182 size = pGetSize(p);
183 ok(size == 2 ||
184 broken(size == min), /* win9x */
185 "Expected 2, got %d\n", size);
186
187 /* free the block */
188 res = pFree(p);
189 ok(res == TRUE, "Expected TRUE, got %d\n", res);
190
191 /* free a NULL ptr */
192 res = pFree(NULL);
193 ok(res == TRUE ||
194 broken(res == FALSE), /* win9x */
195 "Expected TRUE, got %d\n", res);
196
197 /* reallocate a NULL ptr */
198 p = pReAlloc(NULL, 2);
199 ok(p != NULL, "Expectd non-NULL ptr\n");
200
201 res = pFree(p);
202 ok(res == TRUE, "Expected TRUE, got %d\n", res);
203 }
204
205 static void test_TaskDialogIndirect(void)
206 {
207 HINSTANCE hinst;
208 void *ptr, *ptr2;
209
210 hinst = LoadLibraryA("comctl32.dll");
211
212 ptr = GetProcAddress(hinst, "TaskDialogIndirect");
213 if (!ptr)
214 {
215 #ifdef __REACTOS__
216 /* Skipped on 2k3 */
217 skip("TaskDialogIndirect not exported by name\n");
218 #else
219 win_skip("TaskDialogIndirect not exported by name\n");
220 #endif
221 return;
222 }
223
224 ptr2 = GetProcAddress(hinst, (const CHAR*)345);
225 ok(ptr == ptr2, "got wrong pointer for ordinal 345, %p expected %p\n", ptr2, ptr);
226 }
227
228 static void test_LoadIconWithScaleDown(void)
229 {
230 static const WCHAR nonexisting_fileW[] = {'n','o','n','e','x','i','s','t','i','n','g','.','i','c','o',0};
231 static const WCHAR nonexisting_resourceW[] = {'N','o','n','e','x','i','s','t','i','n','g',0};
232 static const WCHAR prefixW[] = {'I','C','O',0};
233 HRESULT (WINAPI *pLoadIconMetric)(HINSTANCE, const WCHAR *, int, HICON *);
234 HRESULT (WINAPI *pLoadIconWithScaleDown)(HINSTANCE, const WCHAR *, int, int, HICON *);
235 WCHAR tmp_path[MAX_PATH], icon_path[MAX_PATH];
236 ICONINFO info;
237 HMODULE hinst;
238 HANDLE handle;
239 DWORD written;
240 HRESULT hr;
241 BITMAP bmp;
242 HICON icon;
243 void *ptr;
244 int bytes;
245 BOOL res;
246
247 hinst = LoadLibraryA("comctl32.dll");
248 pLoadIconMetric = (void *)GetProcAddress(hinst, "LoadIconMetric");
249 pLoadIconWithScaleDown = (void *)GetProcAddress(hinst, "LoadIconWithScaleDown");
250 if (!pLoadIconMetric || !pLoadIconWithScaleDown)
251 {
252 #ifdef __REACTOS__
253 skip("LoadIconMetric or pLoadIconWithScaleDown not exported by name\n");
254 #else
255 win_skip("LoadIconMetric or pLoadIconWithScaleDown not exported by name\n");
256 #endif
257 FreeLibrary(hinst);
258 return;
259 }
260
261 GetTempPathW(MAX_PATH, tmp_path);
262 GetTempFileNameW(tmp_path, prefixW, 0, icon_path);
263 handle = CreateFileW(icon_path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
264 FILE_ATTRIBUTE_NORMAL, NULL);
265 ok(handle != INVALID_HANDLE_VALUE, "CreateFileW failed with error %u\n", GetLastError());
266 res = WriteFile(handle, testicon_data, sizeof(testicon_data), &written, NULL);
267 ok(res && written == sizeof(testicon_data), "Failed to write icon file\n");
268 CloseHandle(handle);
269
270 /* test ordinals */
271 ptr = GetProcAddress(hinst, (const char *)380);
272 ok(ptr == pLoadIconMetric,
273 "got wrong pointer for ordinal 380, %p expected %p\n", ptr, pLoadIconMetric);
274
275 ptr = GetProcAddress(hinst, (const char *)381);
276 ok(ptr == pLoadIconWithScaleDown,
277 "got wrong pointer for ordinal 381, %p expected %p\n", ptr, pLoadIconWithScaleDown);
278
279 /* invalid arguments */
280 icon = (HICON)0x1234;
281 hr = pLoadIconMetric(NULL, (LPWSTR)IDI_APPLICATION, 0x100, &icon);
282 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %x\n", hr);
283 ok(icon == NULL, "Expected NULL, got %p\n", icon);
284
285 icon = (HICON)0x1234;
286 hr = pLoadIconMetric(NULL, NULL, LIM_LARGE, &icon);
287 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %x\n", hr);
288 ok(icon == NULL, "Expected NULL, got %p\n", icon);
289
290 icon = (HICON)0x1234;
291 hr = pLoadIconWithScaleDown(NULL, NULL, 32, 32, &icon);
292 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %x\n", hr);
293 ok(icon == NULL, "Expected NULL, got %p\n", icon);
294
295 /* non-existing filename */
296 hr = pLoadIconMetric(NULL, nonexisting_fileW, LIM_LARGE, &icon);
297 todo_wine
298 ok(hr == HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND),
299 "Expected HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND), got %x\n", hr);
300
301 hr = pLoadIconWithScaleDown(NULL, nonexisting_fileW, 32, 32, &icon);
302 todo_wine
303 ok(hr == HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND),
304 "Expected HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND), got %x\n", hr);
305
306 /* non-existing resource name */
307 hr = pLoadIconMetric(hinst, nonexisting_resourceW, LIM_LARGE, &icon);
308 ok(hr == HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND),
309 "Expected HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND), got %x\n", hr);
310
311 hr = pLoadIconWithScaleDown(hinst, nonexisting_resourceW, 32, 32, &icon);
312 ok(hr == HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND),
313 "Expected HRESULT_FROM_WIN32(ERROR_RESOURCE_TYPE_NOT_FOUND), got %x\n", hr);
314
315 /* load icon using predefined identifier */
316 hr = pLoadIconMetric(NULL, (LPWSTR)IDI_APPLICATION, LIM_SMALL, &icon);
317 ok(hr == S_OK, "Expected S_OK, got %x\n", hr);
318 res = GetIconInfo(icon, &info);
319 ok(res, "Failed to get icon info, error %u\n", GetLastError());
320 bytes = GetObjectA(info.hbmColor, sizeof(bmp), &bmp);
321 ok(bytes > 0, "Failed to get bitmap info for icon\n");
322 ok(bmp.bmWidth == GetSystemMetrics(SM_CXSMICON), "Wrong icon width %d\n", bmp.bmWidth);
323 ok(bmp.bmHeight == GetSystemMetrics(SM_CYSMICON), "Wrong icon height %d\n", bmp.bmHeight);
324 DestroyIcon(icon);
325
326 hr = pLoadIconMetric(NULL, (LPWSTR)IDI_APPLICATION, LIM_LARGE, &icon);
327 ok(hr == S_OK, "Expected S_OK, got %x\n", hr);
328 res = GetIconInfo(icon, &info);
329 ok(res, "Failed to get icon info, error %u\n", GetLastError());
330 bytes = GetObjectA(info.hbmColor, sizeof(bmp), &bmp);
331 ok(bytes > 0, "Failed to get bitmap info for icon\n");
332 ok(bmp.bmWidth == GetSystemMetrics(SM_CXICON), "Wrong icon width %d\n", bmp.bmWidth);
333 ok(bmp.bmHeight == GetSystemMetrics(SM_CYICON), "Wrong icon height %d\n", bmp.bmHeight);
334 DestroyIcon(icon);
335
336 hr = pLoadIconWithScaleDown(NULL, (LPWSTR)IDI_APPLICATION, 42, 42, &icon);
337 ok(hr == S_OK, "Expected S_OK, got %x\n", hr);
338 res = GetIconInfo(icon, &info);
339 ok(res, "Failed to get icon info, error %u\n", GetLastError());
340 bytes = GetObjectA(info.hbmColor, sizeof(bmp), &bmp);
341 ok(bytes > 0, "Failed to get bitmap info for icon\n");
342 ok(bmp.bmWidth == 42, "Wrong icon width %d\n", bmp.bmWidth);
343 ok(bmp.bmHeight == 42, "Wrong icon height %d\n", bmp.bmHeight);
344 DestroyIcon(icon);
345
346 /* load icon from file */
347 hr = pLoadIconMetric(NULL, icon_path, LIM_SMALL, &icon);
348 ok(hr == S_OK, "Expected S_OK, got %x\n", hr);
349 res = GetIconInfo(icon, &info);
350 ok(res, "Failed to get icon info, error %u\n", GetLastError());
351 bytes = GetObjectA(info.hbmColor, sizeof(bmp), &bmp);
352 ok(bytes > 0, "Failed to get bitmap info for icon\n");
353 ok(bmp.bmWidth == GetSystemMetrics(SM_CXSMICON), "Wrong icon width %d\n", bmp.bmWidth);
354 ok(bmp.bmHeight == GetSystemMetrics(SM_CYSMICON), "Wrong icon height %d\n", bmp.bmHeight);
355 DestroyIcon(icon);
356
357 hr = pLoadIconWithScaleDown(NULL, icon_path, 42, 42, &icon);
358 ok(hr == S_OK, "Expected S_OK, got %x\n", hr);
359 res = GetIconInfo(icon, &info);
360 ok(res, "Failed to get icon info, error %u\n", GetLastError());
361 bytes = GetObjectA(info.hbmColor, sizeof(bmp), &bmp);
362 ok(bytes > 0, "Failed to get bitmap info for icon\n");
363 ok(bmp.bmWidth == 42, "Wrong icon width %d\n", bmp.bmWidth);
364 ok(bmp.bmHeight == 42, "Wrong icon height %d\n", bmp.bmHeight);
365 DestroyIcon(icon);
366
367 DeleteFileW(icon_path);
368 FreeLibrary(hinst);
369 }
370
371 static void check_class( const char *name, int must_exist, UINT style, UINT ignore )
372 {
373 WNDCLASSA wc;
374
375 if (GetClassInfoA( 0, name, &wc ))
376 {
377 todo_wine
378 ok( !(~wc.style & style & ~ignore), "System class %s is missing bits %x (%08x/%08x)\n",
379 name, ~wc.style & style, wc.style, style );
380 ok( !(wc.style & ~style), "System class %s has extra bits %x (%08x/%08x)\n",
381 name, wc.style & ~style, wc.style, style );
382 ok( !wc.hInstance, "System class %s has hInstance %p\n", name, wc.hInstance );
383 }
384 else
385 ok( !must_exist, "System class %s does not exist\n", name );
386 }
387
388 /* test styles of system classes */
389 static void test_builtin_classes(void)
390 {
391 /* check style bits */
392 check_class( "Button", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS, 0 );
393 check_class( "ComboBox", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS, 0 );
394 check_class( "Edit", 1, CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS, 0 );
395 check_class( "ListBox", 1, CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS, 0 );
396 check_class( "ScrollBar", 1, CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS, 0 );
397 check_class( "Static", 1, CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS, 0 );
398 check_class( "ComboLBox", 1, CS_SAVEBITS | CS_DBLCLKS | CS_DROPSHADOW | CS_GLOBALCLASS, CS_DROPSHADOW );
399 }
400
401 START_TEST(misc)
402 {
403 ULONG_PTR ctx_cookie;
404 HANDLE hCtx;
405
406 if(!InitFunctionPtrs())
407 return;
408
409 test_GetPtrAW();
410 test_Alloc();
411
412 if (!load_v6_module(&ctx_cookie, &hCtx))
413 return;
414
415 test_builtin_classes();
416 test_TaskDialogIndirect();
417 test_LoadIconWithScaleDown();
418
419 unload_v6_module(ctx_cookie, hCtx);
420 }