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