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