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