mshtml: Print wine_gecko version in load_wine_gecko.
[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 CUSTOMDRAWPROC g_CustomDrawProc;
37 int g_CustomDrawCount;
38 DRAWITEMSTRUCT g_DrawItem;
39 BOOL g_DrawItemReceived;
40
41 EXPECTEDNOTIFY expectedNotify[10];
42 INT nExpectedNotify = 0;
43 INT nReceivedNotify = 0;
44 INT unexpectedNotify[10];
45 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, LPCSTR text)
77 {
78     HDITEMA hdItem;
79     hdItem.mask       = HDI_TEXT | HDI_WIDTH;
80     hdItem.cxy        = 100;
81     hdItem.pszText    = (LPSTR)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, LPCSTR text, BOOL fCheckNotifies)
87 {
88     LONG ret;
89     HDITEMA hdexItem;
90     hdexItem.mask       = HDI_TEXT;
91     hdexItem.pszText    = (LPSTR)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, LPCSTR text, LPCWSTR wText)
105 {
106     LONG ret;
107     HDITEMA hdexItem;
108     HDITEMW hdexNotify;
109     hdexItem.mask       = HDI_TEXT;
110     hdexItem.pszText    = (LPSTR)text;
111     hdexItem.cchTextMax = 0;
112     
113     hdexNotify.mask    = HDI_TEXT;
114     hdexNotify.pszText = (LPWSTR)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, "Deleteing 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 const char *str_items[] =
211     {"First Item", "Second Item", "Third Item", "Fourth Item", "Replace Item", "Out Of Range Item"};
212     
213 static const char pszUniTestA[] = "TST";
214 static const WCHAR pszUniTestW[] = {'T','S','T',0};
215
216
217 #define TEST_GET_ITEM(i,c)\
218 {   res = getItem(hWndHeader, i, buffer);\
219     ok(res != 0, "Getting item[%d] using valid index failed unexpectedly (%d)\n", i, res);\
220     ok(strcmp(str_items[c], buffer) == 0, "Getting item[%d] returned \"%s\" expecting \"%s\"\n", i, buffer, str_items[c]);\
221 }
222
223 #define TEST_GET_ITEMCOUNT(i)\
224 {   res = getItemCount(hWndHeader);\
225     ok(res == i, "Got Item Count as %d\n", res);\
226 }
227
228 static void check_auto_format(void)
229 {
230     HDITEMA hdiCreate;
231     HDITEMA hdiRead;
232     static CHAR text[] = "Test";
233     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
234
235     /* Windows implicitly sets some format bits in INSERTITEM */
236
237     /* HDF_STRING is automatically set and cleared for no text */
238     hdiCreate.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
239     hdiCreate.pszText = text;
240     hdiCreate.cxy = 100;
241     hdiCreate.fmt=HDF_CENTER;
242     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
243     ok(hdiRead.fmt == (HDF_STRING|HDF_CENTER), "HDF_STRING not set automatically (fmt=%x)\n", hdiRead.fmt);
244
245     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
246     hdiCreate.pszText = text;
247     hdiCreate.fmt = HDF_CENTER|HDF_STRING;
248     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
249     ok(hdiRead.fmt == (HDF_CENTER), "HDF_STRING should be automatically cleared (fmt=%x)\n", hdiRead.fmt);
250
251     /* HDF_BITMAP is automatically set and cleared for a NULL bitmap or no bitmap */
252     hdiCreate.mask = HDI_BITMAP|HDI_WIDTH|HDI_FORMAT;
253     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
254     hdiCreate.fmt = HDF_CENTER;
255     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
256     ok(hdiRead.fmt == (HDF_BITMAP|HDF_CENTER), "HDF_BITMAP not set automatically (fmt=%x)\n", hdiRead.fmt);
257     DeleteObject(hdiCreate.hbm);
258
259     hdiCreate.hbm = NULL;
260     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
261     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
262     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for NULL bitmap (fmt=%x)\n", hdiRead.fmt);
263
264     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
265     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
266     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
267     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for no bitmap (fmt=%x)\n", hdiRead.fmt);
268
269     /* HDF_IMAGE is automatically set but not cleared */
270     hdiCreate.mask = HDI_IMAGE|HDI_WIDTH|HDI_FORMAT;
271     hdiCreate.iImage = 17;
272     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
273     ok(hdiRead.fmt == (HDF_IMAGE|HDF_CENTER), "HDF_IMAGE not set automatically (fmt=%x)\n", hdiRead.fmt);
274
275     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
276     hdiCreate.fmt = HDF_CENTER|HDF_IMAGE;
277     hdiCreate.iImage = 0;
278     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
279     ok(hdiRead.fmt == (HDF_CENTER|HDF_IMAGE), "HDF_IMAGE shouldn't be cleared automatically (fmt=%x)\n", hdiRead.fmt);
280 }
281
282 static void check_auto_fields(void)
283 {
284     HDITEMA hdiCreate;
285     HDITEMA hdiRead;
286     static CHAR text[] = "Test";
287     LONG res;
288
289     /* Windows stores the format, width, lparam even if they are not in the item's mask */
290     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
291     hdiCreate.mask = HDI_TEXT;
292     hdiCreate.cxy = 100;
293     hdiCreate.pszText = text;
294     addReadDelItem(hWndHeader, &hdiCreate, HDI_WIDTH, &hdiRead);
295     TEST_GET_ITEMCOUNT(6);
296     ok(hdiRead.cxy == hdiCreate.cxy, "cxy should be automatically set\n");
297
298     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
299     hdiCreate.mask = HDI_TEXT;
300     hdiCreate.pszText = text;
301     hdiCreate.lParam = 0x12345678;
302     addReadDelItem(hWndHeader, &hdiCreate, HDI_LPARAM, &hdiRead);
303     TEST_GET_ITEMCOUNT(6);
304     ok(hdiRead.lParam == hdiCreate.lParam, "lParam should be automatically set\n");
305
306     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
307     hdiCreate.mask = HDI_TEXT;
308     hdiCreate.pszText = text;
309     hdiCreate.fmt = HDF_STRING|HDF_CENTER;
310     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
311     TEST_GET_ITEMCOUNT(6);
312     ok(hdiRead.fmt == hdiCreate.fmt, "fmt should be automatically set\n");
313
314     /* others fields are not set */
315     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
316     hdiCreate.mask = HDI_TEXT;
317     hdiCreate.pszText = text;
318     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
319     addReadDelItem(hWndHeader, &hdiCreate, HDI_BITMAP, &hdiRead);
320     TEST_GET_ITEMCOUNT(6);
321     ok(hdiRead.hbm == NULL, "hbm should not be automatically set\n");
322     DeleteObject(hdiCreate.hbm);
323
324     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
325     hdiCreate.mask = HDI_IMAGE;
326     hdiCreate.iImage = 17;
327     hdiCreate.pszText = text;
328     addReadDelItem(hWndHeader, &hdiCreate, HDI_TEXT, &hdiRead);
329     TEST_GET_ITEMCOUNT(6);
330     ok(hdiRead.pszText==NULL, "pszText shouldn't be automatically set\n");
331
332     /* field from comctl >4.0 not tested as the system probably won't touch them */
333 }
334
335 static void check_mask(void)
336 {
337     HDITEMA hdi;
338     static CHAR text[] = "ABC";
339     LRESULT ret;
340
341     /* don't create items if the mask is zero */
342     ZeroMemory(&hdi, sizeof(hdi));
343     hdi.mask = 0;
344     hdi.cxy = 200;
345     hdi.pszText = text;
346     hdi.fmt = 0;
347     hdi.iOrder = 0;
348     hdi.lParam = 17;
349     hdi.cchTextMax = 260;
350     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
351     ok(ret == -1, "Creating an item with a zero mask should have failed\n");
352     if (ret != -1) SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
353
354     /* with a non-zero mask creation will succeed */
355     ZeroMemory(&hdi, sizeof(hdi));
356     hdi.mask = HDI_LPARAM;
357     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
358     ok(ret != -1, "Adding item with non-zero mask failed\n");
359     if (ret != -1)
360         SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
361
362     /* in SETITEM if the mask contains a unknown bit, it is ignored */
363     ZeroMemory(&hdi, sizeof(hdi));
364     hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
365     hdi.lParam = 133;
366     hdi.iImage = 17;
367     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
368     ok(ret != -1, "Adding item failed\n");
369
370     if (ret != -1)
371     {
372         /* check result */
373         ZeroMemory(&hdi, sizeof(hdi));
374         hdi.mask = HDI_LPARAM | HDI_IMAGE;
375         SendMessage(hWndHeader, HDM_GETITEM, (WPARAM)0, (LPARAM)&hdi);
376         ok(hdi.lParam == 133, "comctl32 4.0 field not set\n");
377         ok(hdi.iImage == 17, "comctl32 >4.0 field not set\n");
378
379         /* but in GETITEM if an unknown bit is set, comctl32 uses only version 4.0 fields */
380         ZeroMemory(&hdi, sizeof(hdi));
381         hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
382         SendMessage(hWndHeader, HDM_GETITEM, (WPARAM)0, (LPARAM)&hdi);
383         ok(hdi.lParam == 133, "comctl32 4.0 field not read\n");
384         ok(hdi.iImage == 0, "comctl32 >4.0 field shouldn't be read\n");
385
386         SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
387     }
388 }
389
390 static void test_header_control (void)
391 {
392     LONG res;
393     static char buffer[MAX_CHARS];
394     int i;
395
396     hWndHeader = create_header_control ();
397
398     for (i = 3; i >= 0; i--)
399     {
400         TEST_GET_ITEMCOUNT(3-i);
401         res = addItem(hWndHeader, 0, str_items[i]);
402         ok(res == 0, "Adding simple item failed (%d)\n", res);
403     }
404
405     TEST_GET_ITEMCOUNT(4);
406     res = addItem(hWndHeader, 99, str_items[i+1]);
407     ok(res != -1, "Adding Out of Range item should fail with -1 got (%d)\n", res);
408     TEST_GET_ITEMCOUNT(5);
409     res = addItem(hWndHeader, 5, str_items[i+1]);
410     ok(res != -1, "Adding Out of Range item should fail with -1 got (%d)\n", res);
411     TEST_GET_ITEMCOUNT(6);
412
413     for (i = 0; i < 4; i++) { TEST_GET_ITEM(i,i); TEST_GET_ITEMCOUNT(6); }
414
415     res=getItem(hWndHeader, 99, buffer);
416     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
417     res=getItem(hWndHeader, 5, buffer);
418     ok(res == 1, "Getting Out of Range item should fail with 1 (%d), got %s\n", res,buffer);
419     res=getItem(hWndHeader, -2, buffer);
420     ok(res == 0, "Getting Out of Range item should fail with 0 (%d), got %s\n", res,buffer);
421
422     if (winetest_interactive)
423     {
424         UpdateWindow(hHeaderParentWnd);
425         UpdateWindow(hWndHeader);
426     }
427
428     TEST_GET_ITEMCOUNT(6);
429     res=setItem(hWndHeader, 99, str_items[5], FALSE);
430     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
431     res=setItem(hWndHeader, 5, str_items[5], TRUE);
432     ok(res == 1, "Setting Out of Range item should fail with 1 (%d)\n", res);
433     res=setItem(hWndHeader, -2, str_items[5], FALSE);
434     ok(res == 0, "Setting Out of Range item should fail with 0 (%d)\n", res);
435     TEST_GET_ITEMCOUNT(6);
436
437     for (i = 0; i < 4; i++)
438     {
439         res = setItem(hWndHeader, i, str_items[4], TRUE);
440         ok(res != 0, "Setting %d item failed (%d)\n", i+1, res);
441         TEST_GET_ITEM(i, 4);
442         TEST_GET_ITEMCOUNT(6);
443     }
444     
445     SendMessageA(hWndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, 0);
446     setItemUnicodeNotify(hWndHeader, 3, pszUniTestA, pszUniTestW);
447     SendMessageA(hWndHeader, WM_NOTIFYFORMAT, (WPARAM)hHeaderParentWnd, (LPARAM)NF_REQUERY);
448     setItem(hWndHeader, 3, str_items[4], TRUE);
449     
450     dont_expect_notify(HDN_GETDISPINFOA);
451     dont_expect_notify(HDN_GETDISPINFOW);
452     addItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA);
453     setItem(hWndHeader, 0, str_items[4], TRUE);
454     /* unexpected notifies cleared by notifies_received in setItem */
455     dont_expect_notify(HDN_GETDISPINFOA);
456     dont_expect_notify(HDN_GETDISPINFOW);
457     setItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA, TRUE);
458     /* unexpected notifies cleared by notifies_received in setItem */
459     delItem(hWndHeader, 0);
460
461     check_auto_format();
462     TEST_GET_ITEMCOUNT(6);
463     check_auto_fields();
464     TEST_GET_ITEMCOUNT(6);
465     check_mask();
466     TEST_GET_ITEMCOUNT(6);
467
468     res = delItem(hWndHeader, 5);
469     ok(res == 1, "Deleting Out of Range item should fail with 1 (%d)\n", res);
470     res = delItem(hWndHeader, -2);
471     ok(res == 0, "Deleting Out of Range item should fail with 0 (%d)\n", res);
472     TEST_GET_ITEMCOUNT(5);
473
474     res = delItem(hWndHeader, 3);
475     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
476     TEST_GET_ITEMCOUNT(4);
477     res = delItem(hWndHeader, 0);
478     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
479     TEST_GET_ITEMCOUNT(3);
480     res = delItem(hWndHeader, 0);
481     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
482     TEST_GET_ITEMCOUNT(2);
483     res = delItem(hWndHeader, 0);
484     ok(res != 0, "Deleting using out of range index failed (%d)\n", res);
485     TEST_GET_ITEMCOUNT(1);
486
487     DestroyWindow(hWndHeader);
488 }
489
490
491 #define TEST_NMCUSTOMDRAW(draw_stage, item_spec, lparam, _left, _top, _right, _bottom) \
492     ok(nm->dwDrawStage == draw_stage, "Invalid dwDrawStage %d vs %d\n", draw_stage, nm->dwDrawStage); \
493     if (item_spec != -1) \
494         ok(nm->dwItemSpec == item_spec, "Invalid dwItemSpec %d vs %ld\n", item_spec, nm->dwItemSpec); \
495     ok(nm->lItemlParam == lparam, "Invalid lItemlParam %d vs %ld\n", lparam, nm->lItemlParam); \
496     ok(nm->rc.top == _top && nm->rc.bottom == _bottom && nm->rc.left == _left && nm->rc.right == _right, \
497         "Invalid rect (%d,%d) (%d,%d) vs (%d,%d) (%d,%d)\n", _left, _top, _right, _bottom, \
498         nm->rc.left, nm->rc.top, nm->rc.right, nm->rc.bottom);
499
500 static LRESULT customdraw_1(int n, NMCUSTOMDRAW *nm)
501 {
502     if (nm == NULL) {  /* test ended */
503         ok(n==1, "NM_CUSTOMDRAW messages: %d, expected: 1\n", n);
504         return 0;
505     }
506
507     switch (n)
508     {
509     case 0:
510         /* don't test dwItemSpec - it's 0 no comctl5 but 1308756 on comctl6 */
511         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
512         return 0;
513     }
514
515     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
516     return -1;
517 }
518
519 static LRESULT customdraw_2(int n, NMCUSTOMDRAW *nm)
520 {
521     if (nm == NULL) {  /* test ended */
522         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
523         return 0;
524     }
525
526     switch (n)
527     {
528     case 0:
529         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
530         return CDRF_NOTIFYITEMDRAW;
531     case 1:
532         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, 18);
533         return 0;
534     case 2:
535         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, 18);
536         return 0;
537     case 3:
538         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, 18);
539         return 0;
540     }
541
542     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
543     return 0;
544 }
545
546 static LRESULT customdraw_3(int n, NMCUSTOMDRAW *nm)
547 {
548     if (nm == NULL) {  /* test ended */
549         ok(n==5, "NM_CUSTOMDRAW messages: %d, expected: 5\n", n);
550         return 0;
551     }
552
553     switch (n)
554     {
555     case 0:
556         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
557         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTERASE|CDRF_NOTIFYPOSTPAINT|CDRF_SKIPDEFAULT;
558     case 1:
559         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, 18);
560         return 0;
561     case 2:
562         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 1, 5, 50, 0, 150, 18);
563         return 0;
564     case 3:
565         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, 18);
566         return 0;
567     case 4:
568         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, 18);
569         return 0;
570     }
571
572     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
573     return 0;
574 }
575
576
577 static LRESULT customdraw_4(int n, NMCUSTOMDRAW *nm)
578 {
579     if (nm == NULL) {  /* test ended */
580         ok(n==4, "NM_CUSTOMDRAW messages: %d, expected: 4\n", n);
581         return 0;
582     }
583
584     switch (n)
585     {
586     case 0:
587         TEST_NMCUSTOMDRAW(CDDS_PREPAINT, -1, 0, 0, 0, 670, 18);
588         return CDRF_NOTIFYITEMDRAW|CDRF_NOTIFYPOSTPAINT;
589     case 1:
590         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 0, 0, 0, 0, 50, 18);
591         return 0;
592     case 2:
593         TEST_NMCUSTOMDRAW(CDDS_ITEMPREPAINT, 2, 10, 150, 0, 300, 18);
594         return 0;
595     case 3:
596         TEST_NMCUSTOMDRAW(CDDS_POSTPAINT, -1, 0, 0, 0, 670, 18);
597         return 0;
598     }
599
600     ok(FALSE, "To many custom draw messages (n=%d, nm->dwDrawStage=%d)\n", n, nm->dwDrawStage);
601     return 0;
602 }
603
604 static void run_customdraw_scenario(CUSTOMDRAWPROC proc)
605 {
606     g_CustomDrawProc = proc;
607     g_CustomDrawCount = 0;
608     InvalidateRect(hWndHeader, NULL, TRUE);
609     UpdateWindow(hWndHeader);
610     proc(g_CustomDrawCount, NULL);
611     g_CustomDrawProc = NULL;
612 }
613
614 void test_customdraw()
615 {
616     int i;
617     HDITEM item;
618     RECT rect;
619     CHAR name[] = "Test";
620     hWndHeader = create_header_control();
621     GetClientRect(hWndHeader, &rect);
622     ok(rect.right - rect.left == 670 && rect.bottom - rect.top == 18,
623         "Tests will fail as header size is %dx%d instead of 670x18\n",
624         rect.right - rect.left == 670, rect.bottom - rect.top == 18);
625
626     for (i = 0; i < 3; i++)
627     {
628         ZeroMemory(&item, sizeof(item));
629         item.mask = HDI_TEXT|HDI_WIDTH;
630         item.cxy = 50*(i+1);
631         item.pszText = name;
632         item.lParam = i*5;
633         SendMessage(hWndHeader, HDM_INSERTITEM, i, (LPARAM)&item);
634     }
635
636     run_customdraw_scenario(customdraw_1);
637     run_customdraw_scenario(customdraw_2);
638     run_customdraw_scenario(customdraw_3);
639
640     ZeroMemory(&item, sizeof(item));
641     item.mask = HDI_FORMAT;
642     item.fmt = HDF_OWNERDRAW;
643     SendMessage(hWndHeader, HDM_SETITEM, 1, (LPARAM)&item);
644     g_DrawItem.CtlID = 0;
645     g_DrawItem.CtlType = ODT_HEADER;
646     g_DrawItem.hwndItem = hWndHeader;
647     g_DrawItem.itemID = 1;
648     g_DrawItem.itemState = 0;
649     SendMessage(hWndHeader, HDM_GETITEMRECT, 1, (LPARAM)&g_DrawItem.rcItem);
650     run_customdraw_scenario(customdraw_4);
651     ok(g_DrawItemReceived, "WM_DRAWITEM not received\n");
652     DestroyWindow(hWndHeader);
653     hWndHeader = NULL;
654     g_DrawItem.CtlType = 0;
655     g_DrawItemReceived = FALSE;
656 }
657
658 static void check_order(const int expected_id[], const int expected_order[],
659                         int count, const char *type)
660 {
661     int i;
662     HDITEMA hdi;
663
664     ok(getItemCount(hWndHeader) == count, "Invalid item count in order tests\n");
665     for (i = 0; i < count; i++)
666     {
667         hdi.mask = HDI_LPARAM|HDI_ORDER;
668         SendMessage(hWndHeader, HDM_GETITEMA, i, (LPARAM)&hdi);
669         ok(hdi.lParam == expected_id[i],
670             "Invalid item ids after '%s'- item %d has lParam %d\n", type, i, (int)hdi.lParam);
671         ok(hdi.iOrder == expected_order[i],
672             "Invalid item order after '%s'- item %d has iOrder %d\n", type, i, hdi.iOrder);
673     }
674 }
675
676 static void test_header_order (void)
677 {
678     const int rand1[] = {0, 1, 1, 0, 4};
679     const int rand2[] = {4, 5, 6, 7, 4};
680     const int rand3[] = {5, 5, 1, 6, 1};
681     const int rand4[] = {1, 5, 2, 7, 6, 1, 4, 2, 3, 2};
682     const int rand5[] = {7, 8, 5, 6, 7, 2, 1, 9, 10, 10};
683     const int rand6[] = {2, 8, 3, 4, 0};
684
685     const int ids1[] = {3, 0, 2, 1, 4};
686     const int ord1[] = {0, 1, 2, 3, 4};
687     const int ids2[] = {3, 9, 7, 0, 2, 1, 4, 8, 6, 5};
688     const int ord2[] = {0, 4, 7, 1, 2, 3, 9, 8, 6, 5};
689     const int ord3[] = {0, 3, 9, 2, 1, 8, 7, 6, 5, 4};
690     const int ids4[] = {9, 0, 1, 8, 6};
691     const int ord4[] = {1, 0, 4, 3, 2};
692
693     char buffer[20];
694     HDITEMA hdi;
695     int i;
696
697     hWndHeader = create_header_control();
698
699     ZeroMemory(&hdi, sizeof(HDITEMA));
700     hdi.mask = HDI_TEXT | HDI_LPARAM;
701     hdi.pszText = buffer;
702     strcpy(buffer, "test");
703
704     for (i = 0; i < 5; i++)
705     {
706         hdi.lParam = i;
707         SendMessage(hWndHeader, HDM_INSERTITEMA, rand1[i], (LPARAM)&hdi);
708         rand();
709     }
710     check_order(ids1, ord1, 5, "insert without iOrder");
711
712     hdi.mask |= HDI_ORDER;
713     for (i = 0; i < 5; i++)
714     {
715         hdi.lParam = i + 5;
716         hdi.iOrder = rand2[i];
717         SendMessage(hWndHeader, HDM_INSERTITEMA, rand3[i], (LPARAM)&hdi);
718         rand(); rand();
719     }
720     check_order(ids2, ord2, 10, "insert with order");
721
722     hdi.mask = HDI_ORDER;
723     for (i=0; i<10; i++)
724     {
725         hdi.iOrder = rand5[i];
726         SendMessage(hWndHeader, HDM_SETITEMA, rand4[i], (LPARAM)&hdi);
727         rand(); rand();
728     }
729     check_order(ids2, ord3, 10, "setitems changing order");
730
731     for (i=0; i<5; i++)
732         SendMessage(hWndHeader, HDM_DELETEITEM, rand6[i], 0);
733     check_order(ids4, ord4, 5, "deleteitem");
734
735     DestroyWindow(hWndHeader);
736 }
737
738 LRESULT CALLBACK HeaderTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
739 {
740     DRAWITEMSTRUCT *di;
741     switch(msg) {
742
743     case WM_NOTIFY:
744     {
745         NMHEADERA *hdr = (NMHEADER *)lParam;
746         EXPECTEDNOTIFY *expected;
747         int i;
748
749         if (hdr->hdr.code == NM_CUSTOMDRAW)
750             if (g_CustomDrawProc)
751                 return g_CustomDrawProc(g_CustomDrawCount++, (NMCUSTOMDRAW*)hdr);
752
753         for (i=0; i<nUnexpectedNotify; i++)
754             ok(hdr->hdr.code != unexpectedNotify[i], "Received invalid notify %d\n", hdr->hdr.code);
755         
756         if (nReceivedNotify >= nExpectedNotify || hdr->hdr.hwndFrom != hWndHeader )
757             break;
758
759         expected = &expectedNotify[nReceivedNotify];
760         if (hdr->hdr.code != expected->iCode)
761             break;
762         
763         nReceivedNotify++;
764         compare_items(hdr->hdr.code, &expected->hdItem, hdr->pitem, expected->fUnicode);
765         break;
766     }
767
768     case WM_DRAWITEM:
769         di = (DRAWITEMSTRUCT *)lParam;
770         ok(g_DrawItem.CtlType != 0, "Unexpected WM_DRAWITEM\n");
771         if (g_DrawItem.CtlType == 0) return 0;
772         g_DrawItemReceived = TRUE;
773         compare(di->CtlType,   g_DrawItem.CtlType, "%d");
774         compare(di->CtlID,     g_DrawItem.CtlID, "%d");
775         compare(di->hwndItem,  g_DrawItem.hwndItem, "%p");
776         compare(di->itemID,    g_DrawItem.itemID, "%d");
777         compare(di->itemState, g_DrawItem.itemState, "%d");
778         compare(di->rcItem.left,   g_DrawItem.rcItem.left, "%d");
779         compare(di->rcItem.top,    g_DrawItem.rcItem.top, "%d");
780         compare(di->rcItem.right,  g_DrawItem.rcItem.right, "%d");
781         compare(di->rcItem.bottom, g_DrawItem.rcItem.bottom, "%d");
782         break;
783
784     case WM_DESTROY:
785         PostQuitMessage(0);
786         break;
787   
788     default:
789         return DefWindowProcA(hWnd, msg, wParam, lParam);
790     }
791     
792     return 0L;
793 }
794
795 static void init(void) {
796     WNDCLASSA wc;
797     INITCOMMONCONTROLSEX icex;
798
799     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
800     icex.dwICC  = ICC_USEREX_CLASSES;
801     InitCommonControlsEx(&icex);
802
803     wc.style = CS_HREDRAW | CS_VREDRAW;
804     wc.cbClsExtra = 0;
805     wc.cbWndExtra = 0;
806     wc.hInstance = GetModuleHandleA(NULL);
807     wc.hIcon = NULL;
808     wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_ARROW));
809     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
810     wc.lpszMenuName = NULL;
811     wc.lpszClassName = "HeaderTestClass";
812     wc.lpfnWndProc = HeaderTestWndProc;
813     RegisterClassA(&wc);
814
815     hHeaderParentWnd = CreateWindowExA(0, "HeaderTestClass", "Header test", WS_OVERLAPPEDWINDOW, 
816       CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
817     assert(hHeaderParentWnd != NULL);
818     ShowWindow(hHeaderParentWnd, SW_SHOW);
819 }
820
821 START_TEST(header)
822 {
823     init();
824
825     test_header_control();
826     test_header_order();
827     test_customdraw();
828
829     DestroyWindow(hHeaderParentWnd);
830 }