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