Avoid excessive heap memory reallocation when generating EMF
[wine] / dlls / dsound / tests / propset.c
1 /*
2  * Unit tests for CLSID_DirectSoundPrivate property set functions (used by dxdiag)
3  *
4  * Copyright (c) 2003 Robert Reif
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #define NONAMELESSSTRUCT
22 #define NONAMELESSUNION
23 #include <windows.h>
24
25 #include <math.h>
26 #include <stdlib.h>
27
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "dsound.h"
32 #include "dsconf.h"
33
34 #include "initguid.h"
35
36 DEFINE_GUID(DSPROPSETID_VoiceManager,0x62A69BAE,0xDF9D,0x11D1,0x99,0xA6,0x00,0xC0,0x4F,0xC9,0x9D,0x46);
37 DEFINE_GUID(DSPROPSETID_EAX20_ListenerProperties,0x306a6a8,0xb224,0x11d2,0x99,0xe5,0x0,0x0,0xe8,0xd8,0xc7,0x22);
38 DEFINE_GUID(DSPROPSETID_EAX20_BufferProperties,0x306a6a7,0xb224,0x11d2,0x99,0xe5,0x0,0x0,0xe8,0xd8,0xc7,0x22);
39 DEFINE_GUID(DSPROPSETID_I3DL2_ListenerProperties,0xDA0F0520,0x300A,0x11D3,0x8A,0x2B,0x00,0x60,0x97,0x0D,0xB0,0x11);
40 DEFINE_GUID(DSPROPSETID_I3DL2_BufferProperties,0xDA0F0521,0x300A,0x11D3,0x8A,0x2B,0x00,0x60,0x97,0x0D,0xB0,0x11);
41 DEFINE_GUID(DSPROPSETID_ZOOMFX_BufferProperties,0xCD5368E0,0x3450,0x11D3,0x8B,0x6E,0x00,0x10,0x5A,0x9B,0x7B,0xBC);
42
43 typedef HRESULT  (CALLBACK * MYPROC)(REFCLSID, REFIID, LPVOID *);
44
45 BOOL CALLBACK callback(PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA data, LPVOID context)
46 {
47         trace("found device:\n");
48         trace("\tType: %s\n", 
49                 data->Type == DIRECTSOUNDDEVICE_TYPE_EMULATED ? "Emulated" :
50                 data->Type == DIRECTSOUNDDEVICE_TYPE_VXD ? "VxD" :
51                 data->Type == DIRECTSOUNDDEVICE_TYPE_WDM ? "WDM" : "Unknown");
52         trace("\tDataFlow: %s\n",
53                 data->DataFlow == DIRECTSOUNDDEVICE_DATAFLOW_RENDER ? "Render" :
54                 data->DataFlow == DIRECTSOUNDDEVICE_DATAFLOW_CAPTURE ? "Capture" : "Unknown");
55         trace("\tDeviceId: {%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
56                 data->DeviceId.Data1,data->DeviceId.Data2,data->DeviceId.Data3,
57                 data->DeviceId.Data4[0],data->DeviceId.Data4[1],data->DeviceId.Data4[2],data->DeviceId.Data4[3],
58                 data->DeviceId.Data4[4],data->DeviceId.Data4[5],data->DeviceId.Data4[6],data->DeviceId.Data4[7]);
59         trace("\tDescription: %s\n", data->Description);
60         trace("\tModule: %s\n", data->Module);
61         trace("\tInterface: %s\n", data->Interface);
62         trace("\tWaveDeviceId: %ld\n", data->WaveDeviceId);
63
64         return TRUE;
65 }
66
67 static void propset_private_tests()
68 {
69         HMODULE hDsound;
70         HRESULT hr;
71         IClassFactory   * pcf;
72         IKsPropertySet  * pps;
73         MYPROC fProc;
74         ULONG   support;
75
76         hDsound = LoadLibrary("dsound.dll");
77         ok(hDsound!=0,"LoadLibrary(dsound.dll) failed\n");
78         if (hDsound==0)
79                 return;
80
81         fProc = (MYPROC)GetProcAddress(hDsound, "DllGetClassObject");
82
83         /* try direct sound first */
84         hr = (fProc)(&CLSID_DirectSound, &IID_IClassFactory, (void **)0);
85         ok(hr==DSERR_INVALIDPARAM,"DllGetClassObject(CLSID_DirectSound, IID_IClassFactory) should have failed: 0x%lx\n",hr);
86
87         hr = (fProc)(&CLSID_DirectSound, &IID_IClassFactory, (void **)(&pcf));
88         ok(pcf!=0, "DllGetClassObject(CLSID_DirectSound, IID_IClassFactory) failed: 0x%lx\n",hr);
89         if (pcf==0)
90                 goto error;
91
92         /* direct sound doesn't have an IKsPropertySet */
93         hr = pcf->lpVtbl->CreateInstance(pcf, NULL, &IID_IKsPropertySet, (void **)0);
94         ok(hr==DSERR_INVALIDPARAM, "CreateInstance(IID_IKsPropertySet) should have failed: 0x%lx\n",hr);
95
96         hr = pcf->lpVtbl->CreateInstance(pcf, NULL, &IID_IKsPropertySet, (void **)(&pps));
97         ok(hr==E_NOINTERFACE, "CreateInstance(IID_IKsPropertySet) should have failed: 0x%lx\n",hr);
98
99         /* and the direct sound 8 version */
100         hr = (fProc)(&CLSID_DirectSound8, &IID_IClassFactory, (void **)(&pcf));
101         ok(pcf!=0, "DllGetClassObject(CLSID_DirectSound8, IID_IClassFactory) failed: 0x%lx\n",hr);
102         if (pcf==0)
103                 goto error;
104
105         /* direct sound 8 doesn't have an IKsPropertySet */
106         hr = pcf->lpVtbl->CreateInstance(pcf, NULL, &IID_IKsPropertySet, (void **)(&pps));
107         ok(hr==E_NOINTERFACE, "CreateInstance(IID_IKsPropertySet) should have failed: 0x%lx\n",hr);
108
109         /* try direct sound capture next */
110         hr = (fProc)(&CLSID_DirectSoundCapture, &IID_IClassFactory, (void **)(&pcf));
111         ok(pcf!=0, "DllGetClassObject(CLSID_DirectSoundCapture, IID_IClassFactory) failed: 0x%lx\n",hr);
112         if (pcf==0)
113                 goto error;
114
115         /* direct sound capture doesn't have an IKsPropertySet */
116         hr = pcf->lpVtbl->CreateInstance(pcf, NULL, &IID_IKsPropertySet, (void **)(&pps));
117         ok(hr==E_NOINTERFACE, "CreateInstance(IID_IKsPropertySet) should have failed: 0x%lx\n",hr);
118
119         /* and the direct sound capture 8 version */
120         hr = (fProc)(&CLSID_DirectSoundCapture8, &IID_IClassFactory, (void **)(&pcf));
121         ok(pcf!=0, "DllGetClassObject(CLSID_DirectSoundCapture8, IID_IClassFactory) failed: 0x%lx\n",hr);
122         if (pcf==0)
123                 goto error;
124
125         /* direct sound capture 8 doesn't have an IKsPropertySet */
126         hr = pcf->lpVtbl->CreateInstance(pcf, NULL, &IID_IKsPropertySet, (void **)(&pps));
127         ok(hr==E_NOINTERFACE, "CreateInstance(IID_IKsPropertySet) should have failed: 0x%lx\n",hr);
128
129         /* try direct sound full duplex next */
130         hr = (fProc)(&CLSID_DirectSoundFullDuplex, &IID_IClassFactory, (void **)(&pcf));
131         ok(pcf!=0, "DllGetClassObject(CLSID_DirectSoundFullDuplex, IID_IClassFactory) failed: 0x%lx\n",hr);
132         if (pcf==0)
133                 goto error;
134
135         /* direct sound full duplex doesn't have an IKsPropertySet */
136         hr = pcf->lpVtbl->CreateInstance(pcf, NULL, &IID_IKsPropertySet, (void **)(&pps));
137         ok(hr==E_NOINTERFACE, "CreateInstance(IID_IKsPropertySet) should have failed: 0x%lx\n",hr);
138
139         /* try direct sound private last */
140         hr = (fProc)(&CLSID_DirectSoundPrivate, &IID_IClassFactory, (void **)(&pcf));
141         ok(pcf!=0, "DllGetClassObject(CLSID_DirectSoundPrivate, IID_IClassFactory) failed: 0x%lx\n",hr);
142         if (pcf==0)
143                 goto error;
144
145         /* direct sound private does have an IKsPropertySet */
146         hr = pcf->lpVtbl->CreateInstance(pcf, NULL, &IID_IKsPropertySet, (void **)(&pps));
147         ok(hr==DS_OK, "CreateInstance(IID_IKsPropertySet) failed: 0x%lx\n",hr);
148         if (hr!=DS_OK)
149                 goto error;
150
151         hr = pps->lpVtbl->QuerySupport(pps, &DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, &support);
152         ok(hr==DS_OK, "QuerySupport(DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION) failed: 0x%lx\n",hr);
153         if (hr!=DS_OK)
154                 goto error;
155
156         ok (support & KSPROPERTY_SUPPORT_GET, "Couldn't get DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION: support = 0x%lx\n",support);
157         ok (!(support & KSPROPERTY_SUPPORT_SET), "Shouldn't be able to set DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION: support = 0x%lx\n",support);
158
159         hr = pps->lpVtbl->QuerySupport(pps, &DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_WAVEDEVICEMAPPING, &support);
160         ok(hr==DS_OK, "QuerySupport(DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_WAVEDEVICEMAPPING) failed: 0x%lx\n",hr);
161         if (hr!=DS_OK)
162                 goto error;
163
164         ok (support & KSPROPERTY_SUPPORT_GET, "Couldn't get DSPROPERTY_DIRECTSOUNDDEVICE_WAVEDEVICEMAPPING: support = 0x%lx\n",support);
165         ok (!(support & KSPROPERTY_SUPPORT_SET), "Shouldn't be able to set DSPROPERTY_DIRECTSOUNDDEVICE_WAVEDEVICEMAPPING: support = 0x%lx\n",support);
166
167         hr = pps->lpVtbl->QuerySupport(pps, &DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_ENUMERATE, &support);
168         ok(hr==DS_OK, "QuerySupport(DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_ENUMERATE) failed: 0x%lx\n",hr);
169         if (hr!=DS_OK)
170                 goto error;
171
172         ok (support & KSPROPERTY_SUPPORT_GET, "Couldn't get DSPROPERTY_DIRECTSOUNDDEVICE_ENUMERATE: support = 0x%lx\n",support);
173         ok (!(support & KSPROPERTY_SUPPORT_SET), "Shouldn't be able to set DSPROPERTY_DIRECTSOUNDDEVICE_ENUMERATE: support = 0x%lx\n",support);
174
175         if (support & KSPROPERTY_SUPPORT_GET)
176         {
177                 DSPROPERTY_DIRECTSOUNDDEVICE_ENUMERATE_DATA data;
178                 ULONG bytes;
179
180                 data.Callback = callback;
181                 data.Context = 0;
182
183                 hr = pps->lpVtbl->Get(pps, &DSPROPSETID_DirectSoundDevice, 
184                                           DSPROPERTY_DIRECTSOUNDDEVICE_ENUMERATE,
185                                           NULL,
186                                           0,
187                                           &data, 
188                                           sizeof(data), 
189                                           &bytes);
190                 ok(hr==DS_OK, "Couldn't enumerate: 0x%lx\n",hr);
191         }
192
193 error:
194         FreeLibrary(hDsound);
195 }
196
197 static HWND get_hwnd()
198 {
199     HWND hwnd=GetForegroundWindow();
200     if (!hwnd)
201         hwnd=GetDesktopWindow();
202     return hwnd;
203 }
204                                                                                 
205 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
206                                    LPCSTR lpcstrModule, LPVOID lpContext)
207 {
208     HRESULT rc;
209     LPDIRECTSOUND dso=NULL;
210     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
211     DSBUFFERDESC bufdesc;
212     WAVEFORMATEX wfx;
213     int ref;
214                                                                                 
215     rc=DirectSoundCreate(lpGuid,&dso,NULL);
216     ok(rc==DS_OK,"DirectSoundCreate failed: 0x%lx\n",rc);
217     if (rc!=DS_OK)
218         goto EXIT;
219
220     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
221     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
222     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
223     ok(rc==DS_OK,"SetCooperativeLevel failed: 0x%lx\n",rc);
224     if (rc!=DS_OK)
225         goto EXIT;
226                                                                                 
227     /* Testing 3D buffers */
228     primary=NULL;
229     ZeroMemory(&bufdesc, sizeof(bufdesc));
230     bufdesc.dwSize=sizeof(bufdesc);
231     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_LOCHARDWARE|DSBCAPS_CTRL3D;
232     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
233     ok(rc==DS_OK&&primary!=NULL,"CreateSoundBuffer failed to create a hardware 3D primary buffer: 0x%lx\n",rc);
234     if (rc==DS_OK&&primary!=NULL) {
235         ZeroMemory(&wfx, sizeof(wfx));
236         wfx.wFormatTag=WAVE_FORMAT_PCM;
237         wfx.nChannels=1;
238         wfx.wBitsPerSample=16;
239         wfx.nSamplesPerSec=44100;
240         wfx.nBlockAlign=wfx.nChannels*wfx.wBitsPerSample/8;
241         wfx.nAvgBytesPerSec=wfx.nSamplesPerSec*wfx.nBlockAlign;
242         ZeroMemory(&bufdesc, sizeof(bufdesc));
243         bufdesc.dwSize=sizeof(bufdesc);
244         bufdesc.dwFlags=DSBCAPS_CTRLDEFAULT|DSBCAPS_GETCURRENTPOSITION2;
245         bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec;
246         bufdesc.lpwfxFormat=&wfx;
247         trace("  Testing a secondary buffer at %ldx%dx%d\n",
248             wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels);
249         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
250         ok(rc==DS_OK&&secondary!=NULL,"CreateSoundBuffer failed to create a secondary buffer 0x%lx\n",rc);
251         if (rc==DS_OK&&secondary!=NULL) {
252             IKsPropertySet * pPropertySet=NULL;
253             rc=IDirectSoundBuffer_QueryInterface(secondary,&IID_IKsPropertySet,(void **)&pPropertySet);
254             /* it's not an error for this to fail */
255             if(rc==DS_OK) {
256                 ULONG ulTypeSupport;
257                 trace("  Supports property sets\n");
258                 /* it's not an error for these to fail */
259                 rc=IKsPropertySet_QuerySupport(pPropertySet,&DSPROPSETID_VoiceManager,0,&ulTypeSupport);
260                 if((rc==DS_OK)&&(ulTypeSupport&(KSPROPERTY_SUPPORT_GET|KSPROPERTY_SUPPORT_SET)))
261                     trace("    DSPROPSETID_VoiceManager supported\n");
262                 else
263                     trace("    DSPROPSETID_VoiceManager not supported\n");
264                 rc=IKsPropertySet_QuerySupport(pPropertySet,&DSPROPSETID_EAX20_ListenerProperties,0,&ulTypeSupport);
265                 if((rc==DS_OK)&&(ulTypeSupport&(KSPROPERTY_SUPPORT_GET|KSPROPERTY_SUPPORT_SET)))
266                     trace("    DSPROPSETID_EAX20_ListenerProperties supported\n");
267                 else
268                     trace("    DSPROPSETID_EAX20_ListenerProperties not supported\n");
269                 rc=IKsPropertySet_QuerySupport(pPropertySet,&DSPROPSETID_EAX20_BufferProperties,0,&ulTypeSupport);
270                 if((rc==DS_OK)&&(ulTypeSupport&(KSPROPERTY_SUPPORT_GET|KSPROPERTY_SUPPORT_SET)))
271                     trace("    DSPROPSETID_EAX20_BufferProperties supported\n");
272                 else
273                     trace("    DSPROPSETID_EAX20_BufferProperties not supported\n");
274                 rc=IKsPropertySet_QuerySupport(pPropertySet,&DSPROPSETID_I3DL2_ListenerProperties,0,&ulTypeSupport);
275                 if((rc==DS_OK)&&(ulTypeSupport&(KSPROPERTY_SUPPORT_GET|KSPROPERTY_SUPPORT_SET)))
276                     trace("    DSPROPSETID_I3DL2_ListenerProperties supported\n");
277                 else
278                     trace("    DSPROPSETID_I3DL2_ListenerProperties not supported\n");
279                 rc=IKsPropertySet_QuerySupport(pPropertySet,&DSPROPSETID_I3DL2_BufferProperties,0,&ulTypeSupport);
280                 if((rc==DS_OK)&&(ulTypeSupport&(KSPROPERTY_SUPPORT_GET|KSPROPERTY_SUPPORT_SET)))
281                     trace("    DSPROPSETID_I3DL2_BufferProperties supported\n");
282                 else
283                     trace("    DSPROPSETID_I3DL2_BufferProperties not supported\n");
284                 rc=IKsPropertySet_QuerySupport(pPropertySet,&DSPROPSETID_ZOOMFX_BufferProperties,0,&ulTypeSupport);
285                 if((rc==DS_OK)&&(ulTypeSupport&(KSPROPERTY_SUPPORT_GET|KSPROPERTY_SUPPORT_SET)))
286                     trace("    DSPROPSETID_ZOOMFX_BufferProperties supported\n");
287                 else
288                     trace("    DSPROPSETID_ZOOMFX_BufferProperties not supported\n");
289                 ref=IKsPropertySet_Release(pPropertySet);
290                 /* try a few common ones */
291                 ok(ref==0,"IKsPropertySet_Release secondary has %d references, should have 0\n",ref);
292             } else
293                 trace("  Doesn't support property sets\n");
294             ref=IDirectSoundBuffer_Release(secondary);
295             ok(ref==0,"IDirectSoundBuffer_Release secondary has %d references, should have 0\n",ref);
296         }
297  
298         ref=IDirectSoundBuffer_Release(primary);
299         ok(ref==0,"IDirectSoundBuffer_Release primary has %d references, should have 0\n",ref);
300     }
301  
302 EXIT:
303     if (dso!=NULL) {
304         ref=IDirectSound_Release(dso);
305         ok(ref==0,"IDirectSound_Release has %d references, should have 0\n",ref);
306     }
307     return 1;
308 }
309  
310 static void propset_buffer_tests()
311 {
312     HRESULT rc;
313     rc=DirectSoundEnumerateA(&dsenum_callback,NULL);
314     ok(rc==DS_OK,"DirectSoundEnumerate failed: %ld\n",rc);
315 }
316
317 START_TEST(propset)
318 {
319     propset_private_tests();
320     propset_buffer_tests();
321 }