[DINPUT_WINETEST]
[reactos.git] / rostests / winetests / dinput / mouse.c
1 /*
2 * Copyright (c) 2005 Robert Reif
3 * Copyright (c) 2006 Vitaliy Margolen
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #define WIN32_NO_STATUS
21 #define _INC_WINDOWS
22 #define COM_NO_WINDOWS_H
23
24 #define DIRECTINPUT_VERSION 0x0700
25
26 #define COBJMACROS
27 //#include <windows.h>
28
29 //#include <math.h>
30 //#include <stdlib.h>
31
32 #include <wine/test.h>
33 //#include "windef.h"
34 //#include "wingdi.h"
35 #include <dinput.h>
36
37 static const HRESULT SetCoop_null_window[16] = {
38 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
39 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
40 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG,
41 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
42
43 static const HRESULT SetCoop_real_window[16] = {
44 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
45 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
46 E_INVALIDARG, E_NOTIMPL, S_OK, E_INVALIDARG,
47 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
48
49 static const HRESULT SetCoop_child_window[16] = {
50 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
51 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
52 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
53 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
54
55 static void test_set_coop(IDirectInputA *pDI, HWND hwnd)
56 {
57 HRESULT hr;
58 IDirectInputDeviceA *pMouse = NULL;
59 int i;
60 HWND child;
61
62 hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
63 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
64 if (FAILED(hr)) return;
65
66 for (i=0; i<16; i++)
67 {
68 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, NULL, i);
69 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
70 }
71 for (i=0; i<16; i++)
72 {
73 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, i);
74 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
75 }
76
77 child = CreateWindowA("static", "Title", WS_CHILD | WS_VISIBLE, 10, 10, 50, 50, hwnd, NULL,
78 NULL, NULL);
79 ok(child != NULL, "err: %d\n", GetLastError());
80
81 for (i=0; i<16; i++)
82 {
83 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, child, i);
84 ok(hr == SetCoop_child_window[i], "SetCooperativeLevel(child, %d): %08x\n", i, hr);
85 }
86
87 DestroyWindow(child);
88 if (pMouse) IUnknown_Release(pMouse);
89 }
90
91 static void test_acquire(IDirectInputA *pDI, HWND hwnd)
92 {
93 HRESULT hr;
94 IDirectInputDeviceA *pMouse = NULL;
95 DIMOUSESTATE m_state;
96 HWND hwnd2;
97 DIPROPDWORD di_op;
98 DIDEVICEOBJECTDATA mouse_state;
99 DWORD cnt;
100 int i;
101
102 if (! SetForegroundWindow(hwnd))
103 {
104 skip("Not running as foreground app, skipping acquire tests\n");
105 return;
106 }
107
108 hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
109 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
110 if (FAILED(hr)) return;
111
112 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
113 ok(hr == S_OK, "SetCooperativeLevel: %08x\n", hr);
114
115 memset(&di_op, 0, sizeof(di_op));
116 di_op.dwData = 5;
117 di_op.diph.dwHow = DIPH_DEVICE;
118 di_op.diph.dwSize = sizeof(DIPROPDWORD);
119 di_op.diph.dwHeaderSize = sizeof(DIPROPHEADER);
120 hr = IDirectInputDevice_SetProperty(pMouse, DIPROP_BUFFERSIZE, (LPCDIPROPHEADER)&di_op);
121 ok(hr == S_OK, "SetProperty() failed: %08x\n", hr);
122
123 hr = IDirectInputDevice_SetDataFormat(pMouse, &c_dfDIMouse);
124 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
125 hr = IDirectInputDevice_Unacquire(pMouse);
126 ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr);
127 hr = IDirectInputDevice_Acquire(pMouse);
128 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
129 hr = IDirectInputDevice_Acquire(pMouse);
130 ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr);
131
132 /* Foreground coop level requires window to have focus */
133 /* Create a temporary window, this should make dinput
134 * loose mouse input */
135 hwnd2 = CreateWindowA("static", "Temporary", WS_VISIBLE, 10, 210, 200, 200, NULL, NULL, NULL,
136 NULL);
137
138 hr = IDirectInputDevice_GetDeviceState(pMouse, sizeof(m_state), &m_state);
139 ok(hr == DIERR_NOTACQUIRED, "GetDeviceState() should have failed: %08x\n", hr);
140 /* Workaround so we can test other things. Remove when Wine is fixed */
141 IDirectInputDevice_Unacquire(pMouse);
142
143 hr = IDirectInputDevice_Acquire(pMouse);
144 ok(hr == DIERR_OTHERAPPHASPRIO, "Acquire() should have failed: %08x\n", hr);
145
146 SetActiveWindow( hwnd );
147 hr = IDirectInputDevice_Acquire(pMouse);
148 ok(hr == S_OK, "Acquire() failed: %08x\n", hr);
149
150 if (!winetest_interactive)
151 skip("ROSTESTS-176/CORE-9710: Skipping randomly failing tests\n");
152 else {
153
154 mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
155 cnt = 1;
156 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
157 ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
158
159 mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
160 IDirectInputDevice_Unacquire(pMouse);
161 cnt = 1;
162 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
163 ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
164
165 IDirectInputDevice_Acquire(pMouse);
166 mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
167 IDirectInputDevice_Unacquire(pMouse);
168 IDirectInputDevice_Acquire(pMouse);
169 cnt = 1;
170 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
171 ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
172
173 /* Check for buffer owerflow */
174 for (i = 0; i < 6; i++)
175 mouse_event(MOUSEEVENTF_MOVE, 10 + i, 10 + i, 0, 0);
176
177 cnt = 1;
178 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
179 ok(hr == DI_OK, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
180 cnt = 1;
181 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
182 ok(hr == DI_OK && cnt == 1, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
183 }
184 if (pMouse) IUnknown_Release(pMouse);
185
186 DestroyWindow( hwnd2 );
187 }
188
189 static void mouse_tests(void)
190 {
191 HRESULT hr;
192 IDirectInputA *pDI = NULL;
193 HINSTANCE hInstance = GetModuleHandleW(NULL);
194 HWND hwnd;
195 ULONG ref = 0;
196
197 hr = DirectInputCreateA(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
198 if (hr == DIERR_OLDDIRECTINPUTVERSION)
199 {
200 skip("Tests require a newer dinput version\n");
201 return;
202 }
203 ok(SUCCEEDED(hr), "DirectInputCreateA() failed: %08x\n", hr);
204 if (FAILED(hr)) return;
205
206 hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL,
207 NULL, NULL);
208 ok(hwnd != NULL, "err: %d\n", GetLastError());
209 if (hwnd)
210 {
211 ShowWindow(hwnd, SW_SHOW);
212
213 test_set_coop(pDI, hwnd);
214 test_acquire(pDI, hwnd);
215
216 DestroyWindow(hwnd);
217 }
218 if (pDI) ref = IUnknown_Release(pDI);
219 ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
220 }
221
222 START_TEST(mouse)
223 {
224 CoInitialize(NULL);
225
226 mouse_tests();
227
228 CoUninitialize();
229 }