jscript: Exception tests clean up.
[wine] / dlls / shdocvw / tests / intshcut.c
1 /*
2  * Unit tests to document InternetShortcut's behaviour
3  *
4  * Copyright 2008 Damjan Jovanovic
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winerror.h"
30
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"
39
40 #include "wine/test.h"
41
42 static HRESULT WINAPI Unknown_QueryInterface(IUnknown *pUnknown, REFIID riid, void **ppvObject)
43 {
44     if (IsEqualGUID(&IID_IUnknown, riid))
45     {
46         *ppvObject = pUnknown;
47         return S_OK;
48     }
49     return E_NOINTERFACE;
50 }
51
52 static ULONG WINAPI Unknown_AddRef(IUnknown *pUnknown)
53 {
54     return 2;
55 }
56
57 static ULONG WINAPI Unknown_Release(IUnknown *pUnknown)
58 {
59     return 1;
60 }
61
62 static IUnknownVtbl unknownVtbl = {
63     Unknown_QueryInterface,
64     Unknown_AddRef,
65     Unknown_Release
66 };
67
68 static IUnknown unknown = {
69     &unknownVtbl
70 };
71
72 static const char *printGUID(const GUID *guid)
73 {
74     static char guidSTR[39];
75
76     if (!guid) return NULL;
77
78     sprintf(guidSTR, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
79      guid->Data1, guid->Data2, guid->Data3,
80      guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
81      guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
82     return guidSTR;
83 }
84
85 static void test_Aggregability(void)
86 {
87     HRESULT hr;
88     IUnknown *pUnknown = NULL;
89
90     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
91     ok(SUCCEEDED(hr), "could not create instance of CLSID_InternetShortcut with IID_IUnknown, hr = 0x%x\n", hr);
92     if (pUnknown)
93         IUnknown_Release(pUnknown);
94
95     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&pUnknown);
96     ok(SUCCEEDED(hr), "could not create instance of CLSID_InternetShortcut with IID_IUniformResourceLocatorA, hr = 0x%x\n", hr);
97     if (pUnknown)
98         IUnknown_Release(pUnknown);
99
100     hr = CoCreateInstance(&CLSID_InternetShortcut, &unknown, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
101     ok(FAILED(hr), "aggregation didn't fail like it should, hr = 0x%x\n", hr);
102     if (pUnknown)
103         IUnknown_Release(pUnknown);
104 }
105
106 static void can_query_interface(IUnknown *pUnknown, REFIID riid)
107 {
108     HRESULT hr;
109     IUnknown *newInterface;
110     hr = IUnknown_QueryInterface(pUnknown, riid, (void**)&newInterface);
111     ok(SUCCEEDED(hr), "interface %s could not be queried\n", printGUID(riid));
112     if (SUCCEEDED(hr))
113         IUnknown_Release(newInterface);
114 }
115
116 static void test_QueryInterface(void)
117 {
118     HRESULT hr;
119     IUnknown *pUnknown;
120
121     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
122     if (SUCCEEDED(hr))
123     {
124         can_query_interface(pUnknown, &IID_IUniformResourceLocatorA);
125         can_query_interface(pUnknown, &IID_IUniformResourceLocatorW);
126         can_query_interface(pUnknown, &IID_IPersistFile);
127         IUnknown_Release(pUnknown);
128     }
129     else
130         skip("could not create a CLSID_InternetShortcut for QueryInterface tests, hr=0x%x\n", hr);
131 }
132
133 static CHAR *set_and_get_url(IUniformResourceLocatorA *urlA, LPCSTR input, DWORD flags)
134 {
135     HRESULT hr;
136     hr = urlA->lpVtbl->SetURL(urlA, input, flags);
137     if (SUCCEEDED(hr))
138     {
139         CHAR *output;
140         hr = urlA->lpVtbl->GetURL(urlA, &output);
141         if (SUCCEEDED(hr))
142             return output;
143         else
144             skip("GetUrl failed, hr=0x%x\n", hr);
145     }
146     else
147         skip("SetUrl (%s, 0x%x) failed, hr=0x%x\n", input, flags, hr);
148     return NULL;
149 }
150
151 static void check_string_transform(IUniformResourceLocatorA *urlA, LPCSTR input, DWORD flags, LPCSTR expectedOutput)
152 {
153     CHAR *output = set_and_get_url(urlA, input, flags);
154     if (output != NULL)
155     {
156         ok(lstrcmpA(output, expectedOutput) == 0, "unexpected URL change %s -> %s (expected %s)\n",
157             input, output, expectedOutput);
158         CoTaskMemFree(output);
159     }
160 }
161
162 static BOOL check_ie(void)
163 {
164     IHTMLDocument5 *doc;
165     HRESULT hres;
166
167     hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
168                             &IID_IHTMLDocument5, (void**)&doc);
169     if(FAILED(hres))
170         return FALSE;
171
172     IHTMLDocument5_Release(doc);
173     return TRUE;
174 }
175
176 static void test_ReadAndWriteProperties(void)
177 {
178     HRESULT hr;
179     IUniformResourceLocatorA *urlA;
180     IUniformResourceLocatorA *urlAFromFile;
181     WCHAR fileNameW[MAX_PATH];
182     static const WCHAR shortcutW[] = {'t','e','s','t','s','h','o','r','t','c','u','t','.','u','r','l',0};
183     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};
184     int iconIndex = 7;
185     char testurl[] = "http://some/bogus/url.html";
186     PROPSPEC ps[2];
187     ps[0].ulKind = PRSPEC_PROPID;
188     ps[0].propid = PID_IS_ICONFILE;
189     ps[1].ulKind = PRSPEC_PROPID;
190     ps[1].propid = PID_IS_ICONINDEX;
191
192     /* Make sure we have a valid temporary directory */
193     GetTempPathW(MAX_PATH, fileNameW);
194     lstrcatW(fileNameW, shortcutW);
195
196     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
197     if (hr == S_OK)
198     {
199         IPersistFile *pf;
200         IPropertyStorage *pPropStgWrite;
201         IPropertySetStorage *pPropSetStg;
202         PROPVARIANT pv[2];
203         pv[0].vt = VT_LPWSTR;
204         pv[0].pwszVal = (void *) iconPath;
205         pv[1].vt = VT_I4;
206         pv[1].iVal = iconIndex;
207         hr = urlA->lpVtbl->QueryInterface(urlA, &IID_IPropertySetStorage, (void **) &pPropSetStg);
208         ok(hr == S_OK, "Unable to get an IPropertySetStorage, hr=0x%x\n", hr);
209
210         hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, &pPropStgWrite);
211         ok(hr == S_OK, "Unable to get an IPropertyStorage for writing, hr=0x%x\n", hr);
212
213         hr = IPropertyStorage_WriteMultiple(pPropStgWrite, 2, ps, pv, 0);
214         ok(hr == S_OK, "Unable to set properties, hr=0x%x\n", hr);
215
216         hr = IPropertyStorage_Commit(pPropStgWrite, STGC_DEFAULT);
217         ok(hr == S_OK, "Failed to commit properties, hr=0x%x\n", hr);
218
219         pPropStgWrite->lpVtbl->Release(pPropStgWrite);
220
221         /* We need to set a URL -- IPersistFile refuses to save without one. */
222         hr = urlA->lpVtbl->SetURL(urlA, testurl, 0);
223         ok(hr == S_OK, "Failed to set a URL.  hr=0x%x\n", hr);
224
225         /* Write this shortcut out to a file so that we can test reading it in again. */
226         hr = urlA->lpVtbl->QueryInterface(urlA, &IID_IPersistFile, (void **) &pf);
227         ok(hr == S_OK, "Failed to get the IPersistFile for writing.  hr=0x%x\n", hr);
228
229         hr = IPersistFile_Save(pf, fileNameW, TRUE);
230         ok(hr == S_OK, "Failed to save via IPersistFile. hr=0x%x\n", hr);
231
232         IPersistFile_Release(pf);
233         urlA->lpVtbl->Release(urlA);
234         IPropertySetStorage_Release(pPropSetStg);
235     }
236     else
237         skip("could not create a CLSID_InternetShortcut for property tests, hr=0x%x\n", hr);
238
239     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlAFromFile);
240     if (hr == S_OK)
241     {
242         IPropertySetStorage *pPropSetStg;
243         IPropertyStorage *pPropStgRead;
244         PROPVARIANT pvread[2];
245         IPersistFile *pf;
246         LPSTR url = NULL;
247
248         /* Now read that .url file back in. */
249         hr = urlAFromFile->lpVtbl->QueryInterface(urlAFromFile, &IID_IPersistFile, (void **) &pf);
250         ok(hr == S_OK, "Failed to get the IPersistFile for reading.  hr=0x%x\n", hr);
251
252         hr = IPersistFile_Load(pf, fileNameW, 0);
253         ok(hr == S_OK, "Failed to load via IPersistFile. hr=0x%x\n", hr);
254         IPersistFile_Release(pf);
255
256
257         hr = urlAFromFile->lpVtbl->GetURL(urlAFromFile, &url);
258         ok(lstrcmp(url, testurl) == 0, "Wrong url read from file: %s\n",url);
259
260
261         hr = urlAFromFile->lpVtbl->QueryInterface(urlAFromFile, &IID_IPropertySetStorage, (void **) &pPropSetStg);
262         ok(hr == S_OK, "Unable to get an IPropertySetStorage, hr=0x%x\n", hr);
263
264         hr = IPropertySetStorage_Open(pPropSetStg, &FMTID_Intshcut, STGM_READ | STGM_SHARE_EXCLUSIVE, &pPropStgRead);
265         ok(hr == S_OK, "Unable to get an IPropertyStorage for reading, hr=0x%x\n", hr);
266
267         hr = IPropertyStorage_ReadMultiple(pPropStgRead, 2, ps, pvread);
268         ok(hr == S_OK, "Unable to read properties, hr=0x%x\n", hr);
269
270         ok(pvread[1].iVal == iconIndex, "Read wrong icon index: %d\n", pvread[1].iVal);
271
272         ok(lstrcmpW(pvread[0].pwszVal, iconPath) == 0, "Wrong icon path read: %s\n",wine_dbgstr_w(pvread[0].pwszVal));
273
274         PropVariantClear(&pvread[0]);
275         PropVariantClear(&pvread[1]);
276         IPropertyStorage_Release(pPropStgRead);
277         IPropertySetStorage_Release(pPropSetStg);
278         urlAFromFile->lpVtbl->Release(urlAFromFile);
279         DeleteFileW(fileNameW);
280     }
281     else
282         skip("could not create a CLSID_InternetShortcut for property tests, hr=0x%x\n", hr);
283 }
284
285 static void test_NullURLs(void)
286 {
287     HRESULT hr;
288     IUniformResourceLocatorA *urlA;
289
290     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
291     if (SUCCEEDED(hr))
292     {
293         LPSTR url = NULL;
294
295         hr = urlA->lpVtbl->GetURL(urlA, &url);
296         ok(SUCCEEDED(hr), "getting uninitialized URL unexpectedly failed, hr=0x%x\n", hr);
297         ok(url == NULL, "uninitialized URL is not NULL but %s\n", url);
298
299         hr = urlA->lpVtbl->SetURL(urlA, NULL, 0);
300         ok(SUCCEEDED(hr), "setting NULL URL unexpectedly failed, hr=0x%x\n", hr);
301
302         hr = urlA->lpVtbl->GetURL(urlA, &url);
303         ok(SUCCEEDED(hr), "getting NULL URL unexpectedly failed, hr=0x%x\n", hr);
304         ok(url == NULL, "URL unexpectedly not NULL but %s\n", url);
305
306         urlA->lpVtbl->Release(urlA);
307     }
308     else
309         skip("could not create a CLSID_InternetShortcut for NullURL tests, hr=0x%x\n", hr);
310 }
311
312 static void test_SetURLFlags(void)
313 {
314     HRESULT hr;
315     IUniformResourceLocatorA *urlA;
316
317     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
318     if (SUCCEEDED(hr))
319     {
320         check_string_transform(urlA, "somerandomstring", 0, "somerandomstring");
321         check_string_transform(urlA, "www.winehq.org", 0, "www.winehq.org");
322
323         todo_wine
324         {
325             check_string_transform(urlA, "www.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "http://www.winehq.org/");
326             check_string_transform(urlA, "ftp.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "ftp://ftp.winehq.org/");
327         }
328
329         urlA->lpVtbl->Release(urlA);
330     }
331     else
332         skip("could not create a CLSID_InternetShortcut for SetUrl tests, hr=0x%x\n", hr);
333 }
334
335 static void test_InternetShortcut(void)
336 {
337     if (check_ie())
338     {
339         test_Aggregability();
340         test_QueryInterface();
341         test_NullURLs();
342         test_SetURLFlags();
343         test_ReadAndWriteProperties();
344     }
345 }
346
347 START_TEST(intshcut)
348 {
349     OleInitialize(NULL);
350     test_InternetShortcut();
351     OleUninitialize();
352 }