htmllocation.c 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright 2009 Andrew Eikum for CodeWeavers
 *
 * 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
 */

#define COBJMACROS
#define CONST_VTABLE

#include <wine/test.h>

#include "mshtml.h"
Jacek Caban's avatar
Jacek Caban committed
25
#include "wininet.h"
26 27 28

struct location_test {
    const char *name;
Jacek Caban's avatar
Jacek Caban committed
29
    const char *url;
30 31 32 33 34 35 36 37 38 39 40

    const char *href;
    const char *protocol;
    const char *host;
    const char *hostname;
    const char *port;
    const char *pathname;
    const char *search;
    const char *hash;
};

Jacek Caban's avatar
Jacek Caban committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
static const struct location_test location_tests[] = {
    {
        "HTTP",
        "http://www.winehq.org?search#hash",
        "http://www.winehq.org/?search#hash",
        "http:",
        "www.winehq.org:80",
        "www.winehq.org",
        "80",
        "",
        "?search",
        "#hash"
    },
    {
        "HTTP with file",
        "http://www.winehq.org/file?search#hash",
        "http://www.winehq.org/file?search#hash",
        "http:",
        "www.winehq.org:80",
        "www.winehq.org",
        "80",
        "file",
        "?search",
        "#hash"
    },
    {
        "FTP",
        "ftp://ftp.winehq.org/",
        "ftp://ftp.winehq.org/",
        "ftp:",
        "ftp.winehq.org:21",
        "ftp.winehq.org",
        "21",
        "",
        NULL,
        NULL
    },
    {
        "FTP with file",
        "ftp://ftp.winehq.org/file",
        "ftp://ftp.winehq.org/file",
        "ftp:",
        "ftp.winehq.org:21",
        "ftp.winehq.org",
        "21",
        "file",
        NULL,
        NULL
    },
    {
        "FILE",
        "file://C:\\windows\\win.ini",
        "file:///C:/windows/win.ini",
        "file:",
        NULL,
        NULL,
        "",
        "C:\\windows\\win.ini",
        NULL,
        NULL
    }
};
103 104 105 106 107

static int str_eq_wa(LPCWSTR strw, const char *stra)
{
    CHAR buf[512];

108 109
    if(!strw || !stra)
        return (void*)strw == (void*)stra;
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
    return !lstrcmpA(stra, buf);
}

static void test_href(IHTMLLocation *loc, const struct location_test *test)
{
    HRESULT hres;
    BSTR str;

    hres = IHTMLLocation_get_href(loc, NULL);
    ok(hres == E_POINTER,
            "%s: get_href should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
            test->name, E_POINTER, hres);

    hres = IHTMLLocation_get_href(loc, &str);
    ok(hres == S_OK, "%s: get_href failed: 0x%08x\n", test->name, hres);
127 128 129 130
    if(hres == S_OK)
        ok(str_eq_wa(str, test->href),
                "%s: expected retrieved href to be L\"%s\", was: %s\n",
                test->name, test->href, wine_dbgstr_w(str));
131
    SysFreeString(str);
132 133 134 135 136 137

    hres = IHTMLLocation_toString(loc, &str);
    ok(hres == S_OK, "%s: toString failed: 0x%08x\n", test->name, hres);
    ok(str_eq_wa(str, test->href), "%s: toString returned %s, expected %s\n",
       test->name, wine_dbgstr_w(str), test->href);
    SysFreeString(str);
138 139 140 141 142 143 144 145 146 147 148 149 150
}

static void test_protocol(IHTMLLocation *loc, const struct location_test *test)
{
    HRESULT hres;
    BSTR str;

    hres = IHTMLLocation_get_protocol(loc, NULL);
    ok(hres == E_POINTER,
            "%s: get_protocol should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
            test->name, E_POINTER, hres);

    hres = IHTMLLocation_get_protocol(loc, &str);
151
    ok(hres == S_OK, "%s: get_protocol failed: 0x%08x\n", test->name, hres);
152 153 154 155
    if(hres == S_OK)
        ok(str_eq_wa(str, test->protocol),
                "%s: expected retrieved protocol to be L\"%s\", was: %s\n",
                test->name, test->protocol, wine_dbgstr_w(str));
156
    SysFreeString(str);
157 158 159 160 161 162 163 164 165 166 167 168 169
}

