[QEDIT_WINETEST] Import from Wine Staging 1.7.55. CORE-10536
[reactos.git] / rostests / winetests / qedit / mediadet.c
1 /*
2 * Unit tests for Media Detector
3 *
4 * Copyright (C) 2008 Google (Lei Zhang, Dan Hipschman)
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 #define CONST_VTABLE
23
24 #include "initguid.h"
25 #include "ole2.h"
26 #include "vfwmsgs.h"
27 #include "uuids.h"
28 #include "wine/test.h"
29 #include "qedit.h"
30 #include "control.h"
31 #include "rc.h"
32
33 /* Outer IUnknown for COM aggregation tests */
34 struct unk_impl {
35 IUnknown IUnknown_iface;
36 LONG ref;
37 IUnknown *inner_unk;
38 };
39
40 static inline struct unk_impl *impl_from_IUnknown(IUnknown *iface)
41 {
42 return CONTAINING_RECORD(iface, struct unk_impl, IUnknown_iface);
43 }
44
45 static HRESULT WINAPI unk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
46 {
47 struct unk_impl *This = impl_from_IUnknown(iface);
48
49 return IUnknown_QueryInterface(This->inner_unk, riid, ppv);
50 }
51
52 static ULONG WINAPI unk_AddRef(IUnknown *iface)
53 {
54 struct unk_impl *This = impl_from_IUnknown(iface);
55
56 return InterlockedIncrement(&This->ref);
57 }
58
59 static ULONG WINAPI unk_Release(IUnknown *iface)
60 {
61 struct unk_impl *This = impl_from_IUnknown(iface);
62
63 return InterlockedDecrement(&This->ref);
64 }
65
66 static const IUnknownVtbl unk_vtbl =
67 {
68 unk_QueryInterface,
69 unk_AddRef,
70 unk_Release
71 };
72
73
74 static WCHAR test_avi_filename[MAX_PATH];
75 static WCHAR test_sound_avi_filename[MAX_PATH];
76
77 static BOOL unpack_avi_file(int id, WCHAR name[MAX_PATH])
78 {
79 static WCHAR temp_path[MAX_PATH];
80 static const WCHAR prefix[] = {'D','E','S',0};
81 static const WCHAR avi[] = {'a','v','i',0};
82 HRSRC res;
83 HGLOBAL data;
84 char *mem;
85 DWORD size, written;
86 HANDLE fh;
87 BOOL ret;
88
89 res = FindResourceW(NULL, MAKEINTRESOURCEW(id), MAKEINTRESOURCEW(AVI_RES_TYPE));
90 if (!res)
91 return FALSE;
92
93 data = LoadResource(NULL, res);
94 if (!data)
95 return FALSE;
96
97 mem = LockResource(data);
98 if (!mem)
99 return FALSE;
100
101 size = SizeofResource(NULL, res);
102 if (size == 0)
103 return FALSE;
104
105 if (!GetTempPathW(MAX_PATH, temp_path))
106 return FALSE;
107
108 /* We might end up relying on the extension here, so .TMP is no good. */
109 if (!GetTempFileNameW(temp_path, prefix, 0, name))
110 return FALSE;
111
112 DeleteFileW(name);
113 lstrcpyW(name + lstrlenW(name) - 3, avi);
114
115 fh = CreateFileW(name, GENERIC_WRITE, 0, NULL, CREATE_NEW,
116 FILE_ATTRIBUTE_NORMAL, NULL);
117 if (fh == INVALID_HANDLE_VALUE)
118 return FALSE;
119
120 ret = WriteFile(fh, mem, size, &written, NULL);
121 CloseHandle(fh);
122 return ret && written == size;
123 }
124
125 static BOOL init_tests(void)
126 {
127 return unpack_avi_file(TEST_AVI_RES, test_avi_filename)
128 && unpack_avi_file(TEST_SOUND_AVI_RES, test_sound_avi_filename);
129 }
130
131 static void test_mediadet(void)
132 {
133 HRESULT hr;
134 struct unk_impl unk_obj = {{&unk_vtbl}, 19, NULL};
135 IMediaDet *pM = NULL;
136 ULONG refcount;
137 BSTR filename = NULL;
138 LONG nstrms = 0;
139 LONG strm;
140 AM_MEDIA_TYPE mt;
141 double fps;
142 int flags;
143 int i;
144
145 /* COM aggregation */
146 hr = CoCreateInstance(&CLSID_MediaDet, &unk_obj.IUnknown_iface, CLSCTX_INPROC_SERVER,
147 &IID_IUnknown, (void**)&unk_obj.inner_unk);
148 ok(hr == S_OK, "CoCreateInstance failed: %08x\n", hr);
149
150 hr = IUnknown_QueryInterface(unk_obj.inner_unk, &IID_IMediaDet, (void**)&pM);
151 ok(hr == S_OK, "QueryInterface for IID_IMediaDet failed: %08x\n", hr);
152 refcount = IMediaDet_AddRef(pM);
153 ok(refcount == unk_obj.ref, "MediaDet just pretends to support COM aggregation\n");
154 refcount = IMediaDet_Release(pM);
155 ok(refcount == unk_obj.ref, "MediaDet just pretends to support COM aggregation\n");
156 refcount = IMediaDet_Release(pM);
157 ok(refcount == 19, "Refcount should be back at 19 but is %u\n", refcount);
158
159 IUnknown_Release(unk_obj.inner_unk);
160
161 /* test.avi has one video stream. */
162 hr = CoCreateInstance(&CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER,
163 &IID_IMediaDet, (LPVOID*)&pM);
164 ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
165 ok(pM != NULL, "pM is NULL\n");
166
167 filename = NULL;
168 hr = IMediaDet_get_Filename(pM, &filename);
169 /* Despite what MSDN claims, this returns S_OK. */
170 ok(hr == S_OK, "IMediaDet_get_Filename failed: %08x\n", hr);
171 ok(filename == NULL, "IMediaDet_get_Filename\n");
172
173 filename = (BSTR) -1;
174 hr = IMediaDet_get_Filename(pM, &filename);
175 /* Despite what MSDN claims, this returns S_OK. */
176 ok(hr == S_OK, "IMediaDet_get_Filename failed: %08x\n", hr);
177 ok(filename == NULL, "IMediaDet_get_Filename\n");
178
179 nstrms = -1;
180 hr = IMediaDet_get_OutputStreams(pM, &nstrms);
181 ok(hr == E_INVALIDARG, "IMediaDet_get_OutputStreams failed: %08x\n", hr);
182 ok(nstrms == -1, "IMediaDet_get_OutputStreams: nstrms is %i\n", nstrms);
183
184 strm = -1;
185 /* The stream defaults to 0, even without a file! */
186 hr = IMediaDet_get_CurrentStream(pM, &strm);
187 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
188 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
189
190 hr = IMediaDet_get_CurrentStream(pM, NULL);
191 ok(hr == E_POINTER, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
192
193 /* But put_CurrentStream doesn't. */
194 hr = IMediaDet_put_CurrentStream(pM, 0);
195 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
196
197 hr = IMediaDet_put_CurrentStream(pM, -1);
198 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
199
200 hr = IMediaDet_get_StreamMediaType(pM, &mt);
201 ok(hr == E_INVALIDARG, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
202
203 hr = IMediaDet_get_StreamMediaType(pM, NULL);
204 ok(hr == E_POINTER, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
205
206 filename = SysAllocString(test_avi_filename);
207 hr = IMediaDet_put_Filename(pM, filename);
208 ok(hr == S_OK, "IMediaDet_put_Filename failed: %08x\n", hr);
209 SysFreeString(filename);
210
211 strm = -1;
212 /* The stream defaults to 0. */
213 hr = IMediaDet_get_CurrentStream(pM, &strm);
214 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
215 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
216
217 ZeroMemory(&mt, sizeof mt);
218 hr = IMediaDet_get_StreamMediaType(pM, &mt);
219 ok(hr == S_OK, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
220 CoTaskMemFree(mt.pbFormat);
221
222 /* Even before get_OutputStreams. */
223 hr = IMediaDet_put_CurrentStream(pM, 1);
224 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
225
226 hr = IMediaDet_get_OutputStreams(pM, &nstrms);
227 ok(hr == S_OK, "IMediaDet_get_OutputStreams failed: %08x\n", hr);
228 ok(nstrms == 1, "IMediaDet_get_OutputStreams: nstrms is %i\n", nstrms);
229
230 filename = NULL;
231 hr = IMediaDet_get_Filename(pM, &filename);
232 ok(hr == S_OK, "IMediaDet_get_Filename failed: %08x\n", hr);
233 ok(lstrcmpW(filename, test_avi_filename) == 0,
234 "IMediaDet_get_Filename\n");
235 SysFreeString(filename);
236
237 hr = IMediaDet_get_Filename(pM, NULL);
238 ok(hr == E_POINTER, "IMediaDet_get_Filename failed: %08x\n", hr);
239
240 strm = -1;
241 hr = IMediaDet_get_CurrentStream(pM, &strm);
242 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
243 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
244
245 hr = IMediaDet_get_CurrentStream(pM, NULL);
246 ok(hr == E_POINTER, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
247
248 hr = IMediaDet_put_CurrentStream(pM, -1);
249 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
250
251 hr = IMediaDet_put_CurrentStream(pM, 1);
252 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
253
254 /* Try again. */
255 strm = -1;
256 hr = IMediaDet_get_CurrentStream(pM, &strm);
257 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
258 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
259
260 hr = IMediaDet_put_CurrentStream(pM, 0);
261 ok(hr == S_OK, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
262
263 strm = -1;
264 hr = IMediaDet_get_CurrentStream(pM, &strm);
265 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
266 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
267
268 ZeroMemory(&mt, sizeof mt);
269 hr = IMediaDet_get_StreamMediaType(pM, &mt);
270 ok(hr == S_OK, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
271 ok(IsEqualGUID(&mt.majortype, &MEDIATYPE_Video),
272 "IMediaDet_get_StreamMediaType\n");
273 CoTaskMemFree(mt.pbFormat);
274
275 hr = IMediaDet_get_FrameRate(pM, NULL);
276 ok(hr == E_POINTER, "IMediaDet_get_FrameRate failed: %08x\n", hr);
277
278 hr = IMediaDet_get_FrameRate(pM, &fps);
279 ok(hr == S_OK, "IMediaDet_get_FrameRate failed: %08x\n", hr);
280 ok(fps == 10.0, "IMediaDet_get_FrameRate: fps is %f\n", fps);
281
282 hr = IMediaDet_Release(pM);
283 ok(hr == 0, "IMediaDet_Release returned: %x\n", hr);
284
285 DeleteFileW(test_avi_filename);
286
287 /* test_sound.avi has one video stream and one audio stream. */
288 hr = CoCreateInstance(&CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER,
289 &IID_IMediaDet, (LPVOID*)&pM);
290 ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
291 ok(pM != NULL, "pM is NULL\n");
292
293 filename = SysAllocString(test_sound_avi_filename);
294 hr = IMediaDet_put_Filename(pM, filename);
295 ok(hr == S_OK, "IMediaDet_put_Filename failed: %08x\n", hr);
296 SysFreeString(filename);
297
298 hr = IMediaDet_get_OutputStreams(pM, &nstrms);
299 ok(hr == S_OK, "IMediaDet_get_OutputStreams failed: %08x\n", hr);
300 ok(nstrms == 2, "IMediaDet_get_OutputStreams: nstrms is %i\n", nstrms);
301
302 filename = NULL;
303 hr = IMediaDet_get_Filename(pM, &filename);
304 ok(hr == S_OK, "IMediaDet_get_Filename failed: %08x\n", hr);
305 ok(lstrcmpW(filename, test_sound_avi_filename) == 0,
306 "IMediaDet_get_Filename\n");
307 SysFreeString(filename);
308
309 /* I don't know if the stream order is deterministic. Just check
310 for both an audio and video stream. */
311 flags = 0;
312
313 for (i = 0; i < 2; ++i)
314 {
315 hr = IMediaDet_put_CurrentStream(pM, i);
316 ok(hr == S_OK, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
317
318 strm = -1;
319 hr = IMediaDet_get_CurrentStream(pM, &strm);
320 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
321 ok(strm == i, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
322
323 ZeroMemory(&mt, sizeof mt);
324 hr = IMediaDet_get_StreamMediaType(pM, &mt);
325 ok(hr == S_OK, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
326 flags += (IsEqualGUID(&mt.majortype, &MEDIATYPE_Video)
327 ? 1
328 : (IsEqualGUID(&mt.majortype, &MEDIATYPE_Audio)
329 ? 2
330 : 0));
331
332 if (IsEqualGUID(&mt.majortype, &MEDIATYPE_Audio))
333 {
334 hr = IMediaDet_get_FrameRate(pM, &fps);
335 ok(hr == VFW_E_INVALIDMEDIATYPE, "IMediaDet_get_FrameRate failed: %08x\n", hr);
336 }
337
338 CoTaskMemFree(mt.pbFormat);
339 }
340 ok(flags == 3, "IMediaDet_get_StreamMediaType: flags are %i\n", flags);
341
342 hr = IMediaDet_put_CurrentStream(pM, 2);
343 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
344
345 strm = -1;
346 hr = IMediaDet_get_CurrentStream(pM, &strm);
347 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
348 ok(strm == 1, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
349
350 hr = IMediaDet_Release(pM);
351 ok(hr == 0, "IMediaDet_Release returned: %x\n", hr);
352
353 DeleteFileW(test_sound_avi_filename);
354 }
355
356 static void test_samplegrabber(void)
357 {
358 struct unk_impl unk_obj = {{&unk_vtbl}, 19, NULL};
359 ISampleGrabber *sg;
360 IBaseFilter *bf;
361 IMediaFilter *mf;
362 IPersist *persist;
363 IUnknown *unk;
364 ULONG refcount;
365 HRESULT hr;
366
367 /* COM aggregation */
368 hr = CoCreateInstance(&CLSID_SampleGrabber, &unk_obj.IUnknown_iface, CLSCTX_INPROC_SERVER,
369 &IID_IUnknown, (void**)&unk_obj.inner_unk);
370 ok(hr == S_OK, "CoCreateInstance failed: %08x\n", hr);
371
372 hr = IUnknown_QueryInterface(unk_obj.inner_unk, &IID_ISampleGrabber, (void**)&sg);
373 ok(hr == S_OK, "QueryInterface for IID_ISampleGrabber failed: %08x\n", hr);
374 refcount = ISampleGrabber_AddRef(sg);
375 ok(refcount == unk_obj.ref, "SampleGrabber just pretends to support COM aggregation\n");
376 refcount = ISampleGrabber_Release(sg);
377 ok(refcount == unk_obj.ref, "SampleGrabber just pretends to support COM aggregation\n");
378 refcount = ISampleGrabber_Release(sg);
379 ok(refcount == 19, "Refcount should be back at 19 but is %u\n", refcount);
380 IUnknown_Release(unk_obj.inner_unk);
381
382 /* Invalid RIID */
383 hr = CoCreateInstance(&CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, &IID_IClassFactory,
384 (void**)&sg);
385 ok(hr == E_NOINTERFACE, "SampleGrabber create failed: %08x, expected E_NOINTERFACE\n", hr);
386
387 /* Same refcount for all SampleGrabber interfaces */
388 hr = CoCreateInstance(&CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, &IID_ISampleGrabber,
389 (void**)&sg);
390 ok(hr == S_OK, "SampleGrabber create failed: %08x, expected S_OK\n", hr);
391 refcount = ISampleGrabber_AddRef(sg);
392 ok(refcount == 2, "refcount == %u, expected 2\n", refcount);
393
394 hr = ISampleGrabber_QueryInterface(sg, &IID_IBaseFilter, (void**)&bf);
395 ok(hr == S_OK, "QueryInterface for IID_IBaseFilter failed: %08x\n", hr);
396 refcount = IBaseFilter_AddRef(bf);
397 ok(refcount == 4, "refcount == %u, expected 4\n", refcount);
398 refcount = IBaseFilter_Release(bf);
399
400 hr = ISampleGrabber_QueryInterface(sg, &IID_IMediaFilter, (void**)&mf);
401 ok(hr == S_OK, "QueryInterface for IID_IMediaFilter failed: %08x\n", hr);
402 refcount = IMediaFilter_AddRef(mf);
403 ok(refcount == 5, "refcount == %u, expected 5\n", refcount);
404 refcount = IMediaFilter_Release(mf);
405
406 hr = ISampleGrabber_QueryInterface(sg, &IID_IPersist, (void**)&persist);
407 ok(hr == S_OK, "QueryInterface for IID_IPersist failed: %08x\n", hr);
408 refcount = IPersist_AddRef(persist);
409 ok(refcount == 6, "refcount == %u, expected 6\n", refcount);
410 refcount = IPersist_Release(persist);
411
412 hr = ISampleGrabber_QueryInterface(sg, &IID_IUnknown, (void**)&unk);
413 ok(hr == S_OK, "QueryInterface for IID_IUnknown failed: %08x\n", hr);
414 refcount = IUnknown_AddRef(unk);
415 ok(refcount == 7, "refcount == %u, expected 7\n", refcount);
416 refcount = IUnknown_Release(unk);
417
418 /* Interfaces that native does not support */
419 hr = ISampleGrabber_QueryInterface(sg, &IID_IMediaPosition, (void**)&unk);
420 todo_wine ok(hr == E_NOINTERFACE, "QueryInterface for IID_IMediaPosition failed: %08x\n", hr);
421 hr = ISampleGrabber_QueryInterface(sg, &IID_IMediaSeeking, (void**)&unk);
422 todo_wine ok(hr == E_NOINTERFACE, "QueryInterface for IID_IMediaSeeking failed: %08x\n", hr);
423 hr = ISampleGrabber_QueryInterface(sg, &IID_IMemInputPin, (void**)&unk);
424 ok(hr == E_NOINTERFACE, "QueryInterface for IID_IMemInputPin failed: %08x\n", hr);
425 hr = ISampleGrabber_QueryInterface(sg, &IID_IQualityControl, (void**)&unk);
426 ok(hr == E_NOINTERFACE, "QueryInterface for IID_IQualityControl failed: %08x\n", hr);
427 hr = ISampleGrabber_QueryInterface(sg, &IID_ISeekingPassThru, (void**)&unk);
428 ok(hr == E_NOINTERFACE, "QueryInterface for IID_ISeekingPassThru failed: %08x\n", hr);
429
430 while (ISampleGrabber_Release(sg));
431 }
432
433 static void test_COM_sg_enumpins(void)
434 {
435 IBaseFilter *bf;
436 IEnumPins *pins, *pins2;
437 IUnknown *unk;
438 ULONG refcount;
439 HRESULT hr;
440
441 hr = CoCreateInstance(&CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, &IID_IBaseFilter,
442 (void**)&bf);
443 ok(hr == S_OK, "SampleGrabber create failed: %08x, expected S_OK\n", hr);
444 hr = IBaseFilter_EnumPins(bf, &pins);
445 ok(hr == S_OK, "EnumPins create failed: %08x, expected S_OK\n", hr);
446
447 /* Same refcount for all EnumPins interfaces */
448 refcount = IEnumPins_AddRef(pins);
449 ok(refcount == 2, "refcount == %u, expected 2\n", refcount);
450 hr = IEnumPins_QueryInterface(pins, &IID_IEnumPins, (void**)&pins2);
451 ok(hr == S_OK, "QueryInterface for IID_IEnumPins failed: %08x\n", hr);
452 ok(pins == pins2, "QueryInterface for self failed (%p != %p)\n", pins, pins2);
453 IEnumPins_Release(pins2);
454
455 hr = IEnumPins_QueryInterface(pins, &IID_IUnknown, (void**)&unk);
456 ok(hr == S_OK, "QueryInterface for IID_IUnknown failed: %08x\n", hr);
457 refcount = IUnknown_AddRef(unk);
458 ok(refcount == 4, "refcount == %u, expected 4\n", refcount);
459 refcount = IUnknown_Release(unk);
460
461 while (IEnumPins_Release(pins));
462 IBaseFilter_Release(bf);
463 }
464
465 START_TEST(mediadet)
466 {
467 if (!init_tests())
468 {
469 skip("Couldn't initialize tests!\n");
470 return;
471 }
472
473 CoInitialize(NULL);
474 test_mediadet();
475 test_samplegrabber();
476 test_COM_sg_enumpins();
477 CoUninitialize();
478 }