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