janitorial: Remove remaining NULL checks before free() (found by Smatch).
[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 EXPECTEDNOTIFY expectedNotify[10];
35 INT nExpectedNotify = 0;
36 INT nReceivedNotify = 0;
37 INT unexpectedNotify[10];
38 INT nUnexpectedNotify = 0;
39
40 static HWND hHeaderParentWnd;
41 static HWND hWndHeader;
42 #define MAX_CHARS 100
43
44 static void expect_notify(INT iCode, BOOL fUnicode, HDITEMA *lpItem)
45 {
46     assert(nExpectedNotify < 10);
47     expectedNotify[nExpectedNotify].iCode = iCode;
48     expectedNotify[nExpectedNotify].fUnicode = fUnicode;
49     expectedNotify[nExpectedNotify].hdItem = *lpItem;
50     nExpectedNotify++;
51 }
52
53 static void dont_expect_notify(INT iCode)
54 {
55     assert(nUnexpectedNotify < 10);
56     unexpectedNotify[nUnexpectedNotify++] = iCode;
57 }
58
59 static BOOL notifies_received(void)
60 {
61     BOOL fRet = (nExpectedNotify == nReceivedNotify);
62     nExpectedNotify = nReceivedNotify = 0;
63     nUnexpectedNotify = 0;
64     return fRet;
65 }
66
67 static LONG addItem(HWND hdex, int idx, LPCSTR text)
68 {
69     HDITEMA hdItem;
70     hdItem.mask       = HDI_TEXT | HDI_WIDTH;
71     hdItem.cxy        = 100;
72     hdItem.pszText    = (LPSTR)text;
73     hdItem.cchTextMax = 0;
74     return (LONG)SendMessage(hdex, HDM_INSERTITEMA, (WPARAM)idx, (LPARAM)&hdItem);
75 }
76
77 static LONG setItem(HWND hdex, int idx, LPCSTR text, BOOL fCheckNotifies)
78 {
79     LONG ret;
80     HDITEMA hdexItem;
81     hdexItem.mask       = HDI_TEXT;
82     hdexItem.pszText    = (LPSTR)text;
83     hdexItem.cchTextMax = 0;
84     if (fCheckNotifies)
85     {
86         expect_notify(HDN_ITEMCHANGINGA, FALSE, &hdexItem);
87         expect_notify(HDN_ITEMCHANGEDA, FALSE, &hdexItem);
88     }
89     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
90     if (fCheckNotifies)
91         ok(notifies_received(), "setItem(): not all expected notifies were received\n");
92     return ret;
93 }
94
95 static LONG setItemUnicodeNotify(HWND hdex, int idx, LPCSTR text, LPCWSTR wText)
96 {
97     LONG ret;
98     HDITEMA hdexItem;
99     HDITEMW hdexNotify;
100     hdexItem.mask       = HDI_TEXT;
101     hdexItem.pszText    = (LPSTR)text;
102     hdexItem.cchTextMax = 0;
103     
104     hdexNotify.mask    = HDI_TEXT;
105     hdexNotify.pszText = (LPWSTR)wText;
106     
107     expect_notify(HDN_ITEMCHANGINGW, TRUE, (HDITEMA*)&hdexNotify);
108     expect_notify(HDN_ITEMCHANGEDW, TRUE, (HDITEMA*)&hdexNotify);
109     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
110     ok(notifies_received(), "setItemUnicodeNotify(): not all expected notifies were received\n");
111     return ret;
112 }
113
114 static LONG delItem(HWND hdex, int idx)
115 {
116     return (LONG)SendMessage(hdex, HDM_DELETEITEM, (WPARAM)idx, 0);
117 }
118
119 static LONG getItemCount(HWND hdex)
120 {
121     return (LONG)SendMessage(hdex, HDM_GETITEMCOUNT, 0, 0);
122 }
123
124 static LONG getItem(HWND hdex, int idx, LPSTR textBuffer)
125 {
126     HDITEMA hdItem;
127     hdItem.mask         = HDI_TEXT;
128     hdItem.pszText      = textBuffer;
129     hdItem.cchTextMax   = MAX_CHARS;
130     return (LONG)SendMessage(hdex, HDM_GETITEMA, (WPARAM)idx, (LPARAM)&hdItem);
131 }
132
133 static void addReadDelItem(HWND hdex, HDITEMA *phdiCreate, int maskRead, HDITEMA *phdiRead)
134 {
135     ok(SendMessage(hdex, HDM_INSERTITEMA, (WPARAM)0, (LPARAM)phdiCreate)!=-1, "Adding item failed\n");
136     ZeroMemory(phdiRead, sizeof(HDITEMA));
137     phdiRead->mask = maskRead;
138     ok(SendMessage(hdex, HDM_GETITEMA, (WPARAM)0, (LPARAM)phdiRead)!=0, "Getting item data failed\n");
139     ok(SendMessage(hdex, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0)!=0, "Deleteing item failed\n");
140 }
141
142 static HWND create_header_control (void)
143 {
144     HWND handle;
145     HDLAYOUT hlayout;
146     RECT rectwin;
147     WINDOWPOS winpos;
148
149     handle = CreateWindowEx(0, WC_HEADER, NULL,
150                             WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
151                             0, 0, 0, 0,
152                             hHeaderParentWnd, NULL, NULL, NULL);
153     assert(handle);
154
155     if (winetest_interactive)
156         ShowWindow (hHeaderParentWnd, SW_SHOW);
157
158     GetClientRect(hHeaderParentWnd,&rectwin);
159     hlayout.prc = &rectwin;
160     hlayout.pwpos = &winpos;
161     SendMessageA(handle,HDM_LAYOUT,0,(LPARAM) &hlayout);
162     SetWindowPos(handle, winpos.hwndInsertAfter, winpos.x, winpos.y, 
163                  winpos.cx, winpos.cy, 0);
164
165     return handle;
166 }
167
168 static void compare_items(INT iCode, HDITEMA *hdi1, HDITEMA *hdi2, BOOL fUnicode)
169 {
170     ok(hdi1->mask == hdi2->mask, "Notify %d mask mismatch (%08x != %08x)\n", iCode, hdi1->mask, hdi2->mask);
171     if (hdi1->mask & HDI_WIDTH)
172     {
173         ok(hdi1->cxy == hdi2->cxy, "Notify %d cxy mismatch (%08x != %08x)\n", iCode, hdi1->cxy, hdi2->cxy);
174     }
175     if (hdi1->mask & HDI_TEXT)
176     {
177         if (hdi1->pszText == LPSTR_TEXTCALLBACKA)
178         {
179             ok(hdi1->pszText == LPSTR_TEXTCALLBACKA, "Notify %d - only one item is LPSTR_TEXTCALLBACK\n", iCode);
180         }
181         else
182         if (fUnicode)
183         {
184             char buf1[260];
185             char buf2[260];
186             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi1->pszText, -1, buf1, 260, NULL, NULL);
187             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi2->pszText, -1, buf2, 260, NULL, NULL);
188             ok(lstrcmpW((LPWSTR)hdi1->pszText, (LPWSTR)hdi2->pszText)==0,
189                 "Notify %d text mismatch (L\"%s\" vs L\"%s\")\n",
190                     iCode, buf1, buf2);
191         }
192         else
193         {
194             ok(strcmp(hdi1->pszText, hdi2->pszText)==0,
195                 "Notify %d text mismatch (\"%s\" vs \"%s\")\n",
196                     iCode, hdi1->pszText, hdi2->pszText);
197             }
198     }
199 }
200
201 static const char *str_items[] =
202     {"First Item", "Second Item", "Third Item", "Fourth Item", "Replace Item", "Out Of Range Item"};
203     
204 static const char pszUniTestA[] = "TST";
205 static const WCHAR pszUniTestW[] = {'T','S','T',0};
206
207
208 #define TEST_GET_ITEM(i,c)\
209 {   res = getItem(hWndHeader, i, buffer);\
210     ok(res != 0, "Getting item[%d] using valid index failed unexpectedly (%ld)\n", i, res);\
211     ok(strcmp(str_items[c], buffer) == 0, "Getting item[%d] returned \"%s\" expecting \"%s\"\n", i, buffer, str_items[c]);\
212 }
213
214 #define TEST_GET_ITEMCOUNT(i)\
215 {   res = getItemCount(hWndHeader);\
216     ok(res == i, "Got Item Count as %ld\n", res);\
217 }
218
219 static void check_auto_format(void)
220 {
221     HDITEMA hdiCreate;
222     HDITEMA hdiRead;
223     static CHAR text[] = "Test";
224     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
225
226     /* Windows implicitly sets some format bits in INSERTITEM */
227
228     /* HDF_STRING is automatically set and cleared for no text */
229     hdiCreate.mask = HDI_TEXT|HDI_WIDTH|HDI_FORMAT;
230     hdiCreate.pszText = text;
231     hdiCreate.cxy = 100;
232     hdiCreate.fmt=HDF_CENTER;
233     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
234     ok(hdiRead.fmt == (HDF_STRING|HDF_CENTER), "HDF_STRING not set automatically (fmt=%x)\n", hdiRead.fmt);
235
236     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
237     hdiCreate.pszText = text;
238     hdiCreate.fmt = HDF_CENTER|HDF_STRING;
239     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
240     ok(hdiRead.fmt == (HDF_CENTER), "HDF_STRING should be automatically cleared (fmt=%x)\n", hdiRead.fmt);
241
242     /* HDF_BITMAP is automatically set and cleared for a NULL bitmap or no bitmap */
243     hdiCreate.mask = HDI_BITMAP|HDI_WIDTH|HDI_FORMAT;
244     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
245     hdiCreate.fmt = HDF_CENTER;
246     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
247     ok(hdiRead.fmt == (HDF_BITMAP|HDF_CENTER), "HDF_BITMAP not set automatically (fmt=%x)\n", hdiRead.fmt);
248     DeleteObject(hdiCreate.hbm);
249
250     hdiCreate.hbm = NULL;
251     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
252     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
253     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for NULL bitmap (fmt=%x)\n", hdiRead.fmt);
254
255     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
256     hdiCreate.fmt = HDF_CENTER|HDF_BITMAP;
257     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
258     ok(hdiRead.fmt == HDF_CENTER, "HDF_BITMAP not cleared automatically for no bitmap (fmt=%x)\n", hdiRead.fmt);
259
260     /* HDF_IMAGE is automatically set but not cleared */
261     hdiCreate.mask = HDI_IMAGE|HDI_WIDTH|HDI_FORMAT;
262     hdiCreate.iImage = 17;
263     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
264     ok(hdiRead.fmt == (HDF_IMAGE|HDF_CENTER), "HDF_IMAGE not set automatically (fmt=%x)\n", hdiRead.fmt);
265
266     hdiCreate.mask = HDI_WIDTH|HDI_FORMAT;
267     hdiCreate.fmt = HDF_CENTER|HDF_IMAGE;
268     hdiCreate.iImage = 0;
269     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
270     ok(hdiRead.fmt == (HDF_CENTER|HDF_IMAGE), "HDF_IMAGE shouldn't be cleared automatically (fmt=%x)\n", hdiRead.fmt);
271 }
272
273 static void check_auto_fields(void)
274 {
275     HDITEMA hdiCreate;
276     HDITEMA hdiRead;
277     static CHAR text[] = "Test";
278     LRESULT res;
279
280     /* Windows stores the format, width, lparam even if they are not in the item's mask */
281     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
282     hdiCreate.mask = HDI_TEXT;
283     hdiCreate.cxy = 100;
284     hdiCreate.pszText = text;
285     addReadDelItem(hWndHeader, &hdiCreate, HDI_WIDTH, &hdiRead);
286     TEST_GET_ITEMCOUNT(6);
287     ok(hdiRead.cxy == hdiCreate.cxy, "cxy should be automatically set\n");
288
289     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
290     hdiCreate.mask = HDI_TEXT;
291     hdiCreate.pszText = text;
292     hdiCreate.lParam = 0x12345678;
293     addReadDelItem(hWndHeader, &hdiCreate, HDI_LPARAM, &hdiRead);
294     TEST_GET_ITEMCOUNT(6);
295     ok(hdiRead.lParam == hdiCreate.lParam, "lParam should be automatically set\n");
296
297     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
298     hdiCreate.mask = HDI_TEXT;
299     hdiCreate.pszText = text;
300     hdiCreate.fmt = HDF_STRING|HDF_CENTER;
301     addReadDelItem(hWndHeader, &hdiCreate, HDI_FORMAT, &hdiRead);
302     TEST_GET_ITEMCOUNT(6);
303     ok(hdiRead.fmt == hdiCreate.fmt, "fmt should be automatically set\n");
304
305     /* others fields are not set */
306     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
307     hdiCreate.mask = HDI_TEXT;
308     hdiCreate.pszText = text;
309     hdiCreate.hbm = CreateBitmap(16, 16, 1, 8, NULL);
310     addReadDelItem(hWndHeader, &hdiCreate, HDI_BITMAP, &hdiRead);
311     TEST_GET_ITEMCOUNT(6);
312     ok(hdiRead.hbm == NULL, "hbm should not be automatically set\n");
313     DeleteObject(hdiCreate.hbm);
314
315     ZeroMemory(&hdiCreate, sizeof(HDITEMA));
316     hdiCreate.mask = HDI_IMAGE;
317     hdiCreate.iImage = 17;
318     hdiCreate.pszText = text;
319     addReadDelItem(hWndHeader, &hdiCreate, HDI_TEXT, &hdiRead);
320     TEST_GET_ITEMCOUNT(6);
321     ok(hdiRead.pszText==NULL, "pszText shouldn't be automatically set\n");
322
323     /* field from comctl >4.0 not tested as the system probably won't touch them */
324 }
325
326 static void check_mask(void)
327 {
328     HDITEMA hdi;
329     static CHAR text[] = "ABC";
330     LRESULT ret;
331
332     /* don't create items if the mask is zero */
333     ZeroMemory(&hdi, sizeof(hdi));
334     hdi.mask = 0;
335     hdi.cxy = 200;
336     hdi.pszText = text;
337     hdi.fmt = 0;
338     hdi.iOrder = 0;
339     hdi.lParam = 17;
340     hdi.cchTextMax = 260;
341     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
342     ok(ret == -1, "Creating an item with a zero mask should have failed\n");
343     if (ret != -1) SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
344
345     /* with a non-zero mask creation will succeed */
346     ZeroMemory(&hdi, sizeof(hdi));
347     hdi.mask = HDI_LPARAM;
348     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
349     ok(ret != -1, "Adding item with non-zero mask failed\n");
350     if (ret != -1)
351         SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
352
353     /* in SETITEM if the mask contains a unknown bit, it is ignored */
354     ZeroMemory(&hdi, sizeof(hdi));
355     hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
356     hdi.lParam = 133;
357     hdi.iImage = 17;
358     ret = SendMessage(hWndHeader, HDM_INSERTITEM, (WPARAM)0, (LPARAM)&hdi);
359     ok(ret != -1, "Adding item failed\n");
360
361     if (ret != -1)
362     {
363         /* check result */
364         ZeroMemory(&hdi, sizeof(hdi));
365         hdi.mask = HDI_LPARAM | HDI_IMAGE;
366         SendMessage(hWndHeader, HDM_GETITEM, (WPARAM)0, (LPARAM)&hdi);
367         ok(hdi.lParam == 133, "comctl32 4.0 field not set\n");
368         ok(hdi.iImage == 17, "comctl32 >4.0 field not set\n");
369
370         /* but in GETITEM if an unknown bit is set, comctl32 uses only version 4.0 fields */
371         ZeroMemory(&hdi, sizeof(hdi));
372         hdi.mask = 0x08000000 | HDI_LPARAM | HDI_IMAGE;
373         SendMessage(hWndHeader, HDM_GETITEM, (WPARAM)0, (LPARAM)&hdi);
374         ok(hdi.lParam == 133, "comctl32 4.0 field not read\n");
375         ok(hdi.iImage == 0, "comctl32 >4.0 field shouldn't be read\n");
376
377         SendMessage(hWndHeader, HDM_DELETEITEM, (WPARAM)0, (LPARAM)0);
378     }
379 }
380
381 static void test_header_control (void)
382 {
383     LONG res;
384     static char buffer[MAX_CHARS];
385     int i;
386
387     hWndHeader = create_header_control ();
388
389     for (i = 3; i >= 0; i--)
390     {
391         TEST_GET_ITEMCOUNT(3-i);
392         res = addItem(hWndHeader, 0, str_items[i]);
393         ok(res == 0, "Adding simple item failed (%ld)\n", res);
394     }
395
396     TEST_GET_ITEMCOUNT(4);
397     res = addItem(hWndHeader, 99, str_items[i+1]);
398     ok(res != -1, "Adding Out of Range item should fail with -1 got (%ld)\n", res);
399     TEST_GET_ITEMCOUNT(5);
400     res = addItem(hWndHeader, 5, str_items[i+1]);
401     ok(res != -1, "Adding Out of Range item should fail with -1 got (%ld)\n", res);
402     TEST_GET_ITEMCOUNT(6);
403
404     for (i = 0; i < 4; i++) { TEST_GET_ITEM(i,i); TEST_GET_ITEMCOUNT(6); }
405
406     res=getItem(hWndHeader, 99, buffer);
407     ok(res == 0, "Getting Out of Range item should fail with 0 (%ld), got %s\n", res,buffer);
408     res=getItem(hWndHeader, 5, buffer);
409     ok(res == 1, "Getting Out of Range item should fail with 1 (%ld), got %s\n", res,buffer);
410     res=getItem(hWndHeader, -2, buffer);
411     ok(res == 0, "Getting Out of Range item should fail with 0 (%ld), got %s\n", res,buffer);
412
413     if (winetest_interactive)
414     {
415         UpdateWindow(hHeaderParentWnd);
416         UpdateWindow(hWndHeader);
417     }
418
419     TEST_GET_ITEMCOUNT(6);
420     res=setItem(hWndHeader, 99, str_items[5], FALSE);
421     ok(res == 0, "Setting Out of Range item should fail with 0 (%ld)\n", res);
422     res=setItem(hWndHeader, 5, str_items[5], TRUE);
423     ok(res == 1, "Setting Out of Range item should fail with 1 (%ld)\n", res);
424     res=setItem(hWndHeader, -2, str_items[5], FALSE);
425     ok(res == 0, "Setting Out of Range item should fail with 0 (%ld)\n", res);
426     TEST_GET_ITEMCOUNT(6);
427
428     for (i = 0; i < 4; i++)
429     {
430         res = setItem(hWndHeader, i, str_items[4], TRUE);
431         ok(res != 0, "Setting %d item failed (%ld)\n", i+1, res);
432         TEST_GET_ITEM(i, 4);
433         TEST_GET_ITEMCOUNT(6);
434     }
435     
436     SendMessageA(hWndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, 0);
437     setItemUnicodeNotify(hWndHeader, 3, pszUniTestA, pszUniTestW);
438     SendMessageA(hWndHeader, WM_NOTIFYFORMAT, (WPARAM)hHeaderParentWnd, (LPARAM)NF_REQUERY);
439     setItem(hWndHeader, 3, str_items[4], TRUE);
440     
441     dont_expect_notify(HDN_GETDISPINFOA);
442     dont_expect_notify(HDN_GETDISPINFOW);
443     addItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA);
444     setItem(hWndHeader, 0, str_items[4], TRUE);
445     /* unexpected notifies cleared by notifies_received in setItem */
446     dont_expect_notify(HDN_GETDISPINFOA);
447     dont_expect_notify(HDN_GETDISPINFOW);
448     setItem(hWndHeader, 0, LPSTR_TEXTCALLBACKA, TRUE);
449     /* unexpected notifies cleared by notifies_received in setItem */
450     delItem(hWndHeader, 0);
451
452     check_auto_format();
453     TEST_GET_ITEMCOUNT(6);
454     check_auto_fields();
455     TEST_GET_ITEMCOUNT(6);
456     check_mask();
457     TEST_GET_ITEMCOUNT(6);
458
459     res = delItem(hWndHeader, 5);
460     ok(res == 1, "Deleting Out of Range item should fail with 1 (%ld)\n", res);
461     res = delItem(hWndHeader, -2);
462     ok(res == 0, "Deleting Out of Range item should fail with 0 (%ld)\n", res);
463     TEST_GET_ITEMCOUNT(5);
464
465     res = delItem(hWndHeader, 3);
466     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
467     TEST_GET_ITEMCOUNT(4);
468     res = delItem(hWndHeader, 0);
469     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
470     TEST_GET_ITEMCOUNT(3);
471     res = delItem(hWndHeader, 0);
472     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
473     TEST_GET_ITEMCOUNT(2);
474     res = delItem(hWndHeader, 0);
475     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
476     TEST_GET_ITEMCOUNT(1);
477
478     DestroyWindow(hWndHeader);
479 }
480
481
482 static void check_order(const int expected_id[], const int expected_order[],
483                         int count, const char *type)
484 {
485     int i;
486     HDITEMA hdi;
487
488     ok(getItemCount(hWndHeader) == count, "Invalid item count in order tests\n");
489     for (i = 0; i < count; i++)
490     {
491         hdi.mask = HDI_LPARAM|HDI_ORDER;
492         SendMessage(hWndHeader, HDM_GETITEMA, i, (LPARAM)&hdi);
493         ok(hdi.lParam == expected_id[i],
494             "Invalid item ids after '%s'- item %d has lParam %d\n", type, i, (int)hdi.lParam);
495         ok(hdi.iOrder == expected_order[i],
496             "Invalid item order after '%s'- item %d has iOrder %d\n", type, i, hdi.iOrder);
497     }
498 }
499
500 static void test_header_order (void)
501 {
502     const int rand1[] = {0, 1, 1, 0, 4};
503     const int rand2[] = {4, 5, 6, 7, 4};
504     const int rand3[] = {5, 5, 1, 6, 1};
505     const int rand4[] = {1, 5, 2, 7, 6, 1, 4, 2, 3, 2};
506     const int rand5[] = {7, 8, 5, 6, 7, 2, 1, 9, 10, 10};
507     const int rand6[] = {2, 8, 3, 4, 0};
508
509     const int ids1[] = {3, 0, 2, 1, 4};
510     const int ord1[] = {0, 1, 2, 3, 4};
511     const int ids2[] = {3, 9, 7, 0, 2, 1, 4, 8, 6, 5};
512     const int ord2[] = {0, 4, 7, 1, 2, 3, 9, 8, 6, 5};
513     const int ord3[] = {0, 3, 9, 2, 1, 8, 7, 6, 5, 4};
514     const int ids4[] = {9, 0, 1, 8, 6};
515     const int ord4[] = {1, 0, 4, 3, 2};
516
517     char buffer[20];
518     HDITEMA hdi;
519     int i;
520
521     hWndHeader = create_header_control();
522
523     ZeroMemory(&hdi, sizeof(HDITEMA));
524     hdi.mask = HDI_TEXT | HDI_LPARAM;
525     hdi.pszText = buffer;
526     strcpy(buffer, "test");
527
528     for (i = 0; i < 5; i++)
529     {
530         hdi.lParam = i;
531         SendMessage(hWndHeader, HDM_INSERTITEMA, rand1[i], (LPARAM)&hdi);
532         rand();
533     }
534     check_order(ids1, ord1, 5, "insert without iOrder");
535
536     hdi.mask |= HDI_ORDER;
537     for (i = 0; i < 5; i++)
538     {
539         hdi.lParam = i + 5;
540         hdi.iOrder = rand2[i];
541         SendMessage(hWndHeader, HDM_INSERTITEMA, rand3[i], (LPARAM)&hdi);
542         rand(); rand();
543     }
544     check_order(ids2, ord2, 10, "insert with order");
545
546     hdi.mask = HDI_ORDER;
547     for (i=0; i<10; i++)
548     {
549         hdi.iOrder = rand5[i];
550         SendMessage(hWndHeader, HDM_SETITEMA, rand4[i], (LPARAM)&hdi);
551         rand(); rand();
552     }
553     check_order(ids2, ord3, 10, "setitems changing order");
554
555     for (i=0; i<5; i++)
556         SendMessage(hWndHeader, HDM_DELETEITEM, rand6[i], 0);
557     check_order(ids4, ord4, 5, "deleteitem");
558
559     DestroyWindow(hWndHeader);
560 }
561
562 LRESULT CALLBACK HeaderTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
563 {
564     switch(msg) {
565
566     case WM_NOTIFY:
567     {
568         NMHEADERA *hdr = (NMHEADER *)lParam;
569         EXPECTEDNOTIFY *expected;
570         int i;
571         
572         for (i=0; i<nUnexpectedNotify; i++)
573             ok(hdr->hdr.code != unexpectedNotify[i], "Received invalid notify %d\n", hdr->hdr.code);
574         
575         if (nReceivedNotify >= nExpectedNotify || hdr->hdr.hwndFrom != hWndHeader )
576             break;
577
578         expected = &expectedNotify[nReceivedNotify];
579         if (hdr->hdr.code != expected->iCode)
580             break;
581         
582         nReceivedNotify++;
583         compare_items(hdr->hdr.code, &expected->hdItem, hdr->pitem, expected->fUnicode);
584         break;
585     }
586     
587     case WM_DESTROY:
588         PostQuitMessage(0);
589         break;
590   
591     default:
592         return DefWindowProcA(hWnd, msg, wParam, lParam);
593     }
594     
595     return 0L;
596 }
597
598 static void init(void) {
599     WNDCLASSA wc;
600     INITCOMMONCONTROLSEX icex;
601
602     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
603     icex.dwICC  = ICC_USEREX_CLASSES;
604     InitCommonControlsEx(&icex);
605
606     wc.style = CS_HREDRAW | CS_VREDRAW;
607     wc.cbClsExtra = 0;
608     wc.cbWndExtra = 0;
609     wc.hInstance = GetModuleHandleA(NULL);
610     wc.hIcon = NULL;
611     wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_ARROW));
612     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
613     wc.lpszMenuName = NULL;
614     wc.lpszClassName = "HeaderTestClass";
615     wc.lpfnWndProc = HeaderTestWndProc;
616     RegisterClassA(&wc);
617
618     hHeaderParentWnd = CreateWindowExA(0, "HeaderTestClass", "Header test", WS_OVERLAPPEDWINDOW, 
619       CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
620     assert(hHeaderParentWnd != NULL);
621 }
622
623 START_TEST(header)
624 {
625     init();
626
627     test_header_control();
628     test_header_order();
629
630     DestroyWindow(hHeaderParentWnd);
631 }