autocomplete.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Unit tests for autocomplete
 *
 * Copyright 2007 Mikolaj Zalewski
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>

22
#include <initguid.h>
23 24
#include <windows.h>
#include <shlobj.h>
25
#include <shldisp.h>
26
#include <shlwapi.h>
27
#include <shlguid.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#include "wine/test.h"

#define stop_on_error(exp) \
{ \
    HRESULT res = (exp); \
    if (FAILED(res)) \
    { \
        ok(FALSE, #exp " failed: %x\n", res); \
        return; \
    } \
}

#define ole_ok(exp) \
{ \
    HRESULT res = (exp); \
    if (res != S_OK) \
        ok(FALSE, #exp " failed: %x\n", res); \
}

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 53 54 55 56 57
    MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, size+1);
    return wstr;
}

typedef struct
{
58 59
    IEnumString IEnumString_iface;
    IACList IACList_iface;
60 61 62 63 64 65 66 67 68 69 70
    LONG ref;
    HRESULT expret;
    INT expcount;
    INT pos;
    INT limit;
    const char **data;
} TestACL;

extern IEnumStringVtbl TestACLVtbl;
extern IACListVtbl TestACL_ACListVtbl;

71 72 73 74 75
static inline TestACL *impl_from_IEnumString(IEnumString *iface)
{
    return CONTAINING_RECORD(iface, TestACL, IEnumString_iface);
}

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 84
{
    TestACL *This = CoTaskMemAlloc(sizeof(TestACL));
    ZeroMemory(This, sizeof(*This));
85 86
    This->IEnumString_iface.lpVtbl = &TestACLVtbl;
    This->IACList_iface.lpVtbl = &TestACL_ACListVtbl;
87 88 89 90 91 92 93
    This->ref = 1;
    This->expret = S_OK;
    This->limit = limit;
    This->data = strings;
    return This;
}

94
static ULONG STDMETHODCALLTYPE TestACL_AddRef(IEnumString *iface)
95
{
96
    TestACL *This = impl_from_IEnumString(iface);
97 98 99 100
    trace("ACL(%p): addref (%d)\n", This, This->ref+1);
    return InterlockedIncrement(&This->ref);
}

101
static ULONG STDMETHODCALLTYPE TestACL_Release(IEnumString *iface)
102
{
103
    TestACL *This = impl_from_IEnumString(iface);
104 105 106 107 108 109 110
    ULONG res;

    res = InterlockedDecrement(&This->ref);
    trace("ACL(%p): release (%d)\n", This, res);
    return res;
}

111
static HRESULT STDMETHODCALLTYPE TestACL_QueryInterface(IEnumString *iface, REFIID iid, LPVOID *ppvOut)
112
{
113
    TestACL *This = impl_from_IEnumString(iface);
114 115 116 117 118 119 120
    *ppvOut = NULL;
    if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IEnumString))
    {
        *ppvOut = iface;
    }
    else if (IsEqualGUID(iid, &IID_IACList))
    {
121
        *ppvOut = &This->IACList_iface;
122 123 124 125 126 127 128 129 130 131 132 133 134
    }

    if (*ppvOut)
    {
        iface->lpVtbl->AddRef(iface);
        return S_OK;
    }

    if (!IsEqualGUID(iid, &IID_IEnumACString))
        trace("unknown interface queried\n");
    return E_NOINTERFACE;
}

135
static HRESULT STDMETHODCALLTYPE TestACL_Next(IEnumString *iface, ULONG celt, LPOLESTR *rgelt, ULONG *pceltFetched)
136
{
137
    TestACL *This = impl_from_IEnumString(iface);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    ULONG i;

    trace("ACL(%p): read %d item(s)\n", This, celt);
    for (i = 0; i < celt; i++)
    {
        if (This->pos >= This->limit)
            break;
        rgelt[i] = strdup_AtoW(This->data[This->pos]);
        This->pos++;
    }

    if (pceltFetched)
        *pceltFetched = i;
    if (i == celt)
        return S_OK;
    return S_FALSE;
}

156
static HRESULT STDMETHODCALLTYPE TestACL_Skip(IEnumString *iface, ULONG celt)
157 158 159 160 161
{
    ok(FALSE, "Unexpected call to TestACL_Skip\n");
    return E_NOTIMPL;
}

