3
0

ActorComponentBusTests.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/std/smart_ptr/unique_ptr.h>
  9. #include <AzFramework/Components/TransformComponent.h>
  10. #include <EMotionFX/Source/Attachment.h>
  11. #include <Integration/Components/ActorComponent.h>
  12. #include <Include/Integration/ActorComponentBus.h>
  13. #include <Tests/Integration/EntityComponentFixture.h>
  14. #include <Tests/TestAssetCode/ActorFactory.h>
  15. #include <Tests/TestAssetCode/TestActorAssets.h>
  16. #include <Tests/TestAssetCode/SimpleActors.h>
  17. #include <Tests/Printers.h>
  18. namespace EMotionFX
  19. {
  20. class ActorComponentNotificationTestBus
  21. : Integration::ActorComponentNotificationBus::Handler
  22. {
  23. public:
  24. ActorComponentNotificationTestBus(AZ::EntityId entityId)
  25. {
  26. Integration::ActorComponentNotificationBus::Handler::BusConnect(entityId);
  27. }
  28. ~ActorComponentNotificationTestBus()
  29. {
  30. Integration::ActorComponentNotificationBus::Handler::BusDisconnect();
  31. }
  32. MOCK_METHOD1(OnActorInstanceCreated, void(EMotionFX::ActorInstance*));
  33. MOCK_METHOD1(OnActorInstanceDestroyed, void(EMotionFX::ActorInstance*));
  34. };
  35. TEST_F(EntityComponentFixture, ActorComponentNotificationBusTest)
  36. {
  37. const AZ::EntityId id(740216387);
  38. ActorComponentNotificationTestBus testBus(id);
  39. EXPECT_CALL(testBus, OnActorInstanceCreated(testing::_)).Times(testing::AtLeast(1));
  40. EXPECT_CALL(testBus, OnActorInstanceDestroyed(testing::_)).Times(1);
  41. AZStd::unique_ptr<AZ::Entity> entity = AZStd::make_unique<AZ::Entity>(id);
  42. entity->CreateComponent<AzFramework::TransformComponent>();
  43. Integration::ActorComponent::Configuration actorConf;
  44. Integration::ActorComponent* actorComponent = entity->CreateComponent<Integration::ActorComponent>(&actorConf);
  45. entity->Init();
  46. entity->Activate();
  47. const AZ::Data::AssetId actorAssetId("{5060227D-B6F4-422E-BF82-41AAC5F228A5}");
  48. AZ::Data::Asset<Integration::ActorAsset> actorAsset = TestActorAssets::GetAssetFromActor(actorAssetId, ActorFactory::CreateAndInit<SimpleJointChainActor>(3));
  49. actorConf.m_actorAsset = actorAsset;
  50. actorComponent->SetActorAsset(actorConf.m_actorAsset);
  51. entity->Deactivate();
  52. }
  53. class ActorComponentRequestsFixture
  54. : public EntityComponentFixture
  55. {
  56. public:
  57. void SetUp() override
  58. {
  59. EntityComponentFixture::SetUp();
  60. const AZ::Data::AssetId actorAssetId("{5060227D-B6F4-422E-BF82-41AAC5F228A5}");
  61. AZ::Data::Asset<Integration::ActorAsset> actorAsset = TestActorAssets::GetAssetFromActor(actorAssetId, ActorFactory::CreateAndInit<SimpleJointChainActor>(3));
  62. m_entity = AZStd::make_unique<AZ::Entity>(AZ::EntityId(740216387));
  63. m_transformComponent = m_entity->CreateComponent<AzFramework::TransformComponent>();
  64. Integration::ActorComponent::Configuration actorConfig;
  65. actorConfig.m_attachmentType = Integration::AttachmentType::SkinAttachment;
  66. actorConfig.m_actorAsset = actorAsset;
  67. m_actorComponent = m_entity->CreateComponent<Integration::ActorComponent>(&actorConfig);
  68. m_entity->Init();
  69. m_entity->Activate();
  70. m_actorComponent->SetActorAsset(actorAsset);
  71. }
  72. void TearDown() override
  73. {
  74. m_entity.reset();
  75. EntityComponentFixture::TearDown();
  76. }
  77. AZStd::unique_ptr<AZ::Entity> m_entity;
  78. Integration::ActorComponent* m_actorComponent;
  79. AzFramework::TransformComponent* m_transformComponent;
  80. };
  81. TEST_F(ActorComponentRequestsFixture, GetActorInstance)
  82. {
  83. ActorInstance* gotActorInstance;
  84. Integration::ActorComponentRequestBus::EventResult(gotActorInstance, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetActorInstance);
  85. EXPECT_EQ(gotActorInstance, m_actorComponent->GetActorInstance());
  86. }
  87. TEST_F(ActorComponentRequestsFixture, GetNumJoints)
  88. {
  89. size_t gotNumJoints;
  90. Integration::ActorComponentRequestBus::EventResult(gotNumJoints, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetNumJoints);
  91. EXPECT_EQ(gotNumJoints, 3);
  92. }
  93. TEST_F(ActorComponentRequestsFixture, GetJointIndexByName)
  94. {
  95. size_t gotJointIndex;
  96. Integration::ActorComponentRequestBus::EventResult(gotJointIndex, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetJointIndexByName, "rootJoint");
  97. EXPECT_EQ(gotJointIndex, 0);
  98. Integration::ActorComponentRequestBus::EventResult(gotJointIndex, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetJointIndexByName, "joint1");
  99. EXPECT_EQ(gotJointIndex, 1);
  100. Integration::ActorComponentRequestBus::EventResult(gotJointIndex, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetJointIndexByName, "joint2");
  101. EXPECT_EQ(gotJointIndex, 2);
  102. }
  103. TEST_F(ActorComponentRequestsFixture, GetJointTransform)
  104. {
  105. AZ::Transform gotTransform;
  106. Integration::ActorComponentRequestBus::EventResult(gotTransform, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetJointTransform, 0, Integration::Space::LocalSpace);
  107. EXPECT_EQ(gotTransform.GetTranslation(), AZ::Vector3::CreateZero());
  108. Integration::ActorComponentRequestBus::EventResult(gotTransform, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetJointTransform, 1, Integration::Space::LocalSpace);
  109. EXPECT_EQ(gotTransform.GetTranslation(), AZ::Vector3::CreateAxisX(1.0f));
  110. Integration::ActorComponentRequestBus::EventResult(gotTransform, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetJointTransform, 2, Integration::Space::LocalSpace);
  111. EXPECT_EQ(gotTransform.GetTranslation(), AZ::Vector3::CreateAxisX(2.0f));
  112. }
  113. #if AZ_TRAIT_DISABLE_FAILED_EMOTION_FX_TESTS
  114. TEST_F(ActorComponentRequestsFixture, DISABLED_Attach_Detach_Entity)
  115. #else
  116. TEST_F(ActorComponentRequestsFixture, Attach_Detach_Entity)
  117. #endif // AZ_TRAIT_DISABLE_FAILED_EMOTION_FX_TESTS
  118. {
  119. const AZ::Data::AssetId actorAssetId("{AD308159-879C-420E-B7D7-22E4A243F5A9}");
  120. AZ::Data::Asset<Integration::ActorAsset> targetActorAsset = TestActorAssets::GetAssetFromActor(actorAssetId, ActorFactory::CreateAndInit<SimpleJointChainActor>(3, "parentActor"));
  121. auto targetEntity = AZStd::make_unique<AZ::Entity>(AZ::EntityId(92484));
  122. AzFramework::TransformComponent* targetTransformComponent = targetEntity->CreateComponent<AzFramework::TransformComponent>();
  123. targetTransformComponent->SetWorldTM(AZ::Transform::CreateTranslation(AZ::Vector3(9.0f, 24.0f, 84.0f)));
  124. Integration::ActorComponent::Configuration actorConf;
  125. actorConf.m_actorAsset = targetActorAsset;
  126. Integration::ActorComponent* targetActorComponent = targetEntity->CreateComponent<Integration::ActorComponent>(&actorConf);
  127. targetEntity->Init();
  128. targetEntity->Activate();
  129. targetActorComponent->SetActorAsset(targetActorAsset);
  130. Integration::ActorComponentRequestBus::Event(m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::AttachToEntity, targetEntity->GetId(), Integration::AttachmentType::SkinAttachment);
  131. EXPECT_EQ(
  132. m_transformComponent->GetWorldTM().GetTranslation(),
  133. targetTransformComponent->GetWorldTM().GetTranslation()
  134. );
  135. EXPECT_EQ(targetActorComponent->GetActorInstance()->GetNumAttachments(), 1);
  136. EXPECT_EQ(
  137. targetActorComponent->GetActorInstance()->GetAttachment(0)->GetAttachmentActorInstance(),
  138. m_actorComponent->GetActorInstance()
  139. );
  140. EXPECT_EQ(
  141. m_actorComponent->GetActorInstance()->GetAttachedTo(),
  142. targetActorComponent->GetActorInstance()
  143. );
  144. Integration::ActorComponentRequestBus::Event(m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::DetachFromEntity);
  145. EXPECT_EQ(
  146. m_transformComponent->GetWorldTM().GetTranslation(),
  147. AZ::Vector3::CreateZero()
  148. );
  149. EXPECT_EQ(targetActorComponent->GetActorInstance()->GetNumAttachments(), 0);
  150. EXPECT_EQ(m_actorComponent->GetActorInstance()->GetAttachedTo(), nullptr);
  151. }
  152. TEST_F(ActorComponentRequestsFixture, GetSetRenderCharacter)
  153. {
  154. bool gotRenderCharacter = false;
  155. Integration::ActorComponentRequestBus::EventResult(gotRenderCharacter, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetRenderCharacter);
  156. EXPECT_TRUE(gotRenderCharacter);
  157. Integration::ActorComponentRequestBus::Event(m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::SetRenderCharacter, false);
  158. Integration::ActorComponentRequestBus::EventResult(gotRenderCharacter, m_entity->GetId(), &Integration::ActorComponentRequestBus::Events::GetRenderCharacter);
  159. EXPECT_FALSE(gotRenderCharacter);
  160. }
  161. } // end namespace EMotionFX