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