BusCallTest.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "AzManipulatorTestFrameworkTestFixtures.h"
  9. #include <AzToolsFramework/Viewport/ViewportTypes.h>
  10. #include <AzToolsFramework/ViewportSelection/EditorDefaultSelection.h>
  11. #include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h>
  12. namespace UnitTest
  13. {
  14. class AzManipulatorTestFrameworkBusCallTestFixture : public LinearManipulatorTestFixture
  15. {
  16. protected:
  17. AzManipulatorTestFrameworkBusCallTestFixture()
  18. : LinearManipulatorTestFixture(AzToolsFramework::g_mainManipulatorManagerId)
  19. {
  20. }
  21. bool IsManipulatorInteractingBusCall() const
  22. {
  23. bool manipulatorInteracting = false;
  24. AzToolsFramework::ManipulatorManagerRequestBus::EventResult(
  25. manipulatorInteracting, AzToolsFramework::g_mainManipulatorManagerId,
  26. &AzToolsFramework::ManipulatorManagerRequestBus::Events::Interacting);
  27. return manipulatorInteracting;
  28. }
  29. };
  30. TEST_F(AzManipulatorTestFrameworkBusCallTestFixture, ConsumeViewportLeftMouseClick)
  31. {
  32. // given a left mouse down ray in world space
  33. auto event = AzToolsFramework::ViewportInteraction::BuildMouseInteractionEvent(
  34. m_interaction, AzToolsFramework::ViewportInteraction::MouseEvent::Down);
  35. // consume the mouse down and up events
  36. AzManipulatorTestFramework::DispatchMouseInteractionEvent(event);
  37. event.m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Up;
  38. AzManipulatorTestFramework::DispatchMouseInteractionEvent(event);
  39. // expect the left mouse down and mouse up sanity flags to be set
  40. EXPECT_TRUE(m_receivedLeftMouseDown);
  41. EXPECT_TRUE(m_receivedLeftMouseUp);
  42. // do not expect the mouse move sanity flag to be set
  43. EXPECT_FALSE(m_receivedMouseMove);
  44. }
  45. TEST_F(AzManipulatorTestFrameworkBusCallTestFixture, ConsumeViewportMouseMoveHover)
  46. {
  47. // given a left mouse down ray in world space
  48. const auto event = AzToolsFramework::ViewportInteraction::BuildMouseInteractionEvent(
  49. m_interaction, AzToolsFramework::ViewportInteraction::MouseEvent::Move);
  50. // consume the mouse move event
  51. AzManipulatorTestFramework::DispatchMouseInteractionEvent(event);
  52. // do not expect the manipulator to be performing an action
  53. EXPECT_FALSE(m_linearManipulator->PerformingAction());
  54. EXPECT_FALSE(IsManipulatorInteractingBusCall());
  55. // do not expect the left mouse down/up and mouse move sanity flags to be set
  56. EXPECT_FALSE(m_receivedLeftMouseDown);
  57. EXPECT_FALSE(m_receivedMouseMove);
  58. EXPECT_FALSE(m_receivedLeftMouseUp);
  59. }
  60. TEST_F(AzManipulatorTestFrameworkBusCallTestFixture, ConsumeViewportMouseMoveActive)
  61. {
  62. // given a left mouse down ray in world space
  63. auto event = AzToolsFramework::ViewportInteraction::BuildMouseInteractionEvent(
  64. m_interaction, AzToolsFramework::ViewportInteraction::MouseEvent::Down);
  65. // consume the mouse down event
  66. AzManipulatorTestFramework::DispatchMouseInteractionEvent(event);
  67. // consume the mouse move event
  68. event.m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Move;
  69. AzManipulatorTestFramework::DispatchMouseInteractionEvent(event);
  70. // do not expect the mouse to be hovering over the manipulator
  71. EXPECT_FALSE(m_linearManipulator->MouseOver());
  72. // expect the manipulator to be performing an action
  73. EXPECT_TRUE(m_linearManipulator->PerformingAction());
  74. EXPECT_TRUE(IsManipulatorInteractingBusCall());
  75. // consume the mouse up event
  76. event.m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Up;
  77. AzManipulatorTestFramework::DispatchMouseInteractionEvent(event);
  78. // expect the left mouse down/up and mouse move sanity flags to be set
  79. EXPECT_TRUE(m_receivedLeftMouseDown);
  80. EXPECT_TRUE(m_receivedMouseMove);
  81. EXPECT_TRUE(m_receivedLeftMouseUp);
  82. }
  83. TEST_F(AzManipulatorTestFrameworkBusCallTestFixture, MoveManipulatorAlongAxis)
  84. {
  85. AZ::Vector3 movementAlongAxis = AZ::Vector3::CreateZero();
  86. const AZ::Vector3 expectedMovementAlongAxis = AZ::Vector3(-5.0f, 0.0f, 0.0f);
  87. const AZ::Vector3 initialManipulatorPosition = m_linearManipulator->GetLocalPosition();
  88. m_linearManipulator->InstallMouseMoveCallback(
  89. [&movementAlongAxis, this](const AzToolsFramework::LinearManipulator::Action& action)
  90. {
  91. movementAlongAxis = action.LocalPositionOffset();
  92. m_linearManipulator->SetLocalPosition(action.LocalPosition());
  93. });
  94. // given a left mouse down ray in world space
  95. auto event = AzToolsFramework::ViewportInteraction::BuildMouseInteractionEvent(
  96. m_interaction, AzToolsFramework::ViewportInteraction::MouseEvent::Down);
  97. // consume the mouse down event
  98. AzManipulatorTestFramework::DispatchMouseInteractionEvent(event);
  99. // move the mouse along the -x axis
  100. event.m_mouseInteraction.m_mousePick.m_rayOrigin += expectedMovementAlongAxis;
  101. event.m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Move;
  102. AzManipulatorTestFramework::DispatchMouseInteractionEvent(event);
  103. // expect the manipulator to be performing an action
  104. EXPECT_TRUE(m_linearManipulator->PerformingAction());
  105. EXPECT_TRUE(IsManipulatorInteractingBusCall());
  106. // consume the mouse up event
  107. event.m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Up;
  108. AzManipulatorTestFramework::DispatchMouseInteractionEvent(event);
  109. // expect the left mouse down/up sanity flags to be set
  110. EXPECT_TRUE(m_receivedLeftMouseDown);
  111. EXPECT_TRUE(m_receivedLeftMouseUp);
  112. // expect the manipulator movement along the axis to match the mouse movement along the axis
  113. EXPECT_EQ(movementAlongAxis, expectedMovementAlongAxis);
  114. EXPECT_EQ(m_linearManipulator->GetLocalPosition(), initialManipulatorPosition + expectedMovementAlongAxis);
  115. }
  116. } // namespace UnitTest