ActorRenderManagerTest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <gmock/gmock.h>
  9. #include <gtest/gtest_prod.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/UnitTest/TestTypes.h>
  12. #include <AzCore/std/smart_ptr/make_shared.h>
  13. #include <AzFramework/Physics/Common/PhysicsSceneQueries.h>
  14. #include <AzFramework/Physics/SystemBus.h>
  15. #include <AzTest/AzTest.h>
  16. #include <Blast/BlastSystemBus.h>
  17. #include <Family/ActorRenderManager.h>
  18. #include <MockMeshFeatureProcessor.h>
  19. #include <Mocks/BlastMocks.h>
  20. using testing::_;
  21. using testing::Eq;
  22. using testing::NaggyMock;
  23. using testing::Return;
  24. using testing::StrictMock;
  25. namespace Blast
  26. {
  27. // Helper class to make it possible to make test a friend without polluting the tested class itself
  28. class TestableActorRenderManager : public ActorRenderManager
  29. {
  30. public:
  31. TestableActorRenderManager(
  32. AZ::Render::MeshFeatureProcessorInterface* meshFeatureProcessor, BlastMeshData* meshData,
  33. AZ::EntityId entityId, uint32_t chunkCount, AZ::Vector3 scale)
  34. : ActorRenderManager(meshFeatureProcessor, meshData, entityId, chunkCount, scale)
  35. {
  36. }
  37. FRIEND_TEST(ActorRenderManagerTest, KeepsTrackOfAndRendersChunks_SUITE_sandbox);
  38. };
  39. class ActorRenderManagerTest
  40. : public testing::Test
  41. {
  42. protected:
  43. void SetUp() override
  44. {
  45. m_chunkCount = 2;
  46. m_mockMeshData = AZStd::make_shared<MockBlastMeshData>();
  47. m_mockMeshFeatureProcessor = AZStd::make_shared<UnitTest::MockMeshFeatureProcessor>();
  48. m_actorFactory = AZStd::make_unique<FakeActorFactory>(1);
  49. m_fakeEntityProvider = AZStd::make_shared<FakeEntityProvider>(m_chunkCount);
  50. m_actorFactory->m_mockActors[0]->m_chunkIndices = {0, 1};
  51. }
  52. AZStd::shared_ptr<MockBlastMeshData> m_mockMeshData;
  53. AZStd::shared_ptr<UnitTest::MockMeshFeatureProcessor> m_mockMeshFeatureProcessor;
  54. AZStd::unique_ptr<FakeActorFactory> m_actorFactory;
  55. AZStd::shared_ptr<FakeEntityProvider> m_fakeEntityProvider;
  56. uint32_t m_chunkCount;
  57. };
  58. TEST_F(ActorRenderManagerTest, KeepsTrackOfAndRendersChunks_SUITE_sandbox)
  59. {
  60. MockTransformBusHandler transformHandler;
  61. for (const auto id : m_fakeEntityProvider->m_createdEntityIds)
  62. {
  63. transformHandler.Connect(id);
  64. }
  65. AZStd::unique_ptr<TestableActorRenderManager> actorRenderManager;
  66. AZ::EntityId entityId;
  67. // ActorRenderManager::ActorRenderManager
  68. {
  69. AZ::Data::Asset<AZ::RPI::ModelAsset> asset{AZ::Data::AssetLoadBehavior::NoLoad};
  70. EXPECT_CALL(*m_mockMeshData, GetMeshAsset(_)).Times(m_chunkCount).WillRepeatedly(testing::ReturnRef(asset));
  71. actorRenderManager = AZStd::make_unique<TestableActorRenderManager>(
  72. m_mockMeshFeatureProcessor.get(), m_mockMeshData.get(), entityId, m_chunkCount,
  73. AZ::Vector3::CreateOne());
  74. EXPECT_EQ(actorRenderManager->m_chunkMeshHandles.size(), m_chunkCount);
  75. }
  76. // ActorRenderManager::OnActorCreated
  77. {
  78. EXPECT_CALL(
  79. *m_mockMeshFeatureProcessor, AcquireMesh(_, testing::A<const AZ::Render::CustomMaterialMap&>()))
  80. .Times(aznumeric_cast<int>(m_actorFactory->m_mockActors[0]->GetChunkIndices().size()))
  81. .WillOnce(Return(testing::ByMove(AZ::Render::MeshFeatureProcessorInterface::MeshHandle())))
  82. .WillOnce(Return(testing::ByMove(AZ::Render::MeshFeatureProcessorInterface::MeshHandle())));
  83. actorRenderManager->OnActorCreated(*m_actorFactory->m_mockActors[0]);
  84. for (auto chunkId : m_actorFactory->m_mockActors[0]->m_chunkIndices)
  85. {
  86. EXPECT_EQ(
  87. actorRenderManager->m_chunkActors[chunkId],
  88. static_cast<BlastActor*>(m_actorFactory->m_mockActors[0]));
  89. }
  90. }
  91. // ActorRenderManager::SyncMeshes
  92. {
  93. EXPECT_CALL(*m_mockMeshFeatureProcessor, SetTransform(_, _, _))
  94. .Times(aznumeric_cast<int>(m_actorFactory->m_mockActors[0]->GetChunkIndices().size()));
  95. actorRenderManager->SyncMeshes();
  96. }
  97. // ActorRenderManager::OnActorDestroyed
  98. {
  99. EXPECT_CALL(*m_mockMeshFeatureProcessor, ReleaseMesh(_))
  100. .Times(aznumeric_cast<int>(m_actorFactory->m_mockActors[0]->GetChunkIndices().size()))
  101. .WillRepeatedly(Return(true));
  102. actorRenderManager->OnActorDestroyed(*m_actorFactory->m_mockActors[0]);
  103. for (auto chunkId : m_actorFactory->m_mockActors[0]->m_chunkIndices)
  104. {
  105. EXPECT_EQ(actorRenderManager->m_chunkActors[chunkId], nullptr);
  106. }
  107. }
  108. }
  109. } // namespace Blast