wined3d: Revert 4ba16b84a9e60ffcc98fc4b4c303c975589b59e2.
[wine] / dlls / quartz / tests / misc.c
1 /*
2  * Misc unit tests for Quartz
3  *
4  * Copyright (C) 2007 Google (Lei Zhang)
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22
23 #include "wine/test.h"
24 #include "dshow.h"
25
26 #define QI_SUCCEED(iface, riid, ppv) hr = IUnknown_QueryInterface(iface, &riid, (LPVOID*)&ppv); \
27     ok(hr == S_OK, "IUnknown_QueryInterface returned %x\n", hr); \
28     ok(ppv != NULL, "Pointer is NULL\n");
29
30 #define QI_FAIL(iface, riid, ppv) hr = IUnknown_QueryInterface(iface, &riid, (LPVOID*)&ppv); \
31     ok(hr == E_NOINTERFACE, "IUnknown_QueryInterface returned %x\n", hr); \
32     ok(ppv == NULL, "Pointer is %p\n", ppv);
33
34 #define ADDREF_EXPECT(iface, num) if (iface) { \
35     refCount = IUnknown_AddRef(iface); \
36     ok(refCount == num, "IUnknown_AddRef should return %d, got %d\n", num, refCount); \
37 }
38
39 #define RELEASE_EXPECT(iface, num) if (iface) { \
40     refCount = IUnknown_Release(iface); \
41     ok(refCount == num, "IUnknown_Release should return %d, got %d\n", num, refCount); \
42 }
43
44 static void test_aggregation(const CLSID clsidOuter, const CLSID clsidInner,
45                              const IID iidOuter, const IID iidInner)
46 {
47     HRESULT hr;
48     ULONG refCount;
49     IUnknown *pUnkOuter = NULL;
50     IUnknown *pUnkInner = NULL;
51     IUnknown *pUnkInnerFail = NULL;
52     IUnknown *pUnkOuterTest = NULL;
53     IUnknown *pUnkInnerTest = NULL;
54     IUnknown *pUnkAggregatee = NULL;
55     IUnknown *pUnkAggregator = NULL;
56     IUnknown *pUnkTest = NULL;
57
58     hr = CoCreateInstance(&clsidOuter, NULL, CLSCTX_INPROC_SERVER,
59                           &IID_IUnknown, (LPVOID*)&pUnkOuter);
60     ok(hr == S_OK, "CoCreateInstance failed with %x\n", hr);
61     ok(pUnkOuter != NULL, "pUnkOuter is NULL\n");
62
63     if (!pUnkOuter)
64     {
65         skip("pUnkOuter is NULL\n");
66         return;
67     }
68
69     /* for aggregation, we should only be able to request IUnknown */
70     hr = CoCreateInstance(&clsidInner, pUnkOuter, CLSCTX_INPROC_SERVER,
71                           &iidInner, (LPVOID*)&pUnkInnerFail);
72     ok(hr == E_NOINTERFACE, "CoCreateInstance returned %x\n", hr);
73     ok(pUnkInnerFail == NULL, "pUnkInnerFail is not NULL\n");
74
75     /* aggregation, request IUnknown */
76     hr = CoCreateInstance(&clsidInner, pUnkOuter, CLSCTX_INPROC_SERVER,
77                           &IID_IUnknown, (LPVOID*)&pUnkInner);
78     ok(hr == S_OK, "CoCreateInstance returned %x\n", hr);
79     ok(pUnkInner != NULL, "pUnkInner is NULL\n");
80
81     if (!pUnkInner)
82     {
83         skip("pUnkInner is NULL\n");
84         return;
85     }
86
87     ADDREF_EXPECT(pUnkOuter, 2);
88     ADDREF_EXPECT(pUnkInner, 2);
89     RELEASE_EXPECT(pUnkOuter, 1);
90     RELEASE_EXPECT(pUnkInner, 1);
91
92     QI_FAIL(pUnkOuter, iidInner, pUnkAggregatee);
93     QI_FAIL(pUnkInner, iidOuter, pUnkAggregator);
94
95     /* these QueryInterface calls should work */
96     QI_SUCCEED(pUnkOuter, iidOuter, pUnkAggregator);
97     QI_SUCCEED(pUnkOuter, IID_IUnknown, pUnkOuterTest);
98     /* IGraphConfig interface comes with DirectShow 9 */
99     if(IsEqualGUID(&IID_IGraphConfig, &iidInner))
100     {
101         hr = IUnknown_QueryInterface(pUnkInner, &iidInner, (LPVOID*)&pUnkAggregatee);
102         ok(hr == S_OK || broken(hr == E_NOINTERFACE), "IUnknown_QueryInterface returned %x\n", hr);
103         ok(pUnkAggregatee != NULL || broken(!pUnkAggregatee), "Pointer is NULL\n");
104     }
105     else
106     {
107         QI_SUCCEED(pUnkInner, iidInner, pUnkAggregatee);
108     }
109     QI_SUCCEED(pUnkInner, IID_IUnknown, pUnkInnerTest);
110
111     if (!pUnkAggregator || !pUnkOuterTest || !pUnkAggregatee
112                     || !pUnkInnerTest)
113     {
114         skip("One of the required interfaces is NULL\n");
115         return;
116     }
117
118     ADDREF_EXPECT(pUnkAggregator, 5);
119     ADDREF_EXPECT(pUnkOuterTest, 6);
120     ADDREF_EXPECT(pUnkAggregatee, 7);
121     ADDREF_EXPECT(pUnkInnerTest, 3);
122     RELEASE_EXPECT(pUnkAggregator, 6);
123     RELEASE_EXPECT(pUnkOuterTest, 5);
124     RELEASE_EXPECT(pUnkAggregatee, 4);
125     RELEASE_EXPECT(pUnkInnerTest, 2);
126
127     QI_SUCCEED(pUnkAggregator, IID_IUnknown, pUnkTest);
128     QI_SUCCEED(pUnkOuterTest, IID_IUnknown, pUnkTest);
129     QI_SUCCEED(pUnkAggregatee, IID_IUnknown, pUnkTest);
130     QI_SUCCEED(pUnkInnerTest, IID_IUnknown, pUnkTest);
131
132     QI_FAIL(pUnkAggregator, iidInner, pUnkTest);
133     QI_FAIL(pUnkOuterTest, iidInner, pUnkTest);
134     QI_FAIL(pUnkAggregatee, iidInner, pUnkTest);
135     QI_SUCCEED(pUnkInnerTest, iidInner, pUnkTest);
136
137     QI_SUCCEED(pUnkAggregator, iidOuter, pUnkTest);
138     QI_SUCCEED(pUnkOuterTest, iidOuter, pUnkTest);
139     QI_SUCCEED(pUnkAggregatee, iidOuter, pUnkTest);
140     QI_FAIL(pUnkInnerTest, iidOuter, pUnkTest);
141
142     RELEASE_EXPECT(pUnkAggregator, 10);
143     RELEASE_EXPECT(pUnkOuterTest, 9);
144     RELEASE_EXPECT(pUnkAggregatee, 8);
145     RELEASE_EXPECT(pUnkInnerTest, 2);
146     RELEASE_EXPECT(pUnkOuter, 7);
147     RELEASE_EXPECT(pUnkInner, 1);
148
149     do
150     {
151         refCount = IUnknown_Release(pUnkInner);
152     } while (refCount);
153
154     do
155     {
156         refCount = IUnknown_Release(pUnkOuter);
157     } while (refCount);
158 }
159
160 static void test_video_renderer_aggregations(void)
161 {
162     const IID * iids[] = {
163         &IID_IMediaFilter, &IID_IBaseFilter, &IID_IBasicVideo, &IID_IVideoWindow
164     };
165     int i;
166
167     for (i = 0; i < sizeof(iids) / sizeof(iids[0]); i++)
168     {
169         test_aggregation(CLSID_SystemClock, CLSID_VideoRenderer,
170                          IID_IReferenceClock, *iids[i]);
171     }
172 }
173
174 static void test_filter_graph_aggregations(void)
175 {
176     const IID * iids[] = {
177         &IID_IFilterGraph2, &IID_IMediaControl, &IID_IGraphBuilder,
178         &IID_IFilterGraph, &IID_IMediaSeeking, &IID_IBasicAudio, &IID_IBasicVideo,
179         &IID_IVideoWindow, &IID_IMediaEventEx, &IID_IMediaFilter,
180         &IID_IMediaEventSink, &IID_IGraphConfig, &IID_IMediaPosition
181     };
182     int i;
183
184     for (i = 0; i < sizeof(iids) / sizeof(iids[0]); i++)
185     {
186         test_aggregation(CLSID_SystemClock, CLSID_FilterGraph,
187                          IID_IReferenceClock, *iids[i]);
188     }
189 }
190
191 static void test_filter_mapper_aggregations(void)
192 {
193     const IID * iids[] = {
194         &IID_IFilterMapper2, &IID_IFilterMapper
195     };
196     int i;
197
198     for (i = 0; i < sizeof(iids) / sizeof(iids[0]); i++)
199     {
200         test_aggregation(CLSID_SystemClock, CLSID_FilterMapper2,
201                          IID_IReferenceClock, *iids[i]);
202     }
203 }
204
205 START_TEST(misc)
206 {
207     CoInitialize(NULL);
208
209     test_video_renderer_aggregations();
210     test_filter_graph_aggregations();
211     test_filter_mapper_aggregations();
212
213     CoUninitialize();
214 }