crypt32: Use skip to avoid failures where support is missing.
[wine] / dlls / dinput / tests / mouse.c
1 /*
2  * Copyright (c) 2005 Robert Reif
3  * Copyright (c) 2006 Vitaliy Margolen
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #define DIRECTINPUT_VERSION 0x0700
21
22 #define COBJMACROS
23 #include <windows.h>
24
25 #include <math.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 static const HRESULT SetCoop_null_window[16] =  {
36     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
37     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
38     E_INVALIDARG, E_HANDLE,     S_OK,         E_INVALIDARG,
39     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
40
41 static const HRESULT SetCoop_real_window[16] =  {
42     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
43     E_INVALIDARG, S_OK,         S_OK,         E_INVALIDARG,
44     E_INVALIDARG, E_NOTIMPL,    S_OK,         E_INVALIDARG,
45     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
46
47 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
48 {
49     HRESULT hr;
50     LPDIRECTINPUTDEVICE pMouse = NULL;
51     int i;
52
53     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
54     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
55     if (FAILED(hr)) return;
56
57     for (i=0; i<16; i++)
58     {
59         hr = IDirectInputDevice_SetCooperativeLevel(pMouse, NULL, i);
60         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %s\n", i, DXGetErrorString8(hr));
61     }
62     for (i=0; i<16; i++)
63     {
64         hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, i);
65         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %s\n", i, DXGetErrorString8(hr));
66     }
67
68     if (pMouse) IUnknown_Release(pMouse);
69 }
70
71 static void test_acquire(LPDIRECTINPUT pDI, HWND hwnd)
72 {
73     HRESULT hr;
74     LPDIRECTINPUTDEVICE pMouse = NULL;
75     DIMOUSESTATE2 m_state;
76
77     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
78     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
79     if (FAILED(hr)) return;
80
81     hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
82     ok(hr == S_OK, "SetCooperativeLevel: %s\n", DXGetErrorString8(hr));
83
84     hr = IDirectInputDevice_SetDataFormat(pMouse, &c_dfDIMouse);
85     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
86     hr = IDirectInputDevice_Unacquire(pMouse);
87     ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %s\n", DXGetErrorString8(hr));
88     hr = IDirectInputDevice_Acquire(pMouse);
89     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %s\n", DXGetErrorString8(hr));
90     hr = IDirectInputDevice_Acquire(pMouse);
91     ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %s\n", DXGetErrorString8(hr));
92
93     /* Foreground coop level requires window to have focus */
94     /* This should make dinput loose mouse input */
95     SetActiveWindow( 0 );
96
97     hr = IDirectInputDevice_GetDeviceState(pMouse, sizeof(m_state), &m_state);
98     todo_wine
99     ok(hr == DIERR_NOTACQUIRED, "GetDeviceState() should have failed: %s\n", DXGetErrorString8(hr));
100     /* Workaround so we can test other things. Remove when Wine is fixed */
101     IDirectInputDevice_Unacquire(pMouse);
102
103     hr = IDirectInputDevice_Acquire(pMouse);
104     ok(hr == DIERR_OTHERAPPHASPRIO, "Acquire() should have failed: %s\n", DXGetErrorString8(hr));
105
106     SetActiveWindow( hwnd );
107     hr = IDirectInputDevice_Acquire(pMouse);
108     ok(hr == S_OK, "Acquire() failed: %s\n", DXGetErrorString8(hr));
109
110     if (pMouse) IUnknown_Release(pMouse);
111 }
112
113 static void mouse_tests(void)
114 {
115     HRESULT hr;
116     LPDIRECTINPUT pDI = NULL;
117     HINSTANCE hInstance = GetModuleHandle(NULL);
118     HWND hwnd;
119     ULONG ref = 0;
120
121     hr = DirectInputCreate(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
122     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
123     if (FAILED(hr)) return;
124
125     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
126                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
127     ok(hwnd != NULL, "err: %d\n", GetLastError());
128     if (hwnd)
129     {
130         ShowWindow(hwnd, SW_SHOW);
131
132         test_set_coop(pDI, hwnd);
133         test_acquire(pDI, hwnd);
134
135         DestroyWindow(hwnd);
136     }
137     if (pDI) ref = IUnknown_Release(pDI);
138     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
139 }
140
141 START_TEST(mouse)
142 {
143     CoInitialize(NULL);
144
145     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
146
147     mouse_tests();
148
149     CoUninitialize();
150 }