comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / dinput / tests / keyboard.c
blobe51e3325352b13b4345bceabecb94e2820e4ae47
1 /*
2 * Copyright (c) 2005 Robert Reif
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define DIRECTINPUT_VERSION 0x0700
21 #define COBJMACROS
22 #include <windows.h>
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "dinput.h"
33 static void acquire_tests(IDirectInputA *pDI, HWND hwnd)
35 HRESULT hr;
36 IDirectInputDeviceA *pKeyboard;
37 BYTE kbd_state[256];
38 LONG custom_state[6];
39 int i;
40 DIOBJECTDATAFORMAT dodf[] =
42 { &GUID_Key, sizeof(LONG) * 0, DIDFT_MAKEINSTANCE(DIK_Q)|DIDFT_BUTTON, 0 },
43 { &GUID_Key, sizeof(LONG) * 1, DIDFT_MAKEINSTANCE(DIK_W)|DIDFT_BUTTON, 0 },
44 { &GUID_Key, sizeof(LONG) * 2, DIDFT_MAKEINSTANCE(DIK_E)|DIDFT_BUTTON, 0 },
45 { &GUID_Key, sizeof(LONG) * 4, DIDFT_MAKEINSTANCE(DIK_R)|DIDFT_BUTTON, 0 },
48 DIDATAFORMAT df;
49 df.dwSize = sizeof( df );
50 df.dwObjSize = sizeof( DIOBJECTDATAFORMAT );
51 df.dwFlags = DIDF_RELAXIS;
52 df.dwDataSize = sizeof( custom_state );
53 df.dwNumObjs = sizeof( dodf )/sizeof( dodf[0] );
54 df.rgodf = dodf;
56 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
57 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
58 if (FAILED(hr)) return;
60 hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard);
61 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
62 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
63 ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %08x\n", hr);
64 hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
65 ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr);
66 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
67 ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %08x\n", hr);
68 hr = IDirectInputDevice_Unacquire(pKeyboard);
69 ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr);
70 hr = IDirectInputDevice_Acquire(pKeyboard);
71 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
72 hr = IDirectInputDevice_Acquire(pKeyboard);
73 ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr);
74 hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
75 ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr);
76 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
77 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %08x\n", hr);
78 hr = IDirectInputDevice_Unacquire(pKeyboard);
79 ok(SUCCEEDED(hr), "IDirectInputDevice_Uncquire() failed: %08x\n", hr);
80 hr = IDirectInputDevice_SetDataFormat( pKeyboard , &df );
81 ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
82 hr = IDirectInputDevice_Acquire(pKeyboard);
83 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
84 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
85 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState(4,) failed: %08x\n", hr);
86 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
87 ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(256,) should have failed: %08x\n", hr);
89 memset(custom_state, 0x56, sizeof(custom_state));
90 IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
91 for (i = 0; i < sizeof(custom_state) / sizeof(custom_state[0]); i++)
92 ok(custom_state[i] == 0, "Should be zeroed, got 0x%08x\n", custom_state[i]);
94 /* simulate some keyboard input */
95 SetFocus(hwnd);
96 keybd_event('Q', 0, 0, 0);
97 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
98 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %08x\n", hr);
99 if (!custom_state[0])
100 win_skip("Keyboard event not processed, skipping test\n");
101 else
103 /* unacquiring should reset the device state */
104 hr = IDirectInputDevice_Unacquire(pKeyboard);
105 ok(SUCCEEDED(hr), "IDirectInputDevice_Unacquire() failed: %08x\n", hr);
106 hr = IDirectInputDevice_Acquire(pKeyboard);
107 ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
108 hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
109 ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState failed: %08x\n", hr);
110 for (i = 0; i < sizeof(custom_state) / sizeof(custom_state[0]); i++)
111 ok(custom_state[i] == 0, "Should be zeroed, got 0x%08x\n", custom_state[i]);
113 keybd_event('Q', 0, KEYEVENTF_KEYUP, 0);
115 if (pKeyboard) IUnknown_Release(pKeyboard);
118 static const HRESULT SetCoop_null_window[16] = {
119 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
120 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
121 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG,
122 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
124 static const HRESULT SetCoop_invalid_window[16] = {
125 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
126 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
127 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
128 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
130 static const HRESULT SetCoop_real_window[16] = {
131 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
132 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
133 E_INVALIDARG, E_NOTIMPL, S_OK, E_INVALIDARG,
134 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
136 static const HRESULT SetCoop_child_window[16] = {
137 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
138 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
139 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
140 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
142 static void test_set_coop(IDirectInputA *pDI, HWND hwnd)
144 HRESULT hr;
145 IDirectInputDeviceA *pKeyboard = NULL;
146 int i;
147 HWND child;
149 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
150 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
151 if (FAILED(hr)) return;
153 for (i=0; i<16; i++)
155 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
156 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
158 for (i=0; i<16; i++)
160 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, (HWND)0x400000, i);
161 ok(hr == SetCoop_invalid_window[i], "SetCooperativeLevel(invalid, %d): %08x\n", i, hr);
163 for (i=0; i<16; i++)
165 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
166 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
169 child = CreateWindowA("static", "Title", WS_CHILD | WS_VISIBLE, 10, 10, 50, 50, hwnd, NULL,
170 NULL, NULL);
171 ok(child != NULL, "err: %d\n", GetLastError());
173 for (i=0; i<16; i++)
175 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, child, i);
176 ok(hr == SetCoop_child_window[i], "SetCooperativeLevel(child, %d): %08x\n", i, hr);
179 DestroyWindow(child);
180 if (pKeyboard) IUnknown_Release(pKeyboard);
183 static void test_get_prop(IDirectInputA *pDI, HWND hwnd)
185 HRESULT hr;
186 IDirectInputDeviceA *pKeyboard = NULL;
187 DIPROPRANGE diprg;
189 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
190 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
191 if (FAILED(hr)) return;
193 memset(&diprg, 0, sizeof(diprg));
194 diprg.diph.dwSize = sizeof(DIPROPRANGE);
195 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
196 diprg.diph.dwHow = DIPH_DEVICE;
197 diprg.diph.dwObj = 0;
199 hr = IDirectInputDevice_GetProperty(pKeyboard, DIPROP_RANGE, &diprg.diph);
200 ok(hr == DIERR_UNSUPPORTED, "IDirectInputDevice_GetProperty() did not return DIPROP_RANGE but: %08x\n", hr);
202 if (pKeyboard) IUnknown_Release(pKeyboard);
205 static void test_capabilities(IDirectInputA *pDI, HWND hwnd)
207 HRESULT hr;
208 IDirectInputDeviceA *pKeyboard = NULL;
209 DIDEVCAPS caps;
211 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
212 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
213 if (FAILED(hr)) return;
215 caps.dwSize = sizeof(caps);
216 hr = IDirectInputDevice_GetCapabilities(pKeyboard, &caps);
218 ok (SUCCEEDED(hr), "GetCapabilities failed: 0x%08x\n", hr);
219 ok (caps.dwFlags & DIDC_ATTACHED, "GetCapabilities dwFlags: 0x%08x\n", caps.dwFlags);
220 ok (LOWORD(LOBYTE(caps.dwDevType)) == DIDEVTYPE_KEYBOARD,
221 "GetCapabilities invalid device type for dwDevType: 0x%08x\n", caps.dwDevType);
222 ok (LOWORD(HIBYTE(caps.dwDevType)) != DIDEVTYPEKEYBOARD_UNKNOWN,
223 "GetCapabilities invalid device subtype for dwDevType: 0x%08x\n", caps.dwDevType);
225 IUnknown_Release(pKeyboard);
228 static void keyboard_tests(DWORD version)
230 HRESULT hr;
231 IDirectInputA *pDI = NULL;
232 HINSTANCE hInstance = GetModuleHandleW(NULL);
233 HWND hwnd;
234 ULONG ref = 0;
236 hr = DirectInputCreateA(hInstance, version, &pDI, NULL);
237 if (hr == DIERR_OLDDIRECTINPUTVERSION)
239 skip("Tests require a newer dinput version\n");
240 return;
242 ok(SUCCEEDED(hr), "DirectInputCreateA() failed: %08x\n", hr);
243 if (FAILED(hr)) return;
245 hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 10, 10, 200, 200,
246 NULL, NULL, NULL, NULL);
247 ok(hwnd != NULL, "err: %d\n", GetLastError());
249 if (hwnd)
251 acquire_tests(pDI, hwnd);
252 test_set_coop(pDI, hwnd);
253 test_get_prop(pDI, hwnd);
254 test_capabilities(pDI, hwnd);
257 DestroyWindow(hwnd);
258 if (pDI) ref = IUnknown_Release(pDI);
259 ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
262 START_TEST(keyboard)
264 CoInitialize(NULL);
266 keyboard_tests(0x0700);
268 CoUninitialize();