mshtml: Added ITargetContainer stub implementation.
[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     SysFreeString(str);
135 }
136
137 static void test_protocol(IHTMLLocation *loc, const struct location_test *test)
138 {
139     HRESULT hres;
140     BSTR str;
141
142     hres = IHTMLLocation_get_protocol(loc, NULL);
143     ok(hres == E_POINTER,
144             "%s: get_protocol should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
145             test->name, E_POINTER, hres);
146
147     hres = IHTMLLocation_get_protocol(loc, &str);
148     ok(hres == S_OK, "%s: get_protocol failed: 0x%08x\n", test->name, hres);
149     if(hres == S_OK)
150         ok(str_eq_wa(str, test->protocol),
151                 "%s: expected retrieved protocol to be L\"%s\", was: %s\n",
152                 test->name, test->protocol, wine_dbgstr_w(str));
153     SysFreeString(str);
154 }
155
156 static void test_host(IHTMLLocation *loc, const struct location_test *test)
157 {
158     HRESULT hres;
159     BSTR str;
160
161     hres = IHTMLLocation_get_host(loc, NULL);
162     ok(hres == E_POINTER,
163             "%s: get_host should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
164             test->name, E_POINTER, hres);
165
166     hres = IHTMLLocation_get_host(loc, &str);
167     ok(hres == S_OK, "%s: get_host failed: 0x%08x\n", test->name, hres);
168     if(hres == S_OK)
169         ok(str_eq_wa(str, test->host),
170                 "%s: expected retrieved host to be L\"%s\", was: %s\n",
171                 test->name, test->host, wine_dbgstr_w(str));
172     SysFreeString(str);
173 }
174
175 static void test_hostname(IHTMLLocation *loc, const struct location_test *test)
176 {
177     HRESULT hres;
178     BSTR str;
179
180     hres = IHTMLLocation_get_hostname(loc, NULL);
181     ok(hres == E_POINTER,
182             "%s: get_hostname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
183             test->name, E_POINTER, hres);
184
185     hres = IHTMLLocation_get_hostname(loc, &str);
186     ok(hres == S_OK, "%s: get_hostname failed: 0x%08x\n", test->name, hres);
187     if(hres == S_OK)
188         ok(str_eq_wa(str, test->hostname),
189                 "%s: expected retrieved hostname to be L\"%s\", was: %s\n",
190                 test->name, test->hostname, wine_dbgstr_w(str));
191     SysFreeString(str);
192 }
193
194 static void test_port(IHTMLLocation *loc, const struct location_test *test)
195 {
196     HRESULT hres;
197     BSTR str;
198
199     hres = IHTMLLocation_get_port(loc, NULL);
200     ok(hres == E_POINTER,
201             "%s: get_port should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
202             test->name, E_POINTER, hres);
203
204     hres = IHTMLLocation_get_port(loc, &str);
205     ok(hres == S_OK, "%s: get_port failed: 0x%08x\n", test->name, hres);
206     if(hres == S_OK)
207         ok(str_eq_wa(str, test->port),
208                 "%s: expected retrieved port to be L\"%s\", was: %s\n",
209                 test->name, test->port, wine_dbgstr_w(str));
210     SysFreeString(str);
211 }
212
213 static void test_pathname(IHTMLLocation *loc, const struct location_test *test)
214 {
215     HRESULT hres;
216     BSTR str;
217
218     hres = IHTMLLocation_get_pathname(loc, NULL);
219     ok(hres == E_POINTER,
220             "%s: get_pathname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
221             test->name, E_POINTER, hres);
222
223     hres = IHTMLLocation_get_pathname(loc, &str);
224     ok(hres == S_OK, "%s: get_pathname failed: 0x%08x\n", test->name, hres);
225     if(hres == S_OK)
226         ok(str_eq_wa(str, test->pathname),
227                 "%s: expected retrieved pathname to be L\"%s\", was: %s\n",
228                 test->name, test->pathname, wine_dbgstr_w(str));
229     SysFreeString(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     SysFreeString(str);
249 }
250
251 static void test_hash(IHTMLLocation *loc, const struct location_test *test)
252 {
253     HRESULT hres;
254     BSTR str;
255
256     hres = IHTMLLocation_get_hash(loc, NULL);
257     ok(hres == E_POINTER,
258             "%s: get_hash should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
259             test->name, E_POINTER, hres);
260
261     hres = IHTMLLocation_get_hash(loc, &str);
262     ok(hres == S_OK, "%s: get_hash failed: 0x%08x\n", test->name, hres);
263     if(hres == S_OK)
264         ok(str_eq_wa(str, test->hash),
265                 "%s: expected retrieved hash to be L\"%s\", was: %s\n",
266                 test->name, test->hash, wine_dbgstr_w(str));
267     SysFreeString(str);
268 }
269
270 static void perform_test(const struct location_test* test)
271 {
272     WCHAR url[INTERNET_MAX_URL_LENGTH];
273     HRESULT hres;
274     IBindCtx *bc;
275     IMoniker *url_mon;
276     IPersistMoniker *persist_mon;
277     IHTMLDocument2 *doc;
278     IHTMLDocument6 *doc6;
279     IHTMLLocation *location;
280
281     hres = CreateBindCtx(0, &bc);
282     ok(hres == S_OK, "%s: CreateBindCtx failed: 0x%08x\n", test->name, hres);
283     if(FAILED(hres))
284         return;
285
286     MultiByteToWideChar(CP_ACP, 0, test->url, -1, url, sizeof(url)/sizeof(WCHAR));
287     hres = CreateURLMoniker(NULL, url, &url_mon);
288     ok(hres == S_OK, "%s: CreateURLMoniker failed: 0x%08x\n", test->name, hres);
289     if(FAILED(hres)){
290         IBindCtx_Release(bc);
291         return;
292     }
293
294     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL,
295             CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER, &IID_IHTMLDocument2,
296             (void**)&doc);
297     ok(hres == S_OK, "%s: CoCreateInstance failed: 0x%08x\n", test->name, hres);
298     if(FAILED(hres)){
299         IMoniker_Release(url_mon);
300         IBindCtx_Release(bc);
301         return;
302     }
303
304     hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument6, (void**)&doc6);
305     if(hres == S_OK){
306         IHTMLDocument6_Release(doc6);
307     }else{
308         win_skip("%s: Could not get IHTMLDocument6, probably too old IE. Requires IE 8+\n", test->name);
309         IMoniker_Release(url_mon);
310         IBindCtx_Release(bc);
311         return;
312     }
313
314     hres = IHTMLDocument2_QueryInterface(doc, &IID_IPersistMoniker,
315             (void**)&persist_mon);
316     ok(hres == S_OK, "%s: IHTMlDocument2_QueryInterface failed: 0x%08x\n", test->name, hres);
317     if(FAILED(hres)){
318         IHTMLDocument2_Release(doc);
319         IMoniker_Release(url_mon);
320         IBindCtx_Release(bc);
321         return;
322     }
323
324     hres = IPersistMoniker_Load(persist_mon, FALSE, url_mon, bc,
325             STGM_SHARE_EXCLUSIVE | STGM_READWRITE);
326     ok(hres == S_OK, "%s: IPersistMoniker_Load failed: 0x%08x\n", test->name, hres);
327     if(FAILED(hres)){
328         IPersistMoniker_Release(persist_mon);
329         IHTMLDocument2_Release(doc);
330         IMoniker_Release(url_mon);
331         IBindCtx_Release(bc);
332         return;
333     }
334
335     hres = IHTMLDocument2_get_location(doc, &location);
336     ok(hres == S_OK, "%s: IHTMLDocument2_get_location failed: 0x%08x\n", test->name, hres);
337     if(FAILED(hres)){
338         IPersistMoniker_Release(persist_mon);
339         IHTMLDocument2_Release(doc);
340         IMoniker_Release(url_mon);
341         IBindCtx_Release(bc);
342         return;
343     }
344
345     test_href(location, test);
346     test_protocol(location, test);
347     test_host(location, test);
348     test_hostname(location, test);
349     test_port(location, test);
350     test_pathname(location, test);
351     test_search(location, test);
352     test_hash(location, test);
353
354     IHTMLLocation_Release(location);
355     IPersistMoniker_Release(persist_mon);
356     IHTMLDocument2_Release(doc);
357     IMoniker_Release(url_mon);
358     IBindCtx_Release(bc);
359 }
360
361 START_TEST(htmllocation)
362 {
363     int i;
364
365     CoInitialize(NULL);
366
367     for(i=0; i < sizeof(location_tests)/sizeof(*location_tests); i++)
368         perform_test(location_tests+i);
369
370     CoUninitialize();
371 }