mshtml/tests: Run tests again on boxes with older IE.
[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
26 struct location_test {
27     const char *name;
28     const WCHAR *url;
29
30     const char *href;
31     const char *protocol;
32     const char *host;
33     const char *hostname;
34     const char *port;
35     const char *pathname;
36     const char *search;
37     const char *hash;
38 };
39
40 static const WCHAR http_url[] = {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.','o','r','g','?','s','e','a','r','c','h','#','h','a','s','h',0};
41 static const struct location_test http_test = {
42             "HTTP",
43             http_url,
44             "http://www.winehq.org/?search#hash",
45             "http:",
46             "www.winehq.org:80",
47             "www.winehq.org",
48             "80",
49             "",
50             "?search",
51             "#hash"
52             };
53
54 static const WCHAR http_file_url[] = {'h','t','t','p',':','/','/','w','w','w','.','w','i','n','e','h','q','.','o','r','g','/','f','i','l','e','?','s','e','a','r','c','h','#','h','a','s','h',0};
55 static const struct location_test http_file_test = {
56             "HTTP with file",
57             http_file_url,
58             "http://www.winehq.org/file?search#hash",
59             "http:",
60             "www.winehq.org:80",
61             "www.winehq.org",
62             "80",
63             "file",
64             "?search",
65             "#hash"
66             };
67
68 static const WCHAR ftp_url[] = {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g','/',0};
69 static const struct location_test ftp_test = {
70             "FTP",
71             ftp_url,
72             "ftp://ftp.winehq.org/",
73             "ftp:",
74             "ftp.winehq.org:21",
75             "ftp.winehq.org",
76             "21",
77             "",
78             NULL,
79             NULL
80             };
81
82 static const WCHAR ftp_file_url[] = {'f','t','p',':','/','/','f','t','p','.','w','i','n','e','h','q','.','o','r','g','/','f','i','l','e',0};
83 static const struct location_test ftp_file_test = {
84             "FTP with file",
85             ftp_file_url,
86             "ftp://ftp.winehq.org/file",
87             "ftp:",
88             "ftp.winehq.org:21",
89             "ftp.winehq.org",
90             "21",
91             "file",
92             NULL,
93             NULL
94             };
95
96 static const WCHAR file_url[] = {'f','i','l','e',':','/','/','C',':','\\','w','i','n','d','o','w','s','\\','w','i','n','.','i','n','i',0};
97 static const struct location_test file_test = {
98             "FILE",
99             file_url,
100             "file:///C:/windows/win.ini",
101             "file:",
102             NULL,
103             NULL,
104             "",
105             "C:\\windows\\win.ini",
106             NULL,
107             NULL
108             };
109
110 static int str_eq_wa(LPCWSTR strw, const char *stra)
111 {
112     CHAR buf[512];
113
114     if(strw == NULL || stra == NULL){
115         if((void*)strw == (void*)stra)
116             return 1;
117         return 0;
118     }
119
120     WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
121     return !lstrcmpA(stra, buf);
122 }
123
124 static void test_href(IHTMLLocation *loc, const struct location_test *test)
125 {
126     HRESULT hres;
127     BSTR str;
128
129     hres = IHTMLLocation_get_href(loc, NULL);
130     ok(hres == E_POINTER,
131             "%s: get_href should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
132             test->name, E_POINTER, hres);
133
134     hres = IHTMLLocation_get_href(loc, &str);
135     ok(hres == S_OK, "%s: get_href failed: 0x%08x\n", test->name, hres);
136     if(hres == S_OK)
137         ok(str_eq_wa(str, test->href),
138                 "%s: expected retrieved href to be L\"%s\", was: %s\n",
139                 test->name, test->href, wine_dbgstr_w(str));
140 }
141
142 static void test_protocol(IHTMLLocation *loc, const struct location_test *test)
143 {
144     HRESULT hres;
145     BSTR str;
146
147     hres = IHTMLLocation_get_protocol(loc, NULL);
148     ok(hres == E_POINTER,
149             "%s: get_protocol should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
150             test->name, E_POINTER, hres);
151
152     hres = IHTMLLocation_get_protocol(loc, &str);
153     ok(hres == S_OK, "%s: get_protocol failed: 0x%08x\n", test->name, hres);
154     if(hres == S_OK)
155         ok(str_eq_wa(str, test->protocol),
156                 "%s: expected retrieved protocol to be L\"%s\", was: %s\n",
157                 test->name, test->protocol, wine_dbgstr_w(str));
158 }
159
160 static void test_host(IHTMLLocation *loc, const struct location_test *test)
161 {
162     HRESULT hres;
163     BSTR str;
164
165     hres = IHTMLLocation_get_host(loc, NULL);
166     ok(hres == E_POINTER,
167             "%s: get_host should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
168             test->name, E_POINTER, hres);
169
170     hres = IHTMLLocation_get_host(loc, &str);
171     ok(hres == S_OK, "%s: get_host failed: 0x%08x\n", test->name, hres);
172     if(hres == S_OK)
173         ok(str_eq_wa(str, test->host),
174                 "%s: expected retrieved host to be L\"%s\", was: %s\n",
175                 test->name, test->host, wine_dbgstr_w(str));
176 }
177
178 static void test_hostname(IHTMLLocation *loc, const struct location_test *test)
179 {
180     HRESULT hres;
181     BSTR str;
182
183     hres = IHTMLLocation_get_hostname(loc, NULL);
184     ok(hres == E_POINTER,
185             "%s: get_hostname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
186             test->name, E_POINTER, hres);
187
188     hres = IHTMLLocation_get_hostname(loc, &str);
189     ok(hres == S_OK, "%s: get_hostname failed: 0x%08x\n", test->name, hres);
190     if(hres == S_OK)
191         ok(str_eq_wa(str, test->hostname),
192                 "%s: expected retrieved hostname to be L\"%s\", was: %s\n",
193                 test->name, test->hostname, wine_dbgstr_w(str));
194 }
195
196 static void test_port(IHTMLLocation *loc, const struct location_test *test)
197 {
198     HRESULT hres;
199     BSTR str;
200
201     hres = IHTMLLocation_get_port(loc, NULL);
202     ok(hres == E_POINTER,
203             "%s: get_port should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
204             test->name, E_POINTER, hres);
205
206     hres = IHTMLLocation_get_port(loc, &str);
207     ok(hres == S_OK, "%s: get_port failed: 0x%08x\n", test->name, hres);
208     if(hres == S_OK)
209         ok(str_eq_wa(str, test->port),
210                 "%s: expected retrieved port to be L\"%s\", was: %s\n",
211                 test->name, test->port, wine_dbgstr_w(str));
212 }
213
214 static void test_pathname(IHTMLLocation *loc, const struct location_test *test)
215 {
216     HRESULT hres;
217     BSTR str;
218
219     hres = IHTMLLocation_get_pathname(loc, NULL);
220     ok(hres == E_POINTER,
221             "%s: get_pathname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
222             test->name, E_POINTER, hres);
223
224     hres = IHTMLLocation_get_pathname(loc, &str);
225     ok(hres == S_OK, "%s: get_pathname failed: 0x%08x\n", test->name, hres);
226     if(hres == S_OK)
227         ok(str_eq_wa(str, test->pathname),
228                 "%s: expected retrieved pathname to be L\"%s\", was: %s\n",
229                 test->name, test->pathname, wine_dbgstr_w(str));
230 }
231
232 static void test_search(IHTMLLocation *loc, const struct location_test *test)
233 {
234     HRESULT hres;
235     BSTR str;
236
237     hres = IHTMLLocation_get_search(loc, NULL);
238     ok(hres == E_POINTER,
239             "%s: get_search should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
240             test->name, E_POINTER, hres);
241
242     hres = IHTMLLocation_get_search(loc, &str);
243     ok(hres == S_OK, "%s: get_search failed: 0x%08x\n", test->name, hres);
244     if(hres == S_OK)
245         ok(str_eq_wa(str, test->search),
246                 "%s: expected retrieved search to be L\"%s\", was: %s\n",
247                 test->name, test->search, wine_dbgstr_w(str));
248 }
249
250 static void test_hash(IHTMLLocation *loc, const struct location_test *test)
251 {
252     HRESULT hres;
253     BSTR str;
254
255     hres = IHTMLLocation_get_hash(loc, NULL);
256     ok(hres == E_POINTER,
257             "%s: get_hash should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
258             test->name, E_POINTER, hres);
259
260     hres = IHTMLLocation_get_hash(loc, &str);
261     ok(hres == S_OK, "%s: get_hash failed: 0x%08x\n", test->name, hres);
262     if(hres == S_OK)
263         ok(str_eq_wa(str, test->hash),
264                 "%s: expected retrieved hash to be L\"%s\", was: %s\n",
265                 test->name, test->hash, wine_dbgstr_w(str));
266 }
267
268 static void perform_test(const struct location_test* test)
269 {
270     HRESULT hres;
271     IBindCtx *bc;
272     IMoniker *url_mon;
273     IPersistMoniker *persist_mon;
274     IHTMLDocument2 *doc;
275     IHTMLDocument6 *doc6;
276     IHTMLLocation *location;
277
278     hres = CreateBindCtx(0, &bc);
279     ok(hres == S_OK, "%s: CreateBindCtx failed: 0x%08x\n", test->name, hres);
280     if(FAILED(hres))
281         return;
282
283     hres = CreateURLMoniker(NULL, test->url, &url_mon);
284     ok(hres == S_OK, "%s: CreateURLMoniker failed: 0x%08x\n", test->name, hres);
285     if(FAILED(hres)){
286         IBindCtx_Release(bc);
287         return;
288     }
289
290     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL,
291             CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER, &IID_IHTMLDocument2,
292             (void**)&doc);
293     ok(hres == S_OK, "%s: CoCreateInstance failed: 0x%08x\n", test->name, hres);
294     if(FAILED(hres)){
295         IMoniker_Release(url_mon);
296         IBindCtx_Release(bc);
297         return;
298     }
299
300     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument6, (void**)&doc6);
301     if(hres == S_OK){
302         IHTMLDocument6_Release(doc6);
303     }else{
304         win_skip("%s: Could not get IHTMLDocument6, probably too old IE. Requires IE 8+\n", test->name);
305         IMoniker_Release(url_mon);
306         IBindCtx_Release(bc);
307         return;
308     }
309
310     hres = IHTMLDocument2_QueryInterface(doc, &IID_IPersistMoniker,
311             (void**)&persist_mon);
312     ok(hres == S_OK, "%s: IHTMlDocument2_QueryInterface failed: 0x%08x\n", test->name, hres);
313     if(FAILED(hres)){
314         IHTMLDocument2_Release(doc);
315         IMoniker_Release(url_mon);
316         IBindCtx_Release(bc);
317         return;
318     }
319
320     hres = IPersistMoniker_Load(persist_mon, FALSE, url_mon, bc,
321             STGM_SHARE_EXCLUSIVE | STGM_READWRITE);
322     ok(hres == S_OK, "%s: IPersistMoniker_Load failed: 0x%08x\n", test->name, hres);
323     if(FAILED(hres)){
324         IPersistMoniker_Release(persist_mon);
325         IHTMLDocument2_Release(doc);
326         IMoniker_Release(url_mon);
327         IBindCtx_Release(bc);
328         return;
329     }
330
331     hres = IHTMLDocument2_get_location(doc, &location);
332     ok(hres == S_OK, "%s: IHTMLDocument2_get_location failed: 0x%08x\n", test->name, hres);
333     if(FAILED(hres)){
334         IPersistMoniker_Release(persist_mon);
335         IHTMLDocument2_Release(doc);
336         IMoniker_Release(url_mon);
337         IBindCtx_Release(bc);
338         return;
339     }
340
341     test_href(location, test);
342     test_protocol(location, test);
343     test_host(location, test);
344     test_hostname(location, test);
345     test_port(location, test);
346     test_pathname(location, test);
347     test_search(location, test);
348     test_hash(location, test);
349
350     IHTMLLocation_Release(location);
351     IPersistMoniker_Release(persist_mon);
352     IHTMLDocument2_Release(doc);
353     IMoniker_Release(url_mon);
354     IBindCtx_Release(bc);
355 }
356
357 START_TEST(htmllocation)
358 {
359     CoInitialize(NULL);
360
361     perform_test(&http_test);
362     perform_test(&http_file_test);
363     perform_test(&ftp_test);
364     perform_test(&ftp_file_test);
365     perform_test(&file_test);
366
367     CoUninitialize();
368 }