1 /* Unit test suite for resources.
3 * Copyright 2004 Ferenc Wagner
4 * Copyright 2003, 2004 Mike McCormack
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/test.h"
26 static UINT (WINAPI *pPrivateExtractIconsA)(LPCTSTR, int, int, int, HICON *, UINT *, UINT, UINT) = NULL;
28 static void init_function_pointers(void)
30 HMODULE hmod = GetModuleHandleA("user32.dll");
31 pPrivateExtractIconsA = (void*)GetProcAddress(hmod, "PrivateExtractIconsA");
34 static void test_LoadStringW(void)
36 HINSTANCE hInst = GetModuleHandle(NULL);
37 WCHAR copiedstringw[128], returnedstringw[128], *resourcepointer = NULL;
38 char copiedstring[128], returnedstring[128];
39 int length1, length2, retvalue;
41 /* Check that the string which is returned by LoadStringW matches
42 the string at the pointer returned by LoadStringW when called with buflen = 0 */
43 length1 = LoadStringW(hInst, 2, (WCHAR *) &resourcepointer, 0); /* get pointer to resource. */
44 length2 = LoadStringW(hInst, 2, returnedstringw, sizeof(returnedstringw) /sizeof(WCHAR)); /* get resource string */
45 ok(length2 > 0, "LoadStringW failed to load resource 2, ret %d, err %d\n", length2, GetLastError());
46 ok(length1 == length2, "LoadStringW returned different values dependent on buflen. ret1 %d, ret2 %d\n",
48 ok(length1 > 0 && resourcepointer != NULL, "LoadStringW failed to get pointer to resource 2, ret %d, err %d\n",
49 length1, GetLastError());
51 /* Copy the resource since it is not '\0' terminated, and add '\0' to the end */
52 if(resourcepointer != NULL) /* Check that the resource pointer was loaded to avoid access violation */
54 memcpy(copiedstringw, resourcepointer, length1 * sizeof(WCHAR));
55 copiedstringw[length1] = '\0';
56 /* check that strings match */
57 WideCharToMultiByte( CP_ACP, 0, returnedstringw, -1, returnedstring, 128, NULL, NULL );
58 WideCharToMultiByte( CP_ACP, 0, copiedstringw, -1, copiedstring, 128, NULL, NULL );
59 ok(!memcmp(copiedstringw, returnedstringw, (length2 + 1)*sizeof(WCHAR)),
60 "strings don't match: returnedstring = %s, copiedstring = %s\n", returnedstring, copiedstring);
63 /* check that calling LoadStringW with buffer = NULL returns zero */
64 retvalue = LoadStringW(hInst, 2, NULL, 0);
65 ok(!retvalue, "LoadStringW returned a non-zero value when called with buffer = NULL, retvalue = %d\n", retvalue);
66 /* check again, with a different buflen value, that calling LoadStringW with buffer = NULL returns zero */
67 retvalue = LoadStringW(hInst, 2, NULL, 128);
68 ok(!retvalue, "LoadStringW returned a non-zero value when called with buffer = NULL, retvalue = %d\n", retvalue);
71 static void test_LoadStringA (void)
73 HINSTANCE hInst = GetModuleHandle (NULL);
74 static const char str[] = "String resource"; /* same in resource.rc */
78 unsigned int expected;
80 struct string_test tests[] = {{sizeof buf, sizeof str - 1},
81 {sizeof str, sizeof str - 1},
82 {sizeof str - 1, sizeof str - 2}};
86 assert (sizeof str < sizeof buf);
87 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
88 const unsigned int bufsiz = tests[i].bufsiz;
89 const unsigned int expected = tests[i].expected;
90 const int len = LoadStringA (hInst, 0, buf, bufsiz);
92 ok (len == expected, "bufsiz=%d: got %d, expected %d\n",
93 bufsiz, len, expected);
94 if (len != expected) continue;
95 ok (!memcmp (buf, str, len),
96 "bufsiz=%d: got '%s', expected '%.*s'\n",
97 bufsiz, buf, len, str);
98 ok (buf[len] == 0, "bufsiz=%d: NUL termination missing\n",
102 ret = LoadStringA(hInst, 1, buf, sizeof(buf) );
103 ok( ret > 0, "LoadString failed: ret %d err %d\n", ret, GetLastError());
104 ok( LoadStringA( hInst, MAKELONG( 1, 0x8000 ), buf, sizeof(buf)) == ret,
105 "LoadString failed: ret %d err %d\n", ret, GetLastError());
106 ok( LoadStringA( hInst, MAKELONG( 1, 0xffff ), buf, sizeof(buf)) == ret,
107 "LoadString failed: ret %d err %d\n", ret, GetLastError());
109 ret = LoadStringA(hInst, 65534, buf, sizeof(buf) );
110 ok( ret > 0, "LoadString failed: ret %d err %d\n", ret, GetLastError());
111 ok( LoadStringA( hInst, MAKELONG( 65534, 0x8000 ), buf, sizeof(buf)) == ret,
112 "LoadString failed: ret %d err %d\n", ret, GetLastError());
113 ok( LoadStringA( hInst, MAKELONG( 65534, 0xffff ), buf, sizeof(buf)) == ret,
114 "LoadString failed: ret %d err %d\n", ret, GetLastError());
116 ret = LoadStringA(hInst, 0, buf, 0);
117 ok( ret == -1, "LoadStringA did not return -1 when called with buflen = 0, got %d, err %d\n",
118 ret, GetLastError());
121 static void test_accel1(void)
127 /* now create our own valid accelerator table */
131 ac[n++].fVirt = FVIRTKEY | FNOINVERT;
135 ac[n++].fVirt = FNOINVERT;
141 hAccel = CreateAcceleratorTable( &ac[0], n );
142 ok( hAccel != NULL, "create accelerator table\n");
144 r = DestroyAcceleratorTable( hAccel );
145 ok( r, "destroy accelerator table\n");
147 /* now try create an invalid one */
151 ac[n++].fVirt = FVIRTKEY | FNOINVERT;
155 ac[n++].fVirt = (SHORT) 0xffff;
159 ac[n++].fVirt = (SHORT) 0xfff0;
163 ac[n++].fVirt = (SHORT) 0x0000;
167 ac[n++].fVirt = (SHORT) 0x0001;
169 hAccel = CreateAcceleratorTable( &ac[0], n );
170 ok( hAccel != NULL, "create accelerator table\n");
172 r = CopyAcceleratorTable( hAccel, NULL, 0 );
173 ok( r == n, "two entries in table\n");
175 r = CopyAcceleratorTable( hAccel, &ac[0], r );
176 ok( r == n, "still should be two entries in table\n");
179 ok( ac[n].cmd == 1000, "cmd 0 not preserved\n");
180 ok( ac[n].key == 'A', "key 0 not preserved\n");
181 ok( ac[n].fVirt == (FVIRTKEY | FNOINVERT), "fVirt 0 not preserved\n");
184 ok( ac[n].cmd == 0xffff, "cmd 1 not preserved\n");
185 ok( ac[n].key == 0xffff, "key 1 not preserved\n");
186 ok( ac[n].fVirt == 0x007f, "fVirt 1 not changed\n");
189 ok( ac[n].cmd == 0xfff0, "cmd 2 not preserved\n");
190 ok( ac[n].key == 0x00ff, "key 2 not preserved\n");
191 ok( ac[n].fVirt == 0x0070, "fVirt 2 not changed\n");
194 ok( ac[n].cmd == 0xfff0, "cmd 3 not preserved\n");
195 ok( ac[n].key == 0x00ff, "key 3 not preserved\n");
196 ok( ac[n].fVirt == 0x0000, "fVirt 3 not changed\n");
199 ok( ac[n].cmd == 0xfff0, "cmd 4 not preserved\n");
200 ok( ac[n].key == 0xffff, "key 4 not preserved\n");
201 ok( ac[n].fVirt == 0x0001, "fVirt 4 not changed\n");
203 r = DestroyAcceleratorTable( hAccel );
204 ok( r, "destroy accelerator table\n");
206 hAccel = CreateAcceleratorTable( &ac[0], 0 );
207 ok( !hAccel, "zero elements should fail\n");
209 /* these will on crash win2k
210 hAccel = CreateAcceleratorTable( NULL, 1 );
211 hAccel = CreateAcceleratorTable( &ac[0], -1 );
216 * memcmp on the tables works in Windows, but does not work in wine, as
217 * there is an extra undefined and unused byte between fVirt and the key
219 static void test_accel2(void)
234 * hac = CreateAcceleratorTable( NULL, 1 );
237 /* try a zero count */
238 hac = CreateAcceleratorTable( &ac[0], 0 );
239 ok( !hac , "fail\n");
240 ok( !DestroyAcceleratorTable( hac ), "destroy failed\n");
242 /* creating one accelerator should work */
243 hac = CreateAcceleratorTable( &ac[0], 1 );
244 ok( hac != NULL , "fail\n");
245 ok( 1 == CopyAcceleratorTable( hac, out, 1 ), "copy failed\n");
246 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
248 /* how about two of the same type? */
249 hac = CreateAcceleratorTable( &ac[0], 2);
250 ok( hac != NULL , "fail\n");
251 ok( 2 == CopyAcceleratorTable( hac, NULL, 100 ), "copy null failed\n");
252 ok( 2 == CopyAcceleratorTable( hac, NULL, 0 ), "copy null failed\n");
253 ok( 2 == CopyAcceleratorTable( hac, NULL, 1 ), "copy null failed\n");
254 ok( 1 == CopyAcceleratorTable( hac, out, 1 ), "copy 1 failed\n");
255 ok( 2 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
256 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
257 /* ok( !memcmp( ac, out, sizeof ac ), "tables different\n"); */
259 /* how about two of the same type with a non-zero key? */
262 hac = CreateAcceleratorTable( &ac[0], 2);
263 ok( hac != NULL , "fail\n");
264 ok( 2 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
265 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
266 /* ok( !memcmp( ac, out, sizeof ac ), "tables different\n"); */
268 /* how about two of the same type with a non-zero virtual key? */
269 ac[0].fVirt = FVIRTKEY;
271 ac[1].fVirt = FVIRTKEY;
273 hac = CreateAcceleratorTable( &ac[0], 2);
274 ok( hac != NULL , "fail\n");
275 ok( 2 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
276 /* ok( !memcmp( ac, out, sizeof ac ), "tables different\n"); */
277 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
279 /* how virtual key codes */
280 ac[0].fVirt = FVIRTKEY;
281 hac = CreateAcceleratorTable( &ac[0], 1);
282 ok( hac != NULL , "fail\n");
283 ok( 1 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
284 /* ok( !memcmp( ac, out, sizeof ac/2 ), "tables different\n"); */
285 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
287 /* how turning on all bits? */
291 hac = CreateAcceleratorTable( &ac[0], 1);
292 ok( hac != NULL , "fail\n");
293 ok( 1 == CopyAcceleratorTable( hac, out, 1 ), "copy 1 failed\n");
294 /* ok( memcmp( ac, out, sizeof ac/2 ), "tables not different\n"); */
295 ok( out[0].cmd == ac[0].cmd, "cmd modified\n");
296 ok( out[0].fVirt == (ac[0].fVirt&0x7f), "fVirt not modified\n");
297 ok( out[0].key == ac[0].key, "key modified\n");
298 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
300 /* how turning on all bits? */
301 memset( ac, 0xff, sizeof ac );
302 hac = CreateAcceleratorTable( &ac[0], 2);
303 ok( hac != NULL , "fail\n");
304 ok( 2 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
305 /* ok( memcmp( ac, out, sizeof ac ), "tables not different\n"); */
306 ok( out[0].cmd == ac[0].cmd, "cmd modified\n");
307 ok( out[0].fVirt == (ac[0].fVirt&0x7f), "fVirt not modified\n");
308 ok( out[0].key == ac[0].key, "key modified\n");
309 ok( out[1].cmd == ac[1].cmd, "cmd modified\n");
310 ok( out[1].fVirt == (ac[1].fVirt&0x7f), "fVirt not modified\n");
311 ok( out[1].key == ac[1].key, "key modified\n");
312 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
315 static void test_PrivateExtractIcons(void) {
316 CONST CHAR szShell32Dll[] = "shell32.dll";
319 UINT cIcons, cIcons2;
321 if (!pPrivateExtractIconsA) return;
323 cIcons = pPrivateExtractIconsA(szShell32Dll, 0, 16, 16, NULL, NULL, 0, 0);
324 cIcons2 = pPrivateExtractIconsA(szShell32Dll, 4, MAKELONG(32,16), MAKELONG(32,16),
326 ok((cIcons == cIcons2) && (cIcons > 0),
327 "Icon count should be independent of requested icon sizes and base icon index! "
328 "(cIcons=%d, cIcons2=%d)\n", cIcons, cIcons2);
330 cIcons = pPrivateExtractIconsA(szShell32Dll, 0, 16, 16, ahIcon, aIconId, 0, 0);
331 ok(cIcons == 0, "Zero icons requested, got cIcons=%d\n", cIcons);
333 cIcons = pPrivateExtractIconsA(szShell32Dll, 0, 16, 16, ahIcon, aIconId, 3, 0);
334 ok(cIcons == 3, "Three icons requested got cIcons=%d\n", cIcons);
336 cIcons = pPrivateExtractIconsA(szShell32Dll, 0, MAKELONG(32,16), MAKELONG(32,16),
337 ahIcon, aIconId, 3, 0);
338 ok(cIcons == 4, "Three icons requested, four expected, got cIcons=%d\n", cIcons);
341 static void test_LoadImage(void)
346 bmp = LoadBitmapA(GetModuleHandle(NULL), MAKEINTRESOURCE(100));
347 ok(bmp != NULL, "Could not load a bitmap resource\n");
348 if (bmp) DeleteObject(bmp);
350 hres = FindResource(GetModuleHandle(NULL), "#100", RT_BITMAP);
351 ok(hres != NULL, "Could not find a bitmap resource with a numeric string\n");
353 bmp = LoadBitmapA(GetModuleHandle(NULL), "#100");
354 ok(bmp != NULL, "Could not load a bitmap resource with a numeric string\n");
355 if (bmp) DeleteObject(bmp);
360 init_function_pointers();
365 test_PrivateExtractIcons();