dsound: Cleanup error handling in IDirectSoundFullDuplex::Initialize().
[wine/multimedia.git] / dlls / dsound / tests / duplex.c
blob170b55243fed3f475bd2e5912da2be4bd4981d29
1 /*
2 * Unit tests for duplex functions
4 * Copyright (c) 2006 Robert Reif
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 #include <windows.h>
23 #include <stdio.h>
25 #include "wine/test.h"
26 #include "dsound.h"
27 #include "mmreg.h"
28 #include "dsconf.h"
30 #include "dsound_test.h"
32 static HRESULT (WINAPI *pDirectSoundFullDuplexCreate)(LPCGUID, LPCGUID,
33 LPCDSCBUFFERDESC, LPCDSBUFFERDESC, HWND, DWORD, LPDIRECTSOUNDFULLDUPLEX *,
34 LPDIRECTSOUNDCAPTUREBUFFER8*, LPDIRECTSOUNDBUFFER8*, LPUNKNOWN)=NULL;
36 static void IDirectSoundFullDuplex_test(LPDIRECTSOUNDFULLDUPLEX dsfdo,
37 BOOL initialized, LPCGUID lpGuidCapture,
38 LPCGUID lpGuidRender)
40 HRESULT rc;
41 int ref;
42 IUnknown * unknown;
43 IDirectSound * ds;
44 IDirectSound8 * ds8;
45 IDirectSoundCapture * dsc;
46 IDirectSoundFullDuplex * dsfd;
48 /* Try to Query for objects */
49 rc=IDirectSoundFullDuplex_QueryInterface(dsfdo,&IID_IUnknown,(LPVOID*)&unknown);
50 ok(rc==DS_OK,"IDirectSoundFullDuplex_QueryInterface(IID_IUnknown) failed: %08x\n", rc);
51 if (rc==DS_OK) {
52 ref=IDirectSoundFullDuplex_Release(unknown);
53 ok(ref==0, "IDirectSoundFullDuplex_Release() has %d references, "
54 "should have 0\n", ref);
57 rc=IDirectSoundFullDuplex_QueryInterface(dsfdo,&IID_IDirectSound,(LPVOID*)&ds);
58 ok(rc==(initialized?DS_OK:E_NOINTERFACE),"IDirectSoundFullDuplex_QueryInterface(IID_IDirectSound) failed: %08x\n", rc);
59 if (rc==DS_OK) {
60 ref=IDirectSound_Release(ds);
61 ok(ref==0, "IDirectSound_Release() has %d references, "
62 "should have 0\n", ref);
65 rc=IDirectSoundFullDuplex_QueryInterface(dsfdo,&IID_IDirectSound8,(LPVOID*)&ds8);
66 ok(rc==(initialized?DS_OK:E_NOINTERFACE),"IDirectSoundFullDuplex_QueryInterface(IID_IDirectSound8) "
67 "failed: %08x\n",rc);
68 if (rc==DS_OK) {
69 IDirectSoundFullDuplex * dsfd1;
70 rc=IDirectSound8_QueryInterface(ds8,&IID_IDirectSoundFullDuplex,(LPVOID*)&dsfd1);
71 ok(rc==DS_OK,"IDirectSound8_QueryInterface(IID_IDirectSoundFullDuplex) "
72 "failed: %08x\n",rc);
73 if (rc==DS_OK) {
74 ref=IDirectSoundFullDuplex_Release(dsfd1);
75 ok(ref==1, "IDirectSoundFullDuplex_Release() has %d references, "
76 "should have 1\n", ref);
78 ref=IDirectSound8_Release(ds8);
79 ok(ref==0, "IDirectSound8_Release() has %d references, "
80 "should have 0\n", ref);
83 rc=IDirectSoundFullDuplex_QueryInterface(dsfdo,&IID_IDirectSoundCapture,(LPVOID*)&dsc);
84 ok(rc==(initialized?DS_OK:E_NOINTERFACE),"IDirectSoundFullDuplex_QueryInterface(IID_IDirectSoundCapture) "
85 "failed: %08x\n",rc);
86 if (rc==DS_OK) {
87 ref=IDirectSoundCapture_Release(dsc);
88 ok(ref==0, "IDirectSoundCapture_Release() has %d references, "
89 "should have 0\n", ref);
92 rc=IDirectSoundFullDuplex_QueryInterface(dsfdo,&IID_IDirectSoundFullDuplex,(LPVOID*)&dsfd);
93 ok(rc==DS_OK,"IDirectSoundFullDuplex_QueryInterface(IID_IDirectSoundFullDuplex) "
94 "failed: %08x\n",rc);
95 if (rc==DS_OK) {
96 ok(dsfdo==dsfd, "different interfaces\n");
97 ref=IDirectSound8_Release(dsfd);
98 ok(ref==1, "IDirectSoundFullDuplex_Release() has %d references, should have 1\n", ref);
101 ref=IDirectSoundFullDuplex_Release(dsfdo);
102 ok(ref==0, "IDirectSoundFullDuplex_Release() has %d references, "
103 "should have 0\n", ref);
106 static void IDirectSoundFullDuplex_tests(void)
108 HRESULT rc;
109 LPDIRECTSOUNDFULLDUPLEX dsfdo = NULL;
110 DSCBUFFERDESC DSCBufferDesc;
111 DSBUFFERDESC DSBufferDesc;
112 LPDIRECTSOUNDCAPTUREBUFFER8 pDSCBuffer8;
113 LPDIRECTSOUNDBUFFER8 pDSBuffer8;
114 WAVEFORMATEX wfex;
116 trace("Testing IDirectSoundFullDuplex\n");
118 /* try the COM class factory method of creation with no devices specified */
119 rc=CoCreateInstance(&CLSID_DirectSoundFullDuplex, NULL,
120 CLSCTX_INPROC_SERVER, &IID_IDirectSoundFullDuplex,
121 (void**)&dsfdo);
122 ok(rc==S_OK||rc==REGDB_E_CLASSNOTREG||rc==CLASS_E_CLASSNOTAVAILABLE,
123 "CoCreateInstance(CLSID_DirectSoundFullDuplex) failed: 0x%08x\n", rc);
124 if (rc==REGDB_E_CLASSNOTREG) {
125 trace(" Class Not Registered\n");
126 return;
127 } else if (rc==CLASS_E_CLASSNOTAVAILABLE) {
128 trace(" Class Not Available\n");
129 return;
131 if (dsfdo)
132 IDirectSoundFullDuplex_test(dsfdo, FALSE, NULL, NULL);
134 /* try the COM class factory method of creation with default devices
135 * specified */
136 rc=CoCreateInstance(&CLSID_DirectSoundFullDuplex, NULL,
137 CLSCTX_INPROC_SERVER, &IID_IDirectSoundFullDuplex,
138 (void**)&dsfdo);
139 ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSoundFullDuplex) failed: 0x%08x\n", rc);
140 if (dsfdo)
141 IDirectSoundFullDuplex_test(dsfdo, FALSE, &DSDEVID_DefaultCapture,
142 &DSDEVID_DefaultPlayback);
144 /* try the COM class factory method of creation with default voice
145 * devices specified */
146 rc=CoCreateInstance(&CLSID_DirectSoundFullDuplex, NULL,
147 CLSCTX_INPROC_SERVER, &IID_IDirectSoundFullDuplex,
148 (void**)&dsfdo);
149 ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSoundFullDuplex) failed: 0x%08x\n", rc);
150 if (dsfdo)
151 IDirectSoundFullDuplex_test(dsfdo, FALSE, &DSDEVID_DefaultVoiceCapture,
152 &DSDEVID_DefaultVoicePlayback);
154 /* try the COM class factory method of creation with a bad
155 * IID specified */
156 rc=CoCreateInstance(&CLSID_DirectSoundFullDuplex, NULL,
157 CLSCTX_INPROC_SERVER, &CLSID_DirectSoundPrivate,
158 (void**)&dsfdo);
159 ok(rc==E_NOINTERFACE,
160 "CoCreateInstance(CLSID_DirectSoundFullDuplex,CLSID_DirectSoundPrivate) "
161 "should have failed: 0x%08x\n", rc);
163 ZeroMemory(&wfex, sizeof(wfex));
164 wfex.wFormatTag = WAVE_FORMAT_PCM;
165 wfex.nChannels = 1;
166 wfex.nSamplesPerSec = 8000;
167 wfex.wBitsPerSample = 16;
168 wfex.nBlockAlign = (wfex.wBitsPerSample * wfex.nChannels) / 8;
169 wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
171 ZeroMemory(&DSCBufferDesc, sizeof(DSCBufferDesc));
172 DSCBufferDesc.dwSize = sizeof(DSCBufferDesc);
173 DSCBufferDesc.dwFlags = DSCBCAPS_WAVEMAPPED;
174 DSCBufferDesc.dwBufferBytes = 8192;
175 DSCBufferDesc.lpwfxFormat = &wfex;
177 ZeroMemory(&DSBufferDesc, sizeof(DSBufferDesc));
178 DSBufferDesc.dwSize = sizeof(DSBufferDesc);
179 DSBufferDesc.dwFlags = DSBCAPS_GLOBALFOCUS;
180 DSBufferDesc.dwBufferBytes = 8192;
181 DSBufferDesc.lpwfxFormat = &wfex;
183 /* try with no device specified */
184 rc=pDirectSoundFullDuplexCreate(NULL,NULL,&DSCBufferDesc,&DSBufferDesc,
185 get_hwnd(),DSSCL_EXCLUSIVE ,&dsfdo,&pDSCBuffer8,
186 &pDSBuffer8,NULL);
187 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL||rc==DSERR_INVALIDCALL,
188 "DirectSoundFullDuplexCreate(NULL,NULL) failed: %08x\n",rc);
189 if (rc==S_OK && dsfdo)
190 IDirectSoundFullDuplex_test(dsfdo, TRUE, NULL, NULL);
192 /* try with default devices specified */
193 rc=pDirectSoundFullDuplexCreate(&DSDEVID_DefaultCapture,
194 &DSDEVID_DefaultPlayback,&DSCBufferDesc,
195 &DSBufferDesc,get_hwnd(),DSSCL_EXCLUSIVE,&dsfdo,
196 &pDSCBuffer8,&pDSBuffer8,NULL);
197 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL||rc==DSERR_INVALIDCALL,
198 "DirectSoundFullDuplexCreate(DSDEVID_DefaultCapture,"
199 "DSDEVID_DefaultPlayback) failed: %08x\n", rc);
200 if (rc==DS_OK && dsfdo)
201 IDirectSoundFullDuplex_test(dsfdo, TRUE, NULL, NULL);
203 /* try with default voice devices specified */
204 rc=pDirectSoundFullDuplexCreate(&DSDEVID_DefaultVoiceCapture,
205 &DSDEVID_DefaultVoicePlayback,
206 &DSCBufferDesc,&DSBufferDesc,get_hwnd(),DSSCL_EXCLUSIVE,
207 &dsfdo,&pDSCBuffer8,&pDSBuffer8,NULL);
208 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL||rc==DSERR_INVALIDCALL,
209 "DirectSoundFullDuplexCreate(DSDEVID_DefaultVoiceCapture,"
210 "DSDEVID_DefaultVoicePlayback) failed: %08x\n", rc);
211 if (rc==DS_OK && dsfdo)
212 IDirectSoundFullDuplex_test(dsfdo, TRUE, NULL, NULL);
214 /* try with bad devices specified */
215 rc=pDirectSoundFullDuplexCreate(&DSDEVID_DefaultVoicePlayback,
216 &DSDEVID_DefaultVoiceCapture,
217 &DSCBufferDesc,&DSBufferDesc,get_hwnd(),DSSCL_EXCLUSIVE,
218 &dsfdo,&pDSCBuffer8,&pDSBuffer8,NULL);
219 ok(rc==DSERR_NODRIVER||rc==DSERR_INVALIDCALL,
220 "DirectSoundFullDuplexCreate(DSDEVID_DefaultVoicePlayback,"
221 "DSDEVID_DefaultVoiceCapture) should have failed: %08x\n", rc);
222 if (rc==DS_OK && dsfdo)
223 IDirectSoundFullDuplex_Release(dsfdo);
226 static void test_COM(void)
228 IDirectSoundFullDuplex *dsfd = (IDirectSoundFullDuplex*)0xdeadbeef;
229 IDirectSound *ds;
230 IDirectSound8 *ds8;
231 IDirectSoundCapture *dsc;
232 IUnknown *unk, *unk8;
233 IDirectSoundBuffer8 *dsb8;
234 IDirectSoundCaptureBuffer8 *dscb8;
235 DSBUFFERDESC bufdesc;
236 DSCBUFFERDESC cbufdesc;
237 WAVEFORMATEX wfx;
238 ULONG refcount;
239 HRESULT hr;
241 /* COM aggregation */
242 hr = CoCreateInstance(&CLSID_DirectSoundFullDuplex, (IUnknown*)&dsfd, CLSCTX_INPROC_SERVER,
243 &IID_IUnknown, (void**)&dsfd);
244 ok(hr == CLASS_E_NOAGGREGATION,
245 "DirectSoundFullDuplex create failed: %08x, expected CLASS_E_NOAGGREGATION\n", hr);
246 ok(!dsfd, "dsfd = %p\n", dsfd);
248 /* Invalid RIID */
249 hr = CoCreateInstance(&CLSID_DirectSoundFullDuplex, NULL, CLSCTX_INPROC_SERVER,
250 &IID_IDirectSound3DBuffer, (void**)&dsfd);
251 ok(hr == E_NOINTERFACE,
252 "DirectSoundFullDuplex create failed: %08x, expected E_NOINTERFACE\n", hr);
254 /* Different refcount for IDirectSoundFullDuplex and for IUnknown */
255 hr = CoCreateInstance(&CLSID_DirectSoundFullDuplex, NULL, CLSCTX_INPROC_SERVER,
256 &IID_IDirectSoundFullDuplex, (void**)&dsfd);
257 ok(hr == S_OK, "DirectSoundFullDuplex create failed: %08x, expected S_OK\n", hr);
258 refcount = IDirectSoundFullDuplex_AddRef(dsfd);
259 ok(refcount == 2, "refcount == %u, expected 2\n", refcount);
260 hr = IDirectSoundFullDuplex_QueryInterface(dsfd, &IID_IUnknown, (void**)&unk);
261 ok(hr == S_OK, "QueryInterface for IID_IUnknown failed: %08x\n", hr);
262 refcount = IUnknown_AddRef(unk);
263 ok(refcount == 2, "refcount == %u, expected 2\n", refcount);
265 /* Not initialized */
266 hr = IDirectSoundFullDuplex_QueryInterface(dsfd, &IID_IDirectSound8, (void**)&ds8);
267 ok(hr == E_NOINTERFACE,
268 "QueryInterface for IID_IDirectSound8 failed: %08x, expected E_NOINTERFACE\n", hr);
269 hr = IDirectSoundFullDuplex_QueryInterface(dsfd, &IID_IDirectSoundCapture, (void**)&dsc);
270 ok(hr == E_NOINTERFACE,
271 "QueryInterface for IID_IDirectSoundCapture failed: %08x, expected E_NOINTERFACE\n", hr);
273 init_format(&wfx, WAVE_FORMAT_PCM, 44100, 16, 1);
274 ZeroMemory(&bufdesc, sizeof(bufdesc));
275 bufdesc.dwSize = sizeof(bufdesc);
276 bufdesc.dwBufferBytes = wfx.nAvgBytesPerSec;
277 bufdesc.lpwfxFormat = &wfx;
278 ZeroMemory(&cbufdesc, sizeof(cbufdesc));
279 cbufdesc.dwSize = sizeof(cbufdesc);
280 cbufdesc.dwBufferBytes = wfx.nAvgBytesPerSec;
281 cbufdesc.lpwfxFormat = &wfx;
282 hr = IDirectSoundFullDuplex_Initialize(dsfd, NULL, NULL, &cbufdesc, &bufdesc, get_hwnd(),
283 DSSCL_EXCLUSIVE, NULL, NULL);
284 ok(hr == E_INVALIDARG,
285 "IDirectSoundFullDuplex_Initialize failed: %08x, expected E_INVALIDARG\n", hr);
286 hr = IDirectSoundFullDuplex_Initialize(dsfd, NULL, NULL, &cbufdesc, &bufdesc, get_hwnd(),
287 DSSCL_EXCLUSIVE, &dscb8, &dsb8);
288 if (hr == DSERR_NODRIVER) {
289 skip("No driver\n");
290 return;
292 ok(hr == S_OK, "IDirectSoundFullDuplex_Initialize failed: %08x\n", hr);
294 /* IDirectSound and IDirectSound8 */
295 hr = IDirectSoundFullDuplex_QueryInterface(dsfd, &IID_IDirectSound8, (void**)&ds8);
296 ok(hr == S_OK, "QueryInterface for IID_IDirectSound8 failed: %08x\n", hr);
297 refcount = IDirectSound8_AddRef(ds8);
298 ok(refcount == 2, "refcount == %u, expected 2\n", refcount);
299 hr = IDirectSoundFullDuplex_QueryInterface(dsfd, &IID_IDirectSound, (void**)&ds);
300 ok(hr == S_OK, "QueryInterface for IID_IDirectSound failed: %08x\n", hr);
301 refcount = IDirectSound8_AddRef(ds8);
302 ok(refcount == 4, "refcount == %u, expected 4\n", refcount);
303 refcount = IDirectSound_AddRef(ds);
304 ok(refcount == 5, "refcount == %u, expected 5\n", refcount);
305 hr = IDirectSound8_QueryInterface(ds8, &IID_IUnknown, (void**)&unk8);
306 ok(hr == S_OK, "QueryInterface for IID_IUnknown failed: %08x\n", hr);
307 ok(unk == unk8, "Got different IUnknown when QI'ing IDirectSoundFullDuplex and IDirectSound\n");
308 refcount = IUnknown_AddRef(unk8);
309 ok(refcount == 4, "refcount == %u, expected 4\n", refcount);
310 refcount = IDirectSound_AddRef(ds);
311 ok(refcount == 6, "refcount == %u, expected 6\n", refcount);
312 refcount = IDirectSoundFullDuplex_AddRef(dsfd);
313 ok(refcount == 3, "refcount == %u, expected 3\n", refcount);
315 /* IDirectSoundCapture */
316 hr = IDirectSoundFullDuplex_QueryInterface(dsfd, &IID_IDirectSoundCapture, (void**)&dsc);
317 ok(hr == S_OK, "QueryInterface for IID_IDirectSoundCapture failed: %08x\n", hr);
318 refcount = IDirectSoundCapture_AddRef(dsc);
319 ok(refcount == 2, "refcount == %u, expected 2\n", refcount);
320 refcount = IDirectSoundFullDuplex_AddRef(dsfd);
321 ok(refcount == 4, "refcount == %u, expected 4\n", refcount);
322 hr = IDirectSoundCapture_QueryInterface(ds8, &IID_IUnknown, (void**)&unk8);
323 ok(hr == S_OK, "QueryInterface for IID_IUnknown failed: %08x\n", hr);
324 ok(unk == unk8,
325 "Got different IUnknown when QI'ing IDirectSoundFullDuplex and IDirectSoundCapture\n");
326 refcount = IUnknown_AddRef(unk8);
327 ok(refcount == 6, "refcount == %u, expected 6\n", refcount);
329 IDirectSoundBuffer8_Release(dsb8);
330 IDirectSoundCaptureBuffer8_Release(dscb8);
331 while (IDirectSound8_Release(ds8));
332 while (IDirectSoundCapture_Release(dsc));
333 while (IDirectSoundFullDuplex_Release(dsfd));
334 while (IUnknown_Release(unk));
337 START_TEST(duplex)
339 HMODULE hDsound;
341 CoInitialize(NULL);
343 hDsound = LoadLibrary("dsound.dll");
344 if (hDsound)
347 pDirectSoundFullDuplexCreate=(void*)GetProcAddress(hDsound,
348 "DirectSoundFullDuplexCreate");
349 if (pDirectSoundFullDuplexCreate) {
350 test_COM();
351 IDirectSoundFullDuplex_tests();
352 } else
353 skip("DirectSoundFullDuplexCreate missing - skipping all tests\n");
355 FreeLibrary(hDsound);
357 else
358 skip("dsound.dll not found - skipping all tests\n");
360 CoUninitialize();