ViewportUiManagerTests.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/Math/Transform.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AzFramework/Viewport/CameraState.h>
  11. #include <AzFramework/Viewport/ViewportScreen.h>
  12. #include <AzTest/AzTest.h>
  13. #include <AzToolsFramework/Viewport/ViewportMessages.h>
  14. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  15. namespace UnitTest
  16. {
  17. // sets up a parent widget and render overlay to attach the Viewport UI to
  18. // as well as a cluster with one button
  19. class ViewportUiManagerTestFixture : public ::testing::Test
  20. {
  21. public:
  22. ViewportUiManagerTestFixture() = default;
  23. ViewportManagerWrapper m_viewportManagerWrapper;
  24. void SetUp() override
  25. {
  26. m_viewportManagerWrapper.Create();
  27. }
  28. void TearDown() override
  29. {
  30. m_viewportManagerWrapper.Destroy();
  31. }
  32. };
  33. TEST_F(ViewportUiManagerTestFixture, CreateClusterAddsNewClusterAndReturnsId)
  34. {
  35. auto clusterId = m_viewportManagerWrapper.GetViewportManager()->CreateCluster(AzToolsFramework::ViewportUi::Alignment::TopLeft);
  36. auto clusterEntry = m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().find(clusterId);
  37. EXPECT_TRUE(clusterEntry != m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().end());
  38. EXPECT_TRUE(clusterEntry->second.get() != nullptr);
  39. }
  40. TEST_F(ViewportUiManagerTestFixture, CreateClusterButtonAddsNewButtonAndReturnsId)
  41. {
  42. auto clusterId = m_viewportManagerWrapper.GetViewportManager()->CreateCluster(AzToolsFramework::ViewportUi::Alignment::TopLeft);
  43. auto buttonId = m_viewportManagerWrapper.GetViewportManager()->CreateClusterButton(clusterId, "");
  44. auto clusterEntry = m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().find(clusterId);
  45. EXPECT_TRUE(clusterEntry->second->GetButton(buttonId) != nullptr);
  46. }
  47. TEST_F(ViewportUiManagerTestFixture, SetClusterActiveButtonSetsButtonStateToActive)
  48. {
  49. auto clusterId = m_viewportManagerWrapper.GetViewportManager()->CreateCluster(AzToolsFramework::ViewportUi::Alignment::TopLeft);
  50. auto buttonId = m_viewportManagerWrapper.GetViewportManager()->CreateClusterButton(clusterId, "");
  51. auto clusterEntry = m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().find(clusterId);
  52. auto button = clusterEntry->second->GetButton(buttonId);
  53. m_viewportManagerWrapper.GetViewportManager()->SetClusterActiveButton(clusterId, buttonId);
  54. EXPECT_TRUE(button->m_state == AzToolsFramework::ViewportUi::Internal::Button::State::Selected);
  55. }
  56. TEST_F(ViewportUiManagerTestFixture, ClearClusterActiveButtonSetsButtonStateToDeselected)
  57. {
  58. // setup
  59. auto clusterId = m_viewportManagerWrapper.GetViewportManager()->CreateCluster(AzToolsFramework::ViewportUi::Alignment::TopLeft);
  60. auto buttonId = m_viewportManagerWrapper.GetViewportManager()->CreateClusterButton(clusterId, "");
  61. auto clusterEntry = m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().find(clusterId);
  62. auto button = clusterEntry->second->GetButton(buttonId);
  63. // first set a button to active
  64. m_viewportManagerWrapper.GetViewportManager()->SetClusterActiveButton(clusterId, buttonId);
  65. EXPECT_TRUE(button->m_state == AzToolsFramework::ViewportUi::Internal::Button::State::Selected);
  66. // clear the active button on the cluster
  67. m_viewportManagerWrapper.GetViewportManager()->ClearClusterActiveButton(clusterId);
  68. // the button should now be deselected
  69. EXPECT_TRUE(button->m_state == AzToolsFramework::ViewportUi::Internal::Button::State::Deselected);
  70. }
  71. TEST_F(ViewportUiManagerTestFixture, SetClusterDisableButtonOnActiveButton)
  72. {
  73. // setup
  74. auto clusterId = m_viewportManagerWrapper.GetViewportManager()->CreateCluster(AzToolsFramework::ViewportUi::Alignment::TopLeft);
  75. auto buttonId = m_viewportManagerWrapper.GetViewportManager()->CreateClusterButton(clusterId, "");
  76. auto clusterEntry = m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().find(clusterId);
  77. auto button = clusterEntry->second->GetButton(buttonId);
  78. // first set a button to active
  79. m_viewportManagerWrapper.GetViewportManager()->SetClusterActiveButton(clusterId, buttonId);
  80. EXPECT_TRUE(button->m_state == AzToolsFramework::ViewportUi::Internal::Button::State::Selected);
  81. // Disable active button
  82. m_viewportManagerWrapper.GetViewportManager()->SetClusterDisableButton(clusterId, buttonId, true);
  83. // try clear active button
  84. m_viewportManagerWrapper.GetViewportManager()->ClearClusterActiveButton(clusterId);
  85. // the button should now be disabled
  86. EXPECT_TRUE(button->m_state == AzToolsFramework::ViewportUi::Internal::Button::State::Disabled);
  87. }
  88. TEST_F(ViewportUiManagerTestFixture, SetClusterDisableButton)
  89. {
  90. // setup
  91. auto clusterId = m_viewportManagerWrapper.GetViewportManager()->CreateCluster(AzToolsFramework::ViewportUi::Alignment::TopLeft);
  92. auto buttonId = m_viewportManagerWrapper.GetViewportManager()->CreateClusterButton(clusterId, "");
  93. auto buttonId2 = m_viewportManagerWrapper.GetViewportManager()->CreateClusterButton(clusterId, "");
  94. auto clusterEntry = m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().find(clusterId);
  95. auto button = clusterEntry->second->GetButton(buttonId);
  96. auto button2 = clusterEntry->second->GetButton(buttonId2);
  97. // the button should now be disable
  98. EXPECT_TRUE(button->m_state == AzToolsFramework::ViewportUi::Internal::Button::State::Deselected);
  99. EXPECT_TRUE(button2->m_state == AzToolsFramework::ViewportUi::Internal::Button::State::Deselected);
  100. m_viewportManagerWrapper.GetViewportManager()->SetClusterDisableButton(clusterId, buttonId, true);
  101. m_viewportManagerWrapper.GetViewportManager()->SetClusterDisableButton(clusterId, buttonId2, true);
  102. // the button should now be disabled
  103. EXPECT_TRUE(button->m_state == AzToolsFramework::ViewportUi::Internal::Button::State::Disabled);
  104. EXPECT_TRUE(button2->m_state == AzToolsFramework::ViewportUi::Internal::Button::State::Disabled);
  105. }
  106. TEST_F(ViewportUiManagerTestFixture, RegisterClusterEventHandlerConnectsHandlerToClusterEvent)
  107. {
  108. auto clusterId = m_viewportManagerWrapper.GetViewportManager()->CreateCluster(AzToolsFramework::ViewportUi::Alignment::TopLeft);
  109. auto buttonId = m_viewportManagerWrapper.GetViewportManager()->CreateClusterButton(clusterId, "");
  110. // create a handler which will be triggered by the cluster
  111. bool handlerTriggered = false;
  112. auto testButtonId = AzToolsFramework::ViewportUi::ButtonId(buttonId);
  113. AZ::Event<AzToolsFramework::ViewportUi::ButtonId>::Handler handler(
  114. [&handlerTriggered, testButtonId](AzToolsFramework::ViewportUi::ButtonId buttonId)
  115. {
  116. if (buttonId == testButtonId)
  117. {
  118. handlerTriggered = true;
  119. }
  120. });
  121. auto clusterEntry = m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().find(clusterId);
  122. // trigger the cluster
  123. m_viewportManagerWrapper.GetViewportManager()->RegisterClusterEventHandler(clusterId, handler);
  124. clusterEntry->second->PressButton(buttonId);
  125. EXPECT_TRUE(handlerTriggered);
  126. }
  127. TEST_F(ViewportUiManagerTestFixture, RemoveClusterRemovesClusterFromViewportUi)
  128. {
  129. auto clusterId = m_viewportManagerWrapper.GetViewportManager()->CreateCluster(AzToolsFramework::ViewportUi::Alignment::TopLeft);
  130. m_viewportManagerWrapper.GetViewportManager()->RemoveCluster(clusterId);
  131. auto clusterEntry = m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().find(clusterId);
  132. EXPECT_TRUE(clusterEntry == m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().end());
  133. }
  134. TEST_F(ViewportUiManagerTestFixture, SetClusterVisibleChangesClusterVisibility)
  135. {
  136. m_viewportManagerWrapper.GetMockRenderOverlay()->setVisible(true);
  137. auto clusterId = m_viewportManagerWrapper.GetViewportManager()->CreateCluster(AzToolsFramework::ViewportUi::Alignment::TopLeft);
  138. m_viewportManagerWrapper.GetViewportManager()->CreateClusterButton(clusterId, "");
  139. m_viewportManagerWrapper.GetViewportManager()->Update();
  140. m_viewportManagerWrapper.GetViewportManager()->SetClusterVisible(clusterId, false);
  141. auto cluster = m_viewportManagerWrapper.GetViewportManager()->GetClusterMap().find(clusterId)->second;
  142. bool visible =
  143. m_viewportManagerWrapper.GetViewportManager()->GetViewportUiDisplay()->IsViewportUiElementVisible(cluster->GetViewportUiElementId());
  144. EXPECT_FALSE(visible);
  145. m_viewportManagerWrapper.GetViewportManager()->SetClusterVisible(clusterId, true);
  146. visible =
  147. m_viewportManagerWrapper.GetViewportManager()->GetViewportUiDisplay()->IsViewportUiElementVisible(cluster->GetViewportUiElementId());
  148. EXPECT_TRUE(visible);
  149. }
  150. } // namespace UnitTest