1 /* Unit test suite for list boxes.
3 * Copyright 2003 Ferenc Wagner
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
30 #include "wine/test.h"
33 #define WAIT Sleep (1000)
34 #define REDRAW RedrawWindow (handle, NULL, 0, RDW_UPDATENOW)
40 static const char * const strings[4] = {
44 "Fourth added which is very long because at some time we only had a 256 byte character buffer and that was overflowing in one of those applications that had a common dialog file open box and tried to add a 300 characters long custom filter string which of course the code did not like and crashed. Just make sure this string is longer than 256 characters."
48 create_listbox (DWORD add_style, HWND parent)
54 handle=CreateWindow ("LISTBOX", "TestList",
55 (LBS_STANDARD & ~LBS_SORT) | add_style,
57 parent, (HMENU)ctl_id, NULL, 0);
60 SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[0]);
61 SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[1]);
62 SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[2]);
63 SendMessage (handle, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) strings[3]);
66 ShowWindow (handle, SW_SHOW);
78 int selected, anchor, caret, selcount;
82 struct listbox_prop prop;
83 struct listbox_stat init, init_todo;
84 struct listbox_stat click, click_todo;
85 struct listbox_stat step, step_todo;
86 struct listbox_stat sel, sel_todo;
90 listbox_query (HWND handle, struct listbox_stat *results)
92 results->selected = SendMessage (handle, LB_GETCURSEL, 0, 0);
93 results->anchor = SendMessage (handle, LB_GETANCHORINDEX, 0, 0);
94 results->caret = SendMessage (handle, LB_GETCARETINDEX, 0, 0);
95 results->selcount = SendMessage (handle, LB_GETSELCOUNT, 0, 0);
99 buttonpress (HWND handle, WORD x, WORD y)
104 SendMessage (handle, WM_LBUTTONDOWN, (WPARAM) MK_LBUTTON, lp);
105 SendMessage (handle, WM_LBUTTONUP , (WPARAM) 0 , lp);
110 keypress (HWND handle, WPARAM keycode, BYTE scancode, BOOL extended)
112 LPARAM lp=1+(scancode<<16)+(extended?KEYEVENTF_EXTENDEDKEY:0);
115 SendMessage (handle, WM_KEYDOWN, keycode, lp);
116 SendMessage (handle, WM_KEYUP , keycode, lp | 0xc000000);
120 #define listbox_field_ok(t, s, f, got) \
121 ok (t.s.f==got.f, "style %#x, step " #s ", field " #f \
122 ": expected %d, got %d\n", (unsigned int)t.prop.add_style, \
125 #define listbox_todo_field_ok(t, s, f, got) \
126 if (t.s##_todo.f) todo_wine { listbox_field_ok(t, s, f, got); } \
127 else listbox_field_ok(t, s, f, got)
129 #define listbox_ok(t, s, got) \
130 listbox_todo_field_ok(t, s, selected, got); \
131 listbox_todo_field_ok(t, s, anchor, got); \
132 listbox_todo_field_ok(t, s, caret, got); \
133 listbox_todo_field_ok(t, s, selcount, got)
136 check (const struct listbox_test test)
138 struct listbox_stat answer;
139 HWND hLB=create_listbox (test.prop.add_style, 0);
144 listbox_query (hLB, &answer);
145 listbox_ok (test, init, answer);
147 SendMessage (hLB, LB_GETITEMRECT, (WPARAM) 1, (LPARAM) &second_item);
148 buttonpress(hLB, (WORD)second_item.left, (WORD)second_item.top);
150 listbox_query (hLB, &answer);
151 listbox_ok (test, click, answer);
153 keypress (hLB, VK_DOWN, 0x50, TRUE);
155 listbox_query (hLB, &answer);
156 listbox_ok (test, step, answer);
159 hLB=create_listbox (test.prop.add_style, 0);
161 SendMessage (hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 2));
162 listbox_query (hLB, &answer);
163 listbox_ok (test, sel, answer);
166 DWORD size = SendMessage (hLB, LB_GETTEXTLEN, i, 0);
170 txt = HeapAlloc (GetProcessHeap(), 0, size+1);
171 SendMessageA(hLB, LB_GETTEXT, i, (LPARAM)txt);
172 ok(!strcmp (txt, strings[i]), "returned string for item %d does not match %s vs %s\n", i, txt, strings[i]);
174 txtw = HeapAlloc (GetProcessHeap(), 0, 2*size+2);
175 SendMessageW(hLB, LB_GETTEXT, i, (LPARAM)txtw);
176 WideCharToMultiByte( CP_ACP, 0, txtw, -1, txt, size, NULL, NULL );
177 ok(!strcmp (txt, strings[i]), "returned string for item %d does not match %s vs %s\n", i, txt, strings[i]);
179 HeapFree (GetProcessHeap(), 0, txtw);
180 HeapFree (GetProcessHeap(), 0, txt);
183 /* Confirm the count of items, and that an invalid delete does not remove anything */
184 res = SendMessage (hLB, LB_GETCOUNT, 0, 0);
185 ok((res==4), "Expected 4 items, got %d\n", res);
186 res = SendMessage (hLB, LB_DELETESTRING, -1, 0);
187 ok((res==LB_ERR), "Expected LB_ERR items, got %d\n", res);
188 res = SendMessage (hLB, LB_DELETESTRING, 4, 0);
189 ok((res==LB_ERR), "Expected LB_ERR items, got %d\n", res);
190 res = SendMessage (hLB, LB_GETCOUNT, 0, 0);
191 ok((res==4), "Expected 4 items, got %d\n", res);
197 static void check_item_height(void)
205 hLB = create_listbox (0, 0);
206 ok ((hdc = GetDCEx( hLB, 0, DCX_CACHE )) != 0, "Can't get hdc\n");
207 ok ((font = GetCurrentObject(hdc, OBJ_FONT)) != 0, "Can't get the current font\n");
208 ok (GetTextMetrics( hdc, &tm ), "Can't read font metrics\n");
209 ReleaseDC( hLB, hdc);
211 ok (SendMessage(hLB, WM_SETFONT, (WPARAM)font, 0) == 0, "Can't set font\n");
213 itemHeight = SendMessage(hLB, LB_GETITEMHEIGHT, 0, 0);
214 ok (itemHeight == tm.tmHeight, "Item height wrong, got %d, expecting %ld\n", itemHeight, tm.tmHeight);
219 static LRESULT WINAPI main_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
225 RECT rc_item, rc_client, rc_clip;
226 DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lparam;
228 trace("%p WM_DRAWITEM %08x %08lx\n", hwnd, wparam, lparam);
230 ok(wparam == dis->CtlID, "got wParam=%08x instead of %08x\n",
232 ok(dis->CtlType == ODT_LISTBOX, "wrong CtlType %04x\n", dis->CtlType);
234 GetClientRect(dis->hwndItem, &rc_client);
235 trace("hwndItem %p client rect (%ld,%ld-%ld,%ld)\n", dis->hwndItem,
236 rc_client.left, rc_client.top, rc_client.right, rc_client.bottom);
237 GetClipBox(dis->hDC, &rc_clip);
238 trace("clip rect (%ld,%ld-%ld,%ld)\n", rc_clip.left, rc_clip.top, rc_clip.right, rc_clip.bottom);
239 ok(EqualRect(&rc_client, &rc_clip), "client rect of the listbox should be equal to the clip box\n");
241 trace("rcItem (%ld,%ld-%ld,%ld)\n", dis->rcItem.left, dis->rcItem.top,
242 dis->rcItem.right, dis->rcItem.bottom);
243 SendMessage(dis->hwndItem, LB_GETITEMRECT, dis->itemID, (LPARAM)&rc_item);
244 trace("item rect (%ld,%ld-%ld,%ld)\n", rc_item.left, rc_item.top, rc_item.right, rc_item.bottom);
245 ok(EqualRect(&dis->rcItem, &rc_item), "item rects are not equal\n");
254 return DefWindowProc(hwnd, msg, wparam, lparam);
257 static void test_ownerdraw(void)
265 cls.lpfnWndProc = main_window_proc;
268 cls.hInstance = GetModuleHandle(0);
270 cls.hCursor = LoadCursor(0, (LPSTR)IDC_ARROW);
271 cls.hbrBackground = GetStockObject(WHITE_BRUSH);
272 cls.lpszMenuName = NULL;
273 cls.lpszClassName = "main_window_class";
274 assert(RegisterClass(&cls));
276 parent = CreateWindowEx(0, "main_window_class", NULL,
277 WS_POPUP | WS_VISIBLE,
279 GetDesktopWindow(), 0,
280 GetModuleHandle(0), NULL);
283 hLB = create_listbox(LBS_OWNERDRAWFIXED | WS_CHILD | WS_VISIBLE, parent);
288 /* make height short enough */
289 SendMessage(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
290 SetWindowPos(hLB, 0, 0, 0, 100, rc.bottom - rc.top + 1,
291 SWP_NOZORDER | SWP_NOMOVE);
293 /* make 0 item invisible */
294 SendMessage(hLB, LB_SETTOPINDEX, 1, 0);
295 ret = SendMessage(hLB, LB_GETTOPINDEX, 0, 0);
296 ok(ret == 1, "wrong top index %d\n", ret);
298 SendMessage(hLB, LB_GETITEMRECT, 0, (LPARAM)&rc);
299 trace("item 0 rect (%ld,%ld-%ld,%ld)\n", rc.left, rc.top, rc.right, rc.bottom);
300 ok(!IsRectEmpty(&rc), "empty item rect\n");
301 ok(rc.top < 0, "rc.top is not negative (%ld)\n", rc.top);
304 DestroyWindow(parent);
307 #define listbox_test_query(exp, got) \
308 ok(exp.selected == got.selected, "expected selected %d, got %d\n", exp.selected, got.selected); \
309 ok(exp.anchor == got.anchor, "expected anchor %d, got %d\n", exp.anchor, got.anchor); \
310 ok(exp.caret == got.caret, "expected caret %d, got %d\n", exp.caret, got.caret); \
311 ok(exp.selcount == got.selcount, "expected selcount %d, got %d\n", exp.selcount, got.selcount);
313 static void test_selection(void)
315 static const struct listbox_stat test_nosel = { 0, LB_ERR, 0, 0 };
316 static const struct listbox_stat test_1 = { 0, LB_ERR, 0, 2 };
317 static const struct listbox_stat test_2 = { 0, LB_ERR, 0, 3 };
318 static const struct listbox_stat test_3 = { 0, LB_ERR, 0, 4 };
320 struct listbox_stat answer;
323 trace("testing LB_SELITEMRANGE\n");
325 hLB = create_listbox(LBS_EXTENDEDSEL, 0);
328 listbox_query(hLB, &answer);
329 listbox_test_query(test_nosel, answer);
331 ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, 2));
332 ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
333 listbox_query(hLB, &answer);
334 listbox_test_query(test_1, answer);
336 SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
337 listbox_query(hLB, &answer);
338 listbox_test_query(test_nosel, answer);
340 ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(0, 4));
341 ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
342 listbox_query(hLB, &answer);
343 listbox_test_query(test_3, answer);
345 SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
346 listbox_query(hLB, &answer);
347 listbox_test_query(test_nosel, answer);
349 ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(-5, 5));
350 ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
351 listbox_query(hLB, &answer);
352 listbox_test_query(test_nosel, answer);
354 SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
355 listbox_query(hLB, &answer);
356 listbox_test_query(test_nosel, answer);
358 ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(2, 10));
359 ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
360 listbox_query(hLB, &answer);
361 listbox_test_query(test_1, answer);
363 SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
364 listbox_query(hLB, &answer);
365 listbox_test_query(test_nosel, answer);
367 ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(4, 10));
368 ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
369 listbox_query(hLB, &answer);
370 listbox_test_query(test_nosel, answer);
372 SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
373 listbox_query(hLB, &answer);
374 listbox_test_query(test_nosel, answer);
376 ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(10, 1));
377 ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
378 listbox_query(hLB, &answer);
379 listbox_test_query(test_2, answer);
381 SendMessage(hLB, LB_SETSEL, FALSE, (LPARAM)-1);
382 listbox_query(hLB, &answer);
383 listbox_test_query(test_nosel, answer);
385 ret = SendMessage(hLB, LB_SELITEMRANGE, TRUE, MAKELPARAM(1, -1));
386 ok(ret == LB_OKAY, "LB_SELITEMRANGE returned %d instead of LB_OKAY\n", ret);
387 listbox_query(hLB, &answer);
388 listbox_test_query(test_2, answer);
395 const struct listbox_test SS =
398 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0},
399 { 1, 1, 1, LB_ERR}, {0,0,0,0},
400 { 2, 2, 2, LB_ERR}, {0,0,0,0},
401 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0}};
402 /* {selected, anchor, caret, selcount}{TODO fields} */
403 const struct listbox_test SS_NS =
405 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0},
406 { 1, 1, 1, LB_ERR}, {0,0,0,0},
407 { 2, 2, 2, LB_ERR}, {0,0,0,0},
408 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0}};
409 const struct listbox_test MS =
411 { 0, LB_ERR, 0, 0}, {0,0,0,0},
412 { 1, 1, 1, 1}, {0,0,0,0},
413 { 2, 1, 2, 1}, {0,0,0,0},
414 { 0, LB_ERR, 0, 2}, {0,0,0,0}};
415 const struct listbox_test MS_NS =
416 {{LBS_MULTIPLESEL | LBS_NOSEL},
417 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0},
418 { 1, 1, 1, LB_ERR}, {0,0,0,0},
419 { 2, 2, 2, LB_ERR}, {0,0,0,0},
420 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0}};
421 const struct listbox_test ES =
423 { 0, LB_ERR, 0, 0}, {0,0,0,0},
424 { 1, 1, 1, 1}, {0,0,0,0},
425 { 2, 2, 2, 1}, {0,0,0,0},
426 { 0, LB_ERR, 0, 2}, {0,0,0,0}};
427 const struct listbox_test ES_NS =
428 {{LBS_EXTENDEDSEL | LBS_NOSEL},
429 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0},
430 { 1, 1, 1, LB_ERR}, {0,0,0,0},
431 { 2, 2, 2, LB_ERR}, {0,0,0,0},
432 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0}};
433 const struct listbox_test EMS =
434 {{LBS_EXTENDEDSEL | LBS_MULTIPLESEL},
435 { 0, LB_ERR, 0, 0}, {0,0,0,0},
436 { 1, 1, 1, 1}, {0,0,0,0},
437 { 2, 2, 2, 1}, {0,0,0,0},
438 { 0, LB_ERR, 0, 2}, {0,0,0,0}};
439 const struct listbox_test EMS_NS =
440 {{LBS_EXTENDEDSEL | LBS_MULTIPLESEL | LBS_NOSEL},
441 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0},
442 { 1, 1, 1, LB_ERR}, {0,0,0,0},
443 { 2, 2, 2, LB_ERR}, {0,0,0,0},
444 {LB_ERR, LB_ERR, 0, LB_ERR}, {0,0,0,0}};
446 trace (" Testing single selection...\n");
448 trace (" ... with NOSEL\n");
450 trace (" Testing multiple selection...\n");
452 trace (" ... with NOSEL\n");
454 trace (" Testing extended selection...\n");
456 trace (" ... with NOSEL\n");
458 trace (" Testing extended and multiple selection...\n");
460 trace (" ... with NOSEL\n");