push a68a33e62d41ba03b0e27371e671eaa1e09ebd44
[wine/hacks.git] / dlls / comctl32 / tests / comboex.c
blob57143657e210e88a11107ac1b893e1fefc94d1fb
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
20 #include <assert.h>
21 #include <windows.h>
22 #include <commctrl.h>
24 #include "wine/test.h"
26 static HWND hComboExParentWnd;
27 static HINSTANCE hMainHinst;
28 static const char ComboExTestClass[] = "ComboExTestClass";
30 #define MAX_CHARS 100
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;
42 cbexItem.iItem = idx;
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;
52 cbexItem.iItem = idx;
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;
66 cbItem->iItem = idx;
67 cbItem->cchTextMax = 100;
68 return (LONG)SendMessage(cbex, CBEM_GETITEM, 0, (LPARAM)cbItem);
71 static void test_comboboxex(void) {
72 HWND myHwnd = 0;
73 LONG res = -1;
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);
170 /* Cleanup */
171 HeapFree(GetProcessHeap(), 0, textBuffer);
172 DestroyWindow(myHwnd);
175 static void test_WM_LBUTTONDOWN(void)
177 HWND hComboEx, hCombo, hEdit, hList;
178 COMBOBOXINFO cbInfo;
179 UINT x, y, item_height;
180 LRESULT result;
181 int i, idx;
182 RECT rect;
183 WCHAR buffer[3];
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'};
187 hComboEx = CreateWindowExA(0, WC_COMBOBOXEXA, NULL,
188 WS_VISIBLE|WS_CHILD|CBS_DROPDOWN, 0, 0, 200, 150,
189 hComboExParentWnd, NULL, hMainHinst, NULL);
191 for (i = 0; i < sizeof(choices)/sizeof(UINT); i++){
192 COMBOBOXEXITEMW cbexItem;
193 wsprintfW(buffer, stringFormat, choices[i]);
195 memset(&cbexItem, 0x00, sizeof(cbexItem));
196 cbexItem.mask = CBEIF_TEXT;
197 cbexItem.iItem = i;
198 cbexItem.pszText = buffer;
199 cbexItem.cchTextMax = 0;
200 ok(SendMessageW(hComboEx, CBEM_INSERTITEMW, 0, (LPARAM)&cbexItem) >= 0,
201 "Failed to add item %d\n", i);
204 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
205 hEdit = (HWND)SendMessage(hComboEx, CBEM_GETEDITCONTROL, 0, 0);
207 cbInfo.cbSize = sizeof(COMBOBOXINFO);
208 result = GetComboBoxInfo(hCombo, &cbInfo);
209 ok(result, "Failed to get combobox info structure. LastError=%d\n",
210 GetLastError());
211 hList = cbInfo.hwndList;
213 trace("hWnd=%p, hComboEx=%p, hCombo=%p, hList=%p, hEdit=%p\n",
214 hComboExParentWnd, hComboEx, hCombo, hList, hEdit);
215 ok(GetFocus() == hComboExParentWnd,
216 "Focus not on Main Window, instead on %p\n", GetFocus());
218 /* Click on the button to drop down the list */
219 x = cbInfo.rcButton.left + (cbInfo.rcButton.right-cbInfo.rcButton.left)/2;
220 y = cbInfo.rcButton.top + (cbInfo.rcButton.bottom-cbInfo.rcButton.top)/2;
221 result = SendMessage(hCombo, WM_LBUTTONDOWN, 0, MAKELPARAM(x, y));
222 ok(result, "WM_LBUTTONDOWN was not processed. LastError=%d\n",
223 GetLastError());
224 ok(GetFocus() == hCombo,
225 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
226 GetFocus());
227 ok(SendMessage(hComboEx, CB_GETDROPPEDSTATE, 0, 0),
228 "The dropdown list should have appeared after clicking the button.\n");
229 idx = SendMessage(hCombo, CB_GETTOPINDEX, 0, 0);
230 ok(idx == 0, "For TopIndex expected %d, got %d\n", 0, idx);
232 result = SendMessage(hCombo, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
233 ok(result, "WM_LBUTTONUP was not processed. LastError=%d\n",
234 GetLastError());
235 ok(GetFocus() == hCombo,
236 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
237 GetFocus());
239 /* Click on the 5th item in the list */
240 item_height = SendMessage(hCombo, CB_GETITEMHEIGHT, 0, 0);
241 ok(GetClientRect(hList, &rect), "Failed to get list's client rect.\n");
242 x = rect.left + (rect.right-rect.left)/2;
243 y = item_height/2 + item_height*4;
244 result = SendMessage(hList, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
245 ok(!result, "WM_MOUSEMOVE was not processed. LastError=%d\n",
246 GetLastError());
247 ok(GetFocus() == hCombo,
248 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
249 GetFocus());
251 result = SendMessage(hList, WM_LBUTTONDOWN, 0, MAKELPARAM(x, y));
252 ok(!result, "WM_LBUTTONDOWN was not processed. LastError=%d\n",
253 GetLastError());
254 ok(GetFocus() == hCombo,
255 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
256 GetFocus());
257 ok(SendMessage(hComboEx, CB_GETDROPPEDSTATE, 0, 0),
258 "The dropdown list should still be visible.\n");
260 result = SendMessage(hList, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
261 ok(!result, "WM_LBUTTONUP was not processed. LastError=%d\n",
262 GetLastError());
263 todo_wine ok(GetFocus() == hEdit,
264 "Focus not on ComboBoxEx's Edit Control, instead on %p\n",
265 GetFocus());
266 ok(!SendMessage(hCombo, CB_GETDROPPEDSTATE, 0, 0),
267 "The dropdown list should have been rolled up.\n");
268 idx = SendMessage(hComboEx, CB_GETCURSEL, 0, 0);
269 ok(idx == 4, "Current Selection: expected %d, got %d\n", 4, idx);
271 DestroyWindow(hComboEx);
274 static LRESULT CALLBACK ComboExTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
276 switch(msg) {
278 case WM_DESTROY:
279 PostQuitMessage(0);
280 break;
282 default:
283 return DefWindowProcA(hWnd, msg, wParam, lParam);
286 return 0L;
289 static int init(void)
291 HMODULE hComctl32;
292 BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
293 WNDCLASSA wc;
294 INITCOMMONCONTROLSEX iccex;
296 hComctl32 = GetModuleHandleA("comctl32.dll");
297 pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
298 if (!pInitCommonControlsEx)
300 skip("InitCommonControlsEx() is missing. Skipping the tests\n");
301 return 0;
303 iccex.dwSize = sizeof(iccex);
304 iccex.dwICC = ICC_USEREX_CLASSES;
305 pInitCommonControlsEx(&iccex);
307 wc.style = CS_HREDRAW | CS_VREDRAW;
308 wc.cbClsExtra = 0;
309 wc.cbWndExtra = 0;
310 wc.hInstance = GetModuleHandleA(NULL);
311 wc.hIcon = NULL;
312 wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
313 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
314 wc.lpszMenuName = NULL;
315 wc.lpszClassName = ComboExTestClass;
316 wc.lpfnWndProc = ComboExTestWndProc;
317 RegisterClassA(&wc);
319 hComboExParentWnd = CreateWindowExA(0, ComboExTestClass, "ComboEx test", WS_OVERLAPPEDWINDOW|WS_VISIBLE,
320 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
321 assert(hComboExParentWnd != NULL);
323 hMainHinst = GetModuleHandleA(NULL);
324 return 1;
327 static void cleanup(void)
329 MSG msg;
331 PostMessageA(hComboExParentWnd, WM_CLOSE, 0, 0);
332 while (GetMessageA(&msg,0,0,0)) {
333 TranslateMessage(&msg);
334 DispatchMessageA(&msg);
337 UnregisterClassA(ComboExTestClass, GetModuleHandleA(NULL));
340 START_TEST(comboex)
342 if (!init())
343 return;
345 test_comboboxex();
346 test_WM_LBUTTONDOWN();
348 cleanup();