mshtml: Added put_backgroundImage implementation.
[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     todo_wine
120         {
121             hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(custom_state), custom_state);
122             ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState(4,) failed: %s\n", DXGetErrorString8(hr));
123             hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
124             ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(256,) should have failed: %s\n", DXGetErrorString8(hr));
125         }
126
127     if (pKeyboard) IUnknown_Release(pKeyboard);
128 }
129
130 static const HRESULT SetCoop_null_window[16] =  {
131     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
132     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
133     E_INVALIDARG, E_HANDLE,     S_OK,         E_INVALIDARG,
134     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
135
136 static const HRESULT SetCoop_real_window[16] =  {
137     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
138     E_INVALIDARG, S_OK,         S_OK,         E_INVALIDARG,
139     E_INVALIDARG, E_NOTIMPL,    S_OK,         E_INVALIDARG,
140     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
141
142 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
143 {
144     HRESULT hr;
145     LPDIRECTINPUTDEVICE pKeyboard = NULL;
146     int i;
147
148     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
149     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
150     if (FAILED(hr)) return;
151
152     for (i=0; i<16; i++)
153     {
154         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
155         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %s\n", i, DXGetErrorString8(hr));
156     }
157     for (i=0; i<16; i++)
158     {
159         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
160         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %s\n", i, DXGetErrorString8(hr));
161     }
162
163     if (pKeyboard) IUnknown_Release(pKeyboard);
164 }
165
166 static void keyboard_tests(DWORD version)
167 {
168     HRESULT hr;
169     LPDIRECTINPUT pDI = NULL;
170     HINSTANCE hInstance = GetModuleHandle(NULL);
171     HWND hwnd;
172     ULONG ref = 0;
173
174     hr = DirectInputCreate(hInstance, version, &pDI, NULL);
175     if (hr == DIERR_OLDDIRECTINPUTVERSION)
176     {
177         skip("Tests require a newer dinput version\n");
178         return;
179     }
180     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
181     if (FAILED(hr)) return;
182
183     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
184                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
185     ok(hwnd != NULL, "err: %d\n", GetLastError());
186
187     if (hwnd)
188     {
189         acquire_tests(pDI, hwnd);
190         test_set_coop(pDI, hwnd);
191     }
192
193     DestroyWindow(hwnd);
194     if (pDI) ref = IUnknown_Release(pDI);
195     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
196 }
197
198 START_TEST(keyboard)
199 {
200     CoInitialize(NULL);
201
202     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
203
204     keyboard_tests(0x0700);
205
206     CoUninitialize();
207 }