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