makefiles: Rename the SRCDIR, TOPSRCDIR and TOPOBJDIR variables to follow autoconf...
[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 #include "poppack.h"
58
59 static char **test_argv;
60 static int test_argc;
61 static HWND child = 0;
62 static HWND parent = 0;
63 static HANDLE child_process;
64
65 #define PROC_INIT (WM_USER+1)
66
67 static BOOL (WINAPI *pGetCursorInfo)(CURSORINFO *);
68
69 static LRESULT CALLBACK callback_child(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
70 {
71     BOOL ret;
72     DWORD error;
73
74     switch (msg)
75     {
76         /* Destroy the cursor. */
77         case WM_USER+1:
78             SetLastError(0xdeadbeef);
79             ret = DestroyCursor((HCURSOR) lParam);
80             error = GetLastError();
81             todo_wine ok(!ret || broken(ret) /* win9x */, "DestroyCursor on the active cursor succeeded.\n");
82             ok(error == ERROR_DESTROY_OBJECT_OF_OTHER_THREAD ||
83                error == 0xdeadbeef,  /* vista */
84                 "Last error: %u\n", error);
85             return TRUE;
86         case WM_DESTROY:
87             PostQuitMessage(0);
88             return 0;
89     }
90
91     return DefWindowProc(hwnd, msg, wParam, lParam);
92 }
93
94 static LRESULT CALLBACK callback_parent(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
95 {
96     if (msg == PROC_INIT)
97     {
98         child = (HWND) wParam;
99         return TRUE;
100     }
101
102     return DefWindowProc(hwnd, msg, wParam, lParam);
103 }
104
105 static void do_child(void)
106 {
107     WNDCLASS class;
108     MSG msg;
109     BOOL ret;
110
111     /* Register a new class. */
112     class.style = CS_GLOBALCLASS;
113     class.lpfnWndProc = callback_child;
114     class.cbClsExtra = 0;
115     class.cbWndExtra = 0;
116     class.hInstance = GetModuleHandle(NULL);
117     class.hIcon = NULL;
118     class.hCursor = NULL;
119     class.hbrBackground = NULL;
120     class.lpszMenuName = NULL;
121     class.lpszClassName = "cursor_child";
122
123     SetLastError(0xdeadbeef);
124     ret = RegisterClass(&class);
125     ok(ret, "Failed to register window class.  Error: %u\n", GetLastError());
126
127     /* Create a window. */
128     child = CreateWindowA("cursor_child", "cursor_child", WS_POPUP | WS_VISIBLE,
129         0, 0, 200, 200, 0, 0, 0, NULL);
130     ok(child != 0, "CreateWindowA failed.  Error: %u\n", GetLastError());
131
132     /* Let the parent know our HWND. */
133     PostMessage(parent, PROC_INIT, (WPARAM) child, 0);
134
135     /* Receive messages. */
136     while ((ret = GetMessage(&msg, 0, 0, 0)))
137     {
138         ok(ret != -1, "GetMessage failed.  Error: %u\n", GetLastError());
139         TranslateMessage(&msg);
140         DispatchMessage(&msg);
141     }
142 }
143
144 static void do_parent(void)
145 {
146     char path_name[MAX_PATH];
147     PROCESS_INFORMATION info;
148     STARTUPINFOA startup;
149     WNDCLASS class;
150     MSG msg;
151     BOOL ret;
152
153     /* Register a new class. */
154     class.style = CS_GLOBALCLASS;
155     class.lpfnWndProc = callback_parent;
156     class.cbClsExtra = 0;
157     class.cbWndExtra = 0;
158     class.hInstance = GetModuleHandle(NULL);
159     class.hIcon = NULL;
160     class.hCursor = NULL;
161     class.hbrBackground = NULL;
162     class.lpszMenuName = NULL;
163     class.lpszClassName = "cursor_parent";
164
165     SetLastError(0xdeadbeef);
166     ret = RegisterClass(&class);
167     ok(ret, "Failed to register window class.  Error: %u\n", GetLastError());
168
169     /* Create a window. */
170     parent = CreateWindowA("cursor_parent", "cursor_parent", WS_POPUP | WS_VISIBLE,
171         0, 0, 200, 200, 0, 0, 0, NULL);
172     ok(parent != 0, "CreateWindowA failed.  Error: %u\n", GetLastError());
173
174     /* Start child process. */
175     memset(&startup, 0, sizeof(startup));
176     startup.cb = sizeof(startup);
177     startup.dwFlags = STARTF_USESHOWWINDOW;
178     startup.wShowWindow = SW_SHOWNORMAL;
179
180     sprintf(path_name, "%s cursoricon %lx", test_argv[0], (INT_PTR)parent);
181     ok(CreateProcessA(NULL, path_name, NULL, NULL, FALSE, 0L, NULL, NULL, &startup, &info), "CreateProcess failed.\n");
182     child_process = info.hProcess;
183
184     /* Wait for child window handle. */
185     while ((child == 0) && (ret = GetMessage(&msg, parent, 0, 0)))
186     {
187         ok(ret != -1, "GetMessage failed.  Error: %u\n", GetLastError());
188         TranslateMessage(&msg);
189         DispatchMessage(&msg);
190     }
191 }
192
193 static void finish_child_process(void)
194 {
195     SendMessage(child, WM_CLOSE, 0, 0);
196     winetest_wait_child_process( child_process );
197     CloseHandle(child_process);
198 }
199
200 static void test_child_process(void)
201 {
202     static const BYTE bmp_bits[4096];
203     HCURSOR cursor;
204     ICONINFO cursorInfo;
205     UINT display_bpp;
206     HDC hdc;
207
208     /* Create and set a dummy cursor. */
209     hdc = GetDC(0);
210     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
211     ReleaseDC(0, hdc);
212
213     cursorInfo.fIcon = FALSE;
214     cursorInfo.xHotspot = 0;
215     cursorInfo.yHotspot = 0;
216     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
217     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
218
219     cursor = CreateIconIndirect(&cursorInfo);
220     ok(cursor != NULL, "CreateIconIndirect returned %p.\n", cursor);
221
222     SetCursor(cursor);
223
224     /* Destroy the cursor. */
225     SendMessage(child, WM_USER+1, 0, (LPARAM) cursor);
226 }
227
228 static BOOL color_match(COLORREF a, COLORREF b)
229 {
230     /* 5-bit accuracy is a sufficient test. This will match as long as
231      * colors are never truncated to less that 3x5-bit accuracy i.e.
232      * palettized. */
233     return (a & 0x00F8F8F8) == (b & 0x00F8F8F8);
234 }
235
236 static void test_CopyImage_Check(HBITMAP bitmap, UINT flags, INT copyWidth, INT copyHeight,
237                                   INT expectedWidth, INT expectedHeight, WORD expectedDepth, BOOL dibExpected)
238 {
239     HBITMAP copy;
240     BITMAP origBitmap;
241     BITMAP copyBitmap;
242     BOOL orig_is_dib;
243     BOOL copy_is_dib;
244
245     copy = CopyImage(bitmap, IMAGE_BITMAP, copyWidth, copyHeight, flags);
246     ok(copy != NULL, "CopyImage() failed\n");
247     if (copy != NULL)
248     {
249         GetObject(bitmap, sizeof(origBitmap), &origBitmap);
250         GetObject(copy, sizeof(copyBitmap), &copyBitmap);
251         orig_is_dib = (origBitmap.bmBits != NULL);
252         copy_is_dib = (copyBitmap.bmBits != NULL);
253
254         if (copy_is_dib && dibExpected
255             && copyBitmap.bmBitsPixel == 24
256             && (expectedDepth == 16 || expectedDepth == 32))
257         {
258             /* Windows 95 doesn't create DIBs with a depth of 16 or 32 bit */
259             if (GetVersion() & 0x80000000)
260             {
261                 expectedDepth = 24;
262             }
263         }
264
265         if (copy_is_dib && !dibExpected && !(flags & LR_CREATEDIBSECTION))
266         {
267             /* It's not forbidden to create a DIB section if the flag
268                LR_CREATEDIBSECTION is absent.
269                Windows 9x does this if the bitmap has a depth that doesn't
270                match the screen depth, Windows NT doesn't */
271             dibExpected = TRUE;
272             expectedDepth = origBitmap.bmBitsPixel;
273         }
274
275         ok((!(dibExpected ^ copy_is_dib)
276              && (copyBitmap.bmWidth == expectedWidth)
277              && (copyBitmap.bmHeight == expectedHeight)
278              && (copyBitmap.bmBitsPixel == expectedDepth)),
279              "CopyImage ((%s, %dx%d, %u bpp), %d, %d, %#x): Expected (%s, %dx%d, %u bpp), got (%s, %dx%d, %u bpp)\n",
280                   orig_is_dib ? "DIB" : "DDB", origBitmap.bmWidth, origBitmap.bmHeight, origBitmap.bmBitsPixel,
281                   copyWidth, copyHeight, flags,
282                   dibExpected ? "DIB" : "DDB", expectedWidth, expectedHeight, expectedDepth,
283                   copy_is_dib ? "DIB" : "DDB", copyBitmap.bmWidth, copyBitmap.bmHeight, copyBitmap.bmBitsPixel);
284
285         DeleteObject(copy);
286     }
287 }
288
289 static void test_CopyImage_Bitmap(int depth)
290 {
291     HBITMAP ddb, dib;
292     HDC screenDC;
293     BITMAPINFO * info;
294     VOID * bits;
295     int screen_depth;
296     unsigned int i;
297
298     /* Create a device-independent bitmap (DIB) */
299     info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
300     info->bmiHeader.biSize = sizeof(info->bmiHeader);
301     info->bmiHeader.biWidth = 2;
302     info->bmiHeader.biHeight = 2;
303     info->bmiHeader.biPlanes = 1;
304     info->bmiHeader.biBitCount = depth;
305     info->bmiHeader.biCompression = BI_RGB;
306
307     for (i=0; i < 256; i++)
308     {
309         info->bmiColors[i].rgbRed = i;
310         info->bmiColors[i].rgbGreen = i;
311         info->bmiColors[i].rgbBlue = 255 - i;
312         info->bmiColors[i].rgbReserved = 0;
313     }
314
315     dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
316
317     /* Create a device-dependent bitmap (DDB) */
318     screenDC = GetDC(NULL);
319     screen_depth = GetDeviceCaps(screenDC, BITSPIXEL);
320     if (depth == 1 || depth == screen_depth)
321     {
322         ddb = CreateBitmap(2, 2, 1, depth, NULL);
323     }
324     else
325     {
326         ddb = NULL;
327     }
328     ReleaseDC(NULL, screenDC);
329
330     if (ddb != NULL)
331     {
332         test_CopyImage_Check(ddb, 0, 0, 0, 2, 2, depth == 1 ? 1 : screen_depth, FALSE);
333         test_CopyImage_Check(ddb, 0, 0, 5, 2, 5, depth == 1 ? 1 : screen_depth, FALSE);
334         test_CopyImage_Check(ddb, 0, 5, 0, 5, 2, depth == 1 ? 1 : screen_depth, FALSE);
335         test_CopyImage_Check(ddb, 0, 5, 5, 5, 5, depth == 1 ? 1 : screen_depth, FALSE);
336
337         test_CopyImage_Check(ddb, LR_MONOCHROME, 0, 0, 2, 2, 1, FALSE);
338         test_CopyImage_Check(ddb, LR_MONOCHROME, 5, 0, 5, 2, 1, FALSE);
339         test_CopyImage_Check(ddb, LR_MONOCHROME, 0, 5, 2, 5, 1, FALSE);
340         test_CopyImage_Check(ddb, LR_MONOCHROME, 5, 5, 5, 5, 1, FALSE);
341
342         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
343         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
344         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
345         test_CopyImage_Check(ddb, LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
346
347         /* LR_MONOCHROME is ignored if LR_CREATEDIBSECTION is present */
348         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
349         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
350         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
351         test_CopyImage_Check(ddb, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
352
353         DeleteObject(ddb);
354     }
355
356     if (depth != 1)
357     {
358         test_CopyImage_Check(dib, 0, 0, 0, 2, 2, screen_depth, FALSE);
359         test_CopyImage_Check(dib, 0, 5, 0, 5, 2, screen_depth, FALSE);
360         test_CopyImage_Check(dib, 0, 0, 5, 2, 5, screen_depth, FALSE);
361         test_CopyImage_Check(dib, 0, 5, 5, 5, 5, screen_depth, FALSE);
362     }
363
364     test_CopyImage_Check(dib, LR_MONOCHROME, 0, 0, 2, 2, 1, FALSE);
365     test_CopyImage_Check(dib, LR_MONOCHROME, 5, 0, 5, 2, 1, FALSE);
366     test_CopyImage_Check(dib, LR_MONOCHROME, 0, 5, 2, 5, 1, FALSE);
367     test_CopyImage_Check(dib, LR_MONOCHROME, 5, 5, 5, 5, 1, FALSE);
368
369     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
370     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
371     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
372     test_CopyImage_Check(dib, LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
373
374     /* LR_MONOCHROME is ignored if LR_CREATEDIBSECTION is present */
375     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 0, 2, 2, depth, TRUE);
376     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 0, 5, 2, depth, TRUE);
377     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 0, 5, 2, 5, depth, TRUE);
378     test_CopyImage_Check(dib, LR_MONOCHROME | LR_CREATEDIBSECTION, 5, 5, 5, 5, depth, TRUE);
379
380     DeleteObject(dib);
381
382     if (depth == 1)
383     {
384         /* Special case: A monochrome DIB is converted to a monochrome DDB if
385            the colors in the color table are black and white.
386
387            Skip this test on Windows 95, it always creates a monochrome DDB
388            in this case */
389
390         if (!(GetVersion() & 0x80000000))
391         {
392             info->bmiHeader.biBitCount = 1;
393             info->bmiColors[0].rgbRed = 0xFF;
394             info->bmiColors[0].rgbGreen = 0;
395             info->bmiColors[0].rgbBlue = 0;
396             info->bmiColors[1].rgbRed = 0;
397             info->bmiColors[1].rgbGreen = 0xFF;
398             info->bmiColors[1].rgbBlue = 0;
399
400             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
401             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, screen_depth, FALSE);
402             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, screen_depth, FALSE);
403             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, screen_depth, FALSE);
404             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, screen_depth, FALSE);
405             DeleteObject(dib);
406
407             info->bmiHeader.biBitCount = 1;
408             info->bmiColors[0].rgbRed = 0;
409             info->bmiColors[0].rgbGreen = 0;
410             info->bmiColors[0].rgbBlue = 0;
411             info->bmiColors[1].rgbRed = 0xFF;
412             info->bmiColors[1].rgbGreen = 0xFF;
413             info->bmiColors[1].rgbBlue = 0xFF;
414
415             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
416             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, 1, FALSE);
417             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, 1, FALSE);
418             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, 1, FALSE);
419             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, 1, FALSE);
420             DeleteObject(dib);
421
422             info->bmiHeader.biBitCount = 1;
423             info->bmiColors[0].rgbRed = 0xFF;
424             info->bmiColors[0].rgbGreen = 0xFF;
425             info->bmiColors[0].rgbBlue = 0xFF;
426             info->bmiColors[1].rgbRed = 0;
427             info->bmiColors[1].rgbGreen = 0;
428             info->bmiColors[1].rgbBlue = 0;
429
430             dib = CreateDIBSection(NULL, info, DIB_RGB_COLORS, &bits, NULL, 0);
431             test_CopyImage_Check(dib, 0, 0, 0, 2, 2, 1, FALSE);
432             test_CopyImage_Check(dib, 0, 5, 0, 5, 2, 1, FALSE);
433             test_CopyImage_Check(dib, 0, 0, 5, 2, 5, 1, FALSE);
434             test_CopyImage_Check(dib, 0, 5, 5, 5, 5, 1, FALSE);
435             DeleteObject(dib);
436         }
437     }
438
439     HeapFree(GetProcessHeap(), 0, info);
440 }
441
442 static void test_initial_cursor(void)
443 {
444     HCURSOR cursor, cursor2;
445     DWORD error;
446
447     cursor = GetCursor();
448
449     /* Check what handle GetCursor() returns if a cursor is not set yet. */
450     SetLastError(0xdeadbeef);
451     cursor2 = LoadCursor(NULL, IDC_WAIT);
452     todo_wine {
453         ok(cursor == cursor2, "cursor (%p) is not IDC_WAIT (%p).\n", cursor, cursor2);
454     }
455     error = GetLastError();
456     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
457 }
458
459 static void test_icon_info_dbg(HICON hIcon, UINT exp_cx, UINT exp_cy, UINT exp_bpp, int line)
460 {
461     ICONINFO info;
462     DWORD ret;
463     BITMAP bmMask, bmColor;
464
465     ret = GetIconInfo(hIcon, &info);
466     ok_(__FILE__, line)(ret, "GetIconInfo failed\n");
467
468     /* CreateIcon under XP causes info.fIcon to be 0 */
469     ok_(__FILE__, line)(info.xHotspot == exp_cx/2, "info.xHotspot = %u\n", info.xHotspot);
470     ok_(__FILE__, line)(info.yHotspot == exp_cy/2, "info.yHotspot = %u\n", info.yHotspot);
471     ok_(__FILE__, line)(info.hbmMask != 0, "info.hbmMask is NULL\n");
472
473     ret = GetObject(info.hbmMask, sizeof(bmMask), &bmMask);
474     ok_(__FILE__, line)(ret == sizeof(bmMask), "GetObject(info.hbmMask) failed, ret %u\n", ret);
475
476     if (exp_bpp == 1)
477         ok_(__FILE__, line)(info.hbmColor == 0, "info.hbmColor should be NULL\n");
478
479     if (info.hbmColor)
480     {
481         HDC hdc;
482         UINT display_bpp;
483
484         hdc = GetDC(0);
485         display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
486         ReleaseDC(0, hdc);
487
488         ret = GetObject(info.hbmColor, sizeof(bmColor), &bmColor);
489         ok_(__FILE__, line)(ret == sizeof(bmColor), "GetObject(info.hbmColor) failed, ret %u\n", ret);
490
491         ok_(__FILE__, line)(bmColor.bmBitsPixel == display_bpp /* XP */ ||
492            bmColor.bmBitsPixel == exp_bpp /* Win98 */,
493            "bmColor.bmBitsPixel = %d\n", bmColor.bmBitsPixel);
494         ok_(__FILE__, line)(bmColor.bmWidth == exp_cx, "bmColor.bmWidth = %d\n", bmColor.bmWidth);
495         ok_(__FILE__, line)(bmColor.bmHeight == exp_cy, "bmColor.bmHeight = %d\n", bmColor.bmHeight);
496
497         ok_(__FILE__, line)(bmMask.bmBitsPixel == 1, "bmMask.bmBitsPixel = %d\n", bmMask.bmBitsPixel);
498         ok_(__FILE__, line)(bmMask.bmWidth == exp_cx, "bmMask.bmWidth = %d\n", bmMask.bmWidth);
499         ok_(__FILE__, line)(bmMask.bmHeight == exp_cy, "bmMask.bmHeight = %d\n", bmMask.bmHeight);
500     }
501     else
502     {
503         ok_(__FILE__, line)(bmMask.bmBitsPixel == 1, "bmMask.bmBitsPixel = %d\n", bmMask.bmBitsPixel);
504         ok_(__FILE__, line)(bmMask.bmWidth == exp_cx, "bmMask.bmWidth = %d\n", bmMask.bmWidth);
505         ok_(__FILE__, line)(bmMask.bmHeight == exp_cy * 2, "bmMask.bmHeight = %d\n", bmMask.bmHeight);
506     }
507 }
508
509 #define test_icon_info(a,b,c,d) test_icon_info_dbg((a),(b),(c),(d),__LINE__)
510
511 static void test_CreateIcon(void)
512 {
513     static const BYTE bmp_bits[1024];
514     HICON hIcon;
515     HBITMAP hbmMask, hbmColor;
516     BITMAPINFO *bmpinfo;
517     ICONINFO info;
518     HDC hdc;
519     void *bits;
520     UINT display_bpp;
521
522     hdc = GetDC(0);
523     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
524
525     /* these crash under XP
526     hIcon = CreateIcon(0, 16, 16, 1, 1, bmp_bits, NULL);
527     hIcon = CreateIcon(0, 16, 16, 1, 1, NULL, bmp_bits);
528     */
529
530     hIcon = CreateIcon(0, 16, 16, 1, 1, bmp_bits, bmp_bits);
531     ok(hIcon != 0, "CreateIcon failed\n");
532     test_icon_info(hIcon, 16, 16, 1);
533     DestroyIcon(hIcon);
534
535     hIcon = CreateIcon(0, 16, 16, 1, display_bpp, bmp_bits, bmp_bits);
536     ok(hIcon != 0, "CreateIcon failed\n");
537     test_icon_info(hIcon, 16, 16, display_bpp);
538     DestroyIcon(hIcon);
539
540     hbmMask = CreateBitmap(16, 16, 1, 1, bmp_bits);
541     ok(hbmMask != 0, "CreateBitmap failed\n");
542     hbmColor = CreateBitmap(16, 16, 1, display_bpp, bmp_bits);
543     ok(hbmColor != 0, "CreateBitmap failed\n");
544
545     info.fIcon = TRUE;
546     info.xHotspot = 8;
547     info.yHotspot = 8;
548     info.hbmMask = 0;
549     info.hbmColor = 0;
550     SetLastError(0xdeadbeaf);
551     hIcon = CreateIconIndirect(&info);
552     ok(!hIcon, "CreateIconIndirect should fail\n");
553     ok(GetLastError() == 0xdeadbeaf, "wrong error %u\n", GetLastError());
554
555     info.fIcon = TRUE;
556     info.xHotspot = 8;
557     info.yHotspot = 8;
558     info.hbmMask = 0;
559     info.hbmColor = hbmColor;
560     SetLastError(0xdeadbeaf);
561     hIcon = CreateIconIndirect(&info);
562     ok(!hIcon, "CreateIconIndirect should fail\n");
563     ok(GetLastError() == 0xdeadbeaf, "wrong error %u\n", GetLastError());
564
565     info.fIcon = TRUE;
566     info.xHotspot = 8;
567     info.yHotspot = 8;
568     info.hbmMask = hbmMask;
569     info.hbmColor = hbmColor;
570     hIcon = CreateIconIndirect(&info);
571     ok(hIcon != 0, "CreateIconIndirect failed\n");
572     test_icon_info(hIcon, 16, 16, display_bpp);
573     DestroyIcon(hIcon);
574
575     DeleteObject(hbmMask);
576     DeleteObject(hbmColor);
577
578     hbmMask = CreateBitmap(16, 32, 1, 1, bmp_bits);
579     ok(hbmMask != 0, "CreateBitmap failed\n");
580
581     info.fIcon = TRUE;
582     info.xHotspot = 8;
583     info.yHotspot = 8;
584     info.hbmMask = hbmMask;
585     info.hbmColor = 0;
586     SetLastError(0xdeadbeaf);
587     hIcon = CreateIconIndirect(&info);
588     ok(hIcon != 0, "CreateIconIndirect failed\n");
589     test_icon_info(hIcon, 16, 16, 1);
590     DestroyIcon(hIcon);
591
592     DeleteObject(hbmMask);
593     DeleteObject(hbmColor);
594
595     /* test creating an icon from a DIB section */
596
597     bmpinfo = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, FIELD_OFFSET(BITMAPINFO,bmiColors[256]));
598     bmpinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
599     bmpinfo->bmiHeader.biWidth = 32;
600     bmpinfo->bmiHeader.biHeight = 32;
601     bmpinfo->bmiHeader.biPlanes = 1;
602     bmpinfo->bmiHeader.biBitCount = 8;
603     bmpinfo->bmiHeader.biCompression = BI_RGB;
604     hbmColor = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
605     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
606     if (bits)
607         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
608     bmpinfo->bmiHeader.biBitCount = 1;
609     hbmMask = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
610     ok(hbmMask != NULL, "Expected a handle to the DIB\n");
611     if (bits)
612         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
613
614     info.fIcon = TRUE;
615     info.xHotspot = 8;
616     info.yHotspot = 8;
617     info.hbmMask = hbmColor;
618     info.hbmColor = hbmMask;
619     SetLastError(0xdeadbeaf);
620     hIcon = CreateIconIndirect(&info);
621     ok(hIcon != 0, "CreateIconIndirect failed\n");
622     test_icon_info(hIcon, 32, 32, 8);
623     DestroyIcon(hIcon);
624     DeleteObject(hbmColor);
625
626     bmpinfo->bmiHeader.biBitCount = 16;
627     hbmColor = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
628     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
629     if (bits)
630         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
631
632     info.fIcon = TRUE;
633     info.xHotspot = 8;
634     info.yHotspot = 8;
635     info.hbmMask = hbmColor;
636     info.hbmColor = hbmMask;
637     SetLastError(0xdeadbeaf);
638     hIcon = CreateIconIndirect(&info);
639     ok(hIcon != 0, "CreateIconIndirect failed\n");
640     test_icon_info(hIcon, 32, 32, 8);
641     DestroyIcon(hIcon);
642     DeleteObject(hbmColor);
643
644     bmpinfo->bmiHeader.biBitCount = 32;
645     hbmColor = CreateDIBSection( hdc, bmpinfo, DIB_RGB_COLORS, &bits, NULL, 0 );
646     ok(hbmColor != NULL, "Expected a handle to the DIB\n");
647     if (bits)
648         memset( bits, 0x55, 32 * 32 * bmpinfo->bmiHeader.biBitCount / 8 );
649
650     info.fIcon = TRUE;
651     info.xHotspot = 8;
652     info.yHotspot = 8;
653     info.hbmMask = hbmColor;
654     info.hbmColor = hbmMask;
655     SetLastError(0xdeadbeaf);
656     hIcon = CreateIconIndirect(&info);
657     ok(hIcon != 0, "CreateIconIndirect failed\n");
658     test_icon_info(hIcon, 32, 32, 8);
659     DestroyIcon(hIcon);
660
661     DeleteObject(hbmMask);
662     DeleteObject(hbmColor);
663     HeapFree( GetProcessHeap(), 0, bmpinfo );
664
665     ReleaseDC(0, hdc);
666 }
667
668 /* Shamelessly ripped from dlls/oleaut32/tests/olepicture.c */
669 /* 1x1 pixel gif */
670 static unsigned char gifimage[35] = {
671 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
672 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
673 0x01,0x00,0x3b
674 };
675
676 /* 1x1 pixel jpg */
677 static unsigned char jpgimage[285] = {
678 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
679 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
680 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
681 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
682 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
683 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
684 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
685 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
686 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
687 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0xff,0xc0,
688 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
689 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
690 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
691 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
692 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
693 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
694 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
695 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
696 };
697
698 /* 1x1 pixel png */
699 static unsigned char pngimage[285] = {
700 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
701 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
702 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
703 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
704 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
705 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
706 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
707 };
708
709 /* 1x1 pixel bmp with gap between palette and bitmap. Correct bitmap contains only
710    zeroes, gap is 0xFF. */
711 static unsigned char bmpimage[70] = {
712 0x42,0x4d,0x46,0x00,0x00,0x00,0xDE,0xAD,0xBE,0xEF,0x42,0x00,0x00,0x00,0x28,0x00,
713 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
714 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
715 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0x55,0x55,0x55,0x00,0xFF,0xFF,
716 0xFF,0xFF,0x00,0x00,0x00,0x00
717 };
718
719 /* 1x1 pixel bmp using BITMAPCOREHEADER */
720 static unsigned char bmpcoreimage[38] = {
721 0x42,0x4d,0x26,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x22,0x00,0x00,0x00,0x0c,0x00,
722 0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xff,0xff,0xff,0x00,0x55,0x55,
723 0x55,0x00,0x00,0x00,0x00,0x00
724 };
725
726 /* 2x2 pixel gif */
727 static unsigned char gif4pixel[42] = {
728 0x47,0x49,0x46,0x38,0x37,0x61,0x02,0x00,0x02,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,
729 0x39,0x62,0xfc,0xff,0x1a,0xe5,0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x02,0x00,
730 0x02,0x00,0x00,0x02,0x03,0x14,0x16,0x05,0x00,0x3b
731 };
732
733 static void test_LoadImageBitmap(const char * test_desc, HBITMAP hbm)
734 {
735     BITMAP bm;
736     BITMAPINFO bmi;
737     DWORD ret, pixel = 0;
738     HDC hdc = GetDC(NULL);
739
740     ret = GetObject(hbm, sizeof(bm), &bm);
741     ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
742
743     memset(&bmi, 0, sizeof(bmi));
744     bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
745     bmi.bmiHeader.biWidth = bm.bmWidth;
746     bmi.bmiHeader.biHeight = bm.bmHeight;
747     bmi.bmiHeader.biPlanes = 1;
748     bmi.bmiHeader.biBitCount= 24;
749     bmi.bmiHeader.biCompression= BI_RGB;
750     ret = GetDIBits(hdc, hbm, 0, bm.bmHeight, &pixel, &bmi, DIB_RGB_COLORS);
751     ok(ret == bm.bmHeight, "%s: %d lines were converted, not %d\n", test_desc, ret, bm.bmHeight);
752
753     ok(color_match(pixel, 0x00ffffff), "%s: Pixel is 0x%08x\n", test_desc, pixel);
754 }
755
756 static void test_LoadImageFile(const char * test_desc, unsigned char * image_data,
757     unsigned int image_size, const char * ext, BOOL expect_success)
758 {
759     HANDLE handle;
760     BOOL ret;
761     DWORD error, bytes_written;
762     char filename[64];
763
764     strcpy(filename, "test.");
765     strcat(filename, ext);
766
767     /* Create the test image. */
768     handle = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
769         FILE_ATTRIBUTE_NORMAL, NULL);
770     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
771     ret = WriteFile(handle, image_data, image_size, &bytes_written, NULL);
772     ok(ret && bytes_written == image_size, "test file created improperly.\n");
773     CloseHandle(handle);
774
775     /* Load as cursor. For all tested formats, this should fail */
776     SetLastError(0xdeadbeef);
777     handle = LoadImageA(NULL, filename, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
778     ok(handle == NULL, "%s: IMAGE_CURSOR succeeded incorrectly.\n", test_desc);
779     error = GetLastError();
780     ok(error == 0 ||
781         broken(error == 0xdeadbeef) || /* Win9x */
782         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
783         "Last error: %u\n", error);
784     if (handle != NULL) DestroyCursor(handle);
785
786     /* Load as icon. For all tested formats, this should fail */
787     SetLastError(0xdeadbeef);
788     handle = LoadImageA(NULL, filename, IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
789     ok(handle == NULL, "%s: IMAGE_ICON succeeded incorrectly.\n", test_desc);
790     error = GetLastError();
791     ok(error == 0 ||
792         broken(error == 0xdeadbeef) || /* Win9x */
793         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
794         "Last error: %u\n", error);
795     if (handle != NULL) DestroyIcon(handle);
796
797     /* Load as bitmap. Should succeed for correct bmp, fail for everything else */
798     SetLastError(0xdeadbeef);
799     handle = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
800     error = GetLastError();
801     ok(error == 0 ||
802         error == 0xdeadbeef, /* Win9x, WinMe */
803         "Last error: %u\n", error);
804
805     if (expect_success) {
806         ok(handle != NULL, "%s: IMAGE_BITMAP failed.\n", test_desc);
807         if (handle != NULL) test_LoadImageBitmap(test_desc, handle);
808     }
809     else ok(handle == NULL, "%s: IMAGE_BITMAP succeeded incorrectly.\n", test_desc);
810
811     if (handle != NULL) DeleteObject(handle);
812     DeleteFileA(filename);
813 }
814
815 static void test_LoadImage(void)
816 {
817     HANDLE handle;
818     BOOL ret;
819     DWORD error, bytes_written;
820     CURSORICONFILEDIR *icon_data;
821     CURSORICONFILEDIRENTRY *icon_entry;
822     BITMAPINFOHEADER *icon_header;
823     ICONINFO icon_info;
824
825 #define ICON_WIDTH 32
826 #define ICON_HEIGHT 32
827 #define ICON_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
828 #define ICON_BPP 32
829 #define ICON_SIZE \
830     (sizeof(CURSORICONFILEDIR) + sizeof(BITMAPINFOHEADER) \
831     + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
832
833     /* Set icon data. */
834     icon_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ICON_SIZE);
835     icon_data->idReserved = 0;
836     icon_data->idType = 1;
837     icon_data->idCount = 1;
838
839     icon_entry = icon_data->idEntries;
840     icon_entry->bWidth = ICON_WIDTH;
841     icon_entry->bHeight = ICON_HEIGHT;
842     icon_entry->bColorCount = 0;
843     icon_entry->bReserved = 0;
844     icon_entry->xHotspot = 1;
845     icon_entry->yHotspot = 1;
846     icon_entry->dwDIBSize = ICON_SIZE - sizeof(CURSORICONFILEDIR);
847     icon_entry->dwDIBOffset = sizeof(CURSORICONFILEDIR);
848
849     icon_header = (BITMAPINFOHEADER *) ((BYTE *) icon_data + icon_entry->dwDIBOffset);
850     icon_header->biSize = sizeof(BITMAPINFOHEADER);
851     icon_header->biWidth = ICON_WIDTH;
852     icon_header->biHeight = ICON_HEIGHT*2;
853     icon_header->biPlanes = 1;
854     icon_header->biBitCount = ICON_BPP;
855     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
856
857     /* Create the icon. */
858     handle = CreateFileA("icon.ico", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
859         FILE_ATTRIBUTE_NORMAL, NULL);
860     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
861     ret = WriteFile(handle, icon_data, ICON_SIZE, &bytes_written, NULL);
862     ok(ret && bytes_written == ICON_SIZE, "icon.ico created improperly.\n");
863     CloseHandle(handle);
864
865     /* Test loading an icon as a cursor. */
866     SetLastError(0xdeadbeef);
867     handle = LoadImageA(NULL, "icon.ico", IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
868     ok(handle != NULL, "LoadImage() failed.\n");
869     error = GetLastError();
870     ok(error == 0 ||
871         broken(error == 0xdeadbeef) || /* Win9x */
872         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
873         "Last error: %u\n", error);
874
875     /* Test the icon information. */
876     SetLastError(0xdeadbeef);
877     ret = GetIconInfo(handle, &icon_info);
878     ok(ret, "GetIconInfo() failed.\n");
879     error = GetLastError();
880     ok(error == 0xdeadbeef, "Last error: %u\n", error);
881
882     if (ret)
883     {
884         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
885         ok(icon_info.xHotspot == 1, "xHotspot is %u.\n", icon_info.xHotspot);
886         ok(icon_info.yHotspot == 1, "yHotspot is %u.\n", icon_info.yHotspot);
887         ok(icon_info.hbmColor != NULL || broken(!icon_info.hbmColor) /* no color cursor support */,
888            "No hbmColor!\n");
889         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
890     }
891
892     /* Clean up. */
893     SetLastError(0xdeadbeef);
894     ret = DestroyCursor(handle);
895     ok(ret, "DestroyCursor() failed.\n");
896     error = GetLastError();
897     ok(error == 0xdeadbeef, "Last error: %u\n", error);
898
899     HeapFree(GetProcessHeap(), 0, icon_data);
900     DeleteFileA("icon.ico");
901
902     test_LoadImageFile("BMP", bmpimage, sizeof(bmpimage), "bmp", 1);
903     test_LoadImageFile("BMP (coreinfo)", bmpcoreimage, sizeof(bmpcoreimage), "bmp", 1);
904     test_LoadImageFile("GIF", gifimage, sizeof(gifimage), "gif", 0);
905     test_LoadImageFile("GIF (2x2 pixel)", gif4pixel, sizeof(gif4pixel), "gif", 0);
906     test_LoadImageFile("JPG", jpgimage, sizeof(jpgimage), "jpg", 0);
907     test_LoadImageFile("PNG", pngimage, sizeof(pngimage), "png", 0);
908     /* Check failure for broken BMP images */
909     bmpimage[0x14]++; /* biHeight > 65535 */
910     test_LoadImageFile("BMP (too high)", bmpimage, sizeof(bmpimage), "bmp", 0);
911     bmpimage[0x14]--;
912     bmpimage[0x18]++; /* biWidth > 65535 */
913     test_LoadImageFile("BMP (too wide)", bmpimage, sizeof(bmpimage), "bmp", 0);
914     bmpimage[0x18]--;
915 }
916
917 static void test_CreateIconFromResource(void)
918 {
919     HANDLE handle;
920     BOOL ret;
921     DWORD error;
922     BITMAPINFOHEADER *icon_header;
923     INT16 *hotspot;
924     ICONINFO icon_info;
925
926 #define ICON_RES_WIDTH 32
927 #define ICON_RES_HEIGHT 32
928 #define ICON_RES_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
929 #define ICON_RES_BPP 32
930 #define ICON_RES_SIZE \
931     (sizeof(BITMAPINFOHEADER) + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
932 #define CRSR_RES_SIZE (2*sizeof(INT16) + ICON_RES_SIZE)
933
934     /* Set icon data. */
935     hotspot = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, CRSR_RES_SIZE);
936
937     /* Cursor resources have an extra hotspot, icon resources not. */
938     hotspot[0] = 3;
939     hotspot[1] = 3;
940
941     icon_header = (BITMAPINFOHEADER *) (hotspot + 2);
942     icon_header->biSize = sizeof(BITMAPINFOHEADER);
943     icon_header->biWidth = ICON_WIDTH;
944     icon_header->biHeight = ICON_HEIGHT*2;
945     icon_header->biPlanes = 1;
946     icon_header->biBitCount = ICON_BPP;
947     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
948
949     /* Test creating a cursor. */
950     SetLastError(0xdeadbeef);
951     handle = CreateIconFromResource((PBYTE) hotspot, CRSR_RES_SIZE, FALSE, 0x00030000);
952     ok(handle != NULL, "Create cursor failed.\n");
953
954     /* Test the icon information. */
955     SetLastError(0xdeadbeef);
956     ret = GetIconInfo(handle, &icon_info);
957     ok(ret, "GetIconInfo() failed.\n");
958     error = GetLastError();
959     ok(error == 0xdeadbeef, "Last error: %u\n", error);
960
961     if (ret)
962     {
963         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
964         ok(icon_info.xHotspot == 3, "xHotspot is %u.\n", icon_info.xHotspot);
965         ok(icon_info.yHotspot == 3, "yHotspot is %u.\n", icon_info.yHotspot);
966         ok(icon_info.hbmColor != NULL || broken(!icon_info.hbmColor) /* no color cursor support */,
967            "No hbmColor!\n");
968         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
969     }
970
971     /* Clean up. */
972     SetLastError(0xdeadbeef);
973     ret = DestroyCursor(handle);
974     ok(ret, "DestroyCursor() failed.\n");
975     error = GetLastError();
976     ok(error == 0xdeadbeef, "Last error: %u\n", error);
977
978     /* Test creating an icon. */
979     SetLastError(0xdeadbeef);
980     handle = CreateIconFromResource((PBYTE) icon_header, ICON_RES_SIZE, TRUE,
981                                     0x00030000);
982     ok(handle != NULL, "Create icon failed.\n");
983
984     /* Test the icon information. */
985     SetLastError(0xdeadbeef);
986     ret = GetIconInfo(handle, &icon_info);
987     ok(ret, "GetIconInfo() failed.\n");
988     error = GetLastError();
989     ok(error == 0xdeadbeef, "Last error: %u\n", error);
990
991     if (ret)
992     {
993         ok(icon_info.fIcon == TRUE, "fIcon != TRUE.\n");
994         /* Icons always have hotspot in the middle */
995         ok(icon_info.xHotspot == ICON_WIDTH/2, "xHotspot is %u.\n", icon_info.xHotspot);
996         ok(icon_info.yHotspot == ICON_HEIGHT/2, "yHotspot is %u.\n", icon_info.yHotspot);
997         ok(icon_info.hbmColor != NULL, "No hbmColor!\n");
998         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
999     }
1000
1001     /* Clean up. */
1002     SetLastError(0xdeadbeef);
1003     ret = DestroyCursor(handle);
1004     ok(ret, "DestroyCursor() failed.\n");
1005     error = GetLastError();
1006     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1007
1008     /* Rejection of NULL pointer crashes at least on WNT4WSSP6, W2KPROSP4, WXPPROSP3
1009      *
1010      * handle = CreateIconFromResource(NULL, ICON_RES_SIZE, TRUE, 0x00030000);
1011      * ok(handle == NULL, "Invalid pointer accepted (%p)\n", handle);
1012      */
1013     HeapFree(GetProcessHeap(), 0, hotspot);
1014 }
1015
1016 static HICON create_test_icon(HDC hdc, int width, int height, int bpp,
1017                               BOOL maskvalue, UINT32 *color, int colorSize)
1018 {
1019     ICONINFO iconInfo;
1020     BITMAPINFO bitmapInfo;
1021     void *buffer = NULL;
1022     UINT32 mask = maskvalue ? 0xFFFFFFFF : 0x00000000;
1023
1024     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1025     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1026     bitmapInfo.bmiHeader.biWidth = width;
1027     bitmapInfo.bmiHeader.biHeight = height;
1028     bitmapInfo.bmiHeader.biPlanes = 1;
1029     bitmapInfo.bmiHeader.biBitCount = bpp;
1030     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1031     bitmapInfo.bmiHeader.biSizeImage = colorSize;
1032
1033     iconInfo.fIcon = TRUE;
1034     iconInfo.xHotspot = 0;
1035     iconInfo.yHotspot = 0;
1036
1037     iconInfo.hbmMask = CreateBitmap( width, height, 1, 1, &mask );
1038     if(!iconInfo.hbmMask) return NULL;
1039
1040     iconInfo.hbmColor = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, &buffer, NULL, 0);
1041     if(!iconInfo.hbmColor || !buffer)
1042     {
1043         DeleteObject(iconInfo.hbmMask);
1044         return NULL;
1045     }
1046
1047     memcpy(buffer, color, colorSize);
1048
1049     return CreateIconIndirect(&iconInfo);
1050 }
1051
1052 static void check_alpha_draw(HDC hdc, BOOL drawiconex, BOOL alpha, int bpp, int line)
1053 {
1054     HICON hicon;
1055     UINT32 color[2];
1056     COLORREF modern_expected, legacy_expected, result;
1057
1058     color[0] = 0x00A0B0C0;
1059     color[1] = alpha ? 0xFF000000 : 0x00000000;
1060     modern_expected = alpha ? 0x00FFFFFF : 0x00C0B0A0;
1061     legacy_expected = 0x00C0B0A0;
1062
1063     hicon = create_test_icon(hdc, 2, 1, bpp, 0, color, sizeof(color));
1064     if (!hicon) return;
1065
1066     SetPixelV(hdc, 0, 0, 0x00FFFFFF);
1067
1068     if(drawiconex)
1069         DrawIconEx(hdc, 0, 0, hicon, 2, 1, 0, NULL, DI_NORMAL);
1070     else
1071         DrawIcon(hdc, 0, 0, hicon);
1072
1073     result = GetPixel(hdc, 0, 0);
1074     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1075         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1076         "%s. Expected a close match to %06X (modern) or %06X (legacy) with %s. "
1077         "Got %06X from line %d\n",
1078         alpha ? "Alpha blending" : "Not alpha blending", modern_expected, legacy_expected,
1079         drawiconex ? "DrawIconEx" : "DrawIcon", result, line);
1080 }
1081
1082 static void check_DrawIcon(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, COLORREF background,
1083                            COLORREF modern_expected, COLORREF legacy_expected, int line)
1084 {
1085     COLORREF result;
1086     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1087     if (!hicon) return;
1088     SetPixelV(hdc, 0, 0, background);
1089     SetPixelV(hdc, GetSystemMetrics(SM_CXICON)-1, GetSystemMetrics(SM_CYICON)-1, background);
1090     SetPixelV(hdc, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), background);
1091     DrawIcon(hdc, 0, 0, hicon);
1092     result = GetPixel(hdc, 0, 0);
1093
1094     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1095         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1096         "Overlaying Mask %d on Color %06X with DrawIcon. "
1097         "Expected a close match to %06X (modern), or %06X (legacy). Got %06X from line %d\n",
1098         maskvalue, color, modern_expected, legacy_expected, result, line);
1099
1100     result = GetPixel(hdc, GetSystemMetrics(SM_CXICON)-1, GetSystemMetrics(SM_CYICON)-1);
1101
1102     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1103         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1104         "Overlaying Mask %d on Color %06X with DrawIcon. "
1105         "Expected a close match to %06X (modern), or %06X (legacy). Got %06X from line %d\n",
1106         maskvalue, color, modern_expected, legacy_expected, result, line);
1107
1108     result = GetPixel(hdc, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
1109
1110     ok (color_match(result, background),
1111         "Overlaying Mask %d on Color %06X with DrawIcon. "
1112         "Expected unchanged background color %06X. Got %06X from line %d\n",
1113         maskvalue, color, background, result, line);
1114 }
1115
1116 static void test_DrawIcon(void)
1117 {
1118     BITMAPINFO bitmapInfo;
1119     HDC hdcDst = NULL;
1120     HBITMAP bmpDst = NULL;
1121     HBITMAP bmpOld = NULL;
1122     void *bits = 0;
1123
1124     hdcDst = CreateCompatibleDC(0);
1125     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1126     if (!hdcDst)
1127         return;
1128
1129     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1130     {
1131         skip("Windows will distort DrawIcon colors at 8-bpp and less due to palletizing.\n");
1132         goto cleanup;
1133     }
1134
1135     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1136     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1137     bitmapInfo.bmiHeader.biWidth = GetSystemMetrics(SM_CXICON)+1;
1138     bitmapInfo.bmiHeader.biHeight = GetSystemMetrics(SM_CYICON)+1;
1139     bitmapInfo.bmiHeader.biBitCount = 32;
1140     bitmapInfo.bmiHeader.biPlanes = 1;
1141     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1142     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1143
1144     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1145     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1146     if (!bmpDst || !bits)
1147         goto cleanup;
1148     bmpOld = SelectObject(hdcDst, bmpDst);
1149
1150     /* Mask is only heeded if alpha channel is always zero */
1151     check_DrawIcon(hdcDst, FALSE, 0x00A0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1152     check_DrawIcon(hdcDst, TRUE, 0x00A0B0C0, 32, 0x00FFFFFF, 0x003F4F5F, 0x003F4F5F, __LINE__);
1153
1154     /* Test alpha blending */
1155     /* Windows 2000 and up will alpha blend, earlier Windows versions will not */
1156     check_DrawIcon(hdcDst, FALSE, 0xFFA0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1157     check_DrawIcon(hdcDst, TRUE, 0xFFA0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x003F4F5F, __LINE__);
1158
1159     check_DrawIcon(hdcDst, FALSE, 0x80A0B0C0, 32, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1160     check_DrawIcon(hdcDst, TRUE, 0x80A0B0C0, 32, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1161     check_DrawIcon(hdcDst, FALSE, 0x80A0B0C0, 32, 0x00FFFFFF, 0x00DFD7CF, 0x00C0B0A0, __LINE__);
1162     check_DrawIcon(hdcDst, TRUE, 0x80A0B0C0, 32, 0x00FFFFFF, 0x00DFD7CF, 0x003F4F5F, __LINE__);
1163
1164     check_DrawIcon(hdcDst, FALSE, 0x01FFFFFF, 32, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1165     check_DrawIcon(hdcDst, TRUE, 0x01FFFFFF, 32, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1166
1167     /* Test detecting of alpha channel */
1168     /* If a single pixel's alpha channel is non-zero, the icon
1169        will be alpha blended, otherwise it will be draw with
1170        and + xor blts. */
1171     check_alpha_draw(hdcDst, FALSE, FALSE, 32, __LINE__);
1172     check_alpha_draw(hdcDst, FALSE, TRUE, 32, __LINE__);
1173
1174 cleanup:
1175     if(bmpOld)
1176         SelectObject(hdcDst, bmpOld);
1177     if(bmpDst)
1178         DeleteObject(bmpDst);
1179     if(hdcDst)
1180         DeleteDC(hdcDst);
1181 }
1182
1183 static void check_DrawIconEx(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, UINT flags, COLORREF background,
1184                              COLORREF modern_expected, COLORREF legacy_expected, int line)
1185 {
1186     COLORREF result;
1187     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1188     if (!hicon) return;
1189     SetPixelV(hdc, 0, 0, background);
1190     DrawIconEx(hdc, 0, 0, hicon, 1, 1, 0, NULL, flags);
1191     result = GetPixel(hdc, 0, 0);
1192
1193     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1194         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1195         "Overlaying Mask %d on Color %06X with DrawIconEx flags %08X. "
1196         "Expected a close match to %06X (modern) or %06X (legacy). Got %06X from line %d\n",
1197         maskvalue, color, flags, modern_expected, legacy_expected, result, line);
1198 }
1199
1200 static void test_DrawIconEx(void)
1201 {
1202     BITMAPINFO bitmapInfo;
1203     HDC hdcDst = NULL;
1204     HBITMAP bmpDst = NULL;
1205     HBITMAP bmpOld = NULL;
1206     void *bits = 0;
1207
1208     hdcDst = CreateCompatibleDC(0);
1209     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1210     if (!hdcDst)
1211         return;
1212
1213     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1214     {
1215         skip("Windows will distort DrawIconEx colors at 8-bpp and less due to palletizing.\n");
1216         goto cleanup;
1217     }
1218
1219     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1220     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1221     bitmapInfo.bmiHeader.biWidth = 1;
1222     bitmapInfo.bmiHeader.biHeight = 1;
1223     bitmapInfo.bmiHeader.biBitCount = 32;
1224     bitmapInfo.bmiHeader.biPlanes = 1;
1225     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1226     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1227     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1228     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1229     if (!bmpDst || !bits)
1230         goto cleanup;
1231     bmpOld = SelectObject(hdcDst, bmpDst);
1232
1233     /* Test null, image only, and mask only drawing */
1234     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, 0, 0x00102030, 0x00102030, 0x00102030, __LINE__);
1235     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, 0, 0x00102030, 0x00102030, 0x00102030, __LINE__);
1236
1237     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_MASK, 0x00FFFFFF, 0x00000000, 0x00000000, __LINE__);
1238     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_MASK, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, __LINE__);
1239
1240     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, DI_IMAGE, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1241     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, DI_IMAGE, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1242
1243     /* Test normal drawing */
1244     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1245     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x003F4F5F, 0x003F4F5F, __LINE__);
1246     check_DrawIconEx(hdcDst, FALSE, 0xFFA0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1247
1248     /* Test alpha blending */
1249     /* Windows 2000 and up will alpha blend, earlier Windows versions will not */
1250     check_DrawIconEx(hdcDst, TRUE, 0xFFA0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x003F4F5F, __LINE__);
1251
1252     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_NORMAL, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1253     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_NORMAL, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1254     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00DFD7CF, 0x00C0B0A0, __LINE__);
1255     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00DFD7CF, 0x003F4F5F, __LINE__);
1256
1257     check_DrawIconEx(hdcDst, FALSE, 0x01FFFFFF, 32, DI_NORMAL, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1258     check_DrawIconEx(hdcDst, TRUE, 0x01FFFFFF, 32, DI_NORMAL, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1259
1260     /* Test detecting of alpha channel */
1261     /* If a single pixel's alpha channel is non-zero, the icon
1262        will be alpha blended, otherwise it will be draw with
1263        and + xor blts. */
1264     check_alpha_draw(hdcDst, TRUE, FALSE, 32, __LINE__);
1265     check_alpha_draw(hdcDst, TRUE, TRUE, 32, __LINE__);
1266
1267 cleanup:
1268     if(bmpOld)
1269         SelectObject(hdcDst, bmpOld);
1270     if(bmpDst)
1271         DeleteObject(bmpDst);
1272     if(hdcDst)
1273         DeleteDC(hdcDst);
1274 }
1275
1276 static void check_DrawState_Size(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, HBRUSH hbr, UINT flags, int line)
1277 {
1278     COLORREF result, background;
1279     BOOL passed[2];
1280     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1281     background = 0x00FFFFFF;
1282     /* Set color of the 2 pixels that will be checked afterwards */
1283     SetPixelV(hdc, 0, 0, background);
1284     SetPixelV(hdc, 2, 2, background);
1285
1286     /* Let DrawState calculate the size of the icon (it's 1x1) */
1287     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 0, 0, (DST_ICON | flags ));
1288
1289     result = GetPixel(hdc, 0, 0);
1290     passed[0] = color_match(result, background);
1291     result = GetPixel(hdc, 2, 2);
1292     passed[0] = passed[0] & color_match(result, background);
1293
1294     /* Check if manually specifying the icon size DOESN'T work */
1295
1296     /* IMPORTANT: For Icons, DrawState wants the size of the source image, not the
1297      *            size in which it should be ultimately drawn. Therefore giving
1298      *            width/height 2x2 if the icon is only 1x1 pixels in size should
1299      *            result in drawing it with size 1x1. The size parameters must be
1300      *            ignored if a Icon has to be drawn! */
1301     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 2, 2, (DST_ICON | flags ));
1302
1303     result = GetPixel(hdc, 0, 0);
1304     passed[1] = color_match(result, background);
1305     result = GetPixel(hdc, 2, 2);
1306     passed[1] = passed[0] & color_match(result, background);
1307
1308     if(!passed[0]&&!passed[1])
1309         ok (passed[1],
1310         "DrawState failed to draw a 1x1 Icon in the correct size, independent of the "
1311         "width and height settings passed to it, for Icon with: Overlaying Mask %d on "
1312         "Color %06X with flags %08X. Line %d\n",
1313         maskvalue, color, (DST_ICON | flags), line);
1314     else if(!passed[1])
1315         ok (passed[1],
1316         "DrawState failed to draw a 1x1 Icon in the correct size, if the width and height "
1317         "parameters passed to it are bigger than the real Icon size, for Icon with: Overlaying "
1318         "Mask %d on Color %06X with flags %08X. Line %d\n",
1319         maskvalue, color, (DST_ICON | flags), line);
1320     else
1321         ok (passed[0],
1322         "DrawState failed to draw a 1x1 Icon in the correct size, if the width and height "
1323         "parameters passed to it are 0, for Icon with: Overlaying Mask %d on "
1324         "Color %06X with flags %08X. Line %d\n",
1325         maskvalue, color, (DST_ICON | flags), line);
1326 }
1327
1328 static void check_DrawState_Color(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, HBRUSH hbr, UINT flags,
1329                              COLORREF background, COLORREF modern_expected, COLORREF legacy_expected, int line)
1330 {
1331     COLORREF result;
1332     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1333     if (!hicon) return;
1334     /* Set color of the pixel that will be checked afterwards */
1335     SetPixelV(hdc, 1, 1, background);
1336
1337     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 0, 0, ( DST_ICON | flags ));
1338
1339     /* Check the color of the pixel is correct */
1340     result = GetPixel(hdc, 1, 1);
1341
1342     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1343         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1344         "DrawState drawing Icon with Overlaying Mask %d on Color %06X with flags %08X. "
1345         "Expected a close match to %06X (modern) or %06X (legacy). Got %06X from line %d\n",
1346         maskvalue, color, (DST_ICON | flags), modern_expected, legacy_expected, result, line);
1347 }
1348
1349 static void test_DrawState(void)
1350 {
1351     BITMAPINFO bitmapInfo;
1352     HDC hdcDst = NULL;
1353     HBITMAP bmpDst = NULL;
1354     HBITMAP bmpOld = NULL;
1355     void *bits = 0;
1356
1357     hdcDst = CreateCompatibleDC(0);
1358     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1359     if (!hdcDst)
1360         return;
1361
1362     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1363     {
1364         skip("Windows will distort DrawIconEx colors at 8-bpp and less due to palletizing.\n");
1365         goto cleanup;
1366     }
1367
1368     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1369     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1370     bitmapInfo.bmiHeader.biWidth = 3;
1371     bitmapInfo.bmiHeader.biHeight = 3;
1372     bitmapInfo.bmiHeader.biBitCount = 32;
1373     bitmapInfo.bmiHeader.biPlanes = 1;
1374     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1375     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1376     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1377     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1378     if (!bmpDst || !bits)
1379         goto cleanup;
1380     bmpOld = SelectObject(hdcDst, bmpDst);
1381
1382     /* potential flags to test with DrawState are: */
1383     /* DSS_DISABLED embosses the icon */
1384     /* DSS_MONO draw Icon using a brush as parameter 5 */
1385     /* DSS_NORMAL draw Icon without any modifications */
1386     /* DSS_UNION draw the Icon dithered */
1387
1388     check_DrawState_Size(hdcDst, FALSE, 0x00A0B0C0, 32, 0, DSS_NORMAL, __LINE__);
1389     check_DrawState_Color(hdcDst, FALSE, 0x00A0B0C0, 32, 0, DSS_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1390
1391 cleanup:
1392     if(bmpOld)
1393         SelectObject(hdcDst, bmpOld);
1394     if(bmpDst)
1395         DeleteObject(bmpDst);
1396     if(hdcDst)
1397         DeleteDC(hdcDst);
1398 }
1399
1400 static DWORD parent_id;
1401
1402 static DWORD CALLBACK set_cursor_thread( void *arg )
1403 {
1404     HCURSOR ret;
1405
1406     PeekMessage( 0, 0, 0, 0, PM_NOREMOVE );  /* create a msg queue */
1407     if (parent_id)
1408     {
1409         BOOL ret = AttachThreadInput( GetCurrentThreadId(), parent_id, TRUE );
1410         ok( ret, "AttachThreadInput failed\n" );
1411     }
1412     if (arg) ret = SetCursor( (HCURSOR)arg );
1413     else ret = GetCursor();
1414     return (DWORD_PTR)ret;
1415 }
1416
1417 static void test_SetCursor(void)
1418 {
1419     static const BYTE bmp_bits[4096];
1420     ICONINFO cursorInfo;
1421     HCURSOR cursor, old_cursor, global_cursor = 0;
1422     DWORD error, id, result;
1423     UINT display_bpp;
1424     HDC hdc;
1425     HANDLE thread;
1426     CURSORINFO info;
1427
1428     if (pGetCursorInfo)
1429     {
1430         memset( &info, 0, sizeof(info) );
1431         info.cbSize = sizeof(info);
1432         if (!pGetCursorInfo( &info ))
1433         {
1434             win_skip( "GetCursorInfo not working\n" );
1435             pGetCursorInfo = NULL;
1436         }
1437         else global_cursor = info.hCursor;
1438     }
1439     cursor = GetCursor();
1440     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
1441     WaitForSingleObject( thread, 1000 );
1442     GetExitCodeThread( thread, &result );
1443     ok( result == (DWORD_PTR)cursor, "wrong thread cursor %x/%p\n", result, cursor );
1444
1445     hdc = GetDC(0);
1446     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
1447     ReleaseDC(0, hdc);
1448
1449     cursorInfo.fIcon = FALSE;
1450     cursorInfo.xHotspot = 0;
1451     cursorInfo.yHotspot = 0;
1452     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
1453     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
1454
1455     cursor = CreateIconIndirect(&cursorInfo);
1456     ok(cursor != NULL, "CreateIconIndirect returned %p\n", cursor);
1457     old_cursor = SetCursor( cursor );
1458
1459     if (pGetCursorInfo)
1460     {
1461         info.cbSize = sizeof(info);
1462         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1463         /* global cursor doesn't change since we don't have a window */
1464         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
1465             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
1466     }
1467     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
1468     WaitForSingleObject( thread, 1000 );
1469     GetExitCodeThread( thread, &result );
1470     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1471
1472     SetCursor( 0 );
1473     ok( GetCursor() == 0, "wrong cursor %p\n", GetCursor() );
1474     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
1475     WaitForSingleObject( thread, 1000 );
1476     GetExitCodeThread( thread, &result );
1477     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1478
1479     thread = CreateThread( NULL, 0, set_cursor_thread, cursor, 0, &id );
1480     WaitForSingleObject( thread, 1000 );
1481     GetExitCodeThread( thread, &result );
1482     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1483     ok( GetCursor() == 0, "wrong cursor %p/0\n", GetCursor() );
1484
1485     parent_id = GetCurrentThreadId();
1486     thread = CreateThread( NULL, 0, set_cursor_thread, cursor, 0, &id );
1487     WaitForSingleObject( thread, 1000 );
1488     GetExitCodeThread( thread, &result );
1489     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1490     ok( GetCursor() == cursor, "wrong cursor %p/0\n", cursor );
1491
1492     if (pGetCursorInfo)
1493     {
1494         info.cbSize = sizeof(info);
1495         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1496         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
1497             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
1498     }
1499     SetCursor( old_cursor );
1500     DestroyCursor( cursor );
1501
1502     SetLastError( 0xdeadbeef );
1503     cursor = SetCursor( (HCURSOR)0xbadbad );
1504     error = GetLastError();
1505     ok( cursor == 0, "wrong cursor %p/0\n", cursor );
1506     ok( error == ERROR_INVALID_CURSOR_HANDLE || broken( error == 0xdeadbeef ),  /* win9x */
1507         "wrong error %u\n", error );
1508
1509     if (pGetCursorInfo)
1510     {
1511         info.cbSize = sizeof(info);
1512         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1513         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
1514             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
1515     }
1516 }
1517
1518 static HANDLE event_start, event_next;
1519
1520 static DWORD CALLBACK show_cursor_thread( void *arg )
1521 {
1522     DWORD count = (DWORD_PTR)arg;
1523     int ret;
1524
1525     PeekMessage( 0, 0, 0, 0, PM_NOREMOVE );  /* create a msg queue */
1526     if (parent_id)
1527     {
1528         BOOL ret = AttachThreadInput( GetCurrentThreadId(), parent_id, TRUE );
1529         ok( ret, "AttachThreadInput failed\n" );
1530     }
1531     if (!count) ret = ShowCursor( FALSE );
1532     else while (count--) ret = ShowCursor( TRUE );
1533     SetEvent( event_start );
1534     WaitForSingleObject( event_next, 2000 );
1535     return ret;
1536 }
1537
1538 static void test_ShowCursor(void)
1539 {
1540     int count;
1541     DWORD id, result;
1542     HANDLE thread;
1543     CURSORINFO info;
1544
1545     if (pGetCursorInfo)
1546     {
1547         memset( &info, 0, sizeof(info) );
1548         info.cbSize = sizeof(info);
1549         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1550         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1551     }
1552
1553     event_start = CreateEvent( NULL, FALSE, FALSE, NULL );
1554     event_next = CreateEvent( NULL, FALSE, FALSE, NULL );
1555
1556     count = ShowCursor( TRUE );
1557     ok( count == 1, "wrong count %d\n", count );
1558     count = ShowCursor( TRUE );
1559     ok( count == 2, "wrong count %d\n", count );
1560     count = ShowCursor( FALSE );
1561     ok( count == 1, "wrong count %d\n", count );
1562     count = ShowCursor( FALSE );
1563     ok( count == 0, "wrong count %d\n", count );
1564     count = ShowCursor( FALSE );
1565     ok( count == -1, "wrong count %d\n", count );
1566     count = ShowCursor( FALSE );
1567     ok( count == -2, "wrong count %d\n", count );
1568
1569     if (pGetCursorInfo)
1570     {
1571         info.cbSize = sizeof(info);
1572         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1573         /* global show count is not affected since we don't have a window */
1574         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1575     }
1576
1577     parent_id = 0;
1578     thread = CreateThread( NULL, 0, show_cursor_thread, NULL, 0, &id );
1579     WaitForSingleObject( event_start, 1000 );
1580     count = ShowCursor( FALSE );
1581     ok( count == -3, "wrong count %d\n", count );
1582     SetEvent( event_next );
1583     WaitForSingleObject( thread, 1000 );
1584     GetExitCodeThread( thread, &result );
1585     ok( result == -1, "wrong thread count %d\n", result );
1586     count = ShowCursor( FALSE );
1587     ok( count == -4, "wrong count %d\n", count );
1588
1589     thread = CreateThread( NULL, 0, show_cursor_thread, (void *)1, 0, &id );
1590     WaitForSingleObject( event_start, 1000 );
1591     count = ShowCursor( TRUE );
1592     ok( count == -3, "wrong count %d\n", count );
1593     SetEvent( event_next );
1594     WaitForSingleObject( thread, 1000 );
1595     GetExitCodeThread( thread, &result );
1596     ok( result == 1, "wrong thread count %d\n", result );
1597     count = ShowCursor( TRUE );
1598     ok( count == -2, "wrong count %d\n", count );
1599
1600     parent_id = GetCurrentThreadId();
1601     thread = CreateThread( NULL, 0, show_cursor_thread, NULL, 0, &id );
1602     WaitForSingleObject( event_start, 1000 );
1603     count = ShowCursor( TRUE );
1604     ok( count == -2, "wrong count %d\n", count );
1605     SetEvent( event_next );
1606     WaitForSingleObject( thread, 1000 );
1607     GetExitCodeThread( thread, &result );
1608     ok( result == -3, "wrong thread count %d\n", result );
1609     count = ShowCursor( FALSE );
1610     ok( count == -2, "wrong count %d\n", count );
1611
1612     thread = CreateThread( NULL, 0, show_cursor_thread, (void *)3, 0, &id );
1613     WaitForSingleObject( event_start, 1000 );
1614     count = ShowCursor( TRUE );
1615     ok( count == 2, "wrong count %d\n", count );
1616     SetEvent( event_next );
1617     WaitForSingleObject( thread, 1000 );
1618     GetExitCodeThread( thread, &result );
1619     ok( result == 1, "wrong thread count %d\n", result );
1620     count = ShowCursor( FALSE );
1621     ok( count == -2, "wrong count %d\n", count );
1622
1623     if (pGetCursorInfo)
1624     {
1625         info.cbSize = sizeof(info);
1626         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1627         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1628     }
1629
1630     count = ShowCursor( TRUE );
1631     ok( count == -1, "wrong count %d\n", count );
1632     count = ShowCursor( TRUE );
1633     ok( count == 0, "wrong count %d\n", count );
1634
1635     if (pGetCursorInfo)
1636     {
1637         info.cbSize = sizeof(info);
1638         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1639         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1640     }
1641 }
1642
1643
1644 static void test_DestroyCursor(void)
1645 {
1646     static const BYTE bmp_bits[4096];
1647     ICONINFO cursorInfo;
1648     HCURSOR cursor, cursor2;
1649     BOOL ret;
1650     DWORD error;
1651     UINT display_bpp;
1652     HDC hdc;
1653
1654     hdc = GetDC(0);
1655     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
1656     ReleaseDC(0, hdc);
1657
1658     cursorInfo.fIcon = FALSE;
1659     cursorInfo.xHotspot = 0;
1660     cursorInfo.yHotspot = 0;
1661     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
1662     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
1663
1664     cursor = CreateIconIndirect(&cursorInfo);
1665     ok(cursor != NULL, "CreateIconIndirect returned %p\n", cursor);
1666     if(!cursor) {
1667         return;
1668     }
1669     SetCursor(cursor);
1670
1671     SetLastError(0xdeadbeef);
1672     ret = DestroyCursor(cursor);
1673     ok(!ret || broken(ret)  /* succeeds on win9x */, "DestroyCursor on the active cursor succeeded\n");
1674     error = GetLastError();
1675     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1676     if (!ret)
1677     {
1678         cursor2 = GetCursor();
1679         ok(cursor2 == cursor, "Active was set to %p when trying to destroy it\n", cursor2);
1680         SetCursor(NULL);
1681
1682         /* Trying to destroy the cursor properly fails now with
1683          * ERROR_INVALID_CURSOR_HANDLE.  This happens because we called
1684          * DestroyCursor() 2+ times after calling SetCursor().  The calls to
1685          * GetCursor() and SetCursor(NULL) in between make no difference. */
1686         SetLastError(0xdeadbeef);
1687         ret = DestroyCursor(cursor);
1688         todo_wine ok(!ret, "DestroyCursor succeeded.\n");
1689         error = GetLastError();
1690         ok(error == ERROR_INVALID_CURSOR_HANDLE || error == 0xdeadbeef, /* vista */
1691            "Last error: 0x%08x\n", error);
1692     }
1693
1694     DeleteObject(cursorInfo.hbmMask);
1695     DeleteObject(cursorInfo.hbmColor);
1696
1697     /* Try testing DestroyCursor() now using LoadCursor() cursors. */
1698     cursor = LoadCursor(NULL, IDC_ARROW);
1699
1700     SetLastError(0xdeadbeef);
1701     ret = DestroyCursor(cursor);
1702     ok(ret || broken(!ret) /* fails on win9x */, "DestroyCursor on the active cursor failed.\n");
1703     error = GetLastError();
1704     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
1705
1706     /* Try setting the cursor to a destroyed OEM cursor. */
1707     SetLastError(0xdeadbeef);
1708     SetCursor(cursor);
1709     error = GetLastError();
1710     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
1711
1712     /* Check if LoadCursor() returns the same handle with the same icon. */
1713     cursor2 = LoadCursor(NULL, IDC_ARROW);
1714     ok(cursor2 == cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
1715
1716     /* Check if LoadCursor() returns the same handle with a different icon. */
1717     cursor2 = LoadCursor(NULL, IDC_WAIT);
1718     ok(cursor2 != cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
1719 }
1720
1721 START_TEST(cursoricon)
1722 {
1723     pGetCursorInfo = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetCursorInfo" );
1724     test_argc = winetest_get_mainargs(&test_argv);
1725
1726     if (test_argc >= 3)
1727     {
1728         /* Child process. */
1729         sscanf (test_argv[2], "%x", (unsigned int *) &parent);
1730
1731         ok(parent != NULL, "Parent not found.\n");
1732         if (parent == NULL)
1733             ExitProcess(1);
1734
1735         do_child();
1736         return;
1737     }
1738
1739     test_CopyImage_Bitmap(1);
1740     test_CopyImage_Bitmap(4);
1741     test_CopyImage_Bitmap(8);
1742     test_CopyImage_Bitmap(16);
1743     test_CopyImage_Bitmap(24);
1744     test_CopyImage_Bitmap(32);
1745     test_initial_cursor();
1746     test_CreateIcon();
1747     test_LoadImage();
1748     test_CreateIconFromResource();
1749     test_DrawIcon();
1750     test_DrawIconEx();
1751     test_DrawState();
1752     test_SetCursor();
1753     test_ShowCursor();
1754     test_DestroyCursor();
1755     do_parent();
1756     test_child_process();
1757     finish_child_process();
1758 }