shdocvw: Add IServiceProvider support.
[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 "initguid.h"
36 #include "isguids.h"
37 #include "intshcut.h"
38
39 #include "wine/test.h"
40
41 static HRESULT WINAPI Unknown_QueryInterface(IUnknown *pUnknown, REFIID riid, void **ppvObject)
42 {
43     if (IsEqualGUID(&IID_IUnknown, riid))
44     {
45         *ppvObject = pUnknown;
46         return S_OK;
47     }
48     return E_NOINTERFACE;
49 }
50
51 static ULONG WINAPI Unknown_AddRef(IUnknown *pUnknown)
52 {
53     return 2;
54 }
55
56 static ULONG WINAPI Unknown_Release(IUnknown *pUnknown)
57 {
58     return 1;
59 }
60
61 static IUnknownVtbl unknownVtbl = {
62     Unknown_QueryInterface,
63     Unknown_AddRef,
64     Unknown_Release
65 };
66
67 static IUnknown unknown = {
68     &unknownVtbl
69 };
70
71 static const char *printGUID(const GUID *guid)
72 {
73     static char guidSTR[39];
74
75     if (!guid) return NULL;
76
77     sprintf(guidSTR, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
78      guid->Data1, guid->Data2, guid->Data3,
79      guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
80      guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
81     return guidSTR;
82 }
83
84 static void test_Aggregability(void)
85 {
86     HRESULT hr;
87     IUnknown *pUnknown = NULL;
88
89     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
90     ok(SUCCEEDED(hr), "could not create instance of CLSID_InternetShortcut with IID_IUnknown, hr = 0x%x\n", hr);
91     if (pUnknown)
92         IUnknown_Release(pUnknown);
93
94     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&pUnknown);
95     ok(SUCCEEDED(hr), "could not create instance of CLSID_InternetShortcut with IID_IUniformResourceLocatorA, hr = 0x%x\n", hr);
96     if (pUnknown)
97         IUnknown_Release(pUnknown);
98
99     hr = CoCreateInstance(&CLSID_InternetShortcut, &unknown, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
100     ok(FAILED(hr), "aggregation didn't fail like it should, hr = 0x%x\n", hr);
101     if (pUnknown)
102         IUnknown_Release(pUnknown);
103 }
104
105 static void can_query_interface(IUnknown *pUnknown, REFIID riid)
106 {
107     HRESULT hr;
108     IUnknown *newInterface;
109     hr = IUnknown_QueryInterface(pUnknown, riid, (void**)&newInterface);
110     ok(SUCCEEDED(hr), "interface %s could not be queried\n", printGUID(riid));
111     if (SUCCEEDED(hr))
112         IUnknown_Release(newInterface);
113 }
114
115 static void test_QueryInterface(void)
116 {
117     HRESULT hr;
118     IUnknown *pUnknown;
119
120     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUnknown, (void**)&pUnknown);
121     if (SUCCEEDED(hr))
122     {
123         can_query_interface(pUnknown, &IID_IUniformResourceLocatorA);
124         can_query_interface(pUnknown, &IID_IUniformResourceLocatorW);
125         can_query_interface(pUnknown, &IID_IPersistFile);
126         IUnknown_Release(pUnknown);
127     }
128     else
129         skip("could not create a CLSID_InternetShortcut for QueryInterface tests, hr=0x%x\n", hr);
130 }
131
132 static CHAR *set_and_get_url(IUniformResourceLocatorA *urlA, LPCSTR input, DWORD flags)
133 {
134     HRESULT hr;
135     hr = urlA->lpVtbl->SetURL(urlA, input, flags);
136     if (SUCCEEDED(hr))
137     {
138         CHAR *output;
139         hr = urlA->lpVtbl->GetURL(urlA, &output);
140         if (SUCCEEDED(hr))
141             return output;
142         else
143             skip("GetUrl failed, hr=0x%x\n", hr);
144     }
145     else
146         skip("SetUrl (%s, 0x%x) failed, hr=0x%x\n", input, flags, hr);
147     return NULL;
148 }
149
150 static void check_string_transform(IUniformResourceLocatorA *urlA, LPCSTR input, DWORD flags, LPCSTR expectedOutput)
151 {
152     CHAR *output = set_and_get_url(urlA, input, flags);
153     if (output != NULL)
154     {
155         ok(lstrcmpA(output, expectedOutput) == 0, "unexpected URL change %s -> %s (expected %s)\n",
156             input, output, expectedOutput);
157         CoTaskMemFree(output);
158     }
159 }
160
161 static void test_NullURLs(void)
162 {
163     HRESULT hr;
164     IUniformResourceLocatorA *urlA;
165
166     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
167     if (SUCCEEDED(hr))
168     {
169         LPSTR url = NULL;
170
171         hr = urlA->lpVtbl->GetURL(urlA, &url);
172         ok(SUCCEEDED(hr), "getting uninitialized URL unexpectedly failed, hr=0x%x\n", hr);
173         ok(url == NULL, "uninitialized URL is not NULL but %s\n", url);
174
175         hr = urlA->lpVtbl->SetURL(urlA, NULL, 0);
176         ok(SUCCEEDED(hr), "setting NULL URL unexpectedly failed, hr=0x%x\n", hr);
177
178         hr = urlA->lpVtbl->GetURL(urlA, &url);
179         ok(SUCCEEDED(hr), "getting NULL URL unexpectedly failed, hr=0x%x\n", hr);
180         ok(url == NULL, "URL unexpectedly not NULL but %s\n", url);
181
182         urlA->lpVtbl->Release(urlA);
183     }
184     else
185         skip("could not create a CLSID_InternetShortcut for NullURL tests, hr=0x%x\n", hr);
186 }
187
188 static void test_SetURLFlags(void)
189 {
190     HRESULT hr;
191     IUniformResourceLocatorA *urlA;
192
193     hr = CoCreateInstance(&CLSID_InternetShortcut, NULL, CLSCTX_ALL, &IID_IUniformResourceLocatorA, (void**)&urlA);
194     if (SUCCEEDED(hr))
195     {
196         check_string_transform(urlA, "somerandomstring", 0, "somerandomstring");
197         check_string_transform(urlA, "www.winehq.org", 0, "www.winehq.org");
198
199         todo_wine
200         {
201             check_string_transform(urlA, "www.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "http://www.winehq.org/");
202             check_string_transform(urlA, "ftp.winehq.org", IURL_SETURL_FL_GUESS_PROTOCOL, "ftp://ftp.winehq.org/");
203         }
204
205         urlA->lpVtbl->Release(urlA);
206     }
207     else
208         skip("could not create a CLSID_InternetShortcut for SetUrl tests, hr=0x%x\n", hr);
209 }
210
211 static void test_InternetShortcut(void)
212 {
213     test_Aggregability();
214     test_QueryInterface();
215     test_NullURLs();
216     test_SetURLFlags();
217 }
218
219 START_TEST(intshcut)
220 {
221     OleInitialize(NULL);
222     test_InternetShortcut();
223     OleUninitialize();
224 }