[BROWSEUI_WINETEST]
[reactos.git] / rostests / winetests / browseui / autocomplete.c
1 /* Unit tests for autocomplete
2 *
3 * Copyright 2007 Mikolaj Zalewski
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 <stdarg.h>
22
23 #include <initguid.h>
24 #include <windows.h>
25 #include <shlobj.h>
26 #include <shlwapi.h>
27 #include <shlguid.h>
28
29 #include "wine/test.h"
30
31 #define stop_on_error(exp) \
32 { \
33 HRESULT res = (exp); \
34 if (FAILED(res)) \
35 { \
36 ok(FALSE, #exp " failed: %x\n", res); \
37 return; \
38 } \
39 }
40
41 #define ole_ok(exp) \
42 { \
43 HRESULT res = (exp); \
44 if (res != S_OK) \
45 ok(FALSE, #exp " failed: %x\n", res); \
46 }
47
48 static LPWSTR strdup_AtoW(LPCSTR str)
49 {
50 int size = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
51 LPWSTR wstr = CoTaskMemAlloc((size + 1)*sizeof(WCHAR));
52 MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, size+1);
53 return wstr;
54 }
55
56 typedef struct
57 {
58 IEnumStringVtbl *vtbl;
59 IACListVtbl *aclVtbl;
60 LONG ref;
61 HRESULT expret;
62 INT expcount;
63 INT pos;
64 INT limit;
65 const char **data;
66 } TestACL;
67
68 extern IEnumStringVtbl TestACLVtbl;
69 extern IACListVtbl TestACL_ACListVtbl;
70
71 static TestACL *impl_from_IACList(IACList *iface)
72 {
73 return (TestACL *)((char *)iface - FIELD_OFFSET(TestACL, aclVtbl));
74 }
75
76 static TestACL *TestACL_Constructor(int limit, const char **strings)
77 {
78 TestACL *This = CoTaskMemAlloc(sizeof(TestACL));
79 ZeroMemory(This, sizeof(*This));
80 This->vtbl = &TestACLVtbl;
81 This->aclVtbl = &TestACL_ACListVtbl;
82 This->ref = 1;
83 This->expret = S_OK;
84 This->limit = limit;
85 This->data = strings;
86 return This;
87 }
88
89 static ULONG STDMETHODCALLTYPE TestACL_AddRef(IEnumString *iface)
90 {
91 TestACL *This = (TestACL *)iface;
92 trace("ACL(%p): addref (%d)\n", This, This->ref+1);
93 return InterlockedIncrement(&This->ref);
94 }
95
96 static ULONG STDMETHODCALLTYPE TestACL_Release(IEnumString *iface)
97 {
98 TestACL *This = (TestACL *)iface;
99 ULONG res;
100
101 res = InterlockedDecrement(&This->ref);
102 trace("ACL(%p): release (%d)\n", This, res);
103 return res;
104 }
105
106 static HRESULT STDMETHODCALLTYPE TestACL_QueryInterface(IEnumString *iface, REFIID iid, LPVOID *ppvOut)
107 {
108 TestACL *This = (TestACL *)iface;
109 *ppvOut = NULL;
110 if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IEnumString))
111 {
112 *ppvOut = iface;
113 }
114 else if (IsEqualGUID(iid, &IID_IACList))
115 {
116 *ppvOut = &This->aclVtbl;
117 }
118
119 if (*ppvOut)
120 {
121 iface->lpVtbl->AddRef(iface);
122 return S_OK;
123 }
124
125 #if 0 /* IID_IEnumACString not defined yet in wine */
126 if (!IsEqualGUID(iid, &IID_IEnumACString))
127 trace("unknown interface queried\n");
128 #endif
129 return E_NOINTERFACE;
130 }
131
132 static HRESULT STDMETHODCALLTYPE TestACL_Next(IEnumString *iface, ULONG celt, LPOLESTR *rgelt, ULONG *pceltFetched)
133 {
134 TestACL *This = (TestACL *)iface;
135 ULONG i;
136
137 trace("ACL(%p): read %d item(s)\n", This, celt);
138 for (i = 0; i < celt; i++)
139 {
140 if (This->pos >= This->limit)
141 break;
142 rgelt[i] = strdup_AtoW(This->data[This->pos]);
143 This->pos++;
144 }
145
146 if (pceltFetched)
147 *pceltFetched = i;
148 if (i == celt)
149 return S_OK;
150 return S_FALSE;
151 }
152
153 static HRESULT STDMETHODCALLTYPE TestACL_Skip(IEnumString *iface, ULONG celt)
154 {
155 ok(FALSE, "Unexpected call to TestACL_Skip\n");
156 return E_NOTIMPL;
157 }
158
159 static HRESULT STDMETHODCALLTYPE TestACL_Clone(IEnumString *iface, IEnumString **out)
160 {
161 ok(FALSE, "Unexpected call to TestACL_Clone\n");
162 return E_OUTOFMEMORY;
163 }
164
165 static HRESULT STDMETHODCALLTYPE TestACL_Reset(IEnumString *iface)
166 {
167 TestACL *This = (TestACL *)iface;
168 trace("ACL(%p): Reset\n", This);
169 This->pos = 0;
170 return S_OK;
171 }
172
173 static HRESULT STDMETHODCALLTYPE TestACL_Expand(IACList *iface, LPCOLESTR str)
174 {
175 TestACL *This = impl_from_IACList(iface);
176 trace("ACL(%p): Expand\n", impl_from_IACList(iface));
177 This->expcount++;
178 return This->expret;
179 }
180
181 IEnumStringVtbl TestACLVtbl =
182 {
183 TestACL_QueryInterface,
184 TestACL_AddRef,
185 TestACL_Release,
186
187 TestACL_Next,
188 TestACL_Skip,
189 TestACL_Reset,
190 TestACL_Clone
191 };
192
193 static ULONG STDMETHODCALLTYPE TestACL_ACList_AddRef(IACList *iface)
194 {
195 return TestACL_AddRef((IEnumString *)impl_from_IACList(iface));
196 }
197
198 static ULONG STDMETHODCALLTYPE TestACL_ACList_Release(IACList *iface)
199 {
200 return TestACL_Release((IEnumString *)impl_from_IACList(iface));
201 }
202
203 static HRESULT STDMETHODCALLTYPE TestACL_ACList_QueryInterface(IACList *iface, REFIID iid, LPVOID *ppvout)
204 {
205 return TestACL_QueryInterface((IEnumString *)impl_from_IACList(iface), iid, ppvout);
206 }
207
208 IACListVtbl TestACL_ACListVtbl =
209 {
210 TestACL_ACList_QueryInterface,
211 TestACL_ACList_AddRef,
212 TestACL_ACList_Release,
213
214 TestACL_Expand
215 };
216
217 #define expect_str(obj, str) \
218 { \
219 ole_ok(obj->lpVtbl->Next(obj, 1, &wstr, &i)); \
220 ok(i == 1, "Expected i == 1, got %d\n", i); \
221 ok(str[0] == wstr[0], "String mismatch\n"); \
222 CoTaskMemFree(wstr); \
223 }
224
225 #define expect_end(obj) \
226 ok(obj->lpVtbl->Next(obj, 1, &wstr, &i) == S_FALSE, "Unexpected return from Next\n");
227
228 static void test_ACLMulti(void)
229 {
230 const char *strings1[] = {"a", "c", "e"};
231 const char *strings2[] = {"a", "b", "d"};
232 WCHAR exp[] = {'A','B','C',0};
233 IEnumString *obj;
234 TestACL *acl1, *acl2;
235 IACList *acl;
236 IObjMgr *mgr;
237 LPWSTR wstr;
238 LPWSTR wstrtab[15];
239 LPVOID tmp;
240 UINT i;
241
242 stop_on_error(CoCreateInstance(&CLSID_ACLMulti, NULL, CLSCTX_INPROC, &IID_IEnumString, (LPVOID *)&obj));
243 stop_on_error(obj->lpVtbl->QueryInterface(obj, &IID_IACList, (LPVOID *)&acl));
244 ok(obj->lpVtbl->QueryInterface(obj, &IID_IACList2, &tmp) == E_NOINTERFACE,
245 "Unexpected interface IACList2 in ACLMulti\n");
246 stop_on_error(obj->lpVtbl->QueryInterface(obj, &IID_IObjMgr, (LPVOID *)&mgr));
247 #if 0 /* IID_IEnumACString not defined yet in wine */
248 ole_ok(obj->lpVtbl->QueryInterface(obj, &IID_IEnumACString, &unk));
249 if (unk != NULL)
250 unk->lpVtbl->Release(unk);
251 #endif
252
253 ok(obj->lpVtbl->Next(obj, 1, (LPOLESTR *)&tmp, &i) == S_FALSE, "Unexpected return from Next\n");
254 ok(i == 0, "Unexpected fetched value %d\n", i);
255 ok(obj->lpVtbl->Next(obj, 44, (LPOLESTR *)&tmp, &i) == S_FALSE, "Unexpected return from Next\n");
256 ok(obj->lpVtbl->Skip(obj, 1) == E_NOTIMPL, "Unexpected return from Skip\n");
257 ok(obj->lpVtbl->Clone(obj, (IEnumString **)&tmp) == E_OUTOFMEMORY, "Unexpected return from Clone\n");
258 ole_ok(acl->lpVtbl->Expand(acl, exp));
259
260 acl1 = TestACL_Constructor(3, strings1);
261 acl2 = TestACL_Constructor(3, strings2);
262 stop_on_error(mgr->lpVtbl->Append(mgr, (IUnknown *)acl1));
263 stop_on_error(mgr->lpVtbl->Append(mgr, (IUnknown *)acl2));
264 ok(mgr->lpVtbl->Append(mgr, NULL) == E_FAIL, "Unexpected return from Append\n");
265 expect_str(obj, "a");
266 expect_str(obj, "c");
267 expect_str(obj, "e");
268 expect_str(obj, "a");
269 expect_str(obj, "b");
270 expect_str(obj, "d");
271 expect_end(obj);
272
273 ole_ok(obj->lpVtbl->Reset(obj));
274 ok(acl1->pos == 0, "acl1 not reset\n");
275 ok(acl2->pos == 0, "acl2 not reset\n");
276
277 ole_ok(acl->lpVtbl->Expand(acl, exp));
278 ok(acl1->expcount == 1, "expcount - expected 1, got %d\n", acl1->expcount);
279 ok(acl2->expcount == 0 /* XP */ || acl2->expcount == 1 /* Vista */,
280 "expcount - expected 0 or 1, got %d\n", acl2->expcount);
281
282 ole_ok(obj->lpVtbl->Next(obj, 15, wstrtab, &i));
283 ok(i == 1, "Expected i == 1, got %d\n", i);
284 CoTaskMemFree(wstrtab[0]);
285 ole_ok(obj->lpVtbl->Next(obj, 15, wstrtab, &i));
286 CoTaskMemFree(wstrtab[0]);
287 ole_ok(obj->lpVtbl->Next(obj, 15, wstrtab, &i));
288 CoTaskMemFree(wstrtab[0]);
289 ole_ok(obj->lpVtbl->Next(obj, 15, wstrtab, &i));
290 CoTaskMemFree(wstrtab[0]);
291 ole_ok(acl->lpVtbl->Expand(acl, exp));
292 ok(acl1->expcount == 2, "expcount - expected 1, got %d\n", acl1->expcount);
293 ok(acl2->expcount == 0 /* XP */ || acl2->expcount == 2 /* Vista */,
294 "expcount - expected 0 or 2, got %d\n", acl2->expcount);
295 acl1->expret = S_FALSE;
296 ole_ok(acl->lpVtbl->Expand(acl, exp));
297 ok(acl1->expcount == 3, "expcount - expected 1, got %d\n", acl1->expcount);
298 ok(acl2->expcount == 1 /* XP */ || acl2->expcount == 3 /* Vista */,
299 "expcount - expected 0 or 3, got %d\n", acl2->expcount);
300 acl1->expret = E_NOTIMPL;
301 ole_ok(acl->lpVtbl->Expand(acl, exp));
302 ok(acl1->expcount == 4, "expcount - expected 1, got %d\n", acl1->expcount);
303 ok(acl2->expcount == 2 /* XP */ || acl2->expcount == 4 /* Vista */,
304 "expcount - expected 0 or 4, got %d\n", acl2->expcount);
305 acl2->expret = E_OUTOFMEMORY;
306 ok(acl->lpVtbl->Expand(acl, exp) == E_OUTOFMEMORY, "Unexpected Expand return\n");
307 acl2->expret = E_FAIL;
308 ok(acl->lpVtbl->Expand(acl, exp) == E_FAIL, "Unexpected Expand return\n");
309
310 stop_on_error(mgr->lpVtbl->Remove(mgr, (IUnknown *)acl1));
311 ok(acl1->ref == 1, "acl1 not released\n");
312 expect_end(obj);
313 obj->lpVtbl->Reset(obj);
314 expect_str(obj, "a");
315 expect_str(obj, "b");
316 expect_str(obj, "d");
317 expect_end(obj);
318
319 obj->lpVtbl->Release(obj);
320 acl->lpVtbl->Release(acl);
321 ok(mgr->lpVtbl->Release(mgr) == 0, "Unexpected references\n");
322 ok(acl1->ref == 1, "acl1 not released\n");
323 ok(acl2->ref == 1, "acl2 not released\n");
324
325 CoTaskMemFree(acl1);
326 CoTaskMemFree(acl2);
327 }
328
329 START_TEST(autocomplete)
330 {
331 CoInitialize(NULL);
332 test_ACLMulti();
333 CoUninitialize();
334 }