9cb738076414c1adc98f6842d3873b7ba6425156
[reactos.git] / rostests / winetests / user32 / resource.c
1 /* Unit test suite for resources.
2 *
3 * Copyright 2004 Ferenc Wagner
4 * Copyright 2003, 2004 Mike McCormack
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 <assert.h>
22 #include <windows.h>
23
24 #include "wine/test.h"
25
26 static UINT (WINAPI *pPrivateExtractIconsA)(LPCSTR, int, int, int, HICON *, UINT *, UINT, UINT) = NULL;
27
28 static void init_function_pointers(void)
29 {
30 HMODULE hmod = GetModuleHandleA("user32.dll");
31 pPrivateExtractIconsA = (void*)GetProcAddress(hmod, "PrivateExtractIconsA");
32 }
33
34 static void test_LoadStringW(void)
35 {
36 HINSTANCE hInst = GetModuleHandleA(NULL);
37 WCHAR copiedstringw[128], returnedstringw[128], *resourcepointer = NULL;
38 char copiedstring[128], returnedstring[128];
39 int length1, length2, retvalue;
40
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 SetLastError(0xdeadbeef);
44 length1 = LoadStringW(hInst, 2, (WCHAR *) &resourcepointer, 0); /* get pointer to resource. */
45 if (!length1)
46 {
47 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
48 win_skip( "LoadStringW not implemented\n" );
49 else
50 win_skip( "LoadStringW does not return a pointer to the resource\n" );
51 return;
52 }
53 length2 = LoadStringW(hInst, 2, returnedstringw, sizeof(returnedstringw) /sizeof(WCHAR)); /* get resource string */
54 ok(length2 > 0, "LoadStringW failed to load resource 2, ret %d, err %d\n", length2, GetLastError());
55 ok(length1 == length2, "LoadStringW returned different values dependent on buflen. ret1 %d, ret2 %d\n",
56 length1, length2);
57 ok(length1 > 0 && resourcepointer != NULL, "LoadStringW failed to get pointer to resource 2, ret %d, err %d\n",
58 length1, GetLastError());
59
60 /* Copy the resource since it is not '\0' terminated, and add '\0' to the end */
61 if(resourcepointer != NULL) /* Check that the resource pointer was loaded to avoid access violation */
62 {
63 memcpy(copiedstringw, resourcepointer, length1 * sizeof(WCHAR));
64 copiedstringw[length1] = '\0';
65 /* check that strings match */
66 WideCharToMultiByte( CP_ACP, 0, returnedstringw, -1, returnedstring, 128, NULL, NULL );
67 WideCharToMultiByte( CP_ACP, 0, copiedstringw, -1, copiedstring, 128, NULL, NULL );
68 ok(!memcmp(copiedstringw, returnedstringw, (length2 + 1)*sizeof(WCHAR)),
69 "strings don't match: returnedstring = %s, copiedstring = %s\n", returnedstring, copiedstring);
70 }
71
72 /* check that calling LoadStringW with buffer = NULL returns zero */
73 retvalue = LoadStringW(hInst, 2, NULL, 0);
74 ok(!retvalue, "LoadStringW returned a non-zero value when called with buffer = NULL, retvalue = %d\n", retvalue);
75 /* check again, with a different buflen value, that calling LoadStringW with buffer = NULL returns zero */
76 retvalue = LoadStringW(hInst, 2, NULL, 128);
77 ok(!retvalue, "LoadStringW returned a non-zero value when called with buffer = NULL, retvalue = %d\n", retvalue);
78 }
79
80 static void test_LoadStringA (void)
81 {
82 HINSTANCE hInst = GetModuleHandleA(NULL);
83 static const char str[] = "String resource"; /* same in resource.rc */
84 char buf[128];
85 struct string_test {
86 unsigned int bufsiz;
87 unsigned int expected;
88 };
89 struct string_test tests[] = {{sizeof buf, sizeof str - 1},
90 {sizeof str, sizeof str - 1},
91 {sizeof str - 1, sizeof str - 2}};
92 unsigned int i;
93 int ret, ret2;
94
95 assert (sizeof str < sizeof buf);
96 for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
97 const unsigned int bufsiz = tests[i].bufsiz;
98 const unsigned int expected = tests[i].expected;
99 const int len = LoadStringA (hInst, 0, buf, bufsiz);
100
101 ok (len == expected, "bufsiz=%d: got %d, expected %d\n",
102 bufsiz, len, expected);
103 if (len != expected) continue;
104 ok (!memcmp (buf, str, len),
105 "bufsiz=%d: got '%s', expected '%.*s'\n",
106 bufsiz, buf, len, str);
107 ok (buf[len] == 0, "bufsiz=%d: NUL termination missing\n",
108 bufsiz);
109 }
110
111 ret = LoadStringA(hInst, 1, buf, sizeof(buf) );
112 ok( ret > 0, "LoadString failed: ret %d err %d\n", ret, GetLastError());
113 ret2 = LoadStringA( hInst, MAKELONG( 1, 0x8000 ), buf, sizeof(buf));
114 ok( ret2 == ret, "LoadString failed: ret %d err %d\n", ret, GetLastError());
115 ret2 = LoadStringA( hInst, MAKELONG( 1, 0xffff ), buf, sizeof(buf));
116 ok( ret2 == ret, "LoadString failed: ret %d err %d\n", ret, GetLastError());
117
118 ret = LoadStringA(hInst, 65534, buf, sizeof(buf) );
119 ok( ret > 0, "LoadString failed: ret %d err %d\n", ret, GetLastError());
120 ret2 = LoadStringA( hInst, MAKELONG( 65534, 0x8000 ), buf, sizeof(buf));
121 ok( ret2 == ret, "LoadString failed: ret %d err %d\n", ret, GetLastError());
122 ret2 = LoadStringA( hInst, MAKELONG( 65534, 0xffff ), buf, sizeof(buf));
123 ok( ret2 == ret, "LoadString failed: ret %d err %d\n", ret, GetLastError());
124
125 ret = LoadStringA(hInst, 0, buf, 0);
126 ok( ret == -1 || broken(ret == 0),
127 "LoadStringA did not return -1 when called with buflen = 0, got %d, err %d\n",
128 ret, GetLastError());
129 }
130
131 static void test_accel1(void)
132 {
133 UINT r, n;
134 HACCEL hAccel;
135 ACCEL ac[10];
136
137 /* now create our own valid accelerator table */
138 n = 0;
139 ac[n].cmd = 1000;
140 ac[n].key = 'A';
141 ac[n++].fVirt = FVIRTKEY | FNOINVERT;
142
143 ac[n].cmd = 1001;
144 ac[n].key = 'B';
145 ac[n++].fVirt = FNOINVERT;
146
147 ac[n].cmd = 0;
148 ac[n].key = 0;
149 ac[n++].fVirt = 0;
150
151 hAccel = CreateAcceleratorTableA( &ac[0], n );
152 ok( hAccel != NULL, "create accelerator table\n");
153
154 r = DestroyAcceleratorTable( hAccel );
155 ok( r, "destroy accelerator table\n");
156
157 /* now try create an invalid one */
158 n = 0;
159 ac[n].cmd = 1000;
160 ac[n].key = 'A';
161 ac[n++].fVirt = FVIRTKEY | FNOINVERT;
162
163 ac[n].cmd = 0xffff;
164 ac[n].key = 0xffff;
165 ac[n++].fVirt = (SHORT) 0xffff;
166
167 ac[n].cmd = 0xfff0;
168 ac[n].key = 0xffff;
169 ac[n++].fVirt = (SHORT) 0xfff0;
170
171 ac[n].cmd = 0xfff0;
172 ac[n].key = 0xffff;
173 ac[n++].fVirt = 0x0000;
174
175 ac[n].cmd = 0xfff0;
176 ac[n].key = 0xffff;
177 ac[n++].fVirt = 0x0001;
178
179 hAccel = CreateAcceleratorTableA( &ac[0], n );
180 ok( hAccel != NULL, "create accelerator table\n");
181
182 r = CopyAcceleratorTableA( hAccel, NULL, 0 );
183 ok( r == n, "two entries in table %u/%u\n", r, n);
184
185 r = CopyAcceleratorTableA( hAccel, &ac[0], n );
186 ok( r == n, "still should be two entries in table %u/%u\n", r, n);
187
188 n=0;
189 ok( ac[n].cmd == 1000, "cmd 0 not preserved got %x\n", ac[n].cmd);
190 ok( ac[n].key == 'A', "key 0 not preserved got %x\n", ac[n].key);
191 ok( ac[n].fVirt == (FVIRTKEY | FNOINVERT), "fVirt 0 not preserved got %x\n", ac[n].fVirt);
192
193 if (++n == r) goto done;
194 ok( ac[n].cmd == 0xffff, "cmd 1 not preserved got %x\n", ac[n].cmd);
195 ok( ac[n].key == 0xffff, "key 1 not preserved got %x\n", ac[n].key);
196 ok( ac[n].fVirt == 0x007f, "fVirt 1 wrong got %x\n", ac[n].fVirt);
197
198 if (++n == r) goto done;
199 ok( ac[n].cmd == 0xfff0, "cmd 2 not preserved got %x\n", ac[n].cmd);
200 ok( (ac[n].key & 0xff) == 0xff, "key 2 not preserved got %x\n", ac[n].key);
201 ok( ac[n].fVirt == 0x0070, "fVirt 2 wrong got %x\n", ac[n].fVirt);
202
203 if (++n == r) goto done;
204 ok( ac[n].cmd == 0xfff0, "cmd 3 not preserved got %x\n", ac[n].cmd);
205 ok( (ac[n].key & 0xff) == 0xff, "key 3 not preserved got %x\n", ac[n].key);
206 ok( ac[n].fVirt == 0x0000, "fVirt 3 wrong got %x\n", ac[n].fVirt);
207
208 if (++n == r) goto done;
209 ok( ac[n].cmd == 0xfff0, "cmd 4 not preserved got %x\n", ac[n].cmd);
210 ok( ac[n].key == 0xffff, "key 4 not preserved got %x\n", ac[n].key);
211 ok( ac[n].fVirt == 0x0001, "fVirt 4 wrong got %x\n", ac[n].fVirt);
212 done:
213 r = DestroyAcceleratorTable( hAccel );
214 ok( r, "destroy accelerator table\n");
215
216 hAccel = CreateAcceleratorTableA( &ac[0], 0 );
217 ok( !hAccel || broken(hAccel != NULL), /* nt4 */ "zero elements should fail\n");
218
219 /* these will on crash win2k
220 hAccel = CreateAcceleratorTable( NULL, 1 );
221 hAccel = CreateAcceleratorTable( &ac[0], -1 );
222 */
223 }
224
225 /*
226 * memcmp on the tables works in Windows, but does not work in wine, as
227 * there is an extra undefined and unused byte between fVirt and the key
228 */
229 static void test_accel2(void)
230 {
231 ACCEL ac[2], out[2];
232 HACCEL hac;
233 int res;
234
235 ac[0].cmd = 0;
236 ac[0].fVirt = 0;
237 ac[0].key = 0;
238
239 ac[1].cmd = 0;
240 ac[1].fVirt = 0;
241 ac[1].key = 0;
242
243 /*
244 * crashes on win2k
245 * hac = CreateAcceleratorTable( NULL, 1 );
246 */
247
248 /* try a zero count */
249 hac = CreateAcceleratorTableA( &ac[0], 0 );
250 ok( !hac || broken(hac != NULL), /* nt4 */ "fail\n");
251 if (!hac) ok( !DestroyAcceleratorTable( hac ), "destroy failed\n");
252
253 /* creating one accelerator should work */
254 hac = CreateAcceleratorTableA( &ac[0], 1 );
255 ok( hac != NULL , "fail\n");
256 ok( 1 == CopyAcceleratorTableA( hac, out, 1 ), "copy failed\n");
257 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
258
259 /* how about two of the same type? */
260 hac = CreateAcceleratorTableA( &ac[0], 2);
261 ok( hac != NULL , "fail\n");
262 res = CopyAcceleratorTableA( hac, NULL, 100 );
263 ok( res == 2, "copy null failed %d\n", res);
264 res = CopyAcceleratorTableA( hac, NULL, 0 );
265 ok( res == 2, "copy null failed %d\n", res);
266 res = CopyAcceleratorTableA( hac, NULL, 1 );
267 ok( res == 2, "copy null failed %d\n", res);
268 ok( 1 == CopyAcceleratorTableA( hac, out, 1 ), "copy 1 failed\n");
269 ok( 2 == CopyAcceleratorTableA( hac, out, 2 ), "copy 2 failed\n");
270 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
271 /* ok( !memcmp( ac, out, sizeof ac ), "tables different\n"); */
272
273 /* how about two of the same type with a non-zero key? */
274 ac[0].key = 0x20;
275 ac[1].key = 0x20;
276 hac = CreateAcceleratorTableA( &ac[0], 2);
277 ok( hac != NULL , "fail\n");
278 ok( 2 == CopyAcceleratorTableA( hac, out, 2 ), "copy 2 failed\n");
279 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
280 /* ok( !memcmp( ac, out, sizeof ac ), "tables different\n"); */
281
282 /* how about two of the same type with a non-zero virtual key? */
283 ac[0].fVirt = FVIRTKEY;
284 ac[0].key = 0x40;
285 ac[1].fVirt = FVIRTKEY;
286 ac[1].key = 0x40;
287 hac = CreateAcceleratorTableA( &ac[0], 2);
288 ok( hac != NULL , "fail\n");
289 ok( 2 == CopyAcceleratorTableA( hac, out, 2 ), "copy 2 failed\n");
290 /* ok( !memcmp( ac, out, sizeof ac ), "tables different\n"); */
291 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
292
293 /* how virtual key codes */
294 ac[0].fVirt = FVIRTKEY;
295 hac = CreateAcceleratorTableA( &ac[0], 1);
296 ok( hac != NULL , "fail\n");
297 ok( 1 == CopyAcceleratorTableA( hac, out, 2 ), "copy 2 failed\n");
298 /* ok( !memcmp( ac, out, sizeof ac/2 ), "tables different\n"); */
299 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
300
301 /* how turning on all bits? */
302 ac[0].cmd = 0xffff;
303 ac[0].fVirt = 0xff;
304 ac[0].key = 0xffff;
305 hac = CreateAcceleratorTableA( &ac[0], 1);
306 ok( hac != NULL , "fail\n");
307 ok( 1 == CopyAcceleratorTableA( hac, out, 1 ), "copy 1 failed\n");
308 /* ok( memcmp( ac, out, sizeof ac/2 ), "tables not different\n"); */
309 ok( out[0].cmd == ac[0].cmd, "cmd modified\n");
310 ok( out[0].fVirt == (ac[0].fVirt&0x7f), "fVirt not modified\n");
311 ok( out[0].key == ac[0].key, "key modified\n");
312 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
313
314 /* how turning on all bits? */
315 memset( ac, 0xff, sizeof ac );
316 hac = CreateAcceleratorTableA( &ac[0], 2);
317 ok( hac != NULL , "fail\n");
318 res = CopyAcceleratorTableA( hac, out, 2 );
319 ok( res == 2, "copy 2 failed %d\n", res);
320 /* ok( memcmp( ac, out, sizeof ac ), "tables not different\n"); */
321 ok( out[0].cmd == ac[0].cmd, "cmd modified\n");
322 ok( out[0].fVirt == (ac[0].fVirt&0x7f), "fVirt not modified\n");
323 ok( out[0].key == ac[0].key, "key modified\n");
324 if (res == 2)
325 {
326 ok( out[1].cmd == ac[1].cmd, "cmd modified\n");
327 ok( out[1].fVirt == (ac[1].fVirt&0x7f), "fVirt not modified\n");
328 ok( out[1].key == ac[1].key, "key modified\n");
329 }
330 ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
331 }
332
333 static void test_PrivateExtractIcons(void) {
334 const CHAR szShell32Dll[] = "shell32.dll";
335 HICON ahIcon[256];
336 UINT i, aIconId[256], cIcons, cIcons2;
337
338 if (!pPrivateExtractIconsA) return;
339
340 cIcons = pPrivateExtractIconsA("", 0, 16, 16, ahIcon, aIconId, 1, 0);
341 ok(cIcons == ~0u, "got %u\n", cIcons);
342
343 cIcons = pPrivateExtractIconsA("notepad.exe", 0, 16, 16, NULL, NULL, 1, 0);
344 ok(cIcons == 1 || broken(cIcons == 2) /* win2k */, "got %u\n", cIcons);
345
346 ahIcon[0] = (HICON)0xdeadbeef;
347 cIcons = pPrivateExtractIconsA("notepad.exe", 0, 16, 16, ahIcon, NULL, 1, 0);
348 ok(cIcons == 1, "got %u\n", cIcons);
349 ok(ahIcon[0] != (HICON)0xdeadbeef, "icon not set\n");
350 DestroyIcon(ahIcon[0]);
351
352 ahIcon[0] = (HICON)0xdeadbeef;
353 aIconId[0] = 0xdeadbeef;
354 cIcons = pPrivateExtractIconsA("notepad.exe", 0, 16, 16, ahIcon, aIconId, 1, 0);
355 ok(cIcons == 1, "got %u\n", cIcons);
356 ok(ahIcon[0] != (HICON)0xdeadbeef, "icon not set\n");
357 ok(aIconId[0] != 0xdeadbeef, "id not set\n");
358 DestroyIcon(ahIcon[0]);
359
360 cIcons = pPrivateExtractIconsA(szShell32Dll, 0, 16, 16, NULL, NULL, 0, 0);
361 cIcons2 = pPrivateExtractIconsA(szShell32Dll, 4, MAKELONG(32,16), MAKELONG(32,16),
362 NULL, NULL, 256, 0);
363 ok((cIcons == cIcons2) && (cIcons > 0),
364 "Icon count should be independent of requested icon sizes and base icon index! "
365 "(cIcons=%d, cIcons2=%d)\n", cIcons, cIcons2);
366
367 cIcons = pPrivateExtractIconsA(szShell32Dll, 0, 16, 16, ahIcon, aIconId, 0, 0);
368 ok(cIcons == 0, "Zero icons requested, got cIcons=%d\n", cIcons);
369
370 cIcons = pPrivateExtractIconsA(szShell32Dll, 0, 16, 16, ahIcon, aIconId, 3, 0);
371 ok(cIcons == 3, "Three icons requested got cIcons=%d\n", cIcons);
372 for (i = 0; i < cIcons; i++) DestroyIcon(ahIcon[i]);
373
374 /* count must be a multiple of two when getting two sizes */
375 cIcons = pPrivateExtractIconsA(szShell32Dll, 0, MAKELONG(16,32), MAKELONG(16,32),
376 ahIcon, aIconId, 3, 0);
377 ok(cIcons == 0 /* vista */ || cIcons == 4, "Three icons requested got cIcons=%d\n", cIcons);
378 for (i = 0; i < cIcons; i++) DestroyIcon(ahIcon[i]);
379
380 cIcons = pPrivateExtractIconsA(szShell32Dll, 0, MAKELONG(16,32), MAKELONG(16,32),
381 ahIcon, aIconId, 4, 0);
382 ok(cIcons == 4, "Four icons requested got cIcons=%d\n", cIcons);
383 for (i = 0; i < cIcons; i++) DestroyIcon(ahIcon[i]);
384 }
385
386 static void test_LoadImage(void)
387 {
388 HBITMAP bmp;
389 HRSRC hres;
390
391 bmp = LoadBitmapA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(100));
392 ok(bmp != NULL, "Could not load a bitmap resource\n");
393 if (bmp) DeleteObject(bmp);
394
395 hres = FindResourceA(GetModuleHandleA(NULL), "#100", (LPCSTR)RT_BITMAP);
396 ok(hres != NULL, "Could not find a bitmap resource with a numeric string\n");
397
398 bmp = LoadBitmapA(GetModuleHandleA(NULL), "#100");
399 ok(bmp != NULL, "Could not load a bitmap resource with a numeric string\n");
400 if (bmp) DeleteObject(bmp);
401 }
402
403 START_TEST(resource)
404 {
405 init_function_pointers();
406 test_LoadStringA();
407 test_LoadStringW();
408 test_accel1();
409 test_accel2();
410 test_PrivateExtractIcons();
411 test_LoadImage();
412 }