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