quartz/tests: Fix test failures on Win9x by using A-functions.
[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             CHAR val1[512], val2[512];
61
62             WideCharToMultiByte(CP_ACP, 0, (WCHAR*)V_UNION(&var, bstrVal), -1, val1, sizeof(val1), 0, 0);
63             WideCharToMultiByte(CP_ACP, 0, wszFilterName, -1, val2, sizeof(val2), 0, 0);
64             if (!lstrcmpA(val1, val2)) found = TRUE;
65         }
66
67         IPropertyBag_Release(pPropBagCat);
68         IMoniker_Release(pMoniker);
69         VariantClear(&var);
70     }
71
72     return found;
73 }
74
75 static void test_fm2_enummatchingfilters(void)
76 {
77     IFilterMapper2 *pMapper = NULL;
78     HRESULT hr;
79     REGFILTER2 rgf2;
80     REGFILTERPINS2 rgPins2[2];
81     REGPINTYPES rgPinType;
82     static const WCHAR wszFilterName1[] = {'T', 'e', 's', 't', 'f', 'i', 'l', 't', 'e', 'r', '1', 0 };
83     static const WCHAR wszFilterName2[] = {'T', 'e', 's', 't', 'f', 'i', 'l', 't', 'e', 'r', '2', 0 };
84     CLSID clsidFilter1;
85     CLSID clsidFilter2;
86     IEnumMoniker *pEnum = NULL;
87     BOOL found;
88
89     ZeroMemory(&rgf2, sizeof(rgf2));
90
91     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
92             &IID_IFilterMapper2, (LPVOID*)&pMapper);
93     ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
94     if (FAILED(hr)) goto out;
95
96     hr = CoCreateGuid(&clsidFilter1);
97     ok(hr == S_OK, "CoCreateGuid failed with %x\n", hr);
98     hr = CoCreateGuid(&clsidFilter2);
99     ok(hr == S_OK, "CoCreateGuid failed with %x\n", hr);
100
101     /* Test that a test renderer filter is returned when enumerating filters with bRender=FALSE */
102     rgf2.dwVersion = 2;
103     rgf2.dwMerit = MERIT_UNLIKELY;
104     S1(U(rgf2)).cPins2 = 1;
105     S1(U(rgf2)).rgPins2 = rgPins2;
106
107     rgPins2[0].dwFlags = REG_PINFLAG_B_RENDERER;
108     rgPins2[0].cInstances = 1;
109     rgPins2[0].nMediaTypes = 1;
110     rgPins2[0].lpMediaType = &rgPinType;
111     rgPins2[0].nMediums = 0;
112     rgPins2[0].lpMedium = NULL;
113     rgPins2[0].clsPinCategory = NULL;
114
115     rgPinType.clsMajorType = &GUID_NULL;
116     rgPinType.clsMinorType = &GUID_NULL;
117
118     hr = IFilterMapper2_RegisterFilter(pMapper, &clsidFilter1, wszFilterName1, NULL,
119                     &CLSID_LegacyAmFilterCategory, NULL, &rgf2);
120     ok(hr == S_OK, "IFilterMapper2_RegisterFilter failed with %x\n", hr);
121
122     rgPins2[0].dwFlags = 0;
123
124     rgPins2[1].dwFlags = REG_PINFLAG_B_OUTPUT;
125     rgPins2[1].cInstances = 1;
126     rgPins2[1].nMediaTypes = 1;
127     rgPins2[1].lpMediaType = &rgPinType;
128     rgPins2[1].nMediums = 0;
129     rgPins2[1].lpMedium = NULL;
130     rgPins2[1].clsPinCategory = NULL;
131
132     S1(U(rgf2)).cPins2 = 2;
133
134     hr = IFilterMapper2_RegisterFilter(pMapper, &clsidFilter2, wszFilterName2, NULL,
135                     &CLSID_LegacyAmFilterCategory, NULL, &rgf2);
136     ok(hr == S_OK, "IFilterMapper2_RegisterFilter failed with %x\n", hr);
137
138     hr = IFilterMapper2_EnumMatchingFilters(pMapper, &pEnum, 0, TRUE, MERIT_UNLIKELY, TRUE,
139                 0, NULL, NULL, &GUID_NULL, FALSE, FALSE, 0, NULL, NULL, &GUID_NULL);
140     ok(hr == S_OK, "IFilterMapper2_EnumMatchingFilters failed with %x\n", hr);
141     if (SUCCEEDED(hr) && pEnum)
142     {
143         found = enum_find_filter(wszFilterName1, pEnum);
144         ok(found, "EnumMatchingFilters failed to return the test filter 1\n");
145         found = enum_find_filter(wszFilterName2, pEnum);
146         ok(found, "EnumMatchingFilters failed to return the test filter 2\n");
147     }
148
149     if (pEnum) IEnumMoniker_Release(pEnum);
150     pEnum = NULL;
151
152     /* Non renderer must not be returned with bRender=TRUE */
153
154     hr = IFilterMapper2_EnumMatchingFilters(pMapper, &pEnum, 0, TRUE, MERIT_UNLIKELY, TRUE,
155                 0, NULL, NULL, &GUID_NULL, TRUE, FALSE, 0, NULL, NULL, &GUID_NULL);
156     ok(hr == S_OK, "IFilterMapper2_EnumMatchingFilters failed with %x\n", hr);
157
158     if (SUCCEEDED(hr) && pEnum)
159     {
160         found = enum_find_filter(wszFilterName1, pEnum);
161         ok(found, "EnumMatchingFilters failed to return the test filter 1\n");
162         found = enum_find_filter(wszFilterName2, pEnum);
163         ok(!found, "EnumMatchingFilters should not return the test filter 2\n");
164     }
165
166     hr = IFilterMapper2_UnregisterFilter(pMapper, &CLSID_LegacyAmFilterCategory, NULL,
167             &clsidFilter1);
168     ok(SUCCEEDED(hr), "IFilterMapper2_UnregisterFilter failed with %x\n", hr);
169
170     hr = IFilterMapper2_UnregisterFilter(pMapper, &CLSID_LegacyAmFilterCategory, NULL,
171             &clsidFilter2);
172     ok(SUCCEEDED(hr), "IFilterMapper2_UnregisterFilter failed with %x\n", hr);
173
174     out:
175
176     if (pEnum) IEnumMoniker_Release(pEnum);
177     if (pMapper) IFilterMapper2_Release(pMapper);
178 }
179
180 static void test_legacy_filter_registration(void)
181 {
182     IFilterMapper2 *pMapper2 = NULL;
183     IFilterMapper *pMapper = NULL;
184     HRESULT hr;
185     static const WCHAR wszFilterName[] = {'T', 'e', 's', 't', 'f', 'i', 'l', 't', 'e', 'r', 0 };
186     static const CHAR szFilterName[] = "Testfilter";
187     static const WCHAR wszPinName[] = {'P', 'i', 'n', '1', 0 };
188     CLSID clsidFilter;
189     WCHAR wszRegKey[MAX_PATH];
190     CHAR szRegKey[MAX_PATH];
191     static const WCHAR wszClsid[] = {'C','L','S','I','D', 0};
192     static const CHAR szClsid[] = "CLSID";
193     static const WCHAR wszSlash[] = {'\\', 0};
194     LONG lRet;
195     HKEY hKey = NULL;
196     IEnumMoniker *pEnum = NULL;
197     BOOL found;
198     IEnumRegFilters *pRegEnum = NULL;
199
200     /* Test if legacy filter registration scheme works (filter is added to HKCR\Filter). IFilterMapper_RegisterFilter
201      * registers in this way. Filters so registered must then be accessible through both IFilterMapper_EnumMatchingFilters
202      * and IFilterMapper2_EnumMatchingFilters. */
203     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
204             &IID_IFilterMapper2, (LPVOID*)&pMapper2);
205     ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
206     if (FAILED(hr)) goto out;
207
208     hr = IFilterMapper2_QueryInterface(pMapper2, &IID_IFilterMapper, (LPVOID)&pMapper);
209     ok(hr == S_OK, "IFilterMapper2_QueryInterface failed with %x\n", hr);
210     if (FAILED(hr)) goto out;
211
212     /* Register a test filter. */
213     hr = CoCreateGuid(&clsidFilter);
214     ok(hr == S_OK, "CoCreateGuid failed with %x\n", hr);
215
216     lstrcpyW(wszRegKey, wszClsid);
217     lstrcatW(wszRegKey, wszSlash);
218     lRet = StringFromGUID2(&clsidFilter, wszRegKey + lstrlenW(wszRegKey), MAX_PATH - lstrlenW(wszRegKey));
219     ok(lRet > 0, "StringFromGUID2 failed\n");
220     if (!lRet) goto out;
221     WideCharToMultiByte(CP_ACP, 0, wszRegKey, -1, szRegKey, sizeof(szRegKey), 0, 0);
222
223     /* Register---- functions need a filter class key to write pin and pin media type data to. Create a bogus
224      * class key for it. */
225     lRet = RegCreateKeyExA(HKEY_CLASSES_ROOT, szRegKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
226     ok(lRet == ERROR_SUCCESS, "RegCreateKeyExA failed with %x\n", HRESULT_FROM_WIN32(lRet));
227
228     /* Set default value - this is interpreted as "friendly name" later. */
229     lRet = RegSetValueExA(hKey, NULL, 0, REG_SZ, (LPBYTE)szFilterName, lstrlenA(szFilterName) + 1);
230     ok(lRet == ERROR_SUCCESS, "RegSetValueExA failed with %x\n", HRESULT_FROM_WIN32(lRet));
231
232     if (hKey) RegCloseKey(hKey);
233     hKey = NULL;
234
235     hr = IFilterMapper_RegisterFilter(pMapper, clsidFilter, wszFilterName, MERIT_UNLIKELY);
236     ok(hr == S_OK, "IFilterMapper_RegisterFilter failed with %x\n", hr);
237
238     hr = IFilterMapper_RegisterPin(pMapper, clsidFilter, wszPinName, TRUE, FALSE, FALSE, FALSE, GUID_NULL, NULL);
239     ok(hr == S_OK, "IFilterMapper_RegisterPin failed with %x\n", hr);
240
241     hr = IFilterMapper_RegisterPinType(pMapper, clsidFilter, wszPinName, GUID_NULL, GUID_NULL);
242     ok(hr == S_OK, "IFilterMapper_RegisterPinType failed with %x\n", hr);
243
244     hr = IFilterMapper2_EnumMatchingFilters(pMapper2, &pEnum, 0, TRUE, MERIT_UNLIKELY, TRUE,
245                 0, NULL, NULL, &GUID_NULL, FALSE, FALSE, 0, NULL, NULL, &GUID_NULL);
246     ok(hr == S_OK, "IFilterMapper2_EnumMatchingFilters failed with %x\n", hr);
247     if (SUCCEEDED(hr) && pEnum)
248     {
249         found = enum_find_filter(wszFilterName, pEnum);
250         ok(found, "IFilterMapper2_EnumMatchingFilters failed to return the test filter\n");
251     }
252
253     if (pEnum) IEnumMoniker_Release(pEnum);
254     pEnum = NULL;
255
256     found = FALSE;
257     hr = IFilterMapper_EnumMatchingFilters(pMapper, &pRegEnum, MERIT_UNLIKELY, TRUE, GUID_NULL, GUID_NULL,
258             FALSE, FALSE, GUID_NULL, GUID_NULL);
259     ok(hr == S_OK, "IFilterMapper_EnumMatchingFilters failed with %x\n", hr);
260     if (SUCCEEDED(hr) && pRegEnum)
261     {
262         ULONG cFetched;
263         REGFILTER *prgf;
264
265         while(!found && IEnumRegFilters_Next(pRegEnum, 1, &prgf, &cFetched) == S_OK)
266         {
267             CHAR val[512];
268
269             WideCharToMultiByte(CP_ACP, 0, prgf->Name, -1, val, sizeof(val), 0, 0);
270             if (!lstrcmpA(val, szFilterName)) found = TRUE;
271
272             CoTaskMemFree(prgf);
273         }
274
275         IEnumRegFilters_Release(pRegEnum);
276     }
277     ok(found, "IFilterMapper_EnumMatchingFilters failed to return the test filter\n");
278
279     hr = IFilterMapper_UnregisterFilter(pMapper, clsidFilter);
280     ok(hr == S_OK, "FilterMapper_UnregisterFilter failed with %x\n", hr);
281
282     lRet = RegOpenKeyExA(HKEY_CLASSES_ROOT, szClsid, 0, KEY_WRITE | DELETE, &hKey);
283     ok(lRet == ERROR_SUCCESS, "RegOpenKeyExA failed with %x\n", HRESULT_FROM_WIN32(lRet));
284
285     lRet = StringFromGUID2(&clsidFilter, wszRegKey, MAX_PATH);
286     ok(lRet > 0, "StringFromGUID2 failed\n");
287     WideCharToMultiByte(CP_ACP, 0, wszRegKey, -1, szRegKey, sizeof(szRegKey), 0, 0);
288
289     lRet = RegDeleteKeyA(hKey, szRegKey);
290     ok(lRet == ERROR_SUCCESS, "RegDeleteKeyA failed with %x\n", HRESULT_FROM_WIN32(lRet));
291
292     if (hKey) RegCloseKey(hKey);
293     hKey = NULL;
294
295     out:
296
297     if (pMapper) IFilterMapper_Release(pMapper);
298     if (pMapper2) IFilterMapper2_Release(pMapper2);
299 }
300
301 static ULONG getRefcount(IUnknown *iface)
302 {
303     IUnknown_AddRef(iface);
304     return IUnknown_Release(iface);
305 }
306
307 static void test_ifiltermapper_from_filtergraph(void)
308 {
309     IFilterGraph2* pgraph2 = NULL;
310     IFilterMapper2 *pMapper2 = NULL;
311     IFilterGraph *filtergraph = NULL;
312     HRESULT hr;
313     ULONG refcount;
314
315     hr = CoCreateInstance(&CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, &IID_IFilterGraph2, (LPVOID*)&pgraph2);
316     ok(hr == S_OK, "CoCreateInstance failed with %08x\n", hr);
317     if (!pgraph2) goto out;
318
319     hr = IFilterGraph2_QueryInterface(pgraph2, &IID_IFilterMapper2, (LPVOID*)&pMapper2);
320     ok(hr == S_OK, "IFilterGraph2_QueryInterface failed with %08x\n", hr);
321     if (!pMapper2) goto out;
322
323     refcount = getRefcount((IUnknown*)pgraph2);
324     ok(refcount == 2, "unexpected reference count: %u\n", refcount);
325     refcount = getRefcount((IUnknown*)pMapper2);
326     ok(refcount == 2, "unexpected reference count: %u\n", refcount);
327
328     IFilterMapper2_AddRef(pMapper2);
329     refcount = getRefcount((IUnknown*)pgraph2);
330     ok(refcount == 3, "unexpected reference count: %u\n", refcount);
331     refcount = getRefcount((IUnknown*)pMapper2);
332     ok(refcount == 3, "unexpected reference count: %u\n", refcount);
333     IFilterMapper2_Release(pMapper2);
334
335     hr = IFilterMapper2_QueryInterface(pMapper2, &IID_IFilterGraph, (LPVOID*)&filtergraph);
336     ok(hr == S_OK, "IFilterMapper2_QueryInterface failed with %08x\n", hr);
337     if (!filtergraph) goto out;
338
339     IFilterMapper2_Release(pMapper2);
340     pMapper2 = NULL;
341     IFilterGraph_Release(filtergraph);
342     filtergraph = NULL;
343
344     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER, &IID_IFilterMapper2, (LPVOID*)&pMapper2);
345     ok(hr == S_OK, "CoCreateInstance failed with %08x\n", hr);
346     if (!pMapper2) goto out;
347
348     hr = IFilterMapper2_QueryInterface(pMapper2, &IID_IFilterGraph, (LPVOID*)&filtergraph);
349     ok(hr == E_NOINTERFACE, "IFilterMapper2_QueryInterface unexpected result: %08x\n", hr);
350
351     out:
352
353     if (pMapper2) IFilterMapper2_Release(pMapper2);
354     if (filtergraph) IFilterGraph_Release(filtergraph);
355     if (pgraph2) IFilterGraph2_Release(pgraph2);
356 }
357
358 static void test_register_filter_with_null_clsMinorType(void)
359 {
360     IFilterMapper2 *pMapper = NULL;
361     HRESULT hr;
362     REGFILTER2 rgf2;
363     REGFILTERPINS rgPins;
364     REGFILTERPINS2 rgPins2;
365     REGPINTYPES rgPinType;
366     static WCHAR wszPinName[] = {'P', 'i', 'n', 0 };
367     static const WCHAR wszFilterName1[] = {'T', 'e', 's', 't', 'f', 'i', 'l', 't', 'e', 'r', '1', 0 };
368     static const WCHAR wszFilterName2[] = {'T', 'e', 's', 't', 'f', 'i', 'l', 't', 'e', 'r', '2', 0 };
369     CLSID clsidFilter1;
370     CLSID clsidFilter2;
371
372     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
373             &IID_IFilterMapper2, (LPVOID*)&pMapper);
374     ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
375     if (FAILED(hr)) goto out;
376
377     hr = CoCreateGuid(&clsidFilter1);
378     ok(hr == S_OK, "CoCreateGuid failed with %x\n", hr);
379     hr = CoCreateGuid(&clsidFilter2);
380     ok(hr == S_OK, "CoCreateGuid failed with %x\n", hr);
381
382     rgPinType.clsMajorType = &GUID_NULL;
383     /* Make sure quartz accepts it without crashing */
384     rgPinType.clsMinorType = NULL;
385
386     /* Test with pin descript version 1 */
387     ZeroMemory(&rgf2, sizeof(rgf2));
388     rgf2.dwVersion = 1;
389     rgf2.dwMerit = MERIT_UNLIKELY;
390     S(U(rgf2)).cPins = 1;
391     S(U(rgf2)).rgPins = &rgPins;
392
393     rgPins.strName = wszPinName;
394     rgPins.bRendered = 1;
395     rgPins.bOutput = 0;
396     rgPins.bZero = 0;
397     rgPins.bMany = 0;
398     rgPins.clsConnectsToFilter = NULL;
399     rgPins.strConnectsToPin = NULL;
400     rgPins.nMediaTypes = 1;
401     rgPins.lpMediaType = &rgPinType;
402
403     hr = IFilterMapper2_RegisterFilter(pMapper, &clsidFilter1, wszFilterName1, NULL,
404                     &CLSID_LegacyAmFilterCategory, NULL, &rgf2);
405     ok(hr == S_OK, "IFilterMapper2_RegisterFilter failed with %x\n", hr);
406
407     hr = IFilterMapper2_UnregisterFilter(pMapper, &CLSID_LegacyAmFilterCategory, NULL, &clsidFilter1);
408     ok(hr == S_OK, "FilterMapper_UnregisterFilter failed with %x\n", hr);
409
410     /* Test with pin descript version 2 */
411     ZeroMemory(&rgf2, sizeof(rgf2));
412     rgf2.dwVersion = 2;
413     rgf2.dwMerit = MERIT_UNLIKELY;
414     S1(U(rgf2)).cPins2 = 1;
415     S1(U(rgf2)).rgPins2 = &rgPins2;
416
417     rgPins2.dwFlags = REG_PINFLAG_B_RENDERER;
418     rgPins2.cInstances = 1;
419     rgPins2.nMediaTypes = 1;
420     rgPins2.lpMediaType = &rgPinType;
421     rgPins2.nMediums = 0;
422     rgPins2.lpMedium = NULL;
423     rgPins2.clsPinCategory = NULL;
424
425     hr = IFilterMapper2_RegisterFilter(pMapper, &clsidFilter2, wszFilterName2, NULL,
426                     &CLSID_LegacyAmFilterCategory, NULL, &rgf2);
427     ok(hr == S_OK, "IFilterMapper2_RegisterFilter failed with %x\n", hr);
428
429     hr = IFilterMapper2_UnregisterFilter(pMapper, &CLSID_LegacyAmFilterCategory, NULL, &clsidFilter2);
430     ok(hr == S_OK, "FilterMapper_UnregisterFilter failed with %x\n", hr);
431
432     out:
433
434     if (pMapper) IFilterMapper2_Release(pMapper);
435 }
436
437
438 START_TEST(filtermapper)
439 {
440     CoInitialize(NULL);
441
442     test_fm2_enummatchingfilters();
443     test_legacy_filter_registration();
444     test_ifiltermapper_from_filtergraph();
445     test_register_filter_with_null_clsMinorType();
446
447     CoUninitialize();
448 }