d3dcompiler: Add argument check in D3DReflect().
[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
33 static const HRESULT SetCoop_null_window[16] =  {
34     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
35     E_INVALIDARG, E_HANDLE,     E_HANDLE,     E_INVALIDARG,
36     E_INVALIDARG, E_HANDLE,     S_OK,         E_INVALIDARG,
37     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
38
39 static const HRESULT SetCoop_real_window[16] =  {
40     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG,
41     E_INVALIDARG, S_OK,         S_OK,         E_INVALIDARG,
42     E_INVALIDARG, E_NOTIMPL,    S_OK,         E_INVALIDARG,
43     E_INVALIDARG, E_INVALIDARG, E_INVALIDARG, E_INVALIDARG};
44
45 static void test_set_coop(LPDIRECTINPUT pDI, HWND hwnd)
46 {
47     HRESULT hr;
48     LPDIRECTINPUTDEVICE pMouse = NULL;
49     int i;
50
51     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
52     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
53     if (FAILED(hr)) return;
54
55     for (i=0; i<16; i++)
56     {
57         hr = IDirectInputDevice_SetCooperativeLevel(pMouse, NULL, i);
58         ok(hr == SetCoop_null_window[i], "SetCooperativeLevel(NULL, %d): %08x\n", i, hr);
59     }
60     for (i=0; i<16; i++)
61     {
62         hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, i);
63         ok(hr == SetCoop_real_window[i], "SetCooperativeLevel(hwnd, %d): %08x\n", i, hr);
64     }
65
66     if (pMouse) IUnknown_Release(pMouse);
67 }
68
69 static void test_acquire(LPDIRECTINPUT pDI, HWND hwnd)
70 {
71     HRESULT hr;
72     LPDIRECTINPUTDEVICE pMouse = NULL;
73     DIMOUSESTATE m_state;
74     HWND hwnd2;
75
76     if (! SetForegroundWindow(hwnd))
77     {
78         skip("Not running as foreground app, skipping acquire tests\n");
79         return;
80     }
81
82     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
83     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %08x\n", hr);
84     if (FAILED(hr)) return;
85
86     hr = IDirectInputDevice_SetCooperativeLevel(pMouse, hwnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);
87     ok(hr == S_OK, "SetCooperativeLevel: %08x\n", hr);
88
89     hr = IDirectInputDevice_SetDataFormat(pMouse, &c_dfDIMouse);
90     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %08x\n", hr);
91     hr = IDirectInputDevice_Unacquire(pMouse);
92     ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %08x\n", hr);
93     hr = IDirectInputDevice_Acquire(pMouse);
94     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %08x\n", hr);
95     hr = IDirectInputDevice_Acquire(pMouse);
96     ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %08x\n", hr);
97
98     /* Foreground coop level requires window to have focus */
99     /* Create a temporary window, this should make dinput
100      * loose mouse input */
101     hwnd2 = CreateWindow("static", "Temporary", WS_VISIBLE,
102                          10, 210, 200, 200, NULL, NULL, NULL, NULL);
103
104     hr = IDirectInputDevice_GetDeviceState(pMouse, sizeof(m_state), &m_state);
105     ok(hr == DIERR_NOTACQUIRED, "GetDeviceState() should have failed: %08x\n", hr);
106     /* Workaround so we can test other things. Remove when Wine is fixed */
107     IDirectInputDevice_Unacquire(pMouse);
108
109     hr = IDirectInputDevice_Acquire(pMouse);
110     ok(hr == DIERR_OTHERAPPHASPRIO, "Acquire() should have failed: %08x\n", hr);
111
112     SetActiveWindow( hwnd );
113     hr = IDirectInputDevice_Acquire(pMouse);
114     ok(hr == S_OK, "Acquire() failed: %08x\n", hr);
115
116     if (pMouse) IUnknown_Release(pMouse);
117
118     DestroyWindow( hwnd2 );
119 }
120
121 static void mouse_tests(void)
122 {
123     HRESULT hr;
124     LPDIRECTINPUT pDI = NULL;
125     HINSTANCE hInstance = GetModuleHandle(NULL);
126     HWND hwnd;
127     ULONG ref = 0;
128
129     hr = DirectInputCreate(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
130     if (hr == DIERR_OLDDIRECTINPUTVERSION)
131     {
132         skip("Tests require a newer dinput version\n");
133         return;
134     }
135     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %08x\n", hr);
136     if (FAILED(hr)) return;
137
138     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
139                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
140     ok(hwnd != NULL, "err: %d\n", GetLastError());
141     if (hwnd)
142     {
143         ShowWindow(hwnd, SW_SHOW);
144
145         test_set_coop(pDI, hwnd);
146         test_acquire(pDI, hwnd);
147
148         DestroyWindow(hwnd);
149     }
150     if (pDI) ref = IUnknown_Release(pDI);
151     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
152 }
153
154 START_TEST(mouse)
155 {
156     CoInitialize(NULL);
157
158     mouse_tests();
159
160     CoUninitialize();
161 }