wininet: Keep handles invalid but reserved in InternetCloseHandle.
[wine] / dlls / mmdevapi / tests / 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 "dshow.h"
27 #include "dsound.h"
28 #include "devpkey.h"
29
30 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
31
32 /* Some of the QueryInterface tests are really just to check if I got the IID's right :) */
33
34 /* IMMDeviceCollection appears to have no QueryInterface method and instead forwards to mme */
35 static void test_collection(IMMDeviceEnumerator *mme, IMMDeviceCollection *col)
36 {
37     IMMDeviceCollection *col2;
38     IMMDeviceEnumerator *mme2;
39     IUnknown *unk;
40     HRESULT hr;
41     ULONG ref;
42     UINT numdev;
43     IMMDevice *dev;
44
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);
49
50     ref = IUnknown_AddRef(col);
51     IUnknown_Release(col);
52     ok(ref == 2, "Invalid reference count %u on collection\n", ref);
53
54     hr = IUnknown_QueryInterface(col, &IID_IUnknown, NULL);
55     ok(hr == E_POINTER, "Null ppv returns %08x\n", hr);
56
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)
60     {
61         ok((LONG_PTR)col == (LONG_PTR)unk, "Pointers are not identical %p/%p/%p\n", col, unk, mme);
62         IUnknown_Release(unk);
63     }
64
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);
69
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);
74
75     hr = IMMDeviceCollection_GetCount(col, NULL);
76     ok(hr == E_POINTER, "GetCount returned 0x%08x\n", hr);
77
78     hr = IMMDeviceCollection_GetCount(col, &numdev);
79     ok(hr == S_OK, "GetCount returned 0x%08x\n", hr);
80
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");
85
86     if (numdev)
87     {
88         hr = IMMDeviceCollection_Item(col, 0, NULL);
89         ok(hr == E_POINTER, "Query with null pointer returned 0x%08x\n", hr);
90
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)
95         {
96             char temp[128];
97             WCHAR *id = NULL;
98             if (IMMDevice_GetId(dev, &id) == S_OK)
99             {
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);
104             }
105         }
106         if (dev)
107             IUnknown_Release(dev);
108     }
109     IUnknown_Release(col);
110 }
111
112 /* Only do parameter tests here, the actual MMDevice testing should be a separate test */
113 START_TEST(mmdevenum)
114 {
115     HRESULT hr;
116     IUnknown *unk = NULL;
117     IMMDeviceEnumerator *mme, *mme2;
118     ULONG ref;
119     IMMDeviceCollection *col;
120
121     CoInitializeEx(NULL, COINIT_MULTITHREADED);
122     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&mme);
123     if (FAILED(hr))
124     {
125         skip("mmdevapi not available: 0x%08x\n", hr);
126         return;
127     }
128
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);
133
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;
137
138     ok( (LONG_PTR)mme == (LONG_PTR)unk, "Pointers are unequal %p/%p\n", unk, mme);
139     IUnknown_Release(unk);
140
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");
145
146     hr = IUnknown_QueryInterface(mme, &IID_IUnknown, NULL);
147     ok(hr == E_POINTER, "Null pointer on QueryInterface returned %08x\n", hr);
148
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);
152
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");
157
158     hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL+1, &col);
159     ok(hr == E_INVALIDARG, "Setting invalid mask returned 0x%08x\n", hr);
160
161     hr = IMMDeviceEnumerator_EnumAudioEndpoints(mme, eAll, DEVICE_STATEMASK_ALL, NULL);
162     ok(hr == E_POINTER, "Invalid pointer returned: 0x%08x\n", hr);
163
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)
167     {
168         ok(!!col, "Returned null pointer\n");
169         if (col)
170             test_collection(mme, col);
171     }
172
173     IUnknown_Release(mme);
174 }