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