Merge my current work done on the kd++ branch:
[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 == NULL || stra == NULL){
111 if((void*)strw == (void*)stra)
112 return 1;
113 return 0;
114 }
115
116 WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), NULL, NULL);
117 return !lstrcmpA(stra, buf);
118 }
119
120 static void test_href(IHTMLLocation *loc, const struct location_test *test)
121 {
122 HRESULT hres;
123 BSTR str;
124
125 hres = IHTMLLocation_get_href(loc, NULL);
126 ok(hres == E_POINTER,
127 "%s: get_href should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
128 test->name, E_POINTER, hres);
129
130 hres = IHTMLLocation_get_href(loc, &str);
131 ok(hres == S_OK, "%s: get_href failed: 0x%08x\n", test->name, hres);
132 if(hres == S_OK)
133 ok(str_eq_wa(str, test->href),
134 "%s: expected retrieved href to be L\"%s\", was: %s\n",
135 test->name, test->href, wine_dbgstr_w(str));
136 }
137
138 static void test_protocol(IHTMLLocation *loc, const struct location_test *test)
139 {
140 HRESULT hres;
141 BSTR str;
142
143 hres = IHTMLLocation_get_protocol(loc, NULL);
144 ok(hres == E_POINTER,
145 "%s: get_protocol should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
146 test->name, E_POINTER, hres);
147
148 hres = IHTMLLocation_get_protocol(loc, &str);
149 ok(hres == S_OK, "%s: get_protocol failed: 0x%08x\n", test->name, hres);
150 if(hres == S_OK)
151 ok(str_eq_wa(str, test->protocol),
152 "%s: expected retrieved protocol to be L\"%s\", was: %s\n",
153 test->name, test->protocol, wine_dbgstr_w(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 }
173
174 static void test_hostname(IHTMLLocation *loc, const struct location_test *test)
175 {
176 HRESULT hres;
177 BSTR str;
178
179 hres = IHTMLLocation_get_hostname(loc, NULL);
180 ok(hres == E_POINTER,
181 "%s: get_hostname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
182 test->name, E_POINTER, hres);
183
184 hres = IHTMLLocation_get_hostname(loc, &str);
185 ok(hres == S_OK, "%s: get_hostname failed: 0x%08x\n", test->name, hres);
186 if(hres == S_OK)
187 ok(str_eq_wa(str, test->hostname),
188 "%s: expected retrieved hostname to be L\"%s\", was: %s\n",
189 test->name, test->hostname, wine_dbgstr_w(str));
190 }
191
192 static void test_port(IHTMLLocation *loc, const struct location_test *test)
193 {
194 HRESULT hres;
195 BSTR str;
196
197 hres = IHTMLLocation_get_port(loc, NULL);
198 ok(hres == E_POINTER,
199 "%s: get_port should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
200 test->name, E_POINTER, hres);
201
202 hres = IHTMLLocation_get_port(loc, &str);
203 ok(hres == S_OK, "%s: get_port failed: 0x%08x\n", test->name, hres);
204 if(hres == S_OK)
205 ok(str_eq_wa(str, test->port),
206 "%s: expected retrieved port to be L\"%s\", was: %s\n",
207 test->name, test->port, wine_dbgstr_w(str));
208 }
209
210 static void test_pathname(IHTMLLocation *loc, const struct location_test *test)
211 {
212 HRESULT hres;
213 BSTR str;
214
215 hres = IHTMLLocation_get_pathname(loc, NULL);
216 ok(hres == E_POINTER,
217 "%s: get_pathname should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
218 test->name, E_POINTER, hres);
219
220 hres = IHTMLLocation_get_pathname(loc, &str);
221 ok(hres == S_OK, "%s: get_pathname failed: 0x%08x\n", test->name, hres);
222 if(hres == S_OK)
223 ok(str_eq_wa(str, test->pathname),
224 "%s: expected retrieved pathname to be L\"%s\", was: %s\n",
225 test->name, test->pathname, wine_dbgstr_w(str));
226 }
227
228 static void test_search(IHTMLLocation *loc, const struct location_test *test)
229 {
230 HRESULT hres;
231 BSTR str;
232
233 hres = IHTMLLocation_get_search(loc, NULL);
234 ok(hres == E_POINTER,
235 "%s: get_search should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
236 test->name, E_POINTER, hres);
237
238 hres = IHTMLLocation_get_search(loc, &str);
239 ok(hres == S_OK, "%s: get_search failed: 0x%08x\n", test->name, hres);
240 if(hres == S_OK)
241 ok(str_eq_wa(str, test->search),
242 "%s: expected retrieved search to be L\"%s\", was: %s\n",
243 test->name, test->search, wine_dbgstr_w(str));
244 }
245
246 static void test_hash(IHTMLLocation *loc, const struct location_test *test)
247 {
248 HRESULT hres;
249 BSTR str;
250
251 hres = IHTMLLocation_get_hash(loc, NULL);
252 ok(hres == E_POINTER,
253 "%s: get_hash should have failed with E_POINTER (0x%08x), was: 0x%08x\n",
254 test->name, E_POINTER, hres);
255
256 hres = IHTMLLocation_get_hash(loc, &str);
257 ok(hres == S_OK, "%s: get_hash failed: 0x%08x\n", test->name, hres);
258 if(hres == S_OK)
259 ok(str_eq_wa(str, test->hash),
260 "%s: expected retrieved hash to be L\"%s\", was: %s\n",
261 test->name, test->hash, wine_dbgstr_w(str));
262 }
263
264 static void perform_test(const struct location_test* test)
265 {
266 WCHAR url[INTERNET_MAX_URL_LENGTH];
267 HRESULT hres;
268 IBindCtx *bc;
269 IMoniker *url_mon;
270 IPersistMoniker *persist_mon;
271 IHTMLDocument2 *doc;
272 IHTMLDocument6 *doc6;
273 IHTMLLocation *location;
274
275 hres = CreateBindCtx(0, &bc);
276 ok(hres == S_OK, "%s: CreateBindCtx failed: 0x%08x\n", test->name, hres);
277 if(FAILED(hres))
278 return;
279
280 MultiByteToWideChar(CP_ACP, 0, test->url, -1, url, sizeof(url)/sizeof(WCHAR));
281 hres = CreateURLMoniker(NULL, url, &url_mon);
282 ok(hres == S_OK, "%s: CreateURLMoniker failed: 0x%08x\n", test->name, hres);
283 if(FAILED(hres)){
284 IBindCtx_Release(bc);
285 return;
286 }
287
288 hres = CoCreateInstance(&CLSID_HTMLDocument, NULL,
289 CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER, &IID_IHTMLDocument2,
290 (void**)&doc);
291 ok(hres == S_OK, "%s: CoCreateInstance failed: 0x%08x\n", test->name, hres);
292 if(FAILED(hres)){
293 IMoniker_Release(url_mon);
294 IBindCtx_Release(bc);
295 return;
296 }
297
298 hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument6, (void**)&doc6);
299 if(hres == S_OK){
300 IHTMLDocument6_Release(doc6);
301 }else{
302 win_skip("%s: Could not get IHTMLDocument6, probably too old IE. Requires IE 8+\n", test->name);
303 IMoniker_Release(url_mon);
304 IBindCtx_Release(bc);
305 return;
306 }
307
308 hres = IHTMLDocument2_QueryInterface(doc, &IID_IPersistMoniker,
309 (void**)&persist_mon);
310 ok(hres == S_OK, "%s: IHTMlDocument2_QueryInterface failed: 0x%08x\n", test->name, hres);
311 if(FAILED(hres)){
312 IHTMLDocument2_Release(doc);
313 IMoniker_Release(url_mon);
314 IBindCtx_Release(bc);
315 return;
316 }
317
318 hres = IPersistMoniker_Load(persist_mon, FALSE, url_mon, bc,
319 STGM_SHARE_EXCLUSIVE | STGM_READWRITE);
320 ok(hres == S_OK, "%s: IPersistMoniker_Load failed: 0x%08x\n", test->name, hres);
321 if(FAILED(hres)){
322 IPersistMoniker_Release(persist_mon);
323 IHTMLDocument2_Release(doc);
324 IMoniker_Release(url_mon);
325 IBindCtx_Release(bc);
326 return;
327 }
328
329 hres = IHTMLDocument2_get_location(doc, &location);
330 ok(hres == S_OK, "%s: IHTMLDocument2_get_location failed: 0x%08x\n", test->name, hres);
331 if(FAILED(hres)){
332 IPersistMoniker_Release(persist_mon);
333 IHTMLDocument2_Release(doc);
334 IMoniker_Release(url_mon);
335 IBindCtx_Release(bc);
336 return;
337 }
338
339 test_href(location, test);
340 test_protocol(location, test);
341 test_host(location, test);
342 test_hostname(location, test);
343 test_port(location, test);
344 test_pathname(location, test);
345 test_search(location, test);
346 test_hash(location, test);
347
348 IHTMLLocation_Release(location);
349 IPersistMoniker_Release(persist_mon);
350 IHTMLDocument2_Release(doc);
351 IMoniker_Release(url_mon);
352 IBindCtx_Release(bc);
353 }
354 #endif /* ROSTESTS_81_IS_FIXED */
355
356 START_TEST(htmllocation)
357 {
358 #if ROSTESTS_81_IS_FIXED
359 int i;
360
361 CoInitialize(NULL);
362
363 for(i=0; i < sizeof(location_tests)/sizeof(*location_tests); i++)
364 perform_test(location_tests+i);
365
366 CoUninitialize();
367 #endif /* ROSTESTS_81_IS_FIXED */
368 }