EditorViewportHelperTests.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/UnitTest/TestTypes.h>
  9. #include <AzFramework/UnitTest/TestDebugDisplayRequests.h>
  10. #include <AzFramework/Viewport/CameraState.h>
  11. #include <AzTest/AzTest.h>
  12. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  13. #include <AzToolsFramework/UnitTest/Mocks/MockEditorViewportIconDisplayInterface.h>
  14. #include <AzToolsFramework/UnitTest/Mocks/MockEditorVisibleEntityDataCacheInterface.h>
  15. #include <AzToolsFramework/UnitTest/Mocks/MockFocusModeInterface.h>
  16. #include <AzToolsFramework/ViewportSelection/EditorHelpers.h>
  17. namespace UnitTest
  18. {
  19. inline static constexpr AzFramework::ViewportId TestViewportId = 2468;
  20. static void DisplayHelpers(
  21. AzToolsFramework::EditorHelpers& editorHelpers,
  22. AzFramework::CameraState cameraState,
  23. AzFramework::DebugDisplayRequests& debugDisplay)
  24. {
  25. editorHelpers.DisplayHelpers(
  26. AzFramework::ViewportInfo{ TestViewportId },
  27. cameraState,
  28. debugDisplay,
  29. [](AZ::EntityId)
  30. {
  31. return true;
  32. });
  33. }
  34. static bool IsEntitySelected(AZ::EntityId entityId)
  35. {
  36. AzToolsFramework::EntityIdList entityIds;
  37. AzToolsFramework::ToolsApplicationRequestBus::BroadcastResult(
  38. entityIds, &AzToolsFramework::ToolsApplicationRequests::GetSelectedEntities);
  39. return AZStd::ranges::find(entityIds, entityId) != entityIds.end();
  40. }
  41. class EditorViewportOnlyDrawHelpersForSelectedEntityFixture
  42. : public ToolsApplicationFixture<>
  43. , public AzFramework::EntityDebugDisplayEventBus::MultiHandler
  44. {
  45. public:
  46. void SetUpEditorFixtureImpl() override
  47. {
  48. // Setup entity to use for EntityDebugDisplayEventBus and tests
  49. AZ::Entity* entity1 = nullptr;
  50. m_entityId = CreateDefaultEditorEntity("DebugHelpersEntity", &entity1);
  51. AZ::Entity* entity2 = nullptr;
  52. m_entityId2 = CreateDefaultEditorEntity("DebugHelpersEntity2", &entity2);
  53. AzFramework::EntityDebugDisplayEventBus::MultiHandler::BusConnect(m_entityId);
  54. AzFramework::EntityDebugDisplayEventBus::MultiHandler::BusConnect(m_entityId2);
  55. // DebugDisplay to use in DisplayHelpers
  56. AzFramework::DebugDisplayRequestBus::BusPtr debugDisplayBus;
  57. AzFramework::DebugDisplayRequestBus::Bind(debugDisplayBus, TestViewportId);
  58. m_debugDisplay = AzFramework::DebugDisplayRequestBus::FindFirstHandler(debugDisplayBus);
  59. m_cameraState = AzFramework::CreateDefaultCamera(AZ::Transform::CreateIdentity(), AzFramework::ScreenSize(1024, 768));
  60. m_entityVisibleEntityDataCacheMock = AZStd::make_unique<::testing::NiceMock<MockEditorVisibleEntityDataCacheInterface>>();
  61. m_editorHelpers = AZStd::make_unique<AzToolsFramework::EditorHelpers>(m_entityVisibleEntityDataCacheMock.get());
  62. m_viewportSettings = AZStd::make_unique<ViewportSettingsTestImpl>();
  63. m_viewportSettings->Connect(TestViewportId);
  64. m_viewportSettings->m_helpersVisible = true;
  65. m_viewportSettings->m_iconsVisible = true;
  66. using ::testing::_;
  67. using ::testing::Return;
  68. EXPECT_CALL(*m_entityVisibleEntityDataCacheMock, GetVisibleEntityId(_))
  69. .WillOnce(Return(m_entityId))
  70. .WillOnce(Return(m_entityId2));
  71. ON_CALL(*m_entityVisibleEntityDataCacheMock, VisibleEntityDataCount()).WillByDefault(Return(2));
  72. ON_CALL(*m_entityVisibleEntityDataCacheMock, IsVisibleEntityVisible(_)).WillByDefault(Return(true));
  73. }
  74. void TearDownEditorFixtureImpl() override
  75. {
  76. m_viewportSettings->Disconnect();
  77. m_viewportSettings.reset();
  78. m_editorHelpers.reset();
  79. m_entityVisibleEntityDataCacheMock.reset();
  80. AzFramework::EntityDebugDisplayEventBus::MultiHandler::BusDisconnect();
  81. }
  82. // EntityDebugDisplayEventBus overrides ...
  83. // This function is called in DisplayComponents which is responsible for drawing the helpers, if this is called it means that a
  84. // helper has been drawn
  85. void DisplayEntityViewport(const AzFramework::ViewportInfo&, AzFramework::DebugDisplayRequests&) override
  86. {
  87. const AZ::EntityId entityId = *AzFramework::EntityDebugDisplayEventBus::GetCurrentBusId();
  88. m_displayEntityViewportEvent = true;
  89. m_drawnEntities.push_back(entityId);
  90. }
  91. AZ::EntityId m_entityId;
  92. AZ::EntityId m_entityId2;
  93. AzToolsFramework::EntityIdList m_drawnEntities;
  94. AzFramework::DebugDisplayRequests* m_debugDisplay = nullptr;
  95. bool m_displayEntityViewportEvent = false;
  96. AZStd::unique_ptr<ViewportSettingsTestImpl> m_viewportSettings;
  97. AZStd::unique_ptr<AzToolsFramework::EditorHelpers> m_editorHelpers;
  98. AZStd::unique_ptr<::testing::NiceMock<MockEditorVisibleEntityDataCacheInterface>> m_entityVisibleEntityDataCacheMock;
  99. AzFramework::CameraState m_cameraState;
  100. };
  101. TEST_F(EditorViewportOnlyDrawHelpersForSelectedEntityFixture, DisplayDebugDrawIfSelectedEntitiesOptionDisabledAndEntitySelected)
  102. {
  103. // Given the entity is selected and the option to only show helpers for selected entities is false
  104. const AzToolsFramework::EntityIdList entityIds = { m_entityId };
  105. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(
  106. &AzToolsFramework::ToolsApplicationRequests::SetSelectedEntities, entityIds);
  107. m_viewportSettings->m_onlyShowForSelectedEntities = false;
  108. // When the draw function is called
  109. DisplayHelpers(*m_editorHelpers, m_cameraState, *m_debugDisplay);
  110. // Then the helper should be drawn
  111. EXPECT_TRUE(m_displayEntityViewportEvent);
  112. EXPECT_EQ(m_drawnEntities.size(), 2);
  113. }
  114. TEST_F(EditorViewportOnlyDrawHelpersForSelectedEntityFixture, DisplayDebugDrawIfSelectedEntitiesOptionDisabledAndEntityNotSelected)
  115. {
  116. // Given the entity is not selected and the option to only show helpers for selected entities is false
  117. m_viewportSettings->m_onlyShowForSelectedEntities = false;
  118. // When the draw function is called
  119. DisplayHelpers(*m_editorHelpers, m_cameraState, *m_debugDisplay);
  120. // Then the helper should be drawn
  121. EXPECT_TRUE(m_displayEntityViewportEvent);
  122. EXPECT_EQ(m_drawnEntities.size(), 2);
  123. }
  124. TEST_F(EditorViewportOnlyDrawHelpersForSelectedEntityFixture, DoNotDisplayDebugDrawIfSelectedEntitiesOptionEnabledAndEntityNotSelected)
  125. {
  126. // Given the entity is not selected and the option to only show helpers for selected entities is true
  127. m_viewportSettings->m_onlyShowForSelectedEntities = true;
  128. using ::testing::_;
  129. using ::testing::Return;
  130. // This should only be called if m_onlyShowForSelectedEntities is true
  131. EXPECT_CALL(*m_entityVisibleEntityDataCacheMock, IsVisibleEntitySelected(_))
  132. .WillOnce(Return(IsEntitySelected(m_entityId)))
  133. .WillOnce(Return(IsEntitySelected(m_entityId2)));
  134. // When the draw function is called
  135. DisplayHelpers(*m_editorHelpers, m_cameraState, *m_debugDisplay);
  136. // Then the helper should not be drawn
  137. EXPECT_FALSE(m_displayEntityViewportEvent);
  138. EXPECT_EQ(m_drawnEntities.size(), 0);
  139. }
  140. TEST_F(EditorViewportOnlyDrawHelpersForSelectedEntityFixture, DisplayDebugDrawIfSelectedEntitiesOptionEnabledAndEntityIsSelected)
  141. {
  142. // Given the entity is selected and the option to only show helpers for selected entities is true
  143. const AzToolsFramework::EntityIdList entityIds = { m_entityId };
  144. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(
  145. &AzToolsFramework::ToolsApplicationRequests::SetSelectedEntities, entityIds);
  146. using ::testing::_;
  147. using ::testing::Return;
  148. EXPECT_CALL(*m_entityVisibleEntityDataCacheMock, IsVisibleEntitySelected(_))
  149. .WillOnce(Return(IsEntitySelected(m_entityId)))
  150. .WillOnce(Return(IsEntitySelected(m_entityId2)));
  151. m_viewportSettings->m_onlyShowForSelectedEntities = true;
  152. // When the draw function is called
  153. DisplayHelpers(*m_editorHelpers, m_cameraState, *m_debugDisplay);
  154. // Then the helper should be drawn
  155. EXPECT_TRUE(m_displayEntityViewportEvent);
  156. EXPECT_EQ(m_drawnEntities.size(), 1);
  157. EXPECT_EQ(m_drawnEntities.back(), m_entityId);
  158. }
  159. TEST_F(EditorViewportOnlyDrawHelpersForSelectedEntityFixture, DisplayDebugDrawIfSelectedEntitiesOptionEnabledAndEntitiesAreSelected)
  160. {
  161. // Given the entity is selected and the option to only show helpers for selected entities is true
  162. const AzToolsFramework::EntityIdList entityIds = { m_entityId, m_entityId2 };
  163. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(
  164. &AzToolsFramework::ToolsApplicationRequests::SetSelectedEntities, entityIds);
  165. m_viewportSettings->m_onlyShowForSelectedEntities = true;
  166. using ::testing::_;
  167. using ::testing::Return;
  168. EXPECT_CALL(*m_entityVisibleEntityDataCacheMock, IsVisibleEntitySelected(_))
  169. .WillOnce(Return(IsEntitySelected(m_entityId)))
  170. .WillOnce(Return(IsEntitySelected(m_entityId2)));
  171. // When the draw function is called
  172. DisplayHelpers(*m_editorHelpers, m_cameraState, *m_debugDisplay);
  173. // Then the helper should be drawn
  174. EXPECT_TRUE(m_displayEntityViewportEvent);
  175. EXPECT_EQ(m_drawnEntities.size(), 2);
  176. EXPECT_EQ(m_drawnEntities.back(), m_entityId2);
  177. }
  178. } // namespace UnitTest