advpack: Set the ldids of the install section in install_init.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
38 static HWND hHeaderParentWnd;
39 static HWND hWndHeader;
40 #define MAX_CHARS 100
41
42 static void expect_notify(INT iCode, BOOL fUnicode, HDITEMA *lpItem)
43 {
44     assert(nExpectedNotify < 10);
45     expectedNotify[nExpectedNotify].iCode = iCode;
46     expectedNotify[nExpectedNotify].fUnicode = fUnicode;
47     expectedNotify[nExpectedNotify].hdItem = *lpItem;
48     nExpectedNotify++;
49 }
50
51 static BOOL notifies_received()
52 {
53     BOOL fRet = (nExpectedNotify == nReceivedNotify);
54     nExpectedNotify = nReceivedNotify = 0;
55     return fRet;
56 }
57
58 static LONG addItem(HWND hdex, int idx, LPCSTR text)
59 {
60     HDITEMA hdItem;
61     hdItem.mask       = HDI_TEXT | HDI_WIDTH;
62     hdItem.cxy        = 100;
63     hdItem.pszText    = (LPSTR)text;
64     hdItem.cchTextMax = 0;
65     return (LONG)SendMessage(hdex, HDM_INSERTITEMA, (WPARAM)idx, (LPARAM)&hdItem);
66 }
67
68 static LONG setItem(HWND hdex, int idx, LPCSTR text, BOOL fCheckNotifies)
69 {
70     LONG ret;
71     HDITEMA hdexItem;
72     hdexItem.mask       = HDI_TEXT;
73     hdexItem.pszText    = (LPSTR)text;
74     hdexItem.cchTextMax = 0;
75     if (fCheckNotifies)
76     {
77         expect_notify(HDN_ITEMCHANGINGA, FALSE, &hdexItem);
78         expect_notify(HDN_ITEMCHANGEDA, FALSE, &hdexItem);
79     }
80     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
81     if (fCheckNotifies)
82         ok(notifies_received(), "setItem(): not all expected notifies were received\n");
83     return ret;
84 }
85
86 static LONG setItemUnicodeNotify(HWND hdex, int idx, LPCSTR text, LPCWSTR wText)
87 {
88     LONG ret;
89     HDITEMA hdexItem;
90     HDITEMW hdexNotify;
91     hdexItem.mask       = HDI_TEXT;
92     hdexItem.pszText    = (LPSTR)text;
93     hdexItem.cchTextMax = 0;
94     
95     hdexNotify.mask    = HDI_TEXT;
96     hdexNotify.pszText = (LPWSTR)wText;
97     
98     expect_notify(HDN_ITEMCHANGINGW, TRUE, (HDITEMA*)&hdexNotify);
99     expect_notify(HDN_ITEMCHANGEDW, TRUE, (HDITEMA*)&hdexNotify);
100     ret = (LONG)SendMessage(hdex, HDM_SETITEMA, (WPARAM)idx, (LPARAM)&hdexItem);
101     ok(notifies_received(), "setItemUnicodeNotify(): not all expected notifies were received\n");
102     return ret;
103 }
104
105 static LONG delItem(HWND hdex, int idx)
106 {
107     return (LONG)SendMessage(hdex, HDM_DELETEITEM, (WPARAM)idx, 0);
108 }
109
110 static LONG getItemCount(HWND hdex)
111 {
112     return (LONG)SendMessage(hdex, HDM_GETITEMCOUNT, 0, 0);
113 }
114
115 static LONG getItem(HWND hdex, int idx, LPSTR textBuffer)
116 {
117     HDITEMA hdItem;
118     hdItem.mask         = HDI_TEXT;
119     hdItem.pszText      = textBuffer;
120     hdItem.cchTextMax   = MAX_CHARS;
121     return (LONG)SendMessage(hdex, HDM_GETITEMA, (WPARAM)idx, (LPARAM)&hdItem);
122 }
123
124 static HWND create_header_control (void)
125 {
126     HWND handle;
127     HDLAYOUT hlayout;
128     RECT rectwin;
129     WINDOWPOS winpos;
130
131     handle = CreateWindowEx(0, WC_HEADER, NULL,
132                             WS_CHILD|WS_BORDER|WS_VISIBLE|HDS_BUTTONS|HDS_HORZ,
133                             0, 0, 0, 0,
134                             hHeaderParentWnd, NULL, NULL, NULL);
135     assert(handle);
136
137     if (winetest_interactive)
138         ShowWindow (hHeaderParentWnd, SW_SHOW);
139
140     GetClientRect(hHeaderParentWnd,&rectwin);
141     hlayout.prc = &rectwin;
142     hlayout.pwpos = &winpos;
143     SendMessageA(handle,HDM_LAYOUT,0,(LPARAM) &hlayout);
144     SetWindowPos(handle, winpos.hwndInsertAfter, winpos.x, winpos.y, 
145                  winpos.cx, winpos.cy, 0);
146
147     return handle;
148 }
149
150 static void compare_items(INT iCode, HDITEMA *hdi1, HDITEMA *hdi2, BOOL fUnicode)
151 {
152     ok(hdi1->mask == hdi2->mask, "Notify %d mask mismatch (%08x != %08x)\n", iCode, hdi1->mask, hdi2->mask);
153     if (hdi1->mask & HDI_WIDTH)
154     {
155         ok(hdi1->cxy == hdi2->cxy, "Notify %d cxy mismatch (%08x != %08x)\n", iCode, hdi1->cxy, hdi2->cxy);
156     }
157     if (hdi1->mask & HDI_TEXT)
158     {
159         if (fUnicode)
160         {
161             char buf1[260];
162             char buf2[260];
163             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi1->pszText, -1, buf1, 260, NULL, NULL);
164             WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)hdi2->pszText, -1, buf2, 260, NULL, NULL);
165             ok(lstrcmpW((LPWSTR)hdi1->pszText, (LPWSTR)hdi2->pszText)==0,
166                 "Notify %d text mismatch (L\"%s\" vs L\"%s\")\n",
167                     iCode, buf1, buf2);
168         }
169         else
170         {
171             ok(strcmp(hdi1->pszText, hdi2->pszText)==0,
172                 "Notify %d text mismatch (\"%s\" vs \"%s\")\n",
173                     iCode, hdi1->pszText, hdi2->pszText);
174             }
175     }
176 }
177
178 static const char *str_items[] =
179     {"First Item", "Second Item", "Third Item", "Fourth Item", "Replace Item", "Out Of Range Item"};
180     
181 static const char pszUniTestA[] = "TST";
182 static const WCHAR pszUniTestW[] = {'T','S','T',0};
183
184
185 #define TEST_GET_ITEM(i,c)\
186 {   res = getItem(hWndHeader, i, buffer);\
187     ok(res != 0, "Getting item[%d] using valid index failed unexpectedly (%ld)\n", i, res);\
188     ok(strcmp(str_items[c], buffer) == 0, "Getting item[%d] returned \"%s\" expecting \"%s\"\n", i, buffer, str_items[c]);\
189 }
190
191 #define TEST_GET_ITEMCOUNT(i)\
192 {   res = getItemCount(hWndHeader);\
193     ok(res == i, "Got Item Count as %ld\n", res);\
194 }
195
196 static void test_header_control (void)
197 {
198     LONG res;
199     static char buffer[MAX_CHARS];
200     int i;
201
202     hWndHeader = create_header_control ();
203
204     for (i = 3; i >= 0; i--)
205     {
206         TEST_GET_ITEMCOUNT(3-i);
207         res = addItem(hWndHeader, 0, str_items[i]);
208         ok(res == 0, "Adding simple item failed (%ld)\n", res);
209     }
210
211     TEST_GET_ITEMCOUNT(4);
212     res = addItem(hWndHeader, 99, str_items[i+1]);
213     ok(res != -1, "Adding Out of Range item should fail with -1 got (%ld)\n", res);
214     TEST_GET_ITEMCOUNT(5);
215     res = addItem(hWndHeader, 5, str_items[i+1]);
216     ok(res != -1, "Adding Out of Range item should fail with -1 got (%ld)\n", res);
217     TEST_GET_ITEMCOUNT(6);
218
219     for (i = 0; i < 4; i++) { TEST_GET_ITEM(i,i); TEST_GET_ITEMCOUNT(6); }
220
221     res=getItem(hWndHeader, 99, buffer);
222     ok(res == 0, "Getting Out of Range item should fail with 0 (%ld), got %s\n", res,buffer);
223     res=getItem(hWndHeader, 5, buffer);
224     ok(res == 1, "Getting Out of Range item should fail with 1 (%ld), got %s\n", res,buffer);
225     res=getItem(hWndHeader, -2, buffer);
226     ok(res == 0, "Getting Out of Range item should fail with 0 (%ld), got %s\n", res,buffer);
227
228     if (winetest_interactive)
229     {
230         UpdateWindow(hHeaderParentWnd);
231         UpdateWindow(hWndHeader);
232     }
233
234     TEST_GET_ITEMCOUNT(6);
235     res=setItem(hWndHeader, 99, str_items[5], FALSE);
236     ok(res == 0, "Setting Out of Range item should fail with 0 (%ld)\n", res);
237     res=setItem(hWndHeader, 5, str_items[5], TRUE);
238     ok(res == 1, "Setting Out of Range item should fail with 1 (%ld)\n", res);
239     res=setItem(hWndHeader, -2, str_items[5], FALSE);
240     ok(res == 0, "Setting Out of Range item should fail with 0 (%ld)\n", res);
241     TEST_GET_ITEMCOUNT(6);
242
243     for (i = 0; i < 4; i++)
244     {
245         res = setItem(hWndHeader, i, str_items[4], TRUE);
246         ok(res != 0, "Setting %d item failed (%ld)\n", i+1, res);
247         TEST_GET_ITEM(i, 4);
248         TEST_GET_ITEMCOUNT(6);
249     }
250     
251     SendMessageA(hWndHeader, HDM_SETUNICODEFORMAT, (WPARAM)TRUE, 0);
252     setItemUnicodeNotify(hWndHeader, 3, pszUniTestA, pszUniTestW);
253     SendMessageA(hWndHeader, WM_NOTIFYFORMAT, (WPARAM)hHeaderParentWnd, (LPARAM)NF_REQUERY);
254     setItem(hWndHeader, 3, str_items[4], TRUE);
255
256     res = delItem(hWndHeader, 5);
257     ok(res == 1, "Deleting Out of Range item should fail with 1 (%ld)\n", res);
258     res = delItem(hWndHeader, -2);
259     ok(res == 0, "Deleting Out of Range item should fail with 0 (%ld)\n", res);
260     TEST_GET_ITEMCOUNT(5);
261
262     res = delItem(hWndHeader, 3);
263     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
264     TEST_GET_ITEMCOUNT(4);
265     res = delItem(hWndHeader, 0);
266     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
267     TEST_GET_ITEMCOUNT(3);
268     res = delItem(hWndHeader, 0);
269     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
270     TEST_GET_ITEMCOUNT(2);
271     res = delItem(hWndHeader, 0);
272     ok(res != 0, "Deleting using out of range index failed (%ld)\n", res);
273     TEST_GET_ITEMCOUNT(1);
274
275     DestroyWindow(hWndHeader);
276 }
277
278
279 LRESULT CALLBACK HeaderTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
280 {
281     switch(msg) {
282
283     case WM_NOTIFY:
284     {
285         NMHEADERA *hdr = (NMHEADER *)lParam;
286         EXPECTEDNOTIFY *expected;
287         
288         if (nReceivedNotify >= nExpectedNotify || hdr->hdr.hwndFrom != hWndHeader )
289             break;
290
291         expected = &expectedNotify[nReceivedNotify];
292         if (hdr->hdr.code != expected->iCode)
293             break;
294         
295         nReceivedNotify++;
296         compare_items(hdr->hdr.code, &expected->hdItem, hdr->pitem, expected->fUnicode);
297         break;
298     }
299     
300     case WM_DESTROY:
301         PostQuitMessage(0);
302         break;
303   
304     default:
305         return DefWindowProcA(hWnd, msg, wParam, lParam);
306     }
307     
308     return 0L;
309 }
310
311 static void init(void) {
312     WNDCLASSA wc;
313     INITCOMMONCONTROLSEX icex;
314
315     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
316     icex.dwICC  = ICC_USEREX_CLASSES;
317     InitCommonControlsEx(&icex);
318
319     wc.style = CS_HREDRAW | CS_VREDRAW;
320     wc.cbClsExtra = 0;
321     wc.cbWndExtra = 0;
322     wc.hInstance = GetModuleHandleA(NULL);
323     wc.hIcon = NULL;
324     wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_ARROW));
325     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
326     wc.lpszMenuName = NULL;
327     wc.lpszClassName = "HeaderTestClass";
328     wc.lpfnWndProc = HeaderTestWndProc;
329     RegisterClassA(&wc);
330
331     hHeaderParentWnd = CreateWindowExA(0, "HeaderTestClass", "Header test", WS_OVERLAPPEDWINDOW, 
332       CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
333     assert(hHeaderParentWnd != NULL);
334 }
335
336 START_TEST(header)
337 {
338     init();
339
340     test_header_control();
341
342     DestroyWindow(hHeaderParentWnd);
343 }