dinput8: DirectInput8Create rewrite.
[wine] / dlls / ole32 / tests / compobj.c
1 /*
2  * Component Object Tests
3  *
4  * Copyright 2005 Robert Shearman
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 <stdarg.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28 #include "shlguid.h"
29
30 #include "wine/test.h"
31
32 /* functions that are not present on all versions of Windows */
33 HRESULT (WINAPI * pCoInitializeEx)(LPVOID lpReserved, DWORD dwCoInit);
34
35 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08lx\n", hr)
36
37 static const CLSID CLSID_non_existent =   { 0x12345678, 0x1234, 0x1234, { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 } };
38 static const CLSID CLSID_CDeviceMoniker = { 0x4315d437, 0x5b8c, 0x11d0, { 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86 } };
39 static const WCHAR devicedotone[] = {'d','e','v','i','c','e','.','1',0};
40 static const WCHAR wszCLSID_CDeviceMoniker[] =
41 {
42     '{',
43     '4','3','1','5','d','4','3','7','-',
44     '5','b','8','c','-',
45     '1','1','d','0','-',
46     'b','d','3','b','-',
47     '0','0','a','0','c','9','1','1','c','e','8','6',
48     '}',0
49 };
50
51 static void test_ProgIDFromCLSID(void)
52 {
53     LPWSTR progid;
54     HRESULT hr = ProgIDFromCLSID(&CLSID_CDeviceMoniker, &progid);
55     ok(hr == S_OK, "ProgIDFromCLSID failed with error 0x%08lx\n", hr);
56     if (hr == S_OK)
57     {
58         ok(!lstrcmpiW(progid, devicedotone), "Didn't get expected prog ID\n");
59         CoTaskMemFree(progid);
60     }
61
62     progid = (LPWSTR)0xdeadbeef;
63     hr = ProgIDFromCLSID(&CLSID_non_existent, &progid);
64     ok(hr == REGDB_E_CLASSNOTREG, "ProgIDFromCLSID returned %08lx\n", hr);
65     ok(progid == NULL, "ProgIDFromCLSID returns with progid %p\n", progid);
66 }
67
68 static void test_CLSIDFromProgID(void)
69 {
70     CLSID clsid;
71     HRESULT hr = CLSIDFromProgID(devicedotone, &clsid);
72     ok(hr == S_OK, "CLSIDFromProgID failed with error 0x%08lx\n", hr);
73     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
74
75     hr = CLSIDFromString((LPOLESTR)devicedotone, &clsid);
76     ok_ole_success(hr, "CLSIDFromString");
77     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
78 }
79
80 static void test_CLSIDFromString(void)
81 {
82     CLSID clsid;
83     HRESULT hr = CLSIDFromString((LPOLESTR)wszCLSID_CDeviceMoniker, &clsid);
84     ok_ole_success(hr, "CLSIDFromString");
85     ok(IsEqualCLSID(&clsid, &CLSID_CDeviceMoniker), "clsid wasn't equal to CLSID_CDeviceMoniker\n");
86 }
87
88 static void test_CoCreateInstance(void)
89 {
90     REFCLSID rclsid = &CLSID_MyComputer;
91     IUnknown *pUnk = (IUnknown *)0xdeadbeef;
92     HRESULT hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
93     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08lx\n", hr);
94     ok(pUnk == NULL, "CoCreateInstance should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
95
96     OleInitialize(NULL);
97     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
98     ok_ole_success(hr, "CoCreateInstance");
99     IUnknown_Release(pUnk);
100     OleUninitialize();
101
102     hr = CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown, (void **)&pUnk);
103     ok(hr == CO_E_NOTINITIALIZED, "CoCreateInstance should have returned CO_E_NOTINITIALIZED instead of 0x%08lx\n", hr);
104 }
105
106 static void test_CoGetClassObject(void)
107 {
108     IUnknown *pUnk = (IUnknown *)0xdeadbeef;
109     HRESULT hr = CoGetClassObject(&CLSID_MyComputer, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void **)&pUnk);
110     ok(hr == CO_E_NOTINITIALIZED, "CoGetClassObject should have returned CO_E_NOTINITIALIZED instead of 0x%08lx\n", hr);
111     ok(pUnk == NULL, "CoGetClassObject should have changed the passed in pointer to NULL, instead of %p\n", pUnk);
112
113     hr = CoGetClassObject(&CLSID_MyComputer, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, NULL);
114     ok(hr == E_INVALIDARG, "CoGetClassObject should have returned E_INVALIDARG instead of 0x%08lx\n", hr);
115 }
116
117 static ATOM register_dummy_class(void)
118 {
119     WNDCLASS wc =
120     {
121         0,
122         DefWindowProc,
123         0,
124         0,
125         GetModuleHandle(NULL),
126         NULL,
127         LoadCursor(NULL, IDC_ARROW),
128         (HBRUSH)(COLOR_BTNFACE+1),
129         NULL,
130         TEXT("WineOleTestClass"),
131     };
132     
133     return RegisterClass(&wc);
134 }
135
136 static void test_ole_menu(void)
137 {
138         HWND hwndFrame;
139         HRESULT hr;
140
141         hwndFrame = CreateWindow(MAKEINTATOM(register_dummy_class()), "Test", 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
142         hr = OleSetMenuDescriptor(NULL, hwndFrame, NULL, NULL, NULL);
143         todo_wine ok_ole_success(hr, "OleSetMenuDescriptor");
144
145         DestroyWindow(hwndFrame);
146 }
147
148
149 static HRESULT WINAPI MessageFilter_QueryInterface(IMessageFilter *iface, REFIID riid, void ** ppvObj)
150 {
151     if (ppvObj == NULL) return E_POINTER;
152
153     if (IsEqualGUID(riid, &IID_IUnknown) ||
154         IsEqualGUID(riid, &IID_IClassFactory))
155     {
156         *ppvObj = (LPVOID)iface;
157         IMessageFilter_AddRef(iface);
158         return S_OK;
159     }
160
161     return E_NOINTERFACE;
162 }
163
164 static ULONG WINAPI MessageFilter_AddRef(IMessageFilter *iface)
165 {
166     return 2; /* non-heap object */
167 }
168
169 static ULONG WINAPI MessageFilter_Release(IMessageFilter *iface)
170 {
171     return 1; /* non-heap object */
172 }
173
174 static DWORD WINAPI MessageFilter_HandleInComingCall(
175   IMessageFilter *iface,
176   DWORD dwCallType,
177   HTASK threadIDCaller,
178   DWORD dwTickCount,
179   LPINTERFACEINFO lpInterfaceInfo)
180 {
181     trace("HandleInComingCall\n");
182     return SERVERCALL_ISHANDLED;
183 }
184
185 static DWORD WINAPI MessageFilter_RetryRejectedCall(
186   IMessageFilter *iface,
187   HTASK threadIDCallee,
188   DWORD dwTickCount,
189   DWORD dwRejectType)
190 {
191     trace("RetryRejectedCall\n");
192     return 0;
193 }
194
195 static DWORD WINAPI MessageFilter_MessagePending(
196   IMessageFilter *iface,
197   HTASK threadIDCallee,
198   DWORD dwTickCount,
199   DWORD dwPendingType)
200 {
201     trace("MessagePending\n");
202     return PENDINGMSG_WAITNOPROCESS;
203 }
204
205 static const IMessageFilterVtbl MessageFilter_Vtbl =
206 {
207     MessageFilter_QueryInterface,
208     MessageFilter_AddRef,
209     MessageFilter_Release,
210     MessageFilter_HandleInComingCall,
211     MessageFilter_RetryRejectedCall,
212     MessageFilter_MessagePending
213 };
214
215 static IMessageFilter MessageFilter = { &MessageFilter_Vtbl };
216
217 static void test_CoRegisterMessageFilter(void)
218 {
219     HRESULT hr;
220     IMessageFilter *prev_filter;
221
222 #if 0 /* crashes without an apartment! */
223     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
224 #endif
225
226     pCoInitializeEx(NULL, COINIT_MULTITHREADED);
227     prev_filter = (IMessageFilter *)0xdeadbeef;
228     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
229     ok(hr == CO_E_NOT_SUPPORTED,
230         "CoRegisterMessageFilter should have failed with CO_E_NOT_SUPPORTED instead of 0x%08lx\n",
231         hr);
232     ok(prev_filter == (IMessageFilter *)0xdeadbeef,
233         "prev_filter should have been set to %p\n", prev_filter);
234     CoUninitialize();
235
236     pCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
237
238     hr = CoRegisterMessageFilter(NULL, NULL);
239     ok_ole_success(hr, "CoRegisterMessageFilter");
240
241     prev_filter = (IMessageFilter *)0xdeadbeef;
242     hr = CoRegisterMessageFilter(NULL, &prev_filter);
243     ok_ole_success(hr, "CoRegisterMessageFilter");
244     ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
245
246     hr = CoRegisterMessageFilter(&MessageFilter, &prev_filter);
247     ok_ole_success(hr, "CoRegisterMessageFilter");
248     ok(prev_filter == NULL, "prev_filter should have been set to NULL instead of %p\n", prev_filter);
249
250     hr = CoRegisterMessageFilter(NULL, NULL);
251     ok_ole_success(hr, "CoRegisterMessageFilter");
252
253     CoUninitialize();
254 }
255
256 START_TEST(compobj)
257 {
258     HMODULE hOle32 = GetModuleHandle("ole32");
259     if (!(pCoInitializeEx = (void*)GetProcAddress(hOle32, "CoInitializeEx")))
260     {
261         trace("You need DCOM95 installed to run this test\n");
262         return;
263     }
264
265     test_ProgIDFromCLSID();
266     test_CLSIDFromProgID();
267     test_CLSIDFromString();
268     test_CoCreateInstance();
269     test_ole_menu();
270     test_CoGetClassObject();
271     test_CoRegisterMessageFilter();
272 }