samlib: Add stubbed samlib.dll.
[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 /* 2x2 pixel gif */
720 static unsigned char gif4pixel[42] = {
721 0x47,0x49,0x46,0x38,0x37,0x61,0x02,0x00,0x02,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,
722 0x39,0x62,0xfc,0xff,0x1a,0xe5,0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x02,0x00,
723 0x02,0x00,0x00,0x02,0x03,0x14,0x16,0x05,0x00,0x3b
724 };
725
726 static void test_LoadImageBitmap(HBITMAP hbm)
727 {
728     BITMAP bm;
729     BITMAPINFO bmi;
730     DWORD ret, pixel = 0;
731     HDC hdc = GetDC(NULL);
732
733     ret = GetObject(hbm, sizeof(bm), &bm);
734     ok(ret == sizeof(bm), "GetObject returned %d\n", ret);
735
736     memset(&bmi, 0, sizeof(bmi));
737     bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
738     bmi.bmiHeader.biWidth = bm.bmWidth;
739     bmi.bmiHeader.biHeight = bm.bmHeight;
740     bmi.bmiHeader.biPlanes = 1;
741     bmi.bmiHeader.biBitCount= 24;
742     bmi.bmiHeader.biCompression= BI_RGB;
743     ret = GetDIBits(hdc, hbm, 0, bm.bmHeight, &pixel, &bmi, DIB_RGB_COLORS);
744     ok(ret == bm.bmHeight, "%d lines were converted, not %d\n", ret, bm.bmHeight);
745
746     ok(color_match(pixel, 0x00ffffff), "Pixel is 0x%08x\n", pixel);
747 }
748
749 static void test_LoadImageFile(unsigned char * image_data,
750     unsigned int image_size, const char * ext, BOOL expect_success)
751 {
752     HANDLE handle;
753     BOOL ret;
754     DWORD error, bytes_written;
755     char filename[64];
756
757     strcpy(filename, "test.");
758     strcat(filename, ext);
759
760     /* Create the test image. */
761     handle = CreateFileA(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
762         FILE_ATTRIBUTE_NORMAL, NULL);
763     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
764     ret = WriteFile(handle, image_data, image_size, &bytes_written, NULL);
765     ok(ret && bytes_written == image_size, "test file created improperly.\n");
766     CloseHandle(handle);
767
768     /* Load as cursor. For all tested formats, this should fail */
769     SetLastError(0xdeadbeef);
770     handle = LoadImageA(NULL, filename, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
771     ok(handle == NULL, "LoadImage(%s) as IMAGE_CURSOR succeeded incorrectly.\n", ext);
772     error = GetLastError();
773     ok(error == 0 ||
774         broken(error == 0xdeadbeef) || /* Win9x */
775         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
776         "Last error: %u\n", error);
777     if (handle != NULL) DestroyCursor(handle);
778
779     /* Load as icon. For all tested formats, this should fail */
780     SetLastError(0xdeadbeef);
781     handle = LoadImageA(NULL, filename, IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
782     ok(handle == NULL, "LoadImage(%s) as IMAGE_ICON succeeded incorrectly.\n", ext);
783     error = GetLastError();
784     ok(error == 0 ||
785         broken(error == 0xdeadbeef) || /* Win9x */
786         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
787         "Last error: %u\n", error);
788     if (handle != NULL) DestroyIcon(handle);
789
790     /* Load as bitmap. Should succeed for correct bmp, fail for everything else */
791     SetLastError(0xdeadbeef);
792     handle = LoadImageA(NULL, filename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
793     error = GetLastError();
794     ok(error == 0 ||
795         error == 0xdeadbeef, /* Win9x, WinMe */
796         "Last error: %u\n", error);
797
798     if (expect_success) {
799         ok(handle != NULL, "LoadImage(%s) as IMAGE_BITMAP failed.\n", ext);
800         if (handle != NULL) test_LoadImageBitmap(handle);
801     }
802     else ok(handle == NULL, "LoadImage(%s) as IMAGE_BITMAP succeeded incorrectly.\n", ext);
803
804     if (handle != NULL) DeleteObject(handle);
805     DeleteFileA(filename);
806 }
807
808 static void test_LoadImage(void)
809 {
810     HANDLE handle;
811     BOOL ret;
812     DWORD error, bytes_written;
813     CURSORICONFILEDIR *icon_data;
814     CURSORICONFILEDIRENTRY *icon_entry;
815     BITMAPINFOHEADER *icon_header;
816     ICONINFO icon_info;
817
818 #define ICON_WIDTH 32
819 #define ICON_HEIGHT 32
820 #define ICON_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
821 #define ICON_BPP 32
822 #define ICON_SIZE \
823     (sizeof(CURSORICONFILEDIR) + sizeof(BITMAPINFOHEADER) \
824     + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
825
826     /* Set icon data. */
827     icon_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ICON_SIZE);
828     icon_data->idReserved = 0;
829     icon_data->idType = 1;
830     icon_data->idCount = 1;
831
832     icon_entry = icon_data->idEntries;
833     icon_entry->bWidth = ICON_WIDTH;
834     icon_entry->bHeight = ICON_HEIGHT;
835     icon_entry->bColorCount = 0;
836     icon_entry->bReserved = 0;
837     icon_entry->xHotspot = 1;
838     icon_entry->yHotspot = 1;
839     icon_entry->dwDIBSize = ICON_SIZE - sizeof(CURSORICONFILEDIR);
840     icon_entry->dwDIBOffset = sizeof(CURSORICONFILEDIR);
841
842     icon_header = (BITMAPINFOHEADER *) ((BYTE *) icon_data + icon_entry->dwDIBOffset);
843     icon_header->biSize = sizeof(BITMAPINFOHEADER);
844     icon_header->biWidth = ICON_WIDTH;
845     icon_header->biHeight = ICON_HEIGHT*2;
846     icon_header->biPlanes = 1;
847     icon_header->biBitCount = ICON_BPP;
848     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
849
850     /* Create the icon. */
851     handle = CreateFileA("icon.ico", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_NEW,
852         FILE_ATTRIBUTE_NORMAL, NULL);
853     ok(handle != INVALID_HANDLE_VALUE, "CreateFileA failed. %u\n", GetLastError());
854     ret = WriteFile(handle, icon_data, ICON_SIZE, &bytes_written, NULL);
855     ok(ret && bytes_written == ICON_SIZE, "icon.ico created improperly.\n");
856     CloseHandle(handle);
857
858     /* Test loading an icon as a cursor. */
859     SetLastError(0xdeadbeef);
860     handle = LoadImageA(NULL, "icon.ico", IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
861     ok(handle != NULL, "LoadImage() failed.\n");
862     error = GetLastError();
863     ok(error == 0 ||
864         broken(error == 0xdeadbeef) || /* Win9x */
865         broken(error == ERROR_BAD_PATHNAME), /* Win98, WinMe */
866         "Last error: %u\n", error);
867
868     /* Test the icon information. */
869     SetLastError(0xdeadbeef);
870     ret = GetIconInfo(handle, &icon_info);
871     ok(ret, "GetIconInfo() failed.\n");
872     error = GetLastError();
873     ok(error == 0xdeadbeef, "Last error: %u\n", error);
874
875     if (ret)
876     {
877         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
878         ok(icon_info.xHotspot == 1, "xHotspot is %u.\n", icon_info.xHotspot);
879         ok(icon_info.yHotspot == 1, "yHotspot is %u.\n", icon_info.yHotspot);
880         ok(icon_info.hbmColor != NULL || broken(!icon_info.hbmColor) /* no color cursor support */,
881            "No hbmColor!\n");
882         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
883     }
884
885     /* Clean up. */
886     SetLastError(0xdeadbeef);
887     ret = DestroyCursor(handle);
888     ok(ret, "DestroyCursor() failed.\n");
889     error = GetLastError();
890     ok(error == 0xdeadbeef, "Last error: %u\n", error);
891
892     HeapFree(GetProcessHeap(), 0, icon_data);
893     DeleteFileA("icon.ico");
894
895     test_LoadImageFile(bmpimage, sizeof(bmpimage), "bmp", 1);
896     test_LoadImageFile(gifimage, sizeof(gifimage), "gif", 0);
897     test_LoadImageFile(gif4pixel, sizeof(gif4pixel), "gif", 0);
898     test_LoadImageFile(jpgimage, sizeof(jpgimage), "jpg", 0);
899     test_LoadImageFile(pngimage, sizeof(pngimage), "png", 0);
900     /* Check failure for broken BMP images */
901     bmpimage[0x14]++; /* biHeight > 65535 */
902     test_LoadImageFile(bmpimage, sizeof(bmpimage), "bmp", 0);
903     bmpimage[0x14]--;
904     bmpimage[0x18]++; /* biWidth > 65535 */
905     test_LoadImageFile(bmpimage, sizeof(bmpimage), "bmp", 0);
906     bmpimage[0x18]--;
907 }
908
909 static void test_CreateIconFromResource(void)
910 {
911     HANDLE handle;
912     BOOL ret;
913     DWORD error;
914     BITMAPINFOHEADER *icon_header;
915     INT16 *hotspot;
916     ICONINFO icon_info;
917
918 #define ICON_RES_WIDTH 32
919 #define ICON_RES_HEIGHT 32
920 #define ICON_RES_AND_SIZE (ICON_WIDTH*ICON_HEIGHT/8)
921 #define ICON_RES_BPP 32
922 #define ICON_RES_SIZE \
923     (sizeof(BITMAPINFOHEADER) + ICON_AND_SIZE + ICON_AND_SIZE*ICON_BPP)
924 #define CRSR_RES_SIZE (2*sizeof(INT16) + ICON_RES_SIZE)
925
926     /* Set icon data. */
927     hotspot = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, CRSR_RES_SIZE);
928
929     /* Cursor resources have an extra hotspot, icon resources not. */
930     hotspot[0] = 3;
931     hotspot[1] = 3;
932
933     icon_header = (BITMAPINFOHEADER *) (hotspot + 2);
934     icon_header->biSize = sizeof(BITMAPINFOHEADER);
935     icon_header->biWidth = ICON_WIDTH;
936     icon_header->biHeight = ICON_HEIGHT*2;
937     icon_header->biPlanes = 1;
938     icon_header->biBitCount = ICON_BPP;
939     icon_header->biSizeImage = 0; /* Uncompressed bitmap. */
940
941     /* Test creating a cursor. */
942     SetLastError(0xdeadbeef);
943     handle = CreateIconFromResource((PBYTE) hotspot, CRSR_RES_SIZE, FALSE, 0x00030000);
944     ok(handle != NULL, "Create cursor failed.\n");
945
946     /* Test the icon information. */
947     SetLastError(0xdeadbeef);
948     ret = GetIconInfo(handle, &icon_info);
949     ok(ret, "GetIconInfo() failed.\n");
950     error = GetLastError();
951     ok(error == 0xdeadbeef, "Last error: %u\n", error);
952
953     if (ret)
954     {
955         ok(icon_info.fIcon == FALSE, "fIcon != FALSE.\n");
956         ok(icon_info.xHotspot == 3, "xHotspot is %u.\n", icon_info.xHotspot);
957         ok(icon_info.yHotspot == 3, "yHotspot is %u.\n", icon_info.yHotspot);
958         ok(icon_info.hbmColor != NULL || broken(!icon_info.hbmColor) /* no color cursor support */,
959            "No hbmColor!\n");
960         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
961     }
962
963     /* Clean up. */
964     SetLastError(0xdeadbeef);
965     ret = DestroyCursor(handle);
966     ok(ret, "DestroyCursor() failed.\n");
967     error = GetLastError();
968     ok(error == 0xdeadbeef, "Last error: %u\n", error);
969
970     /* Test creating an icon. */
971     SetLastError(0xdeadbeef);
972     handle = CreateIconFromResource((PBYTE) icon_header, ICON_RES_SIZE, TRUE,
973                                     0x00030000);
974     ok(handle != NULL, "Create icon failed.\n");
975
976     /* Test the icon information. */
977     SetLastError(0xdeadbeef);
978     ret = GetIconInfo(handle, &icon_info);
979     ok(ret, "GetIconInfo() failed.\n");
980     error = GetLastError();
981     ok(error == 0xdeadbeef, "Last error: %u\n", error);
982
983     if (ret)
984     {
985         ok(icon_info.fIcon == TRUE, "fIcon != TRUE.\n");
986         /* Icons always have hotspot in the middle */
987         ok(icon_info.xHotspot == ICON_WIDTH/2, "xHotspot is %u.\n", icon_info.xHotspot);
988         ok(icon_info.yHotspot == ICON_HEIGHT/2, "yHotspot is %u.\n", icon_info.yHotspot);
989         ok(icon_info.hbmColor != NULL, "No hbmColor!\n");
990         ok(icon_info.hbmMask != NULL, "No hbmMask!\n");
991     }
992
993     /* Clean up. */
994     SetLastError(0xdeadbeef);
995     ret = DestroyCursor(handle);
996     ok(ret, "DestroyCursor() failed.\n");
997     error = GetLastError();
998     ok(error == 0xdeadbeef, "Last error: %u\n", error);
999
1000     HeapFree(GetProcessHeap(), 0, hotspot);
1001 }
1002
1003 static HICON create_test_icon(HDC hdc, int width, int height, int bpp,
1004                               BOOL maskvalue, UINT32 *color, int colorSize)
1005 {
1006     ICONINFO iconInfo;
1007     BITMAPINFO bitmapInfo;
1008     void *buffer = NULL;
1009     UINT32 mask = maskvalue ? 0xFFFFFFFF : 0x00000000;
1010
1011     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1012     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1013     bitmapInfo.bmiHeader.biWidth = width;
1014     bitmapInfo.bmiHeader.biHeight = height;
1015     bitmapInfo.bmiHeader.biPlanes = 1;
1016     bitmapInfo.bmiHeader.biBitCount = bpp;
1017     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1018     bitmapInfo.bmiHeader.biSizeImage = colorSize;
1019
1020     iconInfo.fIcon = TRUE;
1021     iconInfo.xHotspot = 0;
1022     iconInfo.yHotspot = 0;
1023
1024     iconInfo.hbmMask = CreateBitmap( width, height, 1, 1, &mask );
1025     if(!iconInfo.hbmMask) return NULL;
1026
1027     iconInfo.hbmColor = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, &buffer, NULL, 0);
1028     if(!iconInfo.hbmColor || !buffer)
1029     {
1030         DeleteObject(iconInfo.hbmMask);
1031         return NULL;
1032     }
1033
1034     memcpy(buffer, color, colorSize);
1035
1036     return CreateIconIndirect(&iconInfo);
1037 }
1038
1039 static void check_alpha_draw(HDC hdc, BOOL drawiconex, BOOL alpha, int bpp, int line)
1040 {
1041     HICON hicon;
1042     UINT32 color[2];
1043     COLORREF modern_expected, legacy_expected, result;
1044
1045     color[0] = 0x00A0B0C0;
1046     color[1] = alpha ? 0xFF000000 : 0x00000000;
1047     modern_expected = alpha ? 0x00FFFFFF : 0x00C0B0A0;
1048     legacy_expected = 0x00C0B0A0;
1049
1050     hicon = create_test_icon(hdc, 2, 1, bpp, 0, color, sizeof(color));
1051     if (!hicon) return;
1052
1053     SetPixelV(hdc, 0, 0, 0x00FFFFFF);
1054
1055     if(drawiconex)
1056         DrawIconEx(hdc, 0, 0, hicon, 2, 1, 0, NULL, DI_NORMAL);
1057     else
1058         DrawIcon(hdc, 0, 0, hicon);
1059
1060     result = GetPixel(hdc, 0, 0);
1061     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1062         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1063         "%s. Expected a close match to %06X (modern) or %06X (legacy) with %s. "
1064         "Got %06X from line %d\n",
1065         alpha ? "Alpha blending" : "Not alpha blending", modern_expected, legacy_expected,
1066         drawiconex ? "DrawIconEx" : "DrawIcon", result, line);
1067 }
1068
1069 static void check_DrawIcon(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, COLORREF background,
1070                            COLORREF modern_expected, COLORREF legacy_expected, int line)
1071 {
1072     COLORREF result;
1073     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1074     if (!hicon) return;
1075     SetPixelV(hdc, 0, 0, background);
1076     SetPixelV(hdc, GetSystemMetrics(SM_CXICON)-1, GetSystemMetrics(SM_CYICON)-1, background);
1077     SetPixelV(hdc, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), background);
1078     DrawIcon(hdc, 0, 0, hicon);
1079     result = GetPixel(hdc, 0, 0);
1080
1081     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1082         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1083         "Overlaying Mask %d on Color %06X with DrawIcon. "
1084         "Expected a close match to %06X (modern), or %06X (legacy). Got %06X from line %d\n",
1085         maskvalue, color, modern_expected, legacy_expected, result, line);
1086
1087     result = GetPixel(hdc, GetSystemMetrics(SM_CXICON)-1, GetSystemMetrics(SM_CYICON)-1);
1088
1089     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1090         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1091         "Overlaying Mask %d on Color %06X with DrawIcon. "
1092         "Expected a close match to %06X (modern), or %06X (legacy). Got %06X from line %d\n",
1093         maskvalue, color, modern_expected, legacy_expected, result, line);
1094
1095     result = GetPixel(hdc, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
1096
1097     ok (color_match(result, background),
1098         "Overlaying Mask %d on Color %06X with DrawIcon. "
1099         "Expected unchanged background color %06X. Got %06X from line %d\n",
1100         maskvalue, color, background, result, line);
1101 }
1102
1103 static void test_DrawIcon(void)
1104 {
1105     BITMAPINFO bitmapInfo;
1106     HDC hdcDst = NULL;
1107     HBITMAP bmpDst = NULL;
1108     HBITMAP bmpOld = NULL;
1109     void *bits = 0;
1110
1111     hdcDst = CreateCompatibleDC(0);
1112     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1113     if (!hdcDst)
1114         return;
1115
1116     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1117     {
1118         skip("Windows will distort DrawIcon colors at 8-bpp and less due to palletizing.\n");
1119         goto cleanup;
1120     }
1121
1122     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1123     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1124     bitmapInfo.bmiHeader.biWidth = GetSystemMetrics(SM_CXICON)+1;
1125     bitmapInfo.bmiHeader.biHeight = GetSystemMetrics(SM_CYICON)+1;
1126     bitmapInfo.bmiHeader.biBitCount = 32;
1127     bitmapInfo.bmiHeader.biPlanes = 1;
1128     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1129     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1130
1131     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1132     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1133     if (!bmpDst || !bits)
1134         goto cleanup;
1135     bmpOld = SelectObject(hdcDst, bmpDst);
1136
1137     /* Mask is only heeded if alpha channel is always zero */
1138     check_DrawIcon(hdcDst, FALSE, 0x00A0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1139     check_DrawIcon(hdcDst, TRUE, 0x00A0B0C0, 32, 0x00FFFFFF, 0x003F4F5F, 0x003F4F5F, __LINE__);
1140
1141     /* Test alpha blending */
1142     /* Windows 2000 and up will alpha blend, earlier Windows versions will not */
1143     check_DrawIcon(hdcDst, FALSE, 0xFFA0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1144     check_DrawIcon(hdcDst, TRUE, 0xFFA0B0C0, 32, 0x00FFFFFF, 0x00C0B0A0, 0x003F4F5F, __LINE__);
1145
1146     check_DrawIcon(hdcDst, FALSE, 0x80A0B0C0, 32, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1147     check_DrawIcon(hdcDst, TRUE, 0x80A0B0C0, 32, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1148     check_DrawIcon(hdcDst, FALSE, 0x80A0B0C0, 32, 0x00FFFFFF, 0x00DFD7CF, 0x00C0B0A0, __LINE__);
1149     check_DrawIcon(hdcDst, TRUE, 0x80A0B0C0, 32, 0x00FFFFFF, 0x00DFD7CF, 0x003F4F5F, __LINE__);
1150
1151     check_DrawIcon(hdcDst, FALSE, 0x01FFFFFF, 32, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1152     check_DrawIcon(hdcDst, TRUE, 0x01FFFFFF, 32, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1153
1154     /* Test detecting of alpha channel */
1155     /* If a single pixel's alpha channel is non-zero, the icon
1156        will be alpha blended, otherwise it will be draw with
1157        and + xor blts. */
1158     check_alpha_draw(hdcDst, FALSE, FALSE, 32, __LINE__);
1159     check_alpha_draw(hdcDst, FALSE, TRUE, 32, __LINE__);
1160
1161 cleanup:
1162     if(bmpOld)
1163         SelectObject(hdcDst, bmpOld);
1164     if(bmpDst)
1165         DeleteObject(bmpDst);
1166     if(hdcDst)
1167         DeleteDC(hdcDst);
1168 }
1169
1170 static void check_DrawIconEx(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, UINT flags, COLORREF background,
1171                              COLORREF modern_expected, COLORREF legacy_expected, int line)
1172 {
1173     COLORREF result;
1174     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1175     if (!hicon) return;
1176     SetPixelV(hdc, 0, 0, background);
1177     DrawIconEx(hdc, 0, 0, hicon, 1, 1, 0, NULL, flags);
1178     result = GetPixel(hdc, 0, 0);
1179
1180     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1181         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1182         "Overlaying Mask %d on Color %06X with DrawIconEx flags %08X. "
1183         "Expected a close match to %06X (modern) or %06X (legacy). Got %06X from line %d\n",
1184         maskvalue, color, flags, modern_expected, legacy_expected, result, line);
1185 }
1186
1187 static void test_DrawIconEx(void)
1188 {
1189     BITMAPINFO bitmapInfo;
1190     HDC hdcDst = NULL;
1191     HBITMAP bmpDst = NULL;
1192     HBITMAP bmpOld = NULL;
1193     void *bits = 0;
1194
1195     hdcDst = CreateCompatibleDC(0);
1196     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1197     if (!hdcDst)
1198         return;
1199
1200     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1201     {
1202         skip("Windows will distort DrawIconEx colors at 8-bpp and less due to palletizing.\n");
1203         goto cleanup;
1204     }
1205
1206     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1207     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1208     bitmapInfo.bmiHeader.biWidth = 1;
1209     bitmapInfo.bmiHeader.biHeight = 1;
1210     bitmapInfo.bmiHeader.biBitCount = 32;
1211     bitmapInfo.bmiHeader.biPlanes = 1;
1212     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1213     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1214     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1215     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1216     if (!bmpDst || !bits)
1217         goto cleanup;
1218     bmpOld = SelectObject(hdcDst, bmpDst);
1219
1220     /* Test null, image only, and mask only drawing */
1221     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, 0, 0x00102030, 0x00102030, 0x00102030, __LINE__);
1222     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, 0, 0x00102030, 0x00102030, 0x00102030, __LINE__);
1223
1224     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_MASK, 0x00FFFFFF, 0x00000000, 0x00000000, __LINE__);
1225     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_MASK, 0x00FFFFFF, 0x00FFFFFF, 0x00FFFFFF, __LINE__);
1226
1227     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, DI_IMAGE, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1228     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, DI_IMAGE, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1229
1230     /* Test normal drawing */
1231     check_DrawIconEx(hdcDst, FALSE, 0x00A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1232     check_DrawIconEx(hdcDst, TRUE, 0x00A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x003F4F5F, 0x003F4F5F, __LINE__);
1233     check_DrawIconEx(hdcDst, FALSE, 0xFFA0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1234
1235     /* Test alpha blending */
1236     /* Windows 2000 and up will alpha blend, earlier Windows versions will not */
1237     check_DrawIconEx(hdcDst, TRUE, 0xFFA0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x003F4F5F, __LINE__);
1238
1239     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_NORMAL, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1240     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_NORMAL, 0x00000000, 0x00605850, 0x00C0B0A0, __LINE__);
1241     check_DrawIconEx(hdcDst, FALSE, 0x80A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00DFD7CF, 0x00C0B0A0, __LINE__);
1242     check_DrawIconEx(hdcDst, TRUE, 0x80A0B0C0, 32, DI_NORMAL, 0x00FFFFFF, 0x00DFD7CF, 0x003F4F5F, __LINE__);
1243
1244     check_DrawIconEx(hdcDst, FALSE, 0x01FFFFFF, 32, DI_NORMAL, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1245     check_DrawIconEx(hdcDst, TRUE, 0x01FFFFFF, 32, DI_NORMAL, 0x00000000, 0x00010101, 0x00FFFFFF, __LINE__);
1246
1247     /* Test detecting of alpha channel */
1248     /* If a single pixel's alpha channel is non-zero, the icon
1249        will be alpha blended, otherwise it will be draw with
1250        and + xor blts. */
1251     check_alpha_draw(hdcDst, TRUE, FALSE, 32, __LINE__);
1252     check_alpha_draw(hdcDst, TRUE, TRUE, 32, __LINE__);
1253
1254 cleanup:
1255     if(bmpOld)
1256         SelectObject(hdcDst, bmpOld);
1257     if(bmpDst)
1258         DeleteObject(bmpDst);
1259     if(hdcDst)
1260         DeleteDC(hdcDst);
1261 }
1262
1263 static void check_DrawState_Size(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, HBRUSH hbr, UINT flags, int line)
1264 {
1265     COLORREF result, background;
1266     BOOL passed[2];
1267     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1268     background = 0x00FFFFFF;
1269     /* Set color of the 2 pixels that will be checked afterwards */
1270     SetPixelV(hdc, 0, 0, background);
1271     SetPixelV(hdc, 2, 2, background);
1272
1273     /* Let DrawState calculate the size of the icon (it's 1x1) */
1274     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 0, 0, (DST_ICON | flags ));
1275
1276     result = GetPixel(hdc, 0, 0);
1277     passed[0] = color_match(result, background);
1278     result = GetPixel(hdc, 2, 2);
1279     passed[0] = passed[0] & color_match(result, background);
1280
1281     /* Check if manually specifying the icon size DOESN'T work */
1282
1283     /* IMPORTANT: For Icons, DrawState wants the size of the source image, not the
1284      *            size in which it should be ultimately drawn. Therefore giving
1285      *            width/height 2x2 if the icon is only 1x1 pixels in size should
1286      *            result in drawing it with size 1x1. The size parameters must be
1287      *            ignored if a Icon has to be drawn! */
1288     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 2, 2, (DST_ICON | flags ));
1289
1290     result = GetPixel(hdc, 0, 0);
1291     passed[1] = color_match(result, background);
1292     result = GetPixel(hdc, 2, 2);
1293     passed[1] = passed[0] & color_match(result, background);
1294
1295     if(!passed[0]&&!passed[1])
1296         ok (passed[1],
1297         "DrawState failed to draw a 1x1 Icon in the correct size, independent of the "
1298         "width and height settings passed to it, for Icon with: Overlaying Mask %d on "
1299         "Color %06X with flags %08X. Line %d\n",
1300         maskvalue, color, (DST_ICON | flags), line);
1301     else if(!passed[1])
1302         ok (passed[1],
1303         "DrawState failed to draw a 1x1 Icon in the correct size, if the width and height "
1304         "parameters passed to it are bigger than the real Icon size, for Icon with: Overlaying "
1305         "Mask %d on Color %06X with flags %08X. Line %d\n",
1306         maskvalue, color, (DST_ICON | flags), line);
1307     else
1308         ok (passed[0],
1309         "DrawState failed to draw a 1x1 Icon in the correct size, if the width and height "
1310         "parameters passed to it are 0, for Icon with: Overlaying Mask %d on "
1311         "Color %06X with flags %08X. Line %d\n",
1312         maskvalue, color, (DST_ICON | flags), line);
1313 }
1314
1315 static void check_DrawState_Color(HDC hdc, BOOL maskvalue, UINT32 color, int bpp, HBRUSH hbr, UINT flags,
1316                              COLORREF background, COLORREF modern_expected, COLORREF legacy_expected, int line)
1317 {
1318     COLORREF result;
1319     HICON hicon = create_test_icon(hdc, 1, 1, bpp, maskvalue, &color, sizeof(color));
1320     if (!hicon) return;
1321     /* Set color of the pixel that will be checked afterwards */
1322     SetPixelV(hdc, 1, 1, background);
1323
1324     DrawState(hdc, hbr, NULL, (LPARAM) hicon, 0, 1, 1, 0, 0, ( DST_ICON | flags ));
1325
1326     /* Check the color of the pixel is correct */
1327     result = GetPixel(hdc, 1, 1);
1328
1329     ok (color_match(result, modern_expected) ||         /* Windows 2000 and up */
1330         broken(color_match(result, legacy_expected)),   /* Windows NT 4.0, 9X and below */
1331         "DrawState drawing Icon with Overlaying Mask %d on Color %06X with flags %08X. "
1332         "Expected a close match to %06X (modern) or %06X (legacy). Got %06X from line %d\n",
1333         maskvalue, color, (DST_ICON | flags), modern_expected, legacy_expected, result, line);
1334 }
1335
1336 static void test_DrawState(void)
1337 {
1338     BITMAPINFO bitmapInfo;
1339     HDC hdcDst = NULL;
1340     HBITMAP bmpDst = NULL;
1341     HBITMAP bmpOld = NULL;
1342     void *bits = 0;
1343
1344     hdcDst = CreateCompatibleDC(0);
1345     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1346     if (!hdcDst)
1347         return;
1348
1349     if(GetDeviceCaps(hdcDst, BITSPIXEL) <= 8)
1350     {
1351         skip("Windows will distort DrawIconEx colors at 8-bpp and less due to palletizing.\n");
1352         goto cleanup;
1353     }
1354
1355     memset(&bitmapInfo, 0, sizeof(bitmapInfo));
1356     bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1357     bitmapInfo.bmiHeader.biWidth = 3;
1358     bitmapInfo.bmiHeader.biHeight = 3;
1359     bitmapInfo.bmiHeader.biBitCount = 32;
1360     bitmapInfo.bmiHeader.biPlanes = 1;
1361     bitmapInfo.bmiHeader.biCompression = BI_RGB;
1362     bitmapInfo.bmiHeader.biSizeImage = sizeof(UINT32);
1363     bmpDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, &bits, NULL, 0);
1364     ok (bmpDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1365     if (!bmpDst || !bits)
1366         goto cleanup;
1367     bmpOld = SelectObject(hdcDst, bmpDst);
1368
1369     /* potential flags to test with DrawState are: */
1370     /* DSS_DISABLED embosses the icon */
1371     /* DSS_MONO draw Icon using a brush as parameter 5 */
1372     /* DSS_NORMAL draw Icon without any modifications */
1373     /* DSS_UNION draw the Icon dithered */
1374
1375     check_DrawState_Size(hdcDst, FALSE, 0x00A0B0C0, 32, 0, DSS_NORMAL, __LINE__);
1376     check_DrawState_Color(hdcDst, FALSE, 0x00A0B0C0, 32, 0, DSS_NORMAL, 0x00FFFFFF, 0x00C0B0A0, 0x00C0B0A0, __LINE__);
1377
1378 cleanup:
1379     if(bmpOld)
1380         SelectObject(hdcDst, bmpOld);
1381     if(bmpDst)
1382         DeleteObject(bmpDst);
1383     if(hdcDst)
1384         DeleteDC(hdcDst);
1385 }
1386
1387 static DWORD parent_id;
1388
1389 static DWORD CALLBACK set_cursor_thread( void *arg )
1390 {
1391     HCURSOR ret;
1392
1393     PeekMessage( 0, 0, 0, 0, PM_NOREMOVE );  /* create a msg queue */
1394     if (parent_id)
1395     {
1396         BOOL ret = AttachThreadInput( GetCurrentThreadId(), parent_id, TRUE );
1397         ok( ret, "AttachThreadInput failed\n" );
1398     }
1399     if (arg) ret = SetCursor( (HCURSOR)arg );
1400     else ret = GetCursor();
1401     return (DWORD_PTR)ret;
1402 }
1403
1404 static void test_SetCursor(void)
1405 {
1406     static const BYTE bmp_bits[4096];
1407     ICONINFO cursorInfo;
1408     HCURSOR cursor, old_cursor, global_cursor = 0;
1409     DWORD error, id, result;
1410     UINT display_bpp;
1411     HDC hdc;
1412     HANDLE thread;
1413     CURSORINFO info;
1414
1415     if (pGetCursorInfo)
1416     {
1417         memset( &info, 0, sizeof(info) );
1418         info.cbSize = sizeof(info);
1419         if (!pGetCursorInfo( &info ))
1420         {
1421             win_skip( "GetCursorInfo not working\n" );
1422             pGetCursorInfo = NULL;
1423         }
1424         else global_cursor = info.hCursor;
1425     }
1426     cursor = GetCursor();
1427     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
1428     WaitForSingleObject( thread, 1000 );
1429     GetExitCodeThread( thread, &result );
1430     ok( result == (DWORD_PTR)cursor, "wrong thread cursor %x/%p\n", result, cursor );
1431
1432     hdc = GetDC(0);
1433     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
1434     ReleaseDC(0, hdc);
1435
1436     cursorInfo.fIcon = FALSE;
1437     cursorInfo.xHotspot = 0;
1438     cursorInfo.yHotspot = 0;
1439     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
1440     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
1441
1442     cursor = CreateIconIndirect(&cursorInfo);
1443     ok(cursor != NULL, "CreateIconIndirect returned %p\n", cursor);
1444     old_cursor = SetCursor( cursor );
1445
1446     if (pGetCursorInfo)
1447     {
1448         info.cbSize = sizeof(info);
1449         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1450         /* global cursor doesn't change since we don't have a window */
1451         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
1452             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
1453     }
1454     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
1455     WaitForSingleObject( thread, 1000 );
1456     GetExitCodeThread( thread, &result );
1457     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1458
1459     SetCursor( 0 );
1460     ok( GetCursor() == 0, "wrong cursor %p\n", GetCursor() );
1461     thread = CreateThread( NULL, 0, set_cursor_thread, 0, 0, &id );
1462     WaitForSingleObject( thread, 1000 );
1463     GetExitCodeThread( thread, &result );
1464     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1465
1466     thread = CreateThread( NULL, 0, set_cursor_thread, cursor, 0, &id );
1467     WaitForSingleObject( thread, 1000 );
1468     GetExitCodeThread( thread, &result );
1469     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1470     ok( GetCursor() == 0, "wrong cursor %p/0\n", GetCursor() );
1471
1472     parent_id = GetCurrentThreadId();
1473     thread = CreateThread( NULL, 0, set_cursor_thread, cursor, 0, &id );
1474     WaitForSingleObject( thread, 1000 );
1475     GetExitCodeThread( thread, &result );
1476     ok( result == (DWORD_PTR)old_cursor, "wrong thread cursor %x/%p\n", result, old_cursor );
1477     ok( GetCursor() == cursor, "wrong cursor %p/0\n", cursor );
1478
1479     if (pGetCursorInfo)
1480     {
1481         info.cbSize = sizeof(info);
1482         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1483         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
1484             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
1485     }
1486     SetCursor( old_cursor );
1487     DestroyCursor( cursor );
1488
1489     SetLastError( 0xdeadbeef );
1490     cursor = SetCursor( (HCURSOR)0xbadbad );
1491     error = GetLastError();
1492     ok( cursor == 0, "wrong cursor %p/0\n", cursor );
1493     ok( error == ERROR_INVALID_CURSOR_HANDLE || broken( error == 0xdeadbeef ),  /* win9x */
1494         "wrong error %u\n", error );
1495
1496     if (pGetCursorInfo)
1497     {
1498         info.cbSize = sizeof(info);
1499         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1500         ok( info.hCursor == global_cursor || broken(info.hCursor != cursor), /* win9x */
1501             "wrong info cursor %p/%p\n", info.hCursor, global_cursor );
1502     }
1503 }
1504
1505 static HANDLE event_start, event_next;
1506
1507 static DWORD CALLBACK show_cursor_thread( void *arg )
1508 {
1509     DWORD count = (DWORD_PTR)arg;
1510     int ret;
1511
1512     PeekMessage( 0, 0, 0, 0, PM_NOREMOVE );  /* create a msg queue */
1513     if (parent_id)
1514     {
1515         BOOL ret = AttachThreadInput( GetCurrentThreadId(), parent_id, TRUE );
1516         ok( ret, "AttachThreadInput failed\n" );
1517     }
1518     if (!count) ret = ShowCursor( FALSE );
1519     else while (count--) ret = ShowCursor( TRUE );
1520     SetEvent( event_start );
1521     WaitForSingleObject( event_next, 2000 );
1522     return ret;
1523 }
1524
1525 static void test_ShowCursor(void)
1526 {
1527     int count;
1528     DWORD id, result;
1529     HANDLE thread;
1530     CURSORINFO info;
1531
1532     if (pGetCursorInfo)
1533     {
1534         memset( &info, 0, sizeof(info) );
1535         info.cbSize = sizeof(info);
1536         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1537         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1538     }
1539
1540     event_start = CreateEvent( NULL, FALSE, FALSE, NULL );
1541     event_next = CreateEvent( NULL, FALSE, FALSE, NULL );
1542
1543     count = ShowCursor( TRUE );
1544     ok( count == 1, "wrong count %d\n", count );
1545     count = ShowCursor( TRUE );
1546     ok( count == 2, "wrong count %d\n", count );
1547     count = ShowCursor( FALSE );
1548     ok( count == 1, "wrong count %d\n", count );
1549     count = ShowCursor( FALSE );
1550     ok( count == 0, "wrong count %d\n", count );
1551     count = ShowCursor( FALSE );
1552     ok( count == -1, "wrong count %d\n", count );
1553     count = ShowCursor( FALSE );
1554     ok( count == -2, "wrong count %d\n", count );
1555
1556     if (pGetCursorInfo)
1557     {
1558         info.cbSize = sizeof(info);
1559         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1560         /* global show count is not affected since we don't have a window */
1561         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1562     }
1563
1564     parent_id = 0;
1565     thread = CreateThread( NULL, 0, show_cursor_thread, NULL, 0, &id );
1566     WaitForSingleObject( event_start, 1000 );
1567     count = ShowCursor( FALSE );
1568     ok( count == -3, "wrong count %d\n", count );
1569     SetEvent( event_next );
1570     WaitForSingleObject( thread, 1000 );
1571     GetExitCodeThread( thread, &result );
1572     ok( result == -1, "wrong thread count %d\n", result );
1573     count = ShowCursor( FALSE );
1574     ok( count == -4, "wrong count %d\n", count );
1575
1576     thread = CreateThread( NULL, 0, show_cursor_thread, (void *)1, 0, &id );
1577     WaitForSingleObject( event_start, 1000 );
1578     count = ShowCursor( TRUE );
1579     ok( count == -3, "wrong count %d\n", count );
1580     SetEvent( event_next );
1581     WaitForSingleObject( thread, 1000 );
1582     GetExitCodeThread( thread, &result );
1583     ok( result == 1, "wrong thread count %d\n", result );
1584     count = ShowCursor( TRUE );
1585     ok( count == -2, "wrong count %d\n", count );
1586
1587     parent_id = GetCurrentThreadId();
1588     thread = CreateThread( NULL, 0, show_cursor_thread, NULL, 0, &id );
1589     WaitForSingleObject( event_start, 1000 );
1590     count = ShowCursor( TRUE );
1591     ok( count == -2, "wrong count %d\n", count );
1592     SetEvent( event_next );
1593     WaitForSingleObject( thread, 1000 );
1594     GetExitCodeThread( thread, &result );
1595     ok( result == -3, "wrong thread count %d\n", result );
1596     count = ShowCursor( FALSE );
1597     ok( count == -2, "wrong count %d\n", count );
1598
1599     thread = CreateThread( NULL, 0, show_cursor_thread, (void *)3, 0, &id );
1600     WaitForSingleObject( event_start, 1000 );
1601     count = ShowCursor( TRUE );
1602     ok( count == 2, "wrong count %d\n", count );
1603     SetEvent( event_next );
1604     WaitForSingleObject( thread, 1000 );
1605     GetExitCodeThread( thread, &result );
1606     ok( result == 1, "wrong thread count %d\n", result );
1607     count = ShowCursor( FALSE );
1608     ok( count == -2, "wrong count %d\n", count );
1609
1610     if (pGetCursorInfo)
1611     {
1612         info.cbSize = sizeof(info);
1613         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1614         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1615     }
1616
1617     count = ShowCursor( TRUE );
1618     ok( count == -1, "wrong count %d\n", count );
1619     count = ShowCursor( TRUE );
1620     ok( count == 0, "wrong count %d\n", count );
1621
1622     if (pGetCursorInfo)
1623     {
1624         info.cbSize = sizeof(info);
1625         ok( pGetCursorInfo( &info ), "GetCursorInfo failed\n" );
1626         ok( info.flags & CURSOR_SHOWING, "cursor not shown in info\n" );
1627     }
1628 }
1629
1630
1631 static void test_DestroyCursor(void)
1632 {
1633     static const BYTE bmp_bits[4096];
1634     ICONINFO cursorInfo;
1635     HCURSOR cursor, cursor2;
1636     BOOL ret;
1637     DWORD error;
1638     UINT display_bpp;
1639     HDC hdc;
1640
1641     hdc = GetDC(0);
1642     display_bpp = GetDeviceCaps(hdc, BITSPIXEL);
1643     ReleaseDC(0, hdc);
1644
1645     cursorInfo.fIcon = FALSE;
1646     cursorInfo.xHotspot = 0;
1647     cursorInfo.yHotspot = 0;
1648     cursorInfo.hbmMask = CreateBitmap(32, 32, 1, 1, bmp_bits);
1649     cursorInfo.hbmColor = CreateBitmap(32, 32, 1, display_bpp, bmp_bits);
1650
1651     cursor = CreateIconIndirect(&cursorInfo);
1652     ok(cursor != NULL, "CreateIconIndirect returned %p\n", cursor);
1653     if(!cursor) {
1654         return;
1655     }
1656     SetCursor(cursor);
1657
1658     SetLastError(0xdeadbeef);
1659     ret = DestroyCursor(cursor);
1660     ok(!ret || broken(ret)  /* succeeds on win9x */, "DestroyCursor on the active cursor succeeded\n");
1661     error = GetLastError();
1662     ok(error == 0xdeadbeef, "Last error: %u\n", error);
1663     if (!ret)
1664     {
1665         cursor2 = GetCursor();
1666         ok(cursor2 == cursor, "Active was set to %p when trying to destroy it\n", cursor2);
1667         SetCursor(NULL);
1668
1669         /* Trying to destroy the cursor properly fails now with
1670          * ERROR_INVALID_CURSOR_HANDLE.  This happens because we called
1671          * DestroyCursor() 2+ times after calling SetCursor().  The calls to
1672          * GetCursor() and SetCursor(NULL) in between make no difference. */
1673         SetLastError(0xdeadbeef);
1674         ret = DestroyCursor(cursor);
1675         todo_wine ok(!ret, "DestroyCursor succeeded.\n");
1676         error = GetLastError();
1677         ok(error == ERROR_INVALID_CURSOR_HANDLE || error == 0xdeadbeef, /* vista */
1678            "Last error: 0x%08x\n", error);
1679     }
1680
1681     DeleteObject(cursorInfo.hbmMask);
1682     DeleteObject(cursorInfo.hbmColor);
1683
1684     /* Try testing DestroyCursor() now using LoadCursor() cursors. */
1685     cursor = LoadCursor(NULL, IDC_ARROW);
1686
1687     SetLastError(0xdeadbeef);
1688     ret = DestroyCursor(cursor);
1689     ok(ret || broken(!ret) /* fails on win9x */, "DestroyCursor on the active cursor failed.\n");
1690     error = GetLastError();
1691     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
1692
1693     /* Try setting the cursor to a destroyed OEM cursor. */
1694     SetLastError(0xdeadbeef);
1695     SetCursor(cursor);
1696     error = GetLastError();
1697     ok(error == 0xdeadbeef, "Last error: 0x%08x\n", error);
1698
1699     /* Check if LoadCursor() returns the same handle with the same icon. */
1700     cursor2 = LoadCursor(NULL, IDC_ARROW);
1701     ok(cursor2 == cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
1702
1703     /* Check if LoadCursor() returns the same handle with a different icon. */
1704     cursor2 = LoadCursor(NULL, IDC_WAIT);
1705     ok(cursor2 != cursor, "cursor == %p, cursor2 == %p\n", cursor, cursor2);
1706 }
1707
1708 START_TEST(cursoricon)
1709 {
1710     pGetCursorInfo = (void *)GetProcAddress( GetModuleHandleA("user32.dll"), "GetCursorInfo" );
1711     test_argc = winetest_get_mainargs(&test_argv);
1712
1713     if (test_argc >= 3)
1714     {
1715         /* Child process. */
1716         sscanf (test_argv[2], "%x", (unsigned int *) &parent);
1717
1718         ok(parent != NULL, "Parent not found.\n");
1719         if (parent == NULL)
1720             ExitProcess(1);
1721
1722         do_child();
1723         return;
1724     }
1725
1726     test_CopyImage_Bitmap(1);
1727     test_CopyImage_Bitmap(4);
1728     test_CopyImage_Bitmap(8);
1729     test_CopyImage_Bitmap(16);
1730     test_CopyImage_Bitmap(24);
1731     test_CopyImage_Bitmap(32);
1732     test_initial_cursor();
1733     test_CreateIcon();
1734     test_LoadImage();
1735     test_CreateIconFromResource();
1736     test_DrawIcon();
1737     test_DrawIconEx();
1738     test_DrawState();
1739     test_SetCursor();
1740     test_ShowCursor();
1741     test_DestroyCursor();
1742     do_parent();
1743     test_child_process();
1744     finish_child_process();
1745 }