[BROWSEUI_WINETEST] Sync with Wine Staging 1.7.55. CORE-10536
[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 #define WIN32_NO_STATUS
21 #define _INC_WINDOWS
22 #define COM_NO_WINDOWS_H
23 #define COBJMACROS
24
25 #include <wine/test.h>
26
27 //#include <stdarg.h>
28 #include <winnls.h>
29 //#include <initguid.h>
30 #include <shlobj.h>
31 //#include <shldisp.h>
32 //#include <shlwapi.h>
33 //#include <shlguid.h>
34
35
36
37 #define ole_ok(exp) \
38 { \
39 HRESULT res = (exp); \
40 if (res != S_OK) \
41 ok(FALSE, #exp " failed: %x\n", res); \
42 }
43
44 static LPWSTR strdup_AtoW(LPCSTR str)
45 {
46 int size = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
47 LPWSTR wstr = CoTaskMemAlloc((size + 1)*sizeof(WCHAR));
48 MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, size+1);
49 return wstr;
50 }
51
52 typedef struct
53 {
54 IEnumString IEnumString_iface;
55 IACList IACList_iface;
56 LONG ref;
57 HRESULT expret;
58 INT expcount;
59 INT pos;
60 INT limit;
61 const char **data;
62 } TestACL;
63
64 extern IEnumStringVtbl TestACLVtbl;
65 extern IACListVtbl TestACL_ACListVtbl;
66
67 static inline TestACL *impl_from_IEnumString(IEnumString *iface)
68 {
69 return CONTAINING_RECORD(iface, TestACL, IEnumString_iface);
70 }
71
72 static TestACL *impl_from_IACList(IACList *iface)
73 {
74 return CONTAINING_RECORD(iface, TestACL, IACList_iface);
75 }
76
77 static TestACL *TestACL_Constructor(int limit, const char **strings)
78 {
79 TestACL *This = CoTaskMemAlloc(sizeof(TestACL));
80 ZeroMemory(This, sizeof(*This));
81 This->IEnumString_iface.lpVtbl = &TestACLVtbl;
82 This->IACList_iface.lpVtbl = &TestACL_ACListVtbl;
83 This->ref = 1;
84 This->expret = S_OK;
85 This->limit = limit;
86 This->data = strings;
87 return This;
88 }
89
90 static ULONG STDMETHODCALLTYPE TestACL_AddRef(IEnumString *iface)
91 {
92 TestACL *This = impl_from_IEnumString(iface);
93 trace("ACL(%p): addref (%d)\n", This, This->ref+1);
94 return InterlockedIncrement(&This->ref);
95 }
96
97 static ULONG STDMETHODCALLTYPE TestACL_Release(IEnumString *iface)
98 {
99 TestACL *This = impl_from_IEnumString(iface);
100 ULONG res;
101
102 res = InterlockedDecrement(&This->ref);
103 trace("ACL(%p): release (%d)\n", This, res);
104 return res;
105 }
106
107 static HRESULT STDMETHODCALLTYPE TestACL_QueryInterface(IEnumString *iface, REFIID iid, LPVOID *ppvOut)
108 {
109 TestACL *This = impl_from_IEnumString(iface);
110 *ppvOut = NULL;
111 if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IEnumString))
112 {
113 *ppvOut = iface;
114 }
115 else if (IsEqualGUID(iid, &IID_IACList))
116 {
117 *ppvOut = &This->IACList_iface;
118 }
119
120 if (*ppvOut)
121 {
122 IEnumString_AddRef(iface);
123 return S_OK;
124 }
125
126 if (!IsEqualGUID(iid, &IID_IEnumACString))
127 trace("unknown interface queried\n");
128 return E_NOINTERFACE;
129 }
130
131 static HRESULT STDMETHODCALLTYPE TestACL_Next(IEnumString *iface, ULONG celt, LPOLESTR *rgelt, ULONG *pceltFetched)
132 {
133 TestACL *This = impl_from_IEnumString(iface);
134 ULONG i;
135
136 trace("ACL(%p): read %d item(s)\n", This, celt);
137 for (i = 0; i < celt; i++)
138 {
139 if (This->pos >= This->limit)
140 break;
141 rgelt[i] = strdup_AtoW(This->data[This->pos]);
142 This->pos++;
143 }
144
145 if (pceltFetched)
146 *pceltFetched = i;
147 if (i == celt)
148 return S_OK;
149 return S_FALSE;
150 }
151
152 static HRESULT STDMETHODCALLTYPE TestACL_Skip(IEnumString *iface, ULONG celt)
153 {
154 ok(FALSE, "Unexpected call to TestACL_Skip\n");
155 return E_NOTIMPL;
156 }
157
158 static HRESULT STDMETHODCALLTYPE TestACL_Clone(IEnumString *iface, IEnumString **out)
159 {
160 ok(FALSE, "Unexpected call to TestACL_Clone\n");
161 return E_OUTOFMEMORY;
162 }
163
164 static HRESULT STDMETHODCALLTYPE TestACL_Reset(IEnumString *iface)
165 {
166 TestACL *This = impl_from_IEnumString(iface);
167 trace("ACL(%p): Reset\n", This);
168 This->pos = 0;
169 return S_OK;
170 }
171
172 static HRESULT STDMETHODCALLTYPE TestACL_Expand(IACList *iface, LPCOLESTR str)
173 {
174 TestACL *This = impl_from_IACList(iface);
175 trace("ACL(%p): Expand\n", This);
176 This->expcount++;
177 return This->expret;
178 }
179
180 IEnumStringVtbl TestACLVtbl =
181 {
182 TestACL_QueryInterface,
183 TestACL_AddRef,
184 TestACL_Release,
185
186 TestACL_Next,
187 TestACL_Skip,
188 TestACL_Reset,
189 TestACL_Clone
190 };
191
192 static ULONG STDMETHODCALLTYPE TestACL_ACList_AddRef(IACList *iface)
193 {
194 TestACL *This = impl_from_IACList(iface);
195 return TestACL_AddRef(&This->IEnumString_iface);
196 }
197
198 static ULONG STDMETHODCALLTYPE TestACL_ACList_Release(IACList *iface)
199 {
200 TestACL *This = impl_from_IACList(iface);
201 return TestACL_Release(&This->IEnumString_iface);
202 }
203
204 static HRESULT STDMETHODCALLTYPE TestACL_ACList_QueryInterface(IACList *iface, REFIID iid, LPVOID *ppvout)
205 {
206 TestACL *This = impl_from_IACList(iface);
207 return TestACL_QueryInterface(&This->IEnumString_iface, iid, ppvout);
208 }
209
210 IACListVtbl TestACL_ACListVtbl =
211 {
212 TestACL_ACList_QueryInterface,
213 TestACL_ACList_AddRef,
214 TestACL_ACList_Release,
215
216 TestACL_Expand
217 };
218
219 #define expect_str(obj, str) \
220 { \
221 ole_ok(IEnumString_Next(obj, 1, &wstr, &i)); \
222 ok(i == 1, "Expected i == 1, got %d\n", i); \
223 ok(str[0] == wstr[0], "String mismatch\n"); \
224 CoTaskMemFree(wstr); \
225 }
226
227 #define expect_end(obj) \
228 ok(IEnumString_Next(obj, 1, &wstr, &i) == S_FALSE, "Unexpected return from Next\n");
229
230 static void test_ACLMulti(void)
231 {
232 const char *strings1[] = {"a", "c", "e"};
233 const char *strings2[] = {"a", "b", "d"};
234 WCHAR exp[] = {'A','B','C',0};
235 IEnumString *obj;
236 IEnumACString *unk;
237 HRESULT hr;
238 TestACL *acl1, *acl2;
239 IACList *acl;
240 IObjMgr *mgr;
241 LPWSTR wstr;
242 LPWSTR wstrtab[15];
243 LPVOID tmp;
244 ULONG ref;
245 UINT i;
246
247 hr = CoCreateInstance(&CLSID_ACLMulti, NULL, CLSCTX_INPROC, &IID_IEnumString, (void**)&obj);
248 ok(hr == S_OK, "failed to create ACLMulti instance, 0x%08x\n", hr);
249 if (hr != S_OK) return;
250
251 hr = IEnumString_QueryInterface(obj, &IID_IACList, (void**)&acl);
252 ok(hr == S_OK, "got 0x%08x\n", hr);
253 hr = IEnumString_QueryInterface(obj, &IID_IACList2, &tmp);
254 ok(hr == E_NOINTERFACE, "got 0x%08x\n", hr);
255 hr = IEnumString_QueryInterface(obj, &IID_IObjMgr, (void**)&mgr);
256 ok(hr == S_OK, "got 0x%08x\n", hr);
257
258 hr = IEnumString_QueryInterface(obj, &IID_IEnumACString, (LPVOID*)&unk);
259 if (hr == E_NOINTERFACE)
260 todo_wine win_skip("IEnumACString is not supported, skipping tests\n");
261 else
262 {
263 ok(hr == S_OK, "QueryInterface(IID_IEnumACString) failed: %x\n", hr);
264 if (unk != NULL)
265 IEnumACString_Release(unk);
266 }
267
268 i = -1;
269 hr = IEnumString_Next(obj, 1, (LPOLESTR *)&tmp, &i);
270 ok(hr == S_FALSE, "got 0x%08x\n", hr);
271 ok(i == 0, "Unexpected fetched value %d\n", i);
272 hr = IEnumString_Next(obj, 44, (LPOLESTR *)&tmp, &i);
273 ok(hr == S_FALSE, "got 0x%08x\n", hr);
274 hr = IEnumString_Skip(obj, 1);
275 ok(hr == E_NOTIMPL, "got 0x%08x\n", hr);
276 hr = IEnumString_Clone(obj, (IEnumString **)&tmp);
277 ok(hr == E_OUTOFMEMORY, "got 0x%08x\n", hr);
278 hr = IACList_Expand(acl, exp);
279 ok(hr == S_OK, "got 0x%08x\n", hr);
280
281 acl1 = TestACL_Constructor(3, strings1);
282 acl2 = TestACL_Constructor(3, strings2);
283 hr = IObjMgr_Append(mgr, (IUnknown *)&acl1->IACList_iface);
284 ok(hr == S_OK, "got 0x%08x\n", hr);
285 hr = IObjMgr_Append(mgr, (IUnknown *)&acl2->IACList_iface);
286 ok(hr == S_OK, "got 0x%08x\n", hr);
287 hr = IObjMgr_Append(mgr, NULL);
288 ok(hr == E_FAIL, "got 0x%08x\n", hr);
289 expect_str(obj, "a");
290 expect_str(obj, "c");
291 expect_str(obj, "e");
292 expect_str(obj, "a");
293 expect_str(obj, "b");
294 expect_str(obj, "d");
295 expect_end(obj);
296
297 hr = IEnumString_Reset(obj);
298 ok(hr == S_OK, "got 0x%08x\n", hr);
299 ok(acl1->pos == 0, "acl1 not reset\n");
300 ok(acl2->pos == 0, "acl2 not reset\n");
301
302 hr = IACList_Expand(acl, exp);
303 ok(hr == S_OK, "got 0x%08x\n", hr);
304 ok(acl1->expcount == 1, "expcount - expected 1, got %d\n", acl1->expcount);
305 ok(acl2->expcount == 0 /* XP */ || acl2->expcount == 1 /* Vista */,
306 "expcount - expected 0 or 1, got %d\n", acl2->expcount);
307
308 hr = IEnumString_Next(obj, 15, wstrtab, &i);
309 ok(hr == S_OK, "got 0x%08x\n", hr);
310 ok(i == 1, "Expected i == 1, got %d\n", i);
311 CoTaskMemFree(wstrtab[0]);
312
313 hr = IEnumString_Next(obj, 15, wstrtab, &i);
314 ok(hr == S_OK, "got 0x%08x\n", hr);
315 CoTaskMemFree(wstrtab[0]);
316
317 hr = IEnumString_Next(obj, 15, wstrtab, &i);
318 ok(hr == S_OK, "got 0x%08x\n", hr);
319 CoTaskMemFree(wstrtab[0]);
320
321 hr = IEnumString_Next(obj, 15, wstrtab, &i);
322 ok(hr == S_OK, "got 0x%08x\n", hr);
323 CoTaskMemFree(wstrtab[0]);
324
325 hr = IACList_Expand(acl, exp);
326 ok(hr == S_OK, "got 0x%08x\n", hr);
327 ok(acl1->expcount == 2, "expcount - expected 1, got %d\n", acl1->expcount);
328 ok(acl2->expcount == 0 /* XP */ || acl2->expcount == 2 /* Vista */,
329 "expcount - expected 0 or 2, got %d\n", acl2->expcount);
330 acl1->expret = S_FALSE;
331 hr = IACList_Expand(acl, exp);
332 ok(hr == S_OK, "got 0x%08x\n", hr);
333 ok(acl1->expcount == 3, "expcount - expected 1, got %d\n", acl1->expcount);
334 ok(acl2->expcount == 1 /* XP */ || acl2->expcount == 3 /* Vista */,
335 "expcount - expected 0 or 3, got %d\n", acl2->expcount);
336 acl1->expret = E_NOTIMPL;
337 hr = IACList_Expand(acl, exp);
338 ok(hr == S_OK, "got 0x%08x\n", hr);
339 ok(acl1->expcount == 4, "expcount - expected 1, got %d\n", acl1->expcount);
340 ok(acl2->expcount == 2 /* XP */ || acl2->expcount == 4 /* Vista */,
341 "expcount - expected 0 or 4, got %d\n", acl2->expcount);
342 acl2->expret = E_OUTOFMEMORY;
343 hr = IACList_Expand(acl, exp);
344 ok(hr == E_OUTOFMEMORY, "got 0x%08x\n", hr);
345 acl2->expret = E_FAIL;
346 hr = IACList_Expand(acl, exp);
347 ok(hr == E_FAIL, "got 0x%08x\n", hr);
348
349 hr = IObjMgr_Remove(mgr, (IUnknown *)&acl1->IACList_iface);
350 ok(hr == S_OK, "got 0x%08x\n", hr);
351
352 ok(acl1->ref == 1, "acl1 not released\n");
353 expect_end(obj);
354 IEnumString_Reset(obj);
355 expect_str(obj, "a");
356 expect_str(obj, "b");
357 expect_str(obj, "d");
358 expect_end(obj);
359
360 IEnumString_Release(obj);
361 IACList_Release(acl);
362 ref = IObjMgr_Release(mgr);
363 ok(ref == 0, "Unexpected references\n");
364 ok(acl1->ref == 1, "acl1 not released\n");
365 ok(acl2->ref == 1, "acl2 not released\n");
366
367 CoTaskMemFree(acl1);
368 CoTaskMemFree(acl2);
369 }
370
371 static void test_ACListISF(void)
372 {
373 IEnumString *enumstring;
374 IACList *list, *list2;
375 HRESULT hr;
376
377 hr = CoCreateInstance(&CLSID_ACListISF, NULL, CLSCTX_INPROC, &IID_IACList, (void**)&list);
378 ok(hr == S_OK, "failed to create ACListISF instance, 0x%08x\n", hr);
379
380 hr = IACList_QueryInterface(list, &IID_IEnumString, (void**)&enumstring);
381 ok(hr == S_OK, "got 0x%08x\n", hr);
382
383 hr = IEnumString_QueryInterface(enumstring, &IID_IACList, (void**)&list2);
384 ok(hr == S_OK, "got 0x%08x\n", hr);
385 ok(list == list2, "got %p, %p\n", list, list2);
386 IACList_Release(list2);
387
388 IEnumString_Release(enumstring);
389 IACList_Release(list);
390 }
391
392 START_TEST(autocomplete)
393 {
394 CoInitialize(NULL);
395
396 test_ACLMulti();
397
398 if (!winetest_interactive)
399 skip("ROSTESTS-210: Skipping test_ACListISF().\n");
400 else
401 test_ACListISF();
402
403 CoUninitialize();
404 }