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