Using a notification event to wait for input, addressing spurious failures or timeouts.
Signed-off-by: Rémi Bernon rbernon@codeweavers.com --- dlls/dinput8/tests/device.c | 101 ++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 34 deletions(-)
diff --git a/dlls/dinput8/tests/device.c b/dlls/dinput8/tests/device.c index 3cad4ce256c..5ae9e225dc9 100644 --- a/dlls/dinput8/tests/device.c +++ b/dlls/dinput8/tests/device.c @@ -90,67 +90,100 @@ static void flush_events(void) } }
-static void test_device_input(IDirectInputDevice8A *lpdid, DWORD event_type, DWORD event, UINT_PTR expected) +static void test_device_input( IDirectInputDevice8A *device, DWORD type, DWORD code, UINT_PTR expected ) { HRESULT hr; DIDEVICEOBJECTDATA obj_data; - DWORD data_size = 1; + DWORD res, data_size = 1; + HANDLE event; int i;
- hr = IDirectInputDevice8_Acquire(lpdid); - ok (SUCCEEDED(hr), "Failed to acquire device hr=%08x\n", hr); + event = CreateEventW( NULL, FALSE, FALSE, NULL ); + ok( event != NULL, "CreateEventW failed, error %u\n", GetLastError() );
- if (event_type == INPUT_KEYBOARD) - keybd_event(0, event, KEYEVENTF_SCANCODE, 0); + IDirectInputDevice_Unacquire( device );
- if (event_type == INPUT_MOUSE) - mouse_event( event, 0, 0, 0, 0); + hr = IDirectInputDevice8_SetEventNotification( device, event ); + ok( hr == DI_OK, "SetEventNotification returned %#x\n", hr );
- flush_events(); - IDirectInputDevice8_Poll(lpdid); - hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0); + hr = IDirectInputDevice8_Acquire( device ); + ok( hr == DI_OK, "Acquire returned %#x\n", hr );
- if (data_size != 1) + if (type == INPUT_KEYBOARD) { - win_skip("We're not able to inject input into Windows dinput8 with events\n"); - IDirectInputDevice_Unacquire(lpdid); - return; + keybd_event( 0, code, KEYEVENTF_SCANCODE, 0 ); + res = WaitForSingleObject( event, 100 ); + ok( res == WAIT_OBJECT_0, "WaitForSingleObject returned %#x\n", res ); + + keybd_event( 0, code, KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP, 0 ); + res = WaitForSingleObject( event, 100 ); + ok( res == WAIT_OBJECT_0, "WaitForSingleObject returned %#x\n", res ); + } + if (type == INPUT_MOUSE) + { + mouse_event( MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0 ); + res = WaitForSingleObject( event, 100 ); + ok( res == WAIT_OBJECT_0, "WaitForSingleObject returned %#x\n", res ); + + mouse_event( MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 ); + res = WaitForSingleObject( event, 100 ); + ok( res == WAIT_OBJECT_0, "WaitForSingleObject returned %#x\n", res ); }
- ok (obj_data.uAppData == expected, "Retrieval of action failed uAppData=%lu expected=%lu\n", obj_data.uAppData, expected); + hr = IDirectInputDevice8_GetDeviceData( device, sizeof(obj_data), &obj_data, &data_size, 0 ); + ok( hr == DI_OK, "GetDeviceData returned %#x\n", hr ); + ok( data_size == 1, "got data size %u, expected 1\n", data_size ); + ok( obj_data.uAppData == expected, "got action uAppData %p, expected %p\n", + (void *)obj_data.uAppData, (void *)expected );
/* Check for buffer overflow */ for (i = 0; i < 17; i++) - if (event_type == INPUT_KEYBOARD) + { + if (type == INPUT_KEYBOARD) { - keybd_event( VK_SPACE, DIK_SPACE, 0, 0); - keybd_event( VK_SPACE, DIK_SPACE, KEYEVENTF_KEYUP, 0); - } - else if (event_type == INPUT_MOUSE) - { - mouse_event(MOUSEEVENTF_LEFTDOWN, 1, 1, 0, 0); - mouse_event(MOUSEEVENTF_LEFTUP, 1, 1, 0, 0); - } + keybd_event( VK_SPACE, DIK_SPACE, 0, 0 ); + res = WaitForSingleObject( event, 100 ); + ok( res == WAIT_OBJECT_0, "WaitForSingleObject returned %#x\n", res );
- flush_events(); - IDirectInputDevice8_Poll(lpdid); + keybd_event( VK_SPACE, DIK_SPACE, KEYEVENTF_KEYUP, 0 ); + res = WaitForSingleObject( event, 100 ); + ok( res == WAIT_OBJECT_0, "WaitForSingleObject returned %#x\n", res ); + } + if (type == INPUT_MOUSE) + { + mouse_event( MOUSEEVENTF_LEFTDOWN, 1, 1, 0, 0 ); + res = WaitForSingleObject( event, 100 ); + ok( res == WAIT_OBJECT_0, "WaitForSingleObject returned %#x\n", res ); + + mouse_event( MOUSEEVENTF_LEFTUP, 1, 1, 0, 0 ); + res = WaitForSingleObject( event, 100 ); + ok( res == WAIT_OBJECT_0, "WaitForSingleObject returned %#x\n", res ); + } + }
data_size = 1; - hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0); - ok(hr == DI_BUFFEROVERFLOW, "GetDeviceData() failed: %08x\n", hr); + hr = IDirectInputDevice8_GetDeviceData( device, sizeof(obj_data), &obj_data, &data_size, 0 ); + ok( hr == DI_BUFFEROVERFLOW, "GetDeviceData returned %#x\n", hr ); data_size = 1; - hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0); - ok(hr == DI_OK && data_size == 1, "GetDeviceData() failed: %08x cnt:%d\n", hr, data_size); + hr = IDirectInputDevice8_GetDeviceData( device, sizeof(obj_data), &obj_data, &data_size, 0 ); + ok( hr == DI_OK, "GetDeviceData returned %#x\n", hr ); + ok( data_size == 1, "got data_size %u, expected 1\n", data_size );
/* drain device's queue */ while (data_size == 1) { - hr = IDirectInputDevice8_GetDeviceData(lpdid, sizeof(obj_data), &obj_data, &data_size, 0); - ok(hr == DI_OK, "GetDeviceData() failed: %08x cnt:%d\n", hr, data_size); + hr = IDirectInputDevice8_GetDeviceData( device, sizeof(obj_data), &obj_data, &data_size, 0 ); + ok( hr == DI_OK, "GetDeviceData returned %#x\n", hr ); if (hr != DI_OK) break; }
- IDirectInputDevice_Unacquire(lpdid); + hr = IDirectInputDevice_Unacquire( device ); + ok( hr == DI_OK, "Unacquire returned %#x\n", hr ); + + hr = IDirectInputDevice8_SetEventNotification( device, NULL ); + ok( hr == DI_OK, "SetEventNotification returned %#x\n", hr ); + + CloseHandle( event ); }
static void test_build_action_map(IDirectInputDevice8A *lpdid, DIACTIONFORMATA *lpdiaf,