richedit: Add an assert to point out what we're expecting.
[wine] / dlls / dinput / tests / keyboard.c
1 /*
2  * Copyright (c) 2005 Robert Reif
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #define DIRECTINPUT_VERSION 0x0700
20
21 #define COBJMACROS
22 #include <windows.h>
23
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "wine/test.h"
29 #include "windef.h"
30 #include "wingdi.h"
31 #include "dinput.h"
32 #include "dinput_test.h"
33
34 const char * get_file_version(const char * file_name)
35 {
36     static char version[32];
37     static char backslash[] = "\\";
38     DWORD size;
39     DWORD handle;
40
41     size = GetFileVersionInfoSizeA(file_name, &handle);
42     if (size) {
43         char * data = HeapAlloc(GetProcessHeap(), 0, size);
44         if (data) {
45             if (GetFileVersionInfoA(file_name, handle, size, data)) {
46                 VS_FIXEDFILEINFO *pFixedVersionInfo;
47                 UINT len;
48                 if (VerQueryValueA(data, backslash, (LPVOID *)&pFixedVersionInfo, &len)) {
49                     sprintf(version, "%d.%d.%d.%d",
50                             pFixedVersionInfo->dwFileVersionMS >> 16,
51                             pFixedVersionInfo->dwFileVersionMS & 0xffff,
52                             pFixedVersionInfo->dwFileVersionLS >> 16,
53                             pFixedVersionInfo->dwFileVersionLS & 0xffff);
54                 } else
55                     sprintf(version, "not available");
56             } else
57                 sprintf(version, "failed");
58
59             HeapFree(GetProcessHeap(), 0, data);
60         } else
61             sprintf(version, "failed");
62     } else
63         sprintf(version, "not available");
64
65     return version;
66 }
67
68 static void acquire_tests(LPDIRECTINPUT pDI, HWND hwnd)
69 {
70     HRESULT hr;
71     LPDIRECTINPUTDEVICE pKeyboard;
72     BYTE kbd_state[256];
73     BYTE custom_state[4];
74     DIOBJECTDATAFORMAT dodf[] =
75         {
76             { &GUID_Key, 0, DIDFT_MAKEINSTANCE(DIK_Q)|DIDFT_BUTTON, 0 },
77             { &GUID_Key, 1, DIDFT_MAKEINSTANCE(DIK_W)|DIDFT_BUTTON, 0 },
78             { &GUID_Key, 2, DIDFT_MAKEINSTANCE(DIK_E)|DIDFT_BUTTON, 0 },
79             { &GUID_Key, 3, DIDFT_MAKEINSTANCE(DIK_R)|DIDFT_BUTTON, 0 },
80         };
81
82     DIDATAFORMAT df;
83     df.dwSize = sizeof( df );
84     df.dwObjSize = sizeof( DIOBJECTDATAFORMAT );
85     df.dwFlags = DIDF_RELAXIS;
86     df.dwDataSize = sizeof( dodf )/sizeof( dodf[0] );
87     df.dwNumObjs = sizeof( dodf )/sizeof( dodf[0] );
88     df.rgodf = dodf;
89
90     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
91     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
92     if (FAILED(hr)) return;
93
94     hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard);
95     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
96     hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
97     ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %08x\n", hr);
98     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
99     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr);
100     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
101     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %08x\n", hr);
102     hr = IDirectInputDevice_Unacquire(pKeyboard);
103     ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr);
104     hr = IDirectInputDevice_Acquire(pKeyboard);
105     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
106     hr = IDirectInputDevice_Acquire(pKeyboard);
107     ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr);
108     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
109     ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %08x\n", hr);
110     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
111     ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %08x\n", hr);
112     hr = IDirectInputDevice_Unacquire(pKeyboard);
113     ok(SUCCEEDED(hr), "IDirectInputDevice_Uncquire() failed: %08x\n", hr);
114     hr = IDirectInputDevice_SetDataFormat( pKeyboard , &df );
115     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
116     hr = IDirectInputDevice_Acquire(pKeyboard);
117     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
118     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
119     ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState(4,) failed: %08x\n", hr);
120     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
121     ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(256,) should have failed: %08x\n", hr);
122
123     if (pKeyboard) IUnknown_Release(pKeyboard);
124 }
125
126 static const HRESULT SetCoop_null_window[16] =  {
127     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
128     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
129     E_INVALIDARG, E_HANDLE,     S_OK,         E_INVALIDARG,
130     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
131
132 static const HRESULT SetCoop_real_window[16] =  {
133     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
134     E_INVALIDARG, S_OK,         S_OK,         E_INVALIDARG,
135     E_INVALIDARG, E_NOTIMPL,    S_OK,         E_INVALIDARG,
136     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
137
138 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
139 {
140     HRESULT hr;
141     LPDIRECTINPUTDEVICE pKeyboard = NULL;
142     int i;
143
144     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
145     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
146     if (FAILED(hr)) return;
147
148     for (i=0; i<16; i++)
149     {
150         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
151         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
152     }
153     for (i=0; i<16; i++)
154     {
155         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
156         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
157     }
158
159     if (pKeyboard) IUnknown_Release(pKeyboard);
160 }
161
162 static void keyboard_tests(DWORD version)
163 {
164     HRESULT hr;
165     LPDIRECTINPUT pDI = NULL;
166     HINSTANCE hInstance = GetModuleHandle(NULL);
167     HWND hwnd;
168     ULONG ref = 0;
169
170     hr = DirectInputCreate(hInstance, version, &pDI, NULL);
171     if (hr == DIERR_OLDDIRECTINPUTVERSION)
172     {
173         skip("Tests require a newer dinput version\n");
174         return;
175     }
176     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %08x\n", hr);
177     if (FAILED(hr)) return;
178
179     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
180                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
181     ok(hwnd != NULL, "err: %d\n", GetLastError());
182
183     if (hwnd)
184     {
185         acquire_tests(pDI, hwnd);
186         test_set_coop(pDI, hwnd);
187     }
188
189     DestroyWindow(hwnd);
190     if (pDI) ref = IUnknown_Release(pDI);
191     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
192 }
193
194 START_TEST(keyboard)
195 {
196     CoInitialize(NULL);
197
198     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
199
200     keyboard_tests(0x0700);
201
202     CoUninitialize();
203 }