quartz: Use proper alloc/free functions for COM objects.
[wine] / dlls / comctl32 / tests / header.c
1 /* Unit test suite for header control.
2  *
3  * Copyright 2005 Vijay Kiran Kamuju
4  * Copyright 2007 Shanren Zhou
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21
22 #include <windows.h>
23 #include <commctrl.h>
24 #include <assert.h>
25
26 #include "wine/test.h"
27
28 typedef struct tagEXPECTEDNOTIFY
29 {
30     INT iCode;
31     BOOL fUnicode;
32     HDITEMA hdItem;
33 } EXPECTEDNOTIFY;
34
35 typedef LRESULT (*CUSTOMDRAWPROC)(int n, NMCUSTOMDRAW *nm);
36
37 static CUSTOMDRAWPROC g_CustomDrawProc;
38 static int g_CustomDrawCount;
39 static DRAWITEMSTRUCT g_DrawItem;
40 static BOOL g_DrawItemReceived;
41
42 static EXPECTEDNOTIFY expectedNotify[10];
43 static INT nExpectedNotify = 0;
44 static INT nReceivedNotify = 0;
45 static INT unexpectedNotify[10];
46 static INT nUnexpectedNotify = 0;
47
48 static HWND hHeaderParentWnd;
49 static HWND hWndHeader;
50 #define MAX_CHARS 100
51
52 #define compare(val, exp, fmt)  ok((val) == (exp), #val " value: " fmt ", expected: " fmt "\n", (val), (exp))
53
54 #define expect(expected, got) ok(expected == got, "expected %d, got %d\n", expected,got)
55
56 static void expect_notify(INT iCode, BOOL fUnicode, HDITEMA *lpItem)
57 {
58     assert(nExpectedNotify < 10);
59     expectedNotify[nExpectedNotify].iCode = iCode;
60     expectedNotify[nExpectedNotify].fUnicode = fUnicode;
61     expectedNotify[nExpectedNotify].hdItem = *lpItem;
62     nExpectedNotify++;
63 }
64
65 static void dont_expect_notify(INT iCode)
66 {
67     assert(nUnexpectedNotify < 10);
68     unexpectedNotify[nUnexpectedNotify++] = iCode;
69 }
70
71 static BOOL notifies_received(void)
72 {
73     BOOL fRet = (nExpectedNotify == nReceivedNotify);
74     nExpectedNotify = nReceivedNotify = 0;
75     nUnexpectedNotify = 0;
76     return fRet;
77 }
78
79 static LONG addItem(HWND hdex, int idx, LPSTR text)
80 {
81     HDITEMA hdItem;
82     hdItem.mask       = HDI_TEXT | HDI_WIDTH;
83     hdItem.cxy        = 100;
84     hdItem.pszText    = text;
85     hdItem.cchTextMax = 0;
86     return (LONG)SendMessage(hdex, HDM_INSERTITEMA, (WPARAM)idx, (LPARAM)&hdItem);
87 }
88
89 static LONG setItem(HWND hdex, int idx, LPSTR text, BOOL fCheckNotifies)
90 {
91     LONG ret;
92     HDITEMA hdexItem;
93     hdexItem.mask       = HDI_TEXT;
94     hdexItem.pszText    = text;
95     hdexItem.cchTextMax = 0;
96     if (fCheckNotifies)
97     {
98         expect_notify(HDN_ITEMCHANGINGA, FALSE, &hdexItem);
99         expect_notify(HDN_ITEMCHANGEDA, FALSE, &hdexItem);
100     }
101     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
102     if (fCheckNotifies)
103         ok(notifies_received(), "setItem(): not all expected notifies were received\n");
104     return ret;
105 }
106
107 static LONG setItemUnicodeNotify(HWND hdex, int idx, LPSTR text, LPWSTR wText)
108 {
109     LONG ret;
110     HDITEMA hdexItem;
111     HDITEMW hdexNotify;
112     hdexItem.mask       = HDI_TEXT;
113     hdexItem.pszText    = text;
114     hdexItem.cchTextMax = 0;
115     
116     hdexNotify.mask    = HDI_TEXT;
117     hdexNotify.pszText = wText;
118     
119     expect_notify(HDN_ITEMCHANGINGW, TRUE, (HDITEMA*)&hdexNotify);
120     expect_notify(HDN_ITEMCHANGEDW, TRUE, (HDITEMA*)&hdexNotify);
121     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
122     ok(notifies_received(), "setItemUnicodeNotify(): not all expected notifies were received\n");
123     return ret;
124 }
125
126 static LONG delItem(HWND hdex, int idx)
127 {
128     return (LONG)SendMessage(hdex, HDM_DELETEITEM, (WPARAM)idx, 0);
129 }
130
131 static LONG getItemCount(HWND hdex)
132 {
133     return (LONG)SendMessage(hdex, HDM_GETITEMCOUNT, 0, 0);
134 }
135
136 static LONG getItem(HWND hdex, int idx, LPSTR textBuffer)
137 {
138     HDITEMA hdItem;
139     hdItem.mask         = HDI_TEXT;
140     hdItem.pszText      = textBuffer;
141     hdItem.cchTextMax   = MAX_CHARS;
142     return (LONG)SendMessage(hdex, HDM_GETITEMA, (WPARAM)idx, (LPARAM)&hdItem);
143 }
144
145 static void addReadDelItem(HWND hdex, HDITEMA *phdiCreate, int maskRead, HDITEMA *phdiRead)
146 {
147     ok(SendMessage(hdex, HDM_INSERTITEMA, (WPARAM)0, (LPARAM)phdiCreate)!=-1, "Adding item failed\n");
148     ZeroMemory(phdiRead, sizeof(HDITEMA));
149     phdiRead->mask = maskRead;
150     ok(SendMessage(hdex, HDM_GETITEMA, (WPARAM)0, (LPARAM)phdiRead)!=0, "Getting item data failed\n");
151     ok(SendMessage(hdex, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0)!=0, "Deleting item failed\n");
152 }
153
154 static HWND create_header_control (void)
155 {
156     HWND handle;
157     HDLAYOUT hlayout;
158     RECT rectwin;
159     WINDOWPOS winpos;
160
161     handle = CreateWindowEx(0, WC_HEADER, NULL,
162                             WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
163                             0, 0, 0, 0,
164                             hHeaderParentWnd, NULL, NULL, NULL);
165     assert(handle);
166
167     if (winetest_interactive)
168         ShowWindow (hHeaderParentWnd, SW_SHOW);
169
170     GetClientRect(hHeaderParentWnd,&rectwin);
171     hlayout.prc = &rectwin;
172     hlayout.pwpos = &winpos;
173     SendMessageA(handle,HDM_LAYOUT,0,(LPARAM) &hlayout);
174     SetWindowPos(handle, winpos.hwndInsertAfter, winpos.x, winpos.y, 
175                  winpos.cx, winpos.cy, 0);
176
177     return handle;
178 }
179
180 static void compare_items(INT iCode, HDITEMA *hdi1, HDITEMA *hdi2, BOOL fUnicode)
181 {
182     ok(hdi1->mask == hdi2->mask, "Notify %d mask mismatch (%08x != %08x)\n", iCode, hdi1->mask, hdi2->mask);
183     if (hdi1->mask & HDI_WIDTH)
184     {
185         ok(hdi1->cxy == hdi2->cxy, "Notify %d cxy mismatch (%08x != %08x)\n", iCode, hdi1->cxy, hdi2->cxy);
186     }
187     if (hdi1->mask & HDI_TEXT)
188     {
189         if (hdi1->pszText == LPSTR_TEXTCALLBACKA)
190         {
191             ok(hdi1->pszText == LPSTR_TEXTCALLBACKA, "Notify %d - only one item is LPSTR_TEXTCALLBACK\n", iCode);
192         }
193         else
194         if (fUnicode)
195         {
196             char buf1[260];
197             char buf2[260];
198             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi1->pszText, -1, buf1, 260, NULL, NULL);
199             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi2->pszText, -1, buf2, 260, NULL, NULL);
200             ok(lstrcmpW((LPWSTR)hdi1->pszText, (LPWSTR)hdi2->pszText)==0,
201                 "Notify %d text mismatch (L\"%s\" vs L\"%s\")\n",
202                     iCode, buf1, buf2);
203         }
204         else
205         {
206             ok(strcmp(hdi1->pszText, hdi2->pszText)==0,
207                 "Notify %d text mismatch (\"%s\" vs \"%s\")\n",
208                     iCode, hdi1->pszText, hdi2->pszText);
209             }
210     }
211 }
212
213 static char pszFirstItem[]      = "First Item";
214 static char pszSecondItem[]     = "Second Item";
215 static char pszThirdItem[]      = "Third Item";
216 static char pszFourthItem[]     = "Fourth Item";
217 static char pszReplaceItem[]    = "Replace Item";
218 static char pszOutOfRangeItem[] = "Out Of Range Item";
219
220 static char *str_items[] =
221     {pszFirstItem, pszSecondItem, pszThirdItem, pszFourthItem, pszReplaceItem, pszOutOfRangeItem};
222     
223 static char pszUniTestA[]  = "TST";
224 static WCHAR pszUniTestW[] = {'T','S','T',0};
225
226
227 #define TEST_GET_ITEM(i,c)\
228 {   res = getItem(hWndHeader, i, buffer);\
229     ok(res != 0, "Getting item[%d] using valid index failed unexpectedly (%d)\n", i, res);\
230     ok(strcmp(str_items[c], buffer) == 0, "Getting item[%d] returned \"%s\" expecting \"%s\"\n", i, buffer, str_items[c]);\
231 }
232
233 #define TEST_GET_ITEMCOUNT(i)\
234 {   res = getItemCount(hWndHeader);\
235     ok(res == i, "Got Item Count as %d\n", res);\
236 }
237
238 static void check_auto_format(void)
239 {
240     HDITEMA hdiCreate;
241     HDITEMA hdiRead;
242     static CHAR text[] = "Test";
243     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
244
245     /* Windows implicitly sets some format bits in INSERTITEM */
246
247     /* HDF_STRING is automatically set and cleared for no text */
248     hdiCreate.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
249     hdiCreate.pszText = text;
250     hdiCreate.cxy = 100;
251     hdiCreate.fmt=HDF_CENTER;
252     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
253     ok(hdiRead.fmt == (HDF_STRING|HDF_CENTER), "HDF_STRING not set automatically (fmt=%x)\n", hdiRead.fmt);
254
255     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
256     hdiCreate.pszText = text;
257     hdiCreate.fmt = HDF_CENTER|HDF_STRING;
258     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
259     ok(hdiRead.fmt == (HDF_CENTER), "HDF_STRING should be automatically cleared (fmt=%x)\n", hdiRead.fmt);
260
261     /* HDF_BITMAP is automatically set and cleared for a NULL bitmap or no bitmap */
262     hdiCreate.mask = HDI_BITMAP|HDI_WIDTH|HDI_FORMAT;
263     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
264     hdiCreate.fmt = HDF_CENTER;
265     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
266     ok(hdiRead.fmt == (HDF_BITMAP|HDF_CENTER), "HDF_BITMAP not set automatically (fmt=%x)\n", hdiRead.fmt);
267     DeleteObject(hdiCreate.hbm);
268
269     hdiCreate.hbm = NULL;
270     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
271     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
272     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for NULL bitmap (fmt=%x)\n", hdiRead.fmt);
273
274     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
275     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
276     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
277     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for no bitmap (fmt=%x)\n", hdiRead.fmt);
278
279     /* HDF_IMAGE is automatically set but not cleared */
280     hdiCreate.mask = HDI_IMAGE|HDI_WIDTH|HDI_FORMAT;
281     hdiCreate.iImage = 17;
282     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
283     ok(hdiRead.fmt == (HDF_IMAGE|HDF_CENTER), "HDF_IMAGE not set automatically (fmt=%x)\n", hdiRead.fmt);
284
285     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
286     hdiCreate.fmt = HDF_CENTER|HDF_IMAGE;
287     hdiCreate.iImage = 0;
288     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
289     ok(hdiRead.fmt == (HDF_CENTER|HDF_IMAGE), "HDF_IMAGE shouldn't be cleared automatically (fmt=%x)\n", hdiRead.fmt);
290 }
291
292 static void check_auto_fields(void)
293 {
294     HDITEMA hdiCreate;
295     HDITEMA hdiRead;
296     static CHAR text[] = "Test";
297     LONG res;
298
299     /* Windows stores the format, width, lparam even if they are not in the item's mask */
300     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
301     hdiCreate.mask = HDI_TEXT;
302     hdiCreate.cxy = 100;
303     hdiCreate.pszText = text;
304     addReadDelItem(hWndHeader, &hdiCreate, HDI_WIDTH, &hdiRead);
305     TEST_GET_ITEMCOUNT(6);
306     ok(hdiRead.cxy == hdiCreate.cxy, "cxy should be automatically set\n");
307
308     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
309     hdiCreate.mask = HDI_TEXT;
310     hdiCreate.pszText = text;
311     hdiCreate.lParam = 0x12345678;
312     addReadDelItem(hWndHeader, &hdiCreate, HDI_LPARAM, &hdiRead);
313     TEST_GET_ITEMCOUNT(6);
314     ok(hdiRead.lParam == hdiCreate.lParam, "lParam should be automatically set\n");
315
316     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
317     hdiCreate.mask = HDI_TEXT;
318     hdiCreate.pszText = text;
319     hdiCreate.fmt = HDF_STRING|HDF_CENTER;
320     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
321     TEST_GET_ITEMCOUNT(6);
322     ok(hdiRead.fmt == hdiCreate.fmt, "fmt should be automatically set\n");
323
324     /* others fields are not set */
325     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
326     hdiCreate.mask = HDI_TEXT;
327     hdiCreate.pszText = text;
328     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
329     addReadDelItem(hWndHeader, &hdiCreate, HDI_BITMAP, &hdiRead);
330     TEST_GET_ITEMCOUNT(6);
331     ok(hdiRead.hbm == NULL, "hbm should not be automatically set\n");
332     DeleteObject(hdiCreate.hbm);
333
334     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
335     hdiCreate.mask = HDI_IMAGE;
336     hdiCreate.iImage = 17;
337     hdiCreate.pszText = text;
338     addReadDelItem(hWndHeader, &hdiCreate, HDI_TEXT, &hdiRead);
339     TEST_GET_ITEMCOUNT(6);
340     ok(hdiRead.pszText==NULL, "pszText shouldn't be automatically set\n");
341
342     /* field from comctl >4.0 not tested as the system probably won't touch them */
343 }
344
345 static void check_mask(void)
346 {
347     HDITEMA hdi;
348     static CHAR text[] = "ABC";
349     LRESULT ret;
350
351     /* don't create items if the mask is zero */
352     ZeroMemory(&hdi, sizeof(hdi));
353     hdi.mask = 0;
354     hdi.cxy = 200;
355     hdi.pszText = text;
356     hdi.fmt = 0;
357     hdi.iOrder = 0;
358     hdi.lParam = 17;
359     hdi.cchTextMax = 260;
360     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
361     ok(ret == -1, "Creating an item with a zero mask should have failed\n");
362     if (ret != -1) SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
363
364     /* with a non-zero mask creation will succeed */
365     ZeroMemory(&hdi, sizeof(hdi));
366     hdi.mask = HDI_LPARAM;
367     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
368     ok(ret != -1, "Adding item with non-zero mask failed\n");
369     if (ret != -1)
370         SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
371
372     /* in SETITEM if the mask contains a unknown bit, it is ignored */
373     ZeroMemory(&hdi, sizeof(hdi));
374     hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
375     hdi.lParam = 133;
376     hdi.iImage = 17;
377     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
378     ok(ret != -1, "Adding item failed\n");
379
380     if (ret != -1)
381     {
382         /* check result */
383         ZeroMemory(&hdi, sizeof(hdi));
384         hdi.mask = HDI_LPARAM | HDI_IMAGE;
385         SendMessage(hWndHeader, HDM_GETITEM, (WPARAM)0, (LPARAM)&hdi);
386         ok(hdi.lParam == 133, "comctl32 4.0 field not set\n");
387         ok(hdi.iImage == 17, "comctl32 >4.0 field not set\n");
388
389         /* but in GETITEM if an unknown bit is set, comctl32 uses only version 4.0 fields */
390         ZeroMemory(&hdi, sizeof(hdi));
391         hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
392         SendMessage(hWndHeader, HDM_GETITEM, (WPARAM)0, (LPARAM)&hdi);
393         ok(hdi.lParam == 133, "comctl32 4.0 field not read\n");
394         ok(hdi.iImage == 0, "comctl32 >4.0 field shouldn't be read\n");
395
396         SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
397     }
398 }
399
400 static void test_header_control (void)
401 {
402     LONG res;
403     static char buffer[MAX_CHARS];
404     int i;
405
406     hWndHeader = create_header_control ();
407
408     for (i = 3; i >= 0; i--)
409     {
410         TEST_GET_ITEMCOUNT(3-i);
411         res = addItem(hWndHeader, 0, str_items[i]);
412         ok(res == 0, "Adding simple item failed (%d)\n", res);
413     }
414
415     TEST_GET_ITEMCOUNT(4);
416     res = addItem(hWndHeader, 99, str_items[i+1]);
417     ok(res != -1, "Adding Out of Range item should fail with -1 got (%d)\n", res);
418     TEST_GET_ITEMCOUNT(5);
419     res = addItem(hWndHeader, 5, str_items[i+1]);
420     ok(res != -1, "Adding Out of Range item should fail with -1 got (%d)\n", res);
421     TEST_GET_ITEMCOUNT(6);
422
423     for (i = 0; i < 4; i++) { TEST_GET_ITEM(i,i); TEST_GET_ITEMCOUNT(6); }
424
425     res=getItem(hWndHeader, 99, buffer);
426     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
427     res=getItem(hWndHeader, 5, buffer);
428     ok(res == 1, "Getting Out of Range item should fail with 1 (%d), got %s\n", res,buffer);
429     res=getItem(hWndHeader, -2, buffer);
430     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
431
432     if (winetest_interactive)
433     {
434         UpdateWindow(hHeaderParentWnd);
435         UpdateWindow(hWndHeader);
436     }
437
438     TEST_GET_ITEMCOUNT(6);
439     res=setItem(hWndHeader, 99, str_items[5], FALSE);
440     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
441     res=setItem(hWndHeader, 5, str_items[5], TRUE);
442     ok(res == 1, "Setting Out of Range item should fail with 1 (%d)\n", res);
443     res=setItem(hWndHeader, -2, str_items[5], FALSE);
444     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
445     TEST_GET_ITEMCOUNT(6);
446
447     for (i = 0; i < 4; i++)
448     {
449         res = setItem(hWndHeader, i, str_items[4], TRUE);
450         ok(res != 0, "Setting %d item failed (%d)\n", i+1, res);
451         TEST_GET_ITEM(i, 4);
452         TEST_GET_ITEMCOUNT(6);
453     }
454     
455     SendMessageA(hWndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, 0);
456     setItemUnicodeNotify(hWndHeader, 3, pszUniTestA, pszUniTestW);
457     SendMessageA(hWndHeader, WM_NOTIFYFORMAT, (WPARAM)hHeaderParentWnd, (LPARAM)NF_REQUERY);
458     setItem(hWndHeader, 3, str_items[4], TRUE);
459     
460     dont_expect_notify(HDN_GETDISPINFOA);
461     dont_expect_notify(HDN_GETDISPINFOW);
462     addItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA);
463     setItem(hWndHeader, 0, str_items[4], TRUE);
464     /* unexpected notifies cleared by notifies_received in setItem */
465     dont_expect_notify(HDN_GETDISPINFOA);
466     dont_expect_notify(HDN_GETDISPINFOW);
467     setItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA, TRUE);
468     /* unexpected notifies cleared by notifies_received in setItem */
469     delItem(hWndHeader, 0);
470
471     check_auto_format();
472     TEST_GET_ITEMCOUNT(6);
473     check_auto_fields();
474     TEST_GET_ITEMCOUNT(6);
475     check_mask();
476     TEST_GET_ITEMCOUNT(6);
477
478     res = delItem(hWndHeader, 5);
479     ok(res == 1, "Deleting Out of Range item should fail with 1 (%d)\n", res);
480     res = delItem(hWndHeader, -2);
481     ok(res == 0, "Deleting Out of Range item should fail with 0 (%d)\n", res);
482     TEST_GET_ITEMCOUNT(5);
483
484     res = delItem(hWndHeader, 3);
485     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
486     TEST_GET_ITEMCOUNT(4);
487     res = delItem(hWndHeader, 0);
488     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
489     TEST_GET_ITEMCOUNT(3);
490     res = delItem(hWndHeader, 0);
491     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
492     TEST_GET_ITEMCOUNT(2);
493     res = delItem(hWndHeader, 0);
494     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
495     TEST_GET_ITEMCOUNT(1);
496
497     DestroyWindow(hWndHeader);
498 }
499
500 static void test_hdm_getitemrect(void)
501 {
502     /* Afer the setorderarray is called, item with index 0 becomes
503      * the 2nd item and item with index 1 becomes 1st item
504      */
505     RECT rect;
506     int retVal;
507     retVal = SendMessage(hWndHeader, HDM_GETITEMRECT, 0, (LPARAM) &rect);
508     ok(retVal == TRUE, "Getting item rect should TRUE, got %d\n", retVal);
509     /* check bounding rectangle information */
510     expect(80, rect.left);
511     expect(0, rect.top);
512     expect(160, rect.right);
513     expect(18, rect.bottom);
514
515     retVal = SendMessage(hWndHeader, HDM_GETITEMRECT, 1, (LPARAM) &rect);
516     ok(retVal == TRUE, "Getting item rect should TRUE, got %d\n", retVal);
517     /* check bounding rectangle information */
518     expect(0, rect.left);
519     expect(0, rect.top);
520     expect(80, rect.right);
521     expect(18, rect.bottom);
522     retVal = SendMessage(hWndHeader, HDM_GETITEMRECT, 10, (LPARAM) &rect);
523     ok(retVal == 0, "Getting rect of nonexistent item should return 0, got %d\n", retVal);
524 }
525
526 static void test_hdm_layout (void)
527 {
528     int retVal;
529     RECT rect;
530     HDLAYOUT hdLayout;
531     WINDOWPOS windowPos;
532     hdLayout.prc = &rect;
533     hdLayout.pwpos = &windowPos;
534     retVal = SendMessage(hWndHeader, HDM_LAYOUT, 0, (LPARAM) &hdLayout);
535     expect(TRUE, retVal);
536 }
537
538 static void test_hdm_ordertoindex (void)
539 {
540     int retVal;
541     retVal = SendMessage(hWndHeader, HDM_ORDERTOINDEX, 1, 0);
542     expect(0, retVal);
543 }
544
545 static void test_hdm_hittest (void)
546 {
547     int retVal;
548     POINT pt;
549     HDHITTESTINFO hdHitTestInfo;
550     const int firstItemMaxRight = 80;
551     const int secondItemMaxRight = 160;
552     const int maxBottom = 18;
553     /* Afer the setorderarray is called, item with index 0 becomes
554      * the 2nd item and item with index 1 becomes 1st item
555      */
556     pt.x = firstItemMaxRight - 1;
557     pt.y = maxBottom - 1;
558     hdHitTestInfo.pt = pt;
559     retVal = SendMessage(hWndHeader, HDM_HITTEST, 0, (LPARAM) &hdHitTestInfo);
560     expect(1, retVal);
561     expect(1, hdHitTestInfo.iItem);
562
563     pt.x = secondItemMaxRight - 1;
564     pt.y = maxBottom - 1;
565     hdHitTestInfo.pt = pt;
566     retVal = SendMessage(hWndHeader, HDM_HITTEST, 0, (LPARAM) &hdHitTestInfo);
567     expect(0, retVal);
568     expect(0, hdHitTestInfo.iItem);
569
570     pt.x = secondItemMaxRight;
571     pt.y = maxBottom + 1;
572     hdHitTestInfo.pt = pt;
573     todo_wine
574     {
575     retVal = SendMessage(hWndHeader, HDM_HITTEST, 0, (LPARAM) &hdHitTestInfo);
576     expect(-1, retVal);
577     }
578 }
579
580 static void test_hdm_sethotdivider (void)
581 {
582     int retVal;
583     /*  low word: x coordinate = 5
584      *  high word:  y coordinate = 5
585      */
586    todo_wine
587    {
588          retVal = SendMessage(hWndHeader, HDM_SETHOTDIVIDER, TRUE, (LPARAM) 0X00050005);
589          expect(0, retVal);
590    }
591     retVal = SendMessage(hWndHeader, HDM_SETHOTDIVIDER, FALSE, 100);
592     expect(100, retVal);
593     retVal = SendMessage(hWndHeader, HDM_SETHOTDIVIDER, FALSE, 1);
594     expect(1, retVal);
595 }
596
597 static void test_hdm_imageMessages (void)
598 {
599     HIMAGELIST hImageList = ImageList_Create (4, 4, 0, 1, 0);
600     HIMAGELIST hImageListRetVal;
601     hImageListRetVal = (HIMAGELIST) SendMessage(hWndHeader, HDM_SETIMAGELIST, 0, (LPARAM) hImageList);
602     ok(hImageListRetVal == NULL, "Expected NULL, got %d\n", (int) hImageListRetVal);
603     hImageListRetVal = (HIMAGELIST) SendMessage(hWndHeader, HDM_GETIMAGELIST, 0, 0);
604     ok(hImageListRetVal != NULL, "Expected non-NULL handle, got %d\n", (int) hImageListRetVal);
605     hImageListRetVal = (HIMAGELIST) SendMessage(hWndHeader, HDM_CREATEDRAGIMAGE, 0, 0);
606     ok(hImageListRetVal != NULL, "Expected non-NULL handle, got %d\n", (int) hImageListRetVal);
607 }
608
609 static void test_hdm_filterMessages (void)
610 {
611     int retVal;
612     todo_wine
613     {
614      /* msdn incorrecly states that return value
615       * is the index of the filter control being
616       * modified. The sendMessage here should
617       * return previous filter timeout value
618      */
619         retVal = SendMessage(hWndHeader, HDM_SETFILTERCHANGETIMEOUT, 1, 100);
620         expect(1000, retVal);
621         retVal = SendMessage(hWndHeader, HDM_CLEARFILTER, 0, 1);
622         expect(1, retVal);
623         retVal = SendMessage(hWndHeader, HDM_EDITFILTER, 1, 0);
624         expect(1, retVal);
625      }
626 }
627
628 static void test_hdm_unicodeformatMessages (void)
629 {
630     int retVal;
631     retVal = SendMessage(hWndHeader, HDM_SETUNICODEFORMAT, TRUE, 0);
632     expect(0, retVal);
633     retVal = SendMessage(hWndHeader, HDM_GETUNICODEFORMAT, 0, 0);
634     expect(1, retVal);
635 }
636
637 static void test_hdm_bitmapmarginMessages (void)
638 {
639     int retVal;
640     retVal = SendMessage(hWndHeader, HDM_GETBITMAPMARGIN, 0, 0);
641     expect(6, retVal);
642 }
643
644 static void test_hdm_index_messages (void)
645 {
646     int retVal;
647     int loopcnt;
648     int strcmpResult;
649     int iSize;
650     static const int lpiarray[2] = {1, 0};
651     static int lpiarrayReceived[2];
652     static char firstHeaderItem[] = "Name";
653     static char secondHeaderItem[] = "Size";
654     static char thirdHeaderItem[] = "Type";
655     static char fourthHeaderItem[] = "Data Modified";
656     static char *items[] = {firstHeaderItem, secondHeaderItem, thirdHeaderItem, fourthHeaderItem};
657     HDITEM hdItem;
658     hdItem.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT;
659     hdItem.fmt = HDF_LEFT;
660     hdItem.cxy = 80;
661     hdItem.cchTextMax = 260;
662     hWndHeader = create_header_control();
663     for ( loopcnt = 0 ; loopcnt < 4 ; loopcnt++ )
664     {
665       hdItem.pszText = items[loopcnt];
666       retVal = SendMessage(hWndHeader, HDM_INSERTITEM, loopcnt, (LPARAM) &hdItem);
667       ok(retVal == loopcnt, "Adding item %d failed with return value %d\n", ( loopcnt + 1 ), retVal);
668     }
669
670     retVal = SendMessage(hWndHeader, HDM_DELETEITEM, 3, (LPARAM) &hdItem);
671     ok(retVal == TRUE, "Deleting item 3 should return TRUE, got %d\n", retVal);
672     retVal = SendMessage(hWndHeader, HDM_GETITEMCOUNT, 0, (LPARAM) &hdItem);
673     ok(retVal == 3, "Getting item count should return 3, got %d\n", retVal);
674
675     retVal = SendMessage(hWndHeader, HDM_DELETEITEM, 3, (LPARAM) &hdItem);
676     ok(retVal == FALSE, "Deleting already-deleted item should return FALSE, got %d\n", retVal);
677     retVal = SendMessage(hWndHeader, HDM_GETITEMCOUNT, 0, (LPARAM) &hdItem);
678     ok(retVal == 3, "Getting item count should return 3, got %d\n", retVal);
679
680     retVal = SendMessage(hWndHeader, HDM_DELETEITEM, 2, (LPARAM) &hdItem);
681     ok(retVal == TRUE, "Deleting item 2 should return TRUE, got %d\n", retVal);
682     retVal = SendMessage(hWndHeader, HDM_GETITEMCOUNT, 0, (LPARAM) &hdItem);
683     ok(retVal == 2, "Getting item count should return 2, got %d\n", retVal);
684
685     retVal = SendMessage(hWndHeader, HDM_GETITEM, 3, (LPARAM) &hdItem);
686     ok(retVal == FALSE, "Getting already-deleted item should return FALSE, got %d\n", retVal);
687
688     retVal = SendMessage(hWndHeader, HDM_GETITEM, 0, (LPARAM) &hdItem);
689     ok(retVal == TRUE, "Getting the 1st header item should return TRUE, got %d\n", retVal);
690     /* check if the item is the right one */
691     strcmpResult =  strcmp(hdItem.pszText, firstHeaderItem);
692     expect(0, strcmpResult);
693     expect(80, hdItem.cxy);
694
695     iSize = SendMessage(hWndHeader, HDM_GETITEMCOUNT, 0, (LPARAM) &hdItem);
696     retVal = SendMessage(hWndHeader, HDM_SETORDERARRAY, (WPARAM) iSize , (LPARAM) (LPINT) lpiarray );
697     ok(retVal == TRUE, "Setting header items order should return TRUE, got %d\n", retVal);
698     retVal = SendMessage(hWndHeader, HDM_GETORDERARRAY, (WPARAM) iSize, (LPARAM) (LPINT) lpiarrayReceived );
699     ok(retVal == TRUE, "Getting header items order should return TRUE, got %d\n", retVal);
700     /* check if the array order is set correctly and the size of the array is corret. */
701     expect(2, iSize);
702     expect(lpiarray[0], lpiarrayReceived[0]);
703     expect(lpiarray[1], lpiarrayReceived[1]);
704
705     hdItem.mask = HDI_FORMAT;
706     hdItem.fmt = HDF_CENTER | HDF_STRING;
707     retVal = SendMessage(hWndHeader, HDM_SETITEM, 0, (LPARAM) &hdItem);
708     ok(retVal == TRUE, "Aligning 1st header item to center should return TRUE, got %d\n", retVal);
709     hdItem.fmt = HDF_RIGHT | HDF_STRING;
710     retVal = SendMessage(hWndHeader, HDM_SETITEM, 1, (LPARAM) &hdItem);
711     ok(retVal == TRUE, "Aligning 2nd header item to right should return TRUE, got %d\n", retVal);
712 }
713
714 static void test_header_messages (void)
715 {
716
717     test_hdm_index_messages();
718     test_hdm_getitemrect();
719     test_hdm_hittest();
720     test_hdm_layout();
721     test_hdm_ordertoindex();
722     test_hdm_sethotdivider();
723
724     test_hdm_imageMessages();
725     test_hdm_filterMessages();
726     test_hdm_unicodeformatMessages();
727     test_hdm_bitmapmarginMessages();
728 }
729
730 #define TEST_NMCUSTOMDRAW(draw_stage, item_spec, lparam, _left, _top, _right, _bottom) \
731     ok(nm->dwDrawStage == draw_stage, "Invalid dwDrawStage %d vs %d\n", draw_stage, nm->dwDrawStage); \
732     if (item_spec != -1) \
733         ok(nm->dwItemSpec == item_spec, "Invalid dwItemSpec %d vs %ld\n", item_spec, nm->dwItemSpec); \
734     ok(nm->lItemlParam == lparam, "Invalid lItemlParam %d vs %ld\n", lparam, nm->lItemlParam); \
735     ok(nm->rc.top == _top && nm->rc.bottom == _bottom && nm->rc.left == _left && nm->rc.right == _right, \
736         "Invalid rect (%d,%d) (%d,%d) vs (%d,%d) (%d,%d)\n", _left, _top, _right, _bottom, \
737         nm->rc.left, nm->rc.top, nm->rc.right, nm->rc.bottom);
738
739 static LRESULT customdraw_1(int n, NMCUSTOMDRAW *nm)
740 {
741     if (nm == NULL) {  /* test ended */
742         ok(n==1, "NM_CUSTOMDRAW messages: %d, expected: 1\n", n);
743         return 0;
744     }
745
746     switch (n)
747     {
748     case 0:
749         /* don't test dwItemSpec - it's 0 no comctl5 but 1308756 on comctl6 */
750         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
751         return 0;
752     }
753
754     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
755     return -1;
756 }
757
758 static LRESULT customdraw_2(int n, NMCUSTOMDRAW *nm)
759 {
760     if (nm == NULL) {  /* test ended */
761         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
762         return 0;
763     }
764
765     switch (n)
766     {
767     case 0:
768         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
769         return CDRF_NOTIFYITEMDRAW;
770     case 1:
771         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, 18);
772         return 0;
773     case 2:
774         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, 18);
775         return 0;
776     case 3:
777         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, 18);
778         return 0;
779     }
780
781     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
782     return 0;
783 }
784
785 static LRESULT customdraw_3(int n, NMCUSTOMDRAW *nm)
786 {
787     if (nm == NULL) {  /* test ended */
788         ok(n==5, "NM_CUSTOMDRAW messages: %d, expected: 5\n", n);
789         return 0;
790     }
791
792     switch (n)
793     {
794     case 0:
795         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
796         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTERASE|CDRF_NOTIFYPOSTPAINT|CDRF_SKIPDEFAULT;
797     case 1:
798         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, 18);
799         return 0;
800     case 2:
801         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, 18);
802         return 0;
803     case 3:
804         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, 18);
805         return 0;
806     case 4:
807         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, 18);
808         return 0;
809     }
810
811     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
812     return 0;
813 }
814
815
816 static LRESULT customdraw_4(int n, NMCUSTOMDRAW *nm)
817 {
818     if (nm == NULL) {  /* test ended */
819         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
820         return 0;
821     }
822
823     switch (n)
824     {
825     case 0:
826         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
827         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTPAINT;
828     case 1:
829         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, 18);
830         return 0;
831     case 2:
832         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, 18);
833         return 0;
834     case 3:
835         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, 18);
836         return 0;
837     }
838
839     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
840     return 0;
841 }
842
843 static void run_customdraw_scenario(CUSTOMDRAWPROC proc)
844 {
845     g_CustomDrawProc = proc;
846     g_CustomDrawCount = 0;
847     InvalidateRect(hWndHeader, NULL, TRUE);
848     UpdateWindow(hWndHeader);
849     proc(g_CustomDrawCount, NULL);
850     g_CustomDrawProc = NULL;
851 }
852
853 static void test_customdraw(void)
854 {
855     int i;
856     HDITEM item;
857     RECT rect;
858     CHAR name[] = "Test";
859     hWndHeader = create_header_control();
860     GetClientRect(hWndHeader, &rect);
861     ok(rect.right - rect.left == 670 && rect.bottom - rect.top == 18,
862         "Tests will fail as header size is %dx%d instead of 670x18\n",
863         rect.right - rect.left == 670, rect.bottom - rect.top == 18);
864
865     for (i = 0; i < 3; i++)
866     {
867         ZeroMemory(&item, sizeof(item));
868         item.mask = HDI_TEXT|HDI_WIDTH;
869         item.cxy = 50*(i+1);
870         item.pszText = name;
871         item.lParam = i*5;
872         SendMessage(hWndHeader, HDM_INSERTITEM, i, (LPARAM)&item);
873     }
874
875     run_customdraw_scenario(customdraw_1);
876     run_customdraw_scenario(customdraw_2);
877     run_customdraw_scenario(customdraw_3);
878
879     ZeroMemory(&item, sizeof(item));
880     item.mask = HDI_FORMAT;
881     item.fmt = HDF_OWNERDRAW;
882     SendMessage(hWndHeader, HDM_SETITEM, 1, (LPARAM)&item);
883     g_DrawItem.CtlID = 0;
884     g_DrawItem.CtlType = ODT_HEADER;
885     g_DrawItem.hwndItem = hWndHeader;
886     g_DrawItem.itemID = 1;
887     g_DrawItem.itemState = 0;
888     SendMessage(hWndHeader, HDM_GETITEMRECT, 1, (LPARAM)&g_DrawItem.rcItem);
889     run_customdraw_scenario(customdraw_4);
890     ok(g_DrawItemReceived, "WM_DRAWITEM not received\n");
891     DestroyWindow(hWndHeader);
892     hWndHeader = NULL;
893     g_DrawItem.CtlType = 0;
894     g_DrawItemReceived = FALSE;
895 }
896
897 static void check_order(const int expected_id[], const int expected_order[],
898                         int count, const char *type)
899 {
900     int i;
901     HDITEMA hdi;
902
903     ok(getItemCount(hWndHeader) == count, "Invalid item count in order tests\n");
904     for (i = 0; i < count; i++)
905     {
906         hdi.mask = HDI_LPARAM|HDI_ORDER;
907         SendMessage(hWndHeader, HDM_GETITEMA, i, (LPARAM)&hdi);
908         ok(hdi.lParam == expected_id[i],
909             "Invalid item ids after '%s'- item %d has lParam %d\n", type, i, (int)hdi.lParam);
910         ok(hdi.iOrder == expected_order[i],
911             "Invalid item order after '%s'- item %d has iOrder %d\n", type, i, hdi.iOrder);
912     }
913 }
914
915 static void test_header_order (void)
916 {
917     const int rand1[] = {0, 1, 1, 0, 4};
918     const int rand2[] = {4, 5, 6, 7, 4};
919     const int rand3[] = {5, 5, 1, 6, 1};
920     const int rand4[] = {1, 5, 2, 7, 6, 1, 4, 2, 3, 2};
921     const int rand5[] = {7, 8, 5, 6, 7, 2, 1, 9, 10, 10};
922     const int rand6[] = {2, 8, 3, 4, 0};
923
924     const int ids1[] = {3, 0, 2, 1, 4};
925     const int ord1[] = {0, 1, 2, 3, 4};
926     const int ids2[] = {3, 9, 7, 0, 2, 1, 4, 8, 6, 5};
927     const int ord2[] = {0, 4, 7, 1, 2, 3, 9, 8, 6, 5};
928     const int ord3[] = {0, 3, 9, 2, 1, 8, 7, 6, 5, 4};
929     const int ids4[] = {9, 0, 1, 8, 6};
930     const int ord4[] = {1, 0, 4, 3, 2};
931
932     char buffer[20];
933     HDITEMA hdi;
934     int i;
935
936     hWndHeader = create_header_control();
937
938     ZeroMemory(&hdi, sizeof(HDITEMA));
939     hdi.mask = HDI_TEXT | HDI_LPARAM;
940     hdi.pszText = buffer;
941     strcpy(buffer, "test");
942
943     for (i = 0; i < 5; i++)
944     {
945         hdi.lParam = i;
946         SendMessage(hWndHeader, HDM_INSERTITEMA, rand1[i], (LPARAM)&hdi);
947         rand();
948     }
949     check_order(ids1, ord1, 5, "insert without iOrder");
950
951     hdi.mask |= HDI_ORDER;
952     for (i = 0; i < 5; i++)
953     {
954         hdi.lParam = i + 5;
955         hdi.iOrder = rand2[i];
956         SendMessage(hWndHeader, HDM_INSERTITEMA, rand3[i], (LPARAM)&hdi);
957         rand(); rand();
958     }
959     check_order(ids2, ord2, 10, "insert with order");
960
961     hdi.mask = HDI_ORDER;
962     for (i=0; i<10; i++)
963     {
964         hdi.iOrder = rand5[i];
965         SendMessage(hWndHeader, HDM_SETITEMA, rand4[i], (LPARAM)&hdi);
966         rand(); rand();
967     }
968     check_order(ids2, ord3, 10, "setitems changing order");
969
970     for (i=0; i<5; i++)
971         SendMessage(hWndHeader, HDM_DELETEITEM, rand6[i], 0);
972     check_order(ids4, ord4, 5, "deleteitem");
973
974     DestroyWindow(hWndHeader);
975 }
976
977 static LRESULT CALLBACK HeaderTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
978 {
979     DRAWITEMSTRUCT *di;
980     switch(msg) {
981
982     case WM_NOTIFY:
983     {
984         NMHEADERA *hdr = (NMHEADER *)lParam;
985         EXPECTEDNOTIFY *expected;
986         int i;
987
988         if (hdr->hdr.code == NM_CUSTOMDRAW)
989             if (g_CustomDrawProc)
990                 return g_CustomDrawProc(g_CustomDrawCount++, (NMCUSTOMDRAW*)hdr);
991
992         for (i=0; i<nUnexpectedNotify; i++)
993             ok(hdr->hdr.code != unexpectedNotify[i], "Received invalid notify %d\n", hdr->hdr.code);
994         
995         if (nReceivedNotify >= nExpectedNotify || hdr->hdr.hwndFrom != hWndHeader )
996             break;
997
998         expected = &expectedNotify[nReceivedNotify];
999         if (hdr->hdr.code != expected->iCode)
1000             break;
1001         
1002         nReceivedNotify++;
1003         compare_items(hdr->hdr.code, &expected->hdItem, hdr->pitem, expected->fUnicode);
1004         break;
1005     }
1006
1007     case WM_DRAWITEM:
1008         di = (DRAWITEMSTRUCT *)lParam;
1009         ok(g_DrawItem.CtlType != 0, "Unexpected WM_DRAWITEM\n");
1010         if (g_DrawItem.CtlType == 0) return 0;
1011         g_DrawItemReceived = TRUE;
1012         compare(di->CtlType,   g_DrawItem.CtlType, "%d");
1013         compare(di->CtlID,     g_DrawItem.CtlID, "%d");
1014         compare(di->hwndItem,  g_DrawItem.hwndItem, "%p");
1015         compare(di->itemID,    g_DrawItem.itemID, "%d");
1016         compare(di->itemState, g_DrawItem.itemState, "%d");
1017         compare(di->rcItem.left,   g_DrawItem.rcItem.left, "%d");
1018         compare(di->rcItem.top,    g_DrawItem.rcItem.top, "%d");
1019         compare(di->rcItem.right,  g_DrawItem.rcItem.right, "%d");
1020         compare(di->rcItem.bottom, g_DrawItem.rcItem.bottom, "%d");
1021         break;
1022
1023     case WM_DESTROY:
1024         PostQuitMessage(0);
1025         break;
1026   
1027     default:
1028         return DefWindowProcA(hWnd, msg, wParam, lParam);
1029     }
1030     
1031     return 0L;
1032 }
1033
1034 static void init(void) {
1035     WNDCLASSA wc;
1036     INITCOMMONCONTROLSEX icex;
1037
1038     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
1039     icex.dwICC  = ICC_USEREX_CLASSES;
1040     InitCommonControlsEx(&icex);
1041
1042     wc.style = CS_HREDRAW | CS_VREDRAW;
1043     wc.cbClsExtra = 0;
1044     wc.cbWndExtra = 0;
1045     wc.hInstance = GetModuleHandleA(NULL);
1046     wc.hIcon = NULL;
1047     wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_ARROW));
1048     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1049     wc.lpszMenuName = NULL;
1050     wc.lpszClassName = "HeaderTestClass";
1051     wc.lpfnWndProc = HeaderTestWndProc;
1052     RegisterClassA(&wc);
1053
1054     hHeaderParentWnd = CreateWindowExA(0, "HeaderTestClass", "Header test", WS_OVERLAPPEDWINDOW, 
1055       CW_USEDEFAULT, CW_USEDEFAULT, 672+2*GetSystemMetrics(SM_CXSIZEFRAME),
1056       226+GetSystemMetrics(SM_CYCAPTION)+2*GetSystemMetrics(SM_CYSIZEFRAME),
1057       NULL, NULL, GetModuleHandleA(NULL), 0);
1058     assert(hHeaderParentWnd != NULL);
1059     ShowWindow(hHeaderParentWnd, SW_SHOW);
1060 }
1061
1062 START_TEST(header)
1063 {
1064     init();
1065
1066     test_header_control();
1067     test_header_order();
1068     test_customdraw();
1069     test_header_messages();
1070
1071     DestroyWindow(hHeaderParentWnd);
1072 }