ieframe: Moved WebBrowser object tests to ieframe.
[wine/multimedia.git] / dlls / shdocvw / tests / intshcut.c
blob205419b301f9a456f833c1a8ccae8eb1002e69d6
1 /*
2 * Unit tests to document InternetShortcut's behaviour
4 * Copyright 2008 Damjan Jovanovic
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winerror.h"
32 #include "initguid.h"
33 #include "shlobj.h"
34 #include "shobjidl.h"
35 #include "shlguid.h"
36 #include "ole2.h"
37 #include "mshtml.h"
38 #include "initguid.h"
39 #include "isguids.h"
40 #include "intshcut.h"
42 #include "wine/test.h"
44 static HRESULT WINAPI Unknown_QueryInterface(IUnknown *pUnknown, REFIID riid, void **ppvObject)
46 if (IsEqualGUID(&IID_IUnknown, riid))
48 *ppvObject = pUnknown;
49 return S_OK;
51 return E_NOINTERFACE;
54 static ULONG WINAPI Unknown_AddRef(IUnknown *pUnknown)
56 return 2;
59 static ULONG WINAPI Unknown_Release(IUnknown *pUnknown)
61 return 1;
64 static IUnknownVtbl unknownVtbl = {
65 Unknown_QueryInterface,
66 Unknown_AddRef,
67 Unknown_Release
70 static IUnknown unknown = {
71 &unknownVtbl
74 static const char *printGUID(const GUID *guid)
76 static char guidSTR[39];
78 if (!guid) return NULL;
80 sprintf(guidSTR, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
81 guid->Data1, guid->Data2, guid->Data3,
82 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
83 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
84 return guidSTR;
87 static void test_Aggregability(void)
89 HRESULT hr;
90 IUnknown *pUnknown = NULL;
92 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
93 ok(hr == S_OK, "could not create instance of CLSID_InternetShortcut with IID_IUnknown, hr = 0x%x\n", hr);
94 if (pUnknown)
95 IUnknown_Release(pUnknown);
97 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&pUnknown);
98 ok(hr == S_OK, "could not create instance of CLSID_InternetShortcut with IID_IUniformResourceLocatorA, hr = 0x%x\n", hr);
99 if (pUnknown)
100 IUnknown_Release(pUnknown);
102 hr = CoCreateInstance(&CLSID_InternetShortcut, &unknown, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
103 ok(hr == CLASS_E_NOAGGREGATION, "aggregation didn't fail like it should, hr = 0x%x\n", hr);
104 if (pUnknown)
105 IUnknown_Release(pUnknown);
108 static void can_query_interface(IUnknown *pUnknown, REFIID riid)
110 HRESULT hr;
111 IUnknown *newInterface;
112 hr = IUnknown_QueryInterface(pUnknown, riid, (void**)&newInterface);
113 ok(hr == S_OK, "interface %s could not be queried\n", printGUID(riid));
114 if (SUCCEEDED(hr))
115 IUnknown_Release(newInterface);
118 static void test_QueryInterface(void)
120 HRESULT hr;
121 IUnknown *pUnknown;
123 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
124 ok(hr == S_OK, "Could not create InternetShortcut object: %08x\n", hr);
126 can_query_interface(pUnknown, &IID_IUniformResourceLocatorA);
127 can_query_interface(pUnknown, &IID_IUniformResourceLocatorW);
128 can_query_interface(pUnknown, &IID_IPersistFile);
129 IUnknown_Release(pUnknown);
132 #define test_shortcut_url(a,b) _test_shortcut_url(__LINE__,a,b)
133 static void _test_shortcut_url(unsigned line, IUnknown *unk, const char *exurl)
135 IUniformResourceLocatorA *locator_a;
136 char *url_a;
137 HRESULT hres;
139 hres = IUnknown_QueryInterface(unk, &IID_IUniformResourceLocatorA, (void**)&locator_a);
140 ok_(__FILE__,line)(hres == S_OK, "Could not get IUniformResourceLocatorA iface: %08x\n", hres);
142 hres = locator_a->lpVtbl->GetURL(locator_a, &url_a);
143 ok_(__FILE__,line)(hres == S_OK, "GetURL failed: %08x\n", hres);
144 ok_(__FILE__,line)(!strcmp(url_a, exurl), "unexpected URL, got %s, expected %s\n", url_a, exurl);
145 CoTaskMemFree(url_a);
147 IUnknown_Release(locator_a);
150 #define check_string_transform(a,b,c,d,e) _check_string_transform(__LINE__,a,b,c,d,e)
151 static void _check_string_transform(unsigned line, IUniformResourceLocatorA *urlA, LPCSTR input, DWORD flags,
152 LPCSTR expectedOutput, BOOL is_todo)
154 CHAR *output;
155 HRESULT hr;
157 hr = urlA->lpVtbl->SetURL(urlA, input, flags);
158 ok_(__FILE__,line)(hr == S_OK, "SetUrl failed, hr=0x%x\n", hr);
159 if (FAILED(hr))
160 return;
162 output = (void*)0xdeadbeef;
163 hr = urlA->lpVtbl->GetURL(urlA, &output);
164 if(expectedOutput) {
165 if(is_todo) {
166 todo_wine
167 ok_(__FILE__,line)(hr == S_OK, "GetUrl failed, hr=0x%x\n", hr);
168 }else {
169 ok_(__FILE__,line)(hr == S_OK, "GetUrl failed, hr=0x%x\n", hr);
171 todo_wine
172 ok_(__FILE__,line)(!lstrcmpA(output, expectedOutput), "unexpected URL change %s -> %s (expected %s)\n",
173 input, output, expectedOutput);
174 CoTaskMemFree(output);
175 }else {
176 todo_wine
177 ok_(__FILE__,line)(hr == S_FALSE, "GetUrl failed, hr=0x%x\n", hr);
178 todo_wine
179 ok_(__FILE__,line)(!output, "GetUrl returned %s\n", output);
183 static BOOL check_ie(void)
185 IHTMLDocument5 *doc;
186 HRESULT hres;
188 hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
189 &IID_IHTMLDocument5, (void**)&doc);
190 if(FAILED(hres))
191 return FALSE;
193 IHTMLDocument5_Release(doc);
194 return TRUE;
197 static void test_ReadAndWriteProperties(void)
199 int iconIndex = 7;
200 PROPSPEC ps[2];
201 HRESULT hr;
202 IUniformResourceLocatorA *urlA;
203 IUniformResourceLocatorA *urlAFromFile;
204 WCHAR fileNameW[MAX_PATH];
206 static const WCHAR shortcutW[] = {'t','e','s','t','s','h','o','r','t','c','u','t','.','u','r','l',0};
207 WCHAR iconPath[] = {'f','i','l','e',':','/','/','/','C',':','/','a','r','b','i','t','r','a','r','y','/','i','c','o','n','/','p','a','t','h',0};
208 char testurl[] = "http://some/bogus/url.html";
210 ps[0].ulKind = PRSPEC_PROPID;
211 U(ps[0]).propid = PID_IS_ICONFILE;
212 ps[1].ulKind = PRSPEC_PROPID;
213 U(ps[1]).propid = PID_IS_ICONINDEX;
215 /* Make sure we have a valid temporary directory */
216 GetTempPathW(MAX_PATH, fileNameW);
217 lstrcatW(fileNameW, shortcutW);
219 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
220 if (hr == S_OK)
222 IPersistFile *pf;
223 IPropertyStorage *pPropStgWrite;
224 IPropertySetStorage *pPropSetStg;
225 PROPVARIANT pv[2];
227 /* We need to set a URL -- IPersistFile refuses to save without one. */
228 hr = urlA->lpVtbl->SetURL(urlA, testurl, 0);
229 ok(hr == S_OK, "Failed to set a URL. hr=0x%x\n", hr);
231 /* Write this shortcut out to a file so that we can test reading it in again. */
232 hr = urlA->lpVtbl->QueryInterface(urlA, &IID_IPersistFile, (void **) &pf);
233 ok(hr == S_OK, "Failed to get the IPersistFile for writing. hr=0x%x\n", hr);
235 hr = IPersistFile_Save(pf, fileNameW, TRUE);
236 ok(hr == S_OK, "Failed to save via IPersistFile. hr=0x%x\n", hr);
238 IPersistFile_Release(pf);
240 pv[0].vt = VT_LPWSTR;
241 U(pv[0]).pwszVal = (void *) iconPath;
242 pv[1].vt = VT_I4;
243 U(pv[1]).iVal = iconIndex;
244 hr = urlA->lpVtbl->QueryInterface(urlA, &IID_IPropertySetStorage, (void **) &pPropSetStg);
245 ok(hr == S_OK, "Unable to get an IPropertySetStorage, hr=0x%x\n", hr);
247 hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, &pPropStgWrite);
248 ok(hr == S_OK, "Unable to get an IPropertyStorage for writing, hr=0x%x\n", hr);
250 hr = IPropertyStorage_WriteMultiple(pPropStgWrite, 2, ps, pv, 0);
251 ok(hr == S_OK, "Unable to set properties, hr=0x%x\n", hr);
253 hr = IPropertyStorage_Commit(pPropStgWrite, STGC_DEFAULT);
254 ok(hr == S_OK, "Failed to commit properties, hr=0x%x\n", hr);
256 pPropStgWrite->lpVtbl->Release(pPropStgWrite);
257 urlA->lpVtbl->Release(urlA);
258 IPropertySetStorage_Release(pPropSetStg);
260 else
261 skip("could not create a CLSID_InternetShortcut for property tests, hr=0x%x\n", hr);
263 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlAFromFile);
264 if (hr == S_OK)
266 IPropertySetStorage *pPropSetStg;
267 IPropertyStorage *pPropStgRead;
268 PROPVARIANT pvread[2];
269 IPersistFile *pf;
270 LPSTR url = NULL;
272 /* Now read that .url file back in. */
273 hr = urlAFromFile->lpVtbl->QueryInterface(urlAFromFile, &IID_IPersistFile, (void **) &pf);
274 ok(hr == S_OK, "Failed to get the IPersistFile for reading. hr=0x%x\n", hr);
276 hr = IPersistFile_Load(pf, fileNameW, 0);
277 ok(hr == S_OK, "Failed to load via IPersistFile. hr=0x%x\n", hr);
278 IPersistFile_Release(pf);
281 hr = urlAFromFile->lpVtbl->GetURL(urlAFromFile, &url);
282 ok(hr == S_OK, "Unable to get url from file, hr=0x%x\n", hr);
283 ok(lstrcmp(url, testurl) == 0, "Wrong url read from file: %s\n",url);
286 hr = urlAFromFile->lpVtbl->QueryInterface(urlAFromFile, &IID_IPropertySetStorage, (void **) &pPropSetStg);
287 ok(hr == S_OK, "Unable to get an IPropertySetStorage, hr=0x%x\n", hr);
289 hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStgRead);
290 ok(hr == S_OK, "Unable to get an IPropertyStorage for reading, hr=0x%x\n", hr);
292 hr = IPropertyStorage_ReadMultiple(pPropStgRead, 2, ps, pvread);
293 ok(hr == S_OK, "Unable to read properties, hr=0x%x\n", hr);
295 todo_wine /* Wine doesn't yet support setting properties after save */
297 ok(U(pvread[1]).iVal == iconIndex, "Read wrong icon index: %d\n", U(pvread[1]).iVal);
299 ok(lstrcmpW(U(pvread[0]).pwszVal, iconPath) == 0, "Wrong icon path read: %s\n", wine_dbgstr_w(U(pvread[0]).pwszVal));
302 PropVariantClear(&pvread[0]);
303 PropVariantClear(&pvread[1]);
304 IPropertyStorage_Release(pPropStgRead);
305 IPropertySetStorage_Release(pPropSetStg);
306 urlAFromFile->lpVtbl->Release(urlAFromFile);
307 DeleteFileW(fileNameW);
309 else
310 skip("could not create a CLSID_InternetShortcut for property tests, hr=0x%x\n", hr);
313 static void test_NullURLs(void)
315 LPSTR url = NULL;
316 HRESULT hr;
317 IUniformResourceLocatorA *urlA;
319 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
320 ok(hr == S_OK, "Could not create InternetShortcut object: %08x\n", hr);
322 hr = urlA->lpVtbl->GetURL(urlA, &url);
323 ok(hr == S_FALSE, "getting uninitialized URL unexpectedly failed, hr=0x%x\n", hr);
324 ok(url == NULL, "uninitialized URL is not NULL but %s\n", url);
326 hr = urlA->lpVtbl->SetURL(urlA, NULL, 0);
327 ok(hr == S_OK, "setting NULL URL unexpectedly failed, hr=0x%x\n", hr);
329 hr = urlA->lpVtbl->GetURL(urlA, &url);
330 ok(hr == S_FALSE, "getting NULL URL unexpectedly failed, hr=0x%x\n", hr);
331 ok(url == NULL, "URL unexpectedly not NULL but %s\n", url);
333 urlA->lpVtbl->Release(urlA);
336 typedef struct {
337 const char *data;
338 const char *url;
339 } load_test_t;
341 static const load_test_t load_tests[] = {
342 {"[InternetShortcut]\n"
343 "URL=http://www.winehq.org/\n"
344 "HotKey=0\n"
345 "IDList=\n"
346 "[{000214A0-0000-0000-C000-000000000046}]\n"
347 "Prop0=1,2\n",
349 "http://www.winehq.org/"
353 static void test_Load(void)
355 IPersistFile *persist_file;
356 const load_test_t *test;
357 WCHAR file_path[MAX_PATH];
358 DWORD size;
359 HANDLE file;
360 HRESULT hres;
362 static const WCHAR test_urlW[] = {'t','e','s','t','.','u','r','l',0};
364 GetTempPathW(MAX_PATH, file_path);
365 lstrcatW(file_path, test_urlW);
367 for(test = load_tests; test < load_tests + sizeof(load_tests)/sizeof(*load_tests); test++) {
368 file = CreateFileW(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
369 ok(file != INVALID_HANDLE_VALUE, "could not create test file\n");
370 if(file == INVALID_HANDLE_VALUE)
371 continue;
373 WriteFile(file, test->data, strlen(test->data), &size, NULL);
374 CloseHandle(file);
376 hres = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IPersistFile, (void**)&persist_file);
377 ok(hres == S_OK, "Could not create InternetShortcut instance: %08x\n", hres);
379 hres = IPersistFile_Load(persist_file, file_path, 0);
380 ok(hres == S_OK, "Load failed: %08x\n", hres);
382 test_shortcut_url((IUnknown*)persist_file, test->url);
384 IPersistFile_Release(persist_file);
385 DeleteFileW(file_path);
389 static void test_SetURLFlags(void)
391 HRESULT hr;
392 IUniformResourceLocatorA *urlA;
394 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
395 ok(hr == S_OK, "Could not create InternetShortcut object: %08x\n", hr);
397 check_string_transform(urlA, "somerandomstring", 0, NULL, TRUE);
398 check_string_transform(urlA, "www.winehq.org", 0, NULL, TRUE);
400 check_string_transform(urlA, "www.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "http://www.winehq.org/", FALSE);
401 check_string_transform(urlA, "ftp.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "ftp://ftp.winehq.org/", FALSE);
403 urlA->lpVtbl->Release(urlA);
406 static void test_InternetShortcut(void)
408 IUniformResourceLocatorA *url;
409 HRESULT hres;
411 hres = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&url);
412 if(FAILED(hres)) {
413 win_skip("Could not create CLSID_InternetShortcut instance: %08x\n", hres);
414 return;
417 test_Aggregability();
418 test_QueryInterface();
419 test_NullURLs();
420 test_SetURLFlags();
421 test_ReadAndWriteProperties();
422 test_Load();
425 START_TEST(intshcut)
427 OleInitialize(NULL);
429 if(check_ie())
430 test_InternetShortcut();
431 else
432 win_skip("Too old IE\n");
434 OleUninitialize();