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"
26 static HWND hComboExParentWnd;
27 static HINSTANCE hMainHinst;
28 static const char ComboExTestClass[] = "ComboExTestClass";
31 static char *textBuffer = NULL;
33 static HWND createComboEx(DWORD style) {
34 return CreateWindowExA(0, WC_COMBOBOXEXA, NULL, style, 0, 0, 300, 300,
35 hComboExParentWnd, NULL, hMainHinst, NULL);
38 static LONG addItem(HWND cbex, int idx, LPTSTR text) {
39 COMBOBOXEXITEM cbexItem;
40 memset(&cbexItem, 0x00, sizeof(cbexItem));
41 cbexItem.mask = CBEIF_TEXT;
43 cbexItem.pszText = text;
44 cbexItem.cchTextMax = 0;
45 return (LONG)SendMessage(cbex, CBEM_INSERTITEM, 0,(LPARAM)&cbexItem);
48 static LONG setItem(HWND cbex, int idx, LPTSTR text) {
49 COMBOBOXEXITEM cbexItem;
50 memset(&cbexItem, 0x00, sizeof(cbexItem));
51 cbexItem.mask = CBEIF_TEXT;
53 cbexItem.pszText = text;
54 cbexItem.cchTextMax = 0;
55 return (LONG)SendMessage(cbex, CBEM_SETITEM, 0,(LPARAM)&cbexItem);
58 static LONG delItem(HWND cbex, int idx) {
59 return (LONG)SendMessage(cbex, CBEM_DELETEITEM, (LPARAM)idx, 0);
62 static LONG getItem(HWND cbex, int idx, COMBOBOXEXITEM *cbItem) {
63 memset(cbItem, 0x00, sizeof(COMBOBOXEXITEM));
64 cbItem->mask = CBEIF_TEXT;
65 cbItem->pszText = textBuffer;
67 cbItem->cchTextMax = 100;
68 return (LONG)SendMessage(cbex, CBEM_GETITEM, 0, (LPARAM)cbItem);
71 static void test_comboboxex(void) {
74 COMBOBOXEXITEM cbexItem;
75 static TCHAR first_item[] = {'F','i','r','s','t',' ','I','t','e','m',0},
76 second_item[] = {'S','e','c','o','n','d',' ','I','t','e','m',0},
77 third_item[] = {'T','h','i','r','d',' ','I','t','e','m',0},
78 middle_item[] = {'B','e','t','w','e','e','n',' ','F','i','r','s','t',' ','a','n','d',' ',
79 'S','e','c','o','n','d',' ','I','t','e','m','s',0},
80 replacement_item[] = {'B','e','t','w','e','e','n',' ','F','i','r','s','t',' ','a','n','d',' ',
81 'S','e','c','o','n','d',' ','I','t','e','m','s',0},
82 out_of_range_item[] = {'O','u','t',' ','o','f',' ','R','a','n','g','e',' ','I','t','e','m',0};
84 /* Allocate space for result */
85 textBuffer = HeapAlloc(GetProcessHeap(), 0, MAX_CHARS);
87 /* Basic comboboxex test */
88 myHwnd = createComboEx(WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN);
90 /* Add items onto the end of the combobox */
91 res = addItem(myHwnd, -1, first_item);
92 ok(res == 0, "Adding simple item failed (%d)\n", res);
93 res = addItem(myHwnd, -1, second_item);
94 ok(res == 1, "Adding simple item failed (%d)\n", res);
95 res = addItem(myHwnd, 2, third_item);
96 ok(res == 2, "Adding simple item failed (%d)\n", res);
97 res = addItem(myHwnd, 1, middle_item);
98 ok(res == 1, "Inserting simple item failed (%d)\n", res);
100 /* Add an item completely out of range */
101 res = addItem(myHwnd, 99, out_of_range_item);
102 ok(res == -1, "Adding using out of range index worked unexpectedly (%d)\n", res);
103 res = addItem(myHwnd, 5, out_of_range_item);
104 ok(res == -1, "Adding using out of range index worked unexpectedly (%d)\n", res);
105 /* Removed: Causes traps on Windows XP
106 res = addItem(myHwnd, -2, "Out Of Range Item");
107 ok(res == -1, "Adding out of range worked unexpectedly (%ld)\n", res);
110 /* Get an item completely out of range */
111 res = getItem(myHwnd, 99, &cbexItem);
112 ok(res == 0, "Getting item using out of range index worked unexpectedly (%d, %s)\n", res, cbexItem.pszText);
113 res = getItem(myHwnd, 4, &cbexItem);
114 ok(res == 0, "Getting item using out of range index worked unexpectedly (%d, %s)\n", res, cbexItem.pszText);
115 res = getItem(myHwnd, -2, &cbexItem);
116 ok(res == 0, "Getting item using out of range index worked unexpectedly (%d, %s)\n", res, cbexItem.pszText);
118 /* Get an item in range */
119 res = getItem(myHwnd, 0, &cbexItem);
120 ok(res != 0, "Getting item using valid index failed unexpectedly (%d)\n", res);
121 ok(strcmp(first_item, cbexItem.pszText) == 0, "Getting item returned wrong string (%s)\n", cbexItem.pszText);
123 res = getItem(myHwnd, 1, &cbexItem);
124 ok(res != 0, "Getting item using valid index failed unexpectedly (%d)\n", res);
125 ok(strcmp(middle_item, cbexItem.pszText) == 0, "Getting item returned wrong string (%s)\n", cbexItem.pszText);
127 res = getItem(myHwnd, 2, &cbexItem);
128 ok(res != 0, "Getting item using valid index failed unexpectedly (%d)\n", res);
129 ok(strcmp(second_item, cbexItem.pszText) == 0, "Getting item returned wrong string (%s)\n", cbexItem.pszText);
131 res = getItem(myHwnd, 3, &cbexItem);
132 ok(res != 0, "Getting item using valid index failed unexpectedly (%d)\n", res);
133 ok(strcmp(third_item, cbexItem.pszText) == 0, "Getting item returned wrong string (%s)\n", cbexItem.pszText);
135 /* Set an item completely out of range */
136 res = setItem(myHwnd, 99, replacement_item);
137 ok(res == 0, "Setting item using out of range index worked unexpectedly (%d)\n", res);
138 res = setItem(myHwnd, 4, replacement_item);
139 ok(res == 0, "Setting item using out of range index worked unexpectedly (%d)\n", res);
140 res = setItem(myHwnd, -2, replacement_item);
141 ok(res == 0, "Setting item using out of range index worked unexpectedly (%d)\n", res);
143 /* Set an item in range */
144 res = setItem(myHwnd, 0, replacement_item);
145 ok(res != 0, "Setting first item failed (%d)\n", res);
146 res = setItem(myHwnd, 3, replacement_item);
147 ok(res != 0, "Setting last item failed (%d)\n", res);
149 /* Remove items completely out of range (4 items in control at this point) */
150 res = delItem(myHwnd, -1);
151 ok(res == CB_ERR, "Deleting using out of range index worked unexpectedly (%d)\n", res);
152 res = delItem(myHwnd, 4);
153 ok(res == CB_ERR, "Deleting using out of range index worked unexpectedly (%d)\n", res);
155 /* Remove items in range (4 items in control at this point) */
156 res = delItem(myHwnd, 3);
157 ok(res == 3, "Deleting using out of range index failed (%d)\n", res);
158 res = delItem(myHwnd, 0);
159 ok(res == 2, "Deleting using out of range index failed (%d)\n", res);
160 res = delItem(myHwnd, 0);
161 ok(res == 1, "Deleting using out of range index failed (%d)\n", res);
162 res = delItem(myHwnd, 0);
163 ok(res == 0, "Deleting using out of range index failed (%d)\n", res);
165 /* Remove from an empty box */
166 res = delItem(myHwnd, 0);
167 ok(res == CB_ERR, "Deleting using out of range index worked unexpectedly (%d)\n", res);
171 HeapFree(GetProcessHeap(), 0, textBuffer);
172 DestroyWindow(myHwnd);
175 static void test_WM_LBUTTONDOWN(void)
177 HWND hComboEx, hCombo, hEdit, hList;
179 UINT x, y, item_height;
184 static const UINT choices[] = {8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72};
185 static const WCHAR stringFormat[] = {'%','2','d','\0'};
186 BOOL (WINAPI *pGetComboBoxInfo)(HWND, PCOMBOBOXINFO);
188 pGetComboBoxInfo = (void*)GetProcAddress(GetModuleHandleA("user32.dll"), "GetComboBoxInfo");
189 if (!pGetComboBoxInfo){
190 skip("GetComboBoxInfo is not available\n");
194 hComboEx = CreateWindowExA(0, WC_COMBOBOXEXA, NULL,
195 WS_VISIBLE|WS_CHILD|CBS_DROPDOWN, 0, 0, 200, 150,
196 hComboExParentWnd, NULL, hMainHinst, NULL);
198 for (i = 0; i < sizeof(choices)/sizeof(UINT); i++){
199 COMBOBOXEXITEMW cbexItem;
200 wsprintfW(buffer, stringFormat, choices[i]);
202 memset(&cbexItem, 0x00, sizeof(cbexItem));
203 cbexItem.mask = CBEIF_TEXT;
205 cbexItem.pszText = buffer;
206 cbexItem.cchTextMax = 0;
207 ok(SendMessageW(hComboEx, CBEM_INSERTITEMW, 0, (LPARAM)&cbexItem) >= 0,
208 "Failed to add item %d\n", i);
211 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
212 hEdit = (HWND)SendMessage(hComboEx, CBEM_GETEDITCONTROL, 0, 0);
214 cbInfo.cbSize = sizeof(COMBOBOXINFO);
215 result = pGetComboBoxInfo(hCombo, &cbInfo);
216 ok(result, "Failed to get combobox info structure. LastError=%d\n",
218 hList = cbInfo.hwndList;
220 trace("hWnd=%p, hComboEx=%p, hCombo=%p, hList=%p, hEdit=%p\n",
221 hComboExParentWnd, hComboEx, hCombo, hList, hEdit);
222 ok(GetFocus() == hComboExParentWnd,
223 "Focus not on Main Window, instead on %p\n", GetFocus());
225 /* Click on the button to drop down the list */
226 x = cbInfo.rcButton.left + (cbInfo.rcButton.right-cbInfo.rcButton.left)/2;
227 y = cbInfo.rcButton.top + (cbInfo.rcButton.bottom-cbInfo.rcButton.top)/2;
228 result = SendMessage(hCombo, WM_LBUTTONDOWN, 0, MAKELPARAM(x, y));
229 ok(result, "WM_LBUTTONDOWN was not processed. LastError=%d\n",
231 ok(GetFocus() == hCombo,
232 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
234 ok(SendMessage(hComboEx, CB_GETDROPPEDSTATE, 0, 0),
235 "The dropdown list should have appeared after clicking the button.\n");
236 idx = SendMessage(hCombo, CB_GETTOPINDEX, 0, 0);
237 ok(idx == 0, "For TopIndex expected %d, got %d\n", 0, idx);
239 result = SendMessage(hCombo, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
240 ok(result, "WM_LBUTTONUP was not processed. LastError=%d\n",
242 ok(GetFocus() == hCombo,
243 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
246 /* Click on the 5th item in the list */
247 item_height = SendMessage(hCombo, CB_GETITEMHEIGHT, 0, 0);
248 ok(GetClientRect(hList, &rect), "Failed to get list's client rect.\n");
249 x = rect.left + (rect.right-rect.left)/2;
250 y = item_height/2 + item_height*4;
251 result = SendMessage(hList, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
252 ok(!result, "WM_MOUSEMOVE was not processed. LastError=%d\n",
254 ok(GetFocus() == hCombo,
255 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
258 result = SendMessage(hList, WM_LBUTTONDOWN, 0, MAKELPARAM(x, y));
259 ok(!result, "WM_LBUTTONDOWN was not processed. LastError=%d\n",
261 ok(GetFocus() == hCombo,
262 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
264 ok(SendMessage(hComboEx, CB_GETDROPPEDSTATE, 0, 0),
265 "The dropdown list should still be visible.\n");
267 result = SendMessage(hList, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
268 ok(!result, "WM_LBUTTONUP was not processed. LastError=%d\n",
270 todo_wine ok(GetFocus() == hEdit,
271 "Focus not on ComboBoxEx's Edit Control, instead on %p\n",
273 ok(!SendMessage(hCombo, CB_GETDROPPEDSTATE, 0, 0),
274 "The dropdown list should have been rolled up.\n");
275 idx = SendMessage(hComboEx, CB_GETCURSEL, 0, 0);
276 ok(idx == 4, "Current Selection: expected %d, got %d\n", 4, idx);
278 DestroyWindow(hComboEx);
281 static LRESULT CALLBACK ComboExTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
290 return DefWindowProcA(hWnd, msg, wParam, lParam);
296 static int init(void)
299 BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
301 INITCOMMONCONTROLSEX iccex;
303 hComctl32 = GetModuleHandleA("comctl32.dll");
304 pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
305 if (!pInitCommonControlsEx)
307 skip("InitCommonControlsEx() is missing. Skipping the tests\n");
310 iccex.dwSize = sizeof(iccex);
311 iccex.dwICC = ICC_USEREX_CLASSES;
312 pInitCommonControlsEx(&iccex);
314 wc.style = CS_HREDRAW | CS_VREDRAW;
317 wc.hInstance = GetModuleHandleA(NULL);
319 wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
320 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
321 wc.lpszMenuName = NULL;
322 wc.lpszClassName = ComboExTestClass;
323 wc.lpfnWndProc = ComboExTestWndProc;
326 hComboExParentWnd = CreateWindowExA(0, ComboExTestClass, "ComboEx test", WS_OVERLAPPEDWINDOW|WS_VISIBLE,
327 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
328 assert(hComboExParentWnd != NULL);
330 hMainHinst = GetModuleHandleA(NULL);
334 static void cleanup(void)
338 PostMessageA(hComboExParentWnd, WM_CLOSE, 0, 0);
339 while (GetMessageA(&msg,0,0,0)) {
340 TranslateMessage(&msg);
341 DispatchMessageA(&msg);
344 UnregisterClassA(ComboExTestClass, GetModuleHandleA(NULL));
353 test_WM_LBUTTONDOWN();