user32: Remove variable mask which is not really used from check_alpha_draw.
[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 BOOL (WINAPI *pGetCursorInfo)(CURSORINFO *);
68
69 static LRESULT CALLBACK callback_child(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
70 {
71     BOOL ret;
72     DWORD error;
73
74     switch (msg)
75     {
76         /* Destroy the cursor. */
77         case WM_USER+1:
78             SetLastError(0xdeadbeef);
79             ret = DestroyCursor((HCURSOR) lParam);
80             error = GetLastError();
81             todo_wine ok(!ret || broken(ret) /* win9x */, "DestroyCursor on the active cursor succeeded.\n");
82             ok(error == ERROR_DESTROY_OBJECT_OF_OTHER_THREAD ||
83                error == 0xdeadbeef,  /* vista */
84                 "Last error: %u\n", error);
85             return TRUE;
86         case WM_DESTROY:
87             PostQuitMessage(0);
88             return 0;
89     }
90
91     return DefWindowProc(hwnd, msg, wParam, lParam);
92 }
93
94 static LRESULT CALLBACK callback_parent(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
95 {
96     if (msg == PROC_INIT)
97     {
98         child = (HWND) wParam;
99         return TRUE;
100     }
101
102     return DefWindowProc(hwnd, msg, wParam, lParam);
103 }
104
105 static void do_child(void)
106 {
107     WNDCLASS class;
108     MSG msg;
109     BOOL ret;
110
111     /* Register a new class. */
112     class.style = CS_GLOBALCLASS;
113     class.lpfnWndProc = callback_child;
114     class.cbClsExtra = 0;
115     class.cbWndExtra = 0;
116     class.hInstance = GetModuleHandle(NULL);
117     class.hIcon = NULL;
118     class.hCursor = NULL;
119     class.hbrBackground = NULL;
120     class.lpszMenuName = NULL;
121     class.lpszClassName = "cursor_child";
122
123     SetLastError(0xdeadbeef);
124     ret = RegisterClass(&class);
125     ok(ret, "Failed to register window class.  Error: %u\n", GetLastError());
126
127     /* Create a window. */
128     child = CreateWindowA("cursor_child", "cursor_child", WS_POPUP | WS_VISIBLE,
129         0, 0, 200, 200, 0, 0, 0, NULL);
130     ok(child != 0, "CreateWindowA failed.  Error: %u\n", GetLastError());
131
132     /* Let the parent know our HWND. */
133     PostMessage(parent, PROC_INIT, (WPARAM) child, 0);
134
135     /* Receive messages. */
136     while ((ret = GetMessage(&msg, 0, 0, 0)))
137     {
138         ok(ret != -1, "GetMessage failed.  Error: %u\n", GetLastError());
139         TranslateMessage(&msg);
140         DispatchMessage(&msg);
141     }
142 }
143
144 static void do_parent(void)
145 {
146     char path_name[MAX_PATH];
147     PROCESS_INFORMATION info;
148     STARTUPINFOA startup;
149     WNDCLASS class;
150     MSG msg;
151     BOOL ret;
152
153     /* Register a new class. */
154     class.style = CS_GLOBALCLASS;
155     class.lpfnWndProc = callback_parent;
156     class.cbClsExtra = 0;
157     class.cbWndExtra = 0;
158     class.hInstance = GetModuleHandle(NULL);
159     class.hIcon = NULL;
160     class.hCursor = NULL;
161     class.hbrBackground = NULL;
162     class.lpszMenuName = NULL;
163     class.lpszClassName = "cursor_parent";
164
165     SetLastError(0xdeadbeef);
166     ret = RegisterClass(&class);
167     ok(ret, "Failed to register window class.  Error: %u\n", GetLastError());
168
169     /* Create a window. */
170     parent = CreateWindowA("cursor_parent", "cursor_parent", WS_POPUP | WS_VISIBLE,
171         0, 0, 200, 200, 0, 0, 0, NULL);
172     ok(parent != 0, "CreateWindowA failed.  Error: %u\n", GetLastError());
173
174     /* Start child process. */
175     memset(&startup, 0, sizeof(startup));
176     startup.cb = sizeof(startup);
177     startup.dwFlags = STARTF_USESHOWWINDOW;
178     startup.wShowWindow = SW_SHOWNORMAL;
179
180     sprintf(path_name, "%s cursoricon %lx", test_argv[0], (INT_PTR)parent);
181     ok(CreateProcessA(NULL, path_name, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess failed.\n");
182     child_process = info.hProcess;
183
184     /* Wait for child window handle. */
185     while ((child == 0) && (ret = GetMessage(&msg, parent, 0, 0)))
186     {
187         ok(ret != -1, "GetMessage failed.  Error: %u\n", GetLastError());
188         TranslateMessage(&msg);
189         DispatchMessage(&msg);
190     }
191 }
192
193 static void finish_child_process(void)
194 {
195     SendMessage(child, WM_CLOSE, 0, 0);
196     winetest_wait_child_process( child_process );
197     CloseHandle(child_process);
198 }
199
200 static void test_child_process(void)
201 {
202     static const BYTE bmp_bits[4096];
203     HCURSOR cursor;
204     ICONINFO cursorInfo;
205     UINT display_bpp;
206     HDC hdc;
207
208     /* Create and set a dummy cursor. */
209     hdc = GetDC(0);
210     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
211     ReleaseDC(0, hdc);
212
213     cursorInfo.fIcon = FALSE;
214     cursorInfo.xHotspot = 0;
215     cursorInfo.yHotspot = 0;
216     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
217     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
218
219     cursor = CreateIconIndirect(&cursorInfo);
220     ok(cursor != NULL, "CreateIconIndirect returned %p.\n", cursor);
221
222     SetCursor(cursor);
223
224     /* Destroy the cursor. */
225     SendMessage(child, WM_USER+1, 0, (LPARAM) cursor);
226 }
227
228 static void test_CopyImage_Check(HBITMAP bitmap, UINT flags, INT copyWidth, INT copyHeight,
229                                   INT expectedWidth, INT expectedHeight, WORD expectedDepth, BOOL dibExpected)
230 {
231     HBITMAP copy;
232     BITMAP origBitmap;
233     BITMAP copyBitmap;
234     BOOL orig_is_dib;
235     BOOL copy_is_dib;
236
237     copy = CopyImage(bitmap, IMAGE_BITMAP, copyWidth, copyHeight, flags);
238     ok(copy != NULL, "CopyImage() failed\n");
239     if (copy != NULL)
240     {
241         GetObject(bitmap, sizeof(origBitmap), &origBitmap);
242         GetObject(copy, sizeof(copyBitmap), &copyBitmap);
243         orig_is_dib = (origBitmap.bmBits != NULL);
244         copy_is_dib = (copyBitmap.bmBits != NULL);
245
246         if (copy_is_dib && dibExpected
247             && copyBitmap.bmBitsPixel == 24
248             && (expectedDepth == 16 || expectedDepth == 32))
249         {
250             /* Windows 95 doesn't create DIBs with a depth of 16 or 32 bit */
251             if (GetVersion() & 0x80000000)
252             {
253                 expectedDepth = 24;
254             }
255         }
256
257         if (copy_is_dib && !dibExpected && !(flags & LR_CREATEDIBSECTION))
258         {
259             /* It's not forbidden to create a DIB section if the flag
260                LR_CREATEDIBSECTION is absent.
261                Windows 9x does this if the bitmap has a depth that doesn't
262                match the screen depth, Windows NT doesn't */
263             dibExpected = TRUE;
264             expectedDepth = origBitmap.bmBitsPixel;
265         }
266
267         ok((!(dibExpected ^ copy_is_dib)
268              && (copyBitmap.bmWidth == expectedWidth)
269              && (copyBitmap.bmHeight == expectedHeight)
270              && (copyBitmap.bmBitsPixel == expectedDepth)),
271              "CopyImage ((%s, %dx%d, %u bpp), %d, %d, %#x): Expected (%s, %dx%d, %u bpp), got (%s, %dx%d, %u bpp)\n",
272                   orig_is_dib ? "DIB" : "DDB", origBitmap.bmWidth, origBitmap.bmHeight, origBitmap.bmBitsPixel,
273                   copyWidth, copyHeight, flags,
274                   dibExpected ? "DIB" : "DDB", expectedWidth, expectedHeight, expectedDepth,
275                   copy_is_dib ? "DIB" : "DDB", copyBitmap.bmWidth, copyBitmap.bmHeight, copyBitmap.bmBitsPixel);
276
277         DeleteObject(copy);
278     }
279 }
280
281 static void test_CopyImage_Bitmap(int depth)
282 {
283     HBITMAP ddb, dib;
284     HDC screenDC;
285     BITMAPINFO * info;
286     VOID * bits;
287     int screen_depth;
288     unsigned int i;
289
290     /* Create a device-independent bitmap (DIB) */
291     info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
292     info->bmiHeader.biSize = sizeof(info->bmiHeader);
293     info->bmiHeader.biWidth = 2;
294     info->bmiHeader.biHeight = 2;
295     info->bmiHeader.biPlanes = 1;
296     info->bmiHeader.biBitCount = depth;
297     info->bmiHeader.biCompression = BI_RGB;
298
299     for (i=0; i < 256; i++)
300     {
301         info->bmiColors[i].rgbRed = i;
302         info->bmiColors[i].rgbGreen = i;
303         info->bmiColors[i].rgbBlue = 255 - i;
304         info->bmiColors[i].rgbReserved = 0;
305     }
306
307     dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
308
309     /* Create a device-dependent bitmap (DDB) */
310     screenDC = GetDC(NULL);
311     screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
312     if (depth == 1 || depth == screen_depth)
313     {
314         ddb = CreateBitmap(2, 2, 1, depth, NULL);
315     }
316     else
317     {
318         ddb = NULL;
319     }
320     ReleaseDC(NULL, screenDC);
321
322     if (ddb != NULL)
323     {
324         test_CopyImage_Check(ddb, 0, 0, 0, 2, 2, depth == 1 ? 1 : screen_depth, FALSE);
325         test_CopyImage_Check(ddb, 0, 0, 5, 2, 5, depth == 1 ? 1 : screen_depth, FALSE);
326         test_CopyImage_Check(ddb, 0, 5, 0, 5, 2, depth == 1 ? 1 : screen_depth, FALSE);
327         test_CopyImage_Check(ddb, 0, 5, 5, 5, 5, depth == 1 ? 1 : screen_depth, FALSE);
328
329         test_CopyImage_Check(ddb, LR_MONOCHROME, 0, 0, 2, 2, 1, FALSE);
330         test_CopyImage_Check(ddb, LR_MONOCHROME, 5, 0, 5, 2, 1, FALSE);
331         test_CopyImage_Check(ddb, LR_MONOCHROME, 0, 5, 2, 5, 1, FALSE);
332         test_CopyImage_Check(ddb, LR_MONOCHROME, 5, 5, 5, 5, 1, FALSE);
333
334         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
335         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
336         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
337         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
338
339         /* LR_MONOCHROME is ignored if LR_CREATEDIBSECTION is present */
340         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
341         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
342         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
343         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
344
345         DeleteObject(ddb);
346     }
347
348     if (depth != 1)
349     {
350         test_CopyImage_Check(dib, 0, 0, 0, 2, 2, screen_depth, FALSE);
351         test_CopyImage_Check(dib, 0, 5, 0, 5, 2, screen_depth, FALSE);
352         test_CopyImage_Check(dib, 0, 0, 5, 2, 5, screen_depth, FALSE);
353         test_CopyImage_Check(dib, 0, 5, 5, 5, 5, screen_depth, FALSE);
354     }
355
356     test_CopyImage_Check(dib, LR_MONOCHROME, 0, 0, 2, 2, 1, FALSE);
357     test_CopyImage_Check(dib, LR_MONOCHROME, 5, 0, 5, 2, 1, FALSE);
358     test_CopyImage_Check(dib, LR_MONOCHROME, 0, 5, 2, 5, 1, FALSE);
359     test_CopyImage_Check(dib, LR_MONOCHROME, 5, 5, 5, 5, 1, FALSE);
360
361     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
362     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
363     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
364     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
365
366     /* LR_MONOCHROME is ignored if LR_CREATEDIBSECTION is present */
367     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
368     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
369     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
370     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
371
372     DeleteObject(dib);
373
374     if (depth == 1)
375     {
376         /* Special case: A monochrome DIB is converted to a monochrome DDB if
377            the colors in the color table are black and white.
378
379            Skip this test on Windows 95, it always creates a monochrome DDB
380            in this case */
381
382         if (!(GetVersion() & 0x80000000))
383         {
384             info->bmiHeader.biBitCount = 1;
385             info->bmiColors[0].rgbRed = 0xFF;
386             info->bmiColors[0].rgbGreen = 0;
387             info->bmiColors[0].rgbBlue = 0;
388             info->bmiColors[1].rgbRed = 0;
389             info->bmiColors[1].rgbGreen = 0xFF;
390             info->bmiColors[1].rgbBlue = 0;
391
392             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
393             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, screen_depth, FALSE);
394             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, screen_depth, FALSE);
395             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, screen_depth, FALSE);
396             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, screen_depth, FALSE);
397             DeleteObject(dib);
398
399             info->bmiHeader.biBitCount = 1;
400             info->bmiColors[0].rgbRed = 0;
401             info->bmiColors[0].rgbGreen = 0;
402             info->bmiColors[0].rgbBlue = 0;
403             info->bmiColors[1].rgbRed = 0xFF;
404             info->bmiColors[1].rgbGreen = 0xFF;
405             info->bmiColors[1].rgbBlue = 0xFF;
406
407             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
408             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, 1, FALSE);
409             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, 1, FALSE);
410             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, 1, FALSE);
411             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, 1, FALSE);
412             DeleteObject(dib);
413
414             info->bmiHeader.biBitCount = 1;
415             info->bmiColors[0].rgbRed = 0xFF;
416             info->bmiColors[0].rgbGreen = 0xFF;
417             info->bmiColors[0].rgbBlue = 0xFF;
418             info->bmiColors[1].rgbRed = 0;
419             info->bmiColors[1].rgbGreen = 0;
420             info->bmiColors[1].rgbBlue = 0;
421
422             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
423             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, 1, FALSE);
424             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, 1, FALSE);
425             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, 1, FALSE);
426             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, 1, FALSE);
427             DeleteObject(dib);
428         }
429     }
430
431     HeapFree(GetProcessHeap(), 0, info);
432 }
433
434 static void test_initial_cursor(void)
435 {
436     HCURSOR cursor, cursor2;
437     DWORD error;
438
439     cursor = GetCursor();
440
441     /* Check what handle GetCursor() returns if a cursor is not set yet. */
442     SetLastError(0xdeadbeef);
443     cursor2 = LoadCursor(NULL, IDC_WAIT);
444     todo_wine {
445         ok(cursor == cursor2, "cursor (%p) is not IDC_WAIT (%p).\n", cursor, cursor2);
446     }
447     error = GetLastError();
448     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
449 }
450
451 static void test_icon_info_dbg(HICON hIcon, UINT exp_cx, UINT exp_cy, UINT exp_bpp, int line)
452 {
453     ICONINFO info;
454     DWORD ret;
455     BITMAP bmMask, bmColor;
456
457     ret = GetIconInfo(hIcon, &info);
458     ok_(__FILE__, line)(ret, "GetIconInfo failed\n");
459
460     /* CreateIcon under XP causes info.fIcon to be 0 */
461     ok_(__FILE__, line)(info.xHotspot == exp_cx/2, "info.xHotspot = %u\n", info.xHotspot);
462     ok_(__FILE__, line)(info.yHotspot == exp_cy/2, "info.yHotspot = %u\n", info.yHotspot);
463     ok_(__FILE__, line)(info.hbmMask != 0, "info.hbmMask is NULL\n");
464
465     ret = GetObject(info.hbmMask, sizeof(bmMask), &bmMask);
466     ok_(__FILE__, line)(ret == sizeof(bmMask), "GetObject(info.hbmMask) failed, ret %u\n", ret);
467
468     if (exp_bpp == 1)
469         ok_(__FILE__, line)(info.hbmColor == 0, "info.hbmColor should be NULL\n");
470
471     if (info.hbmColor)
472     {
473         HDC hdc;
474         UINT display_bpp;
475
476         hdc = GetDC(0);
477         display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
478         ReleaseDC(0, hdc);
479
480         ret = GetObject(info.hbmColor, sizeof(bmColor), &bmColor);
481         ok_(__FILE__, line)(ret == sizeof(bmColor), "GetObject(info.hbmColor) failed, ret %u\n", ret);
482
483         ok_(__FILE__, line)(bmColor.bmBitsPixel == display_bpp /* XP */ ||
484            bmColor.bmBitsPixel == exp_bpp /* Win98 */,
485            "bmColor.bmBitsPixel = %d\n", bmColor.bmBitsPixel);
486         ok_(__FILE__, line)(bmColor.bmWidth == exp_cx, "bmColor.bmWidth = %d\n", bmColor.bmWidth);
487         ok_(__FILE__, line)(bmColor.bmHeight == exp_cy, "bmColor.bmHeight = %d\n", bmColor.bmHeight);
488
489         ok_(__FILE__, line)(bmMask.bmBitsPixel == 1, "bmMask.bmBitsPixel = %d\n", bmMask.bmBitsPixel);
490         ok_(__FILE__, line)(bmMask.bmWidth == exp_cx, "bmMask.bmWidth = %d\n", bmMask.bmWidth);
491         ok_(__FILE__, line)(bmMask.bmHeight == exp_cy, "bmMask.bmHeight = %d\n", bmMask.bmHeight);
492     }
493     else
494     {
495         ok_(__FILE__, line)(bmMask.bmBitsPixel == 1, "bmMask.bmBitsPixel = %d\n", bmMask.bmBitsPixel);
496         ok_(__FILE__, line)(bmMask.bmWidth == exp_cx, "bmMask.bmWidth = %d\n", bmMask.bmWidth);
497         ok_(__FILE__, line)(bmMask.bmHeight == exp_cy * 2, "bmMask.bmHeight = %d\n", bmMask.bmHeight);
498     }
499 }
500
501 #define test_icon_info(a,b,c,d) test_icon_info_dbg((a),(b),(c),(d),__LINE__)
502
503 static void test_CreateIcon(void)
504 {
505     static const BYTE bmp_bits[1024];
506     HICON hIcon;
507     HBITMAP hbmMask, hbmColor;
508     BITMAPINFO *bmpinfo;
509     ICONINFO info;
510     HDC hdc;
511     void *bits;
512     UINT display_bpp;
513
514     hdc = GetDC(0);
515     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
516
517     /* these crash under XP
518     hIcon = CreateIcon(0, 16, 16, 1, 1, bmp_bits, NULL);
519     hIcon = CreateIcon(0, 16, 16, 1, 1, NULL, bmp_bits);
520     */
521
522     hIcon = CreateIcon(0, 16, 16, 1, 1, bmp_bits, bmp_bits);
523     ok(hIcon != 0, "CreateIcon failed\n");
524     test_icon_info(hIcon, 16, 16, 1);
525     DestroyIcon(hIcon);
526
527     hIcon = CreateIcon(0, 16, 16, 1, display_bpp, bmp_bits, bmp_bits);
528     ok(hIcon != 0, "CreateIcon failed\n");
529     test_icon_info(hIcon, 16, 16, display_bpp);
530     DestroyIcon(hIcon);
531
532     hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
533     ok(hbmMask != 0, "CreateBitmap failed\n");
534     hbmColor = CreateBitmap(16, 16, 1, display_bpp, bmp_bits);
535     ok(hbmColor != 0, "CreateBitmap failed\n");
536
537     info.fIcon = TRUE;
538     info.xHotspot = 8;
539     info.yHotspot = 8;
540     info.hbmMask = 0;
541     info.hbmColor = 0;
542     SetLastError(0xdeadbeaf);
543     hIcon = CreateIconIndirect(&info);
544     ok(!hIcon, "CreateIconIndirect should fail\n");
545     ok(GetLastError() == 0xdeadbeaf, "wrong error %u\n", GetLastError());
546
547     info.fIcon = TRUE;
548     info.xHotspot = 8;
549     info.yHotspot = 8;
550     info.hbmMask = 0;
551     info.hbmColor = hbmColor;
552     SetLastError(0xdeadbeaf);
553     hIcon = CreateIconIndirect(&info);
554     ok(!hIcon, "CreateIconIndirect should fail\n");
555     ok(GetLastError() == 0xdeadbeaf, "wrong error %u\n", GetLastError());
556
557     info.fIcon = TRUE;
558     info.xHotspot = 8;
559     info.yHotspot = 8;
560     info.hbmMask = hbmMask;
561     info.hbmColor = hbmColor;
562     hIcon = CreateIconIndirect(&info);
563     ok(hIcon != 0, "CreateIconIndirect failed\n");
564     test_icon_info(hIcon, 16, 16, display_bpp);
565     DestroyIcon(hIcon);
566
567     DeleteObject(hbmMask);
568     DeleteObject(hbmColor);
569
570     hbmMask = CreateBitmap(16, 32, 1, 1, bmp_bits);
571     ok(hbmMask != 0, "CreateBitmap failed\n");
572
573     info.fIcon = TRUE;
574     info.xHotspot = 8;
575     info.yHotspot = 8;
576     info.hbmMask = hbmMask;
577     info.hbmColor = 0;
578     SetLastError(0xdeadbeaf);
579     hIcon = CreateIconIndirect(&info);
580     ok(hIcon != 0, "CreateIconIndirect failed\n");
581     test_icon_info(hIcon, 16, 16, 1);
582     DestroyIcon(hIcon);
583
584     DeleteObject(hbmMask);
585     DeleteObject(hbmColor);
586
587     /* test creating an icon from a DIB section */
588
589     bmpinfo = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(BITMAPINFO,bmiColors[256]));
590     bmpinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
591     bmpinfo->bmiHeader.biWidth = 32;
592     bmpinfo->bmiHeader.biHeight = 32;
593     bmpinfo->bmiHeader.biPlanes = 1;
594     bmpinfo->bmiHeader.biBitCount = 8;
595     bmpinfo->bmiHeader.biCompression = BI_RGB;
596     hbmColor = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
597     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
598     if (bits)
599         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
600     bmpinfo->bmiHeader.biBitCount = 1;
601     hbmMask = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
602     ok(hbmMask != NULL, "Expected a handle to the DIB\n");
603     if (bits)
604         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
605
606     info.fIcon = TRUE;
607     info.xHotspot = 8;
608     info.yHotspot = 8;
609     info.hbmMask = hbmColor;
610     info.hbmColor = hbmMask;
611     SetLastError(0xdeadbeaf);
612     hIcon = CreateIconIndirect(&info);
613     ok(hIcon != 0, "CreateIconIndirect failed\n");
614     test_icon_info(hIcon, 32, 32, 8);
615     DestroyIcon(hIcon);
616     DeleteObject(hbmColor);
617
618     bmpinfo->bmiHeader.biBitCount = 16;
619     hbmColor = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
620     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
621     if (bits)
622         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
623
624     info.fIcon = TRUE;
625     info.xHotspot = 8;
626     info.yHotspot = 8;
627     info.hbmMask = hbmColor;
628     info.hbmColor = hbmMask;
629     SetLastError(0xdeadbeaf);
630     hIcon = CreateIconIndirect(&info);
631     ok(hIcon != 0, "CreateIconIndirect failed\n");
632     test_icon_info(hIcon, 32, 32, 8);
633     DestroyIcon(hIcon);
634     DeleteObject(hbmColor);
635
636     bmpinfo->bmiHeader.biBitCount = 32;
637     hbmColor = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
638     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
639     if (bits)
640         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
641
642     info.fIcon = TRUE;
643     info.xHotspot = 8;
644     info.yHotspot = 8;
645     info.hbmMask = hbmColor;
646     info.hbmColor = hbmMask;
647     SetLastError(0xdeadbeaf);
648     hIcon = CreateIconIndirect(&info);
649     ok(hIcon != 0, "CreateIconIndirect failed\n");
650     test_icon_info(hIcon, 32, 32, 8);
651     DestroyIcon(hIcon);
652
653     DeleteObject(hbmMask);
654     DeleteObject(hbmColor);
655     HeapFree( GetProcessHeap(), 0, bmpinfo );
656
657     ReleaseDC(0, hdc);
658 }
659
660 /* Shamelessly ripped from dlls/oleaut32/tests/olepicture.c */
661 /* 1x1 pixel gif */
662 static const unsigned char gifimage[35] = {
663 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
664 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
665 0x01,0x00,0x3b
666 };
667
668 /* 1x1 pixel jpg */
669 static const unsigned char jpgimage[285] = {
670 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
671 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
672 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
673 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
674 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
675 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
676 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
677 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
678 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
679 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0xff,0xc0,
680 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
681 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
682 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
683 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
684 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
685 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
686 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
687 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
688 };
689
690 /* 1x1 pixel png */
691 static const unsigned char pngimage[285] = {
692 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
693 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
694 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
695 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
696 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
697 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
698 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
699 };
700
701 /* 1x1 pixel bmp */
702 static const unsigned char bmpimage[66] = {
703 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
704 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
705 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
706 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
707 0x00,0x00
708 };
709
710 /* 2x2 pixel gif */
711 static const unsigned char gif4pixel[42] = {
712 0x47,0x49,0x46,0x38,0x37,0x61,0x02,0x00,0x02,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,
713 0x39,0x62,0xfc,0xff,0x1a,0xe5,0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x02,0x00,
714 0x02,0x00,0x00,0x02,0x03,0x14,0x16,0x05,0x00,0x3b
715 };
716
717 static void test_LoadImageFile(const unsigned char * image_data,
718     unsigned int image_size, const char * ext, BOOL expect_success)
719 {
720     HANDLE handle;
721     BOOL ret;
722     DWORD error, bytes_written;
723     char filename[64];
724
725     strcpy(filename, "test.");
726     strcat(filename, ext);
727
728     /* Create the test image. */
729     handle = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
730         FILE_ATTRIBUTE_NORMAL, NULL);
731     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
732     ret = WriteFile(handle, image_data, image_size, &bytes_written, NULL);
733     ok(bytes_written == image_size, "test file created improperly.\n");
734     CloseHandle(handle);
735
736     /* Load as cursor. For all tested formats, this should fail */
737     SetLastError(0xdeadbeef);
738     handle = LoadImageA(NULL, filename, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
739     ok(handle == NULL, "LoadImage(%s) as IMAGE_CURSOR succeeded incorrectly.\n", ext);
740     error = GetLastError();
741     ok(error == 0 ||
742         broken(error == 0xdeadbeef) || /* Win9x */
743         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
744         "Last error: %u\n", error);
745     if (handle != NULL) DestroyCursor(handle);
746
747     /* Load as icon. For all tested formats, this should fail */
748     SetLastError(0xdeadbeef);
749     handle = LoadImageA(NULL, filename, IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
750     ok(handle == NULL, "LoadImage(%s) as IMAGE_ICON succeeded incorrectly.\n", ext);
751     error = GetLastError();
752     ok(error == 0 ||
753         broken(error == 0xdeadbeef) || /* Win9x */
754         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
755         "Last error: %u\n", error);
756     if (handle != NULL) DestroyIcon(handle);
757
758     /* Load as bitmap. Should succeed if bmp, fail for everything else */
759     SetLastError(0xdeadbeef);
760     handle = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
761     if (expect_success)
762         ok(handle != NULL, "LoadImage(%s) as IMAGE_BITMAP failed.\n", ext);
763     else ok(handle == NULL, "LoadImage(%s) as IMAGE_BITMAP succeeded incorrectly.\n", ext);
764     error = GetLastError();
765     ok(error == 0 ||
766         error == 0xdeadbeef, /* Win9x, WinMe */
767         "Last error: %u\n", error);
768     if (handle != NULL) DeleteObject(handle);
769
770     DeleteFileA(filename);
771 }
772
773 static void test_LoadImage(void)
774 {
775     HANDLE handle;
776     BOOL ret;
777     DWORD error, bytes_written;
778     CURSORICONFILEDIR *icon_data;
779     CURSORICONFILEDIRENTRY *icon_entry;
780     BITMAPINFOHEADER *icon_header;
781     ICONINFO icon_info;
782
783 #define ICON_WIDTH 32
784 #define ICON_HEIGHT 32
785 #define ICON_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
786 #define ICON_BPP 32
787 #define ICON_SIZE \
788     (sizeof(CURSORICONFILEDIR) + sizeof(BITMAPINFOHEADER) \
789     + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
790
791     /* Set icon data. */
792     icon_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ICON_SIZE);
793     icon_data->idReserved = 0;
794     icon_data->idType = 1;
795     icon_data->idCount = 1;
796
797     icon_entry = icon_data->idEntries;
798     icon_entry->bWidth = ICON_WIDTH;
799     icon_entry->bHeight = ICON_HEIGHT;
800     icon_entry->bColorCount = 0;
801     icon_entry->bReserved = 0;
802     icon_entry->xHotspot = 1;
803     icon_entry->yHotspot = 1;
804     icon_entry->dwDIBSize = ICON_SIZE - sizeof(CURSORICONFILEDIR);
805     icon_entry->dwDIBOffset = sizeof(CURSORICONFILEDIR);
806
807     icon_header = (BITMAPINFOHEADER *) ((BYTE *) icon_data + icon_entry->dwDIBOffset);
808     icon_header->biSize = sizeof(BITMAPINFOHEADER);
809     icon_header->biWidth = ICON_WIDTH;
810     icon_header->biHeight = ICON_HEIGHT*2;
811     icon_header->biPlanes = 1;
812     icon_header->biBitCount = ICON_BPP;
813     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
814
815     /* Create the icon. */
816     handle = CreateFileA("icon.ico", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
817         FILE_ATTRIBUTE_NORMAL, NULL);
818     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
819     ret = WriteFile(handle, icon_data, ICON_SIZE, &bytes_written, NULL);
820     ok(bytes_written == ICON_SIZE, "icon.ico created improperly.\n");
821     CloseHandle(handle);
822
823     /* Test loading an icon as a cursor. */
824     SetLastError(0xdeadbeef);
825     handle = LoadImageA(NULL, "icon.ico", IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
826     ok(handle != NULL, "LoadImage() failed.\n");
827     error = GetLastError();
828     ok(error == 0 ||
829         broken(error == 0xdeadbeef) || /* Win9x */
830         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
831         "Last error: %u\n", error);
832
833     /* Test the icon information. */
834     SetLastError(0xdeadbeef);
835     ret = GetIconInfo(handle, &icon_info);
836     ok(ret, "GetIconInfo() failed.\n");
837     error = GetLastError();
838     ok(error == 0xdeadbeef, "Last error: %u\n", error);
839
840     if (ret)
841     {
842         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
843         ok(icon_info.xHotspot == 1, "xHotspot is %u.\n", icon_info.xHotspot);
844         ok(icon_info.yHotspot == 1, "yHotspot is %u.\n", icon_info.yHotspot);
845         ok(icon_info.hbmColor != NULL || broken(!icon_info.hbmColor) /* no color cursor support */,
846            "No hbmColor!\n");
847         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
848     }
849
850     /* Clean up. */
851     SetLastError(0xdeadbeef);
852     ret = DestroyCursor(handle);
853     ok(ret, "DestroyCursor() failed.\n");
854     error = GetLastError();
855     ok(error == 0xdeadbeef, "Last error: %u\n", error);
856
857     HeapFree(GetProcessHeap(), 0, icon_data);
858     DeleteFileA("icon.ico");
859
860     test_LoadImageFile(bmpimage, sizeof(bmpimage), "bmp", 1);
861     test_LoadImageFile(gifimage, sizeof(gifimage), "gif", 0);
862     test_LoadImageFile(gif4pixel, sizeof(gif4pixel), "gif", 0);
863     test_LoadImageFile(jpgimage, sizeof(jpgimage), "jpg", 0);
864     test_LoadImageFile(pngimage, sizeof(pngimage), "png", 0);
865 }
866
867 static void test_CreateIconFromResource(void)
868 {
869     HANDLE handle;
870     BOOL ret;
871     DWORD error;
872     BITMAPINFOHEADER *icon_header;
873     INT16 *hotspot;
874     ICONINFO icon_info;
875
876 #define ICON_RES_WIDTH 32
877 #define ICON_RES_HEIGHT 32
878 #define ICON_RES_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
879 #define ICON_RES_BPP 32
880 #define ICON_RES_SIZE \
881     (sizeof(BITMAPINFOHEADER) + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
882 #define CRSR_RES_SIZE (2*sizeof(INT16) + ICON_RES_SIZE)
883
884     /* Set icon data. */
885     hotspot = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, CRSR_RES_SIZE);
886
887     /* Cursor resources have an extra hotspot, icon resources not. */
888     hotspot[0] = 3;
889     hotspot[1] = 3;
890
891     icon_header = (BITMAPINFOHEADER *) (hotspot + 2);
892     icon_header->biSize = sizeof(BITMAPINFOHEADER);
893     icon_header->biWidth = ICON_WIDTH;
894     icon_header->biHeight = ICON_HEIGHT*2;
895     icon_header->biPlanes = 1;
896     icon_header->biBitCount = ICON_BPP;
897     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
898
899     /* Test creating a cursor. */
900     SetLastError(0xdeadbeef);
901     handle = CreateIconFromResource((PBYTE) hotspot, CRSR_RES_SIZE, FALSE, 0x00030000);
902     ok(handle != NULL, "Create cursor failed.\n");
903
904     /* Test the icon information. */
905     SetLastError(0xdeadbeef);
906     ret = GetIconInfo(handle, &icon_info);
907     ok(ret, "GetIconInfo() failed.\n");
908     error = GetLastError();
909     ok(error == 0xdeadbeef, "Last error: %u\n", error);
910
911     if (ret)
912     {
913         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
914         ok(icon_info.xHotspot == 3, "xHotspot is %u.\n", icon_info.xHotspot);
915         ok(icon_info.yHotspot == 3, "yHotspot is %u.\n", icon_info.yHotspot);
916         ok(icon_info.hbmColor != NULL || broken(!icon_info.hbmColor) /* no color cursor support */,
917            "No hbmColor!\n");
918         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
919     }
920
921     /* Clean up. */
922     SetLastError(0xdeadbeef);
923     ret = DestroyCursor(handle);
924     ok(ret, "DestroyCursor() failed.\n");
925     error = GetLastError();
926     ok(error == 0xdeadbeef, "Last error: %u\n", error);
927
928     /* Test creating an icon. */
929     SetLastError(0xdeadbeef);
930     handle = CreateIconFromResource((PBYTE) icon_header, ICON_RES_SIZE, TRUE,
931                                     0x00030000);
932     ok(handle != NULL, "Create icon failed.\n");
933
934     /* Test the icon information. */
935     SetLastError(0xdeadbeef);
936     ret = GetIconInfo(handle, &icon_info);
937     ok(ret, "GetIconInfo() failed.\n");
938     error = GetLastError();
939     ok(error == 0xdeadbeef, "Last error: %u\n", error);
940
941     if (ret)
942     {
943         ok(icon_info.fIcon == TRUE, "fIcon != TRUE.\n");
944         /* Icons always have hotspot in the middle */
945         ok(icon_info.xHotspot == ICON_WIDTH/2, "xHotspot is %u.\n", icon_info.xHotspot);
946         ok(icon_info.yHotspot == ICON_HEIGHT/2, "yHotspot is %u.\n", icon_info.yHotspot);
947         ok(icon_info.hbmColor != NULL, "No hbmColor!\n");
948         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
949     }
950
951     /* Clean up. */
952     SetLastError(0xdeadbeef);
953     ret = DestroyCursor(handle);
954     ok(ret, "DestroyCursor() failed.\n");
955     error = GetLastError();
956     ok(error == 0xdeadbeef, "Last error: %u\n", error);
957
958     HeapFree(GetProcessHeap(), 0, hotspot);
959 }
960
961 static HICON create_test_icon(HDC hdc, int width, int height, int bpp,
962                               BOOL maskvalue, UINT32 *color, int colorSize)
963 {
964     ICONINFO iconInfo;
965     BITMAPINFO bitmapInfo;
966     void *buffer = NULL;
967     UINT32 mask = maskvalue ? 0xFFFFFFFF : 0x00000000;
968
969     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
970     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
971     bitmapInfo.bmiHeader.biWidth = width;
972     bitmapInfo.bmiHeader.biHeight = height;
973     bitmapInfo.bmiHeader.biPlanes = 1;
974     bitmapInfo.bmiHeader.biBitCount = bpp;
975     bitmapInfo.bmiHeader.biCompression = BI_RGB;
976     bitmapInfo.bmiHeader.biSizeImage = colorSize;
977
978     iconInfo.fIcon = TRUE;
979     iconInfo.xHotspot = 0;
980     iconInfo.yHotspot = 0;
981
982     iconInfo.hbmMask = CreateBitmap( width, height, 1, 1, &mask );
983     if(!iconInfo.hbmMask) return NULL;
984
985     iconInfo.hbmColor = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, &buffer, NULL, 0);
986     if(!iconInfo.hbmColor || !buffer)
987     {
988         DeleteObject(iconInfo.hbmMask);
989         return NULL;
990     }
991
992     memcpy(buffer, color, colorSize);
993
994     return CreateIconIndirect(&iconInfo);
995 }
996
997 static BOOL color_match(COLORREF a, COLORREF b)
998 {
999     /* 5-bit accuracy is a sufficient test. This will match, so long as
1000      * colors are never truncated to less that 3x5-bit accuracy i.e.
1001      * paletized. */
1002     return (a & 0x00F8F8F8) == (b & 0x00F8F8F8);
1003 }
1004
1005 static void check_alpha_draw(HDC hdc, BOOL drawiconex, BOOL alpha, int bpp, int line)
1006 {
1007     HICON hicon;
1008     UINT32 color[2];
1009     COLORREF modern_expected, legacy_expected, result;
1010
1011     color[0] = 0x00A0B0C0;
1012     color[1] = alpha ? 0xFF000000 : 0x00000000;
1013     modern_expected = alpha ? 0x00FFFFFF : 0x00C0B0A0;
1014     legacy_expected = 0x00C0B0A0;
1015
1016     hicon = create_test_icon(hdc, 2, 1, bpp, 0, color, sizeof(color));
1017     if (!hicon) return;
1018
1019     SetPixelV(hdc, 0, 0, 0x00FFFFFF);
1020
1021     if(drawiconex)
1022         DrawIconEx(hdc, 0, 0, hicon, 2, 1, 0, NULL, DI_NORMAL);
1023     else
1024         DrawIcon(hdc, 0, 0, hicon);
1025
1026     result = GetPixel(hdc, 0, 0);
1027     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1028         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1029         "%s. Expected a close match to %06X (modern) or %06X (legacy) with %s. "
1030         "Got %06X from line %d\n",
1031         alpha ? "Alpha blending" : "Not alpha blending", modern_expected, legacy_expected,
1032         drawiconex ? "DrawIconEx" : "DrawIcon", result, line);
1033 }
1034
1035 static void check_DrawIcon(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, COLORREF background,
1036                            COLORREF modern_expected, COLORREF legacy_expected, int line)
1037 {
1038     COLORREF result;
1039     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1040     if (!hicon) return;
1041     SetPixelV(hdc, 0, 0, background);
1042     SetPixelV(hdc, GetSystemMetrics(SM_CXICON)-1, GetSystemMetrics(SM_CYICON)-1, background);
1043     SetPixelV(hdc, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), background);
1044     DrawIcon(hdc, 0, 0, hicon);
1045     result = GetPixel(hdc, 0, 0);
1046
1047     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1048         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1049         "Overlaying Mask %d on Color %06X with DrawIcon. "
1050         "Expected a close match to %06X (modern), or %06X (legacy). Got %06X from line %d\n",
1051         maskvalue, color, modern_expected, legacy_expected, result, line);
1052
1053     result = GetPixel(hdc, GetSystemMetrics(SM_CXICON)-1, GetSystemMetrics(SM_CYICON)-1);
1054
1055     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1056         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1057         "Overlaying Mask %d on Color %06X with DrawIcon. "
1058         "Expected a close match to %06X (modern), or %06X (legacy). Got %06X from line %d\n",
1059         maskvalue, color, modern_expected, legacy_expected, result, line);
1060
1061     result = GetPixel(hdc, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
1062
1063     ok (color_match(result, background),
1064         "Overlaying Mask %d on Color %06X with DrawIcon. "
1065         "Expected unchanged background color %06X. Got %06X from line %d\n",
1066         maskvalue, color, background, result, line);
1067 }
1068
1069 static void test_DrawIcon(void)
1070 {
1071     BITMAPINFO bitmapInfo;
1072     HDC hdcDst = NULL;
1073     HBITMAP bmpDst = NULL;
1074     HBITMAP bmpOld = NULL;
1075     void *bits = 0;
1076
1077     hdcDst = CreateCompatibleDC(0);
1078     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1079     if (!hdcDst)
1080         return;
1081
1082     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1083     {
1084         skip("Windows will distort DrawIcon colors at 8-bpp and less due to palletizing.\n");
1085         goto cleanup;
1086     }
1087
1088     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1089     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1090     bitmapInfo.bmiHeader.biWidth = GetSystemMetrics(SM_CXICON)+1;
1091     bitmapInfo.bmiHeader.biHeight = GetSystemMetrics(SM_CYICON)+1;
1092     bitmapInfo.bmiHeader.biBitCount = 32;
1093     bitmapInfo.bmiHeader.biPlanes = 1;
1094     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1095     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1096
1097     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1098     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1099     if (!bmpDst || !bits)
1100         goto cleanup;
1101     bmpOld = SelectObject(hdcDst, bmpDst);
1102
1103     /* Mask is only heeded if alpha channel is always zero */
1104     check_DrawIcon(hdcDst, FALSE, 0x00A0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1105     check_DrawIcon(hdcDst, TRUE, 0x00A0B0C0, 32, 0x00FFFFFF, 0x003F4F5F, 0x003F4F5F, __LINE__);
1106
1107     /* Test alpha blending */
1108     /* Windows 2000 and up will alpha blend, earlier Windows versions will not */
1109     check_DrawIcon(hdcDst, FALSE, 0xFFA0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1110     check_DrawIcon(hdcDst, TRUE, 0xFFA0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x003F4F5F, __LINE__);
1111
1112     check_DrawIcon(hdcDst, FALSE, 0x80A0B0C0, 32, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1113     check_DrawIcon(hdcDst, TRUE, 0x80A0B0C0, 32, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1114     check_DrawIcon(hdcDst, FALSE, 0x80A0B0C0, 32, 0x00FFFFFF, 0x00DFD7CF, 0x00C0B0A0, __LINE__);
1115     check_DrawIcon(hdcDst, TRUE, 0x80A0B0C0, 32, 0x00FFFFFF, 0x00DFD7CF, 0x003F4F5F, __LINE__);
1116
1117     check_DrawIcon(hdcDst, FALSE, 0x01FFFFFF, 32, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1118     check_DrawIcon(hdcDst, TRUE, 0x01FFFFFF, 32, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1119
1120     /* Test detecting of alpha channel */
1121     /* If a single pixel's alpha channel is non-zero, the icon
1122        will be alpha blended, otherwise it will be draw with
1123        and + xor blts. */
1124     check_alpha_draw(hdcDst, FALSE, FALSE, 32, __LINE__);
1125     check_alpha_draw(hdcDst, FALSE, TRUE, 32, __LINE__);
1126
1127 cleanup:
1128     if(bmpOld)
1129         SelectObject(hdcDst, bmpOld);
1130     if(bmpDst)
1131         DeleteObject(bmpDst);
1132     if(hdcDst)
1133         DeleteDC(hdcDst);
1134 }
1135
1136 static void check_DrawIconEx(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, UINT flags, COLORREF background,
1137                              COLORREF modern_expected, COLORREF legacy_expected, int line)
1138 {
1139     COLORREF result;
1140     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1141     if (!hicon) return;
1142     SetPixelV(hdc, 0, 0, background);
1143     DrawIconEx(hdc, 0, 0, hicon, 1, 1, 0, NULL, flags);
1144     result = GetPixel(hdc, 0, 0);
1145
1146     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1147         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1148         "Overlaying Mask %d on Color %06X with DrawIconEx flags %08X. "
1149         "Expected a close match to %06X (modern) or %06X (legacy). Got %06X from line %d\n",
1150         maskvalue, color, flags, modern_expected, legacy_expected, result, line);
1151 }
1152
1153 static void test_DrawIconEx(void)
1154 {
1155     BITMAPINFO bitmapInfo;
1156     HDC hdcDst = NULL;
1157     HBITMAP bmpDst = NULL;
1158     HBITMAP bmpOld = NULL;
1159     void *bits = 0;
1160
1161     hdcDst = CreateCompatibleDC(0);
1162     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1163     if (!hdcDst)
1164         return;
1165
1166     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1167     {
1168         skip("Windows will distort DrawIconEx colors at 8-bpp and less due to palletizing.\n");
1169         goto cleanup;
1170     }
1171
1172     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1173     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1174     bitmapInfo.bmiHeader.biWidth = 1;
1175     bitmapInfo.bmiHeader.biHeight = 1;
1176     bitmapInfo.bmiHeader.biBitCount = 32;
1177     bitmapInfo.bmiHeader.biPlanes = 1;
1178     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1179     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1180     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1181     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1182     if (!bmpDst || !bits)
1183         goto cleanup;
1184     bmpOld = SelectObject(hdcDst, bmpDst);
1185
1186     /* Test null, image only, and mask only drawing */
1187     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, 0, 0x00102030, 0x00102030, 0x00102030, __LINE__);
1188     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, 0, 0x00102030, 0x00102030, 0x00102030, __LINE__);
1189
1190     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_MASK, 0x00FFFFFF, 0x00000000, 0x00000000, __LINE__);
1191     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_MASK, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, __LINE__);
1192
1193     todo_wine
1194     {
1195         check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, DI_IMAGE, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1196         check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, DI_IMAGE, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1197     }
1198
1199     /* Test normal drawing */
1200     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1201     todo_wine check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x003F4F5F, 0x003F4F5F, __LINE__);
1202     check_DrawIconEx(hdcDst, FALSE, 0xFFA0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1203
1204     /* Test alpha blending */
1205     /* Windows 2000 and up will alpha blend, earlier Windows versions will not */
1206     check_DrawIconEx(hdcDst, TRUE, 0xFFA0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x003F4F5F, __LINE__);
1207
1208     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_NORMAL, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1209     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_NORMAL, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1210     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00DFD7CF, 0x00C0B0A0, __LINE__);
1211     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00DFD7CF, 0x003F4F5F, __LINE__);
1212
1213     check_DrawIconEx(hdcDst, FALSE, 0x01FFFFFF, 32, DI_NORMAL, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1214     check_DrawIconEx(hdcDst, TRUE, 0x01FFFFFF, 32, DI_NORMAL, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1215
1216     /* Test detecting of alpha channel */
1217     /* If a single pixel's alpha channel is non-zero, the icon
1218        will be alpha blended, otherwise it will be draw with
1219        and + xor blts. */
1220     check_alpha_draw(hdcDst, TRUE, FALSE, 32, __LINE__);
1221     check_alpha_draw(hdcDst, TRUE, TRUE, 32, __LINE__);
1222
1223 cleanup:
1224     if(bmpOld)
1225         SelectObject(hdcDst, bmpOld);
1226     if(bmpDst)
1227         DeleteObject(bmpDst);
1228     if(hdcDst)
1229         DeleteDC(hdcDst);
1230 }
1231
1232 static void check_DrawState_Size(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, HBRUSH hbr, UINT flags, int line)
1233 {
1234     COLORREF result, background;
1235     BOOL passed[2];
1236     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1237     background = 0x00FFFFFF;
1238     /* Set color of the 2 pixels that will be checked afterwards */
1239     SetPixelV(hdc, 0, 0, background);
1240     SetPixelV(hdc, 2, 2, background);
1241
1242     /* Let DrawState calculate the size of the icon (it's 1x1) */
1243     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 0, 0, (DST_ICON | flags ));
1244
1245     result = GetPixel(hdc, 0, 0);
1246     passed[0] = color_match(result, background);
1247     result = GetPixel(hdc, 2, 2);
1248     passed[0] = passed[0] & color_match(result, background);
1249
1250     /* Check if manually specifying the icon size DOESN'T work */
1251
1252     /* IMPORTANT: For Icons, DrawState wants the size of the source image, not the
1253      *            size in which it should be ultimately drawn. Therefore giving
1254      *            width/height 2x2 if the icon is only 1x1 pixels in size should
1255      *            result in drawing it with size 1x1. The size parameters must be
1256      *            ignored if a Icon has to be drawn! */
1257     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 2, 2, (DST_ICON | flags ));
1258
1259     result = GetPixel(hdc, 0, 0);
1260     passed[1] = color_match(result, background);
1261     result = GetPixel(hdc, 2, 2);
1262     passed[1] = passed[0] & color_match(result, background);
1263
1264     if(!passed[0]&&!passed[1])
1265         ok (passed[1],
1266         "DrawState failed to draw a 1x1 Icon in the correct size, independent of the "
1267         "width and height settings passed to it, for Icon with: Overlaying Mask %d on "
1268         "Color %06X with flags %08X. Line %d\n",
1269         maskvalue, color, (DST_ICON | flags), line);
1270     else if(!passed[1])
1271         ok (passed[1],
1272         "DrawState failed to draw a 1x1 Icon in the correct size, if the width and height "
1273         "parameters passed to it are bigger than the real Icon size, for Icon with: Overlaying "
1274         "Mask %d on Color %06X with flags %08X. Line %d\n",
1275         maskvalue, color, (DST_ICON | flags), line);
1276     else
1277         ok (passed[0],
1278         "DrawState failed to draw a 1x1 Icon in the correct size, if the width and height "
1279         "parameters passed to it are 0, for Icon with: Overlaying Mask %d on "
1280         "Color %06X with flags %08X. Line %d\n",
1281         maskvalue, color, (DST_ICON | flags), line);
1282 }
1283
1284 static void check_DrawState_Color(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, HBRUSH hbr, UINT flags,
1285                              COLORREF background, COLORREF modern_expected, COLORREF legacy_expected, int line)
1286 {
1287     COLORREF result;
1288     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1289     if (!hicon) return;
1290     /* Set color of the pixel that will be checked afterwards */
1291     SetPixelV(hdc, 1, 1, background);
1292
1293     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 0, 0, ( DST_ICON | flags ));
1294
1295     /* Check the color of the pixel is correct */
1296     result = GetPixel(hdc, 1, 1);
1297
1298     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1299         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1300         "DrawState drawing Icon with Overlaying Mask %d on Color %06X with flags %08X. "
1301         "Expected a close match to %06X (modern) or %06X (legacy). Got %06X from line %d\n",
1302         maskvalue, color, (DST_ICON | flags), modern_expected, legacy_expected, result, line);
1303 }
1304
1305 static void test_DrawState(void)
1306 {
1307     BITMAPINFO bitmapInfo;
1308     HDC hdcDst = NULL;
1309     HBITMAP bmpDst = NULL;
1310     HBITMAP bmpOld = NULL;
1311     void *bits = 0;
1312
1313     hdcDst = CreateCompatibleDC(0);
1314     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1315     if (!hdcDst)
1316         return;
1317
1318     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1319     {
1320         skip("Windows will distort DrawIconEx colors at 8-bpp and less due to palletizing.\n");
1321         goto cleanup;
1322     }
1323
1324     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1325     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1326     bitmapInfo.bmiHeader.biWidth = 3;
1327     bitmapInfo.bmiHeader.biHeight = 3;
1328     bitmapInfo.bmiHeader.biBitCount = 32;
1329     bitmapInfo.bmiHeader.biPlanes = 1;
1330     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1331     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1332     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1333     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1334     if (!bmpDst || !bits)
1335         goto cleanup;
1336     bmpOld = SelectObject(hdcDst, bmpDst);
1337
1338     /* potential flags to test with DrawState are: */
1339     /* DSS_DISABLED embosses the icon */
1340     /* DSS_MONO draw Icon using a brush as parameter 5 */
1341     /* DSS_NORMAL draw Icon without any modifications */
1342     /* DSS_UNION draw the Icon dithered */
1343
1344     check_DrawState_Size(hdcDst, FALSE, 0x00A0B0C0, 32, 0, DSS_NORMAL, __LINE__);
1345     check_DrawState_Color(hdcDst, FALSE, 0x00A0B0C0, 32, 0, DSS_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1346
1347 cleanup:
1348     if(bmpOld)
1349         SelectObject(hdcDst, bmpOld);
1350     if(bmpDst)
1351         DeleteObject(bmpDst);
1352     if(hdcDst)
1353         DeleteDC(hdcDst);
1354 }
1355
1356 static DWORD parent_id;
1357
1358 static DWORD CALLBACK set_cursor_thread( void *arg )
1359 {
1360     HCURSOR ret;
1361
1362     PeekMessage( 0, 0, 0, 0, PM_NOREMOVE );  /* create a msg queue */
1363     if (parent_id)
1364     {
1365         BOOL ret = AttachThreadInput( GetCurrentThreadId(), parent_id, TRUE );
1366         ok( ret, "AttachThreadInput failed\n" );
1367     }
1368     if (arg) ret = SetCursor( (HCURSOR)arg );
1369     else ret = GetCursor();
1370     return (DWORD_PTR)ret;
1371 }
1372
1373 static void test_SetCursor(void)
1374 {
1375     static const BYTE bmp_bits[4096];
1376     ICONINFO cursorInfo;
1377     HCURSOR cursor, old_cursor, global_cursor = 0;
1378     DWORD error, id, result;
1379     UINT display_bpp;
1380     HDC hdc;
1381     HANDLE thread;
1382     CURSORINFO info;
1383
1384     if (pGetCursorInfo)
1385     {
1386         memset( &info, 0, sizeof(info) );
1387         info.cbSize = sizeof(info);
1388         if (!pGetCursorInfo( &info ))
1389         {
1390             win_skip( "GetCursorInfo not working\n" );
1391             pGetCursorInfo = NULL;
1392         }
1393         else global_cursor = info.hCursor;
1394     }
1395     cursor = GetCursor();
1396     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
1397     WaitForSingleObject( thread, 1000 );
1398     GetExitCodeThread( thread, &result );
1399     ok( result == (DWORD_PTR)cursor, "wrong thread cursor %x/%p\n", result, cursor );
1400
1401     hdc = GetDC(0);
1402     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
1403     ReleaseDC(0, hdc);
1404
1405     cursorInfo.fIcon = FALSE;
1406     cursorInfo.xHotspot = 0;
1407     cursorInfo.yHotspot = 0;
1408     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
1409     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
1410
1411     cursor = CreateIconIndirect(&cursorInfo);
1412     ok(cursor != NULL, "CreateIconIndirect returned %p\n", cursor);
1413     old_cursor = SetCursor( cursor );
1414
1415     if (pGetCursorInfo)
1416     {
1417         info.cbSize = sizeof(info);
1418         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1419         /* global cursor doesn't change since we don't have a window */
1420         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
1421             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
1422     }
1423     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
1424     WaitForSingleObject( thread, 1000 );
1425     GetExitCodeThread( thread, &result );
1426     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1427
1428     SetCursor( 0 );
1429     ok( GetCursor() == 0, "wrong cursor %p\n", GetCursor() );
1430     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
1431     WaitForSingleObject( thread, 1000 );
1432     GetExitCodeThread( thread, &result );
1433     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1434
1435     thread = CreateThread( NULL, 0, set_cursor_thread, cursor, 0, &id );
1436     WaitForSingleObject( thread, 1000 );
1437     GetExitCodeThread( thread, &result );
1438     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1439     ok( GetCursor() == 0, "wrong cursor %p/0\n", GetCursor() );
1440
1441     parent_id = GetCurrentThreadId();
1442     thread = CreateThread( NULL, 0, set_cursor_thread, cursor, 0, &id );
1443     WaitForSingleObject( thread, 1000 );
1444     GetExitCodeThread( thread, &result );
1445     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1446     ok( GetCursor() == cursor, "wrong cursor %p/0\n", cursor );
1447
1448     if (pGetCursorInfo)
1449     {
1450         info.cbSize = sizeof(info);
1451         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1452         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
1453             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
1454     }
1455     SetCursor( old_cursor );
1456     DestroyCursor( cursor );
1457
1458     SetLastError( 0xdeadbeef );
1459     cursor = SetCursor( (HCURSOR)0xbadbad );
1460     error = GetLastError();
1461     ok( cursor == 0, "wrong cursor %p/0\n", cursor );
1462     ok( error == ERROR_INVALID_CURSOR_HANDLE || broken( error == 0xdeadbeef ),  /* win9x */
1463         "wrong error %u\n", error );
1464
1465     if (pGetCursorInfo)
1466     {
1467         info.cbSize = sizeof(info);
1468         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1469         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
1470             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
1471     }
1472 }
1473
1474 static HANDLE event_start, event_next;
1475
1476 static DWORD CALLBACK show_cursor_thread( void *arg )
1477 {
1478     DWORD count = (DWORD_PTR)arg;
1479     int ret;
1480
1481     PeekMessage( 0, 0, 0, 0, PM_NOREMOVE );  /* create a msg queue */
1482     if (parent_id)
1483     {
1484         BOOL ret = AttachThreadInput( GetCurrentThreadId(), parent_id, TRUE );
1485         ok( ret, "AttachThreadInput failed\n" );
1486     }
1487     if (!count) ret = ShowCursor( FALSE );
1488     else while (count--) ret = ShowCursor( TRUE );
1489     SetEvent( event_start );
1490     WaitForSingleObject( event_next, 2000 );
1491     return ret;
1492 }
1493
1494 static void test_ShowCursor(void)
1495 {
1496     int count;
1497     DWORD id, result;
1498     HANDLE thread;
1499     CURSORINFO info;
1500
1501     if (pGetCursorInfo)
1502     {
1503         memset( &info, 0, sizeof(info) );
1504         info.cbSize = sizeof(info);
1505         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1506         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1507     }
1508
1509     event_start = CreateEvent( NULL, FALSE, FALSE, NULL );
1510     event_next = CreateEvent( NULL, FALSE, FALSE, NULL );
1511
1512     count = ShowCursor( TRUE );
1513     ok( count == 1, "wrong count %d\n", count );
1514     count = ShowCursor( TRUE );
1515     ok( count == 2, "wrong count %d\n", count );
1516     count = ShowCursor( FALSE );
1517     ok( count == 1, "wrong count %d\n", count );
1518     count = ShowCursor( FALSE );
1519     ok( count == 0, "wrong count %d\n", count );
1520     count = ShowCursor( FALSE );
1521     ok( count == -1, "wrong count %d\n", count );
1522     count = ShowCursor( FALSE );
1523     ok( count == -2, "wrong count %d\n", count );
1524
1525     if (pGetCursorInfo)
1526     {
1527         info.cbSize = sizeof(info);
1528         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1529         /* global show count is not affected since we don't have a window */
1530         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1531     }
1532
1533     parent_id = 0;
1534     thread = CreateThread( NULL, 0, show_cursor_thread, NULL, 0, &id );
1535     WaitForSingleObject( event_start, 1000 );
1536     count = ShowCursor( FALSE );
1537     ok( count == -3, "wrong count %d\n", count );
1538     SetEvent( event_next );
1539     WaitForSingleObject( thread, 1000 );
1540     GetExitCodeThread( thread, &result );
1541     ok( result == -1, "wrong thread count %d\n", result );
1542     count = ShowCursor( FALSE );
1543     ok( count == -4, "wrong count %d\n", count );
1544
1545     thread = CreateThread( NULL, 0, show_cursor_thread, (void *)1, 0, &id );
1546     WaitForSingleObject( event_start, 1000 );
1547     count = ShowCursor( TRUE );
1548     ok( count == -3, "wrong count %d\n", count );
1549     SetEvent( event_next );
1550     WaitForSingleObject( thread, 1000 );
1551     GetExitCodeThread( thread, &result );
1552     ok( result == 1, "wrong thread count %d\n", result );
1553     count = ShowCursor( TRUE );
1554     ok( count == -2, "wrong count %d\n", count );
1555
1556     parent_id = GetCurrentThreadId();
1557     thread = CreateThread( NULL, 0, show_cursor_thread, NULL, 0, &id );
1558     WaitForSingleObject( event_start, 1000 );
1559     count = ShowCursor( TRUE );
1560     ok( count == -2, "wrong count %d\n", count );
1561     SetEvent( event_next );
1562     WaitForSingleObject( thread, 1000 );
1563     GetExitCodeThread( thread, &result );
1564     ok( result == -3, "wrong thread count %d\n", result );
1565     count = ShowCursor( FALSE );
1566     ok( count == -2, "wrong count %d\n", count );
1567
1568     thread = CreateThread( NULL, 0, show_cursor_thread, (void *)3, 0, &id );
1569     WaitForSingleObject( event_start, 1000 );
1570     count = ShowCursor( TRUE );
1571     ok( count == 2, "wrong count %d\n", count );
1572     SetEvent( event_next );
1573     WaitForSingleObject( thread, 1000 );
1574     GetExitCodeThread( thread, &result );
1575     ok( result == 1, "wrong thread count %d\n", result );
1576     count = ShowCursor( FALSE );
1577     ok( count == -2, "wrong count %d\n", count );
1578
1579     if (pGetCursorInfo)
1580     {
1581         info.cbSize = sizeof(info);
1582         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1583         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1584     }
1585
1586     count = ShowCursor( TRUE );
1587     ok( count == -1, "wrong count %d\n", count );
1588     count = ShowCursor( TRUE );
1589     ok( count == 0, "wrong count %d\n", count );
1590
1591     if (pGetCursorInfo)
1592     {
1593         info.cbSize = sizeof(info);
1594         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1595         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1596     }
1597 }
1598
1599
1600 static void test_DestroyCursor(void)
1601 {
1602     static const BYTE bmp_bits[4096];
1603     ICONINFO cursorInfo;
1604     HCURSOR cursor, cursor2;
1605     BOOL ret;
1606     DWORD error;
1607     UINT display_bpp;
1608     HDC hdc;
1609
1610     hdc = GetDC(0);
1611     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
1612     ReleaseDC(0, hdc);
1613
1614     cursorInfo.fIcon = FALSE;
1615     cursorInfo.xHotspot = 0;
1616     cursorInfo.yHotspot = 0;
1617     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
1618     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
1619
1620     cursor = CreateIconIndirect(&cursorInfo);
1621     ok(cursor != NULL, "CreateIconIndirect returned %p\n", cursor);
1622     if(!cursor) {
1623         return;
1624     }
1625     SetCursor(cursor);
1626
1627     SetLastError(0xdeadbeef);
1628     ret = DestroyCursor(cursor);
1629     ok(!ret || broken(ret)  /* succeeds on win9x */, "DestroyCursor on the active cursor succeeded\n");
1630     error = GetLastError();
1631     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1632     if (!ret)
1633     {
1634         cursor2 = GetCursor();
1635         ok(cursor2 == cursor, "Active was set to %p when trying to destroy it\n", cursor2);
1636         SetCursor(NULL);
1637
1638         /* Trying to destroy the cursor properly fails now with
1639          * ERROR_INVALID_CURSOR_HANDLE.  This happens because we called
1640          * DestroyCursor() 2+ times after calling SetCursor().  The calls to
1641          * GetCursor() and SetCursor(NULL) in between make no difference. */
1642         ret = DestroyCursor(cursor);
1643         todo_wine {
1644             ok(!ret, "DestroyCursor succeeded.\n");
1645             error = GetLastError();
1646             ok(error == ERROR_INVALID_CURSOR_HANDLE || error == 0xdeadbeef, /* vista */
1647                "Last error: 0x%08x\n", error);
1648         }
1649     }
1650
1651     DeleteObject(cursorInfo.hbmMask);
1652     DeleteObject(cursorInfo.hbmColor);
1653
1654     /* Try testing DestroyCursor() now using LoadCursor() cursors. */
1655     cursor = LoadCursor(NULL, IDC_ARROW);
1656
1657     SetLastError(0xdeadbeef);
1658     ret = DestroyCursor(cursor);
1659     ok(ret || broken(!ret) /* fails on win9x */, "DestroyCursor on the active cursor failed.\n");
1660     error = GetLastError();
1661     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
1662
1663     /* Try setting the cursor to a destroyed OEM cursor. */
1664     SetLastError(0xdeadbeef);
1665     SetCursor(cursor);
1666     error = GetLastError();
1667     todo_wine {
1668         ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
1669     }
1670
1671     /* Check if LoadCursor() returns the same handle with the same icon. */
1672     cursor2 = LoadCursor(NULL, IDC_ARROW);
1673     ok(cursor2 == cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
1674
1675     /* Check if LoadCursor() returns the same handle with a different icon. */
1676     cursor2 = LoadCursor(NULL, IDC_WAIT);
1677     ok(cursor2 != cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
1678 }
1679
1680 START_TEST(cursoricon)
1681 {
1682     pGetCursorInfo = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetCursorInfo" );
1683     test_argc = winetest_get_mainargs(&test_argv);
1684
1685     if (test_argc >= 3)
1686     {
1687         /* Child process. */
1688         sscanf (test_argv[2], "%x", (unsigned int *) &parent);
1689
1690         ok(parent != NULL, "Parent not found.\n");
1691         if (parent == NULL)
1692             ExitProcess(1);
1693
1694         do_child();
1695         return;
1696     }
1697
1698     test_CopyImage_Bitmap(1);
1699     test_CopyImage_Bitmap(4);
1700     test_CopyImage_Bitmap(8);
1701     test_CopyImage_Bitmap(16);
1702     test_CopyImage_Bitmap(24);
1703     test_CopyImage_Bitmap(32);
1704     test_initial_cursor();
1705     test_CreateIcon();
1706     test_LoadImage();
1707     test_CreateIconFromResource();
1708     test_DrawIcon();
1709     test_DrawIconEx();
1710     test_DrawState();
1711     test_SetCursor();
1712     test_ShowCursor();
1713     test_DestroyCursor();
1714     do_parent();
1715     test_child_process();
1716     finish_child_process();
1717 }