EditorGradientSignalPreviewTests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <Tests/GradientSignalTestFixtures.h>
  9. #include <GradientSignal/Editor/EditorGradientPreviewRenderer.h>
  10. #include <Editor/EditorConstantGradientComponent.h>
  11. #include <AzTest/AzTest.h>
  12. #include <AzCore/Memory/Memory.h>
  13. #include <AzCore/Memory/PoolAllocator.h>
  14. #include <AzCore/Jobs/Job.h>
  15. #include <AzCore/Jobs/JobManager.h>
  16. #include <AzCore/Math/Aabb.h>
  17. namespace UnitTest
  18. {
  19. // Extend the GradientSignalTestEnvironment to include any editor-specific component descriptors that we might need.
  20. class GradientSignalEditorTestEnvironment : public GradientSignalTestEnvironment
  21. {
  22. public:
  23. void AddGemsAndComponents() override
  24. {
  25. GradientSignalTestEnvironment::AddGemsAndComponents();
  26. AddComponentDescriptors({
  27. GradientSignal::EditorConstantGradientComponent::CreateDescriptor(),
  28. });
  29. }
  30. };
  31. struct EditorGradientSignalPreviewTestsFixture
  32. : public GradientSignalTest
  33. {
  34. protected:
  35. AZ::JobManager* m_jobManager = nullptr;
  36. AZ::JobContext* m_jobContext = nullptr;
  37. void SetUp() override
  38. {
  39. GradientSignalTest::SetUp();
  40. auto globalContext = AZ::JobContext::GetGlobalContext();
  41. if (globalContext)
  42. {
  43. AZ_Assert(
  44. globalContext->GetJobManager().GetNumWorkerThreads() >= 2,
  45. "Job Manager previously started by test environment with too few threads for this test.");
  46. }
  47. else
  48. {
  49. // Set up job manager with two threads so that we can run and test the preview job logic.
  50. AZ::JobManagerDesc desc;
  51. AZ::JobManagerThreadDesc threadDesc;
  52. desc.m_workerThreads.push_back(threadDesc);
  53. desc.m_workerThreads.push_back(threadDesc);
  54. m_jobManager = aznew AZ::JobManager(desc);
  55. m_jobContext = aznew AZ::JobContext(*m_jobManager);
  56. AZ::JobContext::SetGlobalContext(m_jobContext);
  57. }
  58. }
  59. void TearDown() override
  60. {
  61. if (m_jobContext)
  62. {
  63. AZ::JobContext::SetGlobalContext(nullptr);
  64. delete m_jobContext;
  65. delete m_jobManager;
  66. }
  67. GradientSignalTest::TearDown();
  68. }
  69. void TestPreviewImage(int imageBounds, const AZStd::vector<AZ::Vector3>& interlaceOrder)
  70. {
  71. // NOTE: We currently only test square images. If we want to test rectangular ones, we'd need to copy the
  72. // centering logic from the EditorGradientPreviewRenderer to validate that the gradient values are ending up
  73. // in the right pixels. It seems a bit redundant, so the tests are currently constrained to squares.
  74. int imageBoundsX = imageBounds;
  75. int imageBoundsY = imageBounds;
  76. // Create a mock gradient entity and a mock entity that owns the preview widget
  77. auto entityMock = CreateEntity();
  78. auto previewOwnerEntityMock = CreateEntity();
  79. // Set up preview bounds. We set them to match up 1:1 with the size of our generated mock gradient data
  80. // so that we can easily validate that the output preview pixels exactly match the input mock data, and
  81. // we can easily validate the order in which the gradient values were requested to test the interlacing
  82. // functionality.
  83. bool constrainToShape = false;
  84. AZ::Aabb previewBounds = AZ::Aabb::CreateFromMinMax(AZ::Vector3(0.0f), AZ::Vector3(aznumeric_cast<float>(imageBoundsX), aznumeric_cast<float>(imageBoundsY), 0.0f));
  85. // Fill in our mock gradient data with values that go from 0.0f in the upper left corner down to 1.0f in the bottom right.
  86. AZStd::vector<float> inputData(imageBoundsX * imageBoundsY);
  87. for (int y = 0; y < imageBoundsY; y++)
  88. {
  89. for (int x = 0; x < imageBoundsX; x++)
  90. {
  91. inputData.push_back(static_cast<float>(x * y) / static_cast<float>((imageBoundsY - 1) * (imageBoundsX - 1)));
  92. }
  93. }
  94. // Set up a gradient sampler that points to our mock entities and listens to the correct gradient EBuses
  95. GradientSignal::GradientSampler sampler;
  96. sampler.m_gradientId = entityMock->GetId();
  97. sampler.m_ownerEntityId = previewOwnerEntityMock->GetId();
  98. UnitTest::MockGradientArrayRequestsBus mockGradientRequestsBus(entityMock->GetId(), inputData, imageBoundsX);
  99. UnitTest::MockGradientPreviewContextRequestBus mockGradientPreviewContextRequestBus(previewOwnerEntityMock->GetId(), previewBounds, constrainToShape);
  100. // Create an empty output preview image, with bounds set to 1:1 match with our mock gradient data.
  101. QImage previewImage;
  102. QSize imageDimensions(imageBoundsX, imageBoundsY);
  103. // Run the gradient preview job and wait for it to finish.
  104. auto updateJob = aznew GradientSignal::EditorGradientPreviewUpdateJob();
  105. updateJob->RefreshPreview(sampler, nullptr, imageDimensions, &previewImage);
  106. updateJob->Wait();
  107. // Verify that we got the exact image format and size that we expected.
  108. EXPECT_EQ(previewImage.format(), QImage::Format_Grayscale8);
  109. ASSERT_EQ(previewImage.size(), imageDimensions);
  110. // Run through the image and verify that every pixel has the value that we expected.
  111. AZ::u8* buffer = static_cast<AZ::u8*>(previewImage.bits());
  112. const uint64_t imageBytesPerLine = previewImage.bytesPerLine();
  113. for (int y = 0; y < imageBoundsY; y++)
  114. {
  115. for (int x = 0; x < imageBoundsX; x++)
  116. {
  117. EXPECT_EQ(buffer[(y * imageBytesPerLine) + x], static_cast<AZ::u8>(inputData[(y * imageBoundsX) + x] * 255));
  118. }
  119. }
  120. // Verify that we requested the exact number of pixels in our image, no more, no less.
  121. EXPECT_EQ(mockGradientRequestsBus.m_positionsRequested.size(), (imageBoundsX * imageBoundsY));
  122. // Check to see if the values requested from the gradient were accessed in the exact interlaced order
  123. // that we expect. This is an optional check, so only perform it if we passed in the expected order.
  124. if (!interlaceOrder.empty())
  125. {
  126. ASSERT_EQ(interlaceOrder.size(), mockGradientRequestsBus.m_positionsRequested.size());
  127. for (int y = 0; y < imageBoundsY; y++)
  128. {
  129. for (int x = 0; x < imageBoundsX; x++)
  130. {
  131. // Verify X matches up
  132. EXPECT_EQ(interlaceOrder[(y*imageBoundsX) + x].GetX(), mockGradientRequestsBus.m_positionsRequested[(y * imageBoundsX) + x].GetX());
  133. // Y should be requested exactly flipped from what we would expect, since images are rendered upside-down relative to world space.
  134. EXPECT_EQ((imageBoundsY - 1.0f) - interlaceOrder[(y * imageBoundsX) + x].GetY(), mockGradientRequestsBus.m_positionsRequested[(y * imageBoundsX) + x].GetY());
  135. }
  136. }
  137. }
  138. delete updateJob;
  139. }
  140. };
  141. TEST_F(EditorGradientSignalPreviewTestsFixture, GradientPreviewImage_2x2_BasicFunctionality)
  142. {
  143. AZStd::vector<AZ::Vector3> interlaceOrder {
  144. AZ::Vector3(0.0f, 0.0f, 0.0f),
  145. AZ::Vector3(1.0f, 0.0f, 0.0f),
  146. AZ::Vector3(0.0f, 1.0f, 0.0f),
  147. AZ::Vector3(1.0f, 1.0f, 0.0f),
  148. };
  149. TestPreviewImage(2, interlaceOrder);
  150. }
  151. TEST_F(EditorGradientSignalPreviewTestsFixture, GradientPreviewImage_4x4_BasicFunctionality)
  152. {
  153. AZStd::vector<AZ::Vector3> interlaceOrder{
  154. AZ::Vector3(0.0f, 0.0f, 0.0f),
  155. AZ::Vector3(2.0f, 0.0f, 0.0f),
  156. AZ::Vector3(0.0f, 2.0f, 0.0f),
  157. AZ::Vector3(2.0f, 2.0f, 0.0f),
  158. AZ::Vector3(1.0f, 0.0f, 0.0f),
  159. AZ::Vector3(3.0f, 0.0f, 0.0f),
  160. AZ::Vector3(1.0f, 2.0f, 0.0f),
  161. AZ::Vector3(3.0f, 2.0f, 0.0f),
  162. AZ::Vector3(0.0f, 1.0f, 0.0f),
  163. AZ::Vector3(1.0f, 1.0f, 0.0f),
  164. AZ::Vector3(2.0f, 1.0f, 0.0f),
  165. AZ::Vector3(3.0f, 1.0f, 0.0f),
  166. AZ::Vector3(0.0f, 3.0f, 0.0f),
  167. AZ::Vector3(1.0f, 3.0f, 0.0f),
  168. AZ::Vector3(2.0f, 3.0f, 0.0f),
  169. AZ::Vector3(3.0f, 3.0f, 0.0f),
  170. };
  171. TestPreviewImage(4, interlaceOrder);
  172. }
  173. TEST_F(EditorGradientSignalPreviewTestsFixture, GradientPreviewImage_1100x1100_LargeImage)
  174. {
  175. // NOTE: we leave the interlaceOrder vector empty to skip validating the interlace pattern.
  176. // It's too complicated to fill in programatically, and too large to write out manually.
  177. AZStd::vector<AZ::Vector3> interlaceOrder;
  178. TestPreviewImage(1100, interlaceOrder);
  179. }
  180. TEST_F(EditorGradientSignalPreviewTestsFixture, GradientPreviewImage_DefaultsToPinningItself)
  181. {
  182. // Verify that the GradientPreviewer will automatically set itself to preview against its own entity's bounds if it
  183. // hasn't already been pinned to preview with a different entity.
  184. float shapeHalfBounds = 20.0f;
  185. // Create an Editor Constant Gradient Component with arbitrary parameters. We need the Editor version so that it has
  186. // a gradient previewer.
  187. auto entity = CreateTestEntity(shapeHalfBounds);
  188. entity->CreateComponent<GradientSignal::EditorConstantGradientComponent>();
  189. ActivateEntity(entity.get());
  190. // Verify that by default, the gradient previewer is hooked up to the entity that it exists on.
  191. AZ::EntityId previewEntityId;
  192. GradientSignal::GradientPreviewContextRequestBus::EventResult(
  193. previewEntityId, entity->GetId(), &GradientSignal::GradientPreviewContextRequestBus::Events::GetPreviewEntity);
  194. EXPECT_EQ(entity->GetId(), previewEntityId);
  195. }
  196. } // namespace UnitTest
  197. // This uses a custom test hook so that we can load LmbrCentral and use Shape components in our unit tests.
  198. AZ_UNIT_TEST_HOOK(new UnitTest::GradientSignalEditorTestEnvironment);