Release 1.6-rc2.
[wine/testsucceed.git] / dlls / dinput / tests / keyboard.c
blobfe5fa896c35fdea7f7f2662a324cde7cc8ba5dbf
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(LPDIRECTINPUT pDI, HWND hwnd)
35 HRESULT hr;
36 LPDIRECTINPUTDEVICE 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 if (pKeyboard) IUnknown_Release(pKeyboard);
97 static const HRESULT SetCoop_null_window[16] = {
98 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
99 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
100 E_INVALIDARG, E_HANDLE, S_OK, E_INVALIDARG,
101 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
103 static const HRESULT SetCoop_real_window[16] = {
104 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
105 E_INVALIDARG, S_OK, S_OK, E_INVALIDARG,
106 E_INVALIDARG, E_NOTIMPL, S_OK, E_INVALIDARG,
107 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
109 static const HRESULT SetCoop_child_window[16] = {
110 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
111 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
112 E_INVALIDARG, E_HANDLE, E_HANDLE, E_INVALIDARG,
113 E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
115 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
117 HRESULT hr;
118 LPDIRECTINPUTDEVICE pKeyboard = NULL;
119 int i;
120 HWND child;
122 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
123 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
124 if (FAILED(hr)) return;
126 for (i=0; i<16; i++)
128 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
129 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
131 for (i=0; i<16; i++)
133 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
134 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
137 child = CreateWindow("static", "Title", WS_CHILD | WS_VISIBLE,
138 10, 10, 50, 50, hwnd, NULL, NULL, NULL);
139 ok(child != NULL, "err: %d\n", GetLastError());
141 for (i=0; i<16; i++)
143 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, child, i);
144 ok(hr == SetCoop_child_window[i], "SetCooperativeLevel(child, %d): %08x\n", i, hr);
147 DestroyWindow(child);
148 if (pKeyboard) IUnknown_Release(pKeyboard);
151 static void test_get_prop(LPDIRECTINPUT pDI, HWND hwnd)
153 HRESULT hr;
154 LPDIRECTINPUTDEVICE pKeyboard = NULL;
155 DIPROPRANGE diprg;
157 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
158 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
159 if (FAILED(hr)) return;
161 memset(&diprg, 0, sizeof(diprg));
162 diprg.diph.dwSize = sizeof(DIPROPRANGE);
163 diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER);
164 diprg.diph.dwHow = DIPH_DEVICE;
165 diprg.diph.dwObj = 0;
167 hr = IDirectInputDevice_GetProperty(pKeyboard, DIPROP_RANGE, &diprg.diph);
168 ok(hr == DIERR_UNSUPPORTED, "IDirectInputDevice_GetProperty() did not return DIPROP_RANGE but: %08x\n", hr);
170 if (pKeyboard) IUnknown_Release(pKeyboard);
173 static void test_capabilities(LPDIRECTINPUT pDI, HWND hwnd)
175 HRESULT hr;
176 LPDIRECTINPUTDEVICE pKeyboard = NULL;
177 DIDEVCAPS caps;
179 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
180 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
181 if (FAILED(hr)) return;
183 caps.dwSize = sizeof(caps);
184 hr = IDirectInputDevice_GetCapabilities(pKeyboard, &caps);
186 ok (SUCCEEDED(hr), "GetCapabilities failed: 0x%08x\n", hr);
187 ok (caps.dwFlags & DIDC_ATTACHED, "GetCapabilities dwFlags: 0x%08x\n", caps.dwFlags);
188 ok (LOWORD(LOBYTE(caps.dwDevType)) == DIDEVTYPE_KEYBOARD,
189 "GetCapabilities invalid device type for dwDevType: 0x%08x\n", caps.dwDevType);
190 ok (LOWORD(HIBYTE(caps.dwDevType)) != DIDEVTYPEKEYBOARD_UNKNOWN,
191 "GetCapabilities invalid device subtype for dwDevType: 0x%08x\n", caps.dwDevType);
193 IUnknown_Release(pKeyboard);
196 static void keyboard_tests(DWORD version)
198 HRESULT hr;
199 LPDIRECTINPUT pDI = NULL;
200 HINSTANCE hInstance = GetModuleHandle(NULL);
201 HWND hwnd;
202 ULONG ref = 0;
204 hr = DirectInputCreate(hInstance, version, &pDI, NULL);
205 if (hr == DIERR_OLDDIRECTINPUTVERSION)
207 skip("Tests require a newer dinput version\n");
208 return;
210 ok(SUCCEEDED(hr), "DirectInputCreate() failed: %08x\n", hr);
211 if (FAILED(hr)) return;
213 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
214 10, 10, 200, 200, NULL, NULL, NULL, NULL);
215 ok(hwnd != NULL, "err: %d\n", GetLastError());
217 if (hwnd)
219 acquire_tests(pDI, hwnd);
220 test_set_coop(pDI, hwnd);
221 test_get_prop(pDI, hwnd);
222 test_capabilities(pDI, hwnd);
225 DestroyWindow(hwnd);
226 if (pDI) ref = IUnknown_Release(pDI);
227 ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
230 START_TEST(keyboard)
232 CoInitialize(NULL);
234 keyboard_tests(0x0700);
236 CoUninitialize();