d3d10core: Fixup HRESULT in a bunch of error cases.
[wine] / dlls / comctl32 / tests / imagelist.c
1 /*
2  * Unit test suite for imagelist control.
3  *
4  * Copyright 2004 Michael Stefaniuc
5  * Copyright 2002 Mike McCormack for CodeWeavers
6  * Copyright 2007 Dmitry Timoshkov
7  * Copyright 2009 Owen Rudge for CodeWeavers
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #define COBJMACROS
25 #define CONST_VTABLE
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <assert.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "objbase.h"
36 #include "commctrl.h" /* must be included after objbase.h to get ImageList_Write */
37 #include "initguid.h"
38 #include "commoncontrols.h"
39 #include "shellapi.h"
40
41 #include "wine/test.h"
42 #include "v6util.h"
43
44 #undef VISIBLE
45
46 #ifdef VISIBLE
47 #define WAIT Sleep (1000)
48 #define REDRAW(hwnd) RedrawWindow (hwnd, NULL, 0, RDW_UPDATENOW)
49 #else
50 #define WAIT
51 #define REDRAW(hwnd)
52 #endif
53
54 #define IMAGELIST_MAGIC (('L' << 8) | 'I')
55
56 #include "pshpack2.h"
57 /* Header used by ImageList_Read() and ImageList_Write() */
58 typedef struct _ILHEAD
59 {
60     USHORT      usMagic;
61     USHORT      usVersion;
62     WORD        cCurImage;
63     WORD        cMaxImage;
64     WORD        cGrow;
65     WORD        cx;
66     WORD        cy;
67     COLORREF    bkcolor;
68     WORD        flags;
69     SHORT       ovls[4];
70 } ILHEAD;
71 #include "poppack.h"
72
73 static HIMAGELIST (WINAPI *pImageList_Create)(int, int, UINT, int, int);
74 static int (WINAPI *pImageList_Add)(HIMAGELIST, HBITMAP, HBITMAP);
75 static BOOL (WINAPI *pImageList_DrawIndirect)(IMAGELISTDRAWPARAMS*);
76 static BOOL (WINAPI *pImageList_SetImageCount)(HIMAGELIST,UINT);
77 static HRESULT (WINAPI *pImageList_CoCreateInstance)(REFCLSID,const IUnknown *,
78     REFIID,void **);
79 static HRESULT (WINAPI *pHIMAGELIST_QueryInterface)(HIMAGELIST,REFIID,void **);
80
81 static HINSTANCE hinst;
82
83 /* These macros build cursor/bitmap data in 4x4 pixel blocks */
84 #define B(x,y) ((x?0xf0:0)|(y?0xf:0))
85 #define ROW1(a,b,c,d,e,f,g,h) B(a,b),B(c,d),B(e,f),B(g,h)
86 #define ROW32(a,b,c,d,e,f,g,h) ROW1(a,b,c,d,e,f,g,h), ROW1(a,b,c,d,e,f,g,h), \
87   ROW1(a,b,c,d,e,f,g,h), ROW1(a,b,c,d,e,f,g,h)
88 #define ROW2(a,b,c,d,e,f,g,h,i,j,k,l) ROW1(a,b,c,d,e,f,g,h),B(i,j),B(k,l)
89 #define ROW48(a,b,c,d,e,f,g,h,i,j,k,l) ROW2(a,b,c,d,e,f,g,h,i,j,k,l), \
90   ROW2(a,b,c,d,e,f,g,h,i,j,k,l), ROW2(a,b,c,d,e,f,g,h,i,j,k,l), \
91   ROW2(a,b,c,d,e,f,g,h,i,j,k,l)
92
93 static const BYTE empty_bits[48*48/8];
94
95 static const BYTE icon_bits[32*32/8] =
96 {
97   ROW32(0,0,0,0,0,0,0,0),
98   ROW32(0,0,1,1,1,1,0,0),
99   ROW32(0,1,1,1,1,1,1,0),
100   ROW32(0,1,1,0,0,1,1,0),
101   ROW32(0,1,1,0,0,1,1,0),
102   ROW32(0,1,1,1,1,1,1,0),
103   ROW32(0,0,1,1,1,1,0,0),
104   ROW32(0,0,0,0,0,0,0,0)
105 };
106
107 static const BYTE bitmap_bits[48*48/8] =
108 {
109   ROW48(0,0,0,0,0,0,0,0,0,0,0,0),
110   ROW48(0,1,1,1,1,1,1,1,1,1,1,0),
111   ROW48(0,1,1,0,0,0,0,0,0,1,1,0),
112   ROW48(0,1,0,0,0,0,0,0,1,0,1,0),
113   ROW48(0,1,0,0,0,0,0,1,0,0,1,0),
114   ROW48(0,1,0,0,0,0,1,0,0,0,1,0),
115   ROW48(0,1,0,0,0,1,0,0,0,0,1,0),
116   ROW48(0,1,0,0,1,0,0,0,0,0,1,0),
117   ROW48(0,1,0,1,0,0,0,0,0,0,1,0),
118   ROW48(0,1,1,0,0,0,0,0,0,1,1,0),
119   ROW48(0,1,1,1,1,1,1,1,1,1,1,0),
120   ROW48(0,0,0,0,0,0,0,0,0,0,0,0)
121 };
122
123 static HIMAGELIST createImageList(int cx, int cy)
124 {
125     /* Create an ImageList and put an image into it */
126     HIMAGELIST himl = ImageList_Create(cx, cy, ILC_COLOR, 1, 1);
127     HBITMAP hbm = CreateBitmap(48, 48, 1, 1, bitmap_bits);
128     ImageList_Add(himl, hbm, NULL);
129     DeleteObject(hbm);
130     return himl;
131 }
132
133 static HWND create_a_window(void)
134 {
135     char className[] = "bmwnd";
136     char winName[]   = "Test Bitmap";
137     HWND hWnd;
138     static int registered = 0;
139
140     if (!registered)
141     {
142         WNDCLASSA cls;
143
144         cls.style         = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
145         cls.lpfnWndProc   = DefWindowProcA;
146         cls.cbClsExtra    = 0;
147         cls.cbWndExtra    = 0;
148         cls.hInstance     = 0;
149         cls.hIcon         = LoadIconA (0, IDI_APPLICATION);
150         cls.hCursor       = LoadCursorA (0, IDC_ARROW);
151         cls.hbrBackground = GetStockObject (WHITE_BRUSH);
152         cls.lpszMenuName  = 0;
153         cls.lpszClassName = className;
154
155         RegisterClassA (&cls);
156         registered = 1;
157     }
158
159     /* Setup window */
160     hWnd = CreateWindowA (className, winName,
161        WS_OVERLAPPEDWINDOW ,
162        CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
163        0, hinst, 0);
164
165 #ifdef VISIBLE
166     ShowWindow (hWnd, SW_SHOW);
167 #endif
168     REDRAW(hWnd);
169     WAIT;
170
171     return hWnd;
172 }
173
174 static HDC show_image(HWND hwnd, HIMAGELIST himl, int idx, int size,
175                       LPCSTR loc, BOOL clear)
176 {
177     HDC hdc = NULL;
178 #ifdef VISIBLE
179     if (!himl) return NULL;
180
181     SetWindowText(hwnd, loc);
182     hdc = GetDC(hwnd);
183     ImageList_Draw(himl, idx, hdc, 0, 0, ILD_TRANSPARENT);
184
185     REDRAW(hwnd);
186     WAIT;
187
188     if (clear)
189     {
190         BitBlt(hdc, 0, 0, size, size, hdc, size+1, size+1, SRCCOPY);
191         ReleaseDC(hwnd, hdc);
192         hdc = NULL;
193     }
194 #endif /* VISIBLE */
195     return hdc;
196 }
197
198 /* Useful for checking differences */
199 #if 0
200 static void dump_bits(const BYTE *p, const BYTE *q, int size)
201 {
202   int i, j;
203
204   size /= 8;
205
206   for (i = 0; i < size * 2; i++)
207   {
208       printf("|");
209       for (j = 0; j < size; j++)
210           printf("%c%c", p[j] & 0xf0 ? 'X' : ' ', p[j] & 0xf ? 'X' : ' ');
211       printf(" -- ");
212       for (j = 0; j < size; j++)
213           printf("%c%c", q[j] & 0xf0 ? 'X' : ' ', q[j] & 0xf ? 'X' : ' ');
214       printf("|\n");
215       p += size * 4;
216       q += size * 4;
217   }
218   printf("\n");
219 }
220 #endif
221
222 static void check_bits(HWND hwnd, HIMAGELIST himl, int idx, int size,
223                        const BYTE *checkbits, LPCSTR loc)
224 {
225 #ifdef VISIBLE
226     BYTE bits[100*100/8];
227     COLORREF c;
228     HDC hdc;
229     int x, y, i = -1;
230
231     if (!himl) return;
232
233     memset(bits, 0, sizeof(bits));
234     hdc = show_image(hwnd, himl, idx, size, loc, FALSE);
235
236     c = GetPixel(hdc, 0, 0);
237
238     for (y = 0; y < size; y ++)
239     {
240         for (x = 0; x < size; x++)
241         {
242             if (!(x & 0x7)) i++;
243             if (GetPixel(hdc, x, y) != c) bits[i] |= (0x80 >> (x & 0x7));
244         }
245     }
246
247     BitBlt(hdc, 0, 0, size, size, hdc, size+1, size+1, SRCCOPY);
248     ReleaseDC(hwnd, hdc);
249
250     ok (memcmp(bits, checkbits, (size * size)/8) == 0,
251         "%s: bits different\n", loc);
252     if (memcmp(bits, checkbits, (size * size)/8))
253         dump_bits(bits, checkbits, size);
254 #endif /* VISIBLE */
255 }
256
257 static void test_hotspot(void)
258 {
259     struct hotspot {
260         int dx;
261         int dy;
262     };
263
264 #define SIZEX1 47
265 #define SIZEY1 31
266 #define SIZEX2 11
267 #define SIZEY2 17
268 #define HOTSPOTS_MAX 4       /* Number of entries in hotspots */
269     static const struct hotspot hotspots[HOTSPOTS_MAX] = {
270         { 10, 7 },
271         { SIZEX1, SIZEY1 },
272         { -9, -8 },
273         { -7, 35 }
274     };
275     int i, j, ret;
276     HIMAGELIST himl1 = createImageList(SIZEX1, SIZEY1);
277     HIMAGELIST himl2 = createImageList(SIZEX2, SIZEY2);
278     HWND hwnd = create_a_window();
279
280
281     for (i = 0; i < HOTSPOTS_MAX; i++) {
282         for (j = 0; j < HOTSPOTS_MAX; j++) {
283             int dx1 = hotspots[i].dx;
284             int dy1 = hotspots[i].dy;
285             int dx2 = hotspots[j].dx;
286             int dy2 = hotspots[j].dy;
287             int correctx, correcty, newx, newy;
288             char loc[256];
289             HIMAGELIST himlNew;
290             POINT ppt;
291
292             ret = ImageList_BeginDrag(himl1, 0, dx1, dy1);
293             ok(ret != 0, "BeginDrag failed for { %d, %d }\n", dx1, dy1);
294             sprintf(loc, "BeginDrag (%d,%d)\n", i, j);
295             show_image(hwnd, himl1, 0, max(SIZEX1, SIZEY1), loc, TRUE);
296
297             /* check merging the dragged image with a second image */
298             ret = ImageList_SetDragCursorImage(himl2, 0, dx2, dy2);
299             ok(ret != 0, "SetDragCursorImage failed for {%d, %d}{%d, %d}\n",
300                     dx1, dy1, dx2, dy2);
301             sprintf(loc, "SetDragCursorImage (%d,%d)\n", i, j);
302             show_image(hwnd, himl2, 0, max(SIZEX2, SIZEY2), loc, TRUE);
303
304             /* check new hotspot, it should be the same like the old one */
305             himlNew = ImageList_GetDragImage(NULL, &ppt);
306             ok(ppt.x == dx1 && ppt.y == dy1,
307                     "Expected drag hotspot [%d,%d] got [%d,%d]\n",
308                     dx1, dy1, ppt.x, ppt.y);
309             /* check size of new dragged image */
310             ImageList_GetIconSize(himlNew, &newx, &newy);
311             correctx = max(SIZEX1, max(SIZEX2 + dx2, SIZEX1 - dx2));
312             correcty = max(SIZEY1, max(SIZEY2 + dy2, SIZEY1 - dy2));
313             ok(newx == correctx && newy == correcty,
314                     "Expected drag image size [%d,%d] got [%d,%d]\n",
315                     correctx, correcty, newx, newy);
316             sprintf(loc, "GetDragImage (%d,%d)\n", i, j);
317             show_image(hwnd, himlNew, 0, max(correctx, correcty), loc, TRUE);
318             ImageList_EndDrag();
319         }
320     }
321 #undef SIZEX1
322 #undef SIZEY1
323 #undef SIZEX2
324 #undef SIZEY2
325 #undef HOTSPOTS_MAX
326     ImageList_Destroy(himl2);
327     ImageList_Destroy(himl1);
328     DestroyWindow(hwnd);
329 }
330
331 static void test_add_remove(void)
332 {
333     HIMAGELIST himl ;
334
335     HICON hicon1 ;
336     HICON hicon2 ;
337     HICON hicon3 ;
338
339     /* create an imagelist to play with */
340     himl = ImageList_Create(84, 84, ILC_COLOR16, 0, 3);
341     ok(himl!=0,"failed to create imagelist\n");
342
343     /* load the icons to add to the image list */
344     hicon1 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
345     ok(hicon1 != 0, "no hicon1\n");
346     hicon2 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
347     ok(hicon2 != 0, "no hicon2\n");
348     hicon3 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
349     ok(hicon3 != 0, "no hicon3\n");
350
351     /* remove when nothing exists */
352     ok(!ImageList_Remove(himl,0),"removed nonexistent icon\n");
353     /* removing everything from an empty imagelist should succeed */
354     ok(ImageList_RemoveAll(himl),"removed nonexistent icon\n");
355
356     /* add three */
357     ok(0==ImageList_AddIcon(himl, hicon1),"failed to add icon1\n");
358     ok(1==ImageList_AddIcon(himl, hicon2),"failed to add icon2\n");
359     ok(2==ImageList_AddIcon(himl, hicon3),"failed to add icon3\n");
360
361     /* remove an index out of range */
362     ok(!ImageList_Remove(himl,4711),"removed nonexistent icon\n");
363
364     /* remove three */
365     ok(ImageList_Remove(himl,0),"can't remove 0\n");
366     ok(ImageList_Remove(himl,0),"can't remove 0\n");
367     ok(ImageList_Remove(himl,0),"can't remove 0\n");
368
369     /* remove one extra */
370     ok(!ImageList_Remove(himl,0),"removed nonexistent icon\n");
371
372     /* destroy it */
373     ok(ImageList_Destroy(himl),"destroy imagelist failed\n");
374
375     ok(-1==ImageList_AddIcon((HIMAGELIST)0xdeadbeef, hicon1),"don't crash on bad handle\n");
376
377     ok(DestroyIcon(hicon1),"icon 1 wasn't deleted\n");
378     ok(DestroyIcon(hicon2),"icon 2 wasn't deleted\n");
379     ok(DestroyIcon(hicon3),"icon 3 wasn't deleted\n");
380 }
381
382 static void test_imagecount(void)
383 {
384     HIMAGELIST himl;
385
386     ok(0==ImageList_GetImageCount((HIMAGELIST)0xdeadbeef),"don't crash on bad handle\n");
387
388     if (!pImageList_SetImageCount)
389     {
390         win_skip("ImageList_SetImageCount not available\n");
391         return;
392     }
393
394     himl = ImageList_Create(84, 84, ILC_COLOR16, 0, 3);
395     ok(himl!=0,"failed to create imagelist\n");
396
397     ok(pImageList_SetImageCount(himl, 3), "couldn't increase image count\n");
398     ok(ImageList_GetImageCount(himl) == 3, "invalid image count after increase\n");
399     ok(pImageList_SetImageCount(himl, 1), "couldn't decrease image count\n");
400     ok(ImageList_GetImageCount(himl) == 1, "invalid image count after decrease to 1\n");
401     ok(pImageList_SetImageCount(himl, 0), "couldn't decrease image count\n");
402     ok(ImageList_GetImageCount(himl) == 0, "invalid image count after decrease to 0\n");
403
404     ok(ImageList_Destroy(himl), "destroy imagelist failed\n");
405 }
406
407 static void test_DrawIndirect(void)
408 {
409     HIMAGELIST himl;
410
411     HBITMAP hbm1;
412     HBITMAP hbm2;
413     HBITMAP hbm3;
414
415     IMAGELISTDRAWPARAMS imldp;
416     HDC hdc;
417     HWND hwndfortest;
418
419     if (!pImageList_DrawIndirect)
420     {
421         win_skip("ImageList_DrawIndirect not available, skipping test\n");
422         return;
423     }
424
425     hwndfortest = create_a_window();
426     hdc = GetDC(hwndfortest);
427     ok(hdc!=NULL, "couldn't get DC\n");
428
429     /* create an imagelist to play with */
430     himl = ImageList_Create(48, 48, ILC_COLOR16, 0, 3);
431     ok(himl!=0,"failed to create imagelist\n");
432
433     /* load the icons to add to the image list */
434     hbm1 = CreateBitmap(48, 48, 1, 1, bitmap_bits);
435     ok(hbm1 != 0, "no bitmap 1\n");
436     hbm2 = CreateBitmap(48, 48, 1, 1, bitmap_bits);
437     ok(hbm2 != 0, "no bitmap 2\n");
438     hbm3 = CreateBitmap(48, 48, 1, 1, bitmap_bits);
439     ok(hbm3 != 0, "no bitmap 3\n");
440
441     /* add three */
442     ok(0==ImageList_Add(himl, hbm1, 0),"failed to add bitmap 1\n");
443     ok(1==ImageList_Add(himl, hbm2, 0),"failed to add bitmap 2\n");
444
445     if (pImageList_SetImageCount)
446     {
447         ok(pImageList_SetImageCount(himl,3),"Setimage count failed\n");
448         /*ok(2==ImageList_Add(himl, hbm3, NULL),"failed to add bitmap 3\n"); */
449         ok(ImageList_Replace(himl, 2, hbm3, 0),"failed to replace bitmap 3\n");
450     }
451
452     memset(&imldp, 0, sizeof (imldp));
453     ok(!pImageList_DrawIndirect(&imldp), "zero data succeeded!\n");
454     imldp.cbSize = IMAGELISTDRAWPARAMS_V3_SIZE;
455     ok(!pImageList_DrawIndirect(&imldp), "zero hdc succeeded!\n");
456     imldp.hdcDst = hdc;
457     ok(!pImageList_DrawIndirect(&imldp),"zero himl succeeded!\n");
458     imldp.himl = (HIMAGELIST)0xdeadbeef;
459     ok(!pImageList_DrawIndirect(&imldp),"bad himl succeeded!\n");
460     imldp.himl = himl;
461
462     REDRAW(hwndfortest);
463     WAIT;
464
465     imldp.fStyle = SRCCOPY;
466     imldp.rgbBk = CLR_DEFAULT;
467     imldp.rgbFg = CLR_DEFAULT;
468     imldp.y = 100;
469     imldp.x = 100;
470     ok(pImageList_DrawIndirect(&imldp),"should succeed\n");
471     imldp.i ++;
472     ok(pImageList_DrawIndirect(&imldp),"should succeed\n");
473     imldp.i ++;
474     ok(pImageList_DrawIndirect(&imldp),"should succeed\n");
475     imldp.i ++;
476     ok(!pImageList_DrawIndirect(&imldp),"should fail\n");
477
478     /* remove three */
479     ok(ImageList_Remove(himl, 0), "removing 1st bitmap\n");
480     ok(ImageList_Remove(himl, 0), "removing 2nd bitmap\n");
481     ok(ImageList_Remove(himl, 0), "removing 3rd bitmap\n");
482
483     /* destroy it */
484     ok(ImageList_Destroy(himl),"destroy imagelist failed\n");
485
486     /* bitmaps should not be deleted by the imagelist */
487     ok(DeleteObject(hbm1),"bitmap 1 can't be deleted\n");
488     ok(DeleteObject(hbm2),"bitmap 2 can't be deleted\n");
489     ok(DeleteObject(hbm3),"bitmap 3 can't be deleted\n");
490
491     ReleaseDC(hwndfortest, hdc);
492     DestroyWindow(hwndfortest);
493 }
494
495 static void test_merge(void)
496 {
497     HIMAGELIST himl1, himl2, hmerge;
498     HICON hicon1;
499     HWND hwnd = create_a_window();
500
501     himl1 = ImageList_Create(32,32,0,0,3);
502     ok(himl1 != NULL,"failed to create himl1\n");
503
504     himl2 = ImageList_Create(32,32,0,0,3);
505     ok(himl2 != NULL,"failed to create himl2\n");
506
507     hicon1 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
508     ok(hicon1 != NULL, "failed to create hicon1\n");
509
510     if (!himl1 || !himl2 || !hicon1)
511         return;
512
513     ok(0==ImageList_AddIcon(himl2, hicon1),"add icon1 to himl2 failed\n");
514     check_bits(hwnd, himl2, 0, 32, icon_bits, "add icon1 to himl2");
515
516     /* If himl1 has no images, merge still succeeds */
517     hmerge = ImageList_Merge(himl1, -1, himl2, 0, 0, 0);
518     ok(hmerge != NULL, "merge himl1,-1 failed\n");
519     check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl1,-1");
520     if (hmerge) ImageList_Destroy(hmerge);
521
522     hmerge = ImageList_Merge(himl1, 0, himl2, 0, 0, 0);
523     ok(hmerge != NULL,"merge himl1,0 failed\n");
524     check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl1,0");
525     if (hmerge) ImageList_Destroy(hmerge);
526
527     /* Same happens if himl2 is empty */
528     ImageList_Destroy(himl2);
529     himl2 = ImageList_Create(32,32,0,0,3);
530     ok(himl2 != NULL,"failed to recreate himl2\n");
531     if (!himl2)
532         return;
533
534     hmerge = ImageList_Merge(himl1, -1, himl2, -1, 0, 0);
535     ok(hmerge != NULL, "merge himl2,-1 failed\n");
536     check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl2,-1");
537     if (hmerge) ImageList_Destroy(hmerge);
538
539     hmerge = ImageList_Merge(himl1, -1, himl2, 0, 0, 0);
540     ok(hmerge != NULL, "merge himl2,0 failed\n");
541     check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl2,0");
542     if (hmerge) ImageList_Destroy(hmerge);
543
544     /* Now try merging an image with itself */
545     ok(0==ImageList_AddIcon(himl2, hicon1),"re-add icon1 to himl2 failed\n");
546
547     hmerge = ImageList_Merge(himl2, 0, himl2, 0, 0, 0);
548     ok(hmerge != NULL, "merge himl2 with itself failed\n");
549     check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl2 with itself");
550     if (hmerge) ImageList_Destroy(hmerge);
551
552     /* Try merging 2 different image lists */
553     ok(0==ImageList_AddIcon(himl1, hicon1),"add icon1 to himl1 failed\n");
554
555     hmerge = ImageList_Merge(himl1, 0, himl2, 0, 0, 0);
556     ok(hmerge != NULL, "merge himl1 with himl2 failed\n");
557     check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl1 with himl2");
558     if (hmerge) ImageList_Destroy(hmerge);
559
560     hmerge = ImageList_Merge(himl1, 0, himl2, 0, 8, 16);
561     ok(hmerge != NULL, "merge himl1 with himl2 8,16 failed\n");
562     check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl1 with himl2, 8,16");
563     if (hmerge) ImageList_Destroy(hmerge);
564
565     ImageList_Destroy(himl1);
566     ImageList_Destroy(himl2);
567     DestroyIcon(hicon1);
568     DestroyWindow(hwnd);
569 }
570
571 /*********************** imagelist storage test ***************************/
572
573 #define BMP_CX 48
574
575 struct my_IStream
576 {
577     IStream is;
578     char *iml_data; /* written imagelist data */
579     ULONG iml_data_size;
580 };
581
582 static HRESULT STDMETHODCALLTYPE Test_Stream_QueryInterface(
583     IStream* This,
584     REFIID riid,
585     void** ppvObject)
586 {
587     assert(0);
588     return E_NOTIMPL;
589 }
590
591 static ULONG STDMETHODCALLTYPE Test_Stream_AddRef(
592     IStream* This)
593 {
594     assert(0);
595     return 2;
596 }
597
598 static ULONG STDMETHODCALLTYPE Test_Stream_Release(
599     IStream* This)
600 {
601     assert(0);
602     return 1;
603 }
604
605 static HRESULT STDMETHODCALLTYPE Test_Stream_Read(
606     IStream* This,
607     void* pv,
608     ULONG cb,
609     ULONG* pcbRead)
610 {
611     assert(0);
612     return E_NOTIMPL;
613 }
614
615 static BOOL allocate_storage(struct my_IStream *my_is, ULONG add)
616 {
617     my_is->iml_data_size += add;
618
619     if (!my_is->iml_data)
620         my_is->iml_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, my_is->iml_data_size);
621     else
622         my_is->iml_data = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, my_is->iml_data, my_is->iml_data_size);
623
624     return my_is->iml_data ? TRUE : FALSE;
625 }
626
627 static HRESULT STDMETHODCALLTYPE Test_Stream_Write(
628     IStream* This,
629     const void* pv,
630     ULONG cb,
631     ULONG* pcbWritten)
632 {
633     struct my_IStream *my_is = (struct my_IStream *)This;
634     ULONG current_iml_data_size = my_is->iml_data_size;
635
636     if (!allocate_storage(my_is, cb)) return E_FAIL;
637
638     memcpy(my_is->iml_data + current_iml_data_size, pv, cb);
639     if (pcbWritten) *pcbWritten = cb;
640
641     return S_OK;
642 }
643
644 static HRESULT STDMETHODCALLTYPE Test_Stream_Seek(
645     IStream* This,
646     LARGE_INTEGER dlibMove,
647     DWORD dwOrigin,
648     ULARGE_INTEGER* plibNewPosition)
649 {
650     assert(0);
651     return E_NOTIMPL;
652 }
653
654 static HRESULT STDMETHODCALLTYPE Test_Stream_SetSize(
655     IStream* This,
656     ULARGE_INTEGER libNewSize)
657 {
658     assert(0);
659     return E_NOTIMPL;
660 }
661
662 static HRESULT STDMETHODCALLTYPE Test_Stream_CopyTo(
663     IStream* This,
664     IStream* pstm,
665     ULARGE_INTEGER cb,
666     ULARGE_INTEGER* pcbRead,
667     ULARGE_INTEGER* pcbWritten)
668 {
669     assert(0);
670     return E_NOTIMPL;
671 }
672
673 static HRESULT STDMETHODCALLTYPE Test_Stream_Commit(
674     IStream* This,
675     DWORD grfCommitFlags)
676 {
677     assert(0);
678     return E_NOTIMPL;
679 }
680
681 static HRESULT STDMETHODCALLTYPE Test_Stream_Revert(
682     IStream* This)
683 {
684     assert(0);
685     return E_NOTIMPL;
686 }
687
688 static HRESULT STDMETHODCALLTYPE Test_Stream_LockRegion(
689     IStream* This,
690     ULARGE_INTEGER libOffset,
691     ULARGE_INTEGER cb,
692     DWORD dwLockType)
693 {
694     assert(0);
695     return E_NOTIMPL;
696 }
697
698 static HRESULT STDMETHODCALLTYPE Test_Stream_UnlockRegion(
699     IStream* This,
700     ULARGE_INTEGER libOffset,
701     ULARGE_INTEGER cb,
702     DWORD dwLockType)
703 {
704     assert(0);
705     return E_NOTIMPL;
706 }
707
708 static HRESULT STDMETHODCALLTYPE Test_Stream_Stat(
709     IStream* This,
710     STATSTG* pstatstg,
711     DWORD grfStatFlag)
712 {
713     assert(0);
714     return E_NOTIMPL;
715 }
716
717 static HRESULT STDMETHODCALLTYPE Test_Stream_Clone(
718     IStream* This,
719     IStream** ppstm)
720 {
721     assert(0);
722     return E_NOTIMPL;
723 }
724
725 static const IStreamVtbl Test_Stream_Vtbl =
726 {
727     Test_Stream_QueryInterface,
728     Test_Stream_AddRef,
729     Test_Stream_Release,
730     Test_Stream_Read,
731     Test_Stream_Write,
732     Test_Stream_Seek,
733     Test_Stream_SetSize,
734     Test_Stream_CopyTo,
735     Test_Stream_Commit,
736     Test_Stream_Revert,
737     Test_Stream_LockRegion,
738     Test_Stream_UnlockRegion,
739     Test_Stream_Stat,
740     Test_Stream_Clone
741 };
742
743 static struct my_IStream Test_Stream = { { &Test_Stream_Vtbl }, 0, 0 };
744
745 static INT DIB_GetWidthBytes( int width, int bpp )
746 {
747     return ((width * bpp + 31) / 8) & ~3;
748 }
749
750 static void check_bitmap_data(const char *bm_data, ULONG bm_data_size,
751                               INT width, INT height, INT bpp,
752                               const char *comment)
753 {
754     const BITMAPFILEHEADER *bmfh = (const BITMAPFILEHEADER *)bm_data;
755     const BITMAPINFOHEADER *bmih = (const BITMAPINFOHEADER *)(bm_data + sizeof(*bmfh));
756     ULONG hdr_size, image_size;
757
758     hdr_size = sizeof(*bmfh) + sizeof(*bmih);
759     if (bmih->biBitCount <= 8) hdr_size += (1 << bpp) * sizeof(RGBQUAD);
760
761     ok(bmfh->bfType == (('M' << 8) | 'B'), "wrong bfType 0x%02x\n", bmfh->bfType);
762     ok(bmfh->bfSize == hdr_size, "wrong bfSize 0x%02x\n", bmfh->bfSize);
763     ok(bmfh->bfReserved1 == 0, "wrong bfReserved1 0x%02x\n", bmfh->bfReserved1);
764     ok(bmfh->bfReserved2 == 0, "wrong bfReserved2 0x%02x\n", bmfh->bfReserved2);
765     ok(bmfh->bfOffBits == hdr_size, "wrong bfOffBits 0x%02x\n", bmfh->bfOffBits);
766
767     ok(bmih->biSize == sizeof(*bmih), "wrong biSize %d\n", bmih->biSize);
768     ok(bmih->biWidth == width, "wrong biWidth %d (expected %d)\n", bmih->biWidth, width);
769     ok(bmih->biHeight == height, "wrong biHeight %d (expected %d)\n", bmih->biHeight, height);
770     ok(bmih->biPlanes == 1, "wrong biPlanes %d\n", bmih->biPlanes);
771     ok(bmih->biBitCount == bpp, "wrong biBitCount %d\n", bmih->biBitCount);
772
773     image_size = DIB_GetWidthBytes(bmih->biWidth, bmih->biBitCount) * bmih->biHeight;
774     ok(bmih->biSizeImage == image_size, "wrong biSizeImage %u\n", bmih->biSizeImage);
775 #if 0
776 {
777     char fname[256];
778     FILE *f;
779     sprintf(fname, "bmp_%s.bmp", comment);
780     f = fopen(fname, "wb");
781     fwrite(bm_data, 1, bm_data_size, f);
782     fclose(f);
783 }
784 #endif
785 }
786
787 static void check_ilhead_data(const char *ilh_data, INT cx, INT cy, INT cur, INT max, INT grow)
788 {
789     const ILHEAD *ilh = (const ILHEAD *)ilh_data;
790
791     ok(ilh->usMagic == IMAGELIST_MAGIC, "wrong usMagic %4x (expected %02x)\n", ilh->usMagic, IMAGELIST_MAGIC);
792     ok(ilh->usVersion == 0x101, "wrong usVersion %x (expected 0x101)\n", ilh->usVersion);
793     ok(ilh->cCurImage == cur, "wrong cCurImage %d (expected %d)\n", ilh->cCurImage, cur);
794     ok(ilh->cMaxImage == max, "wrong cMaxImage %d (expected %d)\n", ilh->cMaxImage, max);
795     ok(ilh->cGrow == grow, "wrong cGrow %d (expected %d)\n", ilh->cGrow, grow);
796     ok(ilh->cx == cx, "wrong cx %d (expected %d)\n", ilh->cx, cx);
797     ok(ilh->cy == cy, "wrong cy %d (expected %d)\n", ilh->cy, cy);
798     ok(ilh->bkcolor == CLR_NONE, "wrong bkcolor %x\n", ilh->bkcolor);
799     ok(ilh->flags == ILC_COLOR24, "wrong flags %04x\n", ilh->flags);
800     ok(ilh->ovls[0] == -1 ||
801        ilh->ovls[0] == 0, /* win95 */
802        "wrong ovls[0] %04x\n", ilh->ovls[0]);
803     ok(ilh->ovls[1] == -1 ||
804        ilh->ovls[1] == 0, /* win95 */
805        "wrong ovls[1] %04x\n", ilh->ovls[1]);
806     ok(ilh->ovls[2] == -1 ||
807        ilh->ovls[2] == 0, /* win95 */
808        "wrong ovls[2] %04x\n", ilh->ovls[2]);
809     ok(ilh->ovls[3] == -1 ||
810        ilh->ovls[3] == 0, /* win95 */
811        "wrong ovls[3] %04x\n", ilh->ovls[3]);
812 }
813
814 static HBITMAP create_bitmap(INT cx, INT cy, COLORREF color, const char *comment)
815 {
816     HDC hdc;
817     char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
818     BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
819     HBITMAP hbmp, hbmp_old;
820     HBRUSH hbrush;
821     RECT rc = { 0, 0, cx, cy };
822
823     hdc = CreateCompatibleDC(0);
824
825     memset(bmi, 0, sizeof(*bmi));
826     bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
827     bmi->bmiHeader.biHeight = cx;
828     bmi->bmiHeader.biWidth = cy;
829     bmi->bmiHeader.biBitCount = 24;
830     bmi->bmiHeader.biPlanes = 1;
831     bmi->bmiHeader.biCompression = BI_RGB;
832     hbmp = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, NULL, 0);
833
834     hbmp_old = SelectObject(hdc, hbmp);
835
836     hbrush = CreateSolidBrush(color);
837     FillRect(hdc, &rc, hbrush);
838     DeleteObject(hbrush);
839
840     DrawText(hdc, comment, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
841
842     SelectObject(hdc, hbmp_old);
843     DeleteDC(hdc);
844
845     return hbmp;
846 }
847
848 #define iml_clear_stream_data() \
849     HeapFree(GetProcessHeap(), 0, Test_Stream.iml_data); \
850     Test_Stream.iml_data = NULL; \
851     Test_Stream.iml_data_size = 0;
852
853 static void check_iml_data(HIMAGELIST himl, INT cx, INT cy, INT cur, INT max, INT grow,
854                            INT width, INT height, INT bpp, const char *comment)
855 {
856     INT ret, cxx, cyy;
857
858     trace("%s\n", comment);
859
860     ret = ImageList_GetImageCount(himl);
861     ok(ret == cur, "expected image count %d got %d\n", cur, ret);
862
863     ret = ImageList_GetIconSize(himl, &cxx, &cyy);
864     ok(ret, "ImageList_GetIconSize failed\n");
865     ok(cxx == cx, "wrong cx %d (expected %d)\n", cxx, cx);
866     ok(cyy == cy, "wrong cy %d (expected %d)\n", cyy, cy);
867
868     iml_clear_stream_data();
869     ret = ImageList_Write(himl, &Test_Stream.is);
870     ok(ret, "ImageList_Write failed\n");
871
872     ok(Test_Stream.iml_data != 0, "ImageList_Write didn't write any data\n");
873     ok(Test_Stream.iml_data_size > sizeof(ILHEAD), "ImageList_Write wrote not enough data\n");
874
875     check_ilhead_data(Test_Stream.iml_data, cx, cy, cur, max, grow);
876     check_bitmap_data(Test_Stream.iml_data + sizeof(ILHEAD),
877                       Test_Stream.iml_data_size - sizeof(ILHEAD),
878                       width, height, bpp, comment);
879 }
880
881 static void image_list_init(HIMAGELIST himl)
882 {
883     HBITMAP hbm;
884     char comment[16];
885     INT n = 1;
886     DWORD i;
887     static const struct test_data
888     {
889         BYTE grey;
890         INT cx, cy, cur, max, grow, width, height, bpp;
891         const char *comment;
892     } td[] =
893     {
894         { 255, BMP_CX, BMP_CX, 1, 2, 4, BMP_CX * 4, BMP_CX * 1, 24, "total 1" },
895         { 170, BMP_CX, BMP_CX, 2, 7, 4, BMP_CX * 4, BMP_CX * 2, 24, "total 2" },
896         { 85, BMP_CX, BMP_CX, 3, 7, 4, BMP_CX * 4, BMP_CX * 2, 24, "total 3" },
897         { 0, BMP_CX, BMP_CX, 4, 7, 4, BMP_CX * 4, BMP_CX * 2, 24, "total 4" },
898         { 0, BMP_CX, BMP_CX, 5, 7, 4, BMP_CX * 4, BMP_CX * 2, 24, "total 5" },
899         { 85, BMP_CX, BMP_CX, 6, 7, 4, BMP_CX * 4, BMP_CX * 2, 24, "total 6" },
900         { 170, BMP_CX, BMP_CX, 7, 12, 4, BMP_CX * 4, BMP_CX * 3, 24, "total 7" },
901         { 255, BMP_CX, BMP_CX, 8, 12, 4, BMP_CX * 4, BMP_CX * 3, 24, "total 8" },
902         { 255, BMP_CX, BMP_CX, 9, 12, 4, BMP_CX * 4, BMP_CX * 3, 24, "total 9" },
903         { 170, BMP_CX, BMP_CX, 10, 12, 4, BMP_CX * 4, BMP_CX * 3, 24, "total 10" },
904         { 85, BMP_CX, BMP_CX, 11, 12, 4, BMP_CX * 4, BMP_CX * 3, 24, "total 11" },
905         { 0, BMP_CX, BMP_CX, 12, 17, 4, BMP_CX * 4, BMP_CX * 5, 24, "total 12" },
906         { 0, BMP_CX, BMP_CX, 13, 17, 4, BMP_CX * 4, BMP_CX * 5, 24, "total 13" },
907         { 85, BMP_CX, BMP_CX, 14, 17, 4, BMP_CX * 4, BMP_CX * 5, 24, "total 14" },
908         { 170, BMP_CX, BMP_CX, 15, 17, 4, BMP_CX * 4, BMP_CX * 5, 24, "total 15" },
909         { 255, BMP_CX, BMP_CX, 16, 17, 4, BMP_CX * 4, BMP_CX * 5, 24, "total 16" },
910         { 255, BMP_CX, BMP_CX, 17, 22, 4, BMP_CX * 4, BMP_CX * 6, 24, "total 17" },
911         { 170, BMP_CX, BMP_CX, 18, 22, 4, BMP_CX * 4, BMP_CX * 6, 24, "total 18" },
912         { 85, BMP_CX, BMP_CX, 19, 22, 4, BMP_CX * 4, BMP_CX * 6, 24, "total 19" },
913         { 0, BMP_CX, BMP_CX, 20, 22, 4, BMP_CX * 4, BMP_CX * 6, 24, "total 20" },
914         { 0, BMP_CX, BMP_CX, 21, 22, 4, BMP_CX * 4, BMP_CX * 6, 24, "total 21" },
915         { 85, BMP_CX, BMP_CX, 22, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "total 22" },
916         { 170, BMP_CX, BMP_CX, 23, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "total 23" },
917         { 255, BMP_CX, BMP_CX, 24, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "total 24" }
918     };
919
920     check_iml_data(himl, BMP_CX, BMP_CX, 0, 2, 4, BMP_CX * 4, BMP_CX * 1, 24, "total 0");
921
922 #define add_bitmap(grey) \
923     sprintf(comment, "%d", n++); \
924     hbm = create_bitmap(BMP_CX, BMP_CX, RGB((grey),(grey),(grey)), comment); \
925     ImageList_Add(himl, hbm, NULL); \
926     DeleteObject(hbm);
927
928     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++)
929     {
930         add_bitmap(td[i].grey);
931         check_iml_data(himl, td[i].cx, td[i].cy, td[i].cur, td[i].max, td[i].grow,
932                        td[i].width, td[i].height, td[i].bpp, td[i].comment);
933     }
934 #undef add_bitmap
935 }
936
937 static void test_imagelist_storage(void)
938 {
939     HIMAGELIST himl;
940     HBITMAP hbm;
941     INT ret;
942
943     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 1, 1);
944     ok(himl != 0, "ImageList_Create failed\n");
945
946     check_iml_data(himl, BMP_CX, BMP_CX, 0, 2, 4, BMP_CX * 4, BMP_CX * 1, 24, "empty");
947
948     image_list_init(himl);
949     check_iml_data(himl, BMP_CX, BMP_CX, 24, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "orig");
950
951     ret = ImageList_Remove(himl, 4);
952     ok(ret, "ImageList_Remove failed\n");
953     check_iml_data(himl, BMP_CX, BMP_CX, 23, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "1");
954
955     ret = ImageList_Remove(himl, 5);
956     ok(ret, "ImageList_Remove failed\n");
957     check_iml_data(himl, BMP_CX, BMP_CX, 22, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "2");
958
959     ret = ImageList_Remove(himl, 6);
960     ok(ret, "ImageList_Remove failed\n");
961     check_iml_data(himl, BMP_CX, BMP_CX, 21, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "3");
962
963     ret = ImageList_Remove(himl, 7);
964     ok(ret, "ImageList_Remove failed\n");
965     check_iml_data(himl, BMP_CX, BMP_CX, 20, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "4");
966
967     ret = ImageList_Remove(himl, -2);
968     ok(!ret, "ImageList_Remove(-2) should fail\n");
969     check_iml_data(himl, BMP_CX, BMP_CX, 20, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "5");
970
971     ret = ImageList_Remove(himl, 20);
972     ok(!ret, "ImageList_Remove(20) should fail\n");
973     check_iml_data(himl, BMP_CX, BMP_CX, 20, 27, 4, BMP_CX * 4, BMP_CX * 7, 24, "6");
974
975     ret = ImageList_Remove(himl, -1);
976     ok(ret, "ImageList_Remove(-1) failed\n");
977     check_iml_data(himl, BMP_CX, BMP_CX, 0, 4, 4, BMP_CX * 4, BMP_CX * 1, 24, "7");
978
979     ret = ImageList_Destroy(himl);
980     ok(ret, "ImageList_Destroy failed\n");
981
982     iml_clear_stream_data();
983
984     /* test ImageList_Create storage allocation */
985
986     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 0, 32);
987     ok(himl != 0, "ImageList_Create failed\n");
988     check_iml_data(himl, BMP_CX, BMP_CX, 0, 1, 32, BMP_CX * 4, BMP_CX * 1, 24, "init 0 grow 32");
989     hbm = create_bitmap(BMP_CX * 9, BMP_CX, 0, "9");
990     ret = ImageList_Add(himl, hbm, NULL);
991     ok(ret == 0, "ImageList_Add returned %d, expected 0\n", ret);
992     check_iml_data(himl, BMP_CX, BMP_CX, 1, 34, 32, BMP_CX * 4, BMP_CX * 9, 24, "add 1 x 9");
993     DeleteObject(hbm);
994     ret = ImageList_Destroy(himl);
995     ok(ret, "ImageList_Destroy failed\n");
996     iml_clear_stream_data();
997
998     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 4, 4);
999     ok(himl != 0, "ImageList_Create failed\n");
1000     check_iml_data(himl, BMP_CX, BMP_CX, 0, 5, 4, BMP_CX * 4, BMP_CX * 2, 24, "init 4 grow 4");
1001     hbm = create_bitmap(BMP_CX, BMP_CX * 9, 0, "9");
1002     ret = ImageList_Add(himl, hbm, NULL);
1003     ok(ret == 0, "ImageList_Add returned %d, expected 0\n", ret);
1004     check_iml_data(himl, BMP_CX, BMP_CX, 9, 15, 4, BMP_CX * 4, BMP_CX * 4, 24, "add 9 x 1");
1005     ret = ImageList_Add(himl, hbm, NULL);
1006     ok(ret == 9, "ImageList_Add returned %d, expected 9\n", ret);
1007     check_iml_data(himl, BMP_CX, BMP_CX, 18, 25, 4, BMP_CX * 4, BMP_CX * 7, 24, "add 9 x 1");
1008     DeleteObject(hbm);
1009     ret = ImageList_Destroy(himl);
1010     ok(ret, "ImageList_Destroy failed\n");
1011     iml_clear_stream_data();
1012
1013     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 207, 209);
1014     ok(himl != 0, "ImageList_Create failed\n");
1015     check_iml_data(himl, BMP_CX, BMP_CX, 0, 208, 212, BMP_CX * 4, BMP_CX * 52, 24, "init 207 grow 209");
1016     ret = ImageList_Destroy(himl);
1017     ok(ret, "ImageList_Destroy failed\n");
1018     iml_clear_stream_data();
1019
1020     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 209, 207);
1021     ok(himl != 0, "ImageList_Create failed\n");
1022     check_iml_data(himl, BMP_CX, BMP_CX, 0, 210, 208, BMP_CX * 4, BMP_CX * 53, 24, "init 209 grow 207");
1023     ret = ImageList_Destroy(himl);
1024     ok(ret, "ImageList_Destroy failed\n");
1025     iml_clear_stream_data();
1026
1027     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 14, 4);
1028     ok(himl != 0, "ImageList_Create failed\n");
1029     check_iml_data(himl, BMP_CX, BMP_CX, 0, 15, 4, BMP_CX * 4, BMP_CX * 4, 24, "init 14 grow 4");
1030     ret = ImageList_Destroy(himl);
1031     ok(ret, "ImageList_Destroy failed\n");
1032     iml_clear_stream_data();
1033
1034     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 5, 9);
1035     ok(himl != 0, "ImageList_Create failed\n");
1036     check_iml_data(himl, BMP_CX, BMP_CX, 0, 6, 12, BMP_CX * 4, BMP_CX * 2, 24, "init 5 grow 9");
1037     ret = ImageList_Destroy(himl);
1038     ok(ret, "ImageList_Destroy failed\n");
1039     iml_clear_stream_data();
1040
1041     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 9, 5);
1042     ok(himl != 0, "ImageList_Create failed\n");
1043     check_iml_data(himl, BMP_CX, BMP_CX, 0, 10, 8, BMP_CX * 4, BMP_CX * 3, 24, "init 9 grow 5");
1044     ret = ImageList_Destroy(himl);
1045     ok(ret, "ImageList_Destroy failed\n");
1046     iml_clear_stream_data();
1047
1048     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 2, 4);
1049     ok(himl != 0, "ImageList_Create failed\n");
1050     check_iml_data(himl, BMP_CX, BMP_CX, 0, 3, 4, BMP_CX * 4, BMP_CX * 1, 24, "init 2 grow 4");
1051     ret = ImageList_Destroy(himl);
1052     ok(ret, "ImageList_Destroy failed\n");
1053     iml_clear_stream_data();
1054
1055     himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 4, 2);
1056     ok(himl != 0, "ImageList_Create failed\n");
1057     check_iml_data(himl, BMP_CX, BMP_CX, 0, 5, 4, BMP_CX * 4, BMP_CX * 2, 24, "init 4 grow 2");
1058     ret = ImageList_Destroy(himl);
1059     ok(ret, "ImageList_Destroy failed\n");
1060     iml_clear_stream_data();
1061 }
1062
1063 static void test_shell_imagelist(void)
1064 {
1065     BOOL (WINAPI *pSHGetImageList)(INT, REFIID, void**);
1066     IImageList *iml = NULL;
1067     HMODULE hShell32;
1068     HRESULT hr;
1069     int out = 0;
1070     RECT rect;
1071     int cx, cy;
1072
1073     /* Try to load function from shell32 */
1074     hShell32 = LoadLibrary("shell32.dll");
1075     pSHGetImageList = (void*)GetProcAddress(hShell32, (LPCSTR) 727);
1076
1077     if (!pSHGetImageList)
1078     {
1079         win_skip("SHGetImageList not available, skipping test\n");
1080         return;
1081     }
1082
1083     /* Get system image list */
1084     hr = (pSHGetImageList)(SHIL_SYSSMALL, &IID_IImageList, (void**)&iml);
1085
1086     ok(SUCCEEDED(hr), "SHGetImageList failed, hr=%x\n", hr);
1087
1088     if (hr != S_OK)
1089         return;
1090
1091     IImageList_GetImageCount(iml, &out);
1092     ok(out > 0, "IImageList_GetImageCount returned out <= 0\n");
1093
1094     /* Fetch the small icon size */
1095     cx = GetSystemMetrics(SM_CXSMICON);
1096     cy = GetSystemMetrics(SM_CYSMICON);
1097
1098     /* Check icon size matches */
1099     IImageList_GetImageRect(iml, 0, &rect);
1100     ok(((rect.right == cx) && (rect.bottom == cy)),
1101                  "IImageList_GetImageRect returned r:%d,b:%d\n",
1102                  rect.right, rect.bottom);
1103
1104     IImageList_Release(iml);
1105     FreeLibrary(hShell32);
1106 }
1107
1108 static HBITMAP create_test_bitmap(HDC hdc, int bpp, UINT32 pixel1, UINT32 pixel2)
1109 {
1110     HBITMAP hBitmap;
1111     UINT32 *buffer = NULL;
1112     BITMAPINFO bitmapInfo = {{sizeof(BITMAPINFOHEADER), 2, 1, 1, bpp, BI_RGB,
1113                                 0, 0, 0, 0, 0}};
1114
1115     hBitmap = CreateDIBSection(hdc, &bitmapInfo, DIB_RGB_COLORS, (void**)&buffer, NULL, 0);
1116     ok(hBitmap != NULL && buffer != NULL, "CreateDIBSection failed.\n");
1117
1118     if(!hBitmap || !buffer)
1119     {
1120         DeleteObject(hBitmap);
1121         return NULL;
1122     }
1123
1124     buffer[0] = pixel1;
1125     buffer[1] = pixel2;
1126
1127     return hBitmap;
1128 }
1129
1130 static BOOL colour_match(UINT32 x, UINT32 y)
1131 {
1132     const INT32 tolerance = 8;
1133
1134     const INT32 dr = abs((INT32)(x & 0x000000FF) - (INT32)(y & 0x000000FF));
1135     const INT32 dg = abs((INT32)((x & 0x0000FF00) >> 8) - (INT32)((y & 0x0000FF00) >> 8));
1136     const INT32 db = abs((INT32)((x & 0x00FF0000) >> 16) - (INT32)((y & 0x00FF0000) >> 16));
1137
1138     return (dr <= tolerance && dg <= tolerance && db <= tolerance);
1139 }
1140
1141 static void check_ImageList_DrawIndirect(IMAGELISTDRAWPARAMS *ildp, UINT32 *bits,
1142                                          UINT32 expected, int line)
1143 {
1144     bits[0] = 0x00FFFFFF;
1145     pImageList_DrawIndirect(ildp);
1146     ok(colour_match(bits[0], expected),
1147        "ImageList_DrawIndirect: Pixel %08X, Expected a close match to %08X from line %d\n",
1148        bits[0] & 0x00FFFFFF, expected, line);
1149 }
1150
1151
1152 static void check_ImageList_DrawIndirect_fStyle(HDC hdc, HIMAGELIST himl, UINT32 *bits, int i,
1153                                                 UINT fStyle, UINT32 expected, int line)
1154 {
1155     IMAGELISTDRAWPARAMS ildp = {sizeof(IMAGELISTDRAWPARAMS), himl, i, hdc,
1156         0, 0, 0, 0, 0, 0, CLR_NONE, CLR_NONE, fStyle, 0, ILS_NORMAL, 0, 0x00000000};
1157     check_ImageList_DrawIndirect(&ildp, bits, expected, line);
1158 }
1159
1160 static void check_ImageList_DrawIndirect_ILD_ROP(HDC hdc, HIMAGELIST himl, UINT32 *bits, int i,
1161                                                 DWORD dwRop, UINT32 expected, int line)
1162 {
1163     IMAGELISTDRAWPARAMS ildp = {sizeof(IMAGELISTDRAWPARAMS), himl, i, hdc,
1164         0, 0, 0, 0, 0, 0, CLR_NONE, CLR_NONE, ILD_IMAGE | ILD_ROP, dwRop, ILS_NORMAL, 0, 0x00000000};
1165     check_ImageList_DrawIndirect(&ildp, bits, expected, line);
1166 }
1167
1168 static void check_ImageList_DrawIndirect_fState(HDC hdc, HIMAGELIST himl, UINT32 *bits, int i, UINT fStyle,
1169                                                 UINT fState, DWORD Frame, UINT32 expected, int line)
1170 {
1171     IMAGELISTDRAWPARAMS ildp = {sizeof(IMAGELISTDRAWPARAMS), himl, i, hdc,
1172         0, 0, 0, 0, 0, 0, CLR_NONE, CLR_NONE, fStyle, 0, fState, Frame, 0x00000000};
1173     check_ImageList_DrawIndirect(&ildp, bits, expected, line);
1174 }
1175
1176 static void check_ImageList_DrawIndirect_broken(HDC hdc, HIMAGELIST himl, UINT32 *bits, int i,
1177                                                 UINT fStyle, UINT fState, DWORD Frame, UINT32 expected,
1178                                                 UINT32 broken_expected, int line)
1179 {
1180     IMAGELISTDRAWPARAMS ildp = {sizeof(IMAGELISTDRAWPARAMS), himl, i, hdc,
1181         0, 0, 0, 0, 0, 0, CLR_NONE, CLR_NONE, fStyle, 0, fState, Frame, 0x00000000};
1182     bits[0] = 0x00FFFFFF;
1183     pImageList_DrawIndirect(&ildp);
1184     ok(colour_match(bits[0], expected) ||
1185        broken(colour_match(bits[0], broken_expected)),
1186        "ImageList_DrawIndirect: Pixel %08X, Expected a close match to %08X from line %d\n",
1187        bits[0] & 0x00FFFFFF, expected, line);
1188 }
1189
1190 static void test_ImageList_DrawIndirect(void)
1191 {
1192     HIMAGELIST himl = NULL;
1193     int ret;
1194     HDC hdcDst = NULL;
1195     HBITMAP hbmOld = NULL, hbmDst = NULL;
1196     HBITMAP hbmMask = NULL, hbmInverseMask = NULL;
1197     HBITMAP hbmImage = NULL, hbmAlphaImage = NULL, hbmTransparentImage = NULL;
1198     int iImage = -1, iAlphaImage = -1, iTransparentImage = -1;
1199     UINT32 *bits = 0;
1200     UINT32 maskBits = 0x00000000, inverseMaskBits = 0xFFFFFFFF;
1201     int bpp, broken_value;
1202
1203     BITMAPINFO bitmapInfo = {{sizeof(BITMAPINFOHEADER), 2, 1, 1, 32, BI_RGB,
1204                                 0, 0, 0, 0, 0}};
1205
1206     hdcDst = CreateCompatibleDC(0);
1207     ok(hdcDst != 0, "CreateCompatibleDC(0) failed to return a valid DC\n");
1208     if (!hdcDst)
1209         return;
1210     bpp = GetDeviceCaps(hdcDst, BITSPIXEL);
1211
1212     hbmMask = CreateBitmap(2, 1, 1, 1, &maskBits);
1213     ok(hbmMask != 0, "CreateBitmap failed\n");
1214     if(!hbmMask) goto cleanup;
1215
1216     hbmInverseMask = CreateBitmap(2, 1, 1, 1, &inverseMaskBits);
1217     ok(hbmInverseMask != 0, "CreateBitmap failed\n");
1218     if(!hbmInverseMask) goto cleanup;
1219
1220     himl = pImageList_Create(2, 1, ILC_COLOR32, 0, 1);
1221     ok(himl != 0, "ImageList_Create failed\n");
1222     if(!himl) goto cleanup;
1223
1224     /* Add a no-alpha image */
1225     hbmImage = create_test_bitmap(hdcDst, 32, 0x00ABCDEF, 0x00ABCDEF);
1226     if(!hbmImage) goto cleanup;
1227
1228     iImage = pImageList_Add(himl, hbmImage, hbmMask);
1229     ok(iImage != -1, "ImageList_Add failed\n");
1230     if(iImage == -1) goto cleanup;
1231
1232     /* Add an alpha image */
1233     hbmAlphaImage = create_test_bitmap(hdcDst, 32, 0x89ABCDEF, 0x89ABCDEF);
1234     if(!hbmAlphaImage) goto cleanup;
1235
1236     iAlphaImage = pImageList_Add(himl, hbmAlphaImage, hbmMask);
1237     ok(iAlphaImage != -1, "ImageList_Add failed\n");
1238     if(iAlphaImage == -1) goto cleanup;
1239
1240     /* Add a transparent alpha image */
1241     hbmTransparentImage = create_test_bitmap(hdcDst, 32, 0x00ABCDEF, 0x89ABCDEF);
1242     if(!hbmTransparentImage) goto cleanup;
1243
1244     iTransparentImage = pImageList_Add(himl, hbmTransparentImage, hbmMask);
1245     ok(iTransparentImage != -1, "ImageList_Add failed\n");
1246     if(iTransparentImage == -1) goto cleanup;
1247
1248     /* 32-bit Tests */
1249     bitmapInfo.bmiHeader.biBitCount = 32;
1250     hbmDst = CreateDIBSection(hdcDst, &bitmapInfo, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1251     ok (hbmDst && bits, "CreateDIBSection failed to return a valid bitmap and buffer\n");
1252     if (!hbmDst || !bits)
1253         goto cleanup;
1254     hbmOld = SelectObject(hdcDst, hbmDst);
1255
1256     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iImage, ILD_NORMAL, 0x00ABCDEF, __LINE__);
1257     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iImage, ILD_TRANSPARENT, 0x00ABCDEF, __LINE__);
1258     todo_wine check_ImageList_DrawIndirect_broken(hdcDst, himl, bits, iAlphaImage, ILD_BLEND25, ILS_NORMAL, 0, 0x00E8F1FA, 0x00D4D9DD, __LINE__);
1259     if (bpp == 16 || bpp == 24) broken_value = 0x00D4D9DD;
1260     else broken_value = 0x00B4BDC4;
1261     todo_wine check_ImageList_DrawIndirect_broken(hdcDst, himl, bits, iAlphaImage, ILD_BLEND50, ILS_NORMAL, 0, 0x00E8F1FA, broken_value, __LINE__);
1262     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iImage, ILD_MASK, 0x00ABCDEF, __LINE__);
1263     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iImage, ILD_IMAGE, 0x00ABCDEF, __LINE__);
1264     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iImage, ILD_PRESERVEALPHA, 0x00ABCDEF, __LINE__);
1265
1266     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iAlphaImage, ILD_NORMAL, 0x00D3E5F7, __LINE__);
1267     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iAlphaImage, ILD_TRANSPARENT, 0x00D3E5F7, __LINE__);
1268
1269     if (bpp == 16 || bpp == 24) broken_value = 0x00D4D9DD;
1270     else broken_value =  0x009DA8B1;
1271     todo_wine check_ImageList_DrawIndirect_broken(hdcDst, himl, bits, iAlphaImage, ILD_BLEND25, ILS_NORMAL, 0, 0x00E8F1FA, broken_value, __LINE__);
1272     if (bpp == 16 || bpp == 24) broken_value = 0x00D4D9DD;
1273     else broken_value =  0x008C99A3;
1274     todo_wine check_ImageList_DrawIndirect_broken(hdcDst, himl, bits, iAlphaImage, ILD_BLEND50, ILS_NORMAL, 0, 0x00E8F1FA, broken_value, __LINE__);
1275     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iAlphaImage, ILD_MASK, 0x00D3E5F7, __LINE__);
1276     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iAlphaImage, ILD_IMAGE, 0x00D3E5F7, __LINE__);
1277     todo_wine check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iAlphaImage, ILD_PRESERVEALPHA, 0x005D6F81, __LINE__);
1278
1279     check_ImageList_DrawIndirect_fStyle(hdcDst, himl, bits, iTransparentImage, ILD_NORMAL, 0x00FFFFFF, __LINE__);
1280
1281     check_ImageList_DrawIndirect_ILD_ROP(hdcDst, himl, bits, iImage, SRCCOPY, 0x00ABCDEF, __LINE__);
1282     check_ImageList_DrawIndirect_ILD_ROP(hdcDst, himl, bits, iImage, SRCINVERT, 0x00543210, __LINE__);
1283
1284     /* ILD_ROP is ignored when the image has an alpha channel */
1285     check_ImageList_DrawIndirect_ILD_ROP(hdcDst, himl, bits, iAlphaImage, SRCCOPY, 0x00D3E5F7, __LINE__);
1286     check_ImageList_DrawIndirect_ILD_ROP(hdcDst, himl, bits, iAlphaImage, SRCINVERT, 0x00D3E5F7, __LINE__);
1287
1288     todo_wine check_ImageList_DrawIndirect_fState(hdcDst, himl, bits, iImage, ILD_NORMAL, ILS_SATURATE, 0, 0x00CCCCCC, __LINE__);
1289     todo_wine check_ImageList_DrawIndirect_broken(hdcDst, himl, bits, iAlphaImage, ILD_NORMAL, ILS_SATURATE, 0, 0x00AFAFAF, 0x00F0F0F0, __LINE__);
1290
1291     check_ImageList_DrawIndirect_fState(hdcDst, himl, bits, iImage, ILD_NORMAL, ILS_GLOW, 0, 0x00ABCDEF, __LINE__);
1292     check_ImageList_DrawIndirect_fState(hdcDst, himl, bits, iImage, ILD_NORMAL, ILS_SHADOW, 0, 0x00ABCDEF, __LINE__);
1293
1294     check_ImageList_DrawIndirect_fState(hdcDst, himl, bits, iImage, ILD_NORMAL, ILS_ALPHA, 127, 0x00D5E6F7, __LINE__);
1295     check_ImageList_DrawIndirect_broken(hdcDst, himl, bits, iAlphaImage, ILD_NORMAL, ILS_ALPHA, 127, 0x00E9F2FB, 0x00AEB7C0, __LINE__);
1296     todo_wine check_ImageList_DrawIndirect_broken(hdcDst, himl, bits, iAlphaImage, ILD_NORMAL, ILS_NORMAL, 127, 0x00E9F2FB, 0x00D3E5F7, __LINE__);
1297
1298 cleanup:
1299
1300     if(hbmOld)
1301         SelectObject(hdcDst, hbmOld);
1302     if(hbmDst)
1303         DeleteObject(hbmDst);
1304
1305     if(hdcDst)
1306         DeleteDC(hdcDst);
1307
1308     if(hbmMask)
1309         DeleteObject(hbmMask);
1310     if(hbmInverseMask)
1311         DeleteObject(hbmInverseMask);
1312
1313     if(hbmImage)
1314         DeleteObject(hbmImage);
1315     if(hbmAlphaImage)
1316         DeleteObject(hbmAlphaImage);
1317     if(hbmTransparentImage)
1318         DeleteObject(hbmTransparentImage);
1319
1320     if(himl)
1321     {
1322         ret = ImageList_Destroy(himl);
1323         ok(ret, "ImageList_Destroy failed\n");
1324     }
1325 }
1326
1327 static void test_iimagelist(void)
1328 {
1329     IImageList *imgl, *imgl2;
1330     HIMAGELIST himl;
1331     HRESULT hr;
1332     ULONG ret;
1333
1334     if (!pHIMAGELIST_QueryInterface)
1335     {
1336         win_skip("XP imagelist functions not available\n");
1337         return;
1338     }
1339
1340     /* test reference counting on destruction */
1341     imgl = (IImageList*)createImageList(32, 32);
1342     ret = IUnknown_AddRef(imgl);
1343     ok(ret == 2, "Expected 2, got %d\n", ret);
1344     ret = ImageList_Destroy((HIMAGELIST)imgl);
1345     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1346     ret = ImageList_Destroy((HIMAGELIST)imgl);
1347     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1348     ret = ImageList_Destroy((HIMAGELIST)imgl);
1349     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
1350
1351     imgl = (IImageList*)createImageList(32, 32);
1352     ret = IUnknown_AddRef(imgl);
1353     ok(ret == 2, "Expected 2, got %d\n", ret);
1354     ret = ImageList_Destroy((HIMAGELIST)imgl);
1355     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1356     ret = IImageList_Release(imgl);
1357     ok(ret == 0, "Expected 0, got %d\n", ret);
1358     ret = ImageList_Destroy((HIMAGELIST)imgl);
1359     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
1360
1361     /* ref counting, HIMAGELIST_QueryInterface adds a reference */
1362     imgl = (IImageList*)createImageList(32, 32);
1363     hr = pHIMAGELIST_QueryInterface((HIMAGELIST)imgl, &IID_IImageList, (void**)&imgl2);
1364     ok(hr == S_OK, "got 0x%08x\n", hr);
1365     ok(imgl2 == imgl, "got different pointer\n");
1366     ret = IImageList_Release(imgl);
1367     ok(ret == 1, "got %u\n", ret);
1368     IImageList_Release(imgl);
1369
1370     if (!pImageList_CoCreateInstance)
1371     {
1372         win_skip("Vista imagelist functions not available\n");
1373         return;
1374     }
1375
1376     hr = pImageList_CoCreateInstance(&CLSID_ImageList, NULL, &IID_IImageList, (void **) &imgl);
1377     ok(SUCCEEDED(hr), "ImageList_CoCreateInstance failed, hr=%x\n", hr);
1378
1379     if (hr == S_OK)
1380         IImageList_Release(imgl);
1381
1382     himl = createImageList(32, 32);
1383
1384     if (!himl)
1385         return;
1386
1387     hr = (pHIMAGELIST_QueryInterface)(himl, &IID_IImageList, (void **) &imgl);
1388     ok(SUCCEEDED(hr), "HIMAGELIST_QueryInterface failed, hr=%x\n", hr);
1389
1390     if (hr == S_OK)
1391         IImageList_Release(imgl);
1392
1393     ImageList_Destroy(himl);
1394 }
1395
1396 static void test_hotspot_v6(void)
1397 {
1398     struct hotspot {
1399         int dx;
1400         int dy;
1401     };
1402
1403 #define SIZEX1 47
1404 #define SIZEY1 31
1405 #define SIZEX2 11
1406 #define SIZEY2 17
1407 #define HOTSPOTS_MAX 4       /* Number of entries in hotspots */
1408     static const struct hotspot hotspots[HOTSPOTS_MAX] = {
1409         { 10, 7 },
1410         { SIZEX1, SIZEY1 },
1411         { -9, -8 },
1412         { -7, 35 }
1413     };
1414     int i, j;
1415     HIMAGELIST himl1 = createImageList(SIZEX1, SIZEY1);
1416     HIMAGELIST himl2 = createImageList(SIZEX2, SIZEY2);
1417     IImageList *imgl1, *imgl2;
1418     HRESULT hr;
1419
1420     /* cast to IImageList */
1421     imgl1 = (IImageList *) himl1;
1422     imgl2 = (IImageList *) himl2;
1423
1424     for (i = 0; i < HOTSPOTS_MAX; i++) {
1425         for (j = 0; j < HOTSPOTS_MAX; j++) {
1426             int dx1 = hotspots[i].dx;
1427             int dy1 = hotspots[i].dy;
1428             int dx2 = hotspots[j].dx;
1429             int dy2 = hotspots[j].dy;
1430             int correctx, correcty, newx, newy;
1431             char loc[256];
1432             IImageList *imglNew;
1433             POINT ppt;
1434
1435             hr = IImageList_BeginDrag(imgl1, 0, dx1, dy1);
1436             ok(SUCCEEDED(hr), "BeginDrag failed for { %d, %d }\n", dx1, dy1);
1437             sprintf(loc, "BeginDrag (%d,%d)\n", i, j);
1438
1439             /* check merging the dragged image with a second image */
1440             hr = IImageList_SetDragCursorImage(imgl2, (IUnknown *) imgl2, 0, dx2, dy2);
1441             ok(SUCCEEDED(hr), "SetDragCursorImage failed for {%d, %d}{%d, %d}\n",
1442                     dx1, dy1, dx2, dy2);
1443             sprintf(loc, "SetDragCursorImage (%d,%d)\n", i, j);
1444
1445             /* check new hotspot, it should be the same like the old one */
1446             hr = IImageList_GetDragImage(imgl2, NULL, &ppt, &IID_IImageList, (PVOID *) &imglNew);
1447             ok(SUCCEEDED(hr), "GetDragImage failed\n");
1448             ok(ppt.x == dx1 && ppt.y == dy1,
1449                     "Expected drag hotspot [%d,%d] got [%d,%d]\n",
1450                     dx1, dy1, ppt.x, ppt.y);
1451             /* check size of new dragged image */
1452             IImageList_GetIconSize(imglNew, &newx, &newy);
1453             correctx = max(SIZEX1, max(SIZEX2 + dx2, SIZEX1 - dx2));
1454             correcty = max(SIZEY1, max(SIZEY2 + dy2, SIZEY1 - dy2));
1455             ok(newx == correctx && newy == correcty,
1456                     "Expected drag image size [%d,%d] got [%d,%d]\n",
1457                     correctx, correcty, newx, newy);
1458             sprintf(loc, "GetDragImage (%d,%d)\n", i, j);
1459             IImageList_EndDrag(imgl2);
1460         }
1461     }
1462 #undef SIZEX1
1463 #undef SIZEY1
1464 #undef SIZEX2
1465 #undef SIZEY2
1466 #undef HOTSPOTS_MAX
1467     IImageList_Release(imgl2);
1468     IImageList_Release(imgl1);
1469 }
1470
1471 static void test_IImageList_Add_Remove(void)
1472 {
1473     IImageList *imgl;
1474     HIMAGELIST himl;
1475     HRESULT hr;
1476
1477     HICON hicon1;
1478     HICON hicon2;
1479     HICON hicon3;
1480
1481     int ret;
1482
1483     /* create an imagelist to play with */
1484     himl = ImageList_Create(84, 84, ILC_COLOR16, 0, 3);
1485     ok(himl != 0,"failed to create imagelist\n");
1486
1487     imgl = (IImageList *) himl;
1488
1489     /* load the icons to add to the image list */
1490     hicon1 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
1491     ok(hicon1 != 0, "no hicon1\n");
1492     hicon2 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
1493     ok(hicon2 != 0, "no hicon2\n");
1494     hicon3 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
1495     ok(hicon3 != 0, "no hicon3\n");
1496
1497     /* remove when nothing exists */
1498     hr = IImageList_Remove(imgl, 0);
1499     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
1500
1501     /* removing everything from an empty imagelist should succeed */
1502     hr = IImageList_Remove(imgl, -1);
1503     ok(hr == S_OK, "removed nonexistent icon\n");
1504
1505     /* add three */
1506     ret = -1;
1507     ok( IImageList_ReplaceIcon(imgl, -1, hicon1, &ret) == S_OK && (ret == 0),"failed to add icon1\n");
1508     ret = -1;
1509     ok( IImageList_ReplaceIcon(imgl, -1, hicon2, &ret) == S_OK && (ret == 1),"failed to add icon2\n");
1510     ret = -1;
1511     ok( IImageList_ReplaceIcon(imgl, -1, hicon3, &ret) == S_OK && (ret == 2),"failed to add icon3\n");
1512
1513     /* remove an index out of range */
1514     ok( IImageList_Remove(imgl, 4711) == E_INVALIDARG, "got 0x%08x\n", hr);
1515
1516     /* remove three */
1517     ok( IImageList_Remove(imgl,0) == S_OK, "can't remove 0\n");
1518     ok( IImageList_Remove(imgl,0) == S_OK, "can't remove 0\n");
1519     ok( IImageList_Remove(imgl,0) == S_OK, "can't remove 0\n");
1520
1521     /* remove one extra */
1522     ok( IImageList_Remove(imgl, 0) == E_INVALIDARG, "got 0x%08x\n", hr);
1523
1524     IImageList_Release(imgl);
1525     ok(DestroyIcon(hicon1),"icon 1 wasn't deleted\n");
1526     ok(DestroyIcon(hicon2),"icon 2 wasn't deleted\n");
1527     ok(DestroyIcon(hicon3),"icon 3 wasn't deleted\n");
1528 }
1529
1530 static void test_IImageList_Get_SetImageCount(void)
1531 {
1532     IImageList *imgl;
1533     HIMAGELIST himl;
1534     HRESULT hr;
1535     INT ret;
1536
1537     /* create an imagelist to play with */
1538     himl = ImageList_Create(84, 84, ILC_COLOR16, 0, 3);
1539     ok(himl != 0,"failed to create imagelist\n");
1540
1541     imgl = (IImageList *) himl;
1542
1543     /* check SetImageCount/GetImageCount */
1544     hr = IImageList_SetImageCount(imgl, 3);
1545     ok(hr == S_OK, "got 0x%08x\n", hr);
1546     ret = 0;
1547     hr = IImageList_GetImageCount(imgl, &ret);
1548     ok(hr == S_OK && ret == 3, "invalid image count after increase\n");
1549     hr = IImageList_SetImageCount(imgl, 1);
1550     ok(hr == S_OK, "got 0x%08x\n", hr);
1551     ret = 0;
1552     hr = IImageList_GetImageCount(imgl, &ret);
1553     ok(hr == S_OK && ret == 1, "invalid image count after decrease to 1\n");
1554     hr = IImageList_SetImageCount(imgl, 0);
1555     ok(hr == S_OK, "got 0x%08x\n", hr);
1556     ret = -1;
1557     hr = IImageList_GetImageCount(imgl, &ret);
1558     ok(hr == S_OK && ret == 0, "invalid image count after decrease to 0\n");
1559
1560     IImageList_Release(imgl);
1561 }
1562
1563 static void test_IImageList_Draw(void)
1564 {
1565     IImageList *imgl;
1566     HIMAGELIST himl;
1567
1568     HBITMAP hbm1;
1569     HBITMAP hbm2;
1570     HBITMAP hbm3;
1571
1572     IMAGELISTDRAWPARAMS imldp;
1573     HWND hwndfortest;
1574     HRESULT hr;
1575     HDC hdc;
1576     int ret;
1577
1578     hwndfortest = create_a_window();
1579     hdc = GetDC(hwndfortest);
1580     ok(hdc!=NULL, "couldn't get DC\n");
1581
1582     /* create an imagelist to play with */
1583     himl = ImageList_Create(48, 48, ILC_COLOR16, 0, 3);
1584     ok(himl!=0,"failed to create imagelist\n");
1585
1586     imgl = (IImageList *) himl;
1587
1588     /* load the icons to add to the image list */
1589     hbm1 = CreateBitmap(48, 48, 1, 1, bitmap_bits);
1590     ok(hbm1 != 0, "no bitmap 1\n");
1591     hbm2 = CreateBitmap(48, 48, 1, 1, bitmap_bits);
1592     ok(hbm2 != 0, "no bitmap 2\n");
1593     hbm3 = CreateBitmap(48, 48, 1, 1, bitmap_bits);
1594     ok(hbm3 != 0, "no bitmap 3\n");
1595
1596     /* add three */
1597     ret = -1;
1598     ok( IImageList_Add(imgl, hbm1, 0, &ret) == S_OK && (ret == 0), "failed to add bitmap 1\n");
1599     ret = -1;
1600     ok( IImageList_Add(imgl, hbm2, 0, &ret) == S_OK && (ret == 1), "failed to add bitmap 2\n");
1601
1602     ok( IImageList_SetImageCount(imgl, 3) == S_OK, "Setimage count failed\n");
1603     ok( IImageList_Replace(imgl, 2, hbm3, 0) == S_OK, "failed to replace bitmap 3\n");
1604
1605 if (0)
1606 {
1607     /* crashes on native */
1608     IImageList_Draw(imgl, NULL);
1609 }
1610
1611     memset(&imldp, 0, sizeof (imldp));
1612     hr = IImageList_Draw(imgl, &imldp);
1613     ok( hr == E_INVALIDARG, "got 0x%08x\n", hr);
1614
1615     imldp.cbSize = IMAGELISTDRAWPARAMS_V3_SIZE;
1616     imldp.hdcDst = hdc;
1617     imldp.himl = himl;
1618
1619     REDRAW(hwndfortest);
1620     WAIT;
1621
1622     imldp.fStyle = SRCCOPY;
1623     imldp.rgbBk = CLR_DEFAULT;
1624     imldp.rgbFg = CLR_DEFAULT;
1625     imldp.y = 100;
1626     imldp.x = 100;
1627     ok( IImageList_Draw(imgl, &imldp) == S_OK, "should succeed\n");
1628     imldp.i ++;
1629     ok( IImageList_Draw(imgl, &imldp) == S_OK, "should succeed\n");
1630     imldp.i ++;
1631     ok( IImageList_Draw(imgl, &imldp) == S_OK, "should succeed\n");
1632     imldp.i ++;
1633     ok( IImageList_Draw(imgl, &imldp) == E_INVALIDARG, "should fail\n");
1634
1635     /* remove three */
1636     ok( IImageList_Remove(imgl, 0) == S_OK, "removing 1st bitmap\n");
1637     ok( IImageList_Remove(imgl, 0) == S_OK, "removing 2nd bitmap\n");
1638     ok( IImageList_Remove(imgl, 0) == S_OK, "removing 3rd bitmap\n");
1639
1640     /* destroy it */
1641     IImageList_Release(imgl);
1642
1643     /* bitmaps should not be deleted by the imagelist */
1644     ok(DeleteObject(hbm1),"bitmap 1 can't be deleted\n");
1645     ok(DeleteObject(hbm2),"bitmap 2 can't be deleted\n");
1646     ok(DeleteObject(hbm3),"bitmap 3 can't be deleted\n");
1647
1648     ReleaseDC(hwndfortest, hdc);
1649     DestroyWindow(hwndfortest);
1650 }
1651
1652 static void test_IImageList_Merge(void)
1653 {
1654     HIMAGELIST himl1, himl2;
1655     IImageList *imgl1, *imgl2, *merge;
1656     HICON hicon1;
1657     HWND hwnd = create_a_window();
1658     HRESULT hr;
1659     int ret;
1660
1661     himl1 = ImageList_Create(32,32,0,0,3);
1662     ok(himl1 != NULL,"failed to create himl1\n");
1663
1664     himl2 = ImageList_Create(32,32,0,0,3);
1665     ok(himl2 != NULL,"failed to create himl2\n");
1666
1667     hicon1 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
1668     ok(hicon1 != NULL, "failed to create hicon1\n");
1669
1670     if (!himl1 || !himl2 || !hicon1)
1671         return;
1672
1673     /* cast to IImageList */
1674     imgl1 = (IImageList *) himl1;
1675     imgl2 = (IImageList *) himl2;
1676
1677     ret = -1;
1678     ok( IImageList_ReplaceIcon(imgl2, -1, hicon1, &ret) == S_OK && (ret == 0),"add icon1 to himl2 failed\n");
1679
1680 if (0)
1681 {
1682     /* null cases that crash on native */
1683     IImageList_Merge(imgl1, -1, NULL, 0, 0, 0, &IID_IImageList, (void**)&merge);
1684     IImageList_Merge(imgl1, -1, (IUnknown*) imgl2, 0, 0, 0, &IID_IImageList, NULL);
1685 }
1686
1687     /* If himl1 has no images, merge still succeeds */
1688     hr = IImageList_Merge(imgl1, -1, (IUnknown *) imgl2, 0, 0, 0, &IID_IImageList, (void **) &merge);
1689     ok(hr == S_OK, "merge himl1,-1 failed\n");
1690     if (hr == S_OK) IImageList_Release(merge);
1691
1692     hr = IImageList_Merge(imgl1, 0, (IUnknown *) imgl2, 0, 0, 0, &IID_IImageList, (void **) &merge);
1693     ok(hr == S_OK, "merge himl1,0 failed\n");
1694     if (hr == S_OK) IImageList_Release(merge);
1695
1696     /* Same happens if himl2 is empty */
1697     IImageList_Release(imgl2);
1698     himl2 = ImageList_Create(32,32,0,0,3);
1699     ok(himl2 != NULL,"failed to recreate himl2\n");
1700
1701     imgl2 = (IImageList *) himl2;
1702
1703     hr = IImageList_Merge(imgl1, -1, (IUnknown *) imgl2, -1, 0, 0, &IID_IImageList, (void **) &merge);
1704     ok(hr == S_OK, "merge himl2,-1 failed\n");
1705     if (hr == S_OK) IImageList_Release(merge);
1706
1707     hr = IImageList_Merge(imgl1, -1, (IUnknown *) imgl2, 0, 0, 0, &IID_IImageList, (void **) &merge);
1708     ok(hr == S_OK, "merge himl2,0 failed\n");
1709     if (hr == S_OK) IImageList_Release(merge);
1710
1711     /* Now try merging an image with itself */
1712     ret = -1;
1713     ok( IImageList_ReplaceIcon(imgl2, -1, hicon1, &ret) == S_OK && (ret == 0),"re-add icon1 to himl2 failed\n");
1714
1715     hr = IImageList_Merge(imgl2, 0, (IUnknown *) imgl2, 0, 0, 0, &IID_IImageList, (void **) &merge);
1716     ok(hr == S_OK, "merge himl2 with itself failed\n");
1717     if (hr == S_OK) IImageList_Release(merge);
1718
1719     /* Try merging 2 different image lists */
1720     ret = -1;
1721     ok( IImageList_ReplaceIcon(imgl1, -1, hicon1, &ret) == S_OK && (ret == 0),"add icon1 to himl1 failed\n");
1722
1723     hr = IImageList_Merge(imgl1, 0, (IUnknown *) imgl2, 0, 0, 0, &IID_IImageList, (void **) &merge);
1724     ok(hr == S_OK, "merge himl1 with himl2 failed\n");
1725     if (hr == S_OK) IImageList_Release(merge);
1726
1727     hr = IImageList_Merge(imgl1, 0, (IUnknown *) imgl2, 0, 8, 16, &IID_IImageList, (void **) &merge);
1728     ok(hr == S_OK, "merge himl1 with himl2 8,16 failed\n");
1729     if (hr == S_OK) IImageList_Release(merge);
1730
1731     IImageList_Release(imgl1);
1732     IImageList_Release(imgl2);
1733
1734     DestroyIcon(hicon1);
1735     DestroyWindow(hwnd);
1736 }
1737
1738 static void test_iconsize(void)
1739 {
1740     HIMAGELIST himl;
1741     INT cx, cy;
1742     BOOL ret;
1743
1744     himl = ImageList_Create(16, 16, ILC_COLOR16, 0, 3);
1745     /* null pointers, not zero imagelist dimensions */
1746     ret = ImageList_GetIconSize(himl, NULL, NULL);
1747     ok(!ret, "got %d\n", ret);
1748
1749     /* doesn't touch return pointers */
1750     cx = 0x1abe11ed;
1751     ret = ImageList_GetIconSize(himl, &cx, NULL);
1752     ok(!ret, "got %d\n", ret);
1753     ok(cx == 0x1abe11ed, "got %d\n", cx);
1754
1755     cy = 0x1abe11ed;
1756     ret = ImageList_GetIconSize(himl, NULL, &cy);
1757     ok(!ret, "got %d\n", ret);
1758     ok(cy == 0x1abe11ed, "got %d\n", cy);
1759
1760     ImageList_Destroy(himl);
1761
1762     ret = ImageList_GetIconSize((HIMAGELIST)0xdeadbeef, &cx, &cy);
1763     ok(!ret, "got %d\n", ret);
1764 }
1765
1766 static void test_create_destroy(void)
1767 {
1768     HIMAGELIST himl;
1769     BOOL rc;
1770
1771     /* list with zero or negative image dimensions */
1772     himl = ImageList_Create(0, 0, ILC_COLOR16, 0, 3);
1773     ok(himl == NULL, "got %p\n", himl);
1774
1775     himl = ImageList_Create(0, 16, ILC_COLOR16, 0, 3);
1776     ok(himl == NULL, "got %p\n", himl);
1777
1778     himl = ImageList_Create(16, 0, ILC_COLOR16, 0, 3);
1779     ok(himl == NULL, "got %p\n", himl);
1780
1781     himl = ImageList_Create(16, -1, ILC_COLOR16, 0, 3);
1782     ok(himl == NULL, "got %p\n", himl);
1783
1784     himl = ImageList_Create(-1, 16, ILC_COLOR16, 0, 3);
1785     ok(himl == NULL, "got %p\n", himl);
1786
1787     rc = ImageList_Destroy((HIMAGELIST)0xdeadbeef);
1788     ok(rc == FALSE, "ImageList_Destroy(0xdeadbeef) should fail and not crash\n");
1789 }
1790
1791 static void test_IImageList_Clone(void)
1792 {
1793     IImageList *imgl, *imgl2;
1794     HIMAGELIST himl;
1795     HRESULT hr;
1796     ULONG ref;
1797
1798     himl = ImageList_Create(16, 16, ILC_COLOR16, 0, 3);
1799     imgl = (IImageList*)himl;
1800
1801 if (0)
1802 {
1803     /* crashes on native */
1804     IImageList_Clone(imgl, &IID_IImageList, NULL);
1805 }
1806
1807     hr = IImageList_Clone(imgl, &IID_IImageList, (void**)&imgl2);
1808     ok(hr == S_OK, "got 0x%08x\n", hr);
1809     ref = IImageList_Release(imgl2);
1810     ok(ref == 0, "got %u\n", ref);
1811
1812     IImageList_Release(imgl);
1813 }
1814
1815 static void test_IImageList_GetBkColor(void)
1816 {
1817     IImageList *imgl;
1818     HIMAGELIST himl;
1819     COLORREF color;
1820     HRESULT hr;
1821
1822     himl = ImageList_Create(16, 16, ILC_COLOR16, 0, 3);
1823     imgl = (IImageList*)himl;
1824
1825 if (0)
1826 {
1827     /* crashes on native */
1828     IImageList_GetBkColor(imgl, NULL);
1829 }
1830
1831     hr = IImageList_GetBkColor(imgl, &color);
1832     ok(hr == S_OK, "got 0x%08x\n", hr);
1833
1834     IImageList_Release(imgl);
1835 }
1836
1837 static void test_IImageList_SetBkColor(void)
1838 {
1839     IImageList *imgl;
1840     HIMAGELIST himl;
1841     COLORREF color;
1842     HRESULT hr;
1843
1844     himl = ImageList_Create(16, 16, ILC_COLOR16, 0, 3);
1845     imgl = (IImageList*)himl;
1846
1847 if (0)
1848 {
1849     /* crashes on native */
1850     IImageList_SetBkColor(imgl, RGB(0, 0, 0), NULL);
1851 }
1852
1853     hr = IImageList_SetBkColor(imgl, CLR_NONE, &color);
1854     ok(hr == S_OK, "got 0x%08x\n", hr);
1855
1856     hr = IImageList_SetBkColor(imgl, CLR_NONE, &color);
1857     ok(hr == S_OK, "got 0x%08x\n", hr);
1858
1859     color = 0xdeadbeef;
1860     hr = IImageList_GetBkColor(imgl, &color);
1861     ok(hr == S_OK, "got 0x%08x\n", hr);
1862     ok(color == CLR_NONE, "got %x\n", color);
1863
1864     IImageList_Release(imgl);
1865 }
1866
1867 static void test_IImageList_GetImageCount(void)
1868 {
1869     IImageList *imgl;
1870     HIMAGELIST himl;
1871     int count;
1872     HRESULT hr;
1873
1874     himl = ImageList_Create(16, 16, ILC_COLOR16, 0, 3);
1875     imgl = (IImageList*)himl;
1876
1877 if (0)
1878 {
1879     /* crashes on native */
1880     IImageList_GetImageCount(imgl, NULL);
1881 }
1882
1883     count = -1;
1884     hr = IImageList_GetImageCount(imgl, &count);
1885     ok(hr == S_OK, "got 0x%08x\n", hr);
1886     ok(count == 0, "got %d\n", count);
1887
1888     IImageList_Release(imgl);
1889 }
1890
1891 static void test_IImageList_GetIconSize(void)
1892 {
1893     IImageList *imgl;
1894     HIMAGELIST himl;
1895     int cx, cy;
1896     HRESULT hr;
1897
1898     himl = ImageList_Create(16, 16, ILC_COLOR16, 0, 3);
1899     imgl = (IImageList*)himl;
1900
1901     hr = IImageList_GetIconSize(imgl, NULL, NULL);
1902     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
1903
1904     hr = IImageList_GetIconSize(imgl, &cx, NULL);
1905     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
1906
1907     hr = IImageList_GetIconSize(imgl, NULL, &cy);
1908     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
1909
1910     IImageList_Release(imgl);
1911 }
1912
1913 START_TEST(imagelist)
1914 {
1915     ULONG_PTR ctx_cookie;
1916     HANDLE hCtx;
1917
1918     HMODULE hComCtl32 = GetModuleHandle("comctl32.dll");
1919     pImageList_Create = NULL;   /* These are not needed for non-v6.0 tests*/
1920     pImageList_Add = NULL;
1921     pImageList_DrawIndirect = (void*)GetProcAddress(hComCtl32, "ImageList_DrawIndirect");
1922     pImageList_SetImageCount = (void*)GetProcAddress(hComCtl32, "ImageList_SetImageCount");
1923
1924     hinst = GetModuleHandleA(NULL);
1925
1926     InitCommonControls();
1927
1928     test_create_destroy();
1929     test_hotspot();
1930     test_add_remove();
1931     test_imagecount();
1932     test_DrawIndirect();
1933     test_merge();
1934     test_imagelist_storage();
1935     test_iconsize();
1936
1937     FreeLibrary(hComCtl32);
1938
1939     /* Now perform v6 tests */
1940
1941     if (!load_v6_module(&ctx_cookie, &hCtx))
1942         return;
1943
1944     /* Reload comctl32 */
1945     hComCtl32 = LoadLibraryA("comctl32.dll");
1946     pImageList_Create = (void*)GetProcAddress(hComCtl32, "ImageList_Create");
1947     pImageList_Add = (void*)GetProcAddress(hComCtl32, "ImageList_Add");
1948     pImageList_DrawIndirect = (void*)GetProcAddress(hComCtl32, "ImageList_DrawIndirect");
1949     pImageList_SetImageCount = (void*)GetProcAddress(hComCtl32, "ImageList_SetImageCount");
1950     pImageList_CoCreateInstance = (void*)GetProcAddress(hComCtl32, "ImageList_CoCreateInstance");
1951     pHIMAGELIST_QueryInterface = (void*)GetProcAddress(hComCtl32, "HIMAGELIST_QueryInterface");
1952
1953     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1954
1955     /* Do v6.0 tests */
1956     test_ImageList_DrawIndirect();
1957     test_shell_imagelist();
1958     test_iimagelist();
1959
1960     test_hotspot_v6();
1961     test_IImageList_Add_Remove();
1962     test_IImageList_Get_SetImageCount();
1963     test_IImageList_Draw();
1964     test_IImageList_Merge();
1965     test_IImageList_Clone();
1966     test_IImageList_GetBkColor();
1967     test_IImageList_SetBkColor();
1968     test_IImageList_GetImageCount();
1969     test_IImageList_GetIconSize();
1970
1971     CoUninitialize();
1972
1973     unload_v6_module(ctx_cookie, hCtx);
1974 }