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 win_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 broken(GetFocus() != hCombo
), /* win98 */
233 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
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
);
240 result
= SendMessage(hCombo
, WM_LBUTTONUP
, 0, MAKELPARAM(x
, y
));
241 ok(result
, "WM_LBUTTONUP was not processed. LastError=%d\n",
243 ok(GetFocus() == hCombo
||
244 broken(GetFocus() != hCombo
), /* win98 */
245 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
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",
256 ok(GetFocus() == hCombo
||
257 broken(GetFocus() != hCombo
), /* win98 */
258 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
261 result
= SendMessage(hList
, WM_LBUTTONDOWN
, 0, MAKELPARAM(x
, y
));
262 ok(!result
, "WM_LBUTTONDOWN was not processed. LastError=%d\n",
264 ok(GetFocus() == hCombo
||
265 broken(GetFocus() != hCombo
), /* win98 */
266 "Focus not on ComboBoxEx's ComboBox Control, instead on %p\n",
268 ok(SendMessage(hComboEx
, CB_GETDROPPEDSTATE
, 0, 0),
269 "The dropdown list should still be visible.\n");
271 result
= SendMessage(hList
, WM_LBUTTONUP
, 0, MAKELPARAM(x
, y
));
272 ok(!result
, "WM_LBUTTONUP was not processed. LastError=%d\n",
274 todo_wine
ok(GetFocus() == hEdit
||
275 broken(GetFocus() == hCombo
), /* win98 */
276 "Focus not on ComboBoxEx's Edit Control, instead on %p\n",
279 result
= SendMessage(hCombo
, CB_GETDROPPEDSTATE
, 0, 0);
281 broken(result
!= 0), /* win98 */
282 "The dropdown list should have been rolled up.\n");
283 idx
= SendMessage(hComboEx
, CB_GETCURSEL
, 0, 0);
285 broken(idx
== -1), /* win98 */
286 "Current Selection: expected %d, got %d\n", 4, idx
);
288 DestroyWindow(hComboEx
);
291 static void test_CB_GETLBTEXT(void)
295 COMBOBOXEXITEMA item
;
298 hCombo
= createComboEx(WS_BORDER
| WS_VISIBLE
| WS_CHILD
| CBS_DROPDOWN
);
300 /* set text to null */
301 addItem(hCombo
, 0, NULL
);
304 item
.mask
= CBEIF_TEXT
;
308 ret
= SendMessage(hCombo
, CBEM_GETITEMA
, 0, (LPARAM
)&item
);
309 ok(ret
!= 0, "CBEM_GETITEM failed\n");
310 ok(buff
[0] == 0, "\n");
312 ret
= SendMessage(hCombo
, CB_GETLBTEXTLEN
, 0, 0);
313 ok(ret
== 0, "Expected zero length\n");
315 ret
= SendMessage(hCombo
, CB_GETLBTEXTLEN
, 0, 0);
316 ok(ret
== 0, "Expected zero length\n");
319 ret
= SendMessage(hCombo
, CB_GETLBTEXT
, 0, (LPARAM
)buff
);
320 ok(ret
== 0, "Expected zero length\n");
321 ok(buff
[0] == 0, "Expected null terminator as a string, got %s\n", buff
);
323 DestroyWindow(hCombo
);
326 static LRESULT CALLBACK
ComboExTestWndProc(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
335 return DefWindowProcA(hWnd
, msg
, wParam
, lParam
);
341 static int init(void)
344 BOOL (WINAPI
*pInitCommonControlsEx
)(const INITCOMMONCONTROLSEX
*);
346 INITCOMMONCONTROLSEX iccex
;
348 hComctl32
= GetModuleHandleA("comctl32.dll");
349 pInitCommonControlsEx
= (void*)GetProcAddress(hComctl32
, "InitCommonControlsEx");
350 if (!pInitCommonControlsEx
)
352 win_skip("InitCommonControlsEx() is missing. Skipping the tests\n");
355 iccex
.dwSize
= sizeof(iccex
);
356 iccex
.dwICC
= ICC_USEREX_CLASSES
;
357 pInitCommonControlsEx(&iccex
);
359 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
362 wc
.hInstance
= GetModuleHandleA(NULL
);
364 wc
.hCursor
= LoadCursorA(NULL
, IDC_ARROW
);
365 wc
.hbrBackground
= GetSysColorBrush(COLOR_WINDOW
);
366 wc
.lpszMenuName
= NULL
;
367 wc
.lpszClassName
= ComboExTestClass
;
368 wc
.lpfnWndProc
= ComboExTestWndProc
;
371 hComboExParentWnd
= CreateWindowExA(0, ComboExTestClass
, "ComboEx test", WS_OVERLAPPEDWINDOW
|WS_VISIBLE
,
372 CW_USEDEFAULT
, CW_USEDEFAULT
, 680, 260, NULL
, NULL
, GetModuleHandleA(NULL
), 0);
373 assert(hComboExParentWnd
!= NULL
);
375 hMainHinst
= GetModuleHandleA(NULL
);
379 static void cleanup(void)
383 PostMessageA(hComboExParentWnd
, WM_CLOSE
, 0, 0);
384 while (GetMessageA(&msg
,0,0,0)) {
385 TranslateMessage(&msg
);
386 DispatchMessageA(&msg
);
389 DestroyWindow(hComboExParentWnd
);
390 UnregisterClassA(ComboExTestClass
, GetModuleHandleA(NULL
));
399 test_WM_LBUTTONDOWN();