791300db2d4ae109f84d537fb3365fd7075821c0
[reactos.git] / modules / 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 #include "precomp.h"
21
22
23 static const HRESULT SetCoop_null_window[16] = {
24 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
25 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
26 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG,
27 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
28
29 static const HRESULT SetCoop_real_window[16] = {
30 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
31 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
32 E_INVALIDARG, E_NOTIMPL, S_OK, E_INVALIDARG,
33 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
34
35 static const HRESULT SetCoop_child_window[16] = {
36 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
37 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
38 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
39 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
40
41 static void test_set_coop(IDirectInputA *pDI, HWND hwnd)
42 {
43 HRESULT hr;
44 IDirectInputDeviceA *pMouse = NULL;
45 int i;
46 HWND child;
47
48 hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
49 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
50 if (FAILED(hr)) return;
51
52 for (i=0; i<16; i++)
53 {
54 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, NULL, i);
55 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
56 }
57 for (i=0; i<16; i++)
58 {
59 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, i);
60 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
61 }
62
63 child = CreateWindowA("static", "Title", WS_CHILD | WS_VISIBLE, 10, 10, 50, 50, hwnd, NULL,
64 NULL, NULL);
65 ok(child != NULL, "err: %d\n", GetLastError());
66
67 for (i=0; i<16; i++)
68 {
69 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, child, i);
70 ok(hr == SetCoop_child_window[i], "SetCooperativeLevel(child, %d): %08x\n", i, hr);
71 }
72
73 DestroyWindow(child);
74 if (pMouse) IUnknown_Release(pMouse);
75 }
76
77 static void test_acquire(IDirectInputA *pDI, HWND hwnd)
78 {
79 HRESULT hr;
80 IDirectInputDeviceA *pMouse = NULL;
81 DIMOUSESTATE m_state;
82 HWND hwnd2;
83 DIPROPDWORD di_op;
84 DIDEVICEOBJECTDATA mouse_state;
85 DWORD cnt;
86 int i;
87
88 if (! SetForegroundWindow(hwnd))
89 {
90 skip("Not running as foreground app, skipping acquire tests\n");
91 return;
92 }
93
94 hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
95 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
96 if (FAILED(hr)) return;
97
98 hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
99 ok(hr == S_OK, "SetCooperativeLevel: %08x\n", hr);
100
101 memset(&di_op, 0, sizeof(di_op));
102 di_op.dwData = 5;
103 di_op.diph.dwHow = DIPH_DEVICE;
104 di_op.diph.dwSize = sizeof(DIPROPDWORD);
105 di_op.diph.dwHeaderSize = sizeof(DIPROPHEADER);
106 hr = IDirectInputDevice_SetProperty(pMouse, DIPROP_BUFFERSIZE, (LPCDIPROPHEADER)&di_op);
107 ok(hr == S_OK, "SetProperty() failed: %08x\n", hr);
108
109 hr = IDirectInputDevice_SetDataFormat(pMouse, &c_dfDIMouse);
110 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
111 hr = IDirectInputDevice_Unacquire(pMouse);
112 ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr);
113 hr = IDirectInputDevice_Acquire(pMouse);
114 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
115 hr = IDirectInputDevice_Acquire(pMouse);
116 ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr);
117
118 /* Foreground coop level requires window to have focus */
119 /* Create a temporary window, this should make dinput
120 * lose mouse input */
121 hwnd2 = CreateWindowA("static", "Temporary", WS_VISIBLE, 10, 210, 200, 200, NULL, NULL, NULL,
122 NULL);
123 ok(hwnd2 != NULL, "CreateWindowA failed with %u\n", GetLastError());
124
125 hr = IDirectInputDevice_GetDeviceState(pMouse, sizeof(m_state), &m_state);
126 ok(hr == DIERR_NOTACQUIRED, "GetDeviceState() should have failed: %08x\n", hr);
127
128 hr = IDirectInputDevice_Acquire(pMouse);
129 ok(hr == DIERR_OTHERAPPHASPRIO, "Acquire() should have failed: %08x\n", hr);
130
131 SetActiveWindow( hwnd );
132 hr = IDirectInputDevice_Acquire(pMouse);
133 ok(hr == S_OK, "Acquire() failed: %08x\n", hr);
134
135 if (!winetest_interactive)
136 skip("ROSTESTS-176/CORE-9710: Skipping randomly failing tests\n");
137 else {
138
139 mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
140 cnt = 1;
141 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
142 ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
143
144 mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
145 IDirectInputDevice_Unacquire(pMouse);
146 cnt = 1;
147 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
148 ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
149
150 IDirectInputDevice_Acquire(pMouse);
151 mouse_event(MOUSEEVENTF_MOVE, 10, 10, 0, 0);
152 IDirectInputDevice_Unacquire(pMouse);
153 IDirectInputDevice_Acquire(pMouse);
154 cnt = 1;
155 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
156 ok(hr == S_OK && cnt > 0, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
157
158 /* Check for buffer owerflow */
159 for (i = 0; i < 6; i++)
160 mouse_event(MOUSEEVENTF_MOVE, 10 + i, 10 + i, 0, 0);
161
162 cnt = 1;
163 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
164 ok(hr == DI_OK, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
165 cnt = 1;
166 hr = IDirectInputDevice_GetDeviceData(pMouse, sizeof(mouse_state), &mouse_state, &cnt, 0);
167 ok(hr == DI_OK && cnt == 1, "GetDeviceData() failed: %08x cnt:%d\n", hr, cnt);
168 }
169 if (pMouse) IUnknown_Release(pMouse);
170
171 DestroyWindow( hwnd2 );
172 }
173
174 static void mouse_tests(void)
175 {
176 HRESULT hr;
177 IDirectInputA *pDI = NULL;
178 HINSTANCE hInstance = GetModuleHandleW(NULL);
179 HWND hwnd;
180 ULONG ref = 0;
181
182 hr = DirectInputCreateA(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
183 if (hr == DIERR_OLDDIRECTINPUTVERSION)
184 {
185 skip("Tests require a newer dinput version\n");
186 return;
187 }
188 ok(SUCCEEDED(hr), "DirectInputCreateA() failed: %08x\n", hr);
189 if (FAILED(hr)) return;
190
191 hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL,
192 NULL, NULL);
193 ok(hwnd != NULL, "err: %d\n", GetLastError());
194 if (hwnd)
195 {
196 ShowWindow(hwnd, SW_SHOW);
197
198 test_set_coop(pDI, hwnd);
199 test_acquire(pDI, hwnd);
200
201 DestroyWindow(hwnd);
202 }
203 if (pDI) ref = IUnknown_Release(pDI);
204 ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
205 }
206
207 START_TEST(mouse)
208 {
209 CoInitialize(NULL);
210
211 mouse_tests();
212
213 CoUninitialize();
214 }