Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / dinput / tests / keyboard.c
blob2d4a12f1e734ce64dd630369e0301b2127db41b5
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 void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
111 HRESULT hr;
112 LPDIRECTINPUTDEVICE pKeyboard = NULL;
113 int i;
115 hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
116 ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
117 if (FAILED(hr)) return;
119 for (i=0; i<16; i++)
121 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
122 ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
124 for (i=0; i<16; i++)
126 hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
127 ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
130 if (pKeyboard) IUnknown_Release(pKeyboard);
133 static void keyboard_tests(DWORD version)
135 HRESULT hr;
136 LPDIRECTINPUT pDI = NULL;
137 HINSTANCE hInstance = GetModuleHandle(NULL);
138 HWND hwnd;
139 ULONG ref = 0;
141 hr = DirectInputCreate(hInstance, version, &pDI, NULL);
142 if (hr == DIERR_OLDDIRECTINPUTVERSION)
144 skip("Tests require a newer dinput version\n");
145 return;
147 ok(SUCCEEDED(hr), "DirectInputCreate() failed: %08x\n", hr);
148 if (FAILED(hr)) return;
150 hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
151 10, 10, 200, 200, NULL, NULL, NULL, NULL);
152 ok(hwnd != NULL, "err: %d\n", GetLastError());
154 if (hwnd)
156 acquire_tests(pDI, hwnd);
157 test_set_coop(pDI, hwnd);
160 DestroyWindow(hwnd);
161 if (pDI) ref = IUnknown_Release(pDI);
162 ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
165 START_TEST(keyboard)
167 CoInitialize(NULL);
169 keyboard_tests(0x0700);
171 CoUninitialize();