1e741924ce8697c2e3295c3ad99018ff3ac6eea7
[reactos.git] / rostests / winetests / comctl32 / comboex.c
1 /* Unit test suite for comboex control.
2 *
3 * Copyright 2005 Jason Edmeades
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include <assert.h>
21 #include <windows.h>
22 #include <commctrl.h>
23
24 #include "wine/test.h"
25
26 static HWND hComboExParentWnd;
27 static HINSTANCE hMainHinst;
28 static const char ComboExTestClass[] = "ComboExTestClass";
29
30 #define MAX_CHARS 100
31 static char *textBuffer = NULL;
32
33 static HWND createComboEx(DWORD style) {
34 return CreateWindowExA(0, WC_COMBOBOXEXA, NULL, style, 0, 0, 300, 300,
35 hComboExParentWnd, NULL, hMainHinst, NULL);
36 }
37
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);
46 }
47
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);
56 }
57
58 static LONG delItem(HWND cbex, int idx) {
59 return (LONG)SendMessage(cbex, CBEM_DELETEITEM, (LPARAM)idx, 0);
60 }
61
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);
69 }
70
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};
83
84 /* Allocate space for result */
85 textBuffer = HeapAlloc(GetProcessHeap(), 0, MAX_CHARS);
86
87 /* Basic comboboxex test */
88 myHwnd = createComboEx(WS_BORDER | WS_VISIBLE | WS_CHILD | CBS_DROPDOWN);
89
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);
99
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);
108 */
109
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);
117
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);
122
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);
126
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);
130
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);
134
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);
142
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);
148
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);
154
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);
164
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);
168
169
170 /* Cleanup */
171 HeapFree(GetProcessHeap(), 0, textBuffer);
172 DestroyWindow(myHwnd);
173 }
174
175 static void test_WM_LBUTTONDOWN(void)
176 {
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'};
186 BOOL (WINAPI *pGetComboBoxInfo)(HWND, PCOMBOBOXINFO);
187
188 pGetComboBoxInfo = (void*)GetProcAddress(GetModuleHandleA("user32.dll"), "GetComboBoxInfo");
189 if (!pGetComboBoxInfo){
190 win_skip("GetComboBoxInfo is not available\n");
191 return;
192 }
193
194 hComboEx = CreateWindowExA(0, WC_COMBOBOXEXA, NULL,
195 WS_VISIBLE|WS_CHILD|CBS_DROPDOWN, 0, 0, 200, 150,
196 hComboExParentWnd, NULL, hMainHinst, NULL);
197
198 for (i = 0; i < sizeof(choices)/sizeof(UINT); i++){
199 COMBOBOXEXITEMW cbexItem;
200 wsprintfW(buffer, stringFormat, choices[i]);
201
202 memset(&cbexItem, 0x00, sizeof(cbexItem));
203 cbexItem.mask = CBEIF_TEXT;
204 cbexItem.iItem = i;
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);
209 }
210
211 hCombo = (HWND)SendMessage(hComboEx, CBEM_GETCOMBOCONTROL, 0, 0);
212 hEdit = (HWND)SendMessage(hComboEx, CBEM_GETEDITCONTROL, 0, 0);
213
214 cbInfo.cbSize = sizeof(COMBOBOXINFO);
215 result = pGetComboBoxInfo(hCombo, &cbInfo);
216 ok(result, "Failed to get combobox info structure. LastError=%d\n",
217 GetLastError());
218 hList = cbInfo.hwndList;
219
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());
224
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",
230 GetLastError());
231 ok(GetFocus() == hCombo ||
232 broken(GetFocus() != hCombo), /* win98 */
233 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
234 GetFocus());
235 ok(SendMessage(hComboEx, CB_GETDROPPEDSTATE, 0, 0),
236 "The dropdown list should have appeared after clicking the button.\n");
237 idx = SendMessage(hCombo, CB_GETTOPINDEX, 0, 0);
238 ok(idx == 0, "For TopIndex expected %d, got %d\n", 0, idx);
239
240 result = SendMessage(hCombo, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
241 ok(result, "WM_LBUTTONUP was not processed. LastError=%d\n",
242 GetLastError());
243 ok(GetFocus() == hCombo ||
244 broken(GetFocus() != hCombo), /* win98 */
245 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
246 GetFocus());
247
248 /* Click on the 5th item in the list */
249 item_height = SendMessage(hCombo, CB_GETITEMHEIGHT, 0, 0);
250 ok(GetClientRect(hList, &rect), "Failed to get list's client rect.\n");
251 x = rect.left + (rect.right-rect.left)/2;
252 y = item_height/2 + item_height*4;
253 result = SendMessage(hList, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
254 ok(!result, "WM_MOUSEMOVE was not processed. LastError=%d\n",
255 GetLastError());
256 ok(GetFocus() == hCombo ||
257 broken(GetFocus() != hCombo), /* win98 */
258 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
259 GetFocus());
260
261 result = SendMessage(hList, WM_LBUTTONDOWN, 0, MAKELPARAM(x, y));
262 ok(!result, "WM_LBUTTONDOWN was not processed. LastError=%d\n",
263 GetLastError());
264 ok(GetFocus() == hCombo ||
265 broken(GetFocus() != hCombo), /* win98 */
266 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
267 GetFocus());
268 ok(SendMessage(hComboEx, CB_GETDROPPEDSTATE, 0, 0),
269 "The dropdown list should still be visible.\n");
270
271 result = SendMessage(hList, WM_LBUTTONUP, 0, MAKELPARAM(x, y));
272 ok(!result, "WM_LBUTTONUP was not processed. LastError=%d\n",
273 GetLastError());
274 todo_wine ok(GetFocus() == hEdit ||
275 broken(GetFocus() == hCombo), /* win98 */
276 "Focus not on ComboBoxEx's Edit Control, instead on %p\n",
277 GetFocus());
278
279 result = SendMessage(hCombo, CB_GETDROPPEDSTATE, 0, 0);
280 ok(!result ||
281 broken(result != 0), /* win98 */
282 "The dropdown list should have been rolled up.\n");
283 idx = SendMessage(hComboEx, CB_GETCURSEL, 0, 0);
284 ok(idx == 4 ||
285 broken(idx == -1), /* win98 */
286 "Current Selection: expected %d, got %d\n", 4, idx);
287
288 DestroyWindow(hComboEx);
289 }
290
291 static LRESULT CALLBACK ComboExTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
292 {
293 switch(msg) {
294
295 case WM_DESTROY:
296 PostQuitMessage(0);
297 break;
298
299 default:
300 return DefWindowProcA(hWnd, msg, wParam, lParam);
301 }
302
303 return 0L;
304 }
305
306 static int init(void)
307 {
308 HMODULE hComctl32;
309 BOOL (WINAPI *pInitCommonControlsEx)(const INITCOMMONCONTROLSEX*);
310 WNDCLASSA wc;
311 INITCOMMONCONTROLSEX iccex;
312
313 hComctl32 = GetModuleHandleA("comctl32.dll");
314 pInitCommonControlsEx = (void*)GetProcAddress(hComctl32, "InitCommonControlsEx");
315 if (!pInitCommonControlsEx)
316 {
317 win_skip("InitCommonControlsEx() is missing. Skipping the tests\n");
318 return 0;
319 }
320 iccex.dwSize = sizeof(iccex);
321 iccex.dwICC = ICC_USEREX_CLASSES;
322 pInitCommonControlsEx(&iccex);
323
324 wc.style = CS_HREDRAW | CS_VREDRAW;
325 wc.cbClsExtra = 0;
326 wc.cbWndExtra = 0;
327 wc.hInstance = GetModuleHandleA(NULL);
328 wc.hIcon = NULL;
329 wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
330 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
331 wc.lpszMenuName = NULL;
332 wc.lpszClassName = ComboExTestClass;
333 wc.lpfnWndProc = ComboExTestWndProc;
334 RegisterClassA(&wc);
335
336 hComboExParentWnd = CreateWindowExA(0, ComboExTestClass, "ComboEx test", WS_OVERLAPPEDWINDOW|WS_VISIBLE,
337 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
338 assert(hComboExParentWnd != NULL);
339
340 hMainHinst = GetModuleHandleA(NULL);
341 return 1;
342 }
343
344 static void cleanup(void)
345 {
346 MSG msg;
347
348 PostMessageA(hComboExParentWnd, WM_CLOSE, 0, 0);
349 while (GetMessageA(&msg,0,0,0)) {
350 TranslateMessage(&msg);
351 DispatchMessageA(&msg);
352 }
353
354 DestroyWindow(hComboExParentWnd);
355 UnregisterClassA(ComboExTestClass, GetModuleHandleA(NULL));
356 }
357
358 START_TEST(comboex)
359 {
360 if (!init())
361 return;
362
363 test_comboboxex();
364 test_WM_LBUTTONDOWN();
365
366 cleanup();
367 }