imm32: Retrieve the graphics driver module from gdi32.
[wine] / dlls / dinput / tests / dinput.c
1 /*
2  * Copyright (c) 2011 Andrew Nguyen
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 <initguid.h>
23 #include <windows.h>
24 #include <dinput.h>
25 #include <dinputd.h>
26
27 #include "wine/test.h"
28
29 HINSTANCE hInstance;
30
31 enum directinput_versions
32 {
33     DIRECTINPUT_VERSION_300 = 0x0300,
34     DIRECTINPUT_VERSION_500 = 0x0500,
35     DIRECTINPUT_VERSION_50A = 0x050A,
36     DIRECTINPUT_VERSION_5B2 = 0x05B2,
37     DIRECTINPUT_VERSION_602 = 0x0602,
38     DIRECTINPUT_VERSION_61A = 0x061A,
39     DIRECTINPUT_VERSION_700 = 0x0700,
40 };
41
42 static const DWORD directinput_version_list[] =
43 {
44     DIRECTINPUT_VERSION_300,
45     DIRECTINPUT_VERSION_500,
46     DIRECTINPUT_VERSION_50A,
47     DIRECTINPUT_VERSION_5B2,
48     DIRECTINPUT_VERSION_602,
49     DIRECTINPUT_VERSION_61A,
50     DIRECTINPUT_VERSION_700,
51 };
52
53 static HRESULT WINAPI (*pDirectInputCreateEx)(HINSTANCE, DWORD, REFIID, LPVOID *, LPUNKNOWN);
54
55 static BOOL CALLBACK dummy_callback(const DIDEVICEINSTANCEA *instance, void *context)
56 {
57     ok(0, "Callback was invoked with parameters (%p, %p)\n", instance, context);
58     return DIENUM_STOP;
59 }
60
61 static void test_preinitialization(void)
62 {
63     static const struct
64     {
65         REFGUID rguid;
66         BOOL pdev;
67         HRESULT expected_hr;
68     } create_device_tests[] =
69     {
70         {NULL, FALSE, E_POINTER},
71         {NULL, TRUE, E_POINTER},
72         {&GUID_Unknown, FALSE, E_POINTER},
73         {&GUID_Unknown, TRUE, DIERR_NOTINITIALIZED},
74         {&GUID_SysMouse, FALSE, E_POINTER},
75         {&GUID_SysMouse, TRUE, DIERR_NOTINITIALIZED},
76     };
77
78     static const struct
79     {
80         DWORD dwDevType;
81         LPDIENUMDEVICESCALLBACKA lpCallback;
82         DWORD dwFlags;
83         HRESULT expected_hr;
84         int todo;
85     } enum_devices_tests[] =
86     {
87         {0, NULL, 0, DIERR_INVALIDPARAM},
88         {0, NULL, ~0u, DIERR_INVALIDPARAM},
89         {0, dummy_callback, 0, DIERR_NOTINITIALIZED},
90         {0, dummy_callback, ~0u, DIERR_INVALIDPARAM},
91         {0xdeadbeef, NULL, 0, DIERR_INVALIDPARAM},
92         {0xdeadbeef, NULL, ~0u, DIERR_INVALIDPARAM},
93         {0xdeadbeef, dummy_callback, 0, DIERR_INVALIDPARAM},
94         {0xdeadbeef, dummy_callback, ~0u, DIERR_INVALIDPARAM},
95     };
96
97     IDirectInputA *pDI;
98     HRESULT hr;
99     int i;
100     IDirectInputDeviceA *pDID;
101
102     hr = CoCreateInstance(&CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectInputA, (void **)&pDI);
103     if (FAILED(hr))
104     {
105         skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
106         return;
107     }
108
109     for (i = 0; i < sizeof(create_device_tests)/sizeof(create_device_tests[0]); i++)
110     {
111         if (create_device_tests[i].pdev) pDID = (void *)0xdeadbeef;
112         hr = IDirectInput_CreateDevice(pDI, create_device_tests[i].rguid,
113                                             create_device_tests[i].pdev ? &pDID : NULL,
114                                             NULL);
115         ok(hr == create_device_tests[i].expected_hr, "[%d] IDirectInput_CreateDevice returned 0x%08x\n", i, hr);
116         if (create_device_tests[i].pdev)
117             ok(pDID == NULL, "[%d] Output interface pointer is %p\n", i, pDID);
118     }
119
120     for (i = 0; i < sizeof(enum_devices_tests)/sizeof(enum_devices_tests[0]); i++)
121     {
122         hr = IDirectInput_EnumDevices(pDI, enum_devices_tests[i].dwDevType,
123                                            enum_devices_tests[i].lpCallback,
124                                            NULL,
125                                            enum_devices_tests[i].dwFlags);
126         if (enum_devices_tests[i].todo)
127         {
128             todo_wine
129             ok(hr == enum_devices_tests[i].expected_hr, "[%d] IDirectInput_EnumDevice returned 0x%08x\n", i, hr);
130         }
131         else
132             ok(hr == enum_devices_tests[i].expected_hr, "[%d] IDirectInput_EnumDevice returned 0x%08x\n", i, hr);
133     }
134
135     hr = IDirectInput_GetDeviceStatus(pDI, NULL);
136     ok(hr == E_POINTER, "IDirectInput_GetDeviceStatus returned 0x%08x\n", hr);
137
138     hr = IDirectInput_GetDeviceStatus(pDI, &GUID_Unknown);
139     ok(hr == DIERR_NOTINITIALIZED, "IDirectInput_GetDeviceStatus returned 0x%08x\n", hr);
140
141     hr = IDirectInput_GetDeviceStatus(pDI, &GUID_SysMouse);
142     ok(hr == DIERR_NOTINITIALIZED, "IDirectInput_GetDeviceStatus returned 0x%08x\n", hr);
143
144     hr = IDirectInput_RunControlPanel(pDI, NULL, 0);
145     ok(hr == DIERR_NOTINITIALIZED, "IDirectInput_RunControlPanel returned 0x%08x\n", hr);
146
147     hr = IDirectInput_RunControlPanel(pDI, NULL, ~0u);
148     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_RunControlPanel returned 0x%08x\n", hr);
149
150     hr = IDirectInput_RunControlPanel(pDI, (HWND)0xdeadbeef, 0);
151     ok(hr == E_HANDLE, "IDirectInput_RunControlPanel returned 0x%08x\n", hr);
152
153     hr = IDirectInput_RunControlPanel(pDI, (HWND)0xdeadbeef, ~0u);
154     ok(hr == E_HANDLE, "IDirectInput_RunControlPanel returned 0x%08x\n", hr);
155
156     IDirectInput_Release(pDI);
157 }
158
159 static void test_DirectInputCreateEx(void)
160 {
161     static const struct
162     {
163         BOOL hinst;
164         DWORD dwVersion;
165         REFIID riid;
166         BOOL ppdi;
167         HRESULT expected_hr;
168         IUnknown *expected_ppdi;
169     } invalid_param_list[] =
170     {
171         {FALSE, 0,                       &IID_IUnknown,  FALSE, DIERR_NOINTERFACE},
172         {FALSE, 0,                       &IID_IUnknown,  TRUE,  DIERR_NOINTERFACE, (void *)0xdeadbeef},
173         {FALSE, 0,                       &IID_IDirectInputA, FALSE, E_POINTER},
174         {FALSE, 0,                       &IID_IDirectInputA, TRUE,  DIERR_INVALIDPARAM, NULL},
175         {FALSE, DIRECTINPUT_VERSION,     &IID_IUnknown,  FALSE, DIERR_NOINTERFACE},
176         {FALSE, DIRECTINPUT_VERSION,     &IID_IUnknown,  TRUE,  DIERR_NOINTERFACE, (void *)0xdeadbeef},
177         {FALSE, DIRECTINPUT_VERSION,     &IID_IDirectInputA, FALSE, E_POINTER},
178         {FALSE, DIRECTINPUT_VERSION,     &IID_IDirectInputA, TRUE,  DIERR_INVALIDPARAM, NULL},
179         {FALSE, DIRECTINPUT_VERSION - 1, &IID_IUnknown,  FALSE, DIERR_NOINTERFACE},
180         {FALSE, DIRECTINPUT_VERSION - 1, &IID_IUnknown,  TRUE,  DIERR_NOINTERFACE, (void *)0xdeadbeef},
181         {FALSE, DIRECTINPUT_VERSION - 1, &IID_IDirectInputA, FALSE, E_POINTER},
182         {FALSE, DIRECTINPUT_VERSION - 1, &IID_IDirectInputA, TRUE,  DIERR_INVALIDPARAM, NULL},
183         {FALSE, DIRECTINPUT_VERSION + 1, &IID_IUnknown,  FALSE, DIERR_NOINTERFACE},
184         {FALSE, DIRECTINPUT_VERSION + 1, &IID_IUnknown,  TRUE,  DIERR_NOINTERFACE, (void *)0xdeadbeef},
185         {FALSE, DIRECTINPUT_VERSION + 1, &IID_IDirectInputA, FALSE, E_POINTER},
186         {FALSE, DIRECTINPUT_VERSION + 1, &IID_IDirectInputA, TRUE,  DIERR_INVALIDPARAM, NULL},
187         {TRUE,  0,                       &IID_IUnknown,  FALSE, DIERR_NOINTERFACE},
188         {TRUE,  0,                       &IID_IUnknown,  TRUE,  DIERR_NOINTERFACE, (void *)0xdeadbeef},
189         {TRUE,  0,                       &IID_IDirectInputA, FALSE, E_POINTER},
190         {TRUE,  0,                       &IID_IDirectInputA, TRUE,  DIERR_NOTINITIALIZED, NULL},
191         {TRUE,  DIRECTINPUT_VERSION,     &IID_IUnknown,  FALSE, DIERR_NOINTERFACE},
192         {TRUE,  DIRECTINPUT_VERSION,     &IID_IUnknown,  TRUE,  DIERR_NOINTERFACE, (void *)0xdeadbeef},
193         {TRUE,  DIRECTINPUT_VERSION,     &IID_IDirectInputA, FALSE, E_POINTER},
194         {TRUE,  DIRECTINPUT_VERSION - 1, &IID_IUnknown,  FALSE, DIERR_NOINTERFACE},
195         {TRUE,  DIRECTINPUT_VERSION - 1, &IID_IUnknown,  TRUE,  DIERR_NOINTERFACE, (void *)0xdeadbeef},
196         {TRUE,  DIRECTINPUT_VERSION - 1, &IID_IDirectInputA, FALSE, E_POINTER},
197         {TRUE,  DIRECTINPUT_VERSION - 1, &IID_IDirectInputA, TRUE,  DIERR_BETADIRECTINPUTVERSION, NULL},
198         {TRUE,  DIRECTINPUT_VERSION + 1, &IID_IUnknown,  FALSE, DIERR_NOINTERFACE},
199         {TRUE,  DIRECTINPUT_VERSION + 1, &IID_IUnknown,  TRUE,  DIERR_NOINTERFACE, (void *)0xdeadbeef},
200         {TRUE,  DIRECTINPUT_VERSION + 1, &IID_IDirectInputA, FALSE, E_POINTER},
201         {TRUE,  DIRECTINPUT_VERSION + 1, &IID_IDirectInputA, TRUE,  DIERR_OLDDIRECTINPUTVERSION, NULL},
202     };
203
204     static REFIID no_interface_list[] = {&IID_IUnknown, &IID_IDirectInput8A,
205                                          &IID_IDirectInput8W, &IID_IDirectInputDeviceA,
206                                          &IID_IDirectInputDeviceW, &IID_IDirectInputDevice2A,
207                                          &IID_IDirectInputDevice2W, &IID_IDirectInputDevice7A,
208                                          &IID_IDirectInputDevice7W, &IID_IDirectInputDevice8A,
209                                          &IID_IDirectInputDevice8W, &IID_IDirectInputEffect};
210
211     static REFIID iid_list[] = {&IID_IDirectInputA, &IID_IDirectInputW,
212                                 &IID_IDirectInput2A, &IID_IDirectInput2W,
213                                 &IID_IDirectInput7A, &IID_IDirectInput7W};
214
215     int i, j;
216     IUnknown *pUnk;
217     HRESULT hr;
218
219     if (!pDirectInputCreateEx)
220     {
221         win_skip("DirectInputCreateEx is not available\n");
222         return;
223     }
224
225     for (i = 0; i < sizeof(invalid_param_list)/sizeof(invalid_param_list[0]); i++)
226     {
227         if (invalid_param_list[i].ppdi) pUnk = (void *)0xdeadbeef;
228         hr = pDirectInputCreateEx(invalid_param_list[i].hinst ? hInstance : NULL,
229                                   invalid_param_list[i].dwVersion,
230                                   invalid_param_list[i].riid,
231                                   invalid_param_list[i].ppdi ? (void **)&pUnk : NULL,
232                                   NULL);
233         ok(hr == invalid_param_list[i].expected_hr, "[%d] DirectInputCreateEx returned 0x%08x\n", i, hr);
234         if (invalid_param_list[i].ppdi)
235             ok(pUnk == invalid_param_list[i].expected_ppdi, "[%d] Output interface pointer is %p\n", i, pUnk);
236     }
237
238     for (i = 0; i < sizeof(no_interface_list)/sizeof(no_interface_list[0]); i++)
239     {
240         pUnk = (void *)0xdeadbeef;
241         hr = pDirectInputCreateEx(hInstance, DIRECTINPUT_VERSION, no_interface_list[i], (void **)&pUnk, NULL);
242         ok(hr == DIERR_NOINTERFACE, "[%d] DirectInputCreateEx returned 0x%08x\n", i, hr);
243         ok(pUnk == (void *)0xdeadbeef, "[%d] Output interface pointer is %p\n", i, pUnk);
244     }
245
246     for (i = 0; i < sizeof(iid_list)/sizeof(iid_list[0]); i++)
247     {
248         pUnk = NULL;
249         hr = pDirectInputCreateEx(hInstance, DIRECTINPUT_VERSION, iid_list[i], (void **)&pUnk, NULL);
250         ok(hr == DI_OK, "[%d] DirectInputCreateEx returned 0x%08x\n", i, hr);
251         ok(pUnk != NULL, "[%d] Output interface pointer is NULL\n", i);
252         if (pUnk)
253             IUnknown_Release(pUnk);
254     }
255
256     /* Examine combinations of requested interfaces and version numbers. */
257     for (i = 0; i < sizeof(directinput_version_list)/sizeof(directinput_version_list[0]); i++)
258     {
259         for (j = 0; j < sizeof(iid_list)/sizeof(iid_list[0]); j++)
260         {
261             pUnk = NULL;
262             hr = pDirectInputCreateEx(hInstance, directinput_version_list[i], iid_list[j], (void **)&pUnk, NULL);
263             ok(hr == DI_OK, "[%d/%d] DirectInputCreateEx returned 0x%08x\n", i, j, hr);
264             ok(pUnk != NULL, "[%d] Output interface pointer is NULL\n", i);
265             if (pUnk)
266                 IUnknown_Release(pUnk);
267         }
268     }
269 }
270
271 static void test_QueryInterface(void)
272 {
273     static REFIID iid_list[] = {&IID_IUnknown, &IID_IDirectInputA, &IID_IDirectInputW,
274                                 &IID_IDirectInput2A, &IID_IDirectInput2W,
275                                 &IID_IDirectInput7A, &IID_IDirectInput7W};
276
277     static const struct
278     {
279         REFIID riid;
280         int test_todo;
281     } no_interface_list[] =
282     {
283         {&IID_IDirectInput8A, 1},
284         {&IID_IDirectInput8W, 1},
285         {&IID_IDirectInputDeviceA},
286         {&IID_IDirectInputDeviceW},
287         {&IID_IDirectInputDevice2A},
288         {&IID_IDirectInputDevice2W},
289         {&IID_IDirectInputDevice7A},
290         {&IID_IDirectInputDevice7W},
291         {&IID_IDirectInputDevice8A},
292         {&IID_IDirectInputDevice8W},
293         {&IID_IDirectInputEffect},
294     };
295
296     IDirectInputA *pDI;
297     HRESULT hr;
298     IUnknown *pUnk;
299     int i;
300
301     hr = DirectInputCreateA(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
302     if (FAILED(hr))
303     {
304         win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
305         return;
306     }
307
308     hr = IDirectInput_QueryInterface(pDI, NULL, NULL);
309     ok(hr == E_POINTER, "IDirectInput_QueryInterface returned 0x%08x\n", hr);
310
311     pUnk = (void *)0xdeadbeef;
312     hr = IDirectInput_QueryInterface(pDI, NULL, (void **)&pUnk);
313     ok(hr == E_POINTER, "IDirectInput_QueryInterface returned 0x%08x\n", hr);
314     ok(pUnk == (void *)0xdeadbeef, "Output interface pointer is %p\n", pUnk);
315
316     hr = IDirectInput_QueryInterface(pDI, &IID_IUnknown, NULL);
317     ok(hr == E_POINTER, "IDirectInput_QueryInterface returned 0x%08x\n", hr);
318
319     for (i = 0; i < sizeof(iid_list)/sizeof(iid_list[0]); i++)
320     {
321         pUnk = NULL;
322         hr = IDirectInput_QueryInterface(pDI, iid_list[i], (void **)&pUnk);
323         ok(hr == S_OK, "[%d] IDirectInput_QueryInterface returned 0x%08x\n", i, hr);
324         ok(pUnk != NULL, "[%d] Output interface pointer is NULL\n", i);
325         if (pUnk) IUnknown_Release(pUnk);
326     }
327
328     for (i = 0; i < sizeof(no_interface_list)/sizeof(no_interface_list[0]); i++)
329     {
330         pUnk = (void *)0xdeadbeef;
331         hr = IDirectInput_QueryInterface(pDI, no_interface_list[i].riid, (void **)&pUnk);
332         if (no_interface_list[i].test_todo)
333         {
334             todo_wine
335             ok(hr == E_NOINTERFACE, "[%d] IDirectInput_QueryInterface returned 0x%08x\n", i, hr);
336             todo_wine
337             ok(pUnk == NULL, "[%d] Output interface pointer is %p\n", i, pUnk);
338
339             if (pUnk) IUnknown_Release(pUnk);
340         }
341         else
342         {
343             ok(hr == E_NOINTERFACE, "[%d] IDirectInput_QueryInterface returned 0x%08x\n", i, hr);
344             ok(pUnk == NULL, "[%d] Output interface pointer is %p\n", i, pUnk);
345         }
346     }
347
348     IDirectInput_Release(pDI);
349 }
350
351 static void test_CreateDevice(void)
352 {
353     IDirectInputA *pDI;
354     HRESULT hr;
355     IDirectInputDeviceA *pDID;
356
357     hr = DirectInputCreateA(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
358     if (FAILED(hr))
359     {
360         win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
361         return;
362     }
363
364     hr = IDirectInput_CreateDevice(pDI, NULL, NULL, NULL);
365     ok(hr == E_POINTER, "IDirectInput_CreateDevice returned 0x%08x\n", hr);
366
367     pDID = (void *)0xdeadbeef;
368     hr = IDirectInput_CreateDevice(pDI, NULL, &pDID, NULL);
369     ok(hr == E_POINTER, "IDirectInput_CreateDevice returned 0x%08x\n", hr);
370     ok(pDID == NULL, "Output interface pointer is %p\n", pDID);
371
372     hr = IDirectInput_CreateDevice(pDI, &GUID_Unknown, NULL, NULL);
373     ok(hr == E_POINTER, "IDirectInput_CreateDevice returned 0x%08x\n", hr);
374
375     pDID = (void *)0xdeadbeef;
376     hr = IDirectInput_CreateDevice(pDI, &GUID_Unknown, &pDID, NULL);
377     ok(hr == DIERR_DEVICENOTREG, "IDirectInput_CreateDevice returned 0x%08x\n", hr);
378     ok(pDID == NULL, "Output interface pointer is %p\n", pDID);
379
380     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, NULL, NULL);
381     ok(hr == E_POINTER, "IDirectInput_CreateDevice returned 0x%08x\n", hr);
382
383     hr = IDirectInput_CreateDevice(pDI, &GUID_SysMouse, &pDID, NULL);
384     ok(hr == DI_OK, "IDirectInput_CreateDevice returned 0x%08x\n", hr);
385
386     IDirectInputDevice_Release(pDID);
387     IDirectInput_Release(pDI);
388 }
389
390 struct enum_devices_test
391 {
392     unsigned int device_count;
393     BOOL return_value;
394 };
395
396 static BOOL CALLBACK enum_devices_callback(const DIDEVICEINSTANCEA *instance, void *context)
397 {
398     struct enum_devices_test *enum_test = context;
399
400     enum_test->device_count++;
401     return enum_test->return_value;
402 }
403
404 static void test_EnumDevices(void)
405 {
406     IDirectInputA *pDI;
407     HRESULT hr;
408     struct enum_devices_test enum_test, enum_test_return;
409
410     hr = DirectInputCreateA(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
411     if (FAILED(hr))
412     {
413         win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
414         return;
415     }
416
417     hr = IDirectInput_EnumDevices(pDI, 0, NULL, NULL, 0);
418     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
419
420     hr = IDirectInput_EnumDevices(pDI, 0, NULL, NULL, ~0u);
421     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
422
423     /* Test crashes on Wine. */
424     if (0)
425     {
426         hr = IDirectInput_EnumDevices(pDI, 0, enum_devices_callback, NULL, ~0u);
427         ok(hr == DIERR_INVALIDPARAM, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
428     }
429
430     hr = IDirectInput_EnumDevices(pDI, 0xdeadbeef, NULL, NULL, 0);
431     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
432
433     hr = IDirectInput_EnumDevices(pDI, 0xdeadbeef, NULL, NULL, ~0u);
434     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
435
436     hr = IDirectInput_EnumDevices(pDI, 0xdeadbeef, enum_devices_callback, NULL, 0);
437     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
438
439     hr = IDirectInput_EnumDevices(pDI, 0xdeadbeef, enum_devices_callback, NULL, ~0u);
440     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
441
442     enum_test.device_count = 0;
443     enum_test.return_value = DIENUM_CONTINUE;
444     hr = IDirectInput_EnumDevices(pDI, 0, enum_devices_callback, &enum_test, 0);
445     ok(hr == DI_OK, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
446     ok(enum_test.device_count != 0, "Device count is %u\n", enum_test.device_count);
447
448     /* Enumeration only stops with an explicit DIENUM_STOP. */
449     enum_test_return.device_count = 0;
450     enum_test_return.return_value = 42;
451     hr = IDirectInput_EnumDevices(pDI, 0, enum_devices_callback, &enum_test_return, 0);
452     ok(hr == DI_OK, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
453     ok(enum_test_return.device_count == enum_test.device_count,
454        "Device count is %u vs. %u\n", enum_test_return.device_count, enum_test.device_count);
455
456     enum_test.device_count = 0;
457     enum_test.return_value = DIENUM_STOP;
458     hr = IDirectInput_EnumDevices(pDI, 0, enum_devices_callback, &enum_test, 0);
459     ok(hr == DI_OK, "IDirectInput_EnumDevices returned 0x%08x\n", hr);
460     ok(enum_test.device_count == 1, "Device count is %u\n", enum_test.device_count);
461
462     IDirectInput_Release(pDI);
463 }
464
465 static void test_GetDeviceStatus(void)
466 {
467     IDirectInputA *pDI;
468     HRESULT hr;
469
470     hr = DirectInputCreateA(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
471     if (FAILED(hr))
472     {
473         win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
474         return;
475     }
476
477     hr = IDirectInput_GetDeviceStatus(pDI, NULL);
478     ok(hr == E_POINTER, "IDirectInput_GetDeviceStatus returned 0x%08x\n", hr);
479
480     hr = IDirectInput_GetDeviceStatus(pDI, &GUID_Unknown);
481     todo_wine
482     ok(hr == DIERR_DEVICENOTREG, "IDirectInput_GetDeviceStatus returned 0x%08x\n", hr);
483
484     hr = IDirectInput_GetDeviceStatus(pDI, &GUID_SysMouse);
485     ok(hr == DI_OK, "IDirectInput_GetDeviceStatus returned 0x%08x\n", hr);
486
487     IDirectInput_Release(pDI);
488 }
489
490 static void test_Initialize(void)
491 {
492     IDirectInputA *pDI;
493     HRESULT hr;
494     int i;
495
496     hr = CoCreateInstance(&CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectInputA, (void **)&pDI);
497     if (FAILED(hr))
498     {
499         win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
500         return;
501     }
502
503     hr = IDirectInput_Initialize(pDI, NULL, 0);
504     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_Initialize returned 0x%08x\n", hr);
505
506     hr = IDirectInput_Initialize(pDI, NULL, DIRECTINPUT_VERSION);
507     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_Initialize returned 0x%08x\n", hr);
508
509     hr = IDirectInput_Initialize(pDI, hInstance, 0);
510     ok(hr == DIERR_NOTINITIALIZED, "IDirectInput_Initialize returned 0x%08x\n", hr);
511
512     /* Invalid DirectInput versions less than 0x0700 yield DIERR_BETADIRECTINPUTVERSION. */
513     hr = IDirectInput_Initialize(pDI, hInstance, 0x0123);
514     ok(hr == DIERR_BETADIRECTINPUTVERSION, "IDirectInput_Initialize returned 0x%08x\n", hr);
515
516     /* Invalid DirectInput versions greater than 0x0700 yield DIERR_BETADIRECTINPUTVERSION. */
517     hr = IDirectInput_Initialize(pDI, hInstance, 0xcafe);
518     ok(hr == DIERR_OLDDIRECTINPUTVERSION, "IDirectInput_Initialize returned 0x%08x\n", hr);
519
520     for (i = 0; i < sizeof(directinput_version_list)/sizeof(directinput_version_list[0]); i++)
521     {
522         hr = IDirectInput_Initialize(pDI, hInstance, directinput_version_list[i]);
523         ok(hr == DI_OK, "IDirectInput_Initialize returned 0x%08x\n", hr);
524     }
525
526     /* Parameters are still validated after successful initialization. */
527     hr = IDirectInput_Initialize(pDI, hInstance, 0);
528     ok(hr == DIERR_NOTINITIALIZED, "IDirectInput_Initialize returned 0x%08x\n", hr);
529
530     IDirectInput_Release(pDI);
531 }
532
533 static void test_RunControlPanel(void)
534 {
535     IDirectInputA *pDI;
536     HRESULT hr;
537
538     hr = DirectInputCreateA(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
539     if (FAILED(hr))
540     {
541         win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
542         return;
543     }
544
545     if (winetest_interactive)
546     {
547         hr = IDirectInput_RunControlPanel(pDI, NULL, 0);
548         ok(hr == S_OK, "IDirectInput_RunControlPanel returned 0x%08x\n", hr);
549
550         hr = IDirectInput_RunControlPanel(pDI, GetDesktopWindow(), 0);
551         ok(hr == S_OK, "IDirectInput_RunControlPanel returned 0x%08x\n", hr);
552     }
553
554     hr = IDirectInput_RunControlPanel(pDI, NULL, ~0u);
555     ok(hr == DIERR_INVALIDPARAM, "IDirectInput_RunControlPanel returned 0x%08x\n", hr);
556
557     hr = IDirectInput_RunControlPanel(pDI, (HWND)0xdeadbeef, 0);
558     ok(hr == E_HANDLE, "IDirectInput_RunControlPanel returned 0x%08x\n", hr);
559
560     hr = IDirectInput_RunControlPanel(pDI, (HWND)0xdeadbeef, ~0u);
561     ok(hr == E_HANDLE, "IDirectInput_RunControlPanel returned 0x%08x\n", hr);
562
563     IDirectInput_Release(pDI);
564 }
565
566 static void test_DirectInputJoyConfig8(void)
567 {
568     IDirectInputA *pDI;
569     IDirectInputDeviceA *pDID;
570     IDirectInputJoyConfig8 *pDIJC;
571     DIJOYCONFIG info;
572     HRESULT hr;
573     int i;
574
575     hr = DirectInputCreateA(hInstance, DIRECTINPUT_VERSION, &pDI, NULL);
576     if (FAILED(hr))
577     {
578         win_skip("Failed to instantiate a IDirectInputA instance: 0x%08x\n", hr);
579         return;
580     }
581
582     hr = IDirectInput_QueryInterface(pDI, &IID_IDirectInputJoyConfig8, (void **)&pDIJC);
583     if (FAILED(hr))
584     {
585         win_skip("Failed to instantiate a IDirectInputJoyConfig8 instance: 0x%08x\n", hr);
586         return;
587     }
588
589     info.dwSize = sizeof(info);
590     hr = DI_OK;
591     i = 0;
592
593     /* Enumerate all connected joystick GUIDs and try to create the respective devices */
594     for (i = 0; SUCCEEDED(hr); i++)
595     {
596         hr = IDirectInputJoyConfig8_GetConfig(pDIJC, i, &info, DIJC_GUIDINSTANCE);
597
598         ok (hr == DI_OK || hr == DIERR_NOMOREITEMS,
599            "IDirectInputJoyConfig8_GetConfig returned 0x%08x\n", hr);
600
601         if (SUCCEEDED(hr))
602             ok (SUCCEEDED(IDirectInput_CreateDevice(pDI, &info.guidInstance, &pDID, NULL)),
603                "IDirectInput_CreateDevice failed with guid from GetConfig hr = 0x%08x\n", hr);
604     }
605
606 }
607
608 START_TEST(dinput)
609 {
610     HMODULE dinput_mod = GetModuleHandleA("dinput.dll");
611
612     hInstance = GetModuleHandleA(NULL);
613
614     pDirectInputCreateEx = (void *)GetProcAddress(dinput_mod, "DirectInputCreateEx");
615
616     CoInitialize(NULL);
617     test_preinitialization();
618     test_DirectInputCreateEx();
619     test_QueryInterface();
620     test_CreateDevice();
621     test_EnumDevices();
622     test_GetDeviceStatus();
623     test_Initialize();
624     test_RunControlPanel();
625     test_DirectInputJoyConfig8();
626     CoUninitialize();
627 }