1 /* Unit test suite for various shell Association objects
3 * Copyright 2012 Detlef Riekenberg
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/test.h"
31 static void test_IQueryAssociations_QueryInterface(void)
33 IQueryAssociations
*qa
;
34 IQueryAssociations
*qa2
;
38 hr
= CoCreateInstance(&CLSID_QueryAssociations
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IQueryAssociations
, (void*)&qa
);
39 ok(hr
== S_OK
, "got 0x%08lx\n", hr
);
41 hr
= IQueryAssociations_QueryInterface(qa
, &IID_IQueryAssociations
, (void**)&qa2
);
42 ok(hr
== S_OK
, "QueryInterface (IQueryAssociations) returned 0x%lx\n", hr
);
44 IQueryAssociations_Release(qa2
);
47 hr
= IQueryAssociations_QueryInterface(qa
, &IID_IUnknown
, (void**)&unk
);
48 ok(hr
== S_OK
, "QueryInterface (IUnknown) returned 0x%lx\n", hr
);
50 IUnknown_Release(unk
);
53 hr
= IQueryAssociations_QueryInterface(qa
, &IID_IUnknown
, NULL
);
54 ok(hr
== E_POINTER
, "got 0x%lx (expected E_POINTER)\n", hr
);
56 IQueryAssociations_Release(qa
);
60 static void test_IApplicationAssociationRegistration_QueryInterface(IApplicationAssociationRegistration
*appreg
)
62 IApplicationAssociationRegistration
*appreg2
;
66 hr
= IApplicationAssociationRegistration_QueryInterface(appreg
, &IID_IApplicationAssociationRegistration
,
68 ok(hr
== S_OK
, "QueryInterface (IApplicationAssociationRegistration) returned 0x%lx\n", hr
);
70 IApplicationAssociationRegistration_Release(appreg2
);
73 hr
= IApplicationAssociationRegistration_QueryInterface(appreg
, &IID_IUnknown
, (void**)&unk
);
74 ok(hr
== S_OK
, "QueryInterface (IUnknown) returned 0x%lx\n", hr
);
76 IUnknown_Release(unk
);
79 hr
= IApplicationAssociationRegistration_QueryInterface(appreg
, &IID_IUnknown
, NULL
);
80 ok(hr
== E_POINTER
, "got 0x%lx (expected E_POINTER)\n", hr
);
83 struct assoc_getstring_test
93 static const WCHAR httpW
[] = {'h','t','t','p',0};
94 static const WCHAR badW
[] = {'b','a','d','b','a','d',0};
96 static struct assoc_getstring_test getstring_tests
[] =
98 { httpW
, 0, ASSOCSTR_EXECUTABLE
, 2, 0x8007007a /* E_NOT_SUFFICIENT_BUFFER */, S_OK
},
99 { httpW
, ASSOCF_NOTRUNCATE
, ASSOCSTR_EXECUTABLE
, 2, E_POINTER
},
103 static void getstring_test(LPCWSTR assocName
, HKEY progIdKey
, ASSOCSTR str
, LPCWSTR expected_string
, int line
)
105 IQueryAssociations
*assoc
;
110 hr
= CoCreateInstance(&CLSID_QueryAssociations
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IQueryAssociations
, (void*)&assoc
);
111 ok_(__FILE__
, line
)(hr
== S_OK
, "failed to create IQueryAssociations, 0x%lx\n", hr
);
112 hr
= IQueryAssociations_Init(assoc
, ASSOCF_NONE
, assocName
, progIdKey
, NULL
);
113 ok_(__FILE__
, line
)(hr
== S_OK
, "IQueryAssociations::Init failed, 0x%lx\n", hr
);
115 hr
= IQueryAssociations_GetString(assoc
, ASSOCF_NONE
, str
, NULL
, NULL
, &len
);
116 if (expected_string
) {
117 ok_(__FILE__
, line
)(hr
== S_FALSE
, "GetString returned 0x%lx, expected S_FALSE\n", hr
);
119 /* don't try to allocate memory using uninitialized len */
120 IQueryAssociations_Release(assoc
);
124 buffer
= malloc(len
* sizeof(WCHAR
));
125 ok_(__FILE__
, line
)(buffer
!= NULL
, "out of memory\n");
126 hr
= IQueryAssociations_GetString(assoc
, 0, str
, NULL
, buffer
, &len
);
127 ok_(__FILE__
, line
)(hr
== S_OK
, "GetString returned 0x%lx, expected S_OK\n", hr
);
129 ok_(__FILE__
, line
)(lstrcmpW(buffer
, expected_string
) == 0, "GetString returned %s, expected %s\n",
130 wine_dbgstr_w(buffer
), wine_dbgstr_w(expected_string
));
133 ok_(__FILE__
, line
)(FAILED(hr
), "GetString returned 0x%lx, expected failure\n", hr
);
136 IQueryAssociations_Release(assoc
);
139 static void test_IQueryAssociations_GetString(void)
141 static WCHAR test_extensionW
[] = {'.','t','e','s','t',0};
142 static WCHAR test_progidW
[] = {'t','e','s','t','f','i','l','e',0};
143 static WCHAR DefaultIconW
[] = {'D','e','f','a','u','l','t','I','c','o','n',0};
144 /* folder.ico, why not */
145 static WCHAR test_iconW
[] = {'s','h','e','l','l','3','2','.','d','l','l',',','1',0};
146 HKEY test_extension_key
;
147 HKEY test_progid_key
;
148 HKEY test_defaulticon_key
;
151 struct assoc_getstring_test
*ptr
= getstring_tests
;
152 IQueryAssociations
*assoc
;
157 r
= RegCreateKeyExW(HKEY_CLASSES_ROOT
, test_extensionW
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &test_extension_key
, NULL
);
158 if (r
== ERROR_ACCESS_DENIED
)
160 win_skip("Not enough permissions to create a test key.\n");
164 ok(r
== ERROR_SUCCESS
, "RegCreateKeyExW(HKCR, \".test\") failed: 0x%Ix\n", r
);
165 r
= RegSetValueExW(test_extension_key
, NULL
, 0, REG_SZ
, (PBYTE
)test_progidW
, sizeof(test_progidW
));
166 ok(r
== ERROR_SUCCESS
, "RegSetValueExW(HKCR\\.test, NULL, \"testfile\") failed: 0x%Ix\n", r
);
168 /* adding progid key with no information should fail to return information */
169 r
= RegCreateKeyExW(HKEY_CLASSES_ROOT
, test_progidW
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &test_progid_key
, NULL
);
170 ok(r
== ERROR_SUCCESS
, "RegCreateKeyExW(HKCR, \"testfile\") failed: 0x%Ix\n", r
);
171 getstring_test(test_extensionW
, NULL
, ASSOCSTR_DEFAULTICON
, NULL
, __LINE__
);
172 getstring_test(test_progidW
, NULL
, ASSOCSTR_DEFAULTICON
, NULL
, __LINE__
);
173 getstring_test(NULL
, test_progid_key
, ASSOCSTR_DEFAULTICON
, NULL
, __LINE__
);
175 /* adding information to the progid should return that information */
176 r
= RegCreateKeyExW(test_progid_key
, DefaultIconW
, 0, NULL
, 0, KEY_ALL_ACCESS
, NULL
, &test_defaulticon_key
, NULL
);
177 ok(r
== ERROR_SUCCESS
, "RegCreateKeyExW(HKCR\\testfile\\DefaultIcon) failed: 0x%Ix\n", r
);
178 r
= RegSetValueExW(test_defaulticon_key
, NULL
, 0, REG_SZ
, (PBYTE
)test_iconW
, sizeof(test_iconW
));
179 ok(r
== ERROR_SUCCESS
, "RegSetValueExW(HKCR\\testfile\\DefaultIcon, NULL, \"folder.ico\") failed: 0x%Ix\n", r
);
180 getstring_test(test_extensionW
, NULL
, ASSOCSTR_DEFAULTICON
, test_iconW
, __LINE__
);
181 getstring_test(test_progidW
, NULL
, ASSOCSTR_DEFAULTICON
, test_iconW
, __LINE__
);
182 getstring_test(NULL
, test_progid_key
, ASSOCSTR_DEFAULTICON
, test_iconW
, __LINE__
);
184 RegDeleteKeyW(test_progid_key
, DefaultIconW
);
185 RegDeleteKeyW(HKEY_CLASSES_ROOT
, test_progidW
);
186 RegDeleteKeyW(HKEY_CLASSES_ROOT
, test_extensionW
);
188 hr
= CoCreateInstance(&CLSID_QueryAssociations
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IQueryAssociations
, (void*)&assoc
);
189 ok(hr
== S_OK
, "failed to create object, 0x%lx\n", hr
);
191 hr
= IQueryAssociations_Init(assoc
, ASSOCF_NONE
, httpW
, NULL
, NULL
);
192 ok(hr
== S_OK
, "Init failed, 0x%lx\n", hr
);
195 hr
= IQueryAssociations_GetString(assoc
, ASSOCF_NONE
, ASSOCSTR_EXECUTABLE
, NULL
, NULL
, &len
);
196 ok(hr
== S_FALSE
, "got 0x%08lx\n", hr
);
197 ok(len
> 0, "got wrong needed length, %ld\n", len
);
201 WCHAR buffW
[MAX_PATH
];
204 hr
= IQueryAssociations_Init(assoc
, ASSOCF_NONE
, ptr
->key
, NULL
, NULL
);
205 ok(hr
== S_OK
, "%d: Init failed, 0x%lx\n", i
, hr
);
208 buffW
[0] = ptr
->flags
& ASSOCF_NOTRUNCATE
? 0x1 : 0;
209 hr
= IQueryAssociations_GetString(assoc
, ptr
->flags
, ptr
->str
, NULL
, buffW
, &len
);
211 ok(broken(hr
== ptr
->brokenhr
), "%d: GetString failed, 0x%08lx\n", i
, hr
);
214 ok(hr
== ptr
->hr
, "%d: GetString failed, 0x%08lx\n", i
, hr
);
215 ok(len
> ptr
->len
, "%d: got needed length %ld\n", i
, len
);
218 /* even with ASSOCF_NOTRUNCATE it's null terminated */
219 if ((ptr
->flags
& ASSOCF_NOTRUNCATE
) && (ptr
->len
> 0))
220 ok(buffW
[0] == 0 || broken(buffW
[0] == 0x1) /* pre win7 */, "%d: got %x\n", i
, buffW
[0]);
222 if (!(ptr
->flags
& ASSOCF_NOTRUNCATE
) && ptr
->len
&& FAILED(ptr
->hr
))
223 ok(buffW
[0] != 0, "%d: got %x\n", i
, buffW
[0]);
229 IQueryAssociations_Release(assoc
);
232 static void test_IQueryAssociations_Init(void)
234 IQueryAssociations
*assoc
;
238 hr
= CoCreateInstance(&CLSID_QueryAssociations
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IQueryAssociations
, (void*)&assoc
);
239 ok(hr
== S_OK
, "failed to create object, 0x%lx\n", hr
);
241 hr
= IQueryAssociations_Init(assoc
, ASSOCF_NONE
, NULL
, NULL
, NULL
);
242 ok(hr
== E_INVALIDARG
, "Init failed, 0x%08lx\n", hr
);
244 hr
= IQueryAssociations_Init(assoc
, ASSOCF_NONE
, httpW
, NULL
, NULL
);
245 ok(hr
== S_OK
, "Init failed, 0x%08lx\n", hr
);
247 hr
= IQueryAssociations_Init(assoc
, ASSOCF_NONE
, badW
, NULL
, NULL
);
248 ok(hr
== S_OK
|| broken(hr
== S_FALSE
) /* pre-vista */, "Init failed, 0x%08lx\n", hr
);
251 hr
= IQueryAssociations_GetString(assoc
, ASSOCF_NONE
, ASSOCSTR_EXECUTABLE
, NULL
, NULL
, &len
);
252 ok(hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
) || broken(hr
== E_FAIL
) /* pre-vista */, "got 0x%08lx\n", hr
);
254 IQueryAssociations_Release(assoc
);
257 static void test_IApplicationAssociationRegistration_QueryCurrentDefault(IApplicationAssociationRegistration
*appreg
)
259 static const WCHAR emptyW
[] = {0};
260 static const WCHAR txtW
[] = {'.','t','x','t',0};
261 static const WCHAR spacetxtW
[] = {' ','.','t','x','t',0};
263 LPWSTR assocprog
= NULL
;
265 hr
= IApplicationAssociationRegistration_QueryCurrentDefault(appreg
, emptyW
, AT_URLPROTOCOL
, AL_EFFECTIVE
, &assocprog
);
266 ok(hr
== E_INVALIDARG
, "got 0x%lx\n", hr
);
268 hr
= IApplicationAssociationRegistration_QueryCurrentDefault(appreg
, emptyW
, AT_FILEEXTENSION
, AL_EFFECTIVE
, &assocprog
);
269 ok(hr
== E_INVALIDARG
, "got 0x%lx\n", hr
);
271 hr
= IApplicationAssociationRegistration_QueryCurrentDefault(appreg
, spacetxtW
, AT_FILEEXTENSION
, AL_EFFECTIVE
, &assocprog
);
272 ok(hr
== E_INVALIDARG
|| hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
) /*Win8*/, "got 0x%lx\n", hr
);
274 hr
= IApplicationAssociationRegistration_QueryCurrentDefault(appreg
, httpW
, AT_URLPROTOCOL
, AL_EFFECTIVE
, NULL
);
275 ok(hr
== E_INVALIDARG
, "got 0x%lx\n", hr
);
277 /* AT_FILEEXTENSION must start with a period */
278 hr
= IApplicationAssociationRegistration_QueryCurrentDefault(appreg
, txtW
, AT_FILEEXTENSION
, AL_EFFECTIVE
, &assocprog
);
279 ok(hr
== S_OK
, "got 0x%lx\n", hr
);
280 trace("%s\n", wine_dbgstr_w(assocprog
));
281 CoTaskMemFree(assocprog
);
283 hr
= IApplicationAssociationRegistration_QueryCurrentDefault(appreg
, emptyW
, AT_STARTMENUCLIENT
, AL_EFFECTIVE
, &assocprog
);
284 ok(hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
), "got 0x%lx\n", hr
);
286 hr
= IApplicationAssociationRegistration_QueryCurrentDefault(appreg
, emptyW
, AT_MIMETYPE
, AL_EFFECTIVE
, &assocprog
);
287 ok(hr
== HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION
), "got 0x%lx\n", hr
);
289 hr
= IApplicationAssociationRegistration_QueryCurrentDefault(appreg
, httpW
, AT_URLPROTOCOL
, AL_EFFECTIVE
, &assocprog
);
290 ok(hr
== S_OK
, "got 0x%lx\n", hr
);
291 trace("%s\n", wine_dbgstr_w(assocprog
));
293 CoTaskMemFree(assocprog
);
298 IQueryAssociations
*qa
;
299 IApplicationAssociationRegistration
*appreg
= NULL
;
304 /* this works since XP */
305 hr
= CoCreateInstance(&CLSID_QueryAssociations
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IQueryAssociations
, (void*)&qa
);
308 test_IQueryAssociations_QueryInterface();
309 test_IQueryAssociations_GetString();
310 test_IQueryAssociations_Init();
312 IQueryAssociations_Release(qa
);
315 win_skip("IQueryAssociations not supported, 0x%lx\n", hr
);
317 /* this works since Vista */
318 hr
= CoCreateInstance(&CLSID_ApplicationAssociationRegistration
, NULL
, CLSCTX_INPROC_SERVER
,
319 &IID_IApplicationAssociationRegistration
, (LPVOID
*)&appreg
);
322 test_IApplicationAssociationRegistration_QueryInterface(appreg
);
323 test_IApplicationAssociationRegistration_QueryCurrentDefault(appreg
);
325 IApplicationAssociationRegistration_Release(appreg
);
328 win_skip("IApplicationAssociationRegistration not supported: 0x%lx\n", hr
);