mshtml: Added DOCHOST_DOCCANNAVIGATE tests.
[wine] / dlls / mshtml / tests / htmllocation.c
1 /*
2  * Copyright 2009 Andrew Eikum for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20 #define CONST_VTABLE
21
22 #include <wine/test.h>
23
24 #include "mshtml.h"
25 #include "wininet.h"
26
27 struct location_test {
28     const char *name;
29     const char *url;
30
31     const char *href;
32     const char *protocol;
33     const char *host;
34     const char *hostname;
35     const char *port;
36     const char *pathname;
37     const char *search;
38     const char *hash;
39 };
40
41 static const struct location_test location_tests[] = {
42     {
43         "HTTP",
44         "http://www.winehq.org?search#hash",
45         "http://www.winehq.org/?search#hash",
46         "http:",
47         "www.winehq.org:80",
48         "www.winehq.org",
49         "80",
50         "",
51         "?search",
52         "#hash"
53     },
54     {
55         "HTTP with file",
56         "http://www.winehq.org/file?search#hash",
57         "http://www.winehq.org/file?search#hash",
58         "http:",
59         "www.winehq.org:80",
60         "www.winehq.org",
61         "80",
62         "file",
63         "?search",
64         "#hash"
65     },
66     {
67         "FTP",
68         "ftp://ftp.winehq.org/",
69         "ftp://ftp.winehq.org/",
70         "ftp:",
71         "ftp.winehq.org:21",
72         "ftp.winehq.org",
73         "21",
74         "",
75         NULL,
76         NULL
77     },
78     {
79         "FTP with file",
80         "ftp://ftp.winehq.org/file",
81         "ftp://ftp.winehq.org/file",
82         "ftp:",
83         "ftp.winehq.org:21",
84         "ftp.winehq.org",
85         "21",
86         "file",
87         NULL,
88         NULL
89     },
90     {
91         "FILE",
92         "file://C:\\windows\\win.ini",
93         "file:///C:/windows/win.ini",
94         "file:",
95         NULL,
96         NULL,
97         "",
98         "C:\\windows\\win.ini",
99         NULL,
100         NULL
101     }
102 };
103
104 static int str_eq_wa(LPCWSTR strw, const char *stra)
105 {
106     CHAR buf[512];
107
108     if(strw == NULL || stra == NULL){
109         if((void*)strw == (void*)stra)
110             return 1;
111         return 0;
112     }
113
114     WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
115     return !lstrcmpA(stra, buf);
116 }
117
118 static void test_href(IHTMLLocation *loc, const struct location_test *test)
119 {
120     HRESULT hres;
121     BSTR str;
122
123     hres = IHTMLLocation_get_href(loc, NULL);
124     ok(hres == E_POINTER,
125             "%s: get_href should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
126             test->name, E_POINTER, hres);
127
128     hres = IHTMLLocation_get_href(loc, &str);
129     ok(hres == S_OK, "%s: get_href failed: 0x%08x\n", test->name, hres);
130     if(hres == S_OK)
131         ok(str_eq_wa(str, test->href),
132                 "%s: expected retrieved href to be L\"%s\", was: %s\n",
133                 test->name, test->href, wine_dbgstr_w(str));
134 }
135
136 static void test_protocol(IHTMLLocation *loc, const struct location_test *test)
137 {
138     HRESULT hres;
139     BSTR str;
140
141     hres = IHTMLLocation_get_protocol(loc, NULL);
142     ok(hres == E_POINTER,
143             "%s: get_protocol should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
144             test->name, E_POINTER, hres);
145
146     hres = IHTMLLocation_get_protocol(loc, &str);
147     ok(hres == S_OK, "%s: get_protocol failed: 0x%08x\n", test->name, hres);
148     if(hres == S_OK)
149         ok(str_eq_wa(str, test->protocol),
150                 "%s: expected retrieved protocol to be L\"%s\", was: %s\n",
151                 test->name, test->protocol, wine_dbgstr_w(str));
152 }
153
154 static void test_host(IHTMLLocation *loc, const struct location_test *test)
155 {
156     HRESULT hres;
157     BSTR str;
158
159     hres = IHTMLLocation_get_host(loc, NULL);
160     ok(hres == E_POINTER,
161             "%s: get_host should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
162             test->name, E_POINTER, hres);
163
164     hres = IHTMLLocation_get_host(loc, &str);
165     ok(hres == S_OK, "%s: get_host failed: 0x%08x\n", test->name, hres);
166     if(hres == S_OK)
167         ok(str_eq_wa(str, test->host),
168                 "%s: expected retrieved host to be L\"%s\", was: %s\n",
169                 test->name, test->host, wine_dbgstr_w(str));
170 }
171
172 static void test_hostname(IHTMLLocation *loc, const struct location_test *test)
173 {
174     HRESULT hres;
175     BSTR str;
176
177     hres = IHTMLLocation_get_hostname(loc, NULL);
178     ok(hres == E_POINTER,
179             "%s: get_hostname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
180             test->name, E_POINTER, hres);
181
182     hres = IHTMLLocation_get_hostname(loc, &str);
183     ok(hres == S_OK, "%s: get_hostname failed: 0x%08x\n", test->name, hres);
184     if(hres == S_OK)
185         ok(str_eq_wa(str, test->hostname),
186                 "%s: expected retrieved hostname to be L\"%s\", was: %s\n",
187                 test->name, test->hostname, wine_dbgstr_w(str));
188 }
189
190 static void test_port(IHTMLLocation *loc, const struct location_test *test)
191 {
192     HRESULT hres;
193     BSTR str;
194
195     hres = IHTMLLocation_get_port(loc, NULL);
196     ok(hres == E_POINTER,
197             "%s: get_port should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
198             test->name, E_POINTER, hres);
199
200     hres = IHTMLLocation_get_port(loc, &str);
201     ok(hres == S_OK, "%s: get_port failed: 0x%08x\n", test->name, hres);
202     if(hres == S_OK)
203         ok(str_eq_wa(str, test->port),
204                 "%s: expected retrieved port to be L\"%s\", was: %s\n",
205                 test->name, test->port, wine_dbgstr_w(str));
206 }
207
208 static void test_pathname(IHTMLLocation *loc, const struct location_test *test)
209 {
210     HRESULT hres;
211     BSTR str;
212
213     hres = IHTMLLocation_get_pathname(loc, NULL);
214     ok(hres == E_POINTER,
215             "%s: get_pathname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
216             test->name, E_POINTER, hres);
217
218     hres = IHTMLLocation_get_pathname(loc, &str);
219     ok(hres == S_OK, "%s: get_pathname failed: 0x%08x\n", test->name, hres);
220     if(hres == S_OK)
221         ok(str_eq_wa(str, test->pathname),
222                 "%s: expected retrieved pathname to be L\"%s\", was: %s\n",
223                 test->name, test->pathname, wine_dbgstr_w(str));
224 }
225
226 static void test_search(IHTMLLocation *loc, const struct location_test *test)
227 {
228     HRESULT hres;
229     BSTR str;
230
231     hres = IHTMLLocation_get_search(loc, NULL);
232     ok(hres == E_POINTER,
233             "%s: get_search should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
234             test->name, E_POINTER, hres);
235
236     hres = IHTMLLocation_get_search(loc, &str);
237     ok(hres == S_OK, "%s: get_search failed: 0x%08x\n", test->name, hres);
238     if(hres == S_OK)
239         ok(str_eq_wa(str, test->search),
240                 "%s: expected retrieved search to be L\"%s\", was: %s\n",
241                 test->name, test->search, wine_dbgstr_w(str));
242 }
243
244 static void test_hash(IHTMLLocation *loc, const struct location_test *test)
245 {
246     HRESULT hres;
247     BSTR str;
248
249     hres = IHTMLLocation_get_hash(loc, NULL);
250     ok(hres == E_POINTER,
251             "%s: get_hash should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
252             test->name, E_POINTER, hres);
253
254     hres = IHTMLLocation_get_hash(loc, &str);
255     ok(hres == S_OK, "%s: get_hash failed: 0x%08x\n", test->name, hres);
256     if(hres == S_OK)
257         ok(str_eq_wa(str, test->hash),
258                 "%s: expected retrieved hash to be L\"%s\", was: %s\n",
259                 test->name, test->hash, wine_dbgstr_w(str));
260 }
261
262 static void perform_test(const struct location_test* test)
263 {
264     WCHAR url[INTERNET_MAX_URL_LENGTH];
265     HRESULT hres;
266     IBindCtx *bc;
267     IMoniker *url_mon;
268     IPersistMoniker *persist_mon;
269     IHTMLDocument2 *doc;
270     IHTMLDocument6 *doc6;
271     IHTMLLocation *location;
272
273     hres = CreateBindCtx(0, &bc);
274     ok(hres == S_OK, "%s: CreateBindCtx failed: 0x%08x\n", test->name, hres);
275     if(FAILED(hres))
276         return;
277
278     MultiByteToWideChar(CP_ACP, 0, test->url, -1, url, sizeof(url)/sizeof(WCHAR));
279     hres = CreateURLMoniker(NULL, url, &url_mon);
280     ok(hres == S_OK, "%s: CreateURLMoniker failed: 0x%08x\n", test->name, hres);
281     if(FAILED(hres)){
282         IBindCtx_Release(bc);
283         return;
284     }
285
286     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL,
287             CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER, &IID_IHTMLDocument2,
288             (void**)&doc);
289     ok(hres == S_OK, "%s: CoCreateInstance failed: 0x%08x\n", test->name, hres);
290     if(FAILED(hres)){
291         IMoniker_Release(url_mon);
292         IBindCtx_Release(bc);
293         return;
294     }
295
296     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument6, (void**)&doc6);
297     if(hres == S_OK){
298         IHTMLDocument6_Release(doc6);
299     }else{
300         win_skip("%s: Could not get IHTMLDocument6, probably too old IE. Requires IE 8+\n", test->name);
301         IMoniker_Release(url_mon);
302         IBindCtx_Release(bc);
303         return;
304     }
305
306     hres = IHTMLDocument2_QueryInterface(doc, &IID_IPersistMoniker,
307             (void**)&persist_mon);
308     ok(hres == S_OK, "%s: IHTMlDocument2_QueryInterface failed: 0x%08x\n", test->name, hres);
309     if(FAILED(hres)){
310         IHTMLDocument2_Release(doc);
311         IMoniker_Release(url_mon);
312         IBindCtx_Release(bc);
313         return;
314     }
315
316     hres = IPersistMoniker_Load(persist_mon, FALSE, url_mon, bc,
317             STGM_SHARE_EXCLUSIVE | STGM_READWRITE);
318     ok(hres == S_OK, "%s: IPersistMoniker_Load failed: 0x%08x\n", test->name, hres);
319     if(FAILED(hres)){
320         IPersistMoniker_Release(persist_mon);
321         IHTMLDocument2_Release(doc);
322         IMoniker_Release(url_mon);
323         IBindCtx_Release(bc);
324         return;
325     }
326
327     hres = IHTMLDocument2_get_location(doc, &location);
328     ok(hres == S_OK, "%s: IHTMLDocument2_get_location failed: 0x%08x\n", test->name, hres);
329     if(FAILED(hres)){
330         IPersistMoniker_Release(persist_mon);
331         IHTMLDocument2_Release(doc);
332         IMoniker_Release(url_mon);
333         IBindCtx_Release(bc);
334         return;
335     }
336
337     test_href(location, test);
338     test_protocol(location, test);
339     test_host(location, test);
340     test_hostname(location, test);
341     test_port(location, test);
342     test_pathname(location, test);
343     test_search(location, test);
344     test_hash(location, test);
345
346     IHTMLLocation_Release(location);
347     IPersistMoniker_Release(persist_mon);
348     IHTMLDocument2_Release(doc);
349     IMoniker_Release(url_mon);
350     IBindCtx_Release(bc);
351 }
352
353 START_TEST(htmllocation)
354 {
355     int i;
356
357     CoInitialize(NULL);
358
359     for(i=0; i < sizeof(location_tests)/sizeof(*location_tests); i++)
360         perform_test(location_tests+i);
361
362     CoUninitialize();
363 }