comctl32: Test the HDM_SETITEM notifications.
[wine/multimedia.git] / dlls / comctl32 / tests / header.c
blob87dec21e8624047aca669a05b1ca88d98922e8e0
1 /* Unit test suite for header control.
3 * Copyright 2005 Vijay Kiran Kamuju
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.
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.
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
21 #include <windows.h>
22 #include <commctrl.h>
23 #include <assert.h>
25 #include "wine/test.h"
27 typedef struct tagEXPECTEDNOTIFY
29 INT iCode;
30 BOOL fUnicode;
31 HDITEMA hdItem;
32 } EXPECTEDNOTIFY;
34 EXPECTEDNOTIFY expectedNotify[10];
35 INT nExpectedNotify = 0;
36 INT nReceivedNotify = 0;
38 static HWND hHeaderParentWnd;
39 static HWND hWndHeader;
40 #define MAX_CHARS 100
42 static void expect_notify(INT iCode, BOOL fUnicode, HDITEMA *lpItem)
44 assert(nExpectedNotify < 10);
45 expectedNotify[nExpectedNotify].iCode = iCode;
46 expectedNotify[nExpectedNotify].fUnicode = fUnicode;
47 expectedNotify[nExpectedNotify].hdItem = *lpItem;
48 nExpectedNotify++;
51 static BOOL notifies_received()
53 BOOL fRet = (nExpectedNotify == nReceivedNotify);
54 nExpectedNotify = nReceivedNotify = 0;
55 return fRet;
58 static LONG addItem(HWND hdex, int idx, LPCSTR text)
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);
68 static LONG setItem(HWND hdex, int idx, LPCSTR text, BOOL fCheckNotifies)
70 LONG ret;
71 HDITEMA hdexItem;
72 hdexItem.mask = HDI_TEXT;
73 hdexItem.pszText = (LPSTR)text;
74 hdexItem.cchTextMax = 0;
75 if (fCheckNotifies)
77 expect_notify(HDN_ITEMCHANGINGA, FALSE, &hdexItem);
78 expect_notify(HDN_ITEMCHANGEDA, FALSE, &hdexItem);
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;
86 static LONG setItemUnicodeNotify(HWND hdex, int idx, LPCSTR text, LPCWSTR wText)
88 LONG ret;
89 HDITEMA hdexItem;
90 HDITEMW hdexNotify;
91 hdexItem.mask = HDI_TEXT;
92 hdexItem.pszText = (LPSTR)text;
93 hdexItem.cchTextMax = 0;
95 hdexNotify.mask = HDI_TEXT;
96 hdexNotify.pszText = (LPWSTR)wText;
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;
105 static LONG delItem(HWND hdex, int idx)
107 return (LONG)SendMessage(hdex, HDM_DELETEITEM, (WPARAM)idx, 0);
110 static LONG getItemCount(HWND hdex)
112 return (LONG)SendMessage(hdex, HDM_GETITEMCOUNT, 0, 0);
115 static LONG getItem(HWND hdex, int idx, LPSTR textBuffer)
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);
124 static HWND create_header_control (void)
126 HWND handle;
127 HDLAYOUT hlayout;
128 RECT rectwin;
129 WINDOWPOS winpos;
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);
137 if (winetest_interactive)
138 ShowWindow (hHeaderParentWnd, SW_SHOW);
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);
147 return handle;
150 static void compare_items(INT iCode, HDITEMA *hdi1, HDITEMA *hdi2, BOOL fUnicode)
152 ok(hdi1->mask == hdi2->mask, "Notify %d mask mismatch (%08x != %08x)\n", iCode, hdi1->mask, hdi2->mask);
153 if (hdi1->mask & HDI_WIDTH)
155 ok(hdi1->cxy == hdi2->cxy, "Notify %d cxy mismatch (%08x != %08x)\n", iCode, hdi1->cxy, hdi2->cxy);
157 if (hdi1->mask & HDI_TEXT)
159 if (fUnicode)
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);
169 else
171 ok(strcmp(hdi1->pszText, hdi2->pszText)==0,
172 "Notify %d text mismatch (\"%s\" vs \"%s\")\n",
173 iCode, hdi1->pszText, hdi2->pszText);
178 static const char *str_items[] =
179 {"First Item", "Second Item", "Third Item", "Fourth Item", "Replace Item", "Out Of Range Item"};
181 static const char pszUniTestA[] = "TST";
182 static const WCHAR pszUniTestW[] = {'T','S','T',0};
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]);\
191 #define TEST_GET_ITEMCOUNT(i)\
192 { res = getItemCount(hWndHeader);\
193 ok(res == i, "Got Item Count as %ld\n", res);\
196 static void test_header_control (void)
198 LONG res;
199 static char buffer[MAX_CHARS];
200 int i;
202 hWndHeader = create_header_control ();
204 for (i = 3; i >= 0; i--)
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);
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);
219 for (i = 0; i < 4; i++) { TEST_GET_ITEM(i,i); TEST_GET_ITEMCOUNT(6); }
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);
228 if (winetest_interactive)
230 UpdateWindow(hHeaderParentWnd);
231 UpdateWindow(hWndHeader);
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);
243 for (i = 0; i < 4; i++)
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);
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);
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);
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);
275 DestroyWindow(hWndHeader);
279 LRESULT CALLBACK HeaderTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
281 switch(msg) {
283 case WM_NOTIFY:
285 NMHEADERA *hdr = (NMHEADER *)lParam;
286 EXPECTEDNOTIFY *expected;
288 if (nReceivedNotify >= nExpectedNotify || hdr->hdr.hwndFrom != hWndHeader )
289 break;
291 expected = &expectedNotify[nReceivedNotify];
292 if (hdr->hdr.code != expected->iCode)
293 break;
295 nReceivedNotify++;
296 compare_items(hdr->hdr.code, &expected->hdItem, hdr->pitem, expected->fUnicode);
297 break;
300 case WM_DESTROY:
301 PostQuitMessage(0);
302 break;
304 default:
305 return DefWindowProcA(hWnd, msg, wParam, lParam);
308 return 0L;
311 static void init(void) {
312 WNDCLASSA wc;
313 INITCOMMONCONTROLSEX icex;
315 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
316 icex.dwICC = ICC_USEREX_CLASSES;
317 InitCommonControlsEx(&icex);
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);
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);
336 START_TEST(header)
338 init();
340 test_header_control();
342 DestroyWindow(hHeaderParentWnd);