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