user32: Draw the mask with SRCCOPY in DrawIcon when the image isn't being drawn.
[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 <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26
27 #include "wine/test.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33
34 #include "pshpack1.h"
35
36 typedef struct
37 {
38     BYTE bWidth;
39     BYTE bHeight;
40     BYTE bColorCount;
41     BYTE bReserved;
42     WORD xHotspot;
43     WORD yHotspot;
44     DWORD dwDIBSize;
45     DWORD dwDIBOffset;
46 } CURSORICONFILEDIRENTRY;
47
48 typedef struct
49 {
50     WORD idReserved;
51     WORD idType;
52     WORD idCount;
53     CURSORICONFILEDIRENTRY idEntries[1];
54 } CURSORICONFILEDIR;
55
56 #define RIFF_FOURCC( c0, c1, c2, c3 ) \
57         ( (DWORD)(BYTE)(c0) | ( (DWORD)(BYTE)(c1) << 8 ) | \
58         ( (DWORD)(BYTE)(c2) << 16 ) | ( (DWORD)(BYTE)(c3) << 24 ) )
59
60 #define ANI_RIFF_ID RIFF_FOURCC('R', 'I', 'F', 'F')
61 #define ANI_LIST_ID RIFF_FOURCC('L', 'I', 'S', 'T')
62 #define ANI_ACON_ID RIFF_FOURCC('A', 'C', 'O', 'N')
63 #define ANI_anih_ID RIFF_FOURCC('a', 'n', 'i', 'h')
64 #define ANI_seq__ID RIFF_FOURCC('s', 'e', 'q', ' ')
65 #define ANI_fram_ID RIFF_FOURCC('f', 'r', 'a', 'm')
66 #define ANI_icon_ID RIFF_FOURCC('i', 'c', 'o', 'n')
67 #define ANI_rate_ID RIFF_FOURCC('r', 'a', 't', 'e')
68
69 #define ANI_FLAG_ICON       0x1
70 #define ANI_FLAG_SEQUENCE   0x2
71
72 typedef struct {
73     DWORD header_size;
74     DWORD num_frames;
75     DWORD num_steps;
76     DWORD width;
77     DWORD height;
78     DWORD bpp;
79     DWORD num_planes;
80     DWORD display_rate;
81     DWORD flags;
82 } ani_header;
83
84 typedef struct {
85     BYTE data[32*32*4];
86 } ani_data32x32x32;
87
88 typedef struct {
89     CURSORICONFILEDIR    icon_info;  /* animated cursor frame information */
90     BITMAPINFOHEADER     bmi_header; /* animated cursor frame header */
91     ani_data32x32x32     bmi_data;   /* animated cursor frame DIB data */
92 } ani_frame32x32x32;
93
94 typedef struct {
95     DWORD                chunk_id;   /* ANI_anih_ID */
96     DWORD                chunk_size; /* actual size of data */
97     ani_header           header;     /* animated cursor header */
98 } riff_header_t;
99
100 typedef struct {
101     DWORD                chunk_id;   /* ANI_LIST_ID */
102     DWORD                chunk_size; /* actual size of data */
103     DWORD                chunk_type; /* ANI_fram_ID */
104 } riff_list_t;
105
106 typedef struct {
107     DWORD                chunk_id;   /* ANI_icon_ID */
108     DWORD                chunk_size; /* actual size of data */
109     ani_frame32x32x32    data;       /* animated cursor frame */
110 } riff_icon32x32x32_t;
111
112 typedef struct {
113     DWORD                chunk_id;   /* ANI_RIFF_ID */
114     DWORD                chunk_size; /* actual size of data */
115     DWORD                chunk_type; /* ANI_ACON_ID */
116     riff_header_t        header;     /* RIFF animated cursor header */
117     riff_list_t          frame_list; /* RIFF animated cursor frame list info */
118     riff_icon32x32x32_t  frames[1];  /* array of animated cursor frames */
119 } riff_cursor1_t;
120
121 typedef struct {
122     DWORD                chunk_id;   /* ANI_RIFF_ID */
123     DWORD                chunk_size; /* actual size of data */
124     DWORD                chunk_type; /* ANI_ACON_ID */
125     riff_header_t        header;     /* RIFF animated cursor header */
126     riff_list_t          frame_list; /* RIFF animated cursor frame list info */
127     riff_icon32x32x32_t  frames[3];  /* array of three animated cursor frames */
128 } riff_cursor3_t;
129
130 typedef struct {
131     DWORD                chunk_id;   /* ANI_rate_ID */
132     DWORD                chunk_size; /* actual size of data */
133     DWORD                rate[3];    /* animated cursor rate data */
134 } riff_rate3_t;
135
136 typedef struct {
137     DWORD                chunk_id;   /* ANI_seq__ID */
138     DWORD                chunk_size; /* actual size of data */
139     DWORD                order[3];   /* animated cursor sequence data */
140 } riff_seq3_t;
141
142 typedef struct {
143     DWORD                chunk_id;   /* ANI_RIFF_ID */
144     DWORD                chunk_size; /* actual size of data */
145     DWORD                chunk_type; /* ANI_ACON_ID */
146     riff_header_t        header;     /* RIFF animated cursor header */
147     riff_seq3_t          seq;        /* sequence data for three cursor frames */
148     riff_rate3_t         rates;      /* rate data for three cursor frames */
149     riff_list_t          frame_list; /* RIFF animated cursor frame list info */
150     riff_icon32x32x32_t  frames[3];  /* array of three animated cursor frames */
151 } riff_cursor3_seq_t;
152
153 #define EMPTY_ICON32 \
154 { \
155     ANI_icon_ID, \
156     sizeof(ani_frame32x32x32), \
157     { \
158         { \
159             0x0, /* reserved */ \
160             0,   /* type: icon(1), cursor(2) */ \
161             1,   /* count */ \
162             { \
163                 { \
164                     32,                        /* width */ \
165                     32,                        /* height */ \
166                     0,                         /* color count */ \
167                     0x0,                       /* reserved */ \
168                     16,                        /* x hotspot */ \
169                     16,                        /* y hotspot */ \
170                     sizeof(ani_data32x32x32),  /* DIB size */ \
171                     sizeof(CURSORICONFILEDIR)  /* DIB offset */ \
172                 } \
173             } \
174         }, \
175         { \
176               sizeof(BITMAPINFOHEADER),  /* structure for DIB-type data */ \
177               32,                        /* width */ \
178               32*2,                      /* actual height times two */ \
179               1,                         /* planes */ \
180               32,                        /* bpp */ \
181               BI_RGB,                    /* compression */ \
182               0,                         /* image size */ \
183               0,                         /* biXPelsPerMeter */ \
184               0,                         /* biYPelsPerMeter */ \
185               0,                         /* biClrUsed */ \
186               0                          /* biClrImportant */ \
187         } \
188         /* DIB data: left uninitialized */ \
189     } \
190 }
191
192 riff_cursor1_t empty_anicursor = {
193     ANI_RIFF_ID,
194     sizeof(empty_anicursor) - sizeof(DWORD)*2,
195     ANI_ACON_ID,
196     {
197         ANI_anih_ID,
198         sizeof(ani_header),
199         {
200             sizeof(ani_header),
201             1,            /* frames */
202             1,            /* steps */
203             32,           /* width */
204             32,           /* height */
205             32,           /* depth */
206             1,            /* planes */
207             10,           /* display rate in jiffies */
208             ANI_FLAG_ICON /* flags */
209         }
210     },
211     {
212         ANI_LIST_ID,
213         sizeof(riff_icon32x32x32_t)*(1 /*frames*/) + sizeof(DWORD),
214         ANI_fram_ID,
215     },
216     {
217         EMPTY_ICON32
218     }
219 };
220
221 riff_cursor3_t empty_anicursor3 = {
222     ANI_RIFF_ID,
223     sizeof(empty_anicursor3) - sizeof(DWORD)*2,
224     ANI_ACON_ID,
225     {
226         ANI_anih_ID,
227         sizeof(ani_header),
228         {
229             sizeof(ani_header),
230             3,            /* frames */
231             3,            /* steps */
232             32,           /* width */
233             32,           /* height */
234             32,           /* depth */
235             1,            /* planes */
236             0xbeef,       /* display rate in jiffies */
237             ANI_FLAG_ICON /* flags */
238         }
239     },
240     {
241         ANI_LIST_ID,
242         sizeof(riff_icon32x32x32_t)*(3 /*frames*/) + sizeof(DWORD),
243         ANI_fram_ID,
244     },
245     {
246         EMPTY_ICON32,
247         EMPTY_ICON32,
248         EMPTY_ICON32
249     }
250 };
251
252 riff_cursor3_seq_t empty_anicursor3_seq = {
253     ANI_RIFF_ID,
254     sizeof(empty_anicursor3_seq) - sizeof(DWORD)*2,
255     ANI_ACON_ID,
256     {
257         ANI_anih_ID,
258         sizeof(ani_header),
259         {
260             sizeof(ani_header),
261             3,                              /* frames */
262             3,                              /* steps */
263             32,                             /* width */
264             32,                             /* height */
265             32,                             /* depth */
266             1,                              /* planes */
267             0xbeef,                         /* display rate in jiffies */
268             ANI_FLAG_ICON|ANI_FLAG_SEQUENCE /* flags */
269         }
270     },
271     {
272         ANI_seq__ID,
273         sizeof(riff_seq3_t) - sizeof(DWORD)*2,
274         { 2, 0, 1} /* show frames in a uniquely identifiable order */
275     },
276     {
277         ANI_rate_ID,
278         sizeof(riff_rate3_t) - sizeof(DWORD)*2,
279         { 0xc0de, 0xcafe, 0xbabe}
280     },
281     {
282         ANI_LIST_ID,
283         sizeof(riff_icon32x32x32_t)*(3 /*frames*/) + sizeof(DWORD),
284         ANI_fram_ID,
285     },
286     {
287         EMPTY_ICON32,
288         EMPTY_ICON32,
289         EMPTY_ICON32
290     }
291 };
292
293 #include "poppack.h"
294
295 static char **test_argv;
296 static int test_argc;
297 static HWND child = 0;
298 static HWND parent = 0;
299 static HANDLE child_process;
300
301 #define PROC_INIT (WM_USER+1)
302
303 static BOOL (WINAPI *pGetCursorInfo)(CURSORINFO *);
304 static BOOL (WINAPI *pGetIconInfoExA)(HICON,ICONINFOEXA *);
305 static BOOL (WINAPI *pGetIconInfoExW)(HICON,ICONINFOEXW *);
306
307 static const int is_win64 = (sizeof(void *) > sizeof(int));
308
309 static LRESULT CALLBACK callback_child(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
310 {
311     BOOL ret;
312     DWORD error;
313
314     switch (msg)
315     {
316         /* Destroy the cursor. */
317         case WM_USER+1:
318             SetLastError(0xdeadbeef);
319             ret = DestroyCursor((HCURSOR) lParam);
320             error = GetLastError();
321             ok(!ret || broken(ret) /* win9x */, "DestroyCursor on the active cursor succeeded.\n");
322             ok(error == ERROR_DESTROY_OBJECT_OF_OTHER_THREAD ||
323                error == 0xdeadbeef,  /* vista */
324                 "Last error: %u\n", error);
325             return TRUE;
326         case WM_DESTROY:
327             PostQuitMessage(0);
328             return 0;
329     }
330
331     return DefWindowProc(hwnd, msg, wParam, lParam);
332 }
333
334 static LRESULT CALLBACK callback_parent(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
335 {
336     if (msg == PROC_INIT)
337     {
338         child = (HWND) wParam;
339         return TRUE;
340     }
341
342     return DefWindowProc(hwnd, msg, wParam, lParam);
343 }
344
345 static void do_child(void)
346 {
347     WNDCLASS class;
348     MSG msg;
349     BOOL ret;
350
351     /* Register a new class. */
352     class.style = CS_GLOBALCLASS;
353     class.lpfnWndProc = callback_child;
354     class.cbClsExtra = 0;
355     class.cbWndExtra = 0;
356     class.hInstance = GetModuleHandle(NULL);
357     class.hIcon = NULL;
358     class.hCursor = NULL;
359     class.hbrBackground = NULL;
360     class.lpszMenuName = NULL;
361     class.lpszClassName = "cursor_child";
362
363     SetLastError(0xdeadbeef);
364     ret = RegisterClass(&class);
365     ok(ret, "Failed to register window class.  Error: %u\n", GetLastError());
366
367     /* Create a window. */
368     child = CreateWindowA("cursor_child", "cursor_child", WS_POPUP | WS_VISIBLE,
369         0, 0, 200, 200, 0, 0, 0, NULL);
370     ok(child != 0, "CreateWindowA failed.  Error: %u\n", GetLastError());
371
372     /* Let the parent know our HWND. */
373     PostMessage(parent, PROC_INIT, (WPARAM) child, 0);
374
375     /* Receive messages. */
376     while ((ret = GetMessage(&msg, 0, 0, 0)))
377     {
378         ok(ret != -1, "GetMessage failed.  Error: %u\n", GetLastError());
379         TranslateMessage(&msg);
380         DispatchMessage(&msg);
381     }
382 }
383
384 static void do_parent(void)
385 {
386     char path_name[MAX_PATH];
387     PROCESS_INFORMATION info;
388     STARTUPINFOA startup;
389     WNDCLASS class;
390     MSG msg;
391     BOOL ret;
392
393     /* Register a new class. */
394     class.style = CS_GLOBALCLASS;
395     class.lpfnWndProc = callback_parent;
396     class.cbClsExtra = 0;
397     class.cbWndExtra = 0;
398     class.hInstance = GetModuleHandle(NULL);
399     class.hIcon = NULL;
400     class.hCursor = NULL;
401     class.hbrBackground = NULL;
402     class.lpszMenuName = NULL;
403     class.lpszClassName = "cursor_parent";
404
405     SetLastError(0xdeadbeef);
406     ret = RegisterClass(&class);
407     ok(ret, "Failed to register window class.  Error: %u\n", GetLastError());
408
409     /* Create a window. */
410     parent = CreateWindowA("cursor_parent", "cursor_parent", WS_POPUP | WS_VISIBLE,
411         0, 0, 200, 200, 0, 0, 0, NULL);
412     ok(parent != 0, "CreateWindowA failed.  Error: %u\n", GetLastError());
413
414     /* Start child process. */
415     memset(&startup, 0, sizeof(startup));
416     startup.cb = sizeof(startup);
417     startup.dwFlags = STARTF_USESHOWWINDOW;
418     startup.wShowWindow = SW_SHOWNORMAL;
419
420     sprintf(path_name, "%s cursoricon %lx", test_argv[0], (INT_PTR)parent);
421     ok(CreateProcessA(NULL, path_name, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess failed.\n");
422     child_process = info.hProcess;
423
424     /* Wait for child window handle. */
425     while ((child == 0) && (ret = GetMessage(&msg, parent, 0, 0)))
426     {
427         ok(ret != -1, "GetMessage failed.  Error: %u\n", GetLastError());
428         TranslateMessage(&msg);
429         DispatchMessage(&msg);
430     }
431 }
432
433 static void finish_child_process(void)
434 {
435     SendMessage(child, WM_CLOSE, 0, 0);
436     winetest_wait_child_process( child_process );
437     CloseHandle(child_process);
438 }
439
440 static void test_child_process(void)
441 {
442     static const BYTE bmp_bits[4096];
443     HCURSOR cursor;
444     ICONINFO cursorInfo;
445     UINT display_bpp;
446     HDC hdc;
447
448     /* Create and set a dummy cursor. */
449     hdc = GetDC(0);
450     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
451     ReleaseDC(0, hdc);
452
453     cursorInfo.fIcon = FALSE;
454     cursorInfo.xHotspot = 0;
455     cursorInfo.yHotspot = 0;
456     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
457     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
458
459     cursor = CreateIconIndirect(&cursorInfo);
460     ok(cursor != NULL, "CreateIconIndirect returned %p.\n", cursor);
461
462     SetCursor(cursor);
463
464     /* Destroy the cursor. */
465     SendMessage(child, WM_USER+1, 0, (LPARAM) cursor);
466 }
467
468 static BOOL color_match(COLORREF a, COLORREF b)
469 {
470     /* 5-bit accuracy is a sufficient test. This will match as long as
471      * colors are never truncated to less that 3x5-bit accuracy i.e.
472      * palettized. */
473     return (a & 0x00F8F8F8) == (b & 0x00F8F8F8);
474 }
475
476 static void test_CopyImage_Check(HBITMAP bitmap, UINT flags, INT copyWidth, INT copyHeight,
477                                   INT expectedWidth, INT expectedHeight, WORD expectedDepth, BOOL dibExpected)
478 {
479     HBITMAP copy;
480     BITMAP origBitmap;
481     BITMAP copyBitmap;
482     BOOL orig_is_dib;
483     BOOL copy_is_dib;
484
485     copy = CopyImage(bitmap, IMAGE_BITMAP, copyWidth, copyHeight, flags);
486     ok(copy != NULL, "CopyImage() failed\n");
487     if (copy != NULL)
488     {
489         GetObject(bitmap, sizeof(origBitmap), &origBitmap);
490         GetObject(copy, sizeof(copyBitmap), &copyBitmap);
491         orig_is_dib = (origBitmap.bmBits != NULL);
492         copy_is_dib = (copyBitmap.bmBits != NULL);
493
494         if (copy_is_dib && dibExpected
495             && copyBitmap.bmBitsPixel == 24
496             && (expectedDepth == 16 || expectedDepth == 32))
497         {
498             /* Windows 95 doesn't create DIBs with a depth of 16 or 32 bit */
499             if (GetVersion() & 0x80000000)
500             {
501                 expectedDepth = 24;
502             }
503         }
504
505         if (copy_is_dib && !dibExpected && !(flags & LR_CREATEDIBSECTION))
506         {
507             /* It's not forbidden to create a DIB section if the flag
508                LR_CREATEDIBSECTION is absent.
509                Windows 9x does this if the bitmap has a depth that doesn't
510                match the screen depth, Windows NT doesn't */
511             dibExpected = TRUE;
512             expectedDepth = origBitmap.bmBitsPixel;
513         }
514
515         ok((!(dibExpected ^ copy_is_dib)
516              && (copyBitmap.bmWidth == expectedWidth)
517              && (copyBitmap.bmHeight == expectedHeight)
518              && (copyBitmap.bmBitsPixel == expectedDepth)),
519              "CopyImage ((%s, %dx%d, %u bpp), %d, %d, %#x): Expected (%s, %dx%d, %u bpp), got (%s, %dx%d, %u bpp)\n",
520                   orig_is_dib ? "DIB" : "DDB", origBitmap.bmWidth, origBitmap.bmHeight, origBitmap.bmBitsPixel,
521                   copyWidth, copyHeight, flags,
522                   dibExpected ? "DIB" : "DDB", expectedWidth, expectedHeight, expectedDepth,
523                   copy_is_dib ? "DIB" : "DDB", copyBitmap.bmWidth, copyBitmap.bmHeight, copyBitmap.bmBitsPixel);
524
525         DeleteObject(copy);
526     }
527 }
528
529 static void test_CopyImage_Bitmap(int depth)
530 {
531     HBITMAP ddb, dib;
532     HDC screenDC;
533     BITMAPINFO * info;
534     VOID * bits;
535     int screen_depth;
536     unsigned int i;
537
538     /* Create a device-independent bitmap (DIB) */
539     info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
540     info->bmiHeader.biSize = sizeof(info->bmiHeader);
541     info->bmiHeader.biWidth = 2;
542     info->bmiHeader.biHeight = 2;
543     info->bmiHeader.biPlanes = 1;
544     info->bmiHeader.biBitCount = depth;
545     info->bmiHeader.biCompression = BI_RGB;
546
547     for (i=0; i < 256; i++)
548     {
549         info->bmiColors[i].rgbRed = i;
550         info->bmiColors[i].rgbGreen = i;
551         info->bmiColors[i].rgbBlue = 255 - i;
552         info->bmiColors[i].rgbReserved = 0;
553     }
554
555     dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
556
557     /* Create a device-dependent bitmap (DDB) */
558     screenDC = GetDC(NULL);
559     screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
560     if (depth == 1 || depth == screen_depth)
561     {
562         ddb = CreateBitmap(2, 2, 1, depth, NULL);
563     }
564     else
565     {
566         ddb = NULL;
567     }
568     ReleaseDC(NULL, screenDC);
569
570     if (ddb != NULL)
571     {
572         test_CopyImage_Check(ddb, 0, 0, 0, 2, 2, depth == 1 ? 1 : screen_depth, FALSE);
573         test_CopyImage_Check(ddb, 0, 0, 5, 2, 5, depth == 1 ? 1 : screen_depth, FALSE);
574         test_CopyImage_Check(ddb, 0, 5, 0, 5, 2, depth == 1 ? 1 : screen_depth, FALSE);
575         test_CopyImage_Check(ddb, 0, 5, 5, 5, 5, depth == 1 ? 1 : screen_depth, FALSE);
576
577         test_CopyImage_Check(ddb, LR_MONOCHROME, 0, 0, 2, 2, 1, FALSE);
578         test_CopyImage_Check(ddb, LR_MONOCHROME, 5, 0, 5, 2, 1, FALSE);
579         test_CopyImage_Check(ddb, LR_MONOCHROME, 0, 5, 2, 5, 1, FALSE);
580         test_CopyImage_Check(ddb, LR_MONOCHROME, 5, 5, 5, 5, 1, FALSE);
581
582         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
583         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
584         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
585         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
586
587         /* LR_MONOCHROME is ignored if LR_CREATEDIBSECTION is present */
588         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
589         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
590         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
591         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
592
593         DeleteObject(ddb);
594     }
595
596     if (depth != 1)
597     {
598         test_CopyImage_Check(dib, 0, 0, 0, 2, 2, screen_depth, FALSE);
599         test_CopyImage_Check(dib, 0, 5, 0, 5, 2, screen_depth, FALSE);
600         test_CopyImage_Check(dib, 0, 0, 5, 2, 5, screen_depth, FALSE);
601         test_CopyImage_Check(dib, 0, 5, 5, 5, 5, screen_depth, FALSE);
602     }
603
604     test_CopyImage_Check(dib, LR_MONOCHROME, 0, 0, 2, 2, 1, FALSE);
605     test_CopyImage_Check(dib, LR_MONOCHROME, 5, 0, 5, 2, 1, FALSE);
606     test_CopyImage_Check(dib, LR_MONOCHROME, 0, 5, 2, 5, 1, FALSE);
607     test_CopyImage_Check(dib, LR_MONOCHROME, 5, 5, 5, 5, 1, FALSE);
608
609     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
610     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
611     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
612     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
613
614     /* LR_MONOCHROME is ignored if LR_CREATEDIBSECTION is present */
615     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
616     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
617     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
618     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
619
620     DeleteObject(dib);
621
622     if (depth == 1)
623     {
624         /* Special case: A monochrome DIB is converted to a monochrome DDB if
625            the colors in the color table are black and white.
626
627            Skip this test on Windows 95, it always creates a monochrome DDB
628            in this case */
629
630         if (!(GetVersion() & 0x80000000))
631         {
632             info->bmiHeader.biBitCount = 1;
633             info->bmiColors[0].rgbRed = 0xFF;
634             info->bmiColors[0].rgbGreen = 0;
635             info->bmiColors[0].rgbBlue = 0;
636             info->bmiColors[1].rgbRed = 0;
637             info->bmiColors[1].rgbGreen = 0xFF;
638             info->bmiColors[1].rgbBlue = 0;
639
640             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
641             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, screen_depth, FALSE);
642             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, screen_depth, FALSE);
643             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, screen_depth, FALSE);
644             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, screen_depth, FALSE);
645             DeleteObject(dib);
646
647             info->bmiHeader.biBitCount = 1;
648             info->bmiColors[0].rgbRed = 0;
649             info->bmiColors[0].rgbGreen = 0;
650             info->bmiColors[0].rgbBlue = 0;
651             info->bmiColors[1].rgbRed = 0xFF;
652             info->bmiColors[1].rgbGreen = 0xFF;
653             info->bmiColors[1].rgbBlue = 0xFF;
654
655             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
656             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, 1, FALSE);
657             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, 1, FALSE);
658             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, 1, FALSE);
659             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, 1, FALSE);
660             DeleteObject(dib);
661
662             info->bmiHeader.biBitCount = 1;
663             info->bmiColors[0].rgbRed = 0xFF;
664             info->bmiColors[0].rgbGreen = 0xFF;
665             info->bmiColors[0].rgbBlue = 0xFF;
666             info->bmiColors[1].rgbRed = 0;
667             info->bmiColors[1].rgbGreen = 0;
668             info->bmiColors[1].rgbBlue = 0;
669
670             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
671             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, 1, FALSE);
672             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, 1, FALSE);
673             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, 1, FALSE);
674             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, 1, FALSE);
675             DeleteObject(dib);
676         }
677     }
678
679     HeapFree(GetProcessHeap(), 0, info);
680 }
681
682 static void test_initial_cursor(void)
683 {
684     HCURSOR cursor, cursor2;
685     DWORD error;
686
687     cursor = GetCursor();
688
689     /* Check what handle GetCursor() returns if a cursor is not set yet. */
690     SetLastError(0xdeadbeef);
691     cursor2 = LoadCursor(NULL, IDC_WAIT);
692     todo_wine {
693         ok(cursor == cursor2, "cursor (%p) is not IDC_WAIT (%p).\n", cursor, cursor2);
694     }
695     error = GetLastError();
696     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
697 }
698
699 static void test_icon_info_dbg(HICON hIcon, UINT exp_cx, UINT exp_cy, UINT exp_mask_cy, UINT exp_bpp, int line)
700 {
701     ICONINFO info;
702     DWORD ret;
703     BITMAP bmMask, bmColor;
704
705     ret = GetIconInfo(hIcon, &info);
706     ok_(__FILE__, line)(ret, "GetIconInfo failed\n");
707
708     /* CreateIcon under XP causes info.fIcon to be 0 */
709     ok_(__FILE__, line)(info.xHotspot == exp_cx/2, "info.xHotspot = %u\n", info.xHotspot);
710     ok_(__FILE__, line)(info.yHotspot == exp_cy/2, "info.yHotspot = %u\n", info.yHotspot);
711     ok_(__FILE__, line)(info.hbmMask != 0, "info.hbmMask is NULL\n");
712
713     ret = GetObject(info.hbmMask, sizeof(bmMask), &bmMask);
714     ok_(__FILE__, line)(ret == sizeof(bmMask), "GetObject(info.hbmMask) failed, ret %u\n", ret);
715
716     if (exp_bpp == 1)
717         ok_(__FILE__, line)(info.hbmColor == 0, "info.hbmColor should be NULL\n");
718
719     if (info.hbmColor)
720     {
721         HDC hdc;
722         UINT display_bpp;
723
724         hdc = GetDC(0);
725         display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
726         ReleaseDC(0, hdc);
727
728         ret = GetObject(info.hbmColor, sizeof(bmColor), &bmColor);
729         ok_(__FILE__, line)(ret == sizeof(bmColor), "GetObject(info.hbmColor) failed, ret %u\n", ret);
730
731         ok_(__FILE__, line)(bmColor.bmBitsPixel == display_bpp /* XP */ ||
732            bmColor.bmBitsPixel == exp_bpp /* Win98 */,
733            "bmColor.bmBitsPixel = %d\n", bmColor.bmBitsPixel);
734         ok_(__FILE__, line)(bmColor.bmWidth == exp_cx, "bmColor.bmWidth = %d\n", bmColor.bmWidth);
735         ok_(__FILE__, line)(bmColor.bmHeight == exp_cy, "bmColor.bmHeight = %d\n", bmColor.bmHeight);
736
737         ok_(__FILE__, line)(bmMask.bmBitsPixel == 1, "bmMask.bmBitsPixel = %d\n", bmMask.bmBitsPixel);
738         ok_(__FILE__, line)(bmMask.bmWidth == exp_cx, "bmMask.bmWidth = %d\n", bmMask.bmWidth);
739         ok_(__FILE__, line)(bmMask.bmHeight == exp_mask_cy, "bmMask.bmHeight = %d\n", bmMask.bmHeight);
740     }
741     else
742     {
743         ok_(__FILE__, line)(bmMask.bmBitsPixel == 1, "bmMask.bmBitsPixel = %d\n", bmMask.bmBitsPixel);
744         ok_(__FILE__, line)(bmMask.bmWidth == exp_cx, "bmMask.bmWidth = %d\n", bmMask.bmWidth);
745         ok_(__FILE__, line)(bmMask.bmHeight == exp_mask_cy, "bmMask.bmHeight = %d\n", bmMask.bmHeight);
746     }
747     if (pGetIconInfoExA)
748     {
749         ICONINFOEXA infoex;
750
751         memset( &infoex, 0xcc, sizeof(infoex) );
752         SetLastError( 0xdeadbeef );
753         infoex.cbSize = sizeof(infoex) - 1;
754         ret = pGetIconInfoExA( hIcon, &infoex );
755         ok_(__FILE__, line)(!ret, "GetIconInfoEx succeeded\n");
756         ok_(__FILE__, line)(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %d\n", GetLastError());
757
758         SetLastError( 0xdeadbeef );
759         infoex.cbSize = sizeof(infoex) + 1;
760         ret = pGetIconInfoExA( hIcon, &infoex );
761         ok_(__FILE__, line)(!ret, "GetIconInfoEx succeeded\n");
762         ok_(__FILE__, line)(GetLastError() == ERROR_INVALID_PARAMETER, "wrong error %d\n", GetLastError());
763
764         SetLastError( 0xdeadbeef );
765         infoex.cbSize = sizeof(infoex);
766         ret = pGetIconInfoExA( (HICON)0xdeadbabe, &infoex );
767         ok_(__FILE__, line)(!ret, "GetIconInfoEx succeeded\n");
768         ok_(__FILE__, line)(GetLastError() == ERROR_INVALID_CURSOR_HANDLE,
769                             "wrong error %d\n", GetLastError());
770
771         infoex.cbSize = sizeof(infoex);
772         ret = pGetIconInfoExA( hIcon, &infoex );
773         ok_(__FILE__, line)(ret, "GetIconInfoEx failed err %d\n", GetLastError());
774         ok_(__FILE__, line)(infoex.wResID == 0, "GetIconInfoEx wrong resid %x\n", infoex.wResID);
775         ok_(__FILE__, line)(infoex.szModName[0] == 0, "GetIconInfoEx wrong module %s\n", infoex.szModName);
776         ok_(__FILE__, line)(infoex.szResName[0] == 0, "GetIconInfoEx wrong name %s\n", infoex.szResName);
777     }
778 }
779
780 #define test_icon_info(a,b,c,d,e) test_icon_info_dbg((a),(b),(c),(d),(e),__LINE__)
781
782 static void test_CreateIcon(void)
783 {
784     static const BYTE bmp_bits[1024];
785     HICON hIcon;
786     HBITMAP hbmMask, hbmColor;
787     BITMAPINFO *bmpinfo;
788     ICONINFO info;
789     HDC hdc;
790     void *bits;
791     UINT display_bpp;
792     int i;
793
794     hdc = GetDC(0);
795     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
796
797     /* these crash under XP
798     hIcon = CreateIcon(0, 16, 16, 1, 1, bmp_bits, NULL);
799     hIcon = CreateIcon(0, 16, 16, 1, 1, NULL, bmp_bits);
800     */
801
802     hIcon = CreateIcon(0, 16, 16, 1, 1, bmp_bits, bmp_bits);
803     ok(hIcon != 0, "CreateIcon failed\n");
804     test_icon_info(hIcon, 16, 16, 32, 1);
805     DestroyIcon(hIcon);
806
807     hIcon = CreateIcon(0, 16, 16, 1, display_bpp, bmp_bits, bmp_bits);
808     ok(hIcon != 0, "CreateIcon failed\n");
809     test_icon_info(hIcon, 16, 16, 16, display_bpp);
810     DestroyIcon(hIcon);
811
812     hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
813     ok(hbmMask != 0, "CreateBitmap failed\n");
814     hbmColor = CreateBitmap(16, 16, 1, display_bpp, bmp_bits);
815     ok(hbmColor != 0, "CreateBitmap failed\n");
816
817     info.fIcon = TRUE;
818     info.xHotspot = 8;
819     info.yHotspot = 8;
820     info.hbmMask = 0;
821     info.hbmColor = 0;
822     SetLastError(0xdeadbeaf);
823     hIcon = CreateIconIndirect(&info);
824     ok(!hIcon, "CreateIconIndirect should fail\n");
825     ok(GetLastError() == 0xdeadbeaf, "wrong error %u\n", GetLastError());
826
827     info.fIcon = TRUE;
828     info.xHotspot = 8;
829     info.yHotspot = 8;
830     info.hbmMask = 0;
831     info.hbmColor = hbmColor;
832     SetLastError(0xdeadbeaf);
833     hIcon = CreateIconIndirect(&info);
834     ok(!hIcon, "CreateIconIndirect should fail\n");
835     ok(GetLastError() == 0xdeadbeaf, "wrong error %u\n", GetLastError());
836
837     info.fIcon = TRUE;
838     info.xHotspot = 8;
839     info.yHotspot = 8;
840     info.hbmMask = hbmMask;
841     info.hbmColor = hbmColor;
842     hIcon = CreateIconIndirect(&info);
843     ok(hIcon != 0, "CreateIconIndirect failed\n");
844     test_icon_info(hIcon, 16, 16, 16, display_bpp);
845     DestroyIcon(hIcon);
846
847     DeleteObject(hbmMask);
848     DeleteObject(hbmColor);
849
850     hbmMask = CreateBitmap(16, 32, 1, 1, bmp_bits);
851     ok(hbmMask != 0, "CreateBitmap failed\n");
852
853     info.fIcon = TRUE;
854     info.xHotspot = 8;
855     info.yHotspot = 8;
856     info.hbmMask = hbmMask;
857     info.hbmColor = 0;
858     SetLastError(0xdeadbeaf);
859     hIcon = CreateIconIndirect(&info);
860     ok(hIcon != 0, "CreateIconIndirect failed\n");
861     test_icon_info(hIcon, 16, 16, 32, 1);
862     DestroyIcon(hIcon);
863     DeleteObject(hbmMask);
864
865     for (i = 0; i <= 4; i++)
866     {
867         hbmMask = CreateBitmap(1, i, 1, 1, bmp_bits);
868         ok(hbmMask != 0, "CreateBitmap failed\n");
869
870         info.fIcon = TRUE;
871         info.xHotspot = 0;
872         info.yHotspot = 0;
873         info.hbmMask = hbmMask;
874         info.hbmColor = 0;
875         SetLastError(0xdeadbeaf);
876         hIcon = CreateIconIndirect(&info);
877         ok(hIcon != 0, "CreateIconIndirect failed\n");
878         test_icon_info(hIcon, 1, i / 2, max(i,1), 1);
879         DestroyIcon(hIcon);
880         DeleteObject(hbmMask);
881     }
882
883     /* test creating an icon from a DIB section */
884
885     bmpinfo = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(BITMAPINFO,bmiColors[256]));
886     bmpinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
887     bmpinfo->bmiHeader.biWidth = 32;
888     bmpinfo->bmiHeader.biHeight = 32;
889     bmpinfo->bmiHeader.biPlanes = 1;
890     bmpinfo->bmiHeader.biBitCount = 8;
891     bmpinfo->bmiHeader.biCompression = BI_RGB;
892     hbmColor = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
893     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
894     if (bits)
895         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
896     bmpinfo->bmiHeader.biBitCount = 1;
897     hbmMask = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
898     ok(hbmMask != NULL, "Expected a handle to the DIB\n");
899     if (bits)
900         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
901
902     info.fIcon = TRUE;
903     info.xHotspot = 8;
904     info.yHotspot = 8;
905     info.hbmMask = hbmColor;
906     info.hbmColor = hbmMask;
907     SetLastError(0xdeadbeaf);
908     hIcon = CreateIconIndirect(&info);
909     ok(hIcon != 0, "CreateIconIndirect failed\n");
910     test_icon_info(hIcon, 32, 32, 32, 8);
911     DestroyIcon(hIcon);
912     DeleteObject(hbmColor);
913
914     bmpinfo->bmiHeader.biBitCount = 16;
915     hbmColor = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
916     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
917     if (bits)
918         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
919
920     info.fIcon = TRUE;
921     info.xHotspot = 8;
922     info.yHotspot = 8;
923     info.hbmMask = hbmColor;
924     info.hbmColor = hbmMask;
925     SetLastError(0xdeadbeaf);
926     hIcon = CreateIconIndirect(&info);
927     ok(hIcon != 0, "CreateIconIndirect failed\n");
928     test_icon_info(hIcon, 32, 32, 32, 8);
929     DestroyIcon(hIcon);
930     DeleteObject(hbmColor);
931
932     bmpinfo->bmiHeader.biBitCount = 32;
933     hbmColor = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
934     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
935     if (bits)
936         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
937
938     info.fIcon = TRUE;
939     info.xHotspot = 8;
940     info.yHotspot = 8;
941     info.hbmMask = hbmColor;
942     info.hbmColor = hbmMask;
943     SetLastError(0xdeadbeaf);
944     hIcon = CreateIconIndirect(&info);
945     ok(hIcon != 0, "CreateIconIndirect failed\n");
946     test_icon_info(hIcon, 32, 32, 32, 8);
947     DestroyIcon(hIcon);
948
949     DeleteObject(hbmMask);
950     DeleteObject(hbmColor);
951     HeapFree( GetProcessHeap(), 0, bmpinfo );
952
953     ReleaseDC(0, hdc);
954 }
955
956 /* Shamelessly ripped from dlls/oleaut32/tests/olepicture.c */
957 /* 1x1 pixel gif */
958 static unsigned char gifimage[35] = {
959 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
960 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
961 0x01,0x00,0x3b
962 };
963
964 /* 1x1 pixel jpg */
965 static unsigned char jpgimage[285] = {
966 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
967 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
968 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
969 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
970 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
971 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
972 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
973 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
974 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
975 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0xff,0xc0,
976 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
977 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
978 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
979 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
980 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
981 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
982 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
983 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
984 };
985
986 /* 1x1 pixel png */
987 static unsigned char pngimage[285] = {
988 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
989 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
990 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
991 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
992 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
993 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
994 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
995 };
996
997 /* 1x1 pixel bmp with gap between palette and bitmap. Correct bitmap contains only
998    zeroes, gap is 0xFF. */
999 static unsigned char bmpimage[70] = {
1000 0x42,0x4d,0x46,0x00,0x00,0x00,0xDE,0xAD,0xBE,0xEF,0x42,0x00,0x00,0x00,0x28,0x00,
1001 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
1002 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
1003 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0x55,0x55,0x55,0x00,0xFF,0xFF,
1004 0xFF,0xFF,0x00,0x00,0x00,0x00
1005 };
1006
1007 /* 1x1 pixel bmp using BITMAPCOREHEADER */
1008 static unsigned char bmpcoreimage[38] = {
1009 0x42,0x4d,0x26,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x22,0x00,0x00,0x00,0x0c,0x00,
1010 0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xff,0xff,0xff,0x00,0x55,0x55,
1011 0x55,0x00,0x00,0x00,0x00,0x00
1012 };
1013
1014 /* 2x2 pixel gif */
1015 static unsigned char gif4pixel[42] = {
1016 0x47,0x49,0x46,0x38,0x37,0x61,0x02,0x00,0x02,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,
1017 0x39,0x62,0xfc,0xff,0x1a,0xe5,0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x02,0x00,
1018 0x02,0x00,0x00,0x02,0x03,0x14,0x16,0x05,0x00,0x3b
1019 };
1020
1021 static const DWORD biSize_tests[] = {
1022     0,
1023     sizeof(BITMAPCOREHEADER) - 1,
1024     sizeof(BITMAPCOREHEADER) + 1,
1025     sizeof(BITMAPINFOHEADER) - 1,
1026     sizeof(BITMAPINFOHEADER) + 1,
1027     sizeof(BITMAPV4HEADER) - 1,
1028     sizeof(BITMAPV4HEADER) + 1,
1029     sizeof(BITMAPV5HEADER) - 1,
1030     sizeof(BITMAPV5HEADER) + 1,
1031     (sizeof(BITMAPCOREHEADER) + sizeof(BITMAPINFOHEADER)) / 2,
1032     (sizeof(BITMAPV4HEADER) + sizeof(BITMAPV5HEADER)) / 2,
1033     0xdeadbeef,
1034     0xffffffff
1035 };
1036
1037 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
1038
1039 static void test_LoadImageBitmap(const char * test_desc, HBITMAP hbm)
1040 {
1041     BITMAP bm;
1042     BITMAPINFO bmi;
1043     DWORD ret, pixel = 0;
1044     HDC hdc = GetDC(NULL);
1045
1046     ret = GetObject(hbm, sizeof(bm), &bm);
1047     ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
1048
1049     memset(&bmi, 0, sizeof(bmi));
1050     bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
1051     bmi.bmiHeader.biWidth = bm.bmWidth;
1052     bmi.bmiHeader.biHeight = bm.bmHeight;
1053     bmi.bmiHeader.biPlanes = 1;
1054     bmi.bmiHeader.biBitCount= 24;
1055     bmi.bmiHeader.biCompression= BI_RGB;
1056     ret = GetDIBits(hdc, hbm, 0, bm.bmHeight, &pixel, &bmi, DIB_RGB_COLORS);
1057     ok(ret == bm.bmHeight, "%s: %d lines were converted, not %d\n", test_desc, ret, bm.bmHeight);
1058
1059     ok(color_match(pixel, 0x00ffffff), "%s: Pixel is 0x%08x\n", test_desc, pixel);
1060 }
1061
1062 static void test_LoadImageFile(const char * test_desc, unsigned char * image_data,
1063     unsigned int image_size, const char * ext, BOOL expect_success)
1064 {
1065     HANDLE handle;
1066     BOOL ret;
1067     DWORD error, bytes_written;
1068     char filename[64];
1069
1070     strcpy(filename, "test.");
1071     strcat(filename, ext);
1072
1073     /* Create the test image. */
1074     handle = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
1075         FILE_ATTRIBUTE_NORMAL, NULL);
1076     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
1077     ret = WriteFile(handle, image_data, image_size, &bytes_written, NULL);
1078     ok(ret && bytes_written == image_size, "test file created improperly.\n");
1079     CloseHandle(handle);
1080
1081     /* Load as cursor. For all tested formats, this should fail */
1082     SetLastError(0xdeadbeef);
1083     handle = LoadImageA(NULL, filename, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
1084     ok(handle == NULL, "%s: IMAGE_CURSOR succeeded incorrectly.\n", test_desc);
1085     error = GetLastError();
1086     ok(error == 0 ||
1087         broken(error == 0xdeadbeef) || /* Win9x */
1088         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
1089         "Last error: %u\n", error);
1090     if (handle != NULL) DestroyCursor(handle);
1091
1092     /* Load as icon. For all tested formats, this should fail */
1093     SetLastError(0xdeadbeef);
1094     handle = LoadImageA(NULL, filename, IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
1095     ok(handle == NULL, "%s: IMAGE_ICON succeeded incorrectly.\n", test_desc);
1096     error = GetLastError();
1097     ok(error == 0 ||
1098         broken(error == 0xdeadbeef) || /* Win9x */
1099         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
1100         "Last error: %u\n", error);
1101     if (handle != NULL) DestroyIcon(handle);
1102
1103     /* Load as bitmap. Should succeed for correct bmp, fail for everything else */
1104     SetLastError(0xdeadbeef);
1105     handle = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
1106     error = GetLastError();
1107     ok(error == 0 ||
1108         error == 0xdeadbeef, /* Win9x, WinMe */
1109         "Last error: %u\n", error);
1110
1111     if (expect_success) {
1112         ok(handle != NULL, "%s: IMAGE_BITMAP failed.\n", test_desc);
1113         if (handle != NULL) test_LoadImageBitmap(test_desc, handle);
1114     }
1115     else ok(handle == NULL, "%s: IMAGE_BITMAP succeeded incorrectly.\n", test_desc);
1116
1117     if (handle != NULL) DeleteObject(handle);
1118     DeleteFileA(filename);
1119 }
1120
1121 static void test_LoadImage(void)
1122 {
1123     HANDLE handle;
1124     BOOL ret;
1125     DWORD error, bytes_written;
1126     CURSORICONFILEDIR *icon_data;
1127     CURSORICONFILEDIRENTRY *icon_entry;
1128     BITMAPINFOHEADER *icon_header, *bitmap_header;
1129     ICONINFO icon_info;
1130     int i;
1131
1132 #define ICON_WIDTH 32
1133 #define ICON_HEIGHT 32
1134 #define ICON_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
1135 #define ICON_BPP 32
1136 #define ICON_SIZE \
1137     (sizeof(CURSORICONFILEDIR) + sizeof(BITMAPINFOHEADER) \
1138     + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
1139
1140     /* Set icon data. */
1141     icon_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ICON_SIZE);
1142     icon_data->idReserved = 0;
1143     icon_data->idType = 1;
1144     icon_data->idCount = 1;
1145
1146     icon_entry = icon_data->idEntries;
1147     icon_entry->bWidth = ICON_WIDTH;
1148     icon_entry->bHeight = ICON_HEIGHT;
1149     icon_entry->bColorCount = 0;
1150     icon_entry->bReserved = 0;
1151     icon_entry->xHotspot = 1;
1152     icon_entry->yHotspot = 1;
1153     icon_entry->dwDIBSize = ICON_SIZE - sizeof(CURSORICONFILEDIR);
1154     icon_entry->dwDIBOffset = sizeof(CURSORICONFILEDIR);
1155
1156     icon_header = (BITMAPINFOHEADER *) ((BYTE *) icon_data + icon_entry->dwDIBOffset);
1157     icon_header->biSize = sizeof(BITMAPINFOHEADER);
1158     icon_header->biWidth = ICON_WIDTH;
1159     icon_header->biHeight = ICON_HEIGHT*2;
1160     icon_header->biPlanes = 1;
1161     icon_header->biBitCount = ICON_BPP;
1162     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
1163
1164     /* Create the icon. */
1165     handle = CreateFileA("icon.ico", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
1166         FILE_ATTRIBUTE_NORMAL, NULL);
1167     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
1168     ret = WriteFile(handle, icon_data, ICON_SIZE, &bytes_written, NULL);
1169     ok(ret && bytes_written == ICON_SIZE, "icon.ico created improperly.\n");
1170     CloseHandle(handle);
1171
1172     /* Test loading an icon as a cursor. */
1173     SetLastError(0xdeadbeef);
1174     handle = LoadImageA(NULL, "icon.ico", IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
1175     ok(handle != NULL, "LoadImage() failed.\n");
1176     error = GetLastError();
1177     ok(error == 0 ||
1178         broken(error == 0xdeadbeef) || /* Win9x */
1179         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
1180         "Last error: %u\n", error);
1181
1182     /* Test the icon information. */
1183     SetLastError(0xdeadbeef);
1184     ret = GetIconInfo(handle, &icon_info);
1185     ok(ret, "GetIconInfo() failed.\n");
1186     error = GetLastError();
1187     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1188
1189     if (ret)
1190     {
1191         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
1192         ok(icon_info.xHotspot == 1, "xHotspot is %u.\n", icon_info.xHotspot);
1193         ok(icon_info.yHotspot == 1, "yHotspot is %u.\n", icon_info.yHotspot);
1194         ok(icon_info.hbmColor != NULL || broken(!icon_info.hbmColor) /* no color cursor support */,
1195            "No hbmColor!\n");
1196         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
1197     }
1198
1199     if (pGetIconInfoExA)
1200     {
1201         ICONINFOEXA infoex;
1202         infoex.cbSize = sizeof(infoex);
1203         ret = pGetIconInfoExA( handle, &infoex );
1204         ok( ret, "GetIconInfoEx failed err %d\n", GetLastError() );
1205         ok( infoex.wResID == 0, "GetIconInfoEx wrong resid %x\n", infoex.wResID );
1206         ok( infoex.szModName[0] == 0, "GetIconInfoEx wrong module %s\n", infoex.szModName );
1207         ok( infoex.szResName[0] == 0, "GetIconInfoEx wrong name %s\n", infoex.szResName );
1208     }
1209     else win_skip( "GetIconInfoEx not available\n" );
1210
1211     /* Clean up. */
1212     SetLastError(0xdeadbeef);
1213     ret = DestroyCursor(handle);
1214     ok(ret, "DestroyCursor() failed.\n");
1215     error = GetLastError();
1216     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1217
1218     HeapFree(GetProcessHeap(), 0, icon_data);
1219     DeleteFileA("icon.ico");
1220
1221     /* Test a system icon */
1222     handle = LoadIcon( 0, IDI_HAND );
1223     ok(handle != NULL, "LoadImage() failed.\n");
1224     if (pGetIconInfoExA)
1225     {
1226         ICONINFOEXA infoexA;
1227         ICONINFOEXW infoexW;
1228         infoexA.cbSize = sizeof(infoexA);
1229         ret = pGetIconInfoExA( handle, &infoexA );
1230         ok( ret, "GetIconInfoEx failed err %d\n", GetLastError() );
1231         ok( infoexA.wResID == (UINT_PTR)IDI_HAND, "GetIconInfoEx wrong resid %x\n", infoexA.wResID );
1232         /* the A version is broken on 64-bit, it truncates the string after the first char */
1233         if (is_win64 && infoexA.szModName[0] && infoexA.szModName[1] == 0)
1234             trace( "GetIconInfoExA broken on Win64\n" );
1235         else
1236             ok( GetModuleHandleA(infoexA.szModName) == GetModuleHandleA("user32.dll"),
1237                 "GetIconInfoEx wrong module %s\n", infoexA.szModName );
1238         ok( infoexA.szResName[0] == 0, "GetIconInfoEx wrong name %s\n", infoexA.szResName );
1239         infoexW.cbSize = sizeof(infoexW);
1240         ret = pGetIconInfoExW( handle, &infoexW );
1241         ok( ret, "GetIconInfoEx failed err %d\n", GetLastError() );
1242         ok( infoexW.wResID == (UINT_PTR)IDI_HAND, "GetIconInfoEx wrong resid %x\n", infoexW.wResID );
1243         ok( GetModuleHandleW(infoexW.szModName) == GetModuleHandleA("user32.dll"),
1244             "GetIconInfoEx wrong module %s\n", wine_dbgstr_w(infoexW.szModName) );
1245         ok( infoexW.szResName[0] == 0, "GetIconInfoEx wrong name %s\n", wine_dbgstr_w(infoexW.szResName) );
1246     }
1247     SetLastError(0xdeadbeef);
1248     DestroyIcon(handle);
1249
1250     test_LoadImageFile("BMP", bmpimage, sizeof(bmpimage), "bmp", 1);
1251     test_LoadImageFile("BMP (coreinfo)", bmpcoreimage, sizeof(bmpcoreimage), "bmp", 1);
1252     test_LoadImageFile("GIF", gifimage, sizeof(gifimage), "gif", 0);
1253     test_LoadImageFile("GIF (2x2 pixel)", gif4pixel, sizeof(gif4pixel), "gif", 0);
1254     test_LoadImageFile("JPG", jpgimage, sizeof(jpgimage), "jpg", 0);
1255     test_LoadImageFile("PNG", pngimage, sizeof(pngimage), "png", 0);
1256
1257     /* Check failure for broken BMP images */
1258     bitmap_header = (BITMAPINFOHEADER *)(bmpimage + sizeof(BITMAPFILEHEADER));
1259
1260     bitmap_header->biHeight = 65536;
1261     test_LoadImageFile("BMP (too high)", bmpimage, sizeof(bmpimage), "bmp", 0);
1262     bitmap_header->biHeight = 1;
1263
1264     bitmap_header->biWidth = 65536;
1265     test_LoadImageFile("BMP (too wide)", bmpimage, sizeof(bmpimage), "bmp", 0);
1266     bitmap_header->biWidth = 1;
1267
1268     for (i = 0; i < ARRAY_SIZE(biSize_tests); i++) {
1269         bitmap_header->biSize = biSize_tests[i];
1270         test_LoadImageFile("BMP (broken biSize)", bmpimage, sizeof(bmpimage), "bmp", 0);
1271     }
1272     bitmap_header->biSize = sizeof(BITMAPINFOHEADER);
1273 }
1274
1275 #undef ARRAY_SIZE
1276
1277 static void test_CreateIconFromResource(void)
1278 {
1279     HANDLE handle;
1280     BOOL ret;
1281     DWORD error;
1282     BITMAPINFOHEADER *icon_header;
1283     INT16 *hotspot;
1284     ICONINFO icon_info;
1285
1286 #define ICON_RES_WIDTH 32
1287 #define ICON_RES_HEIGHT 32
1288 #define ICON_RES_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
1289 #define ICON_RES_BPP 32
1290 #define ICON_RES_SIZE \
1291     (sizeof(BITMAPINFOHEADER) + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
1292 #define CRSR_RES_SIZE (2*sizeof(INT16) + ICON_RES_SIZE)
1293
1294     /* Set icon data. */
1295     hotspot = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, CRSR_RES_SIZE);
1296
1297     /* Cursor resources have an extra hotspot, icon resources not. */
1298     hotspot[0] = 3;
1299     hotspot[1] = 3;
1300
1301     icon_header = (BITMAPINFOHEADER *) (hotspot + 2);
1302     icon_header->biSize = sizeof(BITMAPINFOHEADER);
1303     icon_header->biWidth = ICON_WIDTH;
1304     icon_header->biHeight = ICON_HEIGHT*2;
1305     icon_header->biPlanes = 1;
1306     icon_header->biBitCount = ICON_BPP;
1307     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
1308
1309     /* Test creating a cursor. */
1310     SetLastError(0xdeadbeef);
1311     handle = CreateIconFromResource((PBYTE) hotspot, CRSR_RES_SIZE, FALSE, 0x00030000);
1312     ok(handle != NULL, "Create cursor failed.\n");
1313
1314     /* Test the icon information. */
1315     SetLastError(0xdeadbeef);
1316     ret = GetIconInfo(handle, &icon_info);
1317     ok(ret, "GetIconInfo() failed.\n");
1318     error = GetLastError();
1319     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1320
1321     if (ret)
1322     {
1323         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
1324         ok(icon_info.xHotspot == 3, "xHotspot is %u.\n", icon_info.xHotspot);
1325         ok(icon_info.yHotspot == 3, "yHotspot is %u.\n", icon_info.yHotspot);
1326         ok(icon_info.hbmColor != NULL || broken(!icon_info.hbmColor) /* no color cursor support */,
1327            "No hbmColor!\n");
1328         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
1329     }
1330
1331     if (pGetIconInfoExA)
1332     {
1333         ICONINFOEXA infoex;
1334         infoex.cbSize = sizeof(infoex);
1335         ret = pGetIconInfoExA( handle, &infoex );
1336         ok( ret, "GetIconInfoEx failed err %d\n", GetLastError() );
1337         ok( infoex.wResID == 0, "GetIconInfoEx wrong resid %x\n", infoex.wResID );
1338         ok( infoex.szModName[0] == 0, "GetIconInfoEx wrong module %s\n", infoex.szModName );
1339         ok( infoex.szResName[0] == 0, "GetIconInfoEx wrong name %s\n", infoex.szResName );
1340     }
1341
1342     /* Clean up. */
1343     SetLastError(0xdeadbeef);
1344     ret = DestroyCursor(handle);
1345     ok(ret, "DestroyCursor() failed.\n");
1346     error = GetLastError();
1347     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1348
1349     /* Test creating an icon. */
1350     SetLastError(0xdeadbeef);
1351     handle = CreateIconFromResource((PBYTE) icon_header, ICON_RES_SIZE, TRUE,
1352                                     0x00030000);
1353     ok(handle != NULL, "Create icon failed.\n");
1354
1355     /* Test the icon information. */
1356     SetLastError(0xdeadbeef);
1357     ret = GetIconInfo(handle, &icon_info);
1358     ok(ret, "GetIconInfo() failed.\n");
1359     error = GetLastError();
1360     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1361
1362     if (ret)
1363     {
1364         ok(icon_info.fIcon == TRUE, "fIcon != TRUE.\n");
1365         /* Icons always have hotspot in the middle */
1366         ok(icon_info.xHotspot == ICON_WIDTH/2, "xHotspot is %u.\n", icon_info.xHotspot);
1367         ok(icon_info.yHotspot == ICON_HEIGHT/2, "yHotspot is %u.\n", icon_info.yHotspot);
1368         ok(icon_info.hbmColor != NULL, "No hbmColor!\n");
1369         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
1370     }
1371
1372     /* Clean up. */
1373     SetLastError(0xdeadbeef);
1374     ret = DestroyCursor(handle);
1375     ok(ret, "DestroyCursor() failed.\n");
1376     error = GetLastError();
1377     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1378
1379     /* Rejection of NULL pointer crashes at least on WNT4WSSP6, W2KPROSP4, WXPPROSP3
1380      *
1381      * handle = CreateIconFromResource(NULL, ICON_RES_SIZE, TRUE, 0x00030000);
1382      * ok(handle == NULL, "Invalid pointer accepted (%p)\n", handle);
1383      */
1384     HeapFree(GetProcessHeap(), 0, hotspot);
1385
1386     /* Test creating an animated cursor. */
1387     empty_anicursor.frames[0].data.icon_info.idType = 2; /* type: cursor */
1388     empty_anicursor.frames[0].data.icon_info.idEntries[0].xHotspot = 3;
1389     empty_anicursor.frames[0].data.icon_info.idEntries[0].yHotspot = 3;
1390     handle = CreateIconFromResource((PBYTE) &empty_anicursor, sizeof(empty_anicursor), FALSE, 0x00030000);
1391     ok(handle != NULL, "Create cursor failed.\n");
1392
1393     /* Test the animated cursor's information. */
1394     SetLastError(0xdeadbeef);
1395     ret = GetIconInfo(handle, &icon_info);
1396     ok(ret, "GetIconInfo() failed.\n");
1397     error = GetLastError();
1398     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1399
1400     if (ret)
1401     {
1402         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
1403         ok(icon_info.xHotspot == 3, "xHotspot is %u.\n", icon_info.xHotspot);
1404         ok(icon_info.yHotspot == 3, "yHotspot is %u.\n", icon_info.yHotspot);
1405         ok(icon_info.hbmColor != NULL || broken(!icon_info.hbmColor) /* no color cursor support */,
1406            "No hbmColor!\n");
1407         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
1408     }
1409
1410     /* Clean up. */
1411     SetLastError(0xdeadbeef);
1412     ret = DestroyCursor(handle);
1413     ok(ret, "DestroyCursor() failed.\n");
1414     error = GetLastError();
1415     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1416 }
1417
1418 static int check_cursor_data( HDC hdc, HCURSOR hCursor, void *data, int length)
1419 {
1420     char *image = NULL;
1421     BITMAPINFO *info;
1422     ICONINFO iinfo;
1423     DWORD ret;
1424     int i;
1425
1426     ret = GetIconInfo( hCursor, &iinfo );
1427     ok(ret, "GetIconInfo() failed\n");
1428     if (!ret) return 0;
1429     ret = 0;
1430     info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ));
1431     ok(info != NULL, "HeapAlloc() failed\n");
1432     if (!info) return 0;
1433
1434     info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1435     info->bmiHeader.biWidth = 32;
1436     info->bmiHeader.biHeight = 32;
1437     info->bmiHeader.biPlanes = 1;
1438     info->bmiHeader.biBitCount = 32;
1439     info->bmiHeader.biCompression = BI_RGB;
1440     info->bmiHeader.biSizeImage = 32 * 32 * 4;
1441     info->bmiHeader.biXPelsPerMeter = 0;
1442     info->bmiHeader.biYPelsPerMeter = 0;
1443     info->bmiHeader.biClrUsed = 0;
1444     info->bmiHeader.biClrImportant = 0;
1445     image = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage );
1446     ok(image != NULL, "HeapAlloc() failed\n");
1447     if (!image) goto cleanup;
1448     ret = GetDIBits( hdc, iinfo.hbmColor, 0, 32, image, info, DIB_RGB_COLORS );
1449     ok(ret, "GetDIBits() failed\n");
1450     for (i = 0; ret && i < length / sizeof(COLORREF); i++)
1451     {
1452         ret = color_match( ((COLORREF *)data)[i], ((COLORREF *)image)[i] );
1453         ok(ret, "%04x: Expected 0x%x, actually 0x%x\n", i, ((COLORREF *)data)[i], ((COLORREF *)image)[i] );
1454     }
1455 cleanup:
1456     HeapFree( GetProcessHeap(), 0, image );
1457     HeapFree( GetProcessHeap(), 0, info );
1458     return ret;
1459 }
1460
1461 static HCURSOR (WINAPI *pGetCursorFrameInfo)(HCURSOR hCursor, DWORD unk1, DWORD istep, DWORD *rate, DWORD *steps);
1462 static void test_GetCursorFrameInfo(void)
1463 {
1464     DWORD frame_identifier[] = { 0x10Ad, 0xc001, 0x1c05 };
1465     HBITMAP bmp = NULL, bmpOld = NULL;
1466     DWORD rate, steps;
1467     BITMAPINFOHEADER *icon_header;
1468     BITMAPINFO bitmapInfo;
1469     HDC hdc = NULL;
1470     void *bits = 0;
1471     INT16 *hotspot;
1472     HANDLE h1, h2;
1473     BOOL ret;
1474     int i;
1475
1476     if (!pGetCursorFrameInfo)
1477     {
1478         win_skip( "GetCursorFrameInfo not supported, skipping tests.\n" );
1479         return;
1480     }
1481
1482     hdc = CreateCompatibleDC(0);
1483     ok(hdc != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1484     if (!hdc)
1485         return;
1486
1487     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1488     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1489     bitmapInfo.bmiHeader.biWidth = 3;
1490     bitmapInfo.bmiHeader.biHeight = 3;
1491     bitmapInfo.bmiHeader.biBitCount = 32;
1492     bitmapInfo.bmiHeader.biPlanes = 1;
1493     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1494     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1495     bmp = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1496     ok (bmp && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1497     if (!bmp || !bits)
1498         goto cleanup;
1499     bmpOld = SelectObject(hdc, bmp);
1500
1501 #define ICON_RES_WIDTH 32
1502 #define ICON_RES_HEIGHT 32
1503 #define ICON_RES_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
1504 #define ICON_RES_BPP 32
1505 #define ICON_RES_SIZE \
1506     (sizeof(BITMAPINFOHEADER) + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
1507 #define CRSR_RES_SIZE (2*sizeof(INT16) + ICON_RES_SIZE)
1508
1509     /* Set icon data. */
1510     hotspot = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, CRSR_RES_SIZE);
1511
1512     /* Cursor resources have an extra hotspot, icon resources not. */
1513     hotspot[0] = 3;
1514     hotspot[1] = 3;
1515
1516     icon_header = (BITMAPINFOHEADER *) (hotspot + 2);
1517     icon_header->biSize = sizeof(BITMAPINFOHEADER);
1518     icon_header->biWidth = ICON_WIDTH;
1519     icon_header->biHeight = ICON_HEIGHT*2;
1520     icon_header->biPlanes = 1;
1521     icon_header->biBitCount = ICON_BPP;
1522     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
1523
1524     /* Creating a static cursor. */
1525     SetLastError(0xdeadbeef);
1526     h1 = CreateIconFromResource((PBYTE) hotspot, CRSR_RES_SIZE, FALSE, 0x00030000);
1527     ok(h1 != NULL, "Create cursor failed (error = %d).\n", GetLastError());
1528
1529     /* Check GetCursorFrameInfo behavior on a static cursor */
1530     rate = steps = 0xdead;
1531     h2 = pGetCursorFrameInfo(h1, 0xdead, 0xdead, &rate, &steps);
1532     ok(h1 == h2, "GetCursorFrameInfo() failed: (%p != %p).\n", h1, h2);
1533     ok(rate == 0, "GetCursorFrameInfo() unexpected param 4 value (0x%x != 0x0).\n", rate);
1534     ok(steps == 1, "GetCursorFrameInfo() unexpected param 5 value (%d != 1).\n", steps);
1535
1536     /* Clean up static cursor. */
1537     SetLastError(0xdeadbeef);
1538     ret = DestroyCursor(h1);
1539     ok(ret, "DestroyCursor() failed (error = %d).\n", GetLastError());
1540
1541     /* Creating a single-frame animated cursor. */
1542     empty_anicursor.frames[0].data.icon_info.idType = 2; /* type: cursor */
1543     empty_anicursor.frames[0].data.icon_info.idEntries[0].xHotspot = 3;
1544     empty_anicursor.frames[0].data.icon_info.idEntries[0].yHotspot = 3;
1545     memcpy( &empty_anicursor.frames[0].data.bmi_data.data[0], &frame_identifier[0], sizeof(DWORD) );
1546     SetLastError(0xdeadbeef);
1547     h1 = CreateIconFromResource((PBYTE) &empty_anicursor, sizeof(empty_anicursor), FALSE, 0x00030000);
1548     ok(h1 != NULL, "Create cursor failed (error = %d).\n", GetLastError());
1549
1550     /* Check GetCursorFrameInfo behavior on a single-frame animated cursor */
1551     rate = steps = 0xdead;
1552     h2 = pGetCursorFrameInfo(h1, 0xdead, 0, &rate, &steps);
1553     ok(h1 == h2, "GetCursorFrameInfo() failed: (%p != %p).\n", h1, h2);
1554     ret = check_cursor_data( hdc, h2, &frame_identifier[0], sizeof(DWORD) );
1555     ok(ret, "GetCursorFrameInfo() returned wrong cursor data for frame 0.\n");
1556     ok(rate == 0x0, "GetCursorFrameInfo() unexpected param 4 value (0x%x != 0x0).\n", rate);
1557     ok(steps == empty_anicursor.header.header.num_steps,
1558         "GetCursorFrameInfo() unexpected param 5 value (%d != 1).\n", steps);
1559
1560     /* Clean up single-frame animated cursor. */
1561     SetLastError(0xdeadbeef);
1562     ret = DestroyCursor(h1);
1563     ok(ret, "DestroyCursor() failed (error = %d).\n", GetLastError());
1564
1565     /* Creating a multi-frame animated cursor. */
1566     for (i=0; i<empty_anicursor3.header.header.num_frames; i++)
1567     {
1568         empty_anicursor3.frames[i].data.icon_info.idType = 2; /* type: cursor */
1569         empty_anicursor3.frames[i].data.icon_info.idEntries[0].xHotspot = 3;
1570         empty_anicursor3.frames[i].data.icon_info.idEntries[0].yHotspot = 3;
1571         memcpy( &empty_anicursor3.frames[i].data.bmi_data.data[0], &frame_identifier[i], sizeof(DWORD) );
1572     }
1573     SetLastError(0xdeadbeef);
1574     h1 = CreateIconFromResource((PBYTE) &empty_anicursor3, sizeof(empty_anicursor3), FALSE, 0x00030000);
1575     ok(h1 != NULL, "Create cursor failed (error = %d).\n", GetLastError());
1576
1577     /* Check number of steps in multi-frame animated cursor */
1578     i=0;
1579     while (DrawIconEx(hdc, 0, 0, h1, 32, 32, i, NULL, DI_NORMAL))
1580         i++;
1581     ok(i == empty_anicursor3.header.header.num_steps,
1582         "Unexpected number of steps in cursor (%d != %d)\n",
1583         i, empty_anicursor3.header.header.num_steps);
1584
1585     /* Check GetCursorFrameInfo behavior on a multi-frame animated cursor */
1586     for (i=0; i<empty_anicursor3.header.header.num_frames; i++)
1587     {
1588         rate = steps = 0xdead;
1589         h2 = pGetCursorFrameInfo(h1, 0xdead, i, &rate, &steps);
1590         ok(h1 != h2 && h2 != 0, "GetCursorFrameInfo() failed for cursor %p: (%p, %p).\n", h1, h1, h2);
1591         ret = check_cursor_data( hdc, h2, &frame_identifier[i], sizeof(DWORD) );
1592         ok(ret, "GetCursorFrameInfo() returned wrong cursor data for frame %d.\n", i);
1593         ok(rate == empty_anicursor3.header.header.display_rate,
1594             "GetCursorFrameInfo() unexpected param 4 value (0x%x != 0x%x).\n",
1595             rate, empty_anicursor3.header.header.display_rate);
1596         ok(steps == empty_anicursor3.header.header.num_steps,
1597             "GetCursorFrameInfo() unexpected param 5 value (%d != %d).\n",
1598             steps, empty_anicursor3.header.header.num_steps);
1599     }
1600
1601     /* Check GetCursorFrameInfo behavior on rate 3 of a multi-frame animated cursor */
1602     rate = steps = 0xdead;
1603     h2 = pGetCursorFrameInfo(h1, 0xdead, 3, &rate, &steps);
1604     ok(h2 == 0, "GetCursorFrameInfo() failed for cursor %p: (%p != 0).\n", h1, h2);
1605     ok(rate == 0xdead || broken(rate == empty_anicursor3.header.header.display_rate) /*win2k*/
1606        || broken(rate == ~0) /*win2k (sporadic)*/,
1607         "GetCursorFrameInfo() unexpected param 4 value (0x%x != 0xdead).\n", rate);
1608     ok(steps == 0xdead || broken(steps == empty_anicursor3.header.header.num_steps) /*win2k*/
1609        || broken(steps == 0) /*win2k (sporadic)*/,
1610         "GetCursorFrameInfo() unexpected param 5 value (0x%x != 0xdead).\n", steps);
1611
1612     /* Clean up multi-frame animated cursor. */
1613     SetLastError(0xdeadbeef);
1614     ret = DestroyCursor(h1);
1615     ok(ret, "DestroyCursor() failed (error = %d).\n", GetLastError());
1616
1617     /* Create a multi-frame animated cursor with num_steps == 1 */
1618     empty_anicursor3.header.header.num_steps = 1;
1619     SetLastError(0xdeadbeef);
1620     h1 = CreateIconFromResource((PBYTE) &empty_anicursor3, sizeof(empty_anicursor3), FALSE, 0x00030000);
1621     ok(h1 != NULL, "Create cursor failed (error = %d).\n", GetLastError());
1622
1623     /* Check number of steps in multi-frame animated cursor (mismatch between steps and frames) */
1624     i=0;
1625     while (DrawIconEx(hdc, 0, 0, h1, 32, 32, i, NULL, DI_NORMAL))
1626         i++;
1627     ok(i == empty_anicursor3.header.header.num_steps,
1628         "Unexpected number of steps in cursor (%d != %d)\n",
1629         i, empty_anicursor3.header.header.num_steps);
1630
1631     /* Check GetCursorFrameInfo behavior on rate 0 for a multi-frame animated cursor (with num_steps == 1) */
1632     rate = steps = 0xdead;
1633     h2 = pGetCursorFrameInfo(h1, 0xdead, 0, &rate, &steps);
1634     ok(h1 != h2 && h2 != 0, "GetCursorFrameInfo() failed for cursor %p: (%p, %p).\n", h1, h1, h2);
1635     ret = check_cursor_data( hdc, h2, &frame_identifier[0], sizeof(DWORD) );
1636     ok(ret, "GetCursorFrameInfo() returned wrong cursor data for frame 0.\n");
1637     ok(rate == empty_anicursor3.header.header.display_rate,
1638         "GetCursorFrameInfo() unexpected param 4 value (0x%x != 0x%x).\n",
1639         rate, empty_anicursor3.header.header.display_rate);
1640     ok(steps == ~0 || broken(steps == empty_anicursor3.header.header.num_steps) /*win2k*/,
1641         "GetCursorFrameInfo() unexpected param 5 value (%d != ~0).\n", steps);
1642
1643     /* Check GetCursorFrameInfo behavior on rate 1 for a multi-frame animated cursor (with num_steps == 1) */
1644     rate = steps = 0xdead;
1645     h2 = pGetCursorFrameInfo(h1, 0xdead, 1, &rate, &steps);
1646     ok(h2 == 0, "GetCursorFrameInfo() failed for cursor %p: (%p != 0).\n", h1, h2);
1647     ok(rate == 0xdead || broken(rate == empty_anicursor3.header.header.display_rate) /*win2k*/
1648        || broken(rate == ~0) /*win2k (sporadic)*/,
1649         "GetCursorFrameInfo() unexpected param 4 value (0x%x != 0xdead).\n", rate);
1650     ok(steps == 0xdead || broken(steps == empty_anicursor3.header.header.num_steps) /*win2k*/
1651        || broken(steps == 0) /*win2k (sporadic)*/,
1652         "GetCursorFrameInfo() unexpected param 5 value (%d != 0xdead).\n", steps);
1653
1654     /* Clean up multi-frame animated cursor. */
1655     SetLastError(0xdeadbeef);
1656     ret = DestroyCursor(h1);
1657     ok(ret, "DestroyCursor() failed (error = %d).\n", GetLastError());
1658
1659     /* Creating a multi-frame animated cursor with rate data. */
1660     for (i=0; i<empty_anicursor3_seq.header.header.num_frames; i++)
1661     {
1662         empty_anicursor3_seq.frames[i].data.icon_info.idType = 2; /* type: cursor */
1663         empty_anicursor3_seq.frames[i].data.icon_info.idEntries[0].xHotspot = 3;
1664         empty_anicursor3_seq.frames[i].data.icon_info.idEntries[0].yHotspot = 3;
1665         memcpy( &empty_anicursor3_seq.frames[i].data.bmi_data.data[0], &frame_identifier[i], sizeof(DWORD) );
1666     }
1667     SetLastError(0xdeadbeef);
1668     h1 = CreateIconFromResource((PBYTE) &empty_anicursor3_seq, sizeof(empty_anicursor3_seq), FALSE, 0x00030000);
1669     ok(h1 != NULL, "Create cursor failed (error = %x).\n", GetLastError());
1670
1671     /* Check number of steps in multi-frame animated cursor with rate data */
1672     i=0;
1673     while (DrawIconEx(hdc, 0, 0, h1, 32, 32, i, NULL, DI_NORMAL))
1674         i++;
1675     ok(i == empty_anicursor3_seq.header.header.num_steps,
1676         "Unexpected number of steps in cursor (%d != %d)\n",
1677         i, empty_anicursor3_seq.header.header.num_steps);
1678
1679     /* Check GetCursorFrameInfo behavior on a multi-frame animated cursor with rate data */
1680     for (i=0; i<empty_anicursor3_seq.header.header.num_frames; i++)
1681     {
1682         int frame_id = empty_anicursor3_seq.seq.order[i];
1683
1684         rate = steps = 0xdead;
1685         h2 = pGetCursorFrameInfo(h1, 0xdead, i, &rate, &steps);
1686         ok(h1 != h2 && h2 != 0, "GetCursorFrameInfo() failed for cursor %p: (%p, %p).\n", h1, h1, h2);
1687         ret = check_cursor_data( hdc, h2, &frame_identifier[frame_id], sizeof(DWORD) );
1688         ok(ret, "GetCursorFrameInfo() returned wrong cursor data for frame %d.\n", i);
1689         ok(rate == empty_anicursor3_seq.rates.rate[i],
1690             "GetCursorFrameInfo() unexpected param 4 value (0x%x != 0x%x).\n",
1691             rate, empty_anicursor3_seq.rates.rate[i]);
1692         ok(steps == empty_anicursor3_seq.header.header.num_steps,
1693             "GetCursorFrameInfo() unexpected param 5 value (%d != %d).\n",
1694             steps, empty_anicursor3_seq.header.header.num_steps);
1695     }
1696
1697     /* Clean up multi-frame animated cursor with rate data. */
1698     SetLastError(0xdeadbeef);
1699     ret = DestroyCursor(h1);
1700     ok(ret, "DestroyCursor() failed (error = %d).\n", GetLastError());
1701
1702 cleanup:
1703     if(bmpOld) SelectObject(hdc, bmpOld);
1704     if(bmp) DeleteObject(bmp);
1705     if(hdc) DeleteDC(hdc);
1706 }
1707
1708 static HICON create_test_icon(HDC hdc, int width, int height, int bpp,
1709                               BOOL maskvalue, UINT32 *color, int colorSize)
1710 {
1711     ICONINFO iconInfo;
1712     BITMAPINFO bitmapInfo;
1713     void *buffer = NULL;
1714     UINT32 mask = maskvalue ? 0xFFFFFFFF : 0x00000000;
1715
1716     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1717     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1718     bitmapInfo.bmiHeader.biWidth = width;
1719     bitmapInfo.bmiHeader.biHeight = height;
1720     bitmapInfo.bmiHeader.biPlanes = 1;
1721     bitmapInfo.bmiHeader.biBitCount = bpp;
1722     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1723     bitmapInfo.bmiHeader.biSizeImage = colorSize;
1724
1725     iconInfo.fIcon = TRUE;
1726     iconInfo.xHotspot = 0;
1727     iconInfo.yHotspot = 0;
1728
1729     iconInfo.hbmMask = CreateBitmap( width, height, 1, 1, &mask );
1730     if(!iconInfo.hbmMask) return NULL;
1731
1732     iconInfo.hbmColor = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, &buffer, NULL, 0);
1733     if(!iconInfo.hbmColor || !buffer)
1734     {
1735         DeleteObject(iconInfo.hbmMask);
1736         return NULL;
1737     }
1738
1739     memcpy(buffer, color, colorSize);
1740
1741     return CreateIconIndirect(&iconInfo);
1742 }
1743
1744 static void check_alpha_draw(HDC hdc, BOOL drawiconex, BOOL alpha, int bpp, int line)
1745 {
1746     HICON hicon;
1747     UINT32 color[2];
1748     COLORREF modern_expected, legacy_expected, result;
1749
1750     color[0] = 0x00A0B0C0;
1751     color[1] = alpha ? 0xFF000000 : 0x00000000;
1752     modern_expected = alpha ? 0x00FFFFFF : 0x00C0B0A0;
1753     legacy_expected = 0x00C0B0A0;
1754
1755     hicon = create_test_icon(hdc, 2, 1, bpp, 0, color, sizeof(color));
1756     if (!hicon) return;
1757
1758     SetPixelV(hdc, 0, 0, 0x00FFFFFF);
1759
1760     if(drawiconex)
1761         DrawIconEx(hdc, 0, 0, hicon, 2, 1, 0, NULL, DI_NORMAL);
1762     else
1763         DrawIcon(hdc, 0, 0, hicon);
1764
1765     result = GetPixel(hdc, 0, 0);
1766     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1767         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1768         "%s. Expected a close match to %06X (modern) or %06X (legacy) with %s. "
1769         "Got %06X from line %d\n",
1770         alpha ? "Alpha blending" : "Not alpha blending", modern_expected, legacy_expected,
1771         drawiconex ? "DrawIconEx" : "DrawIcon", result, line);
1772 }
1773
1774 static void check_DrawIcon(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, COLORREF background,
1775                            COLORREF modern_expected, COLORREF legacy_expected, int line)
1776 {
1777     COLORREF result;
1778     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1779     if (!hicon) return;
1780     SetPixelV(hdc, 0, 0, background);
1781     SetPixelV(hdc, GetSystemMetrics(SM_CXICON)-1, GetSystemMetrics(SM_CYICON)-1, background);
1782     SetPixelV(hdc, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), background);
1783     DrawIcon(hdc, 0, 0, hicon);
1784     result = GetPixel(hdc, 0, 0);
1785
1786     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1787         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1788         "Overlaying Mask %d on Color %06X with DrawIcon. "
1789         "Expected a close match to %06X (modern), or %06X (legacy). Got %06X from line %d\n",
1790         maskvalue, color, modern_expected, legacy_expected, result, line);
1791
1792     result = GetPixel(hdc, GetSystemMetrics(SM_CXICON)-1, GetSystemMetrics(SM_CYICON)-1);
1793
1794     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1795         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1796         "Overlaying Mask %d on Color %06X with DrawIcon. "
1797         "Expected a close match to %06X (modern), or %06X (legacy). Got %06X from line %d\n",
1798         maskvalue, color, modern_expected, legacy_expected, result, line);
1799
1800     result = GetPixel(hdc, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
1801
1802     ok (color_match(result, background),
1803         "Overlaying Mask %d on Color %06X with DrawIcon. "
1804         "Expected unchanged background color %06X. Got %06X from line %d\n",
1805         maskvalue, color, background, result, line);
1806 }
1807
1808 static void test_DrawIcon(void)
1809 {
1810     BITMAPINFO bitmapInfo;
1811     HDC hdcDst = NULL;
1812     HBITMAP bmpDst = NULL;
1813     HBITMAP bmpOld = NULL;
1814     void *bits = 0;
1815
1816     hdcDst = CreateCompatibleDC(0);
1817     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1818     if (!hdcDst)
1819         return;
1820
1821     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1822     {
1823         skip("Windows will distort DrawIcon colors at 8-bpp and less due to palettizing.\n");
1824         goto cleanup;
1825     }
1826
1827     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1828     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1829     bitmapInfo.bmiHeader.biWidth = GetSystemMetrics(SM_CXICON)+1;
1830     bitmapInfo.bmiHeader.biHeight = GetSystemMetrics(SM_CYICON)+1;
1831     bitmapInfo.bmiHeader.biBitCount = 32;
1832     bitmapInfo.bmiHeader.biPlanes = 1;
1833     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1834     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1835
1836     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1837     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1838     if (!bmpDst || !bits)
1839         goto cleanup;
1840     bmpOld = SelectObject(hdcDst, bmpDst);
1841
1842     /* Mask is only heeded if alpha channel is always zero */
1843     check_DrawIcon(hdcDst, FALSE, 0x00A0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1844     check_DrawIcon(hdcDst, TRUE, 0x00A0B0C0, 32, 0x00FFFFFF, 0x003F4F5F, 0x003F4F5F, __LINE__);
1845
1846     /* Test alpha blending */
1847     /* Windows 2000 and up will alpha blend, earlier Windows versions will not */
1848     check_DrawIcon(hdcDst, FALSE, 0xFFA0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1849     check_DrawIcon(hdcDst, TRUE, 0xFFA0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x003F4F5F, __LINE__);
1850
1851     check_DrawIcon(hdcDst, FALSE, 0x80A0B0C0, 32, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1852     check_DrawIcon(hdcDst, TRUE, 0x80A0B0C0, 32, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1853     check_DrawIcon(hdcDst, FALSE, 0x80A0B0C0, 32, 0x00FFFFFF, 0x00DFD7CF, 0x00C0B0A0, __LINE__);
1854     check_DrawIcon(hdcDst, TRUE, 0x80A0B0C0, 32, 0x00FFFFFF, 0x00DFD7CF, 0x003F4F5F, __LINE__);
1855
1856     check_DrawIcon(hdcDst, FALSE, 0x01FFFFFF, 32, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1857     check_DrawIcon(hdcDst, TRUE, 0x01FFFFFF, 32, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1858
1859     /* Test detecting of alpha channel */
1860     /* If a single pixel's alpha channel is non-zero, the icon
1861        will be alpha blended, otherwise it will be draw with
1862        and + xor blts. */
1863     check_alpha_draw(hdcDst, FALSE, FALSE, 32, __LINE__);
1864     check_alpha_draw(hdcDst, FALSE, TRUE, 32, __LINE__);
1865
1866 cleanup:
1867     if(bmpOld)
1868         SelectObject(hdcDst, bmpOld);
1869     if(bmpDst)
1870         DeleteObject(bmpDst);
1871     if(hdcDst)
1872         DeleteDC(hdcDst);
1873 }
1874
1875 static void check_DrawIconEx(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, UINT flags, COLORREF background,
1876                              COLORREF modern_expected, COLORREF legacy_expected, int line)
1877 {
1878     COLORREF result;
1879     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1880     if (!hicon) return;
1881     SetPixelV(hdc, 0, 0, background);
1882     DrawIconEx(hdc, 0, 0, hicon, 1, 1, 0, NULL, flags);
1883     result = GetPixel(hdc, 0, 0);
1884
1885     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1886         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1887         "Overlaying Mask %d on Color %06X with DrawIconEx flags %08X. "
1888         "Expected a close match to %06X (modern) or %06X (legacy). Got %06X from line %d\n",
1889         maskvalue, color, flags, modern_expected, legacy_expected, result, line);
1890 }
1891
1892 static void test_DrawIconEx(void)
1893 {
1894     BITMAPINFO bitmapInfo;
1895     HDC hdcDst = NULL;
1896     HBITMAP bmpDst = NULL;
1897     HBITMAP bmpOld = NULL;
1898     void *bits = 0;
1899
1900     hdcDst = CreateCompatibleDC(0);
1901     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1902     if (!hdcDst)
1903         return;
1904
1905     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1906     {
1907         skip("Windows will distort DrawIconEx colors at 8-bpp and less due to palettizing.\n");
1908         goto cleanup;
1909     }
1910
1911     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1912     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1913     bitmapInfo.bmiHeader.biWidth = 1;
1914     bitmapInfo.bmiHeader.biHeight = 1;
1915     bitmapInfo.bmiHeader.biBitCount = 32;
1916     bitmapInfo.bmiHeader.biPlanes = 1;
1917     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1918     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1919     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1920     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1921     if (!bmpDst || !bits)
1922         goto cleanup;
1923     bmpOld = SelectObject(hdcDst, bmpDst);
1924
1925     /* Test null, image only, and mask only drawing */
1926     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, 0, 0x00102030, 0x00102030, 0x00102030, __LINE__);
1927     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, 0, 0x00102030, 0x00102030, 0x00102030, __LINE__);
1928
1929     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_MASK, 0x00123456, 0x00000000, 0x00000000, __LINE__);
1930     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_MASK, 0x00123456, 0x00FFFFFF, 0x00FFFFFF, __LINE__);
1931
1932     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, DI_IMAGE, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1933     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, DI_IMAGE, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1934
1935     /* Test normal drawing */
1936     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1937     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x003F4F5F, 0x003F4F5F, __LINE__);
1938     check_DrawIconEx(hdcDst, FALSE, 0xFFA0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1939
1940     /* Test alpha blending */
1941     /* Windows 2000 and up will alpha blend, earlier Windows versions will not */
1942     check_DrawIconEx(hdcDst, TRUE, 0xFFA0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x003F4F5F, __LINE__);
1943
1944     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_NORMAL, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1945     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_NORMAL, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1946     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00DFD7CF, 0x00C0B0A0, __LINE__);
1947     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00DFD7CF, 0x003F4F5F, __LINE__);
1948
1949     check_DrawIconEx(hdcDst, FALSE, 0x01FFFFFF, 32, DI_NORMAL, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1950     check_DrawIconEx(hdcDst, TRUE, 0x01FFFFFF, 32, DI_NORMAL, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1951
1952     /* Test detecting of alpha channel */
1953     /* If a single pixel's alpha channel is non-zero, the icon
1954        will be alpha blended, otherwise it will be draw with
1955        and + xor blts. */
1956     check_alpha_draw(hdcDst, TRUE, FALSE, 32, __LINE__);
1957     check_alpha_draw(hdcDst, TRUE, TRUE, 32, __LINE__);
1958
1959 cleanup:
1960     if(bmpOld)
1961         SelectObject(hdcDst, bmpOld);
1962     if(bmpDst)
1963         DeleteObject(bmpDst);
1964     if(hdcDst)
1965         DeleteDC(hdcDst);
1966 }
1967
1968 static void check_DrawState_Size(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, HBRUSH hbr, UINT flags, int line)
1969 {
1970     COLORREF result, background;
1971     BOOL passed[2];
1972     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1973     background = 0x00FFFFFF;
1974     /* Set color of the 2 pixels that will be checked afterwards */
1975     SetPixelV(hdc, 0, 0, background);
1976     SetPixelV(hdc, 2, 2, background);
1977
1978     /* Let DrawState calculate the size of the icon (it's 1x1) */
1979     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 0, 0, (DST_ICON | flags ));
1980
1981     result = GetPixel(hdc, 0, 0);
1982     passed[0] = color_match(result, background);
1983     result = GetPixel(hdc, 2, 2);
1984     passed[0] = passed[0] & color_match(result, background);
1985
1986     /* Check if manually specifying the icon size DOESN'T work */
1987
1988     /* IMPORTANT: For Icons, DrawState wants the size of the source image, not the
1989      *            size in which it should be ultimately drawn. Therefore giving
1990      *            width/height 2x2 if the icon is only 1x1 pixels in size should
1991      *            result in drawing it with size 1x1. The size parameters must be
1992      *            ignored if a Icon has to be drawn! */
1993     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 2, 2, (DST_ICON | flags ));
1994
1995     result = GetPixel(hdc, 0, 0);
1996     passed[1] = color_match(result, background);
1997     result = GetPixel(hdc, 2, 2);
1998     passed[1] = passed[0] & color_match(result, background);
1999
2000     if(!passed[0]&&!passed[1])
2001         ok (passed[1],
2002         "DrawState failed to draw a 1x1 Icon in the correct size, independent of the "
2003         "width and height settings passed to it, for Icon with: Overlaying Mask %d on "
2004         "Color %06X with flags %08X. Line %d\n",
2005         maskvalue, color, (DST_ICON | flags), line);
2006     else if(!passed[1])
2007         ok (passed[1],
2008         "DrawState failed to draw a 1x1 Icon in the correct size, if the width and height "
2009         "parameters passed to it are bigger than the real Icon size, for Icon with: Overlaying "
2010         "Mask %d on Color %06X with flags %08X. Line %d\n",
2011         maskvalue, color, (DST_ICON | flags), line);
2012     else
2013         ok (passed[0],
2014         "DrawState failed to draw a 1x1 Icon in the correct size, if the width and height "
2015         "parameters passed to it are 0, for Icon with: Overlaying Mask %d on "
2016         "Color %06X with flags %08X. Line %d\n",
2017         maskvalue, color, (DST_ICON | flags), line);
2018 }
2019
2020 static void check_DrawState_Color(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, HBRUSH hbr, UINT flags,
2021                              COLORREF background, COLORREF modern_expected, COLORREF legacy_expected, int line)
2022 {
2023     COLORREF result;
2024     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
2025     if (!hicon) return;
2026     /* Set color of the pixel that will be checked afterwards */
2027     SetPixelV(hdc, 1, 1, background);
2028
2029     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 0, 0, ( DST_ICON | flags ));
2030
2031     /* Check the color of the pixel is correct */
2032     result = GetPixel(hdc, 1, 1);
2033
2034     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
2035         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
2036         "DrawState drawing Icon with Overlaying Mask %d on Color %06X with flags %08X. "
2037         "Expected a close match to %06X (modern) or %06X (legacy). Got %06X from line %d\n",
2038         maskvalue, color, (DST_ICON | flags), modern_expected, legacy_expected, result, line);
2039 }
2040
2041 static void test_DrawState(void)
2042 {
2043     BITMAPINFO bitmapInfo;
2044     HDC hdcDst = NULL;
2045     HBITMAP bmpDst = NULL;
2046     HBITMAP bmpOld = NULL;
2047     void *bits = 0;
2048
2049     hdcDst = CreateCompatibleDC(0);
2050     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
2051     if (!hdcDst)
2052         return;
2053
2054     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
2055     {
2056         skip("Windows will distort DrawIconEx colors at 8-bpp and less due to palettizing.\n");
2057         goto cleanup;
2058     }
2059
2060     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
2061     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2062     bitmapInfo.bmiHeader.biWidth = 3;
2063     bitmapInfo.bmiHeader.biHeight = 3;
2064     bitmapInfo.bmiHeader.biBitCount = 32;
2065     bitmapInfo.bmiHeader.biPlanes = 1;
2066     bitmapInfo.bmiHeader.biCompression = BI_RGB;
2067     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
2068     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
2069     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
2070     if (!bmpDst || !bits)
2071         goto cleanup;
2072     bmpOld = SelectObject(hdcDst, bmpDst);
2073
2074     /* potential flags to test with DrawState are: */
2075     /* DSS_DISABLED embosses the icon */
2076     /* DSS_MONO draw Icon using a brush as parameter 5 */
2077     /* DSS_NORMAL draw Icon without any modifications */
2078     /* DSS_UNION draw the Icon dithered */
2079
2080     check_DrawState_Size(hdcDst, FALSE, 0x00A0B0C0, 32, 0, DSS_NORMAL, __LINE__);
2081     check_DrawState_Color(hdcDst, FALSE, 0x00A0B0C0, 32, 0, DSS_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
2082
2083 cleanup:
2084     if(bmpOld)
2085         SelectObject(hdcDst, bmpOld);
2086     if(bmpDst)
2087         DeleteObject(bmpDst);
2088     if(hdcDst)
2089         DeleteDC(hdcDst);
2090 }
2091
2092 static DWORD parent_id;
2093
2094 static DWORD CALLBACK set_cursor_thread( void *arg )
2095 {
2096     HCURSOR ret;
2097
2098     PeekMessage( 0, 0, 0, 0, PM_NOREMOVE );  /* create a msg queue */
2099     if (parent_id)
2100     {
2101         BOOL ret = AttachThreadInput( GetCurrentThreadId(), parent_id, TRUE );
2102         ok( ret, "AttachThreadInput failed\n" );
2103     }
2104     if (arg) ret = SetCursor( (HCURSOR)arg );
2105     else ret = GetCursor();
2106     return (DWORD_PTR)ret;
2107 }
2108
2109 static void test_SetCursor(void)
2110 {
2111     static const BYTE bmp_bits[4096];
2112     ICONINFO cursorInfo;
2113     HCURSOR cursor, old_cursor, global_cursor = 0;
2114     DWORD error, id, result;
2115     UINT display_bpp;
2116     HDC hdc;
2117     HANDLE thread;
2118     CURSORINFO info;
2119
2120     if (pGetCursorInfo)
2121     {
2122         memset( &info, 0, sizeof(info) );
2123         info.cbSize = sizeof(info);
2124         if (!pGetCursorInfo( &info ))
2125         {
2126             win_skip( "GetCursorInfo not working\n" );
2127             pGetCursorInfo = NULL;
2128         }
2129         else global_cursor = info.hCursor;
2130     }
2131     cursor = GetCursor();
2132     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
2133     WaitForSingleObject( thread, 1000 );
2134     GetExitCodeThread( thread, &result );
2135     ok( result == (DWORD_PTR)cursor, "wrong thread cursor %x/%p\n", result, cursor );
2136
2137     hdc = GetDC(0);
2138     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
2139     ReleaseDC(0, hdc);
2140
2141     cursorInfo.fIcon = FALSE;
2142     cursorInfo.xHotspot = 0;
2143     cursorInfo.yHotspot = 0;
2144     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
2145     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
2146
2147     cursor = CreateIconIndirect(&cursorInfo);
2148     ok(cursor != NULL, "CreateIconIndirect returned %p\n", cursor);
2149     old_cursor = SetCursor( cursor );
2150
2151     if (pGetCursorInfo)
2152     {
2153         info.cbSize = sizeof(info);
2154         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
2155         /* global cursor doesn't change since we don't have a window */
2156         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
2157             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
2158     }
2159     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
2160     WaitForSingleObject( thread, 1000 );
2161     GetExitCodeThread( thread, &result );
2162     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
2163
2164     SetCursor( 0 );
2165     ok( GetCursor() == 0, "wrong cursor %p\n", GetCursor() );
2166     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
2167     WaitForSingleObject( thread, 1000 );
2168     GetExitCodeThread( thread, &result );
2169     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
2170
2171     thread = CreateThread( NULL, 0, set_cursor_thread, cursor, 0, &id );
2172     WaitForSingleObject( thread, 1000 );
2173     GetExitCodeThread( thread, &result );
2174     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
2175     ok( GetCursor() == 0, "wrong cursor %p/0\n", GetCursor() );
2176
2177     parent_id = GetCurrentThreadId();
2178     thread = CreateThread( NULL, 0, set_cursor_thread, cursor, 0, &id );
2179     WaitForSingleObject( thread, 1000 );
2180     GetExitCodeThread( thread, &result );
2181     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
2182     ok( GetCursor() == cursor, "wrong cursor %p/0\n", cursor );
2183
2184     if (pGetCursorInfo)
2185     {
2186         info.cbSize = sizeof(info);
2187         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
2188         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
2189             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
2190     }
2191     SetCursor( old_cursor );
2192     DestroyCursor( cursor );
2193
2194     SetLastError( 0xdeadbeef );
2195     cursor = SetCursor( (HCURSOR)0xbadbad );
2196     error = GetLastError();
2197     ok( cursor == 0, "wrong cursor %p/0\n", cursor );
2198     ok( error == ERROR_INVALID_CURSOR_HANDLE || broken( error == 0xdeadbeef ),  /* win9x */
2199         "wrong error %u\n", error );
2200
2201     if (pGetCursorInfo)
2202     {
2203         info.cbSize = sizeof(info);
2204         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
2205         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
2206             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
2207     }
2208 }
2209
2210 static HANDLE event_start, event_next;
2211
2212 static DWORD CALLBACK show_cursor_thread( void *arg )
2213 {
2214     DWORD count = (DWORD_PTR)arg;
2215     int ret;
2216
2217     PeekMessage( 0, 0, 0, 0, PM_NOREMOVE );  /* create a msg queue */
2218     if (parent_id)
2219     {
2220         BOOL ret = AttachThreadInput( GetCurrentThreadId(), parent_id, TRUE );
2221         ok( ret, "AttachThreadInput failed\n" );
2222     }
2223     if (!count) ret = ShowCursor( FALSE );
2224     else while (count--) ret = ShowCursor( TRUE );
2225     SetEvent( event_start );
2226     WaitForSingleObject( event_next, 2000 );
2227     return ret;
2228 }
2229
2230 static void test_ShowCursor(void)
2231 {
2232     int count;
2233     DWORD id, result;
2234     HANDLE thread;
2235     CURSORINFO info;
2236
2237     if (pGetCursorInfo)
2238     {
2239         memset( &info, 0, sizeof(info) );
2240         info.cbSize = sizeof(info);
2241         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
2242         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
2243     }
2244
2245     event_start = CreateEvent( NULL, FALSE, FALSE, NULL );
2246     event_next = CreateEvent( NULL, FALSE, FALSE, NULL );
2247
2248     count = ShowCursor( TRUE );
2249     ok( count == 1, "wrong count %d\n", count );
2250     count = ShowCursor( TRUE );
2251     ok( count == 2, "wrong count %d\n", count );
2252     count = ShowCursor( FALSE );
2253     ok( count == 1, "wrong count %d\n", count );
2254     count = ShowCursor( FALSE );
2255     ok( count == 0, "wrong count %d\n", count );
2256     count = ShowCursor( FALSE );
2257     ok( count == -1, "wrong count %d\n", count );
2258     count = ShowCursor( FALSE );
2259     ok( count == -2, "wrong count %d\n", count );
2260
2261     if (pGetCursorInfo)
2262     {
2263         info.cbSize = sizeof(info);
2264         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
2265         /* global show count is not affected since we don't have a window */
2266         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
2267     }
2268
2269     parent_id = 0;
2270     thread = CreateThread( NULL, 0, show_cursor_thread, NULL, 0, &id );
2271     WaitForSingleObject( event_start, 1000 );
2272     count = ShowCursor( FALSE );
2273     ok( count == -3, "wrong count %d\n", count );
2274     SetEvent( event_next );
2275     WaitForSingleObject( thread, 1000 );
2276     GetExitCodeThread( thread, &result );
2277     ok( result == -1, "wrong thread count %d\n", result );
2278     count = ShowCursor( FALSE );
2279     ok( count == -4, "wrong count %d\n", count );
2280
2281     thread = CreateThread( NULL, 0, show_cursor_thread, (void *)1, 0, &id );
2282     WaitForSingleObject( event_start, 1000 );
2283     count = ShowCursor( TRUE );
2284     ok( count == -3, "wrong count %d\n", count );
2285     SetEvent( event_next );
2286     WaitForSingleObject( thread, 1000 );
2287     GetExitCodeThread( thread, &result );
2288     ok( result == 1, "wrong thread count %d\n", result );
2289     count = ShowCursor( TRUE );
2290     ok( count == -2, "wrong count %d\n", count );
2291
2292     parent_id = GetCurrentThreadId();
2293     thread = CreateThread( NULL, 0, show_cursor_thread, NULL, 0, &id );
2294     WaitForSingleObject( event_start, 1000 );
2295     count = ShowCursor( TRUE );
2296     ok( count == -2, "wrong count %d\n", count );
2297     SetEvent( event_next );
2298     WaitForSingleObject( thread, 1000 );
2299     GetExitCodeThread( thread, &result );
2300     ok( result == -3, "wrong thread count %d\n", result );
2301     count = ShowCursor( FALSE );
2302     ok( count == -2, "wrong count %d\n", count );
2303
2304     thread = CreateThread( NULL, 0, show_cursor_thread, (void *)3, 0, &id );
2305     WaitForSingleObject( event_start, 1000 );
2306     count = ShowCursor( TRUE );
2307     ok( count == 2, "wrong count %d\n", count );
2308     SetEvent( event_next );
2309     WaitForSingleObject( thread, 1000 );
2310     GetExitCodeThread( thread, &result );
2311     ok( result == 1, "wrong thread count %d\n", result );
2312     count = ShowCursor( FALSE );
2313     ok( count == -2, "wrong count %d\n", count );
2314
2315     if (pGetCursorInfo)
2316     {
2317         info.cbSize = sizeof(info);
2318         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
2319         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
2320     }
2321
2322     count = ShowCursor( TRUE );
2323     ok( count == -1, "wrong count %d\n", count );
2324     count = ShowCursor( TRUE );
2325     ok( count == 0, "wrong count %d\n", count );
2326
2327     if (pGetCursorInfo)
2328     {
2329         info.cbSize = sizeof(info);
2330         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
2331         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
2332     }
2333 }
2334
2335
2336 static void test_DestroyCursor(void)
2337 {
2338     static const BYTE bmp_bits[4096];
2339     ICONINFO cursorInfo, new_info;
2340     HCURSOR cursor, cursor2, new_cursor;
2341     BOOL ret;
2342     DWORD error;
2343     UINT display_bpp;
2344     HDC hdc;
2345
2346     hdc = GetDC(0);
2347     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
2348     ReleaseDC(0, hdc);
2349
2350     cursorInfo.fIcon = FALSE;
2351     cursorInfo.xHotspot = 0;
2352     cursorInfo.yHotspot = 0;
2353     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
2354     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
2355
2356     cursor = CreateIconIndirect(&cursorInfo);
2357     ok(cursor != NULL, "CreateIconIndirect returned %p\n", cursor);
2358     if(!cursor) {
2359         return;
2360     }
2361     SetCursor(cursor);
2362
2363     SetLastError(0xdeadbeef);
2364     ret = DestroyCursor(cursor);
2365     ok(!ret || broken(ret)  /* succeeds on win9x */, "DestroyCursor on the active cursor succeeded\n");
2366     error = GetLastError();
2367     ok(error == 0xdeadbeef, "Last error: %u\n", error);
2368
2369     new_cursor = GetCursor();
2370     if (ret)  /* win9x replaces cursor by another one on destroy */
2371         ok(new_cursor != cursor, "GetCursor returned %p/%p\n", new_cursor, cursor);
2372     else
2373         ok(new_cursor == cursor, "GetCursor returned %p/%p\n", new_cursor, cursor);
2374
2375     SetLastError(0xdeadbeef);
2376     ret = GetIconInfo( cursor, &new_info );
2377     ok( !ret || broken(ret), /* nt4 */ "GetIconInfo succeeded\n" );
2378     ok( GetLastError() == ERROR_INVALID_CURSOR_HANDLE ||
2379         broken(GetLastError() == 0xdeadbeef), /* win9x */
2380         "wrong error %u\n", GetLastError() );
2381
2382     if (ret)  /* nt4 delays destruction until cursor changes */
2383     {
2384         DeleteObject( new_info.hbmColor );
2385         DeleteObject( new_info.hbmMask );
2386
2387         SetLastError(0xdeadbeef);
2388         ret = DestroyCursor( cursor );
2389         ok( !ret, "DestroyCursor succeeded\n" );
2390         ok( GetLastError() == ERROR_INVALID_CURSOR_HANDLE || GetLastError() == 0xdeadbeef,
2391             "wrong error %u\n", GetLastError() );
2392
2393         SetLastError(0xdeadbeef);
2394         cursor2 = SetCursor( cursor );
2395         ok( cursor2 == cursor, "SetCursor returned %p/%p\n", cursor2, cursor);
2396         ok( GetLastError() == ERROR_INVALID_CURSOR_HANDLE || GetLastError() == 0xdeadbeef,
2397             "wrong error %u\n", GetLastError() );
2398     }
2399     else
2400     {
2401         SetLastError(0xdeadbeef);
2402         cursor2 = CopyCursor( cursor );
2403         ok(!cursor2, "CopyCursor succeeded\n" );
2404         ok( GetLastError() == ERROR_INVALID_CURSOR_HANDLE ||
2405             broken(GetLastError() == 0xdeadbeef), /* win9x */
2406             "wrong error %u\n", GetLastError() );
2407
2408         SetLastError(0xdeadbeef);
2409         ret = DestroyCursor( cursor );
2410         if (new_cursor != cursor)  /* win9x */
2411             ok( ret, "DestroyCursor succeeded\n" );
2412         else
2413             ok( !ret, "DestroyCursor succeeded\n" );
2414         ok( GetLastError() == ERROR_INVALID_CURSOR_HANDLE || GetLastError() == 0xdeadbeef,
2415             "wrong error %u\n", GetLastError() );
2416
2417         SetLastError(0xdeadbeef);
2418         cursor2 = SetCursor( cursor );
2419         ok(!cursor2, "SetCursor returned %p/%p\n", cursor2, cursor);
2420         ok( GetLastError() == ERROR_INVALID_CURSOR_HANDLE || GetLastError() == 0xdeadbeef,
2421             "wrong error %u\n", GetLastError() );
2422     }
2423
2424     cursor2 = GetCursor();
2425     ok(cursor2 == new_cursor, "GetCursor returned %p/%p\n", cursor2, new_cursor);
2426
2427     SetLastError(0xdeadbeef);
2428     cursor2 = SetCursor( 0 );
2429     if (new_cursor != cursor)  /* win9x */
2430         ok(cursor2 == new_cursor, "SetCursor returned %p/%p\n", cursor2, cursor);
2431     else
2432         ok(!cursor2, "SetCursor returned %p/%p\n", cursor2, cursor);
2433     ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
2434
2435     cursor2 = GetCursor();
2436     ok(!cursor2, "GetCursor returned %p/%p\n", cursor2, cursor);
2437
2438     SetLastError(0xdeadbeef);
2439     ret = DestroyCursor(cursor);
2440     if (new_cursor != cursor)  /* win9x */
2441         ok( ret, "DestroyCursor succeeded\n" );
2442     else
2443         ok( !ret, "DestroyCursor succeeded\n" );
2444     ok( GetLastError() == ERROR_INVALID_CURSOR_HANDLE || GetLastError() == 0xdeadbeef,
2445         "wrong error %u\n", GetLastError() );
2446
2447     DeleteObject(cursorInfo.hbmMask);
2448     DeleteObject(cursorInfo.hbmColor);
2449
2450     /* Try testing DestroyCursor() now using LoadCursor() cursors. */
2451     cursor = LoadCursor(NULL, IDC_ARROW);
2452
2453     SetLastError(0xdeadbeef);
2454     ret = DestroyCursor(cursor);
2455     ok(ret || broken(!ret) /* fails on win9x */, "DestroyCursor on the active cursor failed.\n");
2456     error = GetLastError();
2457     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
2458
2459     /* Try setting the cursor to a destroyed OEM cursor. */
2460     SetLastError(0xdeadbeef);
2461     SetCursor(cursor);
2462     error = GetLastError();
2463     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
2464
2465     /* Check if LoadCursor() returns the same handle with the same icon. */
2466     cursor2 = LoadCursor(NULL, IDC_ARROW);
2467     ok(cursor2 == cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
2468
2469     /* Check if LoadCursor() returns the same handle with a different icon. */
2470     cursor2 = LoadCursor(NULL, IDC_WAIT);
2471     ok(cursor2 != cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
2472 }
2473
2474 START_TEST(cursoricon)
2475 {
2476     pGetCursorInfo = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetCursorInfo" );
2477     pGetIconInfoExA = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetIconInfoExA" );
2478     pGetIconInfoExW = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetIconInfoExW" );
2479     pGetCursorFrameInfo = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetCursorFrameInfo" );
2480     test_argc = winetest_get_mainargs(&test_argv);
2481
2482     if (test_argc >= 3)
2483     {
2484         /* Child process. */
2485         sscanf (test_argv[2], "%x", (unsigned int *) &parent);
2486
2487         ok(parent != NULL, "Parent not found.\n");
2488         if (parent == NULL)
2489             ExitProcess(1);
2490
2491         do_child();
2492         return;
2493     }
2494
2495     test_CopyImage_Bitmap(1);
2496     test_CopyImage_Bitmap(4);
2497     test_CopyImage_Bitmap(8);
2498     test_CopyImage_Bitmap(16);
2499     test_CopyImage_Bitmap(24);
2500     test_CopyImage_Bitmap(32);
2501     test_initial_cursor();
2502     test_CreateIcon();
2503     test_LoadImage();
2504     test_CreateIconFromResource();
2505     test_GetCursorFrameInfo();
2506     test_DrawIcon();
2507     test_DrawIconEx();
2508     test_DrawState();
2509     test_SetCursor();
2510     test_ShowCursor();
2511     test_DestroyCursor();
2512     do_parent();
2513     test_child_process();
2514     finish_child_process();
2515 }