12e21561254078143d1cc2e124f539f7cae74dd2
[reactos.git] / modules / rostests / winetests / mmdevapi / mmdevenum.c
1 /*
2 * Copyright 2009 Maarten Lankhorst
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "wine/test.h"
20
21 #define COBJMACROS
22
23 #include "initguid.h"
24 #include "mmdeviceapi.h"
25 #include "audioclient.h"
26 #include "audiopolicy.h"
27 #include "dshow.h"
28 #include "dsound.h"
29 #include "devpkey.h"
30
31 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
32
33 /* Some of the QueryInterface tests are really just to check if I got the IIDs right :) */
34
35 /* IMMDeviceCollection appears to have no QueryInterface method and instead forwards to mme */
36 static void test_collection(IMMDeviceEnumerator *mme, IMMDeviceCollection *col)
37 {
38 IMMDeviceCollection *col2;
39 IMMDeviceEnumerator *mme2;
40 IUnknown *unk;
41 HRESULT hr;
42 ULONG ref;
43 UINT numdev;
44 IMMDevice *dev;
45
46 /* collection doesn't keep a ref on parent */
47 IMMDeviceEnumerator_AddRef(mme);
48 ref = IMMDeviceEnumerator_Release(mme);
49 ok(ref == 2, "Reference count on parent is %u\n", ref);
50
51 ref = IMMDeviceCollection_AddRef(col);
52 IMMDeviceCollection_Release(col);
53 ok(ref == 2, "Invalid reference count %u on collection\n", ref);
54
55 hr = IMMDeviceCollection_QueryInterface(col, &IID_IUnknown, NULL);
56 ok(hr == E_POINTER, "Null ppv returns %08x\n", hr);
57
58 hr = IMMDeviceCollection_QueryInterface(col, &IID_IUnknown, (void**)&unk);
59 ok(hr == S_OK, "Cannot query for IID_IUnknown: 0x%08x\n", hr);
60 if (hr == S_OK)
61 {
62 ok((IUnknown*)col == unk, "Pointers are not identical %p/%p/%p\n", col, unk, mme);
63 IUnknown_Release(unk);
64 }
65
66 hr = IMMDeviceCollection_QueryInterface(col, &IID_IMMDeviceCollection, (void**)&col2);
67 ok(hr == S_OK, "Cannot query for IID_IMMDeviceCollection: 0x%08x\n", hr);
68 if (hr == S_OK)
69 IMMDeviceCollection_Release(col2);
70
71 hr = IMMDeviceCollection_QueryInterface(col, &IID_IMMDeviceEnumerator, (void**)&mme2);
72 ok(hr == E_NOINTERFACE, "Query for IID_IMMDeviceEnumerator returned: 0x%08x\n", hr);
73 if (hr == S_OK)
74 IMMDeviceEnumerator_Release(mme2);
75
76 hr = IMMDeviceCollection_GetCount(col, NULL);
77 ok(hr == E_POINTER, "GetCount returned 0x%08x\n", hr);
78
79 hr = IMMDeviceCollection_GetCount(col, &numdev);
80 ok(hr == S_OK, "GetCount returned 0x%08x\n", hr);
81
82 dev = (void*)(LONG_PTR)0x12345678;
83 hr = IMMDeviceCollection_Item(col, numdev, &dev);
84 ok(hr == E_INVALIDARG, "Asking for too high device returned 0x%08x\n", hr);
85 ok(dev == NULL, "Returned non-null device\n");
86
87 if (numdev)
88 {
89 hr = IMMDeviceCollection_Item(col, 0, NULL);
90 ok(hr == E_POINTER, "Query with null pointer returned 0x%08x\n", hr);
91
92 hr = IMMDeviceCollection_Item(col, 0, &dev);
93 ok(hr == S_OK, "Valid Item returned 0x%08x\n", hr);
94 ok(dev != NULL, "Device is null!\n");
95 if (dev != NULL)
96 {
97 char temp[128];
98 WCHAR *id = NULL;
99 if (IMMDevice_GetId(dev, &id) == S_OK)
100 {
101 IMMDevice *dev2;
102
103 temp[sizeof(temp)-1] = 0;
104 WideCharToMultiByte(CP_ACP, 0, id, -1, temp, sizeof(temp)-1, NULL, NULL);
105 trace("Device found: %s\n", temp);
106
107 hr = IMMDeviceEnumerator_GetDevice(mme, id, &dev2);
108 ok(hr == S_OK, "GetDevice failed: %08x\n", hr);
109
110 IMMDevice_Release(dev2);
111
112 CoTaskMemFree(id);
113 }
114 }
115 if (dev)
116 IMMDevice_Release(dev);
117 }
118 IMMDeviceCollection_Release(col);
119 }
120
121 static HRESULT WINAPI notif_QueryInterface(IMMNotificationClient *iface,
122 const GUID *riid, void **obj)
123 {
124 ok(0, "Unexpected QueryInterface call\n");
125 return E_NOTIMPL;
126 }
127
128 static ULONG WINAPI notif_AddRef(IMMNotificationClient *iface)
129 {
130 ok(0, "Unexpected AddRef call\n");
131 return 2;
132 }
133
134 static ULONG WINAPI notif_Release(IMMNotificationClient *iface)
135 {
136 ok(0, "Unexpected Release call\n");
137 return 1;
138 }
139
140 static HRESULT WINAPI notif_OnDeviceStateChanged(IMMNotificationClient *iface,
141 const WCHAR *device_id, DWORD new_state)
142 {
143 ok(0, "Unexpected OnDeviceStateChanged call\n");
144 return E_NOTIMPL;
145 }
146
147 static HRESULT WINAPI notif_OnDeviceAdded(IMMNotificationClient *iface,
148 const WCHAR *device_id)
149 {
150 ok(0, "Unexpected OnDeviceAdded call\n");
151 return E_NOTIMPL;
152 }
153
154 static HRESULT WINAPI notif_OnDeviceRemoved(IMMNotificationClient *iface,
155 const WCHAR *device_id)
156 {
157 ok(0, "Unexpected OnDeviceRemoved call\n");
158 return E_NOTIMPL;
159 }
160
161 static HRESULT WINAPI notif_OnDefaultDeviceChanged(IMMNotificationClient *iface,
162 EDataFlow flow, ERole role, const WCHAR *device_id)
163 {
164 ok(0, "Unexpected OnDefaultDeviceChanged call\n");
165 return E_NOTIMPL;
166 }
167
168 static HRESULT WINAPI notif_OnPropertyValueChanged(IMMNotificationClient *iface,
169 const WCHAR *device_id, const PROPERTYKEY key)
170 {
171 ok(0, "Unexpected OnPropertyValueChanged call\n");
172 return E_NOTIMPL;
173 }
174
175 static IMMNotificationClientVtbl notif_vtbl = {
176 notif_QueryInterface,
177 notif_AddRef,
178 notif_Release,
179 notif_OnDeviceStateChanged,
180 notif_OnDeviceAdded,
181 notif_OnDeviceRemoved,
182 notif_OnDefaultDeviceChanged,
183 notif_OnPropertyValueChanged
184 };
185
186 static IMMNotificationClient notif = { &notif_vtbl };
187
188 /* Only do parameter tests here, the actual MMDevice testing should be a separate test */
189 START_TEST(mmdevenum)
190 {
191 static const WCHAR not_a_deviceW[] = {'n','o','t','a','d','e','v','i','c','e',0};
192
193 HRESULT hr;
194 IUnknown *unk = NULL;
195 IMMDeviceEnumerator *mme, *mme2;
196 ULONG ref;
197 IMMDeviceCollection *col;
198 IMMDevice *dev;
199
200 CoInitializeEx(NULL, COINIT_MULTITHREADED);
201 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
202 if (FAILED(hr))
203 {
204 skip("mmdevapi not available: 0x%08x\n", hr);
205 return;
206 }
207
208 /* Odd behavior.. bug? */
209 ref = IMMDeviceEnumerator_AddRef(mme);
210 ok(ref == 3, "Invalid reference count after incrementing: %u\n", ref);
211 IMMDeviceEnumerator_Release(mme);
212
213 hr = IMMDeviceEnumerator_QueryInterface(mme, &IID_IUnknown, (void**)&unk);
214 ok(hr == S_OK, "returned 0x%08x\n", hr);
215 if (hr != S_OK) return;
216
217 ok( (LONG_PTR)mme == (LONG_PTR)unk, "Pointers are unequal %p/%p\n", unk, mme);
218 IUnknown_Release(unk);
219
220 /* Proving that it is static.. */
221 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme2);
222 ok(hr == S_OK, "CoCreateInstance failed: 0x%08x\n", hr);
223 IMMDeviceEnumerator_Release(mme2);
224 ok(mme == mme2, "Pointers are not equal!\n");
225
226 hr = IMMDeviceEnumerator_QueryInterface(mme, &IID_IUnknown, NULL);
227 ok(hr == E_POINTER, "Null pointer on QueryInterface returned %08x\n", hr);
228
229 hr = IMMDeviceEnumerator_QueryInterface(mme, &GUID_NULL, (void**)&unk);
230 ok(!unk, "Unk not reset to null after invalid QI\n");
231 ok(hr == E_NOINTERFACE, "Invalid hr %08x returned on IID_NULL\n", hr);
232
233 hr = IMMDeviceEnumerator_GetDevice(mme, not_a_deviceW, NULL);
234 ok(hr == E_POINTER, "GetDevice gave wrong error: %08x\n", hr);
235
236 hr = IMMDeviceEnumerator_GetDevice(mme, NULL, &dev);
237 ok(hr == E_POINTER, "GetDevice gave wrong error: %08x\n", hr);
238
239 hr = IMMDeviceEnumerator_GetDevice(mme, not_a_deviceW, &dev);
240 ok(hr == E_INVALIDARG, "GetDevice gave wrong error: %08x\n", hr);
241
242 col = (void*)(LONG_PTR)0x12345678;
243 hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, 0xffff, DEVICE_STATEMASK_ALL, &col);
244 ok(hr == E_INVALIDARG, "Setting invalid data flow returned 0x%08x\n", hr);
245 ok(col == NULL, "Collection pointer non-null on failure\n");
246
247 hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL+1, &col);
248 ok(hr == E_INVALIDARG, "Setting invalid mask returned 0x%08x\n", hr);
249
250 hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL, NULL);
251 ok(hr == E_POINTER, "Invalid pointer returned: 0x%08x\n", hr);
252
253 hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL, &col);
254 ok(hr == S_OK, "Valid EnumAudioEndpoints returned 0x%08x\n", hr);
255 if (hr == S_OK)
256 {
257 ok(!!col, "Returned null pointer\n");
258 if (col)
259 test_collection(mme, col);
260 }
261
262 hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(mme, NULL);
263 ok(hr == E_POINTER, "RegisterEndpointNotificationCallback failed: %08x\n", hr);
264
265 hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(mme, &notif);
266 ok(hr == S_OK, "RegisterEndpointNotificationCallback failed: %08x\n", hr);
267
268 hr = IMMDeviceEnumerator_RegisterEndpointNotificationCallback(mme, &notif);
269 ok(hr == S_OK, "RegisterEndpointNotificationCallback failed: %08x\n", hr);
270
271 hr = IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(mme, NULL);
272 ok(hr == E_POINTER, "UnregisterEndpointNotificationCallback failed: %08x\n", hr);
273
274 hr = IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(mme, (IMMNotificationClient*)0xdeadbeef);
275 ok(hr == E_NOTFOUND, "UnregisterEndpointNotificationCallback failed: %08x\n", hr);
276
277 hr = IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(mme, &notif);
278 ok(hr == S_OK, "UnregisterEndpointNotificationCallback failed: %08x\n", hr);
279
280 hr = IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(mme, &notif);
281 ok(hr == S_OK, "UnregisterEndpointNotificationCallback failed: %08x\n", hr);
282
283 hr = IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(mme, &notif);
284 ok(hr == E_NOTFOUND, "UnregisterEndpointNotificationCallback failed: %08x\n", hr);
285
286 IMMDeviceEnumerator_Release(mme);
287 }