shlwapi: Don't cast WCHAR string to BSTR.
[wine/testsucceed.git] / dlls / mmdevapi / tests / mmdevenum.c
blob014cbf3de4ab1e00914478a916b6c94848f955d5
1 /*
2 * Copyright 2009 Maarten Lankhorst
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.
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.
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
19 #include "wine/test.h"
21 #define CINTERFACE
22 #define COBJMACROS
24 #include "initguid.h"
25 #include "mmdeviceapi.h"
26 #include "audioclient.h"
27 #include "dshow.h"
28 #include "dsound.h"
30 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
32 /* Some of the QueryInterface tests are really just to check if I got the IID's right :) */
34 /* IMMDeviceCollection appears to have no QueryInterface method and instead forwards to mme */
35 static void test_collection(IMMDeviceEnumerator *mme, IMMDeviceCollection *col)
37 IMMDeviceCollection *col2;
38 IMMDeviceEnumerator *mme2;
39 IUnknown *unk;
40 HRESULT hr;
41 ULONG ref;
42 UINT numdev;
43 IMMDevice *dev;
45 /* collection doesn't keep a ref on parent */
46 IUnknown_AddRef(mme);
47 ref = IUnknown_Release(mme);
48 ok(ref == 2, "Reference count on parent is %u\n", ref);
50 ref = IUnknown_AddRef(col);
51 IUnknown_Release(col);
52 ok(ref == 2, "Invalid reference count %u on collection\n", ref);
54 hr = IUnknown_QueryInterface(col, &IID_IUnknown, NULL);
55 ok(hr == E_POINTER, "Null ppv returns %08x\n", hr);
57 hr = IUnknown_QueryInterface(col, &IID_IUnknown, (void**)&unk);
58 ok(hr == S_OK, "Cannot query for IID_IUnknown: 0x%08x\n", hr);
59 if (hr == S_OK)
61 ok((LONG_PTR)col == (LONG_PTR)unk, "Pointers are not identical %p/%p/%p\n", col, unk, mme);
62 IUnknown_Release(unk);
65 hr = IUnknown_QueryInterface(col, &IID_IMMDeviceCollection, (void**)&col2);
66 ok(hr == S_OK, "Cannot query for IID_IMMDeviceCollection: 0x%08x\n", hr);
67 if (hr == S_OK)
68 IUnknown_Release(col2);
70 hr = IUnknown_QueryInterface(col, &IID_IMMDeviceEnumerator, (void**)&mme2);
71 ok(hr == E_NOINTERFACE, "Query for IID_IMMDeviceEnumerator returned: 0x%08x\n", hr);
72 if (hr == S_OK)
73 IUnknown_Release(mme2);
75 hr = IMMDeviceCollection_GetCount(col, NULL);
76 ok(hr == E_POINTER, "GetCount returned 0x%08x\n", hr);
78 hr = IMMDeviceCollection_GetCount(col, &numdev);
79 ok(hr == S_OK, "GetCount returned 0x%08x\n", hr);
81 dev = (void*)(LONG_PTR)0x12345678;
82 hr = IMMDeviceCollection_Item(col, numdev, &dev);
83 ok(hr == E_INVALIDARG, "Asking for too high device returned 0x%08x\n", hr);
84 ok(dev == NULL, "Returned non-null device\n");
86 if (numdev)
88 hr = IMMDeviceCollection_Item(col, 0, NULL);
89 ok(hr == E_POINTER, "Query with null pointer returned 0x%08x\n", hr);
91 hr = IMMDeviceCollection_Item(col, 0, &dev);
92 ok(hr == S_OK, "Valid Item returned 0x%08x\n", hr);
93 ok(dev != NULL, "Device is null!\n");
94 if (dev != NULL)
96 char temp[128];
97 WCHAR *id = NULL;
98 if (IMMDevice_GetId(dev, &id) == S_OK)
100 temp[sizeof(temp)-1] = 0;
101 WideCharToMultiByte(CP_ACP, 0, id, -1, temp, sizeof(temp)-1, NULL, NULL);
102 trace("Device found: %s\n", temp);
103 CoTaskMemFree(id);
106 if (dev)
107 IUnknown_Release(dev);
109 IUnknown_Release(col);
112 /* Only do parameter tests here, the actual MMDevice testing should be a separate test */
113 START_TEST(mmdevenum)
115 HRESULT hr;
116 IUnknown *unk = NULL;
117 IMMDeviceEnumerator *mme, *mme2;
118 ULONG ref;
119 IMMDeviceCollection *col;
121 CoInitializeEx(NULL, COINIT_MULTITHREADED);
122 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
123 if (FAILED(hr))
125 skip("mmdevapi not available: 0x%08x\n", hr);
126 return;
129 /* Odd behavior.. bug? */
130 ref = IUnknown_AddRef(mme);
131 ok(ref == 3, "Invalid reference count after incrementing: %u\n", ref);
132 IUnknown_Release(mme);
134 hr = IUnknown_QueryInterface(mme, &IID_IUnknown, (void**)&unk);
135 ok(hr == S_OK, "returned 0x%08x\n", hr);
136 if (hr != S_OK) return;
138 ok( (LONG_PTR)mme == (LONG_PTR)unk, "Pointers are unequal %p/%p\n", unk, mme);
139 IUnknown_Release(unk);
141 /* Proving that it is static.. */
142 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme2);
143 IUnknown_Release(mme2);
144 ok(mme == mme2, "Pointers are not equal!\n");
146 hr = IUnknown_QueryInterface(mme, &IID_IUnknown, NULL);
147 ok(hr == E_POINTER, "Null pointer on QueryInterface returned %08x\n", hr);
149 hr = IUnknown_QueryInterface(mme, &GUID_NULL, (void**)&unk);
150 ok(!unk, "Unk not reset to null after invalid QI\n");
151 ok(hr == E_NOINTERFACE, "Invalid hr %08x returned on IID_NULL\n", hr);
153 col = (void*)(LONG_PTR)0x12345678;
154 hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, 0xffff, DEVICE_STATEMASK_ALL, &col);
155 ok(hr == E_INVALIDARG, "Setting invalid data flow returned 0x%08x\n", hr);
156 ok(col == NULL, "Collection pointer non-null on failure\n");
158 hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL+1, &col);
159 ok(hr == E_INVALIDARG, "Setting invalid mask returned 0x%08x\n", hr);
161 hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL, NULL);
162 ok(hr == E_POINTER, "Invalid pointer returned: 0x%08x\n", hr);
164 hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL, &col);
165 ok(hr == S_OK, "Valid EnumAudioEndpoints returned 0x%08x\n", hr);
166 if (hr == S_OK)
168 ok(!!col, "Returned null pointer\n");
169 if (col)
170 test_collection(mme, col);
173 IUnknown_Release(mme);