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