static void test_host(IHTMLLocation *loc, const struct location_test *test)
{
    HRESULT hres;
    BSTR str;

    hres = IHTMLLocation_get_host(loc, NULL);
    ok(hres == E_POINTER,
            "%s: get_host should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
            test->name, E_POINTER, hres);

    hres = IHTMLLocation_get_host(loc, &str);
170
    ok(hres == S_OK, "%s: get_host failed: 0x%08x\n", test->name, hres);
171 172
    if(hres == S_OK){
        int len = test->host ? strlen(test->host) : 0;
173 174 175
        ok(str_eq_wa(str, test->host),
                "%s: expected retrieved host to be L\"%s\", was: %s\n",
                test->name, test->host, wine_dbgstr_w(str));
176 177 178 179
        ok(SysStringLen(str) == len, "%s: unexpected string length %u, expected %u\n",
                test->name, SysStringLen(str), len);
    }

180
    SysFreeString(str);
181 182
}

183
static void test_hostname(IHTMLLocation *loc, IHTMLDocument2 *doc, const struct location_test *test)
184 185 186 187 188 189 190 191 192 193
{
    HRESULT hres;
    BSTR str;

    hres = IHTMLLocation_get_hostname(loc, NULL);
    ok(hres == E_POINTER,
            "%s: get_hostname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
            test->name, E_POINTER, hres);

    hres = IHTMLLocation_get_hostname(loc, &str);
194
    ok(hres == S_OK, "%s: get_hostname failed: 0x%08x\n", test->name, hres);
195 196 197 198
    if(hres == S_OK)
        ok(str_eq_wa(str, test->hostname),
                "%s: expected retrieved hostname to be L\"%s\", was: %s\n",
                test->name, test->hostname, wine_dbgstr_w(str));
199
    SysFreeString(str);
200 201 202 203 204 205 206 207

    hres = IHTMLDocument2_get_domain(doc, &str);
    ok(hres == S_OK, "%s: get_domain failed: 0x%08x\n", test->name, hres);
    if(hres == S_OK)
        ok(str_eq_wa(str, test->hostname ? test->hostname : ""),
                "%s: expected retrieved domain to be L\"%s\", was: %s\n",
                test->name, test->hostname, wine_dbgstr_w(str));
    SysFreeString(str);
208 209 210 211 212 213 214 215 216 217 218 219 220
}

static void test_port(IHTMLLocation *loc, const struct location_test *test)
{
    HRESULT hres;
    BSTR str;

    hres = IHTMLLocation_get_port(loc, NULL);
    ok(hres == E_POINTER,
            "%s: get_port should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
            test->name, E_POINTER, hres);

    hres = IHTMLLocation_get_port(loc, &str);
221
    ok(hres == S_OK, "%s: get_port failed: 0x%08x\n", test->name, hres);
222
    if(hres == S_OK)
223 224
        ok(str_eq_wa(str, test->port)
           || (!str && !*test->port), /* IE11 */
225 226
                "%s: expected retrieved port to be L\"%s\", was: %s\n",
                test->name, test->port, wine_dbgstr_w(str));
227
    SysFreeString(str);
228 229 230 231 232 233 234 235 236 237 238 239 240 241
}

static void test_pathname(IHTMLLocation *loc, const struct location_test *test)
{
    HRESULT hres;
    BSTR str;

    hres = IHTMLLocation_get_pathname(loc, NULL);
    ok(hres == E_POINTER,
            "%s: get_pathname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
            test->name, E_POINTER, hres);

    hres = IHTMLLocation_get_pathname(loc, &str);
    ok(hres == S_OK, "%s: get_pathname failed: 0x%08x\n", test->name, hres);
242 243 244 245
    if(hres == S_OK)
        ok(str_eq_wa(str, test->pathname),
                "%s: expected retrieved pathname to be L\"%s\", was: %s\n",
                test->name, test->pathname, wine_dbgstr_w(str));
246
    SysFreeString(str);
247 248 249 250 251 252 253 254 255 256 257 258 259
}

static void test_search(IHTMLLocation *loc, const struct location_test *test)
{
    HRESULT hres;
    BSTR str;

    hres = IHTMLLocation_get_search(loc, NULL);
    ok(hres == E_POINTER,
            "%s: get_search should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
            test->name, E_POINTER, hres);

    hres = IHTMLLocation_get_search(loc, &str);
260
    ok(hres == S_OK, "%s: get_search failed: 0x%08x\n", test->name, hres);
261 262 263 264
    if(hres == S_OK)
        ok(str_eq_wa(str, test->search),
                "%s: expected retrieved search to be L\"%s\", was: %s\n",
                test->name, test->search, wine_dbgstr_w(str));
265
    SysFreeString(str);
266 267 268 269 270 271 272 273 274 275 276 277 278
}

