SimulationInterfaceTests.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "TestFixture.h"
  9. #include <SimulationInterfaces/SimulationEntityManagerRequestBus.h>
  10. namespace UnitTest
  11. {
  12. TEST_F(SimulationInterfaceTestFixture, EmptyScene)
  13. {
  14. using namespace SimulationInterfaces;
  15. AZ::Outcome<EntityNameList, FailedResult> entities;
  16. SimulationEntityManagerRequestBus::BroadcastResult(
  17. entities, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  18. ASSERT_TRUE(entities.IsSuccess());
  19. EXPECT_EQ(entities.GetValue().size(), 0);
  20. }
  21. TEST_F(SimulationInterfaceTestFixture, AddSimulatedEntityThenRemove)
  22. {
  23. using namespace SimulationInterfaces;
  24. const AZ::EntityId entityId1 = CreateEntityWithStaticBodyComponent("Foo", AZ::Transform::CreateIdentity());
  25. const AZ::EntityId entityId2 = CreateEntityWithStaticBodyComponent("Bar", AZ::Transform::CreateIdentity());
  26. AZ::Outcome<EntityNameList, FailedResult> entities;
  27. SimulationEntityManagerRequestBus::BroadcastResult(
  28. entities, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  29. ASSERT_TRUE(entities.IsSuccess());
  30. ASSERT_EQ(entities.GetValue().size(), 2);
  31. DeleteEntity(entityId1);
  32. AZ::Outcome<EntityNameList, FailedResult> entities2;
  33. SimulationEntityManagerRequestBus::BroadcastResult(
  34. entities2, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  35. ASSERT_TRUE(entities2.IsSuccess());
  36. EXPECT_EQ(entities2.GetValue().size(), 1);
  37. DeleteEntity(entityId2);
  38. AZ::Outcome<EntityNameList, FailedResult> entities3;
  39. SimulationEntityManagerRequestBus::BroadcastResult(
  40. entities3, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  41. ASSERT_TRUE(entities3.IsSuccess());
  42. EXPECT_EQ(entities3.GetValue().size(), 0);
  43. }
  44. TEST_F(SimulationInterfaceTestFixture, AddEntitiesWithDupName)
  45. {
  46. using namespace SimulationInterfaces;
  47. const AZ::EntityId entityId1 = CreateEntityWithStaticBodyComponent("Bar1", AZ::Transform::CreateIdentity());
  48. const AZ::EntityId entityId2 = CreateEntityWithStaticBodyComponent("Bar1", AZ::Transform::CreateIdentity());
  49. AZ::Outcome<EntityNameList, FailedResult> entities;
  50. SimulationEntityManagerRequestBus::BroadcastResult(
  51. entities, &SimulationEntityManagerRequestBus::Events::GetEntities, EntityFilters());
  52. ASSERT_TRUE(entities.IsSuccess());
  53. EXPECT_EQ(entities.GetValue().size(), 2);
  54. EXPECT_NE(entities.GetValue()[0], entities.GetValue()[1]);
  55. DeleteEntity(entityId1);
  56. DeleteEntity(entityId2);
  57. }
  58. TEST_F(SimulationInterfaceTestFixture, TestShapeFilter)
  59. {
  60. // This test is disabled since due to some issue outside to this gem, the rigid body is created without the collider shape
  61. // and the filter is not applied. This test will be enabled once the issue is resolved.
  62. GTEST_SKIP() << "Need to fix the issue with the collider shape creation.";
  63. using namespace SimulationInterfaces;
  64. const AZ::EntityId entityId1 =
  65. CreateEntityWithStaticBodyComponent("Inside", AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 0.0f, 0.0f)));
  66. const AZ::EntityId entityId2 =
  67. CreateEntityWithStaticBodyComponent("Outside", AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 0.0f, 0.0f)));
  68. EntityFilters filter;
  69. filter.m_boundsShape = AZStd::make_shared<Physics::SphereShapeConfiguration>(2.0f);
  70. AZ::Outcome<EntityNameList, FailedResult> entities;
  71. SimulationEntityManagerRequestBus::BroadcastResult(entities, &SimulationEntityManagerRequestBus::Events::GetEntities, filter);
  72. auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
  73. physicsSystem->Simulate(1.0f / 60.0f);
  74. ASSERT_TRUE(entities.IsSuccess());
  75. ASSERT_EQ(entities.GetValue().size(), 1);
  76. EXPECT_EQ(entities.GetValue().front(), "Inside");
  77. DeleteEntity(entityId1);
  78. DeleteEntity(entityId2);
  79. }
  80. TEST_F(SimulationInterfaceTestFixture, TestRegexFilter)
  81. {
  82. using namespace SimulationInterfaces;
  83. const AZ::EntityId entityId1 =
  84. CreateEntityWithStaticBodyComponent("WillMatch", AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 0.0f, 0.0f)));
  85. const AZ::EntityId entityId2 =
  86. CreateEntityWithStaticBodyComponent("WontMatch", AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 0.0f, 0.0f)));
  87. EntityFilters filter;
  88. filter.m_nameFilter = "Will.*";
  89. AZ::Outcome<EntityNameList, FailedResult> entities;
  90. SimulationEntityManagerRequestBus::BroadcastResult(entities, &SimulationEntityManagerRequestBus::Events::GetEntities, filter);
  91. ASSERT_TRUE(entities.IsSuccess());
  92. ASSERT_EQ(entities.GetValue().size(), 1);
  93. EXPECT_EQ(entities.GetValue().front(), "WillMatch");
  94. DeleteEntity(entityId1);
  95. DeleteEntity(entityId2);
  96. }
  97. TEST_F(SimulationInterfaceTestFixture, TestRegexFilterInvalid)
  98. {
  99. // Invalid regex should not match any entity
  100. using namespace SimulationInterfaces;
  101. const AZ::EntityId entityId1 =
  102. CreateEntityWithStaticBodyComponent("WillMatch", AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 0.0f, 0.0f)));
  103. const AZ::EntityId entityId2 =
  104. CreateEntityWithStaticBodyComponent("WontMatch", AZ::Transform::CreateTranslation(AZ::Vector3(10.0f, 0.0f, 0.0f)));
  105. EntityFilters filter;
  106. filter.m_nameFilter = "[a-z";
  107. AZ::Outcome<EntityNameList, FailedResult> entities;
  108. SimulationEntityManagerRequestBus::BroadcastResult(entities, &SimulationEntityManagerRequestBus::Events::GetEntities, filter);
  109. EXPECT_FALSE(entities.IsSuccess());
  110. DeleteEntity(entityId1);
  111. DeleteEntity(entityId2);
  112. }
  113. TEST_F(SimulationInterfaceTestFixture, SmokeTestGetEntityState)
  114. {
  115. // Invalid regex should not match any entity
  116. using namespace SimulationInterfaces;
  117. const AZStd::string entityName = "DroppedBall";
  118. const AZ::EntityId entityId1 =
  119. CreateEntityWithStaticBodyComponent(entityName, AZ::Transform::CreateTranslation(AZ::Vector3(2.0f, 0.0f, 10.0f)));
  120. EntityFilters filter;
  121. AZ::Outcome<EntityState, FailedResult> stateBeforeResult;
  122. SimulationEntityManagerRequestBus::BroadcastResult(
  123. stateBeforeResult, &SimulationEntityManagerRequestBus::Events::GetEntityState, entityName);
  124. EXPECT_TRUE(stateBeforeResult.IsSuccess());
  125. const auto& stateBefore = stateBeforeResult.GetValue();
  126. EXPECT_EQ(stateBefore.m_pose.GetTranslation(), AZ::Vector3(2.0f, 0.0f, 10.0f));
  127. for (int i = 0; i < 10; i++)
  128. {
  129. auto* physicsSystem = AZ::Interface<AzPhysics::SystemInterface>::Get();
  130. physicsSystem->Simulate(1.0f / 60.0f);
  131. }
  132. AZ::Outcome<EntityState, FailedResult> stateAfterResult;
  133. SimulationEntityManagerRequestBus::BroadcastResult(
  134. stateAfterResult, &SimulationEntityManagerRequestBus::Events::GetEntityState, entityName);
  135. EXPECT_TRUE(stateAfterResult.IsSuccess());
  136. const auto& stateAfter = stateAfterResult.GetValue();
  137. AZ::Vector3 deltaPos = stateAfter.m_pose.GetTranslation() - stateBefore.m_pose.GetTranslation();
  138. // check if the entity moved
  139. EXPECT_GT(deltaPos.GetLength(), 0.0f);
  140. // check if entity has velocity
  141. EXPECT_GT(stateAfter.m_twistLinear.GetLength(), 0.0f);
  142. DeleteEntity(entityId1);
  143. }
  144. TEST_F(SimulationInterfaceTestFixture, TestEntitySpawnedDeletionEmptyScene)
  145. {
  146. using namespace SimulationInterfaces;
  147. bool deletionWasCompleted = false;
  148. DeletionCompletedCb deleteAllCompletion = [&deletionWasCompleted](const AZ::Outcome<void, FailedResult>& result)
  149. {
  150. deletionWasCompleted = true;
  151. EXPECT_TRUE(result.IsSuccess());
  152. };
  153. SimulationEntityManagerRequestBus::Broadcast(&SimulationEntityManagerRequestBus::Events::DeleteAllEntities, deleteAllCompletion);
  154. TickApp(100);
  155. EXPECT_TRUE(deletionWasCompleted);
  156. }
  157. } // namespace UnitTest
  158. // required to support running integration tests with Qt and PhysX
  159. AZTEST_EXPORT int AZ_UNIT_TEST_HOOK_NAME(int argc, char** argv)
  160. {
  161. ::testing::InitGoogleMock(&argc, argv);
  162. AzQtComponents::PrepareQtPaths();
  163. QApplication app(argc, argv);
  164. AZ::Test::printUnusedParametersWarning(argc, argv);
  165. AZ::Test::addTestEnvironments({ new UnitTest::SimulationInterfaceTestEnvironment() });
  166. int result = RUN_ALL_TESTS();
  167. return result;
  168. }
  169. IMPLEMENT_TEST_EXECUTABLE_MAIN();