msi: Only insert entries into listbox if property value matches.
[wine] / dlls / comctl32 / tests / header.c
1 /* Unit test suite for header control.
2  *
3  * Copyright 2005 Vijay Kiran Kamuju 
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20
21 #include <windows.h>
22 #include <commctrl.h>
23 #include <assert.h>
24
25 #include "wine/test.h"
26
27 typedef struct tagEXPECTEDNOTIFY
28 {
29     INT iCode;
30     BOOL fUnicode;
31     HDITEMA hdItem;
32 } EXPECTEDNOTIFY;
33
34 typedef LRESULT (*CUSTOMDRAWPROC)(int n, NMCUSTOMDRAW *nm);
35
36 static CUSTOMDRAWPROC g_CustomDrawProc;
37 static int g_CustomDrawCount;
38 static DRAWITEMSTRUCT g_DrawItem;
39 static BOOL g_DrawItemReceived;
40
41 static EXPECTEDNOTIFY expectedNotify[10];
42 static INT nExpectedNotify = 0;
43 static INT nReceivedNotify = 0;
44 static INT unexpectedNotify[10];
45 static INT nUnexpectedNotify = 0;
46
47 static HWND hHeaderParentWnd;
48 static HWND hWndHeader;
49 #define MAX_CHARS 100
50
51 #define compare(val, exp, fmt)  ok((val) == (exp), #val " value: " fmt ", expected: " fmt "\n", (val), (exp))
52
53 static void expect_notify(INT iCode, BOOL fUnicode, HDITEMA *lpItem)
54 {
55     assert(nExpectedNotify < 10);
56     expectedNotify[nExpectedNotify].iCode = iCode;
57     expectedNotify[nExpectedNotify].fUnicode = fUnicode;
58     expectedNotify[nExpectedNotify].hdItem = *lpItem;
59     nExpectedNotify++;
60 }
61
62 static void dont_expect_notify(INT iCode)
63 {
64     assert(nUnexpectedNotify < 10);
65     unexpectedNotify[nUnexpectedNotify++] = iCode;
66 }
67
68 static BOOL notifies_received(void)
69 {
70     BOOL fRet = (nExpectedNotify == nReceivedNotify);
71     nExpectedNotify = nReceivedNotify = 0;
72     nUnexpectedNotify = 0;
73     return fRet;
74 }
75
76 static LONG addItem(HWND hdex, int idx, LPSTR text)
77 {
78     HDITEMA hdItem;
79     hdItem.mask       = HDI_TEXT | HDI_WIDTH;
80     hdItem.cxy        = 100;
81     hdItem.pszText    = text;
82     hdItem.cchTextMax = 0;
83     return (LONG)SendMessage(hdex, HDM_INSERTITEMA, (WPARAM)idx, (LPARAM)&hdItem);
84 }
85
86 static LONG setItem(HWND hdex, int idx, LPSTR text, BOOL fCheckNotifies)
87 {
88     LONG ret;
89     HDITEMA hdexItem;
90     hdexItem.mask       = HDI_TEXT;
91     hdexItem.pszText    = text;
92     hdexItem.cchTextMax = 0;
93     if (fCheckNotifies)
94     {
95         expect_notify(HDN_ITEMCHANGINGA, FALSE, &hdexItem);
96         expect_notify(HDN_ITEMCHANGEDA, FALSE, &hdexItem);
97     }
98     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
99     if (fCheckNotifies)
100         ok(notifies_received(), "setItem(): not all expected notifies were received\n");
101     return ret;
102 }
103
104 static LONG setItemUnicodeNotify(HWND hdex, int idx, LPSTR text, LPWSTR wText)
105 {
106     LONG ret;
107     HDITEMA hdexItem;
108     HDITEMW hdexNotify;
109     hdexItem.mask       = HDI_TEXT;
110     hdexItem.pszText    = text;
111     hdexItem.cchTextMax = 0;
112     
113     hdexNotify.mask    = HDI_TEXT;
114     hdexNotify.pszText = wText;
115     
116     expect_notify(HDN_ITEMCHANGINGW, TRUE, (HDITEMA*)&hdexNotify);
117     expect_notify(HDN_ITEMCHANGEDW, TRUE, (HDITEMA*)&hdexNotify);
118     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
119     ok(notifies_received(), "setItemUnicodeNotify(): not all expected notifies were received\n");
120     return ret;
121 }
122
123 static LONG delItem(HWND hdex, int idx)
124 {
125     return (LONG)SendMessage(hdex, HDM_DELETEITEM, (WPARAM)idx, 0);
126 }
127
128 static LONG getItemCount(HWND hdex)
129 {
130     return (LONG)SendMessage(hdex, HDM_GETITEMCOUNT, 0, 0);
131 }
132
133 static LONG getItem(HWND hdex, int idx, LPSTR textBuffer)
134 {
135     HDITEMA hdItem;
136     hdItem.mask         = HDI_TEXT;
137     hdItem.pszText      = textBuffer;
138     hdItem.cchTextMax   = MAX_CHARS;
139     return (LONG)SendMessage(hdex, HDM_GETITEMA, (WPARAM)idx, (LPARAM)&hdItem);
140 }
141
142 static void addReadDelItem(HWND hdex, HDITEMA *phdiCreate, int maskRead, HDITEMA *phdiRead)
143 {
144     ok(SendMessage(hdex, HDM_INSERTITEMA, (WPARAM)0, (LPARAM)phdiCreate)!=-1, "Adding item failed\n");
145     ZeroMemory(phdiRead, sizeof(HDITEMA));
146     phdiRead->mask = maskRead;
147     ok(SendMessage(hdex, HDM_GETITEMA, (WPARAM)0, (LPARAM)phdiRead)!=0, "Getting item data failed\n");
148     ok(SendMessage(hdex, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0)!=0, "Deleting item failed\n");
149 }
150
151 static HWND create_header_control (void)
152 {
153     HWND handle;
154     HDLAYOUT hlayout;
155     RECT rectwin;
156     WINDOWPOS winpos;
157
158     handle = CreateWindowEx(0, WC_HEADER, NULL,
159                             WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
160                             0, 0, 0, 0,
161                             hHeaderParentWnd, NULL, NULL, NULL);
162     assert(handle);
163
164     if (winetest_interactive)
165         ShowWindow (hHeaderParentWnd, SW_SHOW);
166
167     GetClientRect(hHeaderParentWnd,&rectwin);
168     hlayout.prc = &rectwin;
169     hlayout.pwpos = &winpos;
170     SendMessageA(handle,HDM_LAYOUT,0,(LPARAM) &hlayout);
171     SetWindowPos(handle, winpos.hwndInsertAfter, winpos.x, winpos.y, 
172                  winpos.cx, winpos.cy, 0);
173
174     return handle;
175 }
176
177 static void compare_items(INT iCode, HDITEMA *hdi1, HDITEMA *hdi2, BOOL fUnicode)
178 {
179     ok(hdi1->mask == hdi2->mask, "Notify %d mask mismatch (%08x != %08x)\n", iCode, hdi1->mask, hdi2->mask);
180     if (hdi1->mask & HDI_WIDTH)
181     {
182         ok(hdi1->cxy == hdi2->cxy, "Notify %d cxy mismatch (%08x != %08x)\n", iCode, hdi1->cxy, hdi2->cxy);
183     }
184     if (hdi1->mask & HDI_TEXT)
185     {
186         if (hdi1->pszText == LPSTR_TEXTCALLBACKA)
187         {
188             ok(hdi1->pszText == LPSTR_TEXTCALLBACKA, "Notify %d - only one item is LPSTR_TEXTCALLBACK\n", iCode);
189         }
190         else
191         if (fUnicode)
192         {
193             char buf1[260];
194             char buf2[260];
195             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi1->pszText, -1, buf1, 260, NULL, NULL);
196             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi2->pszText, -1, buf2, 260, NULL, NULL);
197             ok(lstrcmpW((LPWSTR)hdi1->pszText, (LPWSTR)hdi2->pszText)==0,
198                 "Notify %d text mismatch (L\"%s\" vs L\"%s\")\n",
199                     iCode, buf1, buf2);
200         }
201         else
202         {
203             ok(strcmp(hdi1->pszText, hdi2->pszText)==0,
204                 "Notify %d text mismatch (\"%s\" vs \"%s\")\n",
205                     iCode, hdi1->pszText, hdi2->pszText);
206             }
207     }
208 }
209
210 static char pszFirstItem[]      = "First Item";
211 static char pszSecondItem[]     = "Second Item";
212 static char pszThirdItem[]      = "Third Item";
213 static char pszFourthItem[]     = "Fourth Item";
214 static char pszReplaceItem[]    = "Replace Item";
215 static char pszOutOfRangeItem[] = "Out Of Range Item";
216
217 static char *str_items[] =
218     {pszFirstItem, pszSecondItem, pszThirdItem, pszFourthItem, pszReplaceItem, pszOutOfRangeItem};
219     
220 static char pszUniTestA[]  = "TST";
221 static WCHAR pszUniTestW[] = {'T','S','T',0};
222
223
224 #define TEST_GET_ITEM(i,c)\
225 {   res = getItem(hWndHeader, i, buffer);\
226     ok(res != 0, "Getting item[%d] using valid index failed unexpectedly (%d)\n", i, res);\
227     ok(strcmp(str_items[c], buffer) == 0, "Getting item[%d] returned \"%s\" expecting \"%s\"\n", i, buffer, str_items[c]);\
228 }
229
230 #define TEST_GET_ITEMCOUNT(i)\
231 {   res = getItemCount(hWndHeader);\
232     ok(res == i, "Got Item Count as %d\n", res);\
233 }
234
235 static void check_auto_format(void)
236 {
237     HDITEMA hdiCreate;
238     HDITEMA hdiRead;
239     static CHAR text[] = "Test";
240     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
241
242     /* Windows implicitly sets some format bits in INSERTITEM */
243
244     /* HDF_STRING is automatically set and cleared for no text */
245     hdiCreate.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
246     hdiCreate.pszText = text;
247     hdiCreate.cxy = 100;
248     hdiCreate.fmt=HDF_CENTER;
249     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
250     ok(hdiRead.fmt == (HDF_STRING|HDF_CENTER), "HDF_STRING not set automatically (fmt=%x)\n", hdiRead.fmt);
251
252     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
253     hdiCreate.pszText = text;
254     hdiCreate.fmt = HDF_CENTER|HDF_STRING;
255     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
256     ok(hdiRead.fmt == (HDF_CENTER), "HDF_STRING should be automatically cleared (fmt=%x)\n", hdiRead.fmt);
257
258     /* HDF_BITMAP is automatically set and cleared for a NULL bitmap or no bitmap */
259     hdiCreate.mask = HDI_BITMAP|HDI_WIDTH|HDI_FORMAT;
260     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
261     hdiCreate.fmt = HDF_CENTER;
262     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
263     ok(hdiRead.fmt == (HDF_BITMAP|HDF_CENTER), "HDF_BITMAP not set automatically (fmt=%x)\n", hdiRead.fmt);
264     DeleteObject(hdiCreate.hbm);
265
266     hdiCreate.hbm = NULL;
267     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
268     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
269     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for NULL bitmap (fmt=%x)\n", hdiRead.fmt);
270
271     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
272     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
273     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
274     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for no bitmap (fmt=%x)\n", hdiRead.fmt);
275
276     /* HDF_IMAGE is automatically set but not cleared */
277     hdiCreate.mask = HDI_IMAGE|HDI_WIDTH|HDI_FORMAT;
278     hdiCreate.iImage = 17;
279     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
280     ok(hdiRead.fmt == (HDF_IMAGE|HDF_CENTER), "HDF_IMAGE not set automatically (fmt=%x)\n", hdiRead.fmt);
281
282     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
283     hdiCreate.fmt = HDF_CENTER|HDF_IMAGE;
284     hdiCreate.iImage = 0;
285     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
286     ok(hdiRead.fmt == (HDF_CENTER|HDF_IMAGE), "HDF_IMAGE shouldn't be cleared automatically (fmt=%x)\n", hdiRead.fmt);
287 }
288
289 static void check_auto_fields(void)
290 {
291     HDITEMA hdiCreate;
292     HDITEMA hdiRead;
293     static CHAR text[] = "Test";
294     LONG res;
295
296     /* Windows stores the format, width, lparam even if they are not in the item's mask */
297     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
298     hdiCreate.mask = HDI_TEXT;
299     hdiCreate.cxy = 100;
300     hdiCreate.pszText = text;
301     addReadDelItem(hWndHeader, &hdiCreate, HDI_WIDTH, &hdiRead);
302     TEST_GET_ITEMCOUNT(6);
303     ok(hdiRead.cxy == hdiCreate.cxy, "cxy should be automatically set\n");
304
305     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
306     hdiCreate.mask = HDI_TEXT;
307     hdiCreate.pszText = text;
308     hdiCreate.lParam = 0x12345678;
309     addReadDelItem(hWndHeader, &hdiCreate, HDI_LPARAM, &hdiRead);
310     TEST_GET_ITEMCOUNT(6);
311     ok(hdiRead.lParam == hdiCreate.lParam, "lParam should be automatically set\n");
312
313     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
314     hdiCreate.mask = HDI_TEXT;
315     hdiCreate.pszText = text;
316     hdiCreate.fmt = HDF_STRING|HDF_CENTER;
317     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
318     TEST_GET_ITEMCOUNT(6);
319     ok(hdiRead.fmt == hdiCreate.fmt, "fmt should be automatically set\n");
320
321     /* others fields are not set */
322     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
323     hdiCreate.mask = HDI_TEXT;
324     hdiCreate.pszText = text;
325     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
326     addReadDelItem(hWndHeader, &hdiCreate, HDI_BITMAP, &hdiRead);
327     TEST_GET_ITEMCOUNT(6);
328     ok(hdiRead.hbm == NULL, "hbm should not be automatically set\n");
329     DeleteObject(hdiCreate.hbm);
330
331     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
332     hdiCreate.mask = HDI_IMAGE;
333     hdiCreate.iImage = 17;
334     hdiCreate.pszText = text;
335     addReadDelItem(hWndHeader, &hdiCreate, HDI_TEXT, &hdiRead);
336     TEST_GET_ITEMCOUNT(6);
337     ok(hdiRead.pszText==NULL, "pszText shouldn't be automatically set\n");
338
339     /* field from comctl >4.0 not tested as the system probably won't touch them */
340 }
341
342 static void check_mask(void)
343 {
344     HDITEMA hdi;
345     static CHAR text[] = "ABC";
346     LRESULT ret;
347
348     /* don't create items if the mask is zero */
349     ZeroMemory(&hdi, sizeof(hdi));
350     hdi.mask = 0;
351     hdi.cxy = 200;
352     hdi.pszText = text;
353     hdi.fmt = 0;
354     hdi.iOrder = 0;
355     hdi.lParam = 17;
356     hdi.cchTextMax = 260;
357     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
358     ok(ret == -1, "Creating an item with a zero mask should have failed\n");
359     if (ret != -1) SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
360
361     /* with a non-zero mask creation will succeed */
362     ZeroMemory(&hdi, sizeof(hdi));
363     hdi.mask = HDI_LPARAM;
364     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
365     ok(ret != -1, "Adding item with non-zero mask failed\n");
366     if (ret != -1)
367         SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
368
369     /* in SETITEM if the mask contains a unknown bit, it is ignored */
370     ZeroMemory(&hdi, sizeof(hdi));
371     hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
372     hdi.lParam = 133;
373     hdi.iImage = 17;
374     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
375     ok(ret != -1, "Adding item failed\n");
376
377     if (ret != -1)
378     {
379         /* check result */
380         ZeroMemory(&hdi, sizeof(hdi));
381         hdi.mask = HDI_LPARAM | HDI_IMAGE;
382         SendMessage(hWndHeader, HDM_GETITEM, (WPARAM)0, (LPARAM)&hdi);
383         ok(hdi.lParam == 133, "comctl32 4.0 field not set\n");
384         ok(hdi.iImage == 17, "comctl32 >4.0 field not set\n");
385
386         /* but in GETITEM if an unknown bit is set, comctl32 uses only version 4.0 fields */
387         ZeroMemory(&hdi, sizeof(hdi));
388         hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
389         SendMessage(hWndHeader, HDM_GETITEM, (WPARAM)0, (LPARAM)&hdi);
390         ok(hdi.lParam == 133, "comctl32 4.0 field not read\n");
391         ok(hdi.iImage == 0, "comctl32 >4.0 field shouldn't be read\n");
392
393         SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
394     }
395 }
396
397 static void test_header_control (void)
398 {
399     LONG res;
400     static char buffer[MAX_CHARS];
401     int i;
402
403     hWndHeader = create_header_control ();
404
405     for (i = 3; i >= 0; i--)
406     {
407         TEST_GET_ITEMCOUNT(3-i);
408         res = addItem(hWndHeader, 0, str_items[i]);
409         ok(res == 0, "Adding simple item failed (%d)\n", res);
410     }
411
412     TEST_GET_ITEMCOUNT(4);
413     res = addItem(hWndHeader, 99, str_items[i+1]);
414     ok(res != -1, "Adding Out of Range item should fail with -1 got (%d)\n", res);
415     TEST_GET_ITEMCOUNT(5);
416     res = addItem(hWndHeader, 5, 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(6);
419
420     for (i = 0; i < 4; i++) { TEST_GET_ITEM(i,i); TEST_GET_ITEMCOUNT(6); }
421
422     res=getItem(hWndHeader, 99, buffer);
423     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
424     res=getItem(hWndHeader, 5, buffer);
425     ok(res == 1, "Getting Out of Range item should fail with 1 (%d), got %s\n", res,buffer);
426     res=getItem(hWndHeader, -2, buffer);
427     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
428
429     if (winetest_interactive)
430     {
431         UpdateWindow(hHeaderParentWnd);
432         UpdateWindow(hWndHeader);
433     }
434
435     TEST_GET_ITEMCOUNT(6);
436     res=setItem(hWndHeader, 99, str_items[5], FALSE);
437     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
438     res=setItem(hWndHeader, 5, str_items[5], TRUE);
439     ok(res == 1, "Setting Out of Range item should fail with 1 (%d)\n", res);
440     res=setItem(hWndHeader, -2, str_items[5], FALSE);
441     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
442     TEST_GET_ITEMCOUNT(6);
443
444     for (i = 0; i < 4; i++)
445     {
446         res = setItem(hWndHeader, i, str_items[4], TRUE);
447         ok(res != 0, "Setting %d item failed (%d)\n", i+1, res);
448         TEST_GET_ITEM(i, 4);
449         TEST_GET_ITEMCOUNT(6);
450     }
451     
452     SendMessageA(hWndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, 0);
453     setItemUnicodeNotify(hWndHeader, 3, pszUniTestA, pszUniTestW);
454     SendMessageA(hWndHeader, WM_NOTIFYFORMAT, (WPARAM)hHeaderParentWnd, (LPARAM)NF_REQUERY);
455     setItem(hWndHeader, 3, str_items[4], TRUE);
456     
457     dont_expect_notify(HDN_GETDISPINFOA);
458     dont_expect_notify(HDN_GETDISPINFOW);
459     addItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA);
460     setItem(hWndHeader, 0, str_items[4], TRUE);
461     /* unexpected notifies cleared by notifies_received in setItem */
462     dont_expect_notify(HDN_GETDISPINFOA);
463     dont_expect_notify(HDN_GETDISPINFOW);
464     setItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA, TRUE);
465     /* unexpected notifies cleared by notifies_received in setItem */
466     delItem(hWndHeader, 0);
467
468     check_auto_format();
469     TEST_GET_ITEMCOUNT(6);
470     check_auto_fields();
471     TEST_GET_ITEMCOUNT(6);
472     check_mask();
473     TEST_GET_ITEMCOUNT(6);
474
475     res = delItem(hWndHeader, 5);
476     ok(res == 1, "Deleting Out of Range item should fail with 1 (%d)\n", res);
477     res = delItem(hWndHeader, -2);
478     ok(res == 0, "Deleting Out of Range item should fail with 0 (%d)\n", res);
479     TEST_GET_ITEMCOUNT(5);
480
481     res = delItem(hWndHeader, 3);
482     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
483     TEST_GET_ITEMCOUNT(4);
484     res = delItem(hWndHeader, 0);
485     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
486     TEST_GET_ITEMCOUNT(3);
487     res = delItem(hWndHeader, 0);
488     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
489     TEST_GET_ITEMCOUNT(2);
490     res = delItem(hWndHeader, 0);
491     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
492     TEST_GET_ITEMCOUNT(1);
493
494     DestroyWindow(hWndHeader);
495 }
496
497
498 #define TEST_NMCUSTOMDRAW(draw_stage, item_spec, lparam, _left, _top, _right, _bottom) \
499     ok(nm->dwDrawStage == draw_stage, "Invalid dwDrawStage %d vs %d\n", draw_stage, nm->dwDrawStage); \
500     if (item_spec != -1) \
501         ok(nm->dwItemSpec == item_spec, "Invalid dwItemSpec %d vs %ld\n", item_spec, nm->dwItemSpec); \
502     ok(nm->lItemlParam == lparam, "Invalid lItemlParam %d vs %ld\n", lparam, nm->lItemlParam); \
503     ok(nm->rc.top == _top && nm->rc.bottom == _bottom && nm->rc.left == _left && nm->rc.right == _right, \
504         "Invalid rect (%d,%d) (%d,%d) vs (%d,%d) (%d,%d)\n", _left, _top, _right, _bottom, \
505         nm->rc.left, nm->rc.top, nm->rc.right, nm->rc.bottom);
506
507 static LRESULT customdraw_1(int n, NMCUSTOMDRAW *nm)
508 {
509     if (nm == NULL) {  /* test ended */
510         ok(n==1, "NM_CUSTOMDRAW messages: %d, expected: 1\n", n);
511         return 0;
512     }
513
514     switch (n)
515     {
516     case 0:
517         /* don't test dwItemSpec - it's 0 no comctl5 but 1308756 on comctl6 */
518         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
519         return 0;
520     }
521
522     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
523     return -1;
524 }
525
526 static LRESULT customdraw_2(int n, NMCUSTOMDRAW *nm)
527 {
528     if (nm == NULL) {  /* test ended */
529         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
530         return 0;
531     }
532
533     switch (n)
534     {
535     case 0:
536         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
537         return CDRF_NOTIFYITEMDRAW;
538     case 1:
539         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, 18);
540         return 0;
541     case 2:
542         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, 18);
543         return 0;
544     case 3:
545         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, 18);
546         return 0;
547     }
548
549     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
550     return 0;
551 }
552
553 static LRESULT customdraw_3(int n, NMCUSTOMDRAW *nm)
554 {
555     if (nm == NULL) {  /* test ended */
556         ok(n==5, "NM_CUSTOMDRAW messages: %d, expected: 5\n", n);
557         return 0;
558     }
559
560     switch (n)
561     {
562     case 0:
563         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
564         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTERASE|CDRF_NOTIFYPOSTPAINT|CDRF_SKIPDEFAULT;
565     case 1:
566         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, 18);
567         return 0;
568     case 2:
569         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, 18);
570         return 0;
571     case 3:
572         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, 18);
573         return 0;
574     case 4:
575         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, 18);
576         return 0;
577     }
578
579     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
580     return 0;
581 }
582
583
584 static LRESULT customdraw_4(int n, NMCUSTOMDRAW *nm)
585 {
586     if (nm == NULL) {  /* test ended */
587         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
588         return 0;
589     }
590
591     switch (n)
592     {
593     case 0:
594         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
595         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTPAINT;
596     case 1:
597         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, 18);
598         return 0;
599     case 2:
600         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, 18);
601         return 0;
602     case 3:
603         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, 18);
604         return 0;
605     }
606
607     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
608     return 0;
609 }
610
611 static void run_customdraw_scenario(CUSTOMDRAWPROC proc)
612 {
613     g_CustomDrawProc = proc;
614     g_CustomDrawCount = 0;
615     InvalidateRect(hWndHeader, NULL, TRUE);
616     UpdateWindow(hWndHeader);
617     proc(g_CustomDrawCount, NULL);
618     g_CustomDrawProc = NULL;
619 }
620
621 static void test_customdraw(void)
622 {
623     int i;
624     HDITEM item;
625     RECT rect;
626     CHAR name[] = "Test";
627     hWndHeader = create_header_control();
628     GetClientRect(hWndHeader, &rect);
629     ok(rect.right - rect.left == 670 && rect.bottom - rect.top == 18,
630         "Tests will fail as header size is %dx%d instead of 670x18\n",
631         rect.right - rect.left == 670, rect.bottom - rect.top == 18);
632
633     for (i = 0; i < 3; i++)
634     {
635         ZeroMemory(&item, sizeof(item));
636         item.mask = HDI_TEXT|HDI_WIDTH;
637         item.cxy = 50*(i+1);
638         item.pszText = name;
639         item.lParam = i*5;
640         SendMessage(hWndHeader, HDM_INSERTITEM, i, (LPARAM)&item);
641     }
642
643     run_customdraw_scenario(customdraw_1);
644     run_customdraw_scenario(customdraw_2);
645     run_customdraw_scenario(customdraw_3);
646
647     ZeroMemory(&item, sizeof(item));
648     item.mask = HDI_FORMAT;
649     item.fmt = HDF_OWNERDRAW;
650     SendMessage(hWndHeader, HDM_SETITEM, 1, (LPARAM)&item);
651     g_DrawItem.CtlID = 0;
652     g_DrawItem.CtlType = ODT_HEADER;
653     g_DrawItem.hwndItem = hWndHeader;
654     g_DrawItem.itemID = 1;
655     g_DrawItem.itemState = 0;
656     SendMessage(hWndHeader, HDM_GETITEMRECT, 1, (LPARAM)&g_DrawItem.rcItem);
657     run_customdraw_scenario(customdraw_4);
658     ok(g_DrawItemReceived, "WM_DRAWITEM not received\n");
659     DestroyWindow(hWndHeader);
660     hWndHeader = NULL;
661     g_DrawItem.CtlType = 0;
662     g_DrawItemReceived = FALSE;
663 }
664
665 static void check_order(const int expected_id[], const int expected_order[],
666                         int count, const char *type)
667 {
668     int i;
669     HDITEMA hdi;
670
671     ok(getItemCount(hWndHeader) == count, "Invalid item count in order tests\n");
672     for (i = 0; i < count; i++)
673     {
674         hdi.mask = HDI_LPARAM|HDI_ORDER;
675         SendMessage(hWndHeader, HDM_GETITEMA, i, (LPARAM)&hdi);
676         ok(hdi.lParam == expected_id[i],
677             "Invalid item ids after '%s'- item %d has lParam %d\n", type, i, (int)hdi.lParam);
678         ok(hdi.iOrder == expected_order[i],
679             "Invalid item order after '%s'- item %d has iOrder %d\n", type, i, hdi.iOrder);
680     }
681 }
682
683 static void test_header_order (void)
684 {
685     const int rand1[] = {0, 1, 1, 0, 4};
686     const int rand2[] = {4, 5, 6, 7, 4};
687     const int rand3[] = {5, 5, 1, 6, 1};
688     const int rand4[] = {1, 5, 2, 7, 6, 1, 4, 2, 3, 2};
689     const int rand5[] = {7, 8, 5, 6, 7, 2, 1, 9, 10, 10};
690     const int rand6[] = {2, 8, 3, 4, 0};
691
692     const int ids1[] = {3, 0, 2, 1, 4};
693     const int ord1[] = {0, 1, 2, 3, 4};
694     const int ids2[] = {3, 9, 7, 0, 2, 1, 4, 8, 6, 5};
695     const int ord2[] = {0, 4, 7, 1, 2, 3, 9, 8, 6, 5};
696     const int ord3[] = {0, 3, 9, 2, 1, 8, 7, 6, 5, 4};
697     const int ids4[] = {9, 0, 1, 8, 6};
698     const int ord4[] = {1, 0, 4, 3, 2};
699
700     char buffer[20];
701     HDITEMA hdi;
702     int i;
703
704     hWndHeader = create_header_control();
705
706     ZeroMemory(&hdi, sizeof(HDITEMA));
707     hdi.mask = HDI_TEXT | HDI_LPARAM;
708     hdi.pszText = buffer;
709     strcpy(buffer, "test");
710
711     for (i = 0; i < 5; i++)
712     {
713         hdi.lParam = i;
714         SendMessage(hWndHeader, HDM_INSERTITEMA, rand1[i], (LPARAM)&hdi);
715         rand();
716     }
717     check_order(ids1, ord1, 5, "insert without iOrder");
718
719     hdi.mask |= HDI_ORDER;
720     for (i = 0; i < 5; i++)
721     {
722         hdi.lParam = i + 5;
723         hdi.iOrder = rand2[i];
724         SendMessage(hWndHeader, HDM_INSERTITEMA, rand3[i], (LPARAM)&hdi);
725         rand(); rand();
726     }
727     check_order(ids2, ord2, 10, "insert with order");
728
729     hdi.mask = HDI_ORDER;
730     for (i=0; i<10; i++)
731     {
732         hdi.iOrder = rand5[i];
733         SendMessage(hWndHeader, HDM_SETITEMA, rand4[i], (LPARAM)&hdi);
734         rand(); rand();
735     }
736     check_order(ids2, ord3, 10, "setitems changing order");
737
738     for (i=0; i<5; i++)
739         SendMessage(hWndHeader, HDM_DELETEITEM, rand6[i], 0);
740     check_order(ids4, ord4, 5, "deleteitem");
741
742     DestroyWindow(hWndHeader);
743 }
744
745 static LRESULT CALLBACK HeaderTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
746 {
747     DRAWITEMSTRUCT *di;
748     switch(msg) {
749
750     case WM_NOTIFY:
751     {
752         NMHEADERA *hdr = (NMHEADER *)lParam;
753         EXPECTEDNOTIFY *expected;
754         int i;
755
756         if (hdr->hdr.code == NM_CUSTOMDRAW)
757             if (g_CustomDrawProc)
758                 return g_CustomDrawProc(g_CustomDrawCount++, (NMCUSTOMDRAW*)hdr);
759
760         for (i=0; i<nUnexpectedNotify; i++)
761             ok(hdr->hdr.code != unexpectedNotify[i], "Received invalid notify %d\n", hdr->hdr.code);
762         
763         if (nReceivedNotify >= nExpectedNotify || hdr->hdr.hwndFrom != hWndHeader )
764             break;
765
766         expected = &expectedNotify[nReceivedNotify];
767         if (hdr->hdr.code != expected->iCode)
768             break;
769         
770         nReceivedNotify++;
771         compare_items(hdr->hdr.code, &expected->hdItem, hdr->pitem, expected->fUnicode);
772         break;
773     }
774
775     case WM_DRAWITEM:
776         di = (DRAWITEMSTRUCT *)lParam;
777         ok(g_DrawItem.CtlType != 0, "Unexpected WM_DRAWITEM\n");
778         if (g_DrawItem.CtlType == 0) return 0;
779         g_DrawItemReceived = TRUE;
780         compare(di->CtlType,   g_DrawItem.CtlType, "%d");
781         compare(di->CtlID,     g_DrawItem.CtlID, "%d");
782         compare(di->hwndItem,  g_DrawItem.hwndItem, "%p");
783         compare(di->itemID,    g_DrawItem.itemID, "%d");
784         compare(di->itemState, g_DrawItem.itemState, "%d");
785         compare(di->rcItem.left,   g_DrawItem.rcItem.left, "%d");
786         compare(di->rcItem.top,    g_DrawItem.rcItem.top, "%d");
787         compare(di->rcItem.right,  g_DrawItem.rcItem.right, "%d");
788         compare(di->rcItem.bottom, g_DrawItem.rcItem.bottom, "%d");
789         break;
790
791     case WM_DESTROY:
792         PostQuitMessage(0);
793         break;
794   
795     default:
796         return DefWindowProcA(hWnd, msg, wParam, lParam);
797     }
798     
799     return 0L;
800 }
801
802 static void init(void) {
803     WNDCLASSA wc;
804     INITCOMMONCONTROLSEX icex;
805
806     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
807     icex.dwICC  = ICC_USEREX_CLASSES;
808     InitCommonControlsEx(&icex);
809
810     wc.style = CS_HREDRAW | CS_VREDRAW;
811     wc.cbClsExtra = 0;
812     wc.cbWndExtra = 0;
813     wc.hInstance = GetModuleHandleA(NULL);
814     wc.hIcon = NULL;
815     wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_ARROW));
816     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
817     wc.lpszMenuName = NULL;
818     wc.lpszClassName = "HeaderTestClass";
819     wc.lpfnWndProc = HeaderTestWndProc;
820     RegisterClassA(&wc);
821
822     hHeaderParentWnd = CreateWindowExA(0, "HeaderTestClass", "Header test", WS_OVERLAPPEDWINDOW, 
823       CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
824     assert(hHeaderParentWnd != NULL);
825     ShowWindow(hHeaderParentWnd, SW_SHOW);
826 }
827
828 START_TEST(header)
829 {
830     init();
831
832     test_header_control();
833     test_header_order();
834     test_customdraw();
835
836     DestroyWindow(hHeaderParentWnd);
837 }