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