1 /* Unit test suite for comboex control.
3 * Copyright 2005 Jason Edmeades
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/test.h"
27 #define EDITBOX_SEQ_INDEX 0
28 #define NUM_MSG_SEQUENCES 1
32 #define expect(expected, got) ok(got == expected, "Expected %d, got %d\n", expected, got)
34 static struct msg_sequence *sequences[NUM_MSG_SEQUENCES];
36 static HWND hComboExParentWnd;
37 static HINSTANCE hMainHinst;
38 static const char ComboExTestClass[] = "ComboExTestClass";
40 static BOOL (WINAPI *pSetWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR);
43 static char *textBuffer = NULL;
45 static HWND createComboEx(DWORD style) {
46 return CreateWindowExA(0, WC_COMBOBOXEXA, NULL, style, 0, 0, 300, 300,
47 hComboExParentWnd, NULL, hMainHinst, NULL);
50 static LONG addItem(HWND cbex, int idx, LPTSTR text) {
51 COMBOBOXEXITEM cbexItem;
52 memset(&cbexItem, 0x00, sizeof(cbexItem));
53 cbexItem.mask = CBEIF_TEXT;
55 cbexItem.pszText = text;
56 cbexItem.cchTextMax = 0;
57 return SendMessage(cbex, CBEM_INSERTITEM, 0, (LPARAM)&cbexItem);
60 static LONG setItem(HWND cbex, int idx, LPTSTR text) {
61 COMBOBOXEXITEM cbexItem;
62 memset(&cbexItem, 0x00, sizeof(cbexItem));
63 cbexItem.mask = CBEIF_TEXT;
65 cbexItem.pszText = text;
66 cbexItem.cchTextMax = 0;
67 return SendMessage(cbex, CBEM_SETITEM, 0, (LPARAM)&cbexItem);
70 static LONG delItem(HWND cbex, int idx) {
71 return SendMessage(cbex, CBEM_DELETEITEM, idx, 0);
74 static LONG getItem(HWND cbex, int idx, COMBOBOXEXITEM *cbItem) {
75 memset(cbItem, 0x00, sizeof(COMBOBOXEXITEM));
76 cbItem->mask = CBEIF_TEXT;
77 cbItem->pszText = textBuffer;
79 cbItem->cchTextMax = 100;
80 return SendMessage(cbex, CBEM_GETITEM, 0, (LPARAM)cbItem);
83 static LRESULT WINAPI editbox_subclass_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
85 WNDPROC oldproc = (WNDPROC)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
86 static LONG defwndproc_counter = 0;
90 msg.message = message;
91 msg.flags = sent|wparam|lparam;
92 if (defwndproc_counter) msg.flags |= defwinproc;
97 if (message != WM_PAINT &&
98 message != WM_ERASEBKGND &&
99 message != WM_NCPAINT &&
100 message != WM_NCHITTEST &&
101 message != WM_GETTEXT &&
102 message != WM_GETICON &&
103 message != WM_DEVICECHANGE)
105 add_message(sequences, EDITBOX_SEQ_INDEX, &msg);
108 defwndproc_counter++;
109 ret = CallWindowProcA(oldproc, hwnd, message, wParam, lParam);
110 defwndproc_counter--;
114 static HWND subclass_editbox(HWND hwndComboEx)
119 hwnd = (HWND)SendMessage(hwndComboEx, CBEM_GETEDITCONTROL, 0, 0);
120 oldproc = (WNDPROC)SetWindowLongPtrA(hwnd, GWLP_WNDPROC,
121 (LONG_PTR)editbox_subclass_proc);
122 SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)oldproc);
127 static void test_comboboxex(void) {
130 COMBOBOXEXITEM cbexItem;
131 static TCHAR first_item[] = {'F','i','r','s','t',' ','I','t','e','m',0},
132 second_item[] = {'S','e','c','o','n','d',' ','I','t','e','m',0},
133 third_item[] = {'T','h','i','r','d',' ','I','t','e','m',0},
134 middle_item[] = {'B','e','t','w','e','e','n',' ','F','i','r','s','t',' ','a','n','d',' ',
135 'S','e','c','o','n','d',' ','I','t','e','m','s',0},
136 replacement_item[] = {'B','e','t','w','e','e','n',' ','F','i','r','s','t',' ','a','n','d',' ',
137 'S','e','c','o','n','d',' ','I','t','e','m','s',0},
138 out_of_range_item[] = {'O','u','t',' ','o','f',' ','R','a','n','g','e',' ','I','t','e','m',0};
140 /* Allocate space for result */
141 textBuffer = HeapAlloc(GetProcessHeap(), 0, MAX_CHARS);
143 /* Basic comboboxex test */
144 myHwnd = createComboEx(WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN);
146 /* Add items onto the end of the combobox */
147 res = addItem(myHwnd, -1, first_item);
148 ok(res == 0, "Adding simple item failed (%d)\n", res);
149 res = addItem(myHwnd, -1, second_item);
150 ok(res == 1, "Adding simple item failed (%d)\n", res);
151 res = addItem(myHwnd, 2, third_item);
152 ok(res == 2, "Adding simple item failed (%d)\n", res);
153 res = addItem(myHwnd, 1, middle_item);
154 ok(res == 1, "Inserting simple item failed (%d)\n", res);
156 /* Add an item completely out of range */
157 res = addItem(myHwnd, 99, out_of_range_item);
158 ok(res == -1, "Adding using out of range index worked unexpectedly (%d)\n", res);
159 res = addItem(myHwnd, 5, out_of_range_item);
160 ok(res == -1, "Adding using out of range index worked unexpectedly (%d)\n", res);
161 /* Removed: Causes traps on Windows XP
162 res = addItem(myHwnd, -2, "Out Of Range Item");
163 ok(res == -1, "Adding out of range worked unexpectedly (%ld)\n", res);
166 /* Get an item completely out of range */
167 res = getItem(myHwnd, 99, &cbexItem);
168 ok(res == 0, "Getting item using out of range index worked unexpectedly (%d, %s)\n", res, cbexItem.pszText);
169 res = getItem(myHwnd, 4, &cbexItem);
170 ok(res == 0, "Getting item using out of range index worked unexpectedly (%d, %s)\n", res, cbexItem.pszText);
171 res = getItem(myHwnd, -2, &cbexItem);
172 ok(res == 0, "Getting item using out of range index worked unexpectedly (%d, %s)\n", res, cbexItem.pszText);
174 /* Get an item in range */
175 res = getItem(myHwnd, 0, &cbexItem);
176 ok(res != 0, "Getting item using valid index failed unexpectedly (%d)\n", res);
177 ok(strcmp(first_item, cbexItem.pszText) == 0, "Getting item returned wrong string (%s)\n", cbexItem.pszText);
179 res = getItem(myHwnd, 1, &cbexItem);
180 ok(res != 0, "Getting item using valid index failed unexpectedly (%d)\n", res);
181 ok(strcmp(middle_item, cbexItem.pszText) == 0, "Getting item returned wrong string (%s)\n", cbexItem.pszText);
183 res = getItem(myHwnd, 2, &cbexItem);
184 ok(res != 0, "Getting item using valid index failed unexpectedly (%d)\n", res);
185 ok(strcmp(second_item, cbexItem.pszText) == 0, "Getting item returned wrong string (%s)\n", cbexItem.pszText);
187 res = getItem(myHwnd, 3, &cbexItem);
188 ok(res != 0, "Getting item using valid index failed unexpectedly (%d)\n", res);
189 ok(strcmp(third_item, cbexItem.pszText) == 0, "Getting item returned wrong string (%s)\n", cbexItem.pszText);
191 /* Set an item completely out of range */
192 res = setItem(myHwnd, 99, replacement_item);
193 ok(res == 0, "Setting item using out of range index worked unexpectedly (%d)\n", res);
194 res = setItem(myHwnd, 4, replacement_item);
195 ok(res == 0, "Setting item using out of range index worked unexpectedly (%d)\n", res);
196 res = setItem(myHwnd, -2, replacement_item);
197 ok(res == 0, "Setting item using out of range index worked unexpectedly (%d)\n", res);
199 /* Set an item in range */
200 res = setItem(myHwnd, 0, replacement_item);
201 ok(res != 0, "Setting first item failed (%d)\n", res);
202 res = setItem(myHwnd, 3, replacement_item);
203 ok(res != 0, "Setting last item failed (%d)\n", res);
205 /* Remove items completely out of range (4 items in control at this point) */
206 res = delItem(myHwnd, -1);
207 ok(res == CB_ERR, "Deleting using out of range index worked unexpectedly (%d)\n", res);
208 res = delItem(myHwnd, 4);
209 ok(res == CB_ERR, "Deleting using out of range index worked unexpectedly (%d)\n", res);
211 /* Remove items in range (4 items in control at this point) */
212 res = delItem(myHwnd, 3);
213 ok(res == 3, "Deleting using out of range index failed (%d)\n", res);
214 res = delItem(myHwnd, 0);
215 ok(res == 2, "Deleting using out of range index failed (%d)\n", res);
216 res = delItem(myHwnd, 0);
217 ok(res == 1, "Deleting using out of range index failed (%d)\n", res);
218 res = delItem(myHwnd, 0);
219 ok(res == 0, "Deleting using out of range index failed (%d)\n", res);
221 /* Remove from an empty box */
222 res = delItem(myHwnd, 0);
223 ok(res == CB_ERR, "Deleting using out of range index worked unexpectedly (%d)\n", res);
227 HeapFree(GetProcessHeap(), 0, textBuffer);
228 DestroyWindow(myHwnd);
231 static void test_WM_LBUTTONDOWN(void)
233 HWND hComboEx, hCombo, hEdit, hList;
235 UINT x, y, item_height;
241 static const UINT choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
242 static const WCHAR stringFormat[] = {'%','2','d','\0'};
243 BOOL (WINAPI *pGetComboBoxInfo)(HWND, PCOMBOBOXINFO);
245 pGetComboBoxInfo = (void*)GetProcAddress(GetModuleHandleA("user32.dll"), "GetComboBoxInfo");
246 if (!pGetComboBoxInfo){
247 win_skip("GetComboBoxInfo is not available\n");
251 hComboEx = CreateWindowExA(0, WC_COMBOBOXEXA, NULL,
252 WS_VISIBLE|WS_CHILD|CBS_DROPDOWN, 0, 0, 200, 150,
253 hComboExParentWnd, NULL, hMainHinst, NULL);
255 for (i = 0; i < sizeof(choices)/sizeof(UINT); i++){
256 COMBOBOXEXITEMW cbexItem;
257 wsprintfW(buffer, stringFormat, choices[i]);
259 memset(&cbexItem, 0x00, sizeof(cbexItem));
260 cbexItem.mask = CBEIF_TEXT;
262 cbexItem.pszText = buffer;
263 cbexItem.cchTextMax = 0;
264 ok(SendMessageW(hComboEx, CBEM_INSERTITEMW, 0, (LPARAM)&cbexItem) >= 0,
265 "Failed to add item %d\n", i);
268 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
269 hEdit = (HWND)SendMessage(hComboEx, CBEM_GETEDITCONTROL, 0, 0);
271 cbInfo.cbSize = sizeof(COMBOBOXINFO);
272 result = pGetComboBoxInfo(hCombo, &cbInfo);
273 ok(result, "Failed to get combobox info structure. LastError=%d\n",
275 hList = cbInfo.hwndList;
277 trace("hWnd=%p, hComboEx=%p, hCombo=%p, hList=%p, hEdit=%p\n",
278 hComboExParentWnd, hComboEx, hCombo, hList, hEdit);
279 ok(GetFocus() == hComboExParentWnd,
280 "Focus not on Main Window, instead on %p\n", GetFocus());
282 /* Click on the button to drop down the list */
283 x = cbInfo.rcButton.left + (cbInfo.rcButton.right-cbInfo.rcButton.left)/2;
284 y = cbInfo.rcButton.top + (cbInfo.rcButton.bottom-cbInfo.rcButton.top)/2;
285 result = SendMessage(hCombo, WM_LBUTTONDOWN, 0, MAKELPARAM(x, y));
286 ok(result, "WM_LBUTTONDOWN was not processed. LastError=%d\n",
288 ok(GetFocus() == hCombo ||
289 broken(GetFocus() != hCombo), /* win98 */
290 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
292 ok(SendMessage(hComboEx, CB_GETDROPPEDSTATE, 0, 0),
293 "The dropdown list should have appeared after clicking the button.\n");
294 idx = SendMessage(hCombo, CB_GETTOPINDEX, 0, 0);
295 ok(idx == 0, "For TopIndex expected %d, got %d\n", 0, idx);
297 result = SendMessage(hCombo, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
298 ok(result, "WM_LBUTTONUP was not processed. LastError=%d\n",
300 ok(GetFocus() == hCombo ||
301 broken(GetFocus() != hCombo), /* win98 */
302 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
305 /* Click on the 5th item in the list */
306 item_height = SendMessage(hCombo, CB_GETITEMHEIGHT, 0, 0);
307 ok(GetClientRect(hList, &rect), "Failed to get list's client rect.\n");
308 x = rect.left + (rect.right-rect.left)/2;
309 y = item_height/2 + item_height*4;
310 result = SendMessage(hList, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
311 ok(!result, "WM_MOUSEMOVE was not processed. LastError=%d\n",
313 ok(GetFocus() == hCombo ||
314 broken(GetFocus() != hCombo), /* win98 */
315 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
318 result = SendMessage(hList, WM_LBUTTONDOWN, 0, MAKELPARAM(x, y));
319 ok(!result, "WM_LBUTTONDOWN was not processed. LastError=%d\n",
321 ok(GetFocus() == hCombo ||
322 broken(GetFocus() != hCombo), /* win98 */
323 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
325 ok(SendMessage(hComboEx, CB_GETDROPPEDSTATE, 0, 0),
326 "The dropdown list should still be visible.\n");
328 result = SendMessage(hList, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
329 ok(!result, "WM_LBUTTONUP was not processed. LastError=%d\n",
331 todo_wine ok(GetFocus() == hEdit ||
332 broken(GetFocus() == hCombo), /* win98 */
333 "Focus not on ComboBoxEx's Edit Control, instead on %p\n",
336 result = SendMessage(hCombo, CB_GETDROPPEDSTATE, 0, 0);
338 broken(result != 0), /* win98 */
339 "The dropdown list should have been rolled up.\n");
340 idx = SendMessage(hComboEx, CB_GETCURSEL, 0, 0);
342 broken(idx == -1), /* win98 */
343 "Current Selection: expected %d, got %d\n", 4, idx);
345 DestroyWindow(hComboEx);
348 static void test_CB_GETLBTEXT(void)
352 COMBOBOXEXITEMA item;
355 hCombo = createComboEx(WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN);
357 /* set text to null */
358 addItem(hCombo, 0, NULL);
361 item.mask = CBEIF_TEXT;
365 ret = SendMessage(hCombo, CBEM_GETITEMA, 0, (LPARAM)&item);
366 ok(ret != 0, "CBEM_GETITEM failed\n");
367 ok(buff[0] == 0, "\n");
369 ret = SendMessage(hCombo, CB_GETLBTEXTLEN, 0, 0);
370 ok(ret == 0, "Expected zero length\n");
372 ret = SendMessage(hCombo, CB_GETLBTEXTLEN, 0, 0);
373 ok(ret == 0, "Expected zero length\n");
376 ret = SendMessage(hCombo, CB_GETLBTEXT, 0, (LPARAM)buff);
377 ok(ret == 0, "Expected zero length\n");
378 ok(buff[0] == 0, "Expected null terminator as a string, got %s\n", buff);
380 DestroyWindow(hCombo);
383 static void test_WM_WINDOWPOSCHANGING(void)
391 hCombo = createComboEx(WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN);
392 ok(hCombo != NULL, "createComboEx failed\n");
393 ret = GetWindowRect(hCombo, &rect);
394 ok(ret, "GetWindowRect failed\n");
395 combo_height = rect.bottom - rect.top;
396 ok(combo_height > 0, "wrong combo height\n");
398 /* Test height > combo_height */
401 wp.cx = (rect.right - rect.left);
402 wp.cy = combo_height * 2;
405 wp.hwndInsertAfter = NULL;
407 ret = SendMessageA(hCombo, WM_WINDOWPOSCHANGING, 0, (LPARAM)&wp);
408 ok(ret == 0, "expected 0, got %x", ret);
409 ok(wp.cy == combo_height,
410 "Expected height %d, got %d\n", combo_height, wp.cy);
412 /* Test height < combo_height */
415 wp.cx = (rect.right - rect.left);
416 wp.cy = combo_height / 2;
419 wp.hwndInsertAfter = NULL;
421 ret = SendMessageA(hCombo, WM_WINDOWPOSCHANGING, 0, (LPARAM)&wp);
422 ok(ret == 0, "expected 0, got %x", ret);
423 ok(wp.cy == combo_height,
424 "Expected height %d, got %d\n", combo_height, wp.cy);
426 ret = DestroyWindow(hCombo);
427 ok(ret, "DestroyWindow failed\n");
430 static LRESULT CALLBACK ComboExTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
439 return DefWindowProcA(hWnd, msg, wParam, lParam);
445 static int init(void)
448 BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
450 INITCOMMONCONTROLSEX iccex;
452 hComctl32 = GetModuleHandleA("comctl32.dll");
453 pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
454 if (!pInitCommonControlsEx)
456 win_skip("InitCommonControlsEx() is missing. Skipping the tests\n");
459 iccex.dwSize = sizeof(iccex);
460 iccex.dwICC = ICC_USEREX_CLASSES;
461 pInitCommonControlsEx(&iccex);
463 pSetWindowSubclass = (void*)GetProcAddress(hComctl32, (LPSTR)410);
465 wc.style = CS_HREDRAW | CS_VREDRAW;
468 wc.hInstance = GetModuleHandleA(NULL);
470 wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
471 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
472 wc.lpszMenuName = NULL;
473 wc.lpszClassName = ComboExTestClass;
474 wc.lpfnWndProc = ComboExTestWndProc;
477 hComboExParentWnd = CreateWindowExA(0, ComboExTestClass, "ComboEx test", WS_OVERLAPPEDWINDOW|WS_VISIBLE,
478 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
479 assert(hComboExParentWnd != NULL);
481 hMainHinst = GetModuleHandleA(NULL);
485 static void cleanup(void)
489 PostMessageA(hComboExParentWnd, WM_CLOSE, 0, 0);
490 while (GetMessageA(&msg,0,0,0)) {
491 TranslateMessage(&msg);
492 DispatchMessageA(&msg);
495 DestroyWindow(hComboExParentWnd);
496 UnregisterClassA(ComboExTestClass, GetModuleHandleA(NULL));
499 static void test_comboboxex_subclass(void)
501 HWND hComboEx, hCombo, hEdit;
503 hComboEx = createComboEx(WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN);
505 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
506 ok(hCombo != NULL, "Failed to get internal combo\n");
507 hEdit = (HWND)SendMessage(hComboEx, CBEM_GETEDITCONTROL, 0, 0);
508 ok(hEdit != NULL, "Failed to get internal edit\n");
510 if (pSetWindowSubclass)
512 ok(GetPropA(hCombo, "CC32SubclassInfo") != NULL, "Expected CC32SubclassInfo property\n");
513 ok(GetPropA(hEdit, "CC32SubclassInfo") != NULL, "Expected CC32SubclassInfo property\n");
516 DestroyWindow(hComboEx);
519 static const struct message test_setitem_edit_seq[] = {
520 { WM_SETTEXT, sent|id, 0, 0, EDITBOX_ID },
521 { EM_SETSEL, sent|id|wparam|lparam, 0, 0, EDITBOX_ID },
522 { EM_SETSEL, sent|id|wparam|lparam, 0, -1, EDITBOX_ID },
526 static void test_get_set_item(void)
528 char textA[] = "test";
530 COMBOBOXEXITEMA item;
533 hComboEx = createComboEx(WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN);
535 subclass_editbox(hComboEx);
537 flush_sequences(sequences, NUM_MSG_SEQUENCES);
539 memset(&item, 0, sizeof(item));
540 item.mask = CBEIF_TEXT;
541 item.pszText = textA;
543 ret = SendMessage(hComboEx, CBEM_SETITEMA, 0, (LPARAM)&item);
546 ok_sequence(sequences, EDITBOX_SEQ_INDEX, test_setitem_edit_seq, "set item data for edit", FALSE);
549 item.mask = CBEIF_LPARAM;
551 item.lParam = 0xdeadbeef;
552 ret = SendMessage(hComboEx, CBEM_GETITEMA, 0, (LPARAM)&item);
554 ok(item.lParam == 0, "Expected zero, got %lx\n", item.lParam);
556 item.lParam = 0x1abe11ed;
557 ret = SendMessage(hComboEx, CBEM_SETITEMA, 0, (LPARAM)&item);
561 ret = SendMessage(hComboEx, CBEM_GETITEMA, 0, (LPARAM)&item);
563 ok(item.lParam == 0x1abe11ed, "Expected 0x1abe11ed, got %lx\n", item.lParam);
565 DestroyWindow(hComboEx);
573 init_msg_sequences(sequences, NUM_MSG_SEQUENCES);
576 test_WM_LBUTTONDOWN();
578 test_WM_WINDOWPOSCHANGING();
579 test_comboboxex_subclass();