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
25 #include "wine/test.h"
27 typedef struct tagEXPECTEDNOTIFY
34 EXPECTEDNOTIFY expectedNotify
[10];
35 INT nExpectedNotify
= 0;
36 INT nReceivedNotify
= 0;
38 static HWND hHeaderParentWnd
;
39 static HWND hWndHeader
;
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
;
51 static BOOL
notifies_received()
53 BOOL fRet
= (nExpectedNotify
== nReceivedNotify
);
54 nExpectedNotify
= nReceivedNotify
= 0;
58 static LONG
addItem(HWND hdex
, int idx
, LPCSTR text
)
61 hdItem
.mask
= HDI_TEXT
| HDI_WIDTH
;
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
)
72 hdexItem
.mask
= HDI_TEXT
;
73 hdexItem
.pszText
= (LPSTR
)text
;
74 hdexItem
.cchTextMax
= 0;
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
);
82 ok(notifies_received(), "setItem(): not all expected notifies were received\n");
86 static LONG
setItemUnicodeNotify(HWND hdex
, int idx
, LPCSTR text
, LPCWSTR wText
)
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");
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
)
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)
131 handle
= CreateWindowEx(0, WC_HEADER
, NULL
,
132 WS_CHILD
|WS_BORDER
|WS_VISIBLE
|HDS_BUTTONS
|HDS_HORZ
,
134 hHeaderParentWnd
, NULL
, NULL
, NULL
);
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);
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
)
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",
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)
199 static char buffer
[MAX_CHARS
];
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
);
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
)
285 NMHEADERA
*hdr
= (NMHEADER
*)lParam
;
286 EXPECTEDNOTIFY
*expected
;
288 if (nReceivedNotify
>= nExpectedNotify
|| hdr
->hdr
.hwndFrom
!= hWndHeader
)
291 expected
= &expectedNotify
[nReceivedNotify
];
292 if (hdr
->hdr
.code
!= expected
->iCode
)
296 compare_items(hdr
->hdr
.code
, &expected
->hdItem
, hdr
->pitem
, expected
->fUnicode
);
305 return DefWindowProcA(hWnd
, msg
, wParam
, lParam
);
311 static void init(void) {
313 INITCOMMONCONTROLSEX icex
;
315 icex
.dwSize
= sizeof(INITCOMMONCONTROLSEX
);
316 icex
.dwICC
= ICC_USEREX_CLASSES
;
317 InitCommonControlsEx(&icex
);
319 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
322 wc
.hInstance
= GetModuleHandleA(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
;
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
);
340 test_header_control();
342 DestroyWindow(hHeaderParentWnd
);