wined3d: texturefactor-> fragment states.
[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 <initguid.h>
24 #include <windows.h>
25 #include <shlobj.h>
26 #include <shlwapi.h>
27
28 #include "wine/test.h"
29
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     } \
38 }
39
40 #define ole_ok(exp) \
41 { \
42     HRESULT res = (exp); \
43     if (res != S_OK) \
44         ok(FALSE, #exp " failed: %x\n", res); \
45 }
46
47 LPWSTR strdup_AtoW(LPCSTR str)
48 {
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;
53 }
54
55 typedef struct
56 {
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;
66
67 extern IEnumStringVtbl TestACLVtbl;
68 extern IACListVtbl TestACL_ACListVtbl;
69
70 static TestACL *impl_from_IACList(IACList *iface)
71 {
72     return (TestACL *)((char *)iface - FIELD_OFFSET(TestACL, aclVtbl));
73 }
74
75 static TestACL *TestACL_Constructor(int limit, const char **strings)
76 {
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;
86 }
87
88 ULONG STDMETHODCALLTYPE TestACL_AddRef(IEnumString *iface)
89 {
90     TestACL *This = (TestACL *)iface;
91     trace("ACL(%p): addref (%d)\n", This, This->ref+1);
92     return InterlockedIncrement(&This->ref);
93 }
94
95 ULONG STDMETHODCALLTYPE TestACL_Release(IEnumString *iface)
96 {
97     TestACL *This = (TestACL *)iface;
98     ULONG res;
99
100     res = InterlockedDecrement(&This->ref);
101     trace("ACL(%p): release (%d)\n", This, res);
102     return res;
103 }
104
105 HRESULT STDMETHODCALLTYPE TestACL_QueryInterface(IEnumString *iface, REFIID iid, LPVOID *ppvOut)
106 {
107     TestACL *This = (TestACL *)iface;
108     *ppvOut = NULL;
109     if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IEnumString))
110     {
111         *ppvOut = iface;
112     }
113     else if (IsEqualGUID(iid, &IID_IACList))
114     {
115         *ppvOut = &This->aclVtbl;
116     }
117
118     if (*ppvOut)
119     {
120         iface->lpVtbl->AddRef(iface);
121         return S_OK;
122     }
123
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;
129 }
130
131 HRESULT STDMETHODCALLTYPE TestACL_Next(IEnumString *iface, ULONG celt, LPOLESTR *rgelt, ULONG *pceltFetched)
132 {
133     TestACL *This = (TestACL *)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 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 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 HRESULT STDMETHODCALLTYPE TestACL_Reset(IEnumString *iface)
165 {
166     TestACL *This = (TestACL *)iface;
167     trace("ACL(%p): Reset\n", This);
168     This->pos = 0;
169     return S_OK;
170 }
171
172 HRESULT STDMETHODCALLTYPE TestACL_Expand(IACList *iface, LPCOLESTR str)
173 {
174     TestACL *This = impl_from_IACList(iface);
175     trace("ACL(%p): Expand\n", impl_from_IACList(iface));
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 ULONG STDMETHODCALLTYPE TestACL_ACList_AddRef(IACList *iface)
193 {
194     return TestACL_AddRef((IEnumString *)impl_from_IACList(iface));
195 }
196
197 ULONG STDMETHODCALLTYPE TestACL_ACList_Release(IACList *iface)
198 {
199     return TestACL_Release((IEnumString *)impl_from_IACList(iface));
200 }
201
202 HRESULT STDMETHODCALLTYPE TestACL_ACList_QueryInterface(IACList *iface, REFIID iid, LPVOID *ppvout)
203 {
204     return TestACL_QueryInterface((IEnumString *)impl_from_IACList(iface), iid, ppvout);
205 }
206
207 IACListVtbl TestACL_ACListVtbl =
208 {
209     TestACL_ACList_QueryInterface,
210     TestACL_ACList_AddRef,
211     TestACL_ACList_Release,
212
213     TestACL_Expand
214 };
215
216 #define expect_str(obj, str)  \
217 { \
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"); \
221 }
222
223 #define expect_end(obj) \
224     ok(obj->lpVtbl->Next(obj, 1, &wstr, &i) == S_FALSE, "Unexpected return from Next\n");
225
226 static void test_ACLMulti(void)
227 {
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;
239
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
250
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));
257
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);
270
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");
274
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);
279
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");
303
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);
312
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");
318 }
319
320 START_TEST(autocomplete)
321 {
322     CoInitialize(NULL);
323     test_ACLMulti();
324     CoUninitialize();
325 }