test_ViewportManipulatorController.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Settings/SettingsRegistryImpl.h>
  9. #include <AzFramework/Viewport/CameraInput.h>
  10. #include <AzFramework/Viewport/ViewportControllerList.h>
  11. #include <AzToolsFramework/Input/QtEventToAzInputMapper.h>
  12. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  13. #include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h>
  14. #include <Editor/ViewportManipulatorController.h>
  15. #include <Mocks/MockWindowRequests.h>
  16. namespace UnitTest
  17. {
  18. using AzToolsFramework::ViewportInteraction::MouseInteractionEvent;
  19. class EditorInteractionViewportSelectionFake : public AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::Handler
  20. {
  21. public:
  22. void Connect();
  23. void Disconnect();
  24. // EditorInteractionSystemViewportSelectionRequestBus overrides ...
  25. const AzToolsFramework::EditorVisibleEntityDataCacheInterface* GetEntityDataCache() const override;
  26. void SetHandler(const AzToolsFramework::ViewportSelectionRequestsBuilderFn& interactionRequestsBuilder) override;
  27. void SetDefaultHandler() override;
  28. bool InternalHandleMouseViewportInteraction(const MouseInteractionEvent& mouseInteraction) override;
  29. bool InternalHandleMouseManipulatorInteraction(const MouseInteractionEvent& mouseInteraction) override;
  30. AZStd::function<bool(const MouseInteractionEvent& mouseInteraction)> m_internalHandleMouseViewportInteraction;
  31. AZStd::function<bool(const MouseInteractionEvent& mouseInteraction)> m_internalHandleMouseManipulatorInteraction;
  32. };
  33. void EditorInteractionViewportSelectionFake::Connect()
  34. {
  35. AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::Handler::BusConnect(AzToolsFramework::GetEntityContextId());
  36. }
  37. void EditorInteractionViewportSelectionFake::Disconnect()
  38. {
  39. AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::Handler::BusDisconnect();
  40. }
  41. const AzToolsFramework::EditorVisibleEntityDataCacheInterface* EditorInteractionViewportSelectionFake::GetEntityDataCache() const
  42. {
  43. return nullptr;
  44. }
  45. void EditorInteractionViewportSelectionFake::SetHandler(
  46. [[maybe_unused]] const AzToolsFramework::ViewportSelectionRequestsBuilderFn& interactionRequestsBuilder)
  47. {
  48. // noop
  49. }
  50. void EditorInteractionViewportSelectionFake::SetDefaultHandler()
  51. {
  52. // noop
  53. }
  54. bool EditorInteractionViewportSelectionFake::InternalHandleMouseViewportInteraction(const MouseInteractionEvent& mouseInteraction)
  55. {
  56. if (m_internalHandleMouseViewportInteraction)
  57. {
  58. return m_internalHandleMouseViewportInteraction(mouseInteraction);
  59. }
  60. return false;
  61. }
  62. bool EditorInteractionViewportSelectionFake::InternalHandleMouseManipulatorInteraction(const MouseInteractionEvent& mouseInteraction)
  63. {
  64. if (m_internalHandleMouseManipulatorInteraction)
  65. {
  66. return m_internalHandleMouseManipulatorInteraction(mouseInteraction);
  67. }
  68. return false;
  69. }
  70. class ViewportManipulatorControllerFixture : public LeakDetectionFixture
  71. {
  72. public:
  73. static inline constexpr AzFramework::ViewportId TestViewportId = 1234;
  74. static inline const QSize WidgetSize = QSize(1920, 1080);
  75. void SetUp() override
  76. {
  77. LeakDetectionFixture::SetUp();
  78. m_rootWidget = AZStd::make_unique<QWidget>();
  79. m_rootWidget->setFixedSize(WidgetSize);
  80. QApplication::setActiveWindow(m_rootWidget.get());
  81. m_controllerList = AZStd::make_shared<AzFramework::ViewportControllerList>();
  82. m_controllerList->RegisterViewportContext(TestViewportId);
  83. m_inputChannelMapper = AZStd::make_unique<AzToolsFramework::QtEventToAzInputMapper>(m_rootWidget.get(), TestViewportId);
  84. m_settingsRegistry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  85. AZ::SettingsRegistry::Register(m_settingsRegistry.get());
  86. }
  87. void TearDown() override
  88. {
  89. AZ::SettingsRegistry::Unregister(m_settingsRegistry.get());
  90. m_settingsRegistry.reset();
  91. m_inputChannelMapper.reset();
  92. m_controllerList->UnregisterViewportContext(TestViewportId);
  93. m_controllerList.reset();
  94. m_rootWidget.reset();
  95. QApplication::setActiveWindow(nullptr);
  96. LeakDetectionFixture::TearDown();
  97. }
  98. AZStd::unique_ptr<QWidget> m_rootWidget;
  99. AzFramework::ViewportControllerListPtr m_controllerList;
  100. AZStd::unique_ptr<AzToolsFramework::QtEventToAzInputMapper> m_inputChannelMapper;
  101. AZStd::unique_ptr<AZ::SettingsRegistryInterface> m_settingsRegistry;
  102. };
  103. TEST_F(ViewportManipulatorControllerFixture, AnEventIsNotPropagatedToTheViewportWhenAManipulatorHandlesItFirst)
  104. {
  105. // forward input events to our controller list
  106. QObject::connect(
  107. m_inputChannelMapper.get(), &AzToolsFramework::QtEventToAzInputMapper::InputChannelUpdated, m_rootWidget.get(),
  108. [this](const AzFramework::InputChannel* inputChannel, [[maybe_unused]] QEvent* event)
  109. {
  110. m_controllerList->HandleInputChannelEvent(
  111. AzFramework::ViewportControllerInputEvent{ TestViewportId, nullptr, *inputChannel });
  112. });
  113. EditorInteractionViewportSelectionFake editorInteractionViewportFake;
  114. editorInteractionViewportFake.m_internalHandleMouseManipulatorInteraction = [](const MouseInteractionEvent&)
  115. {
  116. // report the event was handled (manipulator was interacted with)
  117. return true;
  118. };
  119. bool viewportInteractionCalled = false;
  120. editorInteractionViewportFake.m_internalHandleMouseViewportInteraction = [&viewportInteractionCalled](const MouseInteractionEvent&)
  121. {
  122. // we should not call this as the manipulator will have consumed this event
  123. viewportInteractionCalled = true;
  124. return true;
  125. };
  126. editorInteractionViewportFake.Connect();
  127. m_controllerList->Add(AZStd::make_shared<SandboxEditor::ViewportManipulatorController>());
  128. // simulate a press and move
  129. MousePressAndMove(m_rootWidget.get(), QPoint(10, 10), QPoint(10, 10), Qt::MouseButton::LeftButton);
  130. MouseMove(m_rootWidget.get(), QPoint(20, 20), QPoint(10, 10), Qt::MouseButton::LeftButton);
  131. MouseMove(m_rootWidget.get(), QPoint(30, 30), QPoint(0, 0), Qt::MouseButton::LeftButton);
  132. QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(30, 30));
  133. // ensure the viewport did not receive the event when it was intercepted first by the manipulator
  134. EXPECT_FALSE(viewportInteractionCalled);
  135. editorInteractionViewportFake.Disconnect();
  136. }
  137. TEST_F(ViewportManipulatorControllerFixture, ChangingFocusDoesNotClearInput)
  138. {
  139. bool endedEvent = false;
  140. // detect input events and ensure that the Alt key press does not end before the end of the test
  141. QObject::connect(
  142. m_inputChannelMapper.get(), &AzToolsFramework::QtEventToAzInputMapper::InputChannelUpdated, m_rootWidget.get(),
  143. [&endedEvent](const AzFramework::InputChannel* inputChannel, [[maybe_unused]] QEvent* event)
  144. {
  145. if (inputChannel->GetInputChannelId() == AzFramework::InputDeviceKeyboard::Key::ModifierAltL &&
  146. inputChannel->IsStateEnded())
  147. {
  148. endedEvent = true;
  149. }
  150. });
  151. // given
  152. auto* secondaryWidget = new QWidget(m_rootWidget.get());
  153. m_rootWidget->show();
  154. secondaryWidget->show();
  155. m_rootWidget->setFocus();
  156. // simulate a key press when root widget has focus
  157. QTest::keyPress(m_rootWidget.get(), Qt::Key_Alt, Qt::KeyboardModifier::AltModifier);
  158. // when
  159. // change focus to secondary widget
  160. secondaryWidget->setFocus();
  161. // then
  162. // the alt key was not released (cleared)
  163. EXPECT_FALSE(endedEvent);
  164. }
  165. // note: Application State Change includes events such as switching to another application or minimizing
  166. // the current application
  167. TEST_F(ViewportManipulatorControllerFixture, ApplicationStateChangeDoesClearInput)
  168. {
  169. bool endedEvent = false;
  170. // detect input events and ensure that the Alt key press does not end before the end of the test
  171. QObject::connect(
  172. m_inputChannelMapper.get(), &AzToolsFramework::QtEventToAzInputMapper::InputChannelUpdated, m_rootWidget.get(),
  173. [&endedEvent](const AzFramework::InputChannel* inputChannel, [[maybe_unused]] QEvent* event)
  174. {
  175. if (inputChannel->GetInputChannelId() == AzFramework::InputDeviceKeyboard::Key::AlphanumericW &&
  176. inputChannel->IsStateEnded())
  177. {
  178. endedEvent = true;
  179. }
  180. });
  181. // given
  182. auto* secondaryWidget = new QWidget(m_rootWidget.get());
  183. m_rootWidget->show();
  184. secondaryWidget->show();
  185. m_rootWidget->setFocus();
  186. // simulate a key press when root widget has focus
  187. QTest::keyPress(m_rootWidget.get(), Qt::Key_W);
  188. // when
  189. // simulate changing the window state
  190. QApplicationStateChangeEvent applicationStateChangeEvent(Qt::ApplicationState::ApplicationInactive);
  191. QCoreApplication::sendEvent(m_rootWidget.get(), &applicationStateChangeEvent);
  192. // then
  193. // the key was released (cleared)
  194. EXPECT_TRUE(endedEvent);
  195. }
  196. TEST_F(ViewportManipulatorControllerFixture, DoubleClickIsNotRegisteredIfMouseDeltaHasMovedMoreThanDeadzoneInClickInterval)
  197. {
  198. AzFramework::NativeWindowHandle nativeWindowHandle = nullptr;
  199. // forward input events to our controller list
  200. QObject::connect(
  201. m_inputChannelMapper.get(), &AzToolsFramework::QtEventToAzInputMapper::InputChannelUpdated, m_rootWidget.get(),
  202. [this, nativeWindowHandle](const AzFramework::InputChannel* inputChannel, [[maybe_unused]] QEvent* event)
  203. {
  204. m_controllerList->HandleInputChannelEvent(
  205. AzFramework::ViewportControllerInputEvent{ TestViewportId, nativeWindowHandle, *inputChannel });
  206. });
  207. ::testing::NiceMock<MockWindowRequests> mockWindowRequests;
  208. mockWindowRequests.Connect(nativeWindowHandle);
  209. using ::testing::Return;
  210. // note: WindowRequests is used internally by ViewportManipulatorController
  211. ON_CALL(mockWindowRequests, GetClientAreaSize())
  212. .WillByDefault(Return(AzFramework::WindowSize(WidgetSize.width(), WidgetSize.height())));
  213. ON_CALL(mockWindowRequests, GetRenderResolution())
  214. .WillByDefault(Return(AzFramework::WindowSize(WidgetSize.width(), WidgetSize.height())));
  215. EditorInteractionViewportSelectionFake editorInteractionViewportFake;
  216. editorInteractionViewportFake.m_internalHandleMouseManipulatorInteraction = [](const MouseInteractionEvent&)
  217. {
  218. // report the event was not handled (manipulator was not interacted with)
  219. return false;
  220. };
  221. bool doubleClickDetected = false;
  222. editorInteractionViewportFake.m_internalHandleMouseViewportInteraction =
  223. [&doubleClickDetected](const MouseInteractionEvent& mouseInteractionEvent)
  224. {
  225. // ensure no double click event is detected with the given inputs below
  226. if (mouseInteractionEvent.m_mouseEvent == AzToolsFramework::ViewportInteraction::MouseEvent::DoubleClick)
  227. {
  228. doubleClickDetected = true;
  229. }
  230. return true;
  231. };
  232. editorInteractionViewportFake.Connect();
  233. m_controllerList->Add(AZStd::make_shared<SandboxEditor::ViewportManipulatorController>());
  234. // simulate a click, move, click
  235. MouseMove(m_rootWidget.get(), QPoint(0, 0), QPoint(10, 10));
  236. MousePressAndMove(m_rootWidget.get(), QPoint(10, 10), QPoint(0, 0), Qt::MouseButton::LeftButton);
  237. QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(10, 10));
  238. MouseMove(m_rootWidget.get(), QPoint(10, 10), QPoint(20, 20));
  239. MousePressAndMove(m_rootWidget.get(), QPoint(20, 20), QPoint(0, 0), Qt::MouseButton::LeftButton);
  240. QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(20, 20));
  241. // ensure no double click was detected
  242. EXPECT_FALSE(doubleClickDetected);
  243. // simulate double click (sanity check it still is detected correctly with no movement)
  244. MouseMove(m_rootWidget.get(), QPoint(0, 0), QPoint(10, 10));
  245. MousePressAndMove(m_rootWidget.get(), QPoint(10, 10), QPoint(0, 0), Qt::MouseButton::LeftButton);
  246. QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(10, 10));
  247. MousePressAndMove(m_rootWidget.get(), QPoint(10, 10), QPoint(0, 0), Qt::MouseButton::LeftButton);
  248. QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(10, 10));
  249. // ensure a double click was detected
  250. EXPECT_TRUE(doubleClickDetected);
  251. mockWindowRequests.Disconnect();
  252. editorInteractionViewportFake.Disconnect();
  253. }
  254. } // namespace UnitTest