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