WhiteBoxTestFixtures.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #pragma once
  9. #include <AZTestShared/Math/MathTestHelpers.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. #include <AzManipulatorTestFramework/AzManipulatorTestFramework.h>
  12. #include <AzManipulatorTestFramework/AzManipulatorTestFrameworkTestHelpers.h>
  13. #include <AzManipulatorTestFramework/AzManipulatorTestFrameworkUtils.h>
  14. #include <AzManipulatorTestFramework/ImmediateModeActionDispatcher.h>
  15. #include <AzManipulatorTestFramework/IndirectManipulatorViewportInteraction.h>
  16. #include <AzManipulatorTestFramework/ViewportInteraction.h>
  17. #include <AzTest/AzTest.h>
  18. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  19. #include <WhiteBox/WhiteBoxToolApi.h>
  20. #include <tuple>
  21. #include "EditorWhiteBoxComponent.h"
  22. #include "Rendering/WhiteBoxRenderData.h"
  23. #include "WhiteBoxComponent.h"
  24. namespace UnitTest
  25. {
  26. class WhiteBoxTestFixture
  27. : public LeakDetectionFixture
  28. , private TraceBusRedirector
  29. {
  30. public:
  31. void SetUp() override final
  32. {
  33. LeakDetectionFixture::SetUp();
  34. AZ::Debug::TraceMessageBus::Handler::BusConnect();
  35. m_whiteBox = WhiteBox::Api::CreateWhiteBoxMesh();
  36. SetUpEditorFixtureImpl();
  37. }
  38. void TearDown() override final
  39. {
  40. TearDownEditorFixtureImpl();
  41. m_whiteBox.reset();
  42. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  43. LeakDetectionFixture::TearDown();
  44. }
  45. virtual void SetUpEditorFixtureImpl() {}
  46. virtual void TearDownEditorFixtureImpl() {}
  47. WhiteBox::Api::WhiteBoxMeshPtr m_whiteBox;
  48. };
  49. class EditorWhiteBoxComponentTestFixture : public ToolsApplicationFixture<>
  50. {
  51. public:
  52. void SetUpEditorFixtureImpl() override;
  53. void TearDownEditorFixtureImpl() override;
  54. AZ::EntityId m_whiteBoxEntityId;
  55. WhiteBox::EditorWhiteBoxComponent* m_whiteBoxComponent = nullptr;
  56. AZStd::unique_ptr<AZ::ComponentDescriptor> m_editorWhiteBoxComponentDescriptor;
  57. AZStd::unique_ptr<AZ::ComponentDescriptor> m_whiteBoxComponentDescriptor;
  58. };
  59. struct EditorWhiteBoxEntityAndComponent
  60. {
  61. AZ::Entity* m_entity = nullptr;
  62. WhiteBox::EditorWhiteBoxComponent* m_editorWhiteBoxComponent = nullptr;
  63. AZ::EntityId GetEntityId() const
  64. {
  65. return m_entity->GetId();
  66. }
  67. };
  68. inline EditorWhiteBoxEntityAndComponent CreateEditorEntityWithEditorWhiteBoxComponent()
  69. {
  70. AZ::Entity* whiteBoxEntity = nullptr;
  71. CreateDefaultEditorEntity("WhiteBox", &whiteBoxEntity);
  72. whiteBoxEntity->Deactivate();
  73. auto* editorWhiteBoxComponent = whiteBoxEntity->CreateComponent<WhiteBox::EditorWhiteBoxComponent>();
  74. whiteBoxEntity->Activate();
  75. return {whiteBoxEntity, editorWhiteBoxComponent};
  76. }
  77. inline void EditorWhiteBoxComponentTestFixture::SetUpEditorFixtureImpl()
  78. {
  79. AZ::SerializeContext* serializeContext = nullptr;
  80. AZ::ComponentApplicationBus::BroadcastResult(
  81. serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  82. m_editorWhiteBoxComponentDescriptor =
  83. AZStd::unique_ptr<AZ::ComponentDescriptor>(WhiteBox::EditorWhiteBoxComponent::CreateDescriptor());
  84. m_editorWhiteBoxComponentDescriptor->Reflect(serializeContext);
  85. m_whiteBoxComponentDescriptor =
  86. AZStd::unique_ptr<AZ::ComponentDescriptor>(WhiteBox::WhiteBoxComponent::CreateDescriptor());
  87. m_whiteBoxComponentDescriptor->Reflect(serializeContext);
  88. auto editorEntityAndWhiteBox = CreateEditorEntityWithEditorWhiteBoxComponent();
  89. m_whiteBoxEntityId = editorEntityAndWhiteBox.GetEntityId();
  90. m_whiteBoxComponent = editorEntityAndWhiteBox.m_editorWhiteBoxComponent;
  91. }
  92. inline void EditorWhiteBoxComponentTestFixture::TearDownEditorFixtureImpl()
  93. {
  94. AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
  95. &AzToolsFramework::EditorEntityContextRequestBus::Events::DestroyEditorEntity, m_whiteBoxEntityId);
  96. m_whiteBoxComponentDescriptor.reset();
  97. m_editorWhiteBoxComponentDescriptor.reset();
  98. }
  99. using EditorWhiteBoxModifierTestFixture =
  100. IndirectCallManipulatorViewportInteractionFixtureMixin<EditorWhiteBoxComponentTestFixture>;
  101. struct FaceTestData
  102. {
  103. std::vector<AZ::Vector3> m_positions;
  104. size_t m_numCulledFaces;
  105. };
  106. class WhiteBoxVertexDataTestFixture
  107. : public WhiteBoxTestFixture
  108. , public testing::WithParamInterface<FaceTestData>
  109. {
  110. public:
  111. WhiteBox::WhiteBoxFaces ConstructFaceData(const FaceTestData& inFaces)
  112. {
  113. const auto& positionData = inFaces.m_positions;
  114. const size_t numVertices = positionData.size();
  115. const size_t numFaces = numVertices / 3;
  116. WhiteBox::WhiteBoxFaces outFaces;
  117. outFaces.resize(numFaces);
  118. // assemble the triangle primitives from the position data
  119. // (normals and uvs will be undefined)
  120. for (size_t faceIndex = 0, vertexIndex = 0; faceIndex < numFaces; faceIndex++, vertexIndex += 3)
  121. {
  122. WhiteBox::WhiteBoxFace& face = outFaces[faceIndex];
  123. face.m_v1.m_position = inFaces.m_positions[vertexIndex];
  124. face.m_v2.m_position = inFaces.m_positions[vertexIndex + 1];
  125. face.m_v3.m_position = inFaces.m_positions[vertexIndex + 2];
  126. }
  127. return outFaces;
  128. }
  129. };
  130. // AZ::Vector3 doesn't play ball with gtest's parameterized tests
  131. struct Vector3
  132. {
  133. float x;
  134. float y;
  135. float z;
  136. };
  137. // vector components to apply random noise to
  138. enum class NoiseSource
  139. {
  140. None,
  141. XComponent,
  142. YComponent,
  143. ZComponent,
  144. XYComponent,
  145. XZComponent,
  146. YZComponent,
  147. XYZComponent
  148. };
  149. // rotation of normal (45 degrees around each axis)
  150. enum class Rotation
  151. {
  152. Identity,
  153. XAxis,
  154. ZAxis,
  155. XZAxis
  156. };
  157. // noise and rotation parameters to be applied per permutation
  158. using WhiteBoxUVTestParams = std::tuple<Vector3, NoiseSource, Rotation>;
  159. class WhiteBoxUVTestFixture
  160. : public WhiteBoxTestFixture
  161. , public ::testing::WithParamInterface<WhiteBoxUVTestParams>
  162. {
  163. public:
  164. };
  165. const auto DefaultViewportSize = AzFramework::ScreenSize(1024, 576); // 16:9 ratio
  166. } // namespace UnitTest