d2d1/tests: Fix stroke style object leak (Valgrind).
[wine.git] / dlls / ieframe / tests / intshcut.c
blob0959a61880714dc4efae32fc551102483d548564
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
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winerror.h"
31 #include "shlobj.h"
32 #include "shobjidl.h"
33 #include "shlguid.h"
34 #include "ole2.h"
35 #include "mshtml.h"
36 #include "initguid.h"
37 #include "isguids.h"
38 #include "intshcut.h"
40 #include "wine/test.h"
42 static HRESULT WINAPI Unknown_QueryInterface(IUnknown *pUnknown, REFIID riid, void **ppvObject)
44 if (IsEqualGUID(&IID_IUnknown, riid))
46 *ppvObject = pUnknown;
47 return S_OK;
49 return E_NOINTERFACE;
52 static ULONG WINAPI Unknown_AddRef(IUnknown *pUnknown)
54 return 2;
57 static ULONG WINAPI Unknown_Release(IUnknown *pUnknown)
59 return 1;
62 static IUnknownVtbl unknownVtbl = {
63 Unknown_QueryInterface,
64 Unknown_AddRef,
65 Unknown_Release
68 static IUnknown unknown = {
69 &unknownVtbl
72 static void test_Aggregability(void)
74 HRESULT hr;
75 IUnknown *pUnknown = NULL;
77 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
78 ok(hr == S_OK, "could not create instance of CLSID_InternetShortcut with IID_IUnknown, hr = 0x%x\n", hr);
79 if (pUnknown)
80 IUnknown_Release(pUnknown);
82 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&pUnknown);
83 ok(hr == S_OK, "could not create instance of CLSID_InternetShortcut with IID_IUniformResourceLocatorA, hr = 0x%x\n", hr);
84 if (pUnknown)
85 IUnknown_Release(pUnknown);
87 hr = CoCreateInstance(&CLSID_InternetShortcut, &unknown, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
88 ok(hr == CLASS_E_NOAGGREGATION, "aggregation didn't fail like it should, hr = 0x%x\n", hr);
89 if (pUnknown)
90 IUnknown_Release(pUnknown);
93 static void can_query_interface(IUnknown *pUnknown, REFIID riid)
95 HRESULT hr;
96 IUnknown *newInterface;
97 hr = IUnknown_QueryInterface(pUnknown, riid, (void**)&newInterface);
98 ok(hr == S_OK, "interface %s could not be queried\n", wine_dbgstr_guid(riid));
99 if (SUCCEEDED(hr))
100 IUnknown_Release(newInterface);
103 static void test_QueryInterface(void)
105 HRESULT hr;
106 IUnknown *pUnknown;
108 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
109 ok(hr == S_OK, "Could not create InternetShortcut object: %08x\n", hr);
111 can_query_interface(pUnknown, &IID_IUniformResourceLocatorA);
112 can_query_interface(pUnknown, &IID_IUniformResourceLocatorW);
113 can_query_interface(pUnknown, &IID_IPersistFile);
114 IUnknown_Release(pUnknown);
117 #define test_shortcut_url(a,b) _test_shortcut_url(__LINE__,a,b)
118 static void _test_shortcut_url(unsigned line, IUnknown *unk, const char *exurl)
120 IUniformResourceLocatorA *locator_a;
121 char *url_a;
122 HRESULT hres;
124 hres = IUnknown_QueryInterface(unk, &IID_IUniformResourceLocatorA, (void**)&locator_a);
125 ok_(__FILE__,line)(hres == S_OK, "Could not get IUniformResourceLocatorA iface: %08x\n", hres);
127 hres = locator_a->lpVtbl->GetURL(locator_a, &url_a);
128 ok_(__FILE__,line)(hres == S_OK, "GetURL failed: %08x\n", hres);
129 ok_(__FILE__,line)(!strcmp(url_a, exurl), "unexpected URL, got %s, expected %s\n", url_a, exurl);
130 CoTaskMemFree(url_a);
132 IUnknown_Release((IUnknown*)locator_a);
135 #define check_string_transform(a,b,c,d,e) _check_string_transform(__LINE__,a,b,c,d,e)
136 static void _check_string_transform(unsigned line, IUniformResourceLocatorA *urlA, LPCSTR input, DWORD flags,
137 LPCSTR expectedOutput, BOOL is_todo)
139 CHAR *output;
140 HRESULT hr;
142 hr = urlA->lpVtbl->SetURL(urlA, input, flags);
143 ok_(__FILE__,line)(hr == S_OK, "SetUrl failed, hr=0x%x\n", hr);
144 if (FAILED(hr))
145 return;
147 output = (void*)0xdeadbeef;
148 hr = urlA->lpVtbl->GetURL(urlA, &output);
149 if(expectedOutput) {
150 todo_wine_if(is_todo) {
151 ok_(__FILE__,line)(hr == S_OK, "GetUrl failed, hr=0x%x\n", hr);
153 todo_wine
154 ok_(__FILE__,line)(!lstrcmpA(output, expectedOutput), "unexpected URL change %s -> %s (expected %s)\n",
155 input, output, expectedOutput);
156 }else {
157 todo_wine
158 ok_(__FILE__,line)(hr == S_FALSE, "GetUrl failed, hr=0x%x\n", hr);
159 todo_wine
160 ok_(__FILE__,line)(!output, "GetUrl returned %s\n", output);
162 if (hr == S_OK)
163 CoTaskMemFree(output);
166 static void test_ReadAndWriteProperties(void)
168 int iconIndex = 7;
169 PROPSPEC ps[2];
170 HRESULT hr;
171 IUniformResourceLocatorA *urlA;
172 IUniformResourceLocatorA *urlAFromFile;
173 WCHAR fileNameW[MAX_PATH];
175 static const WCHAR shortcutW[] = {'t','e','s','t','s','h','o','r','t','c','u','t','.','u','r','l',0};
176 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};
177 char testurl[] = "http://some/bogus/url.html";
179 ps[0].ulKind = PRSPEC_PROPID;
180 U(ps[0]).propid = PID_IS_ICONFILE;
181 ps[1].ulKind = PRSPEC_PROPID;
182 U(ps[1]).propid = PID_IS_ICONINDEX;
184 /* Make sure we have a valid temporary directory */
185 GetTempPathW(MAX_PATH, fileNameW);
186 lstrcatW(fileNameW, shortcutW);
188 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
189 ok(hr == S_OK, "Could not create CLSID_InternetShortcut instance: %08x\n", hr);
190 if (hr == S_OK)
192 IPersistFile *pf;
193 IPropertyStorage *pPropStgWrite;
194 IPropertySetStorage *pPropSetStg;
195 PROPVARIANT pv[2];
197 /* We need to set a URL -- IPersistFile refuses to save without one. */
198 hr = urlA->lpVtbl->SetURL(urlA, testurl, 0);
199 ok(hr == S_OK, "Failed to set a URL. hr=0x%x\n", hr);
201 /* Write this shortcut out to a file so that we can test reading it in again. */
202 hr = urlA->lpVtbl->QueryInterface(urlA, &IID_IPersistFile, (void **) &pf);
203 ok(hr == S_OK, "Failed to get the IPersistFile for writing. hr=0x%x\n", hr);
205 hr = IPersistFile_Save(pf, fileNameW, TRUE);
206 ok(hr == S_OK, "Failed to save via IPersistFile. hr=0x%x\n", hr);
208 IPersistFile_Release(pf);
210 pv[0].vt = VT_LPWSTR;
211 U(pv[0]).pwszVal = (void *) iconPath;
212 pv[1].vt = VT_I4;
213 U(pv[1]).lVal = iconIndex;
214 hr = urlA->lpVtbl->QueryInterface(urlA, &IID_IPropertySetStorage, (void **) &pPropSetStg);
215 ok(hr == S_OK, "Unable to get an IPropertySetStorage, hr=0x%x\n", hr);
217 hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, &pPropStgWrite);
218 ok(hr == S_OK, "Unable to get an IPropertyStorage for writing, hr=0x%x\n", hr);
220 hr = IPropertyStorage_WriteMultiple(pPropStgWrite, 2, ps, pv, 0);
221 ok(hr == S_OK, "Unable to set properties, hr=0x%x\n", hr);
223 hr = IPropertyStorage_Commit(pPropStgWrite, STGC_DEFAULT);
224 ok(hr == S_OK, "Failed to commit properties, hr=0x%x\n", hr);
226 pPropStgWrite->lpVtbl->Release(pPropStgWrite);
227 urlA->lpVtbl->Release(urlA);
228 IPropertySetStorage_Release(pPropSetStg);
231 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlAFromFile);
232 ok(hr == S_OK, "Could not create CLSID_InternetShortcut instance: %08x\n", hr);
233 if (hr == S_OK)
235 IPropertySetStorage *pPropSetStg;
236 IPropertyStorage *pPropStgRead;
237 PROPVARIANT pvread[2];
238 IPersistFile *pf;
239 LPSTR url = NULL;
241 /* Now read that .url file back in. */
242 hr = urlAFromFile->lpVtbl->QueryInterface(urlAFromFile, &IID_IPersistFile, (void **) &pf);
243 ok(hr == S_OK, "Failed to get the IPersistFile for reading. hr=0x%x\n", hr);
245 hr = IPersistFile_Load(pf, fileNameW, 0);
246 ok(hr == S_OK, "Failed to load via IPersistFile. hr=0x%x\n", hr);
247 IPersistFile_Release(pf);
250 hr = urlAFromFile->lpVtbl->GetURL(urlAFromFile, &url);
251 ok(hr == S_OK, "Unable to get url from file, hr=0x%x\n", hr);
252 ok(lstrcmpA(url, testurl) == 0, "Wrong url read from file: %s\n",url);
253 CoTaskMemFree(url);
255 hr = urlAFromFile->lpVtbl->QueryInterface(urlAFromFile, &IID_IPropertySetStorage, (void **) &pPropSetStg);
256 ok(hr == S_OK, "Unable to get an IPropertySetStorage, hr=0x%x\n", hr);
258 hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStgRead);
259 ok(hr == S_OK, "Unable to get an IPropertyStorage for reading, hr=0x%x\n", hr);
261 memset(pvread, 0, sizeof(pvread));
262 hr = IPropertyStorage_ReadMultiple(pPropStgRead, 2, ps, pvread);
263 todo_wine /* Wine doesn't yet support setting properties after save */
265 ok(hr == S_OK, "Unable to read properties, hr=0x%x\n", hr);
266 ok(pvread[1].vt == VT_I4, "got %d\n", pvread[1].vt);
267 ok(U(pvread[1]).lVal == iconIndex, "Read wrong icon index: %d\n", U(pvread[1]).iVal);
268 ok(pvread[0].vt == VT_LPWSTR, "got %d\n", pvread[0].vt);
269 ok(lstrcmpW(U(pvread[0]).pwszVal, iconPath) == 0, "Wrong icon path read: %s\n", wine_dbgstr_w(U(pvread[0]).pwszVal));
271 PropVariantClear(&pvread[0]);
272 PropVariantClear(&pvread[1]);
273 IPropertyStorage_Release(pPropStgRead);
274 IPropertySetStorage_Release(pPropSetStg);
275 urlAFromFile->lpVtbl->Release(urlAFromFile);
276 DeleteFileW(fileNameW);
280 static void test_NullURLs(void)
282 LPSTR url = NULL;
283 HRESULT hr;
284 IUniformResourceLocatorA *urlA;
286 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
287 ok(hr == S_OK, "Could not create InternetShortcut object: %08x\n", hr);
289 hr = urlA->lpVtbl->GetURL(urlA, &url);
290 ok(hr == S_FALSE, "getting uninitialized URL unexpectedly failed, hr=0x%x\n", hr);
291 ok(url == NULL, "uninitialized URL is not NULL but %s\n", url);
293 hr = urlA->lpVtbl->SetURL(urlA, NULL, 0);
294 ok(hr == S_OK, "setting NULL URL unexpectedly failed, hr=0x%x\n", hr);
296 hr = urlA->lpVtbl->GetURL(urlA, &url);
297 ok(hr == S_FALSE, "getting NULL URL unexpectedly failed, hr=0x%x\n", hr);
298 ok(url == NULL, "URL unexpectedly not NULL but %s\n", url);
300 urlA->lpVtbl->Release(urlA);
303 typedef struct {
304 const char *data;
305 const char *url;
306 } load_test_t;
308 static const load_test_t load_tests[] = {
309 {"[InternetShortcut]\n"
310 "URL=http://www.winehq.org/\n"
311 "HotKey=0\n"
312 "IDList=\n"
313 "[{000214A0-0000-0000-C000-000000000046}]\n"
314 "Prop0=1,2\n",
316 "http://www.winehq.org/"
320 static void test_Load(void)
322 IPersistFile *persist_file;
323 const load_test_t *test;
324 WCHAR file_path[MAX_PATH];
325 DWORD size;
326 HANDLE file;
327 HRESULT hres;
329 static const WCHAR test_urlW[] = {'t','e','s','t','.','u','r','l',0};
331 GetTempPathW(MAX_PATH, file_path);
332 lstrcatW(file_path, test_urlW);
334 for(test = load_tests; test < load_tests + sizeof(load_tests)/sizeof(*load_tests); test++) {
335 IPropertySetStorage *propsetstorage;
336 IPropertyStorage *propstorage;
337 PROPVARIANT v;
338 PROPSPEC ps;
340 file = CreateFileW(file_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
341 ok(file != INVALID_HANDLE_VALUE, "could not create test file\n");
342 if(file == INVALID_HANDLE_VALUE)
343 continue;
345 WriteFile(file, test->data, strlen(test->data), &size, NULL);
346 CloseHandle(file);
348 hres = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IPersistFile, (void**)&persist_file);
349 ok(hres == S_OK, "Could not create InternetShortcut instance: %08x\n", hres);
351 hres = IPersistFile_Load(persist_file, file_path, 0);
352 ok(hres == S_OK, "Load failed: %08x\n", hres);
354 test_shortcut_url((IUnknown*)persist_file, test->url);
356 hres = IPersistFile_QueryInterface(persist_file, &IID_IPropertySetStorage, (void **)&propsetstorage);
357 ok(hres == S_OK, "Unable to get an IPropertySetStorage, hr=0x%x\n", hres);
359 hres = IPropertySetStorage_Open(propsetstorage, &FMTID_Intshcut, STGM_READ | STGM_SHARE_EXCLUSIVE, &propstorage);
360 ok(hres == S_OK, "Unable to get an IPropertyStorage for reading, hr=0x%x\n", hres);
362 ps.ulKind = PRSPEC_PROPID;
363 U(ps).propid = PID_IS_ICONFILE;
364 v.vt = VT_NULL;
365 hres = IPropertyStorage_ReadMultiple(propstorage, 1, &ps, &v);
366 ok(hres == S_FALSE, "got 0x%08x\n", hres);
367 ok(v.vt == VT_EMPTY, "got %d\n", v.vt);
369 ps.ulKind = PRSPEC_PROPID;
370 U(ps).propid = PID_IS_ICONINDEX;
371 v.vt = VT_EMPTY;
372 hres = IPropertyStorage_ReadMultiple(propstorage, 1, &ps, &v);
373 ok(hres == S_FALSE, "got 0x%08x\n", hres);
374 ok(v.vt == VT_EMPTY, "got %d\n", v.vt);
376 IPropertyStorage_Release(propstorage);
377 IPropertySetStorage_Release(propsetstorage);
379 IPersistFile_Release(persist_file);
380 DeleteFileW(file_path);
384 static void test_SetURLFlags(void)
386 HRESULT hr;
387 IUniformResourceLocatorA *urlA;
389 hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
390 ok(hr == S_OK, "Could not create InternetShortcut object: %08x\n", hr);
392 check_string_transform(urlA, "somerandomstring", 0, NULL, TRUE);
393 check_string_transform(urlA, "www.winehq.org", 0, NULL, TRUE);
395 check_string_transform(urlA, "www.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "http://www.winehq.org/", FALSE);
396 check_string_transform(urlA, "ftp.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "ftp://ftp.winehq.org/", FALSE);
398 urlA->lpVtbl->Release(urlA);
401 static void test_InternetShortcut(void)
403 IUniformResourceLocatorA *url;
404 HRESULT hres;
406 hres = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&url);
407 ok(hres == S_OK, "Could not create CLSID_InternetShortcut instance: %08x\n", hres);
408 if(FAILED(hres))
409 return;
410 url->lpVtbl->Release(url);
412 test_Aggregability();
413 test_QueryInterface();
414 test_NullURLs();
415 test_SetURLFlags();
416 test_ReadAndWriteProperties();
417 test_Load();
420 START_TEST(intshcut)
422 OleInitialize(NULL);
424 test_InternetShortcut();
426 OleUninitialize();