dinput: AddRef and Release parent DirectInput class.
[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
76     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pMouse, NULL);
77     ok(SUCCEEDED(hr), "IDirectInput_CreateDevice() failed: %s\n", DXGetErrorString8(hr));
78     if (FAILED(hr)) return;
79
80     hr = IDirectInputDevice_SetDataFormat(pMouse, &c_dfDIMouse);
81     ok(SUCCEEDED(hr), "IDirectInputDevice_SetDataFormat() failed: %s\n", DXGetErrorString8(hr));
82     hr = IDirectInputDevice_Unacquire(pMouse);
83     ok(hr == S_FALSE, "IDirectInputDevice_Unacquire() should have failed: %s\n", DXGetErrorString8(hr));
84     hr = IDirectInputDevice_Acquire(pMouse);
85     ok(SUCCEEDED(hr), "IDirectInputDevice_Acquire() failed: %s\n", DXGetErrorString8(hr));
86     hr = IDirectInputDevice_Acquire(pMouse);
87     ok(hr == S_FALSE, "IDirectInputDevice_Acquire() should have failed: %s\n", DXGetErrorString8(hr));
88
89     if (pMouse) IUnknown_Release(pMouse);
90 }
91
92 static void mouse_tests(void)
93 {
94     HRESULT hr;
95     LPDIRECTINPUT pDI = NULL;
96     HINSTANCE hInstance = GetModuleHandle(NULL);
97     HWND hwnd;
98     ULONG ref = 0;
99
100     hr = DirectInputCreate(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
101     ok(SUCCEEDED(hr), "DirectInputCreate() failed: %s\n", DXGetErrorString8(hr));
102     if (FAILED(hr)) return;
103
104     hwnd = CreateWindow("static", "Title", WS_OVERLAPPEDWINDOW,
105                         10, 10, 200, 200, NULL, NULL, NULL, NULL);
106     ok(hwnd != NULL, "err: %d\n", GetLastError());
107     if (hwnd)
108     {
109         ShowWindow(hwnd, SW_SHOW);
110
111         test_set_coop(pDI, hwnd);
112         test_acquire(pDI, hwnd);
113
114         DestroyWindow(hwnd);
115     }
116     if (pDI) ref = IUnknown_Release(pDI);
117     ok(!ref, "IDirectInput_Release() reference count = %d\n", ref);
118 }
119
120 START_TEST(mouse)
121 {
122     CoInitialize(NULL);
123
124     trace("DLL Version: %s\n", get_file_version("dinput.dll"));
125
126     mouse_tests();
127
128     CoUninitialize();
129 }