ole32: Remove extraneous parentheses (LLVM/Clang).
[wine] / dlls / ole32 / tests / dragdrop.c
1 /*
2  * Drag and Drop Tests
3  *
4  * Copyright 2007 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 _WIN32_DCOM
22 #define COBJMACROS
23 #define CONST_VTABLE
24
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "objbase.h"
31
32 #include "wine/test.h"
33
34 static int droptarget_refs;
35
36 /* helper macros to make tests a bit leaner */
37 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
38
39 static HRESULT WINAPI DropTarget_QueryInterface(IDropTarget* iface, REFIID riid,
40                                                 void** ppvObject)
41 {
42     trace("DropTarget_QueryInterface\n");
43     if (IsEqualIID(riid, &IID_IUnknown) ||
44         IsEqualIID(riid, &IID_IDropTarget))
45     {
46         IUnknown_AddRef(iface);
47         *ppvObject = iface;
48         return S_OK;
49     }
50     *ppvObject = NULL;
51     return E_NOINTERFACE;
52 }
53
54 static ULONG WINAPI DropTarget_AddRef(IDropTarget* iface)
55 {
56     droptarget_refs++;
57     return droptarget_refs;
58 }
59
60 static ULONG WINAPI DropTarget_Release(IDropTarget* iface)
61 {
62     droptarget_refs--;
63     return droptarget_refs;
64 }
65
66 static HRESULT WINAPI DropTarget_DragEnter(IDropTarget* iface,
67                                            IDataObject* pDataObj,
68                                            DWORD grfKeyState, POINTL pt,
69                                            DWORD* pdwEffect)
70 {
71     return E_NOTIMPL;
72 }
73
74 static HRESULT WINAPI DropTarget_DragOver(IDropTarget* iface,
75                                           DWORD grfKeyState,
76                                           POINTL pt,
77                                           DWORD* pdwEffect)
78 {
79     return E_NOTIMPL;
80 }
81
82 static HRESULT WINAPI DropTarget_DragLeave(IDropTarget* iface)
83 {
84     return E_NOTIMPL;
85 }
86
87 static HRESULT WINAPI DropTarget_Drop(IDropTarget* iface,
88                                       IDataObject* pDataObj, DWORD grfKeyState,
89                                       POINTL pt, DWORD* pdwEffect)
90 {
91     return E_NOTIMPL;
92 }
93
94 static const IDropTargetVtbl DropTarget_VTbl =
95 {
96     DropTarget_QueryInterface,
97     DropTarget_AddRef,
98     DropTarget_Release,
99     DropTarget_DragEnter,
100     DropTarget_DragOver,
101     DropTarget_DragLeave,
102     DropTarget_Drop
103 };
104
105 static IDropTarget DropTarget = { &DropTarget_VTbl };
106
107 /** stub IDropSource **/
108 static HRESULT WINAPI DropSource_QueryInterface(IDropSource *iface, REFIID riid, void **ppObj)
109 {
110     if (IsEqualIID(riid, &IID_IUnknown) ||
111         IsEqualIID(riid, &IID_IDropSource))
112     {
113         *ppObj = iface;
114         IDropSource_AddRef(iface);
115         return S_OK;
116     }
117     return E_NOINTERFACE;
118 }
119
120 static ULONG WINAPI DropSource_AddRef(IDropSource *iface)
121 {
122     return 2;
123 }
124
125 static ULONG WINAPI DropSource_Release(IDropSource *iface)
126 {
127     return 1;
128 }
129
130 static HRESULT WINAPI DropSource_QueryContinueDrag(
131     IDropSource *iface,
132     BOOL fEscapePressed,
133     DWORD grfKeyState)
134 {
135     /* always drop */
136     return DRAGDROP_S_DROP;
137 }
138
139 static HRESULT WINAPI DropSource_GiveFeedback(
140     IDropSource *iface,
141     DWORD dwEffect)
142 {
143     return DRAGDROP_S_USEDEFAULTCURSORS;
144 }
145
146 static const IDropSourceVtbl dropsource_vtbl = {
147     DropSource_QueryInterface,
148     DropSource_AddRef,
149     DropSource_Release,
150     DropSource_QueryContinueDrag,
151     DropSource_GiveFeedback
152 };
153
154 static IDropSource DropSource = { &dropsource_vtbl };
155
156 /** IDataObject stub **/
157 static HRESULT WINAPI DataObject_QueryInterface(
158     IDataObject *iface,
159     REFIID riid,
160     void **pObj)
161 {
162     if (IsEqualIID(riid, &IID_IUnknown) ||
163         IsEqualIID(riid, &IID_IDataObject))
164     {
165         *pObj = iface;
166         IDataObject_AddRef(iface);
167         return S_OK;
168     }
169     return E_NOINTERFACE;
170 }
171
172 static ULONG WINAPI DataObject_AddRef(IDataObject *iface)
173 {
174     return 2;
175 }
176
177 static ULONG WINAPI DataObject_Release(IDataObject *iface)
178 {
179     return 1;
180 }
181
182 static HRESULT WINAPI DataObject_GetData(
183     IDataObject *iface,
184     FORMATETC *pformatetcIn,
185     STGMEDIUM *pmedium)
186 {
187     return E_NOTIMPL;
188 }
189
190 static HRESULT WINAPI DataObject_GetDataHere(
191     IDataObject *iface,
192     FORMATETC *pformatetc,
193     STGMEDIUM *pmedium)
194 {
195     return E_NOTIMPL;
196 }
197
198 static HRESULT WINAPI DataObject_QueryGetData(
199     IDataObject *iface,
200     FORMATETC *pformatetc)
201 {
202     return S_OK;
203 }
204
205 static HRESULT WINAPI DataObject_GetCanonicalFormatEtc(
206     IDataObject *iface,
207     FORMATETC *pformatectIn,
208     FORMATETC *pformatetcOut)
209 {
210     return E_NOTIMPL;
211 }
212
213 static HRESULT WINAPI DataObject_SetData(
214     IDataObject *iface,
215     FORMATETC *pformatetc,
216     STGMEDIUM *pmedium,
217     BOOL fRelease)
218 {
219     return E_NOTIMPL;
220 }
221
222 static HRESULT WINAPI DataObject_EnumFormatEtc(
223     IDataObject *iface,
224     DWORD dwDirection,
225     IEnumFORMATETC **ppenumFormatEtc)
226 {
227     return S_OK;
228 }
229
230 static HRESULT WINAPI DataObject_DAdvise(
231     IDataObject *iface,
232     FORMATETC *pformatetc,
233     DWORD advf,
234     IAdviseSink *pAdvSink,
235     DWORD *pdwConnection)
236 {
237     return E_NOTIMPL;
238 }
239
240 static HRESULT WINAPI DataObject_DUnadvise(
241     IDataObject *iface,
242     DWORD dwConnection)
243 {
244     return E_NOTIMPL;
245 }
246
247 static HRESULT WINAPI DataObject_EnumDAdvise(
248     IDataObject *iface,
249     IEnumSTATDATA **ppenumAdvise)
250 {
251     return E_NOTIMPL;
252 }
253
254 static const IDataObjectVtbl dataobject_vtbl = {
255     DataObject_QueryInterface,
256     DataObject_AddRef,
257     DataObject_Release,
258     DataObject_GetData,
259     DataObject_GetDataHere,
260     DataObject_QueryGetData,
261     DataObject_GetCanonicalFormatEtc,
262     DataObject_SetData,
263     DataObject_EnumFormatEtc,
264     DataObject_DAdvise,
265     DataObject_DUnadvise,
266     DataObject_EnumDAdvise
267 };
268
269 static IDataObject DataObject = { &dataobject_vtbl };
270
271 static ATOM register_dummy_class(void)
272 {
273     WNDCLASS wc =
274     {
275         0,
276         DefWindowProc,
277         0,
278         0,
279         GetModuleHandle(NULL),
280         NULL,
281         LoadCursor(NULL, IDC_ARROW),
282         (HBRUSH)(COLOR_BTNFACE+1),
283         NULL,
284         TEXT("WineOleTestClass"),
285     };
286
287     return RegisterClass(&wc);
288 }
289
290 static void test_Register_Revoke(void)
291 {
292     HANDLE prop;
293     HRESULT hr;
294     HWND hwnd;
295
296     hwnd = CreateWindowA("WineOleTestClass", "Test", 0,
297         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
298         NULL, NULL, NULL);
299
300     hr = RegisterDragDrop(hwnd, &DropTarget);
301     ok(hr == E_OUTOFMEMORY ||
302         broken(hr == CO_E_NOTINITIALIZED), /* NT4 */
303         "RegisterDragDrop without OLE initialized should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr);
304
305     OleInitialize(NULL);
306
307     hr = RegisterDragDrop(hwnd, NULL);
308     ok(hr == E_INVALIDARG, "RegisterDragDrop with NULL IDropTarget * should return E_INVALIDARG instead of 0x%08x\n", hr);
309
310     hr = RegisterDragDrop(NULL, &DropTarget);
311     ok(hr == DRAGDROP_E_INVALIDHWND, "RegisterDragDrop with NULL hwnd should return DRAGDROP_E_INVALIDHWND instead of 0x%08x\n", hr);
312
313     hr = RegisterDragDrop((HWND)0xdeadbeef, &DropTarget);
314     ok(hr == DRAGDROP_E_INVALIDHWND, "RegisterDragDrop with garbage hwnd should return DRAGDROP_E_INVALIDHWND instead of 0x%08x\n", hr);
315
316     ok(droptarget_refs == 0, "DropTarget refs should be zero not %d\n", droptarget_refs);
317     hr = RegisterDragDrop(hwnd, &DropTarget);
318     ok_ole_success(hr, "RegisterDragDrop");
319     ok(droptarget_refs >= 1, "DropTarget refs should be at least one\n");
320
321     prop = GetPropA(hwnd, "OleDropTargetInterface");
322     ok(prop == &DropTarget, "expected IDropTarget pointer %p, got %p\n", &DropTarget, prop);
323
324     hr = RegisterDragDrop(hwnd, &DropTarget);
325     ok(hr == DRAGDROP_E_ALREADYREGISTERED, "RegisterDragDrop with already registered hwnd should return DRAGDROP_E_ALREADYREGISTERED instead of 0x%08x\n", hr);
326
327     ok(droptarget_refs >= 1, "DropTarget refs should be at least one\n");
328     OleUninitialize();
329     ok(droptarget_refs >= 1, "DropTarget refs should be at least one\n");
330
331     hr = RevokeDragDrop(hwnd);
332     ok_ole_success(hr, "RevokeDragDrop");
333     ok(droptarget_refs == 0 ||
334        broken(droptarget_refs == 1), /* NT4 */
335        "DropTarget refs should be zero not %d\n", droptarget_refs);
336
337     hr = RevokeDragDrop(NULL);
338     ok(hr == DRAGDROP_E_INVALIDHWND, "RevokeDragDrop with NULL hwnd should return DRAGDROP_E_INVALIDHWND instead of 0x%08x\n", hr);
339
340     DestroyWindow(hwnd);
341
342     /* try to revoke with already destroyed window */
343     OleInitialize(NULL);
344
345     hwnd = CreateWindowA("WineOleTestClass", "Test", 0,
346         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
347         NULL, NULL, NULL);
348
349     hr = RegisterDragDrop(hwnd, &DropTarget);
350     ok(hr == S_OK, "got 0x%08x\n", hr);
351
352     DestroyWindow(hwnd);
353
354     hr = RevokeDragDrop(hwnd);
355     ok(hr == DRAGDROP_E_INVALIDHWND, "got 0x%08x\n", hr);
356
357     OleUninitialize();
358 }
359
360 static void test_DoDragDrop(void)
361 {
362     DWORD effect;
363     HRESULT hr;
364     HWND hwnd;
365
366     hwnd = CreateWindowA("WineOleTestClass", "Test", 0,
367         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
368         NULL, NULL, NULL);
369     ok(IsWindow(hwnd), "failed to create window\n");
370
371     hr = OleInitialize(NULL);
372     ok(hr == S_OK, "got 0x%08x\n", hr);
373
374     hr = RegisterDragDrop(hwnd, &DropTarget);
375     ok(hr == S_OK, "got 0x%08x\n", hr);
376
377     /* incomplete arguments set */
378     hr = DoDragDrop(NULL, NULL, 0, NULL);
379     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
380
381     hr = DoDragDrop(NULL, &DropSource, 0, NULL);
382     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
383
384     hr = DoDragDrop(&DataObject, NULL, 0, NULL);
385     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
386
387     hr = DoDragDrop(NULL, NULL, 0, &effect);
388     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
389
390     hr = DoDragDrop(&DataObject, &DropSource, 0, NULL);
391     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
392
393     hr = DoDragDrop(NULL, &DropSource, 0, &effect);
394     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
395
396     hr = DoDragDrop(&DataObject, NULL, 0, &effect);
397     ok(hr == E_INVALIDARG, "got 0x%08x\n", hr);
398
399     OleUninitialize();
400
401     DestroyWindow(hwnd);
402 }
403
404 START_TEST(dragdrop)
405 {
406     register_dummy_class();
407
408     test_Register_Revoke();
409     test_DoDragDrop();
410 }