quartz: Silence requests for ipin on filters.
[wine] / dlls / user32 / tests / 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)(LPCTSTR, 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 = GetModuleHandle(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     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",
47         length1, length2);
48     ok(length1 > 0 && resourcepointer != NULL, "LoadStringW failed to get pointer to resource 2, ret %d, err %d\n",
49         length1, GetLastError());
50
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 */
53     {
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);
61     }
62
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);
69 }
70
71 static void test_LoadStringA (void)
72 {
73     HINSTANCE hInst = GetModuleHandle (NULL);
74     static const char str[] = "String resource"; /* same in resource.rc */
75     char buf[128];
76     struct string_test {
77         int bufsiz;
78         int expected;
79     };
80     struct string_test tests[] = {{sizeof buf, sizeof str - 1},
81                                   {sizeof str, sizeof str - 1},
82                                   {sizeof str - 1, sizeof str - 2}};
83     unsigned int i;
84     int ret;
85
86     assert (sizeof str < sizeof buf);
87     for (i = 0; i < sizeof tests / sizeof tests[0]; i++) {
88         const int bufsiz = tests[i].bufsiz;
89         const int expected = tests[i].expected;
90         const int len = LoadStringA (hInst, 0, buf, bufsiz);
91
92         ok (len == expected, "bufsiz=%d: got %d, expected %d\n",
93             bufsiz, len, expected);
94         ok (!memcmp (buf, str, len),
95             "bufsiz=%d: got '%s', expected '%.*s'\n",
96             bufsiz, buf, len, str);
97         ok (buf[len] == 0, "bufsiz=%d: NUL termination missing\n",
98             bufsiz);
99     }
100
101     ret = LoadStringA(hInst, 1, buf, sizeof(buf) );
102     ok( ret > 0, "LoadString failed: ret %d err %d\n", ret, GetLastError());
103     ok( LoadStringA( hInst, MAKELONG( 1, 0x8000 ), buf, sizeof(buf)) == ret,
104         "LoadString failed: ret %d err %d\n", ret, GetLastError());
105     ok( LoadStringA( hInst, MAKELONG( 1, 0xffff ), buf, sizeof(buf)) == ret,
106         "LoadString failed: ret %d err %d\n", ret, GetLastError());
107
108     ret = LoadStringA(hInst, 65534, buf, sizeof(buf) );
109     ok( ret > 0, "LoadString failed: ret %d err %d\n", ret, GetLastError());
110     ok( LoadStringA( hInst, MAKELONG( 65534, 0x8000 ), buf, sizeof(buf)) == ret,
111         "LoadString failed: ret %d err %d\n", ret, GetLastError());
112     ok( LoadStringA( hInst, MAKELONG( 65534, 0xffff ), buf, sizeof(buf)) == ret,
113         "LoadString failed: ret %d err %d\n", ret, GetLastError());
114
115     ret = LoadStringA(hInst, 0, buf, 0);
116     ok( ret == -1, "LoadStringA did not return -1 when called with buflen = 0, got %d, err %d\n",
117         ret, GetLastError());
118 }
119
120 static void test_accel1(void)
121 {
122     UINT r, n;
123     HACCEL hAccel;
124     ACCEL ac[10];
125
126     /* now create our own valid accelerator table */
127     n = 0;
128     ac[n].cmd = 1000;
129     ac[n].key = 'A';
130     ac[n++].fVirt = FVIRTKEY | FNOINVERT;
131
132     ac[n].cmd = 1001;
133     ac[n].key = 'B';
134     ac[n++].fVirt = FNOINVERT;
135
136     ac[n].cmd = 0;
137     ac[n].key = 0;
138     ac[n++].fVirt = 0;
139
140     hAccel = CreateAcceleratorTable( &ac[0], n );
141     ok( hAccel != NULL, "create accelerator table\n");
142
143     r = DestroyAcceleratorTable( hAccel );
144     ok( r, "destroy accelerator table\n");
145
146     /* now try create an invalid one */
147     n = 0;
148     ac[n].cmd = 1000;
149     ac[n].key = 'A';
150     ac[n++].fVirt = FVIRTKEY | FNOINVERT;
151
152     ac[n].cmd = 0xffff;
153     ac[n].key = 0xffff;
154     ac[n++].fVirt = (SHORT) 0xffff;
155
156     ac[n].cmd = 0xfff0;
157     ac[n].key = 0xffff;
158     ac[n++].fVirt = (SHORT) 0xfff0;
159
160     ac[n].cmd = 0xfff0;
161     ac[n].key = 0xffff;
162     ac[n++].fVirt = (SHORT) 0x0000;
163
164     ac[n].cmd = 0xfff0;
165     ac[n].key = 0xffff;
166     ac[n++].fVirt = (SHORT) 0x0001;
167
168     hAccel = CreateAcceleratorTable( &ac[0], n );
169     ok( hAccel != NULL, "create accelerator table\n");
170
171     r = CopyAcceleratorTable( hAccel, NULL, 0 );
172     ok( r == n, "two entries in table\n");
173
174     r = CopyAcceleratorTable( hAccel, &ac[0], r );
175     ok( r == n, "still should be two entries in table\n");
176
177     n=0;
178     ok( ac[n].cmd == 1000, "cmd 0 not preserved\n");
179     ok( ac[n].key == 'A', "key 0 not preserved\n");
180     ok( ac[n].fVirt == (FVIRTKEY | FNOINVERT), "fVirt 0 not preserved\n");
181
182     n++;
183     ok( ac[n].cmd == 0xffff, "cmd 1 not preserved\n");
184     ok( ac[n].key == 0xffff, "key 1 not preserved\n");
185     ok( ac[n].fVirt == 0x007f, "fVirt 1 not changed\n");
186
187     n++;
188     ok( ac[n].cmd == 0xfff0, "cmd 2 not preserved\n");
189     ok( ac[n].key == 0x00ff, "key 2 not preserved\n");
190     ok( ac[n].fVirt == 0x0070, "fVirt 2 not changed\n");
191
192     n++;
193     ok( ac[n].cmd == 0xfff0, "cmd 3 not preserved\n");
194     ok( ac[n].key == 0x00ff, "key 3 not preserved\n");
195     ok( ac[n].fVirt == 0x0000, "fVirt 3 not changed\n");
196
197     n++;
198     ok( ac[n].cmd == 0xfff0, "cmd 4 not preserved\n");
199     ok( ac[n].key == 0xffff, "key 4 not preserved\n");
200     ok( ac[n].fVirt == 0x0001, "fVirt 4 not changed\n");
201
202     r = DestroyAcceleratorTable( hAccel );
203     ok( r, "destroy accelerator table\n");
204
205     hAccel = CreateAcceleratorTable( &ac[0], 0 );
206     ok( !hAccel, "zero elements should fail\n");
207
208     /* these will on crash win2k
209     hAccel = CreateAcceleratorTable( NULL, 1 );
210     hAccel = CreateAcceleratorTable( &ac[0], -1 );
211     */
212 }
213
214 /*
215  *  memcmp on the tables works in Windows, but does not work in wine, as
216  *  there is an extra undefined and unused byte between fVirt and the key
217  */
218 static void test_accel2(void)
219 {
220     ACCEL ac[2], out[2];
221     HACCEL hac;
222
223     ac[0].cmd   = 0;
224     ac[0].fVirt = 0;
225     ac[0].key   = 0;
226
227     ac[1].cmd   = 0;
228     ac[1].fVirt = 0;
229     ac[1].key   = 0;
230
231     /*
232      * crashes on win2k
233      * hac = CreateAcceleratorTable( NULL, 1 );
234      */
235
236     /* try a zero count */
237     hac = CreateAcceleratorTable( &ac[0], 0 );
238     ok( !hac , "fail\n");
239     ok( !DestroyAcceleratorTable( hac ), "destroy failed\n");
240
241     /* creating one accelerator should work */
242     hac = CreateAcceleratorTable( &ac[0], 1 );
243     ok( hac != NULL , "fail\n");
244     ok( 1 == CopyAcceleratorTable( hac, out, 1 ), "copy failed\n");
245     ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
246
247     /* how about two of the same type? */
248     hac = CreateAcceleratorTable( &ac[0], 2);
249     ok( hac != NULL , "fail\n");
250     ok( 2 == CopyAcceleratorTable( hac, NULL, 100 ), "copy null failed\n");
251     ok( 2 == CopyAcceleratorTable( hac, NULL, 0 ), "copy null failed\n");
252     ok( 2 == CopyAcceleratorTable( hac, NULL, 1 ), "copy null failed\n");
253     ok( 1 == CopyAcceleratorTable( hac, out, 1 ), "copy 1 failed\n");
254     ok( 2 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
255     ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
256     /* ok( !memcmp( ac, out, sizeof ac ), "tables different\n"); */
257
258     /* how about two of the same type with a non-zero key? */
259     ac[0].key = 0x20;
260     ac[1].key = 0x20;
261     hac = CreateAcceleratorTable( &ac[0], 2);
262     ok( hac != NULL , "fail\n");
263     ok( 2 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
264     ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
265     /* ok( !memcmp( ac, out, sizeof ac ), "tables different\n"); */
266
267     /* how about two of the same type with a non-zero virtual key? */
268     ac[0].fVirt = FVIRTKEY;
269     ac[0].key = 0x40;
270     ac[1].fVirt = FVIRTKEY;
271     ac[1].key = 0x40;
272     hac = CreateAcceleratorTable( &ac[0], 2);
273     ok( hac != NULL , "fail\n");
274     ok( 2 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
275     /* ok( !memcmp( ac, out, sizeof ac ), "tables different\n"); */
276     ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
277
278     /* how virtual key codes */
279     ac[0].fVirt = FVIRTKEY;
280     hac = CreateAcceleratorTable( &ac[0], 1);
281     ok( hac != NULL , "fail\n");
282     ok( 1 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
283     /* ok( !memcmp( ac, out, sizeof ac/2 ), "tables different\n"); */
284     ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
285
286     /* how turning on all bits? */
287     ac[0].cmd   = 0xffff;
288     ac[0].fVirt = 0xff;
289     ac[0].key   = 0xffff;
290     hac = CreateAcceleratorTable( &ac[0], 1);
291     ok( hac != NULL , "fail\n");
292     ok( 1 == CopyAcceleratorTable( hac, out, 1 ), "copy 1 failed\n");
293     /* ok( memcmp( ac, out, sizeof ac/2 ), "tables not different\n"); */
294     ok( out[0].cmd == ac[0].cmd, "cmd modified\n");
295     ok( out[0].fVirt == (ac[0].fVirt&0x7f), "fVirt not modified\n");
296     ok( out[0].key == ac[0].key, "key modified\n");
297     ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
298
299     /* how turning on all bits? */
300     memset( ac, 0xff, sizeof ac );
301     hac = CreateAcceleratorTable( &ac[0], 2);
302     ok( hac != NULL , "fail\n");
303     ok( 2 == CopyAcceleratorTable( hac, out, 2 ), "copy 2 failed\n");
304     /* ok( memcmp( ac, out, sizeof ac ), "tables not different\n"); */
305     ok( out[0].cmd == ac[0].cmd, "cmd modified\n");
306     ok( out[0].fVirt == (ac[0].fVirt&0x7f), "fVirt not modified\n");
307     ok( out[0].key == ac[0].key, "key modified\n");
308     ok( out[1].cmd == ac[1].cmd, "cmd modified\n");
309     ok( out[1].fVirt == (ac[1].fVirt&0x7f), "fVirt not modified\n");
310     ok( out[1].key == ac[1].key, "key modified\n");
311     ok( DestroyAcceleratorTable( hac ), "destroy failed\n");
312 }
313
314 static void test_PrivateExtractIcons(void) {
315     CONST CHAR szShell32Dll[] = "shell32.dll";
316     HICON ahIcon[256];
317     UINT aIconId[256];
318     UINT cIcons, cIcons2;
319
320     if (!pPrivateExtractIconsA) return;
321     
322     cIcons = pPrivateExtractIconsA(szShell32Dll, 0, 16, 16, NULL, NULL, 0, 0);
323     cIcons2 = pPrivateExtractIconsA(szShell32Dll, 4, MAKELONG(32,16), MAKELONG(32,16), 
324                                    NULL, NULL, 256, 0);
325     ok((cIcons == cIcons2) && (cIcons > 0), 
326        "Icon count should be independent of requested icon sizes and base icon index! "
327        "(cIcons=%d, cIcons2=%d)\n", cIcons, cIcons2);
328
329     cIcons = pPrivateExtractIconsA(szShell32Dll, 0, 16, 16, ahIcon, aIconId, 0, 0);
330     ok(cIcons == 0, "Zero icons requested, got cIcons=%d\n", cIcons);
331
332     cIcons = pPrivateExtractIconsA(szShell32Dll, 0, 16, 16, ahIcon, aIconId, 3, 0);
333     ok(cIcons == 3, "Three icons requested got cIcons=%d\n", cIcons);
334
335     cIcons = pPrivateExtractIconsA(szShell32Dll, 0, MAKELONG(32,16), MAKELONG(32,16), 
336                                   ahIcon, aIconId, 3, 0);
337     ok(cIcons == 4, "Three icons requested, four expected, got cIcons=%d\n", cIcons);
338 }
339
340 static void test_LoadImage(void)
341 {
342     HBITMAP bmp;
343     HRSRC hres;
344
345     bmp = LoadBitmapA(GetModuleHandle(NULL), MAKEINTRESOURCE(100));
346     ok(bmp != NULL, "Could not load a bitmap resource\n");
347     if (bmp) DeleteObject(bmp);
348
349     hres = FindResource(GetModuleHandle(NULL), "#100", RT_BITMAP);
350     ok(hres != NULL, "Could not find a bitmap resource with a numeric string\n");
351
352     bmp = LoadBitmapA(GetModuleHandle(NULL), "#100");
353     ok(bmp != NULL, "Could not load a bitmap resource with a numeric string\n");
354     if (bmp) DeleteObject(bmp);
355 }
356
357 START_TEST(resource)
358 {
359     init_function_pointers();
360     test_LoadStringA();
361     test_LoadStringW();
362     test_accel1();
363     test_accel2();
364     test_PrivateExtractIcons();
365     test_LoadImage();
366 }