ole32: Fix crash when calling CreateStream on read only storage.
[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_addref_called;
35 static int droptarget_release_called;
36
37 /* helper macros to make tests a bit leaner */
38 #define ok_ole_success(hr, func) ok(hr == S_OK, func " failed with error 0x%08x\n", hr)
39
40 static HRESULT WINAPI DropTarget_QueryInterface(IDropTarget* iface, REFIID riid,
41                                                 void** ppvObject)
42 {
43     trace("DropTarget_QueryInterface\n");
44     if (IsEqualIID(riid, &IID_IUnknown) ||
45         IsEqualIID(riid, &IID_IDropTarget))
46     {
47         IUnknown_AddRef(iface);
48         *ppvObject = iface;
49         return S_OK;
50     }
51     *ppvObject = NULL;
52     return E_NOINTERFACE;
53 }
54
55 static ULONG WINAPI DropTarget_AddRef(IDropTarget* iface)
56 {
57     droptarget_addref_called++;
58     return 2;
59 }
60
61 static ULONG WINAPI DropTarget_Release(IDropTarget* iface)
62 {
63     droptarget_release_called++;
64     return 1;
65 }
66
67 static HRESULT WINAPI DropTarget_DragEnter(IDropTarget* iface,
68                                            IDataObject* pDataObj,
69                                            DWORD grfKeyState, POINTL pt,
70                                            DWORD* pdwEffect)
71 {
72     return E_NOTIMPL;
73 }
74
75 static HRESULT WINAPI DropTarget_DragOver(IDropTarget* iface,
76                                           DWORD grfKeyState,
77                                           POINTL pt,
78                                           DWORD* pdwEffect)
79 {
80     return E_NOTIMPL;
81 }
82
83 static HRESULT WINAPI DropTarget_DragLeave(IDropTarget* iface)
84 {
85     return E_NOTIMPL;
86 }
87
88 static HRESULT WINAPI DropTarget_Drop(IDropTarget* iface,
89                                       IDataObject* pDataObj, DWORD grfKeyState,
90                                       POINTL pt, DWORD* pdwEffect)
91 {
92     return E_NOTIMPL;
93 }
94
95 static const IDropTargetVtbl DropTarget_VTbl =
96 {
97     DropTarget_QueryInterface,
98     DropTarget_AddRef,
99     DropTarget_Release,
100     DropTarget_DragEnter,
101     DropTarget_DragOver,
102     DropTarget_DragLeave,
103     DropTarget_Drop
104 };
105
106 static IDropTarget DropTarget = { &DropTarget_VTbl };
107
108 static ATOM register_dummy_class(void)
109 {
110     WNDCLASS wc =
111     {
112         0,
113         DefWindowProc,
114         0,
115         0,
116         GetModuleHandle(NULL),
117         NULL,
118         LoadCursor(NULL, IDC_ARROW),
119         (HBRUSH)(COLOR_BTNFACE+1),
120         NULL,
121         TEXT("WineOleTestClass"),
122     };
123
124     return RegisterClass(&wc);
125 }
126
127 START_TEST(dragdrop)
128 {
129     HRESULT hr;
130     HWND hwnd;
131
132     hwnd = CreateWindow(MAKEINTATOM(register_dummy_class()), "Test", 0,
133         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
134         NULL, NULL, NULL);
135
136     hr = RegisterDragDrop(hwnd, &DropTarget);
137     ok(hr == E_OUTOFMEMORY ||
138         broken(hr == CO_E_NOTINITIALIZED), /* NT4 */
139         "RegisterDragDrop without OLE initialized should have returned E_OUTOFMEMORY instead of 0x%08x\n", hr);
140
141     OleInitialize(NULL);
142
143     hr = RegisterDragDrop(hwnd, NULL);
144     ok(hr == E_INVALIDARG, "RegisterDragDrop with NULL IDropTarget * should return E_INVALIDARG instead of 0x%08x\n", hr);
145
146     hr = RegisterDragDrop(NULL, &DropTarget);
147     ok(hr == DRAGDROP_E_INVALIDHWND, "RegisterDragDrop with NULL hwnd should return DRAGDROP_E_INVALIDHWND instead of 0x%08x\n", hr);
148
149     hr = RegisterDragDrop((HWND)0xdeadbeef, &DropTarget);
150     ok(hr == DRAGDROP_E_INVALIDHWND, "RegisterDragDrop with garbage hwnd should return DRAGDROP_E_INVALIDHWND instead of 0x%08x\n", hr);
151
152     ok(droptarget_addref_called == 0, "DropTarget_AddRef shouldn't have been called\n");
153     hr = RegisterDragDrop(hwnd, &DropTarget);
154     ok_ole_success(hr, "RegisterDragDrop");
155     ok(droptarget_addref_called == 1, "DropTarget_AddRef should have been called once, not %d times\n", droptarget_addref_called);
156
157     hr = RegisterDragDrop(hwnd, &DropTarget);
158     ok(hr == DRAGDROP_E_ALREADYREGISTERED, "RegisterDragDrop with already registered hwnd should return DRAGDROP_E_ALREADYREGISTERED instead of 0x%08x\n", hr);
159
160     ok(droptarget_release_called == 0, "DropTarget_Release shouldn't have been called\n");
161     OleUninitialize();
162     ok(droptarget_release_called == 0, "DropTarget_Release shouldn't have been called\n");
163
164     hr = RevokeDragDrop(hwnd);
165     ok_ole_success(hr, "RevokeDragDrop");
166     ok(droptarget_release_called == 1 ||
167         broken(droptarget_release_called == 0), /* NT4 */
168         "DropTarget_Release should have been called once, not %d times\n", droptarget_release_called);
169
170     hr = RevokeDragDrop(NULL);
171     ok(hr == DRAGDROP_E_INVALIDHWND, "RevokeDragDrop with NULL hwnd should return DRAGDROP_E_INVALIDHWND instead of 0x%08x\n", hr);
172
173     DestroyWindow(hwnd);
174 }