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