AudioComponentTests.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <AzTest/AzTest.h>
  9. #include <AzCore/Component/ComponentApplication.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. #include <AzFramework/Components/TransformComponent.h>
  12. #include <Audio/AudioMultiPositionComponent.h>
  13. #include <Audio/AudioProxyComponent.h>
  14. #include <Audio/AudioTriggerComponent.h>
  15. namespace UnitTest
  16. {
  17. class AudioMultiPositionComponentTests
  18. : public LeakDetectionFixture
  19. {
  20. AZStd::unique_ptr<AZ::SerializeContext> m_serializeContext;
  21. AZStd::unique_ptr<AZ::ComponentDescriptor> m_transformComponentDescriptor;
  22. AZStd::unique_ptr<AZ::ComponentDescriptor> m_audioProxyComponentDescriptor;
  23. AZStd::unique_ptr<AZ::ComponentDescriptor> m_audioTriggerComponentDescriptor;
  24. AZStd::unique_ptr<AZ::ComponentDescriptor> m_audioMultiPosComponentDescriptor;
  25. public:
  26. AudioMultiPositionComponentTests()
  27. : LeakDetectionFixture()
  28. {}
  29. AZStd::size_t GetNumEntityRefs(LmbrCentral::AudioMultiPositionComponent* audioComponent)
  30. {
  31. return audioComponent->GetNumEntityRefs();
  32. }
  33. protected:
  34. void SetUp() override
  35. {
  36. LeakDetectionFixture::SetUp();
  37. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  38. m_transformComponentDescriptor = AZStd::unique_ptr<AZ::ComponentDescriptor>(AzFramework::TransformComponent::CreateDescriptor());
  39. m_audioProxyComponentDescriptor = AZStd::unique_ptr<AZ::ComponentDescriptor>(LmbrCentral::AudioProxyComponent::CreateDescriptor());
  40. m_audioTriggerComponentDescriptor = AZStd::unique_ptr<AZ::ComponentDescriptor>(LmbrCentral::AudioTriggerComponent::CreateDescriptor());
  41. m_audioMultiPosComponentDescriptor = AZStd::unique_ptr<AZ::ComponentDescriptor>(LmbrCentral::AudioMultiPositionComponent::CreateDescriptor());
  42. m_transformComponentDescriptor->Reflect(m_serializeContext.get());
  43. m_audioProxyComponentDescriptor->Reflect(m_serializeContext.get());
  44. m_audioTriggerComponentDescriptor->Reflect(m_serializeContext.get());
  45. m_audioMultiPosComponentDescriptor->Reflect(m_serializeContext.get());
  46. }
  47. void TearDown() override
  48. {
  49. m_audioMultiPosComponentDescriptor.reset();
  50. m_audioTriggerComponentDescriptor.reset();
  51. m_audioMultiPosComponentDescriptor.reset();
  52. m_audioProxyComponentDescriptor.reset();
  53. m_transformComponentDescriptor.reset();
  54. m_serializeContext.reset();
  55. LeakDetectionFixture::TearDown();
  56. }
  57. void CreateDefaultSetup(AZ::Entity& entity)
  58. {
  59. entity.CreateComponent<AzFramework::TransformComponent>();
  60. entity.CreateComponent<LmbrCentral::AudioProxyComponent>();
  61. entity.CreateComponent<LmbrCentral::AudioTriggerComponent>();
  62. entity.CreateComponent<LmbrCentral::AudioMultiPositionComponent>();
  63. entity.Init();
  64. entity.Activate();
  65. }
  66. };
  67. TEST_F(AudioMultiPositionComponentTests, MultiPositionComponent_ComponentExists)
  68. {
  69. AZ::Entity entity;
  70. CreateDefaultSetup(entity);
  71. LmbrCentral::AudioMultiPositionComponent* multiPosComponent = entity.FindComponent<LmbrCentral::AudioMultiPositionComponent>();
  72. EXPECT_TRUE(multiPosComponent != nullptr);
  73. }
  74. TEST_F(AudioMultiPositionComponentTests, MultiPositionComponent_AddAndRemoveEntity)
  75. {
  76. AZ::Entity entity;
  77. CreateDefaultSetup(entity);
  78. LmbrCentral::AudioMultiPositionComponent* multiPosComponent = entity.FindComponent<LmbrCentral::AudioMultiPositionComponent>();
  79. ASSERT_TRUE(multiPosComponent != nullptr);
  80. if (multiPosComponent)
  81. {
  82. AZ::Entity entity1;
  83. AZ::Entity entity2;
  84. EXPECT_EQ(GetNumEntityRefs(multiPosComponent), 0);
  85. LmbrCentral::AudioMultiPositionComponentRequestBus::Event(entity.GetId(), &LmbrCentral::AudioMultiPositionComponentRequestBus::Events::AddEntity, entity1.GetId());
  86. EXPECT_EQ(GetNumEntityRefs(multiPosComponent), 1);
  87. // Remove non-Added
  88. LmbrCentral::AudioMultiPositionComponentRequestBus::Event(entity.GetId(), &LmbrCentral::AudioMultiPositionComponentRequestBus::Events::RemoveEntity, entity2.GetId());
  89. EXPECT_EQ(GetNumEntityRefs(multiPosComponent), 1);
  90. // Remove Added
  91. LmbrCentral::AudioMultiPositionComponentRequestBus::Event(entity.GetId(), &LmbrCentral::AudioMultiPositionComponentRequestBus::Events::RemoveEntity, entity1.GetId());
  92. EXPECT_EQ(GetNumEntityRefs(multiPosComponent), 0);
  93. }
  94. }
  95. TEST_F(AudioMultiPositionComponentTests, MultiPositionComponent_EntityActivatedObtainsPosition)
  96. {
  97. AZ::Entity entity;
  98. CreateDefaultSetup(entity);
  99. LmbrCentral::AudioMultiPositionComponent* multiPosComponent = entity.FindComponent<LmbrCentral::AudioMultiPositionComponent>();
  100. ASSERT_TRUE(multiPosComponent != nullptr);
  101. if (multiPosComponent)
  102. {
  103. AZ::Entity entity1;
  104. entity1.Init();
  105. entity1.Activate();
  106. LmbrCentral::AudioMultiPositionComponentRequestBus::Event(entity.GetId(), &LmbrCentral::AudioMultiPositionComponentRequestBus::Events::AddEntity, entity1.GetId());
  107. // Send OnEntityActivated
  108. AZ::EntityBus::Event(entity1.GetId(), &AZ::EntityBus::Events::OnEntityActivated, entity1.GetId());
  109. EXPECT_EQ(GetNumEntityRefs(multiPosComponent), 1);
  110. }
  111. }
  112. } // namespace UnitTest