static void test_hash(IHTMLLocation *loc, const struct location_test *test)
{
    HRESULT hres;
    BSTR str;

    hres = IHTMLLocation_get_hash(loc, NULL);
    ok(hres == E_POINTER,
            "%s: get_hash should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
            test->name, E_POINTER, hres);

    hres = IHTMLLocation_get_hash(loc, &str);
279
    ok(hres == S_OK, "%s: get_hash failed: 0x%08x\n", test->name, hres);
280 281 282 283
    if(hres == S_OK)
        ok(str_eq_wa(str, test->hash),
                "%s: expected retrieved hash to be L\"%s\", was: %s\n",
                test->name, test->hash, wine_dbgstr_w(str));
284
    SysFreeString(str);
285 286 287 288
}

static void perform_test(const struct location_test* test)
{
Jacek Caban's avatar
Jacek Caban committed
289
    WCHAR url[INTERNET_MAX_URL_LENGTH];
290 291 292 293 294 295 296 297 298 299 300 301 302
    HRESULT hres;
    IBindCtx *bc;
    IMoniker *url_mon;
    IPersistMoniker *persist_mon;
    IHTMLDocument2 *doc;
    IHTMLDocument6 *doc6;
    IHTMLLocation *location;

    hres = CreateBindCtx(0, &bc);
    ok(hres == S_OK, "%s: CreateBindCtx failed: 0x%08x\n", test->name, hres);
    if(FAILED(hres))
        return;

Jacek Caban's avatar
Jacek Caban committed
303 304
    MultiByteToWideChar(CP_ACP, 0, test->url, -1, url, sizeof(url)/sizeof(WCHAR));
    hres = CreateURLMoniker(NULL, url, &url_mon);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    ok(hres == S_OK, "%s: CreateURLMoniker failed: 0x%08x\n", test->name, hres);
    if(FAILED(hres)){
        IBindCtx_Release(bc);
        return;
    }

    hres = CoCreateInstance(&CLSID_HTMLDocument, NULL,
            CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER, &IID_IHTMLDocument2,
            (void**)&doc);
    ok(hres == S_OK, "%s: CoCreateInstance failed: 0x%08x\n", test->name, hres);
    if(FAILED(hres)){
        IMoniker_Release(url_mon);
        IBindCtx_Release(bc);
        return;
    }

    hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument6, (void**)&doc6);
    if(hres == S_OK){
        IHTMLDocument6_Release(doc6);
    }else{
        win_skip("%s: Could not get IHTMLDocument6, probably too old IE. Requires IE 8+\n", test->name);
        IMoniker_Release(url_mon);
        IBindCtx_Release(bc);
        return;
    }

    hres = IHTMLDocument2_QueryInterface(doc, &IID_IPersistMoniker,
            (void**)&persist_mon);
    ok(hres == S_OK, "%s: IHTMlDocument2_QueryInterface failed: 0x%08x\n", test->name, hres);
    if(FAILED(hres)){
        IHTMLDocument2_Release(doc);
        IMoniker_Release(url_mon);
        IBindCtx_Release(bc);
        return;
    }

    hres = IPersistMoniker_Load(persist_mon, FALSE, url_mon, bc,
            STGM_SHARE_EXCLUSIVE | STGM_READWRITE);
    ok(hres == S_OK, "%s: IPersistMoniker_Load failed: 0x%08x\n", test->name, hres);
    if(FAILED(hres)){
        IPersistMoniker_Release(persist_mon);
        IHTMLDocument2_Release(doc);
        IMoniker_Release(url_mon);
        IBindCtx_Release(bc);
        return;
    }

    hres = IHTMLDocument2_get_location(doc, &location);
    ok(hres == S_OK, "%s: IHTMLDocument2_get_location failed: 0x%08x\n", test->name, hres);
    if(FAILED(hres)){
        IPersistMoniker_Release(persist_mon);
        IHTMLDocument2_Release(doc);
        IMoniker_Release(url_mon);
        IBindCtx_Release(bc);
        return;
    }

    test_href(location, test);
    test_protocol(location, test);
    test_host(location, test);
365
    test_hostname(location, doc, test);
366 367 368 369 370 371 372 373 374 375 376 377 378 379
    test_port(location, test);
    test_pathname(location, test);
    test_search(location, test);
    test_hash(location, test);

    IHTMLLocation_Release(location);
    IPersistMoniker_Release(persist_mon);
    IHTMLDocument2_Release(doc);
    IMoniker_Release(url_mon);
    IBindCtx_Release(bc);
}

START_TEST(htmllocation)
{
Jacek Caban's avatar
Jacek Caban committed
380 381
    int i;

382 383
    CoInitialize(NULL);

Jacek Caban's avatar
Jacek Caban committed
384 385
    for(i=0; i < sizeof(location_tests)/sizeof(*location_tests); i++)
        perform_test(location_tests+i);
386 387 388

    CoUninitialize();
}