162
static HRESULT STDMETHODCALLTYPE TestACL_Clone(IEnumString *iface, IEnumString **out)
163 164 165 166 167
{
    ok(FALSE, "Unexpected call to TestACL_Clone\n");
    return E_OUTOFMEMORY;
}

168
static HRESULT STDMETHODCALLTYPE TestACL_Reset(IEnumString *iface)
169
{
170
    TestACL *This = impl_from_IEnumString(iface);
171 172 173 174 175
    trace("ACL(%p): Reset\n", This);
    This->pos = 0;
    return S_OK;
}

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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    This->expcount++;
    return This->expret;
}

IEnumStringVtbl TestACLVtbl =
{
    TestACL_QueryInterface,
    TestACL_AddRef,
    TestACL_Release,

    TestACL_Next,
    TestACL_Skip,
    TestACL_Reset,
    TestACL_Clone
};

196
static ULONG STDMETHODCALLTYPE TestACL_ACList_AddRef(IACList *iface)
197
{
198 199
    TestACL *This = impl_from_IACList(iface);
    return TestACL_AddRef(&This->IEnumString_iface);
200 201
}

202
static ULONG STDMETHODCALLTYPE TestACL_ACList_Release(IACList *iface)
203
{
204 205
    TestACL *This = impl_from_IACList(iface);
    return TestACL_Release(&This->IEnumString_iface);
206 207
}

208
static HRESULT STDMETHODCALLTYPE TestACL_ACList_QueryInterface(IACList *iface, REFIID iid, LPVOID *ppvout)
209
{
210 211
    TestACL *This = impl_from_IACList(iface);
    return TestACL_QueryInterface(&This->IEnumString_iface, iid, ppvout);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
}

IACListVtbl TestACL_ACListVtbl =
{
    TestACL_ACList_QueryInterface,
    TestACL_ACList_AddRef,
    TestACL_ACList_Release,

    TestACL_Expand
};

#define expect_str(obj, str)  \
{ \
    ole_ok(obj->lpVtbl->Next(obj, 1, &wstr, &i)); \
    ok(i == 1, "Expected i == 1, got %d\n", i); \
    ok(str[0] == wstr[0], "String mismatch\n"); \
228
    CoTaskMemFree(wstr); \
229 230 231 232 233
}

#define expect_end(obj) \
    ok(obj->lpVtbl->Next(obj, 1, &wstr, &i) == S_FALSE, "Unexpected return from Next\n");

