From 43c83f13c6f86e12da5ec07c6f64c50c84598f11 Mon Sep 17 00:00:00 2001 From: Chris Burel Date: Tue, 26 Oct 2021 16:35:29 -0700 Subject: [PATCH] [Linux] Add unit tests for xcb mouse input Signed-off-by: Chris Burel --- .../Tests/Platform/Common/Xcb/Actions.h | 35 ++ .../Platform/Common/Xcb/MockXcbInterface.cpp | 91 ++++ .../Platform/Common/Xcb/MockXcbInterface.h | 33 ++ .../Platform/Common/Xcb/XcbBaseTestFixture.h | 6 + .../Xcb/XcbInputDeviceKeyboardTests.cpp | 6 - .../Common/Xcb/XcbInputDeviceMouseTests.cpp | 401 ++++++++++++++++++ .../Xcb/azframework_xcb_tests_files.cmake | 1 + 7 files changed, 567 insertions(+), 6 deletions(-) create mode 100644 Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbInputDeviceMouseTests.cpp diff --git a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/Actions.h b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/Actions.h index 1650ff4f8d..d227729fd3 100644 --- a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/Actions.h +++ b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/Actions.h @@ -11,6 +11,13 @@ #include #include +ACTION_TEMPLATE(ReturnMalloc, + HAS_1_TEMPLATE_PARAMS(typename, T), + AND_0_VALUE_PARAMS()) { + T* value = static_cast(malloc(sizeof(T))); + *value = T{}; + return value; +} ACTION_TEMPLATE(ReturnMalloc, HAS_1_TEMPLATE_PARAMS(typename, T), AND_1_VALUE_PARAMS(p0)) { @@ -25,3 +32,31 @@ ACTION_TEMPLATE(ReturnMalloc, *value = T{ p0, p1 }; return value; } +ACTION_TEMPLATE(ReturnMalloc, + HAS_1_TEMPLATE_PARAMS(typename, T), + AND_3_VALUE_PARAMS(p0, p1, p2)) { + T* value = static_cast(malloc(sizeof(T))); + *value = T{ p0, p1, p2 }; + return value; +} +ACTION_TEMPLATE(ReturnMalloc, + HAS_1_TEMPLATE_PARAMS(typename, T), + AND_4_VALUE_PARAMS(p0, p1, p2, p3)) { + T* value = static_cast(malloc(sizeof(T))); + *value = T{ p0, p1, p2, p3 }; + return value; +} +ACTION_TEMPLATE(ReturnMalloc, + HAS_1_TEMPLATE_PARAMS(typename, T), + AND_5_VALUE_PARAMS(p0, p1, p2, p3, p4)) { + T* value = static_cast(malloc(sizeof(T))); + *value = T{ p0, p1, p2, p3, p4 }; + return value; +} +ACTION_TEMPLATE(ReturnMalloc, + HAS_1_TEMPLATE_PARAMS(typename, T), + AND_6_VALUE_PARAMS(p0, p1, p2, p3, p4, p5)) { + T* value = static_cast(malloc(sizeof(T))); + *value = T{ p0, p1, p2, p3, p4, p5 }; + return value; +} diff --git a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/MockXcbInterface.cpp b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/MockXcbInterface.cpp index b15809a4c6..39d425b799 100644 --- a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/MockXcbInterface.cpp +++ b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/MockXcbInterface.cpp @@ -32,6 +32,51 @@ xcb_generic_error_t* xcb_request_check(xcb_connection_t* c, xcb_void_cookie_t co { return MockXcbInterface::Instance()->xcb_request_check(c, cookie); } +const xcb_setup_t* xcb_get_setup(xcb_connection_t *c) +{ + return MockXcbInterface::Instance()->xcb_get_setup(c); +} +xcb_screen_iterator_t xcb_setup_roots_iterator(const xcb_setup_t* R) +{ + return MockXcbInterface::Instance()->xcb_setup_roots_iterator(R); +} +const xcb_query_extension_reply_t* xcb_get_extension_data(xcb_connection_t* c, xcb_extension_t* ext) +{ + return MockXcbInterface::Instance()->xcb_get_extension_data(c, ext); +} +int xcb_flush(xcb_connection_t *c) +{ + return MockXcbInterface::Instance()->xcb_flush(c); +} +xcb_query_pointer_cookie_t xcb_query_pointer(xcb_connection_t* c, xcb_window_t window) +{ + return MockXcbInterface::Instance()->xcb_query_pointer(c, window); +} +xcb_query_pointer_reply_t* xcb_query_pointer_reply(xcb_connection_t* c, xcb_query_pointer_cookie_t cookie, xcb_generic_error_t** e) +{ + return MockXcbInterface::Instance()->xcb_query_pointer_reply(c, cookie, e); +} +xcb_get_geometry_cookie_t xcb_get_geometry(xcb_connection_t* c, xcb_drawable_t drawable) +{ + return MockXcbInterface::Instance()->xcb_get_geometry(c, drawable); +} +xcb_get_geometry_reply_t* xcb_get_geometry_reply(xcb_connection_t* c, xcb_get_geometry_cookie_t cookie, xcb_generic_error_t** e) +{ + return MockXcbInterface::Instance()->xcb_get_geometry_reply(c, cookie, e); +} +xcb_void_cookie_t xcb_warp_pointer( + xcb_connection_t* c, + xcb_window_t src_window, + xcb_window_t dst_window, + int16_t src_x, + int16_t src_y, + uint16_t src_width, + uint16_t src_height, + int16_t dst_x, + int16_t dst_y) +{ + return MockXcbInterface::Instance()->xcb_warp_pointer(c, src_window, dst_window, src_x, src_y, src_width, src_height, dst_x, dst_y); +} // ---------------------------------------------------------------------------- // xcb-xkb @@ -116,4 +161,50 @@ xkb_state_component xkb_state_update_mask( state, depressed_mods, latched_mods, locked_mods, depressed_layout, latched_layout, locked_layout); } +// ---------------------------------------------------------------------------- +// xcb-xfixes +xcb_xfixes_query_version_cookie_t xcb_xfixes_query_version( + xcb_connection_t* c, uint32_t client_major_version, uint32_t client_minor_version) +{ + return MockXcbInterface::Instance()->xcb_xfixes_query_version(c, client_major_version, client_minor_version); +} +xcb_xfixes_query_version_reply_t* xcb_xfixes_query_version_reply( + xcb_connection_t* c, xcb_xfixes_query_version_cookie_t cookie, xcb_generic_error_t** e) +{ + return MockXcbInterface::Instance()->xcb_xfixes_query_version_reply(c, cookie, e); +} +xcb_void_cookie_t xcb_xfixes_show_cursor_checked(xcb_connection_t* c, xcb_window_t window) +{ + return MockXcbInterface::Instance()->xcb_xfixes_show_cursor_checked(c, window); +} +xcb_void_cookie_t xcb_xfixes_hide_cursor_checked(xcb_connection_t* c, xcb_window_t window) +{ + return MockXcbInterface::Instance()->xcb_xfixes_hide_cursor_checked(c, window); +} + +// ---------------------------------------------------------------------------- +// xcb-xinput +xcb_input_xi_query_version_cookie_t xcb_input_xi_query_version(xcb_connection_t* c, uint16_t major_version, uint16_t minor_version) +{ + return MockXcbInterface::Instance()->xcb_input_xi_query_version(c, major_version, minor_version); +} +xcb_input_xi_query_version_reply_t* xcb_input_xi_query_version_reply( + xcb_connection_t* c, xcb_input_xi_query_version_cookie_t cookie, xcb_generic_error_t** e) +{ + return MockXcbInterface::Instance()->xcb_input_xi_query_version_reply(c, cookie, e); +} +xcb_void_cookie_t xcb_input_xi_select_events( + xcb_connection_t* c, xcb_window_t window, uint16_t num_mask, const xcb_input_event_mask_t* masks) +{ + return MockXcbInterface::Instance()->xcb_input_xi_select_events(c, window, num_mask, masks); +} +int xcb_input_raw_button_press_axisvalues_length (const xcb_input_raw_button_press_event_t *R) +{ + return MockXcbInterface::Instance()->xcb_input_raw_button_press_axisvalues_length(R); +} +xcb_input_fp3232_t* xcb_input_raw_button_press_axisvalues_raw(const xcb_input_raw_button_press_event_t* R) +{ + return MockXcbInterface::Instance()->xcb_input_raw_button_press_axisvalues_raw(R); +} + } diff --git a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/MockXcbInterface.h b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/MockXcbInterface.h index b57751344e..f38e327f50 100644 --- a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/MockXcbInterface.h +++ b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/MockXcbInterface.h @@ -18,6 +18,8 @@ #undef explicit #include #include +#include +#include #include "Printers.h" @@ -62,6 +64,24 @@ public: MOCK_CONST_METHOD1(xcb_disconnect, void(xcb_connection_t* c)); MOCK_CONST_METHOD1(xcb_poll_for_event, xcb_generic_event_t*(xcb_connection_t* c)); MOCK_CONST_METHOD2(xcb_request_check, xcb_generic_error_t*(xcb_connection_t* c, xcb_void_cookie_t cookie)); + MOCK_CONST_METHOD1(xcb_get_setup, const xcb_setup_t*(xcb_connection_t *c)); + MOCK_CONST_METHOD1(xcb_setup_roots_iterator, xcb_screen_iterator_t(const xcb_setup_t* R)); + MOCK_CONST_METHOD2(xcb_get_extension_data, const xcb_query_extension_reply_t*(xcb_connection_t* c, xcb_extension_t* ext)); + MOCK_CONST_METHOD1(xcb_flush, int(xcb_connection_t *c)); + MOCK_CONST_METHOD2(xcb_query_pointer, xcb_query_pointer_cookie_t(xcb_connection_t* c, xcb_window_t window)); + MOCK_CONST_METHOD3(xcb_query_pointer_reply, xcb_query_pointer_reply_t*(xcb_connection_t* c, xcb_query_pointer_cookie_t cookie, xcb_generic_error_t** e)); + MOCK_CONST_METHOD2(xcb_get_geometry, xcb_get_geometry_cookie_t(xcb_connection_t* c, xcb_drawable_t drawable)); + MOCK_CONST_METHOD3(xcb_get_geometry_reply, xcb_get_geometry_reply_t*(xcb_connection_t* c, xcb_get_geometry_cookie_t cookie, xcb_generic_error_t** e)); + MOCK_CONST_METHOD9(xcb_warp_pointer, xcb_void_cookie_t( + xcb_connection_t* c, + xcb_window_t src_window, + xcb_window_t dst_window, + int16_t src_x, + int16_t src_y, + uint16_t src_width, + uint16_t src_height, + int16_t dst_x, + int16_t dst_y)); // xcb-xkb MOCK_CONST_METHOD3(xcb_xkb_use_extension, xcb_xkb_use_extension_cookie_t(xcb_connection_t* c, uint16_t wantedMajor, uint16_t wantedMinor)); @@ -83,6 +103,19 @@ public: MOCK_CONST_METHOD4(xkb_state_key_get_utf8, int(xkb_state* state, xkb_keycode_t key, char* buffer, size_t size)); MOCK_CONST_METHOD7(xkb_state_update_mask, xkb_state_component(xkb_state* state, xkb_mod_mask_t depressed_mods, xkb_mod_mask_t latched_mods, xkb_mod_mask_t locked_mods, xkb_layout_index_t depressed_layout, xkb_layout_index_t latched_layout, xkb_layout_index_t locked_layout)); + // xcb-xfixes + MOCK_CONST_METHOD3(xcb_xfixes_query_version, xcb_xfixes_query_version_cookie_t(xcb_connection_t* c, uint32_t client_major_version, uint32_t client_minor_version)); + MOCK_CONST_METHOD3(xcb_xfixes_query_version_reply, xcb_xfixes_query_version_reply_t*(xcb_connection_t* c, xcb_xfixes_query_version_cookie_t cookie, xcb_generic_error_t** e)); + MOCK_CONST_METHOD2(xcb_xfixes_show_cursor_checked, xcb_void_cookie_t(xcb_connection_t* c, xcb_window_t window)); + MOCK_CONST_METHOD2(xcb_xfixes_hide_cursor_checked, xcb_void_cookie_t(xcb_connection_t* c, xcb_window_t window)); + + // xcb-xinput + MOCK_CONST_METHOD3(xcb_input_xi_query_version, xcb_input_xi_query_version_cookie_t(xcb_connection_t* c, uint16_t major_version, uint16_t minor_version)); + MOCK_CONST_METHOD3(xcb_input_xi_query_version_reply, xcb_input_xi_query_version_reply_t*(xcb_connection_t* c, xcb_input_xi_query_version_cookie_t cookie, xcb_generic_error_t** e)); + MOCK_CONST_METHOD4(xcb_input_xi_select_events, xcb_void_cookie_t(xcb_connection_t* c, xcb_window_t window, uint16_t num_mask, const xcb_input_event_mask_t* masks)); + MOCK_CONST_METHOD1(xcb_input_raw_button_press_axisvalues_length, int(const xcb_input_raw_button_press_event_t* R)); + MOCK_CONST_METHOD1(xcb_input_raw_button_press_axisvalues_raw, xcb_input_fp3232_t*(const xcb_input_raw_button_press_event_t* R)); + private: static inline MockXcbInterface* self = nullptr; }; diff --git a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbBaseTestFixture.h b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbBaseTestFixture.h index 8e9b008fc1..2a63a5f158 100644 --- a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbBaseTestFixture.h +++ b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbBaseTestFixture.h @@ -22,6 +22,12 @@ namespace AzFramework public: void SetUp() override; + template + static xcb_generic_event_t MakeEvent(T event) + { + return *reinterpret_cast(&event); + } + protected: testing::NiceMock m_interface; xcb_connection_t m_connection{}; diff --git a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbInputDeviceKeyboardTests.cpp b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbInputDeviceKeyboardTests.cpp index 76d00eda50..7209875235 100644 --- a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbInputDeviceKeyboardTests.cpp +++ b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbInputDeviceKeyboardTests.cpp @@ -21,12 +21,6 @@ #include "XcbBaseTestFixture.h" #include "XcbTestApplication.h" -template -xcb_generic_event_t MakeEvent(T event) -{ - return *reinterpret_cast(&event); -} - namespace AzFramework { // Sets up default behavior for mock keyboard responses to xcb methods diff --git a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbInputDeviceMouseTests.cpp b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbInputDeviceMouseTests.cpp new file mode 100644 index 0000000000..40dfdec26f --- /dev/null +++ b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/XcbInputDeviceMouseTests.cpp @@ -0,0 +1,401 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#include +#include + +#include + +#include +#include + +#include "XcbBaseTestFixture.h" +#include "XcbTestApplication.h" +#include "Matchers.h" +#include "Actions.h" + +namespace AzFramework +{ + // Sets up default behavior for mock keyboard responses to xcb methods + class XcbInputDeviceMouseTests + : public XcbBaseTestFixture + { + public: + void SetUp() override + { + using testing::Return; + using testing::_; + + XcbBaseTestFixture::SetUp(); + + ON_CALL(m_interface, xcb_get_setup(&m_connection)) + .WillByDefault(Return(&s_xcbSetup)); + ON_CALL(m_interface, xcb_setup_roots_iterator(&s_xcbSetup)) + .WillByDefault(Return(xcb_screen_iterator_t{&s_xcbScreen})); + + ON_CALL(m_interface, xcb_get_extension_data(&m_connection, &xcb_xfixes_id)) + .WillByDefault(Return(&s_xfixesExtensionReply)); + ON_CALL(m_interface, xcb_xfixes_query_version_reply(&m_connection, _, _)) + .WillByDefault(ReturnMalloc( + /*response_type=*/(uint8_t)XCB_XFIXES_QUERY_VERSION, + /*pad0=*/(uint8_t)0, + /*sequence=*/(uint16_t)1, + /*length=*/0u, + /*major_version=*/5u, + /*minor_version=*/0u + )); + + ON_CALL(m_interface, xcb_get_extension_data(&m_connection, &xcb_input_id)) + .WillByDefault(Return(&s_xfixesExtensionReply)); + ON_CALL(m_interface, xcb_input_xi_query_version_reply(&m_connection, _, _)) + .WillByDefault(ReturnMalloc( + /*response_type=*/(uint8_t)XCB_INPUT_XI_QUERY_VERSION, + /*pad0=*/(uint8_t)0, + /*sequence=*/(uint16_t)1, + /*length=*/0u, + /*major_version=*/(uint16_t)2, + /*minor_version=*/(uint16_t)2 + )); + } + + void PumpApplication() + { + m_application.PumpSystemEventLoopUntilEmpty(); + m_application.TickSystem(); + m_application.Tick(); + } + + protected: + static constexpr inline uint8_t s_xinputMajorOpcode = 131; + static constexpr inline xcb_window_t s_rootWindow = 1; + static constexpr inline xcb_input_device_id_t s_virtualCorePointerId = 2; + static constexpr inline xcb_input_device_id_t s_physicalPointerDeviceId = 3; + static constexpr inline uint16_t s_screenWidthInPixels = 3840; + static constexpr inline uint16_t s_screenHeightInPixels = 2160; + static constexpr inline xcb_setup_t s_xcbSetup{ + /*.status=*/1, + /*.pad0=*/0, + /*.protocol_major_version=*/11, + /*.protocol_minor_version=*/0, + }; + static inline xcb_screen_t s_xcbScreen{ + /*.root=*/s_rootWindow, + /*.default_colormap=*/32, + /*.white_pixel=*/16777215, + /*.black_pixel=*/0, + /*.current_input_masks=*/0, + /*.width_in_pixels=*/s_screenWidthInPixels, + /*.height_in_pixels=*/s_screenHeightInPixels, + /*.width_in_millimeters=*/602, + /*.height_in_millimeters=*/341, + }; + static constexpr inline xcb_query_extension_reply_t s_xfixesExtensionReply{ + /*.response_type=*/XCB_QUERY_EXTENSION, + /*.pad0=*/0, + /*.sequence=*/1, + /*.length=*/0, + /*.present=*/1, + }; + static constexpr inline xcb_query_extension_reply_t s_xinputExtensionReply{ + /*.response_type=*/XCB_QUERY_EXTENSION, + /*.pad0=*/0, + /*.sequence=*/1, + /*.length=*/0, + /*.present=*/1, + /*.major_opcode=*/s_xinputMajorOpcode, + }; + XcbTestApplication m_application{ + /*enabledGamepadsCount=*/0, + /*keyboardEnabled=*/false, + /*motionEnabled=*/false, + /*mouseEnabled=*/true, + /*touchEnabled=*/false, + /*virtualKeyboardEnabled=*/false + }; + }; + + struct MouseButtonTestData + { + xcb_button_index_t m_button; + }; + + class XcbInputDeviceMouseButtonTests + : public XcbInputDeviceMouseTests + , public testing::WithParamInterface + { + public: + static InputChannelId GetInputChannelIdForButton(const xcb_button_index_t button) + { + switch (button) + { + case XCB_BUTTON_INDEX_1: + return InputDeviceMouse::Button::Left; + case XCB_BUTTON_INDEX_2: + return InputDeviceMouse::Button::Right; + case XCB_BUTTON_INDEX_3: + return InputDeviceMouse::Button::Middle; + } + return InputChannelId{}; + } + + AZStd::array GetIdleChannelIdsForButton(const xcb_button_index_t button) + { + switch (button) + { + case XCB_BUTTON_INDEX_1: + return { InputDeviceMouse::Button::Right, InputDeviceMouse::Button::Middle, InputDeviceMouse::Button::Other1, InputDeviceMouse::Button::Other2 }; + case XCB_BUTTON_INDEX_2: + return { InputDeviceMouse::Button::Left, InputDeviceMouse::Button::Middle, InputDeviceMouse::Button::Other1, InputDeviceMouse::Button::Other2 }; + case XCB_BUTTON_INDEX_3: + return { InputDeviceMouse::Button::Left, InputDeviceMouse::Button::Right, InputDeviceMouse::Button::Other1, InputDeviceMouse::Button::Other2 }; + case XCB_BUTTON_INDEX_4: + return { InputDeviceMouse::Button::Left, InputDeviceMouse::Button::Right, InputDeviceMouse::Button::Middle, InputDeviceMouse::Button::Other2 }; + case XCB_BUTTON_INDEX_5: + return { InputDeviceMouse::Button::Left, InputDeviceMouse::Button::Right, InputDeviceMouse::Button::Middle, InputDeviceMouse::Button::Other1 }; + } + return AZStd::array(); + } + }; + + TEST_P(XcbInputDeviceMouseButtonTests, ButtonInputChannelsUpdateStateFromXcbEvents) + { + using testing::Each; + using testing::Eq; + using testing::NotNull; + using testing::Property; + using testing::Return; + + // Set the expectations for the events that will be generated + // nullptr entries represent when the event queue is empty, and will cause + // PumpSystemEventLoopUntilEmpty to return + // + // Event pointers are freed by the calling code, so these actions + // malloc new copies + // + // The xcb mouse does not react to the `XCB_BUTTON_PRESS` / + // `XCB_BUTTON_RELEASE` events, but it will still receive those events + // from the X server. + EXPECT_CALL(m_interface, xcb_poll_for_event(&m_connection)) + .WillOnce(ReturnMalloc(MakeEvent(xcb_input_raw_button_press_event_t{ + /*response_type=*/XCB_GE_GENERIC, + /*extension=*/s_xinputMajorOpcode, + /*sequence=*/4, + /*length=*/2, + /*event_type=*/XCB_INPUT_RAW_BUTTON_PRESS, + /*deviceid=*/s_virtualCorePointerId, + /*time=*/3984920, + /*detail=*/GetParam().m_button, + /*sourceid=*/s_physicalPointerDeviceId, + /*valuators_len=*/2, + /*flags=*/0, + /*pad0[4]=*/{}, + /*full_sequence=*/4 + }))) + .WillOnce(Return(nullptr)) + .WillOnce(ReturnMalloc(MakeEvent(xcb_button_press_event_t{ + /*response_type=*/XCB_BUTTON_PRESS, + /*detail=*/static_cast(GetParam().m_button), + /*sequence=*/4, + /*time=*/3984920, + /*root=*/s_rootWindow, + /*event=*/119537664, + /*child=*/0, + /*root_x=*/55, + /*root_y=*/1099, + /*event_x=*/55, + /*event_y=*/55, + /*state=*/0, + /*same_screen=*/1 + }))) + .WillOnce(Return(nullptr)) + .WillOnce(ReturnMalloc(MakeEvent(xcb_input_raw_button_release_event_t{ + /*response_type=*/XCB_GE_GENERIC, + /*extension=*/s_xinputMajorOpcode, + /*sequence=*/4, + /*length=*/2, + /*event_type=*/XCB_INPUT_RAW_BUTTON_RELEASE, + /*deviceid=*/s_virtualCorePointerId, + /*time=*/3984964, + /*detail=*/GetParam().m_button, + /*sourceid=*/s_physicalPointerDeviceId, + /*valuators_len=*/2, + /*flags=*/0, + /*pad0[4]=*/{}, + /*full_sequence=*/4 + }))) + .WillOnce(Return(nullptr)) + .WillOnce(ReturnMalloc(MakeEvent(xcb_button_release_event_t{ + /*response_type=*/XCB_BUTTON_RELEASE, + /*detail=*/static_cast(GetParam().m_button), + /*sequence=*/4, + /*time=*/3984964, + /*root=*/s_rootWindow, + /*event=*/119537664, + /*child=*/0, + /*root_x=*/55, + /*root_y=*/1099, + /*event_x=*/55, + /*event_y=*/55, + /*state=*/XCB_KEY_BUT_MASK_BUTTON_1, + /*same_screen=*/1 + }))) + .WillOnce(Return(nullptr)) + ; + + m_application.Start(); + InputSystemCursorRequestBus::Event( + InputDeviceMouse::Id, + &InputSystemCursorRequests::SetSystemCursorState, + SystemCursorState::ConstrainedAndHidden); + + const InputChannel* activeButtonChannel = InputChannelRequests::FindInputChannel(GetInputChannelIdForButton(GetParam().m_button)); + const auto inactiveButtonChannels = [this]() + { + const auto inactiveButtonChannelIds = GetIdleChannelIdsForButton(GetParam().m_button); + AZStd::array channels{}; + AZStd::transform(begin(inactiveButtonChannelIds), end(inactiveButtonChannelIds), begin(channels), [](const InputChannelId& id) + { + return InputChannelRequests::FindInputChannel(id); + }); + return channels; + }(); + + ASSERT_TRUE(activeButtonChannel); + ASSERT_THAT(inactiveButtonChannels, Each(NotNull())); + + EXPECT_THAT(activeButtonChannel->GetState(), Eq(InputChannel::State::Idle)); + EXPECT_THAT(inactiveButtonChannels, Each(Property(&InputChannel::GetState, Eq(InputChannel::State::Idle)))); + + PumpApplication(); + + EXPECT_THAT(activeButtonChannel->GetState(), Eq(InputChannel::State::Began)); + EXPECT_THAT(inactiveButtonChannels, Each(Property(&InputChannel::GetState, Eq(InputChannel::State::Idle)))); + + PumpApplication(); + + EXPECT_THAT(activeButtonChannel->GetState(), Eq(InputChannel::State::Updated)); + EXPECT_THAT(inactiveButtonChannels, Each(Property(&InputChannel::GetState, Eq(InputChannel::State::Idle)))); + + PumpApplication(); + + EXPECT_THAT(activeButtonChannel->GetState(), Eq(InputChannel::State::Ended)); + EXPECT_THAT(inactiveButtonChannels, Each(Property(&InputChannel::GetState, Eq(InputChannel::State::Idle)))); + + PumpApplication(); + + EXPECT_THAT(activeButtonChannel->GetState(), Eq(InputChannel::State::Idle)); + EXPECT_THAT(inactiveButtonChannels, Each(Property(&InputChannel::GetState, Eq(InputChannel::State::Idle)))); + } + + INSTANTIATE_TEST_CASE_P( + AllButtons, + XcbInputDeviceMouseButtonTests, + testing::Values( + MouseButtonTestData{ XCB_BUTTON_INDEX_1 }, + MouseButtonTestData{ XCB_BUTTON_INDEX_2 }, + MouseButtonTestData{ XCB_BUTTON_INDEX_3 } + // XCB_BUTTON_INDEX_4 and XCB_BUTTON_INDEX_5 map to positive and + // negative scroll wheel events, which are handled as motion events + ) + ); + + TEST_F(XcbInputDeviceMouseTests, MovementInputChannelsUpdateStateFromXcbEvents) + { + using testing::Each; + using testing::Eq; + using testing::FloatEq; + using testing::NotNull; + using testing::Property; + using testing::Return; + + // Set the expectations for the events that will be generated + // nullptr entries represent when the event queue is empty, and will cause + // PumpSystemEventLoopUntilEmpty to return + // + // Event pointers are freed by the calling code, so these actions + // malloc new copies + // + // The xcb mouse does not react to the `XCB_MOTION_NOTIFY` event, but + // it will still receive it from the X server. + EXPECT_CALL(m_interface, xcb_poll_for_event(&m_connection)) + .WillOnce(ReturnMalloc(MakeEvent(xcb_input_raw_motion_event_t{ + /*response_type=*/XCB_GE_GENERIC, + /*extension=*/s_xinputMajorOpcode, + /*sequence=*/5, + /*length=*/10, + /*event_type=*/XCB_INPUT_RAW_MOTION, + /*deviceid=*/s_virtualCorePointerId, + /*time=*/0, // use the time value to identify each event + /*detail=*/XCB_MOTION_NORMAL, + /*sourceid=*/s_physicalPointerDeviceId, + /*valuators_len=*/2, // number of axes that have values for this event + /*flags=*/0, + /*pad0[4]=*/{}, + /*full_sequence=*/5, + }))) + .WillOnce(Return(nullptr)) + .WillOnce(ReturnMalloc(MakeEvent(xcb_motion_notify_event_t{ + /*response_type=*/XCB_MOTION_NOTIFY, + /*detail=*/XCB_MOTION_NORMAL, + /*sequence=*/5, + /*time=*/1, // use the time value to identify each event + /*root=*/s_rootWindow, + /*event=*/127926272, + /*child=*/0, + /*root_x=*/95, + /*root_y=*/1079, + /*event_x=*/95, + /*event_y=*/20, + /*state=*/0, + /*same_screen=*/1, + }))) + .WillOnce(Return(nullptr)) + ; + + AZStd::array axisValues + { + xcb_input_fp3232_t{ /*.integral=*/ 1, /*.fraction=*/0 }, // x motion + xcb_input_fp3232_t{ /*.integral=*/ 2, /*.fraction=*/0 } // y motion + }; + + EXPECT_CALL(m_interface, xcb_input_raw_button_press_axisvalues_length(testing::Field(&xcb_input_raw_button_press_event_t::time, 0))) + .WillRepeatedly(testing::Return(2)); // x and y axis + EXPECT_CALL(m_interface, xcb_input_raw_button_press_axisvalues_raw(testing::Field(&xcb_input_raw_button_press_event_t::time, 0))) + .WillRepeatedly(testing::Return(axisValues.data())); // x and y axis + + m_application.Start(); + InputSystemCursorRequestBus::Event( + InputDeviceMouse::Id, + &InputSystemCursorRequests::SetSystemCursorState, + SystemCursorState::ConstrainedAndHidden); + + const InputChannel* xMotionChannel = InputChannelRequests::FindInputChannel(InputDeviceMouse::Movement::X); + const InputChannel* yMotionChannel = InputChannelRequests::FindInputChannel(InputDeviceMouse::Movement::Y); + ASSERT_TRUE(xMotionChannel); + ASSERT_TRUE(yMotionChannel); + + EXPECT_THAT(xMotionChannel->GetState(), Eq(InputChannel::State::Idle)); + EXPECT_THAT(yMotionChannel->GetState(), Eq(InputChannel::State::Idle)); + EXPECT_THAT(xMotionChannel->GetValue(), FloatEq(0.0f)); + EXPECT_THAT(yMotionChannel->GetValue(), FloatEq(0.0f)); + + PumpApplication(); + + EXPECT_THAT(xMotionChannel->GetState(), Eq(InputChannel::State::Began)); + EXPECT_THAT(yMotionChannel->GetState(), Eq(InputChannel::State::Began)); + EXPECT_THAT(xMotionChannel->GetValue(), FloatEq(1.0f)); + EXPECT_THAT(yMotionChannel->GetValue(), FloatEq(2.0f)); + + PumpApplication(); + + EXPECT_THAT(xMotionChannel->GetState(), Eq(InputChannel::State::Ended)); + EXPECT_THAT(yMotionChannel->GetState(), Eq(InputChannel::State::Ended)); + EXPECT_THAT(xMotionChannel->GetValue(), FloatEq(0.0f)); + EXPECT_THAT(yMotionChannel->GetValue(), FloatEq(0.0f)); + } +} // namespace AzFramework diff --git a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/azframework_xcb_tests_files.cmake b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/azframework_xcb_tests_files.cmake index 147fd2bfe1..7da00fa18c 100644 --- a/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/azframework_xcb_tests_files.cmake +++ b/Code/Framework/AzFramework/Tests/Platform/Common/Xcb/azframework_xcb_tests_files.cmake @@ -17,5 +17,6 @@ set(FILES XcbBaseTestFixture.cpp XcbBaseTestFixture.h XcbInputDeviceKeyboardTests.cpp + XcbInputDeviceMouseTests.cpp XcbTestApplication.h )