crypt32: Don't fail if buffer is NULL, just return required size.
[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
75     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
76     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
77     if (FAILED(hr)) return;
78
79     hr = IDirectInputDevice_SetDataFormat(pKeyboard, &c_dfDIKeyboard);
80     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
81     hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
82     ok(SUCCEEDED(hr), "IDirectInputDevice_SetCooperativeLevel() failed: %s\n", DXGetErrorString8(hr));
83     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
84     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
85     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
86     ok(hr == DIERR_NOTACQUIRED, "IDirectInputDevice_GetDeviceState() should have failed: %s\n", DXGetErrorString8(hr));
87     hr = IDirectInputDevice_Unacquire(pKeyboard);
88     ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %s\n", DXGetErrorString8(hr));
89     hr = IDirectInputDevice_Acquire(pKeyboard);
90     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %s\n", DXGetErrorString8(hr));
91     hr = IDirectInputDevice_Acquire(pKeyboard);
92     ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %s\n", DXGetErrorString8(hr));
93     hr = IDirectInputDevice_GetDeviceState(pKeyboard, 10, kbd_state);
94     ok(hr == DIERR_INVALIDPARAM, "IDirectInputDevice_GetDeviceState(10,) should have failed: %s\n", DXGetErrorString8(hr));
95     hr = IDirectInputDevice_GetDeviceState(pKeyboard, sizeof(kbd_state), kbd_state);
96     ok(SUCCEEDED(hr), "IDirectInputDevice_GetDeviceState() failed: %s\n", DXGetErrorString8(hr));
97
98     if (pKeyboard) IUnknown_Release(pKeyboard);
99 }
100
101 static const HRESULT SetCoop_null_window[16] =  {
102     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
103     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
104     E_INVALIDARG, E_HANDLE,     S_OK,         E_INVALIDARG,
105     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
106
107 static const HRESULT SetCoop_real_window[16] =  {
108     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
109     E_INVALIDARG, S_OK,         S_OK,         E_INVALIDARG,
110     E_INVALIDARG, E_NOTIMPL,    S_OK,         E_INVALIDARG,
111     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
112
113 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
114 {
115     HRESULT hr;
116     LPDIRECTINPUTDEVICE pKeyboard = NULL;
117     int i;
118
119     hr = IDirectInput_CreateDevice(pDI, &GUID_SysKeyboard, &pKeyboard, NULL);
120     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
121     if (FAILED(hr)) return;
122
123     for (i=0; i<16; i++)
124     {
125         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, NULL, i);
126         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %s\n", i, DXGetErrorString8(hr));
127     }
128     for (i=0; i<16; i++)
129     {
130         hr = IDirectInputDevice_SetCooperativeLevel(pKeyboard, hwnd, i);
131         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %s\n", i, DXGetErrorString8(hr));
132     }
133
134     if (pKeyboard) IUnknown_Release(pKeyboard);
135 }
136
137 static void keyboard_tests(DWORD version)
138 {
139     HRESULT hr;
140     LPDIRECTINPUT pDI = NULL;
141     HINSTANCE hInstance = GetModuleHandle(NULL);
142     HWND hwnd;
143     ULONG ref = 0;
144
145     hr = DirectInputCreate(hInstance, version, &pDI, NULL);
146     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
147     if (FAILED(hr)) return;
148
149     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
150                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
151     ok(hwnd != NULL, "err: %d\n", GetLastError());
152
153     if (hwnd)
154     {
155         acquire_tests(pDI, hwnd);
156         test_set_coop(pDI, hwnd);
157     }
158
159     DestroyWindow(hwnd);
160     if (pDI) ref = IUnknown_Release(pDI);
161     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
162 }
163
164 START_TEST(keyboard)
165 {
166     CoInitialize(NULL);
167
168     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
169
170     keyboard_tests(0x0700);
171
172     CoUninitialize();
173 }