AnimAudioComponentTests.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/SystemComponentFixture.h>
  9. #include <AzCore/Component/Component.h>
  10. #include <AzCore/Component/ComponentApplicationBus.h>
  11. #include <AzCore/RTTI/ReflectContext.h>
  12. #include <AzFramework/Components/TransformComponent.h>
  13. #include <Integration/Components/AnimAudioComponent.h>
  14. namespace EMotionFX
  15. {
  16. class MockAudioProxyComponent
  17. : public AZ::Component
  18. {
  19. public:
  20. AZ_COMPONENT(MockAudioProxyComponent, "{DF130DF1-AE9D-486A-8015-3D7FD64DC4C0}");
  21. void Activate() override {}
  22. void Deactivate() override {}
  23. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) { provided.push_back(AZ_CRC("AudioProxyService", 0x7da4c79c)); }
  24. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible) { incompatible.push_back(AZ_CRC("AudioProxyService", 0x7da4c79c)); }
  25. static void Reflect(AZ::ReflectContext*) {}
  26. };
  27. class MockMeshComponent
  28. : public AZ::Component
  29. {
  30. public:
  31. AZ_COMPONENT(MockMeshComponent, "{876F6E19-D67A-4966-81AE-0F34931602CA}");
  32. void Activate() override {}
  33. void Deactivate() override {}
  34. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided) { provided.push_back(AZ_CRC("MeshService", 0x71d8a455)); }
  35. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible) { incompatible.push_back(AZ_CRC("MeshService", 0x71d8a455)); }
  36. static void Reflect(AZ::ReflectContext*) {}
  37. };
  38. class MockActorComponent
  39. : public AZ::Component
  40. {
  41. public:
  42. AZ_COMPONENT(MockActorComponent, "{6B485E07-8466-4FD1-A7F9-D3D234201F5D}");
  43. void Activate() override {}
  44. void Deactivate() override {}
  45. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  46. {
  47. provided.push_back(AZ_CRC("EMotionFXActorService", 0xd6e8f48d));
  48. provided.push_back(AZ_CRC("MeshService", 0x71d8a455));
  49. provided.push_back(AZ_CRC("CharacterPhysicsDataService", 0x34757927));
  50. }
  51. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  52. {
  53. incompatible.push_back(AZ_CRC("EMotionFXActorService", 0xd6e8f48d));
  54. incompatible.push_back(AZ_CRC("MeshService", 0x71d8a455));
  55. }
  56. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  57. {
  58. required.push_back(AZ_CRC("TransformService", 0x8ee22c50));
  59. }
  60. static void Reflect(AZ::ReflectContext*) {}
  61. };
  62. class ComponentDependencyFixture
  63. : public SystemComponentFixture
  64. {
  65. public:
  66. void SetUp() override
  67. {
  68. SystemComponentFixture::SetUp();
  69. m_mockAudioProxyCompDesc = aznew MockAudioProxyComponent::DescriptorType;
  70. m_mockMeshCompDesc = aznew MockMeshComponent::DescriptorType;
  71. m_mockActorCompDesc = aznew MockActorComponent::DescriptorType;
  72. m_animAudioCompDesc = aznew EMotionFX::Integration::AnimAudioComponent::DescriptorType;
  73. m_txfmCompDesc = aznew AzFramework::TransformComponent::DescriptorType;
  74. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::RegisterComponentDescriptor, m_mockAudioProxyCompDesc);
  75. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::RegisterComponentDescriptor, m_mockMeshCompDesc);
  76. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::RegisterComponentDescriptor, m_mockActorCompDesc);
  77. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::RegisterComponentDescriptor, m_animAudioCompDesc);
  78. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::RegisterComponentDescriptor, m_txfmCompDesc);
  79. }
  80. void TearDown() override
  81. {
  82. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::UnregisterComponentDescriptor, m_mockAudioProxyCompDesc);
  83. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::UnregisterComponentDescriptor, m_mockMeshCompDesc);
  84. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::UnregisterComponentDescriptor, m_mockActorCompDesc);
  85. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::UnregisterComponentDescriptor, m_animAudioCompDesc);
  86. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::UnregisterComponentDescriptor, m_txfmCompDesc);
  87. delete m_mockMeshCompDesc;
  88. delete m_mockAudioProxyCompDesc;
  89. delete m_mockActorCompDesc;
  90. delete m_animAudioCompDesc;
  91. delete m_txfmCompDesc;
  92. SystemComponentFixture::TearDown();
  93. }
  94. protected:
  95. MockAudioProxyComponent::DescriptorType* m_mockAudioProxyCompDesc = nullptr;
  96. MockMeshComponent::DescriptorType* m_mockMeshCompDesc = nullptr;
  97. MockActorComponent::DescriptorType* m_mockActorCompDesc = nullptr;
  98. EMotionFX::Integration::AnimAudioComponent::DescriptorType* m_animAudioCompDesc = nullptr;
  99. AzFramework::TransformComponent::DescriptorType* m_txfmCompDesc = nullptr;
  100. };
  101. TEST_F(ComponentDependencyFixture, AnimAudioComponent_ResolveComponentDependencies)
  102. {
  103. AZ::Entity* entity = aznew AZ::Entity();
  104. entity->CreateComponent<AzFramework::TransformComponent>();
  105. entity->CreateComponent<EMotionFX::Integration::AnimAudioComponent>();
  106. entity->CreateComponent<MockAudioProxyComponent>();
  107. EXPECT_EQ(AZ::Entity::DependencySortResult::MissingRequiredService, entity->EvaluateDependencies());
  108. // Add a "MeshService", services still not met.
  109. auto meshComp = entity->CreateComponent<MockMeshComponent>();
  110. EXPECT_EQ(AZ::Entity::DependencySortResult::MissingRequiredService, entity->EvaluateDependencies());
  111. // Add an "EMotionFXActorService", incompatible with "MeshService" because MockActorComponent already provides "MeshService".
  112. entity->CreateComponent<MockActorComponent>();
  113. EXPECT_EQ(AZ::Entity::DependencySortResult::HasIncompatibleServices, entity->EvaluateDependencies());
  114. // Remove MeshComponent, services should resolve now.
  115. entity->RemoveComponent(meshComp);
  116. EXPECT_EQ(AZ::Entity::DependencySortResult::Success, entity->EvaluateDependencies());
  117. delete meshComp;
  118. delete entity;
  119. }
  120. } // namespace EMotionFX