234
static void test_ACLMulti(void)
235 236 237 238 239
{
    const char *strings1[] = {"a", "c", "e"};
    const char *strings2[] = {"a", "b", "d"};
    WCHAR exp[] = {'A','B','C',0};
    IEnumString *obj;
240
    IEnumACString *unk;
241
    HRESULT hr;
242 243 244 245 246 247 248 249 250 251 252 253 254
    TestACL *acl1, *acl2;
    IACList *acl;
    IObjMgr *mgr;
    LPWSTR wstr;
    LPWSTR wstrtab[15];
    LPVOID tmp;
    UINT i;

    stop_on_error(CoCreateInstance(&CLSID_ACLMulti, NULL, CLSCTX_INPROC, &IID_IEnumString, (LPVOID *)&obj));
    stop_on_error(obj->lpVtbl->QueryInterface(obj, &IID_IACList, (LPVOID *)&acl));
    ok(obj->lpVtbl->QueryInterface(obj, &IID_IACList2, &tmp) == E_NOINTERFACE,
        "Unexpected interface IACList2 in ACLMulti\n");
    stop_on_error(obj->lpVtbl->QueryInterface(obj, &IID_IObjMgr, (LPVOID *)&mgr));
255

256 257 258 259 260 261 262 263 264
    hr = obj->lpVtbl->QueryInterface(obj, &IID_IEnumACString, (LPVOID*)&unk);
    if (hr == E_NOINTERFACE)
        todo_wine win_skip("IEnumACString is not supported, skipping tests\n");
    else
    {
        ok(hr == S_OK, "QueryInterface(IID_IEnumACString) failed: %x\n", hr);
        if (unk != NULL)
            unk->lpVtbl->Release(unk);
    }
265 266 267 268 269 270 271 272 273 274

    ok(obj->lpVtbl->Next(obj, 1, (LPOLESTR *)&tmp, &i) == S_FALSE, "Unexpected return from Next\n");
    ok(i == 0, "Unexpected fetched value %d\n", i);
    ok(obj->lpVtbl->Next(obj, 44, (LPOLESTR *)&tmp, &i) == S_FALSE, "Unexpected return from Next\n");
    ok(obj->lpVtbl->Skip(obj, 1) == E_NOTIMPL, "Unexpected return from Skip\n");
    ok(obj->lpVtbl->Clone(obj, (IEnumString **)&tmp) == E_OUTOFMEMORY, "Unexpected return from Clone\n");
    ole_ok(acl->lpVtbl->Expand(acl, exp));

    acl1 = TestACL_Constructor(3, strings1);
    acl2 = TestACL_Constructor(3, strings2);
275 276
    stop_on_error(mgr->lpVtbl->Append(mgr, (IUnknown *)&acl1->IACList_iface));
    stop_on_error(mgr->lpVtbl->Append(mgr, (IUnknown *)&acl2->IACList_iface));
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    ok(mgr->lpVtbl->Append(mgr, NULL) == E_FAIL, "Unexpected return from Append\n");
    expect_str(obj, "a");
    expect_str(obj, "c");
    expect_str(obj, "e");
    expect_str(obj, "a");
    expect_str(obj, "b");
    expect_str(obj, "d");
    expect_end(obj);

    ole_ok(obj->lpVtbl->Reset(obj));
    ok(acl1->pos == 0, "acl1 not reset\n");
    ok(acl2->pos == 0, "acl2 not reset\n");

    ole_ok(acl->lpVtbl->Expand(acl, exp));
    ok(acl1->expcount == 1, "expcount - expected 1, got %d\n", acl1->expcount);
292 293
    ok(acl2->expcount == 0 /* XP */ || acl2->expcount == 1 /* Vista */,
        "expcount - expected 0 or 1, got %d\n", acl2->expcount);
294 295 296

    ole_ok(obj->lpVtbl->Next(obj, 15, wstrtab, &i));
    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 307
    ok(acl2->expcount == 0 /* XP */ || acl2->expcount == 2 /* Vista */,
        "expcount - expected 0 or 2, got %d\n", acl2->expcount);
308 309
    acl1->expret = S_FALSE;
    ole_ok(acl->lpVtbl->Expand(acl, exp));
310
    ok(acl1->expcount == 3, "expcount - expected 1, got %d\n", acl1->expcount);
311 312
    ok(acl2->expcount == 1 /* XP */ || acl2->expcount == 3 /* Vista */,
        "expcount - expected 0 or 3, got %d\n", acl2->expcount);
313 314
    acl1->expret = E_NOTIMPL;
    ole_ok(acl->lpVtbl->Expand(acl, exp));
315
    ok(acl1->expcount == 4, "expcount - expected 1, got %d\n", acl1->expcount);
316 317
    ok(acl2->expcount == 2 /* XP */ || acl2->expcount == 4 /* Vista */,
        "expcount - expected 0 or 4, got %d\n", acl2->expcount);
318 319 320 321 322
    acl2->expret = E_OUTOFMEMORY;
    ok(acl->lpVtbl->Expand(acl, exp) == E_OUTOFMEMORY, "Unexpected Expand return\n");
    acl2->expret = E_FAIL;
    ok(acl->lpVtbl->Expand(acl, exp) == E_FAIL, "Unexpected Expand return\n");

323
    stop_on_error(mgr->lpVtbl->Remove(mgr, (IUnknown *)&acl1->IACList_iface));
324 325 326 327 328 329 330 331 332 333 334 335 336
    ok(acl1->ref == 1, "acl1 not released\n");
    expect_end(obj);
    obj->lpVtbl->Reset(obj);
    expect_str(obj, "a");
    expect_str(obj, "b");
    expect_str(obj, "d");
    expect_end(obj);

    obj->lpVtbl->Release(obj);
    acl->lpVtbl->Release(acl);
    ok(mgr->lpVtbl->Release(mgr) == 0, "Unexpected references\n");
    ok(acl1->ref == 1, "acl1 not released\n");
    ok(acl2->ref == 1, "acl2 not released\n");
337 338 339

    CoTaskMemFree(acl1);
    CoTaskMemFree(acl2);
340 341 342 343 344 345 346 347
}

START_TEST(autocomplete)
{
    CoInitialize(NULL);
    test_ACLMulti();
    CoUninitialize();
}