user32: CBT hook doesn't send window messages.
[wine] / dlls / user32 / tests / cursoricon.c
1 /*
2  * Unit test suite for cursors and icons.
3  *
4  * Copyright 2006 Michael Kaufmann
5  * Copyright 2007 Dmitry Timoshkov
6  * Copyright 2007-2008 Andrew Riedi
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winreg.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34
35 #include "pshpack1.h"
36
37 typedef struct
38 {
39     BYTE bWidth;
40     BYTE bHeight;
41     BYTE bColorCount;
42     BYTE bReserved;
43     WORD xHotspot;
44     WORD yHotspot;
45     DWORD dwDIBSize;
46     DWORD dwDIBOffset;
47 } CURSORICONFILEDIRENTRY;
48
49 typedef struct
50 {
51     WORD idReserved;
52     WORD idType;
53     WORD idCount;
54     CURSORICONFILEDIRENTRY idEntries[1];
55 } CURSORICONFILEDIR;
56
57 #include "poppack.h"
58
59 static char **test_argv;
60 static int test_argc;
61 static HWND child = 0;
62 static HWND parent = 0;
63 static HANDLE child_process;
64
65 #define PROC_INIT (WM_USER+1)
66
67 static LRESULT CALLBACK callback_child(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
68 {
69     BOOL ret;
70     DWORD error;
71
72     switch (msg)
73     {
74         /* Destroy the cursor. */
75         case WM_USER+1:
76             SetLastError(0xdeadbeef);
77             ret = DestroyCursor((HCURSOR) lParam);
78             error = GetLastError();
79             todo_wine {
80             ok(!ret, "DestroyCursor on the active cursor succeeded.\n");
81             ok(error == ERROR_DESTROY_OBJECT_OF_OTHER_THREAD,
82                 "Last error: %u\n", error);
83             }
84             return TRUE;
85         case WM_DESTROY:
86             PostQuitMessage(0);
87             return 0;
88     }
89
90     return DefWindowProc(hwnd, msg, wParam, lParam);
91 }
92
93 static LRESULT CALLBACK callback_parent(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
94 {
95     if (msg == PROC_INIT)
96     {
97         child = (HWND) wParam;
98         return TRUE;
99     }
100
101     return DefWindowProc(hwnd, msg, wParam, lParam);
102 }
103
104 static void do_child(void)
105 {
106     WNDCLASS class;
107     MSG msg;
108     BOOL ret;
109
110     /* Register a new class. */
111     class.style = CS_GLOBALCLASS;
112     class.lpfnWndProc = callback_child;
113     class.cbClsExtra = 0;
114     class.cbWndExtra = 0;
115     class.hInstance = GetModuleHandle(NULL);
116     class.hIcon = NULL;
117     class.hCursor = NULL;
118     class.hbrBackground = NULL;
119     class.lpszMenuName = NULL;
120     class.lpszClassName = "cursor_child";
121
122     SetLastError(0xdeadbeef);
123     ret = RegisterClass(&class);
124     ok(ret, "Failed to register window class.  Error: %u\n", GetLastError());
125
126     /* Create a window. */
127     child = CreateWindowA("cursor_child", "cursor_child", WS_POPUP | WS_VISIBLE,
128         0, 0, 200, 200, 0, 0, 0, NULL);
129     ok(child != 0, "CreateWindowA failed.  Error: %u\n", GetLastError());
130
131     /* Let the parent know our HWND. */
132     PostMessage(parent, PROC_INIT, (WPARAM) child, 0);
133
134     /* Receive messages. */
135     while ((ret = GetMessage(&msg, child, 0, 0)))
136     {
137         ok(ret != -1, "GetMessage failed.  Error: %u\n", GetLastError());
138         TranslateMessage(&msg);
139         DispatchMessage(&msg);
140     }
141 }
142
143 static void do_parent(void)
144 {
145     char path_name[MAX_PATH];
146     PROCESS_INFORMATION info;
147     STARTUPINFOA startup;
148     WNDCLASS class;
149     MSG msg;
150     BOOL ret;
151
152     /* Register a new class. */
153     class.style = CS_GLOBALCLASS;
154     class.lpfnWndProc = callback_parent;
155     class.cbClsExtra = 0;
156     class.cbWndExtra = 0;
157     class.hInstance = GetModuleHandle(NULL);
158     class.hIcon = NULL;
159     class.hCursor = NULL;
160     class.hbrBackground = NULL;
161     class.lpszMenuName = NULL;
162     class.lpszClassName = "cursor_parent";
163
164     SetLastError(0xdeadbeef);
165     ret = RegisterClass(&class);
166     ok(ret, "Failed to register window class.  Error: %u\n", GetLastError());
167
168     /* Create a window. */
169     parent = CreateWindowA("cursor_parent", "cursor_parent", WS_POPUP | WS_VISIBLE,
170         0, 0, 200, 200, 0, 0, 0, NULL);
171     ok(parent != 0, "CreateWindowA failed.  Error: %u\n", GetLastError());
172
173     /* Start child process. */
174     memset(&startup, 0, sizeof(startup));
175     startup.cb = sizeof(startup);
176     startup.dwFlags = STARTF_USESHOWWINDOW;
177     startup.wShowWindow = SW_SHOWNORMAL;
178
179     sprintf(path_name, "%s cursoricon %x", test_argv[0], (unsigned int) parent);
180     ok(CreateProcessA(NULL, path_name, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess failed.\n");
181     child_process = info.hProcess;
182
183     /* Wait for child window handle. */
184     while ((child == 0) && (ret = GetMessage(&msg, parent, 0, 0)))
185     {
186         ok(ret != -1, "GetMessage failed.  Error: %u\n", GetLastError());
187         TranslateMessage(&msg);
188         DispatchMessage(&msg);
189     }
190 }
191
192 static void finish_child_process(void)
193 {
194     SendMessage(child, WM_CLOSE, 0, 0);
195     winetest_wait_child_process( child_process );
196     CloseHandle(child_process);
197 }
198
199 static void test_child_process(void)
200 {
201     static const BYTE bmp_bits[4096];
202     HCURSOR cursor;
203     ICONINFO cursorInfo;
204     UINT display_bpp;
205     HDC hdc;
206
207     /* Create and set a dummy cursor. */
208     hdc = GetDC(0);
209     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
210     ReleaseDC(0, hdc);
211
212     cursorInfo.fIcon = FALSE;
213     cursorInfo.xHotspot = 0;
214     cursorInfo.yHotspot = 0;
215     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
216     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
217
218     cursor = CreateIconIndirect(&cursorInfo);
219     ok(cursor != NULL, "CreateIconIndirect returned %p.\n", cursor);
220
221     SetCursor(cursor);
222
223     /* Destroy the cursor. */
224     SendMessage(child, WM_USER+1, 0, (LPARAM) cursor);
225 }
226
227 static void test_CopyImage_Check(HBITMAP bitmap, UINT flags, INT copyWidth, INT copyHeight,
228                                   INT expectedWidth, INT expectedHeight, WORD expectedDepth, BOOL dibExpected)
229 {
230     HBITMAP copy;
231     BITMAP origBitmap;
232     BITMAP copyBitmap;
233     BOOL orig_is_dib;
234     BOOL copy_is_dib;
235
236     copy = (HBITMAP) CopyImage(bitmap, IMAGE_BITMAP, copyWidth, copyHeight, flags);
237     ok(copy != NULL, "CopyImage() failed\n");
238     if (copy != NULL)
239     {
240         GetObject(bitmap, sizeof(origBitmap), &origBitmap);
241         GetObject(copy, sizeof(copyBitmap), &copyBitmap);
242         orig_is_dib = (origBitmap.bmBits != NULL);
243         copy_is_dib = (copyBitmap.bmBits != NULL);
244
245         if (copy_is_dib && dibExpected
246             && copyBitmap.bmBitsPixel == 24
247             && (expectedDepth == 16 || expectedDepth == 32))
248         {
249             /* Windows 95 doesn't create DIBs with a depth of 16 or 32 bit */
250             if (GetVersion() & 0x80000000)
251             {
252                 expectedDepth = 24;
253             }
254         }
255
256         if (copy_is_dib && !dibExpected && !(flags & LR_CREATEDIBSECTION))
257         {
258             /* It's not forbidden to create a DIB section if the flag
259                LR_CREATEDIBSECTION is absent.
260                Windows 9x does this if the bitmap has a depth that doesn't
261                match the screen depth, Windows NT doesn't */
262             dibExpected = TRUE;
263             expectedDepth = origBitmap.bmBitsPixel;
264         }
265
266         ok((!(dibExpected ^ copy_is_dib)
267              && (copyBitmap.bmWidth == expectedWidth)
268              && (copyBitmap.bmHeight == expectedHeight)
269              && (copyBitmap.bmBitsPixel == expectedDepth)),
270              "CopyImage ((%s, %dx%d, %u bpp), %d, %d, %#x): Expected (%s, %dx%d, %u bpp), got (%s, %dx%d, %u bpp)\n",
271                   orig_is_dib ? "DIB" : "DDB", origBitmap.bmWidth, origBitmap.bmHeight, origBitmap.bmBitsPixel,
272                   copyWidth, copyHeight, flags,
273                   dibExpected ? "DIB" : "DDB", expectedWidth, expectedHeight, expectedDepth,
274                   copy_is_dib ? "DIB" : "DDB", copyBitmap.bmWidth, copyBitmap.bmHeight, copyBitmap.bmBitsPixel);
275
276         DeleteObject(copy);
277     }
278 }
279
280 static void test_CopyImage_Bitmap(int depth)
281 {
282     HBITMAP ddb, dib;
283     HDC screenDC;
284     BITMAPINFO * info;
285     VOID * bits;
286     int screen_depth;
287     unsigned int i;
288
289     /* Create a device-independent bitmap (DIB) */
290     info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
291     info->bmiHeader.biSize = sizeof(info->bmiHeader);
292     info->bmiHeader.biWidth = 2;
293     info->bmiHeader.biHeight = 2;
294     info->bmiHeader.biPlanes = 1;
295     info->bmiHeader.biBitCount = depth;
296     info->bmiHeader.biCompression = BI_RGB;
297
298     for (i=0; i < 256; i++)
299     {
300         info->bmiColors[i].rgbRed = i;
301         info->bmiColors[i].rgbGreen = i;
302         info->bmiColors[i].rgbBlue = 255 - i;
303         info->bmiColors[i].rgbReserved = 0;
304     }
305
306     dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
307
308     /* Create a device-dependent bitmap (DDB) */
309     screenDC = GetDC(NULL);
310     screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
311     if (depth == 1 || depth == screen_depth)
312     {
313         ddb = CreateBitmap(2, 2, 1, depth, NULL);
314     }
315     else
316     {
317         ddb = NULL;
318     }
319     ReleaseDC(NULL, screenDC);
320
321     if (ddb != NULL)
322     {
323         test_CopyImage_Check(ddb, 0, 0, 0, 2, 2, depth == 1 ? 1 : screen_depth, FALSE);
324         test_CopyImage_Check(ddb, 0, 0, 5, 2, 5, depth == 1 ? 1 : screen_depth, FALSE);
325         test_CopyImage_Check(ddb, 0, 5, 0, 5, 2, depth == 1 ? 1 : screen_depth, FALSE);
326         test_CopyImage_Check(ddb, 0, 5, 5, 5, 5, depth == 1 ? 1 : screen_depth, FALSE);
327
328         test_CopyImage_Check(ddb, LR_MONOCHROME, 0, 0, 2, 2, 1, FALSE);
329         test_CopyImage_Check(ddb, LR_MONOCHROME, 5, 0, 5, 2, 1, FALSE);
330         test_CopyImage_Check(ddb, LR_MONOCHROME, 0, 5, 2, 5, 1, FALSE);
331         test_CopyImage_Check(ddb, LR_MONOCHROME, 5, 5, 5, 5, 1, FALSE);
332
333         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
334         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
335         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
336         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
337
338         /* LR_MONOCHROME is ignored if LR_CREATEDIBSECTION is present */
339         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
340         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
341         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
342         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
343
344         DeleteObject(ddb);
345     }
346
347     if (depth != 1)
348     {
349         test_CopyImage_Check(dib, 0, 0, 0, 2, 2, screen_depth, FALSE);
350         test_CopyImage_Check(dib, 0, 5, 0, 5, 2, screen_depth, FALSE);
351         test_CopyImage_Check(dib, 0, 0, 5, 2, 5, screen_depth, FALSE);
352         test_CopyImage_Check(dib, 0, 5, 5, 5, 5, screen_depth, FALSE);
353     }
354
355     test_CopyImage_Check(dib, LR_MONOCHROME, 0, 0, 2, 2, 1, FALSE);
356     test_CopyImage_Check(dib, LR_MONOCHROME, 5, 0, 5, 2, 1, FALSE);
357     test_CopyImage_Check(dib, LR_MONOCHROME, 0, 5, 2, 5, 1, FALSE);
358     test_CopyImage_Check(dib, LR_MONOCHROME, 5, 5, 5, 5, 1, FALSE);
359
360     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
361     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
362     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
363     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
364
365     /* LR_MONOCHROME is ignored if LR_CREATEDIBSECTION is present */
366     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
367     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
368     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
369     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
370
371     DeleteObject(dib);
372
373     if (depth == 1)
374     {
375         /* Special case: A monochrome DIB is converted to a monochrome DDB if
376            the colors in the color table are black and white.
377
378            Skip this test on Windows 95, it always creates a monochrome DDB
379            in this case */
380
381         if (!(GetVersion() & 0x80000000))
382         {
383             info->bmiHeader.biBitCount = 1;
384             info->bmiColors[0].rgbRed = 0xFF;
385             info->bmiColors[0].rgbGreen = 0;
386             info->bmiColors[0].rgbBlue = 0;
387             info->bmiColors[1].rgbRed = 0;
388             info->bmiColors[1].rgbGreen = 0xFF;
389             info->bmiColors[1].rgbBlue = 0;
390
391             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
392             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, screen_depth, FALSE);
393             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, screen_depth, FALSE);
394             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, screen_depth, FALSE);
395             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, screen_depth, FALSE);
396             DeleteObject(dib);
397
398             info->bmiHeader.biBitCount = 1;
399             info->bmiColors[0].rgbRed = 0;
400             info->bmiColors[0].rgbGreen = 0;
401             info->bmiColors[0].rgbBlue = 0;
402             info->bmiColors[1].rgbRed = 0xFF;
403             info->bmiColors[1].rgbGreen = 0xFF;
404             info->bmiColors[1].rgbBlue = 0xFF;
405
406             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
407             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, 1, FALSE);
408             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, 1, FALSE);
409             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, 1, FALSE);
410             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, 1, FALSE);
411             DeleteObject(dib);
412
413             info->bmiHeader.biBitCount = 1;
414             info->bmiColors[0].rgbRed = 0xFF;
415             info->bmiColors[0].rgbGreen = 0xFF;
416             info->bmiColors[0].rgbBlue = 0xFF;
417             info->bmiColors[1].rgbRed = 0;
418             info->bmiColors[1].rgbGreen = 0;
419             info->bmiColors[1].rgbBlue = 0;
420
421             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
422             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, 1, FALSE);
423             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, 1, FALSE);
424             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, 1, FALSE);
425             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, 1, FALSE);
426             DeleteObject(dib);
427         }
428     }
429
430     HeapFree(GetProcessHeap(), 0, info);
431 }
432
433 static void test_initial_cursor(void)
434 {
435     HCURSOR cursor, cursor2;
436     DWORD error;
437
438     cursor = GetCursor();
439
440     /* Check what handle GetCursor() returns if a cursor is not set yet. */
441     SetLastError(0xdeadbeef);
442     cursor2 = LoadCursor(NULL, IDC_WAIT);
443     todo_wine {
444         ok(cursor == cursor2, "cursor (%p) is not IDC_WAIT (%p).\n", cursor, cursor2);
445     }
446     error = GetLastError();
447     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
448 }
449
450 static void test_icon_info_dbg(HICON hIcon, UINT exp_cx, UINT exp_cy, UINT exp_bpp, int line)
451 {
452     ICONINFO info;
453     DWORD ret;
454     BITMAP bmMask, bmColor;
455
456     ret = GetIconInfo(hIcon, &info);
457     ok_(__FILE__, line)(ret, "GetIconInfo failed\n");
458
459     /* CreateIcon under XP causes info.fIcon to be 0 */
460     ok_(__FILE__, line)(info.xHotspot == exp_cx/2, "info.xHotspot = %u\n", info.xHotspot);
461     ok_(__FILE__, line)(info.yHotspot == exp_cy/2, "info.yHotspot = %u\n", info.yHotspot);
462     ok_(__FILE__, line)(info.hbmMask != 0, "info.hbmMask is NULL\n");
463
464     ret = GetObject(info.hbmMask, sizeof(bmMask), &bmMask);
465     ok_(__FILE__, line)(ret == sizeof(bmMask), "GetObject(info.hbmMask) failed, ret %u\n", ret);
466
467     if (exp_bpp == 1)
468         ok_(__FILE__, line)(info.hbmColor == 0, "info.hbmColor should be NULL\n");
469
470     if (info.hbmColor)
471     {
472         HDC hdc;
473         UINT display_bpp;
474
475         hdc = GetDC(0);
476         display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
477         ReleaseDC(0, hdc);
478
479         ret = GetObject(info.hbmColor, sizeof(bmColor), &bmColor);
480         ok_(__FILE__, line)(ret == sizeof(bmColor), "GetObject(info.hbmColor) failed, ret %u\n", ret);
481
482         ok_(__FILE__, line)(bmColor.bmBitsPixel == display_bpp /* XP */ ||
483            bmColor.bmBitsPixel == exp_bpp /* Win98 */,
484            "bmColor.bmBitsPixel = %d\n", bmColor.bmBitsPixel);
485         ok_(__FILE__, line)(bmColor.bmWidth == exp_cx, "bmColor.bmWidth = %d\n", bmColor.bmWidth);
486         ok_(__FILE__, line)(bmColor.bmHeight == exp_cy, "bmColor.bmHeight = %d\n", bmColor.bmHeight);
487
488         ok_(__FILE__, line)(bmMask.bmBitsPixel == 1, "bmMask.bmBitsPixel = %d\n", bmMask.bmBitsPixel);
489         ok_(__FILE__, line)(bmMask.bmWidth == exp_cx, "bmMask.bmWidth = %d\n", bmMask.bmWidth);
490         ok_(__FILE__, line)(bmMask.bmHeight == exp_cy, "bmMask.bmHeight = %d\n", bmMask.bmHeight);
491     }
492     else
493     {
494         ok_(__FILE__, line)(bmMask.bmBitsPixel == 1, "bmMask.bmBitsPixel = %d\n", bmMask.bmBitsPixel);
495         ok_(__FILE__, line)(bmMask.bmWidth == exp_cx, "bmMask.bmWidth = %d\n", bmMask.bmWidth);
496         ok_(__FILE__, line)(bmMask.bmHeight == exp_cy * 2, "bmMask.bmHeight = %d\n", bmMask.bmHeight);
497     }
498 }
499
500 #define test_icon_info(a,b,c,d) test_icon_info_dbg((a),(b),(c),(d),__LINE__)
501
502 static void test_CreateIcon(void)
503 {
504     static const BYTE bmp_bits[1024];
505     HICON hIcon;
506     HBITMAP hbmMask, hbmColor;
507     BITMAPINFO bmpinfo;
508     ICONINFO info;
509     HDC hdc;
510     void *bits;
511     UINT display_bpp;
512
513     hdc = GetDC(0);
514     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
515
516     /* these crash under XP
517     hIcon = CreateIcon(0, 16, 16, 1, 1, bmp_bits, NULL);
518     hIcon = CreateIcon(0, 16, 16, 1, 1, NULL, bmp_bits);
519     */
520
521     hIcon = CreateIcon(0, 16, 16, 1, 1, bmp_bits, bmp_bits);
522     ok(hIcon != 0, "CreateIcon failed\n");
523     test_icon_info(hIcon, 16, 16, 1);
524     DestroyIcon(hIcon);
525
526     hIcon = CreateIcon(0, 16, 16, 1, display_bpp, bmp_bits, bmp_bits);
527     ok(hIcon != 0, "CreateIcon failed\n");
528     test_icon_info(hIcon, 16, 16, display_bpp);
529     DestroyIcon(hIcon);
530
531     hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
532     ok(hbmMask != 0, "CreateBitmap failed\n");
533     hbmColor = CreateBitmap(16, 16, 1, display_bpp, bmp_bits);
534     ok(hbmColor != 0, "CreateBitmap failed\n");
535
536     info.fIcon = TRUE;
537     info.xHotspot = 8;
538     info.yHotspot = 8;
539     info.hbmMask = 0;
540     info.hbmColor = 0;
541     SetLastError(0xdeadbeaf);
542     hIcon = CreateIconIndirect(&info);
543     ok(!hIcon, "CreateIconIndirect should fail\n");
544     ok(GetLastError() == 0xdeadbeaf, "wrong error %u\n", GetLastError());
545
546     info.fIcon = TRUE;
547     info.xHotspot = 8;
548     info.yHotspot = 8;
549     info.hbmMask = 0;
550     info.hbmColor = hbmColor;
551     SetLastError(0xdeadbeaf);
552     hIcon = CreateIconIndirect(&info);
553     ok(!hIcon, "CreateIconIndirect should fail\n");
554     ok(GetLastError() == 0xdeadbeaf, "wrong error %u\n", GetLastError());
555
556     info.fIcon = TRUE;
557     info.xHotspot = 8;
558     info.yHotspot = 8;
559     info.hbmMask = hbmMask;
560     info.hbmColor = hbmColor;
561     hIcon = CreateIconIndirect(&info);
562     ok(hIcon != 0, "CreateIconIndirect failed\n");
563     test_icon_info(hIcon, 16, 16, display_bpp);
564     DestroyIcon(hIcon);
565
566     DeleteObject(hbmMask);
567     DeleteObject(hbmColor);
568
569     hbmMask = CreateBitmap(16, 32, 1, 1, bmp_bits);
570     ok(hbmMask != 0, "CreateBitmap failed\n");
571
572     info.fIcon = TRUE;
573     info.xHotspot = 8;
574     info.yHotspot = 8;
575     info.hbmMask = hbmMask;
576     info.hbmColor = 0;
577     SetLastError(0xdeadbeaf);
578     hIcon = CreateIconIndirect(&info);
579     ok(hIcon != 0, "CreateIconIndirect failed\n");
580     test_icon_info(hIcon, 16, 16, 1);
581     DestroyIcon(hIcon);
582
583     DeleteObject(hbmMask);
584     DeleteObject(hbmColor);
585
586     /* test creating an icon from a DIB section */
587
588     memset( &bmpinfo, 0, sizeof(bmpinfo) );
589     bmpinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
590     bmpinfo.bmiHeader.biWidth = 32;
591     bmpinfo.bmiHeader.biHeight = 32;
592     bmpinfo.bmiHeader.biPlanes = 1;
593     bmpinfo.bmiHeader.biBitCount = 8;
594     bmpinfo.bmiHeader.biCompression = BI_RGB;
595     hbmColor = CreateDIBSection( hdc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
596     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
597     if (bits)
598         memset( bits, 0x55, 32 * 32 * bmpinfo.bmiHeader.biBitCount / 8 );
599     bmpinfo.bmiHeader.biBitCount = 1;
600     hbmMask = CreateDIBSection( hdc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
601     ok(hbmMask != NULL, "Expected a handle to the DIB\n");
602     if (bits)
603         memset( bits, 0x55, 32 * 32 * bmpinfo.bmiHeader.biBitCount / 8 );
604
605     info.fIcon = TRUE;
606     info.xHotspot = 8;
607     info.yHotspot = 8;
608     info.hbmMask = hbmColor;
609     info.hbmColor = hbmMask;
610     SetLastError(0xdeadbeaf);
611     hIcon = CreateIconIndirect(&info);
612     ok(hIcon != 0, "CreateIconIndirect failed\n");
613     test_icon_info(hIcon, 32, 32, 8);
614     DestroyIcon(hIcon);
615     DeleteObject(hbmColor);
616
617     bmpinfo.bmiHeader.biBitCount = 16;
618     hbmColor = CreateDIBSection( hdc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
619     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
620     if (bits)
621         memset( bits, 0x55, 32 * 32 * bmpinfo.bmiHeader.biBitCount / 8 );
622
623     info.fIcon = TRUE;
624     info.xHotspot = 8;
625     info.yHotspot = 8;
626     info.hbmMask = hbmColor;
627     info.hbmColor = hbmMask;
628     SetLastError(0xdeadbeaf);
629     hIcon = CreateIconIndirect(&info);
630     ok(hIcon != 0, "CreateIconIndirect failed\n");
631     test_icon_info(hIcon, 32, 32, 8);
632     DestroyIcon(hIcon);
633     DeleteObject(hbmColor);
634
635     bmpinfo.bmiHeader.biBitCount = 32;
636     hbmColor = CreateDIBSection( hdc, &bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
637     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
638     if (bits)
639         memset( bits, 0x55, 32 * 32 * bmpinfo.bmiHeader.biBitCount / 8 );
640
641     info.fIcon = TRUE;
642     info.xHotspot = 8;
643     info.yHotspot = 8;
644     info.hbmMask = hbmColor;
645     info.hbmColor = hbmMask;
646     SetLastError(0xdeadbeaf);
647     hIcon = CreateIconIndirect(&info);
648     ok(hIcon != 0, "CreateIconIndirect failed\n");
649     test_icon_info(hIcon, 32, 32, 8);
650     DestroyIcon(hIcon);
651
652     DeleteObject(hbmMask);
653     DeleteObject(hbmColor);
654
655     ReleaseDC(0, hdc);
656 }
657
658 /* Shamelessly ripped from dlls/oleaut32/tests/olepicture.c */
659 /* 1x1 pixel gif */
660 static const unsigned char gifimage[35] = {
661 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
662 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
663 0x01,0x00,0x3b
664 };
665
666 /* 1x1 pixel jpg */
667 static const unsigned char jpgimage[285] = {
668 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
669 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
670 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
671 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
672 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
673 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
674 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
675 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
676 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
677 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0xff,0xc0,
678 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
679 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
680 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
681 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
682 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
683 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
684 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
685 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
686 };
687
688 /* 1x1 pixel png */
689 static const unsigned char pngimage[285] = {
690 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
691 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
692 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
693 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
694 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
695 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
696 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
697 };
698
699 /* 1x1 pixel bmp */
700 static const unsigned char bmpimage[66] = {
701 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
702 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
703 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
704 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
705 0x00,0x00
706 };
707
708 /* 2x2 pixel gif */
709 static const unsigned char gif4pixel[42] = {
710 0x47,0x49,0x46,0x38,0x37,0x61,0x02,0x00,0x02,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,
711 0x39,0x62,0xfc,0xff,0x1a,0xe5,0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x02,0x00,
712 0x02,0x00,0x00,0x02,0x03,0x14,0x16,0x05,0x00,0x3b
713 };
714
715 static void test_LoadImageFile(const unsigned char * image_data,
716     unsigned int image_size, const char * ext, BOOL expect_success)
717 {
718     HANDLE handle;
719     BOOL ret;
720     DWORD error, bytes_written;
721     char filename[64];
722
723     strcpy(filename, "test.");
724     strcat(filename, ext);
725
726     /* Create the test image. */
727     handle = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
728         FILE_ATTRIBUTE_NORMAL, NULL);
729     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
730     ret = WriteFile(handle, image_data, image_size, &bytes_written, NULL);
731     ok(bytes_written == image_size, "test file created improperly.\n");
732     CloseHandle(handle);
733
734     /* Load as cursor. For all tested formats, this should fail */
735     SetLastError(0xdeadbeef);
736     handle = LoadImageA(NULL, filename, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
737     ok(handle == NULL, "LoadImage(%s) as IMAGE_CURSOR succeeded incorrectly.\n", ext);
738     error = GetLastError();
739     ok(error == 0, "Last error: %u\n", error);
740     if (handle != NULL) DestroyCursor(handle);
741
742     /* Load as icon. For all tested formats, this should fail */
743     SetLastError(0xdeadbeef);
744     handle = LoadImageA(NULL, filename, IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
745     ok(handle == NULL, "LoadImage(%s) as IMAGE_ICON succeeded incorrectly.\n", ext);
746     error = GetLastError();
747     ok(error == 0, "Last error: %u\n", error);
748     if (handle != NULL) DestroyIcon(handle);
749
750     /* Load as bitmap. Should succeed if bmp, fail for everything else */
751     SetLastError(0xdeadbeef);
752     handle = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
753     if (expect_success)
754         ok(handle != NULL, "LoadImage(%s) as IMAGE_BITMAP failed.\n", ext);
755     else ok(handle == NULL, "LoadImage(%s) as IMAGE_BITMAP succeeded incorrectly.\n", ext);
756     error = GetLastError();
757     ok(error == 0, "Last error: %u\n", error);
758     if (handle != NULL) DeleteObject(handle);
759
760     DeleteFileA(filename);
761 }
762
763 static void test_LoadImage(void)
764 {
765     HANDLE handle;
766     BOOL ret;
767     DWORD error, bytes_written;
768     CURSORICONFILEDIR *icon_data;
769     CURSORICONFILEDIRENTRY *icon_entry;
770     BITMAPINFOHEADER *icon_header;
771     ICONINFO icon_info;
772
773 #define ICON_WIDTH 32
774 #define ICON_HEIGHT 32
775 #define ICON_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
776 #define ICON_BPP 32
777 #define ICON_SIZE \
778     (sizeof(CURSORICONFILEDIR) + sizeof(BITMAPINFOHEADER) \
779     + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
780
781     /* Set icon data. */
782     icon_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ICON_SIZE);
783     icon_data->idReserved = 0;
784     icon_data->idType = 1;
785     icon_data->idCount = 1;
786
787     icon_entry = icon_data->idEntries;
788     icon_entry->bWidth = ICON_WIDTH;
789     icon_entry->bHeight = ICON_HEIGHT;
790     icon_entry->bColorCount = 0;
791     icon_entry->bReserved = 0;
792     icon_entry->xHotspot = 1;
793     icon_entry->yHotspot = 1;
794     icon_entry->dwDIBSize = ICON_SIZE - sizeof(CURSORICONFILEDIR);
795     icon_entry->dwDIBOffset = sizeof(CURSORICONFILEDIR);
796
797     icon_header = (BITMAPINFOHEADER *) ((BYTE *) icon_data + icon_entry->dwDIBOffset);
798     icon_header->biSize = sizeof(BITMAPINFOHEADER);
799     icon_header->biWidth = ICON_WIDTH;
800     icon_header->biHeight = ICON_HEIGHT*2;
801     icon_header->biPlanes = 1;
802     icon_header->biBitCount = ICON_BPP;
803     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
804
805     /* Create the icon. */
806     handle = CreateFileA("icon.ico", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
807         FILE_ATTRIBUTE_NORMAL, NULL);
808     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
809     ret = WriteFile(handle, icon_data, ICON_SIZE, &bytes_written, NULL);
810     ok(bytes_written == ICON_SIZE, "icon.ico created improperly.\n");
811     CloseHandle(handle);
812
813     /* Test loading an icon as a cursor. */
814     SetLastError(0xdeadbeef);
815     handle = LoadImageA(NULL, "icon.ico", IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
816     todo_wine
817     ok(handle != NULL, "LoadImage() failed.\n");
818     error = GetLastError();
819     ok(error == 0, "Last error: %u\n", error);
820
821     /* Test the icon information. */
822     SetLastError(0xdeadbeef);
823     ret = GetIconInfo(handle, &icon_info);
824     todo_wine
825     ok(ret, "GetIconInfo() failed.\n");
826     error = GetLastError();
827     ok(error == 0xdeadbeef, "Last error: %u\n", error);
828
829     if (ret)
830     {
831         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
832         ok(icon_info.xHotspot == 1, "xHotspot is %u.\n", icon_info.xHotspot);
833         ok(icon_info.yHotspot == 1, "yHotspot is %u.\n", icon_info.yHotspot);
834         ok(icon_info.hbmColor != NULL, "No hbmColor!\n");
835         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
836     }
837
838     /* Clean up. */
839     SetLastError(0xdeadbeef);
840     ret = DestroyCursor(handle);
841     todo_wine
842     ok(ret, "DestroyCursor() failed.\n");
843     error = GetLastError();
844     ok(error == 0xdeadbeef, "Last error: %u\n", error);
845
846     HeapFree(GetProcessHeap(), 0, icon_data);
847     DeleteFileA("icon.ico");
848
849     test_LoadImageFile(bmpimage, sizeof(bmpimage), "bmp", 1);
850     test_LoadImageFile(gifimage, sizeof(gifimage), "gif", 0);
851     test_LoadImageFile(gif4pixel, sizeof(gif4pixel), "gif", 0);
852     test_LoadImageFile(jpgimage, sizeof(jpgimage), "jpg", 0);
853     test_LoadImageFile(pngimage, sizeof(pngimage), "png", 0);
854 }
855
856 static void test_DestroyCursor(void)
857 {
858     static const BYTE bmp_bits[4096];
859     ICONINFO cursorInfo;
860     HCURSOR cursor, cursor2;
861     BOOL ret;
862     DWORD error;
863     UINT display_bpp;
864     HDC hdc;
865
866     hdc = GetDC(0);
867     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
868     ReleaseDC(0, hdc);
869
870     cursorInfo.fIcon = FALSE;
871     cursorInfo.xHotspot = 0;
872     cursorInfo.yHotspot = 0;
873     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
874     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
875
876     cursor = CreateIconIndirect(&cursorInfo);
877     ok(cursor != NULL, "CreateIconIndirect returned %p\n", cursor);
878     if(!cursor) {
879         return;
880     }
881     SetCursor(cursor);
882
883     SetLastError(0xdeadbeef);
884     ret = DestroyCursor(cursor);
885     ok(!ret, "DestroyCursor on the active cursor succeeded\n");
886     error = GetLastError();
887     ok(error == 0xdeadbeef, "Last error: %u\n", error);
888
889     cursor2 = GetCursor();
890     ok(cursor2 == cursor, "Active was set to %p when trying to destroy it\n", cursor2);
891
892     SetCursor(NULL);
893
894     /* Trying to destroy the cursor properly fails now with
895      * ERROR_INVALID_CURSOR_HANDLE.  This happens because we called
896      * DestroyCursor() 2+ times after calling SetCursor().  The calls to
897      * GetCursor() and SetCursor(NULL) in between make no difference. */
898     ret = DestroyCursor(cursor);
899     todo_wine {
900         ok(!ret, "DestroyCursor succeeded.\n");
901         error = GetLastError();
902         ok(error == ERROR_INVALID_CURSOR_HANDLE, "Last error: 0x%08x\n", error);
903     }
904
905     DeleteObject(cursorInfo.hbmMask);
906     DeleteObject(cursorInfo.hbmColor);
907
908     /* Try testing DestroyCursor() now using LoadCursor() cursors. */
909     cursor = LoadCursor(NULL, IDC_ARROW);
910
911     SetLastError(0xdeadbeef);
912     ret = DestroyCursor(cursor);
913     ok(ret, "DestroyCursor on the active cursor failed.\n");
914     error = GetLastError();
915     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
916
917     /* Try setting the cursor to a destroyed OEM cursor. */
918     SetLastError(0xdeadbeef);
919     SetCursor(cursor);
920     error = GetLastError();
921     todo_wine {
922         ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
923     }
924
925     /* Check if LoadCursor() returns the same handle with the same icon. */
926     cursor2 = LoadCursor(NULL, IDC_ARROW);
927     ok(cursor2 == cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
928
929     /* Check if LoadCursor() returns the same handle with a different icon. */
930     cursor2 = LoadCursor(NULL, IDC_WAIT);
931     ok(cursor2 != cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
932 }
933
934 START_TEST(cursoricon)
935 {
936     test_argc = winetest_get_mainargs(&test_argv);
937
938     if (test_argc >= 3)
939     {
940         /* Child process. */
941         sscanf (test_argv[2], "%x", (unsigned int *) &parent);
942
943         ok(parent != NULL, "Parent not found.\n");
944         if (parent == NULL)
945             ExitProcess(1);
946
947         do_child();
948         return;
949     }
950
951     test_CopyImage_Bitmap(1);
952     test_CopyImage_Bitmap(4);
953     test_CopyImage_Bitmap(8);
954     test_CopyImage_Bitmap(16);
955     test_CopyImage_Bitmap(24);
956     test_CopyImage_Bitmap(32);
957     test_initial_cursor();
958     test_CreateIcon();
959     test_LoadImage();
960     test_DestroyCursor();
961     do_parent();
962     test_child_process();
963     finish_child_process();
964 }