quartz/tests: Fix interface leaks in aggregation test.
[wine] / dlls / quartz / tests / filtermapper.c
1 /*
2  * Filtermapper unit tests for Quartz
3  *
4  * Copyright (C) 2008 Alexander Dorofeyev
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 #define COBJMACROS
22
23 #include "wine/test.h"
24 #include "winbase.h"
25 #include "initguid.h"
26 #include "dshow.h"
27
28 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
29
30 /* Helper function, checks if filter with given name was enumerated. */
31 static BOOL enum_find_filter(const WCHAR *wszFilterName, IEnumMoniker *pEnum)
32 {
33     IMoniker *pMoniker = NULL;
34     BOOL found = FALSE;
35     ULONG nb;
36     HRESULT hr;
37     static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
38
39     while(!found && IEnumMoniker_Next(pEnum, 1, &pMoniker, &nb) == S_OK)
40     {
41         IPropertyBag * pPropBagCat = NULL;
42         VARIANT var;
43
44         VariantInit(&var);
45
46         hr = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID*)&pPropBagCat);
47         ok(SUCCEEDED(hr), "IMoniker_BindToStorage failed with %x\n", hr);
48         if (FAILED(hr) || !pPropBagCat)
49         {
50             VariantClear(&var);
51             IMoniker_Release(pMoniker);
52             continue;
53         }
54
55         hr = IPropertyBag_Read(pPropBagCat, wszFriendlyName, &var, NULL);
56         ok(SUCCEEDED(hr), "IPropertyBag_Read failed with %x\n", hr);
57
58         if (SUCCEEDED(hr))
59         {
60             if (!lstrcmpW((WCHAR*)V_UNION(&var, bstrVal), wszFilterName)) found = TRUE;
61         }
62
63         IPropertyBag_Release(pPropBagCat);
64         IMoniker_Release(pMoniker);
65         VariantClear(&var);
66     }
67
68     return found;
69 }
70
71 static void test_fm2_enummatchingfilters(void)
72 {
73     IFilterMapper2 *pMapper = NULL;
74     HRESULT hr;
75     REGFILTER2 rgf2;
76     REGFILTERPINS2 rgPins2[2];
77     REGPINTYPES rgPinType;
78     static const WCHAR wszFilterName1[] = {'T', 'e', 's', 't', 'f', 'i', 'l', 't', 'e', 'r', '1', 0 };
79     static const WCHAR wszFilterName2[] = {'T', 'e', 's', 't', 'f', 'i', 'l', 't', 'e', 'r', '2', 0 };
80     CLSID clsidFilter1;
81     CLSID clsidFilter2;
82     IEnumMoniker *pEnum = NULL;
83     BOOL found;
84
85     ZeroMemory(&rgf2, sizeof(rgf2));
86
87     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
88             &IID_IFilterMapper2, (LPVOID*)&pMapper);
89     ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
90     if (FAILED(hr)) goto out;
91
92     hr = CoCreateGuid(&clsidFilter1);
93     ok(hr == S_OK, "CoCreateGuid failed with %x\n", hr);
94     hr = CoCreateGuid(&clsidFilter2);
95     ok(hr == S_OK, "CoCreateGuid failed with %x\n", hr);
96
97     /* Test that a test renderer filter is returned when enumerating filters with bRender=FALSE */
98     rgf2.dwVersion = 2;
99     rgf2.dwMerit = MERIT_UNLIKELY;
100     S1(U(rgf2)).cPins2 = 1;
101     S1(U(rgf2)).rgPins2 = rgPins2;
102
103     rgPins2[0].dwFlags = REG_PINFLAG_B_RENDERER;
104     rgPins2[0].cInstances = 1;
105     rgPins2[0].nMediaTypes = 1;
106     rgPins2[0].lpMediaType = &rgPinType;
107     rgPins2[0].nMediums = 0;
108     rgPins2[0].lpMedium = NULL;
109     rgPins2[0].clsPinCategory = NULL;
110
111     rgPinType.clsMajorType = &GUID_NULL;
112     rgPinType.clsMinorType = &GUID_NULL;
113
114     hr = IFilterMapper2_RegisterFilter(pMapper, &clsidFilter1, wszFilterName1, NULL,
115                     &CLSID_LegacyAmFilterCategory, NULL, &rgf2);
116     ok(hr == S_OK, "IFilterMapper2_RegisterFilter failed with %x\n", hr);
117
118     rgPins2[0].dwFlags = 0;
119
120     rgPins2[1].dwFlags = REG_PINFLAG_B_OUTPUT;
121     rgPins2[1].cInstances = 1;
122     rgPins2[1].nMediaTypes = 1;
123     rgPins2[1].lpMediaType = &rgPinType;
124     rgPins2[1].nMediums = 0;
125     rgPins2[1].lpMedium = NULL;
126     rgPins2[1].clsPinCategory = NULL;
127
128     S1(U(rgf2)).cPins2 = 2;
129
130     hr = IFilterMapper2_RegisterFilter(pMapper, &clsidFilter2, wszFilterName2, NULL,
131                     &CLSID_LegacyAmFilterCategory, NULL, &rgf2);
132     ok(hr == S_OK, "IFilterMapper2_RegisterFilter failed with %x\n", hr);
133
134     hr = IFilterMapper2_EnumMatchingFilters(pMapper, &pEnum, 0, TRUE, MERIT_UNLIKELY, TRUE,
135                 0, NULL, NULL, &GUID_NULL, FALSE, FALSE, 0, NULL, NULL, &GUID_NULL);
136     ok(hr == S_OK, "IFilterMapper2_EnumMatchingFilters failed with %x\n", hr);
137     if (SUCCEEDED(hr) && pEnum)
138     {
139         found = enum_find_filter(wszFilterName1, pEnum);
140         ok(found, "EnumMatchingFilters failed to return the test filter 1\n");
141         found = enum_find_filter(wszFilterName2, pEnum);
142         ok(found, "EnumMatchingFilters failed to return the test filter 2\n");
143     }
144
145     if (pEnum) IEnumMoniker_Release(pEnum);
146     pEnum = NULL;
147
148     /* Non renderer must not be returned with bRender=TRUE */
149
150     hr = IFilterMapper2_EnumMatchingFilters(pMapper, &pEnum, 0, TRUE, MERIT_UNLIKELY, TRUE,
151                 0, NULL, NULL, &GUID_NULL, TRUE, FALSE, 0, NULL, NULL, &GUID_NULL);
152     ok(hr == S_OK, "IFilterMapper2_EnumMatchingFilters failed with %x\n", hr);
153
154     if (SUCCEEDED(hr) && pEnum)
155     {
156         found = enum_find_filter(wszFilterName1, pEnum);
157         ok(found, "EnumMatchingFilters failed to return the test filter 1\n");
158         found = enum_find_filter(wszFilterName2, pEnum);
159         ok(!found, "EnumMatchingFilters should not return the test filter 2\n");
160     }
161
162     hr = IFilterMapper2_UnregisterFilter(pMapper, &CLSID_LegacyAmFilterCategory, NULL,
163             &clsidFilter1);
164     ok(SUCCEEDED(hr), "IFilterMapper2_UnregisterFilter failed with %x\n", hr);
165
166     hr = IFilterMapper2_UnregisterFilter(pMapper, &CLSID_LegacyAmFilterCategory, NULL,
167             &clsidFilter2);
168     ok(SUCCEEDED(hr), "IFilterMapper2_UnregisterFilter failed with %x\n", hr);
169
170     out:
171
172     if (pEnum) IEnumMoniker_Release(pEnum);
173     if (pMapper) IFilterMapper2_Release(pMapper);
174 }
175
176 static void test_legacy_filter_registration(void)
177 {
178     IFilterMapper2 *pMapper2 = NULL;
179     IFilterMapper *pMapper = NULL;
180     HRESULT hr;
181     static const WCHAR wszFilterName[] = {'T', 'e', 's', 't', 'f', 'i', 'l', 't', 'e', 'r', 0 };
182     static const WCHAR wszPinName[] = {'P', 'i', 'n', '1', 0 };
183     CLSID clsidFilter;
184     WCHAR wszRegKey[MAX_PATH];
185     static const WCHAR wszClsid[] = {'C','L','S','I','D', 0};
186     static const WCHAR wszSlash[] = {'\\', 0};
187     LONG lRet;
188     HKEY hKey = NULL;
189     IEnumMoniker *pEnum = NULL;
190     BOOL found;
191     IEnumRegFilters *pRegEnum = NULL;
192
193     /* Test if legacy filter registration scheme works (filter is added to HKCR\Filter). IFilterMapper_RegisterFilter
194      * registers in this way. Filters so registered must then be accessible through both IFilterMapper_EnumMatchingFilters
195      * and IFilterMapper2_EnumMatchingFilters. */
196     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
197             &IID_IFilterMapper2, (LPVOID*)&pMapper2);
198     ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
199     if (FAILED(hr)) goto out;
200
201     hr = IFilterMapper2_QueryInterface(pMapper2, &IID_IFilterMapper, (LPVOID)&pMapper);
202     ok(hr == S_OK, "IFilterMapper2_QueryInterface failed with %x\n", hr);
203     if (FAILED(hr)) goto out;
204
205     /* Register a test filter. */
206     hr = CoCreateGuid(&clsidFilter);
207     ok(hr == S_OK, "CoCreateGuid failed with %x\n", hr);
208
209     lstrcpyW(wszRegKey, wszClsid);
210     lstrcatW(wszRegKey, wszSlash);
211     lRet = StringFromGUID2(&clsidFilter, wszRegKey + lstrlenW(wszRegKey), MAX_PATH - lstrlenW(wszRegKey));
212     ok(lRet > 0, "StringFromGUID2 failed\n");
213     if (!lRet) goto out;
214
215     /* Register---- functions need a filter class key to write pin and pin media type data to. Create a bogus
216      * class key for it. */
217     lRet = RegCreateKeyExW(HKEY_CLASSES_ROOT, wszRegKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
218     ok(lRet == ERROR_SUCCESS, "RegCreateKeyExW failed with %x\n", HRESULT_FROM_WIN32(lRet));
219
220     /* Set default value - this is interpreted as "friendly name" later. */
221     lRet = RegSetValueExW(hKey, NULL, 0, REG_SZ, (LPBYTE)wszFilterName, (lstrlenW(wszFilterName) + 1) * 2);
222     ok(lRet == ERROR_SUCCESS, "RegSetValueExW failed with %x\n", HRESULT_FROM_WIN32(lRet));
223
224     if (hKey) RegCloseKey(hKey);
225     hKey = NULL;
226
227     hr = IFilterMapper_RegisterFilter(pMapper, clsidFilter, wszFilterName, MERIT_UNLIKELY);
228     ok(hr == S_OK, "IFilterMapper_RegisterFilter failed with %x\n", hr);
229
230     hr = IFilterMapper_RegisterPin(pMapper, clsidFilter, wszPinName, TRUE, FALSE, FALSE, FALSE, GUID_NULL, NULL);
231     ok(hr == S_OK, "IFilterMapper_RegisterPin failed with %x\n", hr);
232
233     hr = IFilterMapper_RegisterPinType(pMapper, clsidFilter, wszPinName, GUID_NULL, GUID_NULL);
234     ok(hr == S_OK, "IFilterMapper_RegisterPinType failed with %x\n", hr);
235
236     hr = IFilterMapper2_EnumMatchingFilters(pMapper2, &pEnum, 0, TRUE, MERIT_UNLIKELY, TRUE,
237                 0, NULL, NULL, &GUID_NULL, FALSE, FALSE, 0, NULL, NULL, &GUID_NULL);
238     ok(hr == S_OK, "IFilterMapper2_EnumMatchingFilters failed with %x\n", hr);
239     if (SUCCEEDED(hr) && pEnum)
240     {
241         found = enum_find_filter(wszFilterName, pEnum);
242         ok(found, "IFilterMapper2_EnumMatchingFilters failed to return the test filter\n");
243     }
244
245     if (pEnum) IEnumMoniker_Release(pEnum);
246     pEnum = NULL;
247
248     found = FALSE;
249     hr = IFilterMapper_EnumMatchingFilters(pMapper, &pRegEnum, MERIT_UNLIKELY, TRUE, GUID_NULL, GUID_NULL,
250             FALSE, FALSE, GUID_NULL, GUID_NULL);
251     ok(hr == S_OK, "IFilterMapper_EnumMatchingFilters failed with %x\n", hr);
252     if (SUCCEEDED(hr) && pRegEnum)
253     {
254         ULONG cFetched;
255         REGFILTER *prgf;
256
257         while(!found && IEnumRegFilters_Next(pRegEnum, 1, &prgf, &cFetched) == S_OK)
258         {
259             if (!lstrcmpW(prgf->Name, wszFilterName)) found = TRUE;
260
261             CoTaskMemFree(prgf);
262         }
263
264         IEnumRegFilters_Release(pRegEnum);
265     }
266     ok(found, "IFilterMapper_EnumMatchingFilters failed to return the test filter\n");
267
268     hr = IFilterMapper_UnregisterFilter(pMapper, clsidFilter);
269     ok(hr == S_OK, "FilterMapper_UnregisterFilter failed with %x\n", hr);
270
271     lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszClsid, 0, KEY_WRITE | DELETE, &hKey);
272     ok(lRet == ERROR_SUCCESS, "RegOpenKeyExW failed with %x\n", HRESULT_FROM_WIN32(lRet));
273
274     lRet = StringFromGUID2(&clsidFilter, wszRegKey, MAX_PATH);
275     ok(lRet > 0, "StringFromGUID2 failed\n");
276
277     lRet = RegDeleteKeyW(hKey, wszRegKey);
278     ok(lRet == ERROR_SUCCESS, "RegDeleteKeyW failed with %x\n", HRESULT_FROM_WIN32(lRet));
279
280     if (hKey) RegCloseKey(hKey);
281     hKey = NULL;
282
283     out:
284
285     if (pMapper) IFilterMapper_Release(pMapper);
286     if (pMapper2) IFilterMapper2_Release(pMapper2);
287 }
288
289 static ULONG getRefcount(IUnknown *iface)
290 {
291     IUnknown_AddRef(iface);
292     return IUnknown_Release(iface);
293 }
294
295 static void test_ifiltermapper_from_filtergraph(void)
296 {
297     IFilterGraph2* pgraph2 = NULL;
298     IFilterMapper2 *pMapper2 = NULL;
299     IFilterGraph *filtergraph = NULL;
300     HRESULT hr;
301     ULONG refcount;
302
303     hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IFilterGraph2, (LPVOID*)&pgraph2);
304     ok(hr == S_OK, "CoCreateInstance failed with %08x\n", hr);
305     if (!pgraph2) goto out;
306
307     hr = IFilterGraph2_QueryInterface(pgraph2, &IID_IFilterMapper2, (LPVOID*)&pMapper2);
308     ok(hr == S_OK, "IFilterGraph2_QueryInterface failed with %08x\n", hr);
309     if (!pMapper2) goto out;
310
311     refcount = getRefcount((IUnknown*)pgraph2);
312     ok(refcount == 2, "unexpected reference count: %u\n", refcount);
313     refcount = getRefcount((IUnknown*)pMapper2);
314     ok(refcount == 2, "unexpected reference count: %u\n", refcount);
315
316     IFilterMapper2_AddRef(pMapper2);
317     refcount = getRefcount((IUnknown*)pgraph2);
318     ok(refcount == 3, "unexpected reference count: %u\n", refcount);
319     refcount = getRefcount((IUnknown*)pMapper2);
320     ok(refcount == 3, "unexpected reference count: %u\n", refcount);
321     IFilterMapper2_Release(pMapper2);
322
323     hr = IFilterMapper2_QueryInterface(pMapper2, &IID_IFilterGraph, (LPVOID*)&filtergraph);
324     ok(hr == S_OK, "IFilterMapper2_QueryInterface failed with %08x\n", hr);
325     if (!filtergraph) goto out;
326
327     IFilterMapper2_Release(pMapper2);
328     pMapper2 = NULL;
329     IFilterGraph_Release(filtergraph);
330     filtergraph = NULL;
331
332     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER, &IID_IFilterMapper2, (LPVOID*)&pMapper2);
333     ok(hr == S_OK, "CoCreateInstance failed with %08x\n", hr);
334     if (!pMapper2) goto out;
335
336     hr = IFilterMapper2_QueryInterface(pMapper2, &IID_IFilterGraph, (LPVOID*)&filtergraph);
337     ok(hr == E_NOINTERFACE, "IFilterMapper2_QueryInterface unexpected result: %08x\n", hr);
338
339     out:
340
341     if (pMapper2) IFilterMapper2_Release(pMapper2);
342     if (filtergraph) IFilterGraph_Release(filtergraph);
343     if (pgraph2) IFilterGraph2_Release(pgraph2);
344 }
345
346 START_TEST(filtermapper)
347 {
348     CoInitialize(NULL);
349
350     test_fm2_enummatchingfilters();
351     test_legacy_filter_registration();
352     test_ifiltermapper_from_filtergraph();
353
354     CoUninitialize();
355 }