Release 1.6-rc2.
[wine/testsucceed.git] / dlls / qedit / tests / mediadet.c
blob42a118475a4e5dc939dfeaff1fd3cabe2bb99604
1 /*
2 * Unit tests for Media Detector
4 * Copyright (C) 2008 Google (Lei Zhang, Dan Hipschman)
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.
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.
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
21 #define COBJMACROS
22 #define CONST_VTABLE
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 "rc.h"
32 /* Outer IUnknown for COM aggregation tests */
33 struct unk_impl {
34 IUnknown IUnknown_iface;
35 LONG ref;
36 IUnknown *inner_unk;
39 static inline struct unk_impl *impl_from_IUnknown(IUnknown *iface)
41 return CONTAINING_RECORD(iface, struct unk_impl, IUnknown_iface);
44 static HRESULT WINAPI unk_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
46 struct unk_impl *This = impl_from_IUnknown(iface);
48 return IUnknown_QueryInterface(This->inner_unk, riid, ppv);
51 static ULONG WINAPI unk_AddRef(IUnknown *iface)
53 struct unk_impl *This = impl_from_IUnknown(iface);
55 return InterlockedIncrement(&This->ref);
58 static ULONG WINAPI unk_Release(IUnknown *iface)
60 struct unk_impl *This = impl_from_IUnknown(iface);
62 return InterlockedDecrement(&This->ref);
65 static const IUnknownVtbl unk_vtbl =
67 unk_QueryInterface,
68 unk_AddRef,
69 unk_Release
73 static WCHAR test_avi_filename[MAX_PATH];
74 static WCHAR test_sound_avi_filename[MAX_PATH];
76 static BOOL unpack_avi_file(int id, WCHAR name[MAX_PATH])
78 static WCHAR temp_path[MAX_PATH];
79 static WCHAR prefix[] = {'D','E','S',0};
80 static WCHAR avi[] = {'a','v','i',0};
81 HRSRC res;
82 HGLOBAL data;
83 char *mem;
84 DWORD size, written;
85 HANDLE fh;
87 res = FindResource(NULL, MAKEINTRESOURCE(id), MAKEINTRESOURCE(AVI_RES_TYPE));
88 if (!res)
89 return FALSE;
91 data = LoadResource(NULL, res);
92 if (!data)
93 return FALSE;
95 mem = LockResource(data);
96 if (!mem)
97 return FALSE;
99 size = SizeofResource(NULL, res);
100 if (size == 0)
101 return FALSE;
103 if (!GetTempPathW(MAX_PATH, temp_path))
104 return FALSE;
106 /* We might end up relying on the extension here, so .TMP is no good. */
107 if (!GetTempFileNameW(temp_path, prefix, 0, name))
108 return FALSE;
110 DeleteFileW(name);
111 lstrcpyW(name + lstrlenW(name) - 3, avi);
113 fh = CreateFileW(name, GENERIC_WRITE, 0, NULL, CREATE_NEW,
114 FILE_ATTRIBUTE_NORMAL, NULL);
115 if (fh == INVALID_HANDLE_VALUE)
116 return FALSE;
118 if (!WriteFile(fh, mem, size, &written, NULL) || written != size)
119 return FALSE;
121 CloseHandle(fh);
123 return TRUE;
126 static BOOL init_tests(void)
128 return unpack_avi_file(TEST_AVI_RES, test_avi_filename)
129 && unpack_avi_file(TEST_SOUND_AVI_RES, test_sound_avi_filename);
132 static void test_mediadet(void)
134 HRESULT hr;
135 struct unk_impl unk_obj = {{&unk_vtbl}, 19, NULL};
136 IMediaDet *pM = NULL;
137 ULONG refcount;
138 BSTR filename = NULL;
139 LONG nstrms = 0;
140 LONG strm;
141 AM_MEDIA_TYPE mt;
142 double fps;
143 int flags;
144 int i;
146 /* COM aggregation */
147 hr = CoCreateInstance(&CLSID_MediaDet, &unk_obj.IUnknown_iface, CLSCTX_INPROC_SERVER,
148 &IID_IUnknown, (void**)&unk_obj.inner_unk);
149 ok(hr == S_OK, "CoCreateInstance failed: %08x\n", hr);
151 hr = IUnknown_QueryInterface(unk_obj.inner_unk, &IID_IMediaDet, (void**)&pM);
152 ok(hr == S_OK, "QueryInterface for IID_IMediaDet failed: %08x\n", hr);
153 refcount = IMediaDet_AddRef(pM);
154 ok(refcount == unk_obj.ref, "MediaDet just pretends to support COM aggregation\n");
155 refcount = IMediaDet_Release(pM);
156 ok(refcount == unk_obj.ref, "MediaDet just pretends to support COM aggregation\n");
157 refcount = IMediaDet_Release(pM);
158 ok(refcount == 19, "Refcount should be back at 19 but is %u\n", refcount);
160 IUnknown_Release(unk_obj.inner_unk);
162 /* test.avi has one video stream. */
163 hr = CoCreateInstance(&CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER,
164 &IID_IMediaDet, (LPVOID*)&pM);
165 ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
166 ok(pM != NULL, "pM is NULL\n");
168 filename = NULL;
169 hr = IMediaDet_get_Filename(pM, &filename);
170 /* Despite what MSDN claims, this returns S_OK. */
171 ok(hr == S_OK, "IMediaDet_get_Filename failed: %08x\n", hr);
172 ok(filename == NULL, "IMediaDet_get_Filename\n");
174 filename = (BSTR) -1;
175 hr = IMediaDet_get_Filename(pM, &filename);
176 /* Despite what MSDN claims, this returns S_OK. */
177 ok(hr == S_OK, "IMediaDet_get_Filename failed: %08x\n", hr);
178 ok(filename == NULL, "IMediaDet_get_Filename\n");
180 nstrms = -1;
181 hr = IMediaDet_get_OutputStreams(pM, &nstrms);
182 ok(hr == E_INVALIDARG, "IMediaDet_get_OutputStreams failed: %08x\n", hr);
183 ok(nstrms == -1, "IMediaDet_get_OutputStreams: nstrms is %i\n", nstrms);
185 strm = -1;
186 /* The stream defaults to 0, even without a file! */
187 hr = IMediaDet_get_CurrentStream(pM, &strm);
188 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
189 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
191 hr = IMediaDet_get_CurrentStream(pM, NULL);
192 ok(hr == E_POINTER, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
194 /* But put_CurrentStream doesn't. */
195 hr = IMediaDet_put_CurrentStream(pM, 0);
196 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
198 hr = IMediaDet_put_CurrentStream(pM, -1);
199 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
201 hr = IMediaDet_get_StreamMediaType(pM, &mt);
202 ok(hr == E_INVALIDARG, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
204 hr = IMediaDet_get_StreamMediaType(pM, NULL);
205 ok(hr == E_POINTER, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
207 filename = SysAllocString(test_avi_filename);
208 hr = IMediaDet_put_Filename(pM, filename);
209 ok(hr == S_OK, "IMediaDet_put_Filename failed: %08x\n", hr);
210 SysFreeString(filename);
212 strm = -1;
213 /* The stream defaults to 0. */
214 hr = IMediaDet_get_CurrentStream(pM, &strm);
215 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
216 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
218 ZeroMemory(&mt, sizeof mt);
219 hr = IMediaDet_get_StreamMediaType(pM, &mt);
220 ok(hr == S_OK, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
221 CoTaskMemFree(mt.pbFormat);
223 /* Even before get_OutputStreams. */
224 hr = IMediaDet_put_CurrentStream(pM, 1);
225 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
227 hr = IMediaDet_get_OutputStreams(pM, &nstrms);
228 ok(hr == S_OK, "IMediaDet_get_OutputStreams failed: %08x\n", hr);
229 ok(nstrms == 1, "IMediaDet_get_OutputStreams: nstrms is %i\n", nstrms);
231 filename = NULL;
232 hr = IMediaDet_get_Filename(pM, &filename);
233 ok(hr == S_OK, "IMediaDet_get_Filename failed: %08x\n", hr);
234 ok(lstrcmpW(filename, test_avi_filename) == 0,
235 "IMediaDet_get_Filename\n");
236 SysFreeString(filename);
238 hr = IMediaDet_get_Filename(pM, NULL);
239 ok(hr == E_POINTER, "IMediaDet_get_Filename failed: %08x\n", hr);
241 strm = -1;
242 hr = IMediaDet_get_CurrentStream(pM, &strm);
243 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
244 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
246 hr = IMediaDet_get_CurrentStream(pM, NULL);
247 ok(hr == E_POINTER, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
249 hr = IMediaDet_put_CurrentStream(pM, -1);
250 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
252 hr = IMediaDet_put_CurrentStream(pM, 1);
253 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
255 /* Try again. */
256 strm = -1;
257 hr = IMediaDet_get_CurrentStream(pM, &strm);
258 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
259 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
261 hr = IMediaDet_put_CurrentStream(pM, 0);
262 ok(hr == S_OK, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
264 strm = -1;
265 hr = IMediaDet_get_CurrentStream(pM, &strm);
266 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
267 ok(strm == 0, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
269 ZeroMemory(&mt, sizeof mt);
270 hr = IMediaDet_get_StreamMediaType(pM, &mt);
271 ok(hr == S_OK, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
272 ok(IsEqualGUID(&mt.majortype, &MEDIATYPE_Video),
273 "IMediaDet_get_StreamMediaType\n");
274 CoTaskMemFree(mt.pbFormat);
276 hr = IMediaDet_get_FrameRate(pM, NULL);
277 ok(hr == E_POINTER, "IMediaDet_get_FrameRate failed: %08x\n", hr);
279 hr = IMediaDet_get_FrameRate(pM, &fps);
280 ok(hr == S_OK, "IMediaDet_get_FrameRate failed: %08x\n", hr);
281 ok(fps == 10.0, "IMediaDet_get_FrameRate: fps is %f\n", fps);
283 hr = IMediaDet_Release(pM);
284 ok(hr == 0, "IMediaDet_Release returned: %x\n", hr);
286 DeleteFileW(test_avi_filename);
288 /* test_sound.avi has one video stream and one audio stream. */
289 hr = CoCreateInstance(&CLSID_MediaDet, NULL, CLSCTX_INPROC_SERVER,
290 &IID_IMediaDet, (LPVOID*)&pM);
291 ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
292 ok(pM != NULL, "pM is NULL\n");
294 filename = SysAllocString(test_sound_avi_filename);
295 hr = IMediaDet_put_Filename(pM, filename);
296 ok(hr == S_OK, "IMediaDet_put_Filename failed: %08x\n", hr);
297 SysFreeString(filename);
299 hr = IMediaDet_get_OutputStreams(pM, &nstrms);
300 ok(hr == S_OK, "IMediaDet_get_OutputStreams failed: %08x\n", hr);
301 ok(nstrms == 2, "IMediaDet_get_OutputStreams: nstrms is %i\n", nstrms);
303 filename = NULL;
304 hr = IMediaDet_get_Filename(pM, &filename);
305 ok(hr == S_OK, "IMediaDet_get_Filename failed: %08x\n", hr);
306 ok(lstrcmpW(filename, test_sound_avi_filename) == 0,
307 "IMediaDet_get_Filename\n");
308 SysFreeString(filename);
310 /* I don't know if the stream order is deterministic. Just check
311 for both an audio and video stream. */
312 flags = 0;
314 for (i = 0; i < 2; ++i)
316 hr = IMediaDet_put_CurrentStream(pM, i);
317 ok(hr == S_OK, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
319 strm = -1;
320 hr = IMediaDet_get_CurrentStream(pM, &strm);
321 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
322 ok(strm == i, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
324 ZeroMemory(&mt, sizeof mt);
325 hr = IMediaDet_get_StreamMediaType(pM, &mt);
326 ok(hr == S_OK, "IMediaDet_get_StreamMediaType failed: %08x\n", hr);
327 flags += (IsEqualGUID(&mt.majortype, &MEDIATYPE_Video)
329 : (IsEqualGUID(&mt.majortype, &MEDIATYPE_Audio)
331 : 0));
333 if (IsEqualGUID(&mt.majortype, &MEDIATYPE_Audio))
335 hr = IMediaDet_get_FrameRate(pM, &fps);
336 ok(hr == VFW_E_INVALIDMEDIATYPE, "IMediaDet_get_FrameRate failed: %08x\n", hr);
339 CoTaskMemFree(mt.pbFormat);
341 ok(flags == 3, "IMediaDet_get_StreamMediaType: flags are %i\n", flags);
343 hr = IMediaDet_put_CurrentStream(pM, 2);
344 ok(hr == E_INVALIDARG, "IMediaDet_put_CurrentStream failed: %08x\n", hr);
346 strm = -1;
347 hr = IMediaDet_get_CurrentStream(pM, &strm);
348 ok(hr == S_OK, "IMediaDet_get_CurrentStream failed: %08x\n", hr);
349 ok(strm == 1, "IMediaDet_get_CurrentStream: strm is %i\n", strm);
351 hr = IMediaDet_Release(pM);
352 ok(hr == 0, "IMediaDet_Release returned: %x\n", hr);
354 DeleteFileW(test_sound_avi_filename);
357 static void test_samplegrabber(void)
359 struct unk_impl unk_obj = {{&unk_vtbl}, 19, NULL};
360 ISampleGrabber *sg;
361 ULONG refcount;
362 HRESULT hr;
364 /* COM aggregation */
365 hr = CoCreateInstance(&CLSID_SampleGrabber, &unk_obj.IUnknown_iface, CLSCTX_INPROC_SERVER,
366 &IID_IUnknown, (void**)&unk_obj.inner_unk);
367 ok(hr == S_OK, "CoCreateInstance failed: %08x\n", hr);
369 hr = IUnknown_QueryInterface(unk_obj.inner_unk, &IID_ISampleGrabber, (void**)&sg);
370 ok(hr == S_OK, "QueryInterface for IID_ISampleGrabber failed: %08x\n", hr);
371 refcount = ISampleGrabber_AddRef(sg);
372 ok(refcount == unk_obj.ref, "SampleGrabber just pretends to support COM aggregation\n");
373 refcount = ISampleGrabber_Release(sg);
374 ok(refcount == unk_obj.ref, "SampleGrabber just pretends to support COM aggregation\n");
375 refcount = ISampleGrabber_Release(sg);
376 ok(refcount == 19, "Refcount should be back at 19 but is %u\n", refcount);
378 IUnknown_Release(unk_obj.inner_unk);
381 START_TEST(mediadet)
383 if (!init_tests())
385 skip("Couldn't initialize tests!\n");
386 return;
389 CoInitialize(NULL);
390 test_mediadet();
391 test_samplegrabber();
392 CoUninitialize();