push aea352fc3df615e3f4b48daf6f897ea93ad1fffd
[wine/hacks.git] / dlls / comctl32 / tests / comboex.c
blobf4d8fc8103f35598a361a494fa74518957952949
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);
175 static LRESULT CALLBACK ComboExTestWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
177 switch(msg) {
179 case WM_DESTROY:
180 PostQuitMessage(0);
181 break;
183 default:
184 return DefWindowProcA(hWnd, msg, wParam, lParam);
187 return 0L;
190 static void init(void) {
191 WNDCLASSA wc;
192 INITCOMMONCONTROLSEX icex;
194 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
195 icex.dwICC = ICC_USEREX_CLASSES;
196 InitCommonControlsEx(&icex);
198 wc.style = CS_HREDRAW | CS_VREDRAW;
199 wc.cbClsExtra = 0;
200 wc.cbWndExtra = 0;
201 wc.hInstance = GetModuleHandleA(NULL);
202 wc.hIcon = NULL;
203 wc.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_ARROW));
204 wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
205 wc.lpszMenuName = NULL;
206 wc.lpszClassName = ComboExTestClass;
207 wc.lpfnWndProc = ComboExTestWndProc;
208 RegisterClassA(&wc);
210 hComboExParentWnd = CreateWindowExA(0, ComboExTestClass, "ComboEx test", WS_OVERLAPPEDWINDOW,
211 CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, GetModuleHandleA(NULL), 0);
212 assert(hComboExParentWnd != NULL);
214 hMainHinst = GetModuleHandleA(NULL);
218 static void cleanup(void)
220 MSG msg;
222 PostMessageA(hComboExParentWnd, WM_CLOSE, 0, 0);
223 while (GetMessageA(&msg,0,0,0)) {
224 TranslateMessage(&msg);
225 DispatchMessageA(&msg);
228 UnregisterClassA(ComboExTestClass, GetModuleHandleA(NULL));
231 START_TEST(comboex)
233 init();
235 test_comboboxex();
237 cleanup();