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