ScriptCanvasUnitTest_EntityFunctions.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/Component/ComponentApplicationBus.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Component/TransformBus.h>
  11. #include <AzCore/UnitTest/TestTypes.h>
  12. #include <Tests/Framework/ScriptCanvasUnitTestFixture.h>
  13. #include <Libraries/Entity/EntityFunctions.h>
  14. namespace ScriptCanvasUnitTest
  15. {
  16. using namespace ScriptCanvas;
  17. class ScriptCanvasUnitTestEntityFunctions
  18. : public ScriptCanvasUnitTestFixture
  19. , public AZ::TransformBus::Handler
  20. , public AZ::ComponentApplicationBus::Handler
  21. {
  22. public:
  23. void SetUp() override
  24. {
  25. ScriptCanvasUnitTestFixture::SetUp();
  26. m_localTransform = AZ::Transform::CreateIdentity();
  27. m_wordTransform = AZ::Transform::CreateIdentity();
  28. m_entity.Init();
  29. m_entity.Activate();
  30. AZ::TransformBus::Handler::BusConnect(m_id);
  31. AZ::ComponentApplicationBus::Handler::BusConnect();
  32. }
  33. void TearDown() override
  34. {
  35. m_entity.Deactivate();
  36. AZ::ComponentApplicationBus::Handler::BusDisconnect();
  37. AZ::TransformBus::Handler::BusDisconnect(m_id);
  38. ScriptCanvasUnitTestFixture::TearDown();
  39. }
  40. //////////////////////////////////////////////////////////////////////////
  41. // ComponentApplicationBus
  42. AZ::ComponentApplication* GetApplication() override { return nullptr; }
  43. void RegisterComponentDescriptor(const AZ::ComponentDescriptor*) override { }
  44. void UnregisterComponentDescriptor(const AZ::ComponentDescriptor*) override { }
  45. void RegisterEntityAddedEventHandler(AZ::EntityAddedEvent::Handler&) override { }
  46. void RegisterEntityRemovedEventHandler(AZ::EntityRemovedEvent::Handler&) override { }
  47. void RegisterEntityActivatedEventHandler(AZ::EntityActivatedEvent::Handler&) override { }
  48. void RegisterEntityDeactivatedEventHandler(AZ::EntityDeactivatedEvent::Handler&) override { }
  49. void SignalEntityActivated(AZ::Entity*) override { }
  50. void SignalEntityDeactivated(AZ::Entity*) override { }
  51. bool AddEntity(AZ::Entity*) override { return false; }
  52. bool RemoveEntity(AZ::Entity*) override { return false; }
  53. bool DeleteEntity(const AZ::EntityId&) override { return false; }
  54. AZ::Entity* FindEntity(const AZ::EntityId&) override { return &m_entity; }
  55. AZ::SerializeContext* GetSerializeContext() override { return nullptr; }
  56. AZ::BehaviorContext* GetBehaviorContext() override { return nullptr; }
  57. AZ::JsonRegistrationContext* GetJsonRegistrationContext() override { return nullptr; }
  58. const char* GetEngineRoot() const override { return nullptr; }
  59. const char* GetExecutableFolder() const override { return nullptr; }
  60. void EnumerateEntities(const AZ::ComponentApplicationRequests::EntityCallback& /*callback*/) override {}
  61. void QueryApplicationType(AZ::ApplicationTypeQuery& /*appType*/) const override {}
  62. //////////////////////////////////////////////////////////////////////////
  63. //////////////////////////////////////////////////////////////////////////
  64. // TransformBus
  65. void BindTransformChangedEventHandler(AZ::TransformChangedEvent::Handler&) override {}
  66. void BindParentChangedEventHandler(AZ::ParentChangedEvent::Handler&) override {}
  67. void BindChildChangedEventHandler(AZ::ChildChangedEvent::Handler&) override {}
  68. void NotifyChildChangedEvent(AZ::ChildChangeType, AZ::EntityId) override {}
  69. const AZ::Transform& GetLocalTM() override { return m_localTransform; }
  70. bool IsStaticTransform() override { return false; }
  71. const AZ::Transform& GetWorldTM() override { return m_wordTransform; }
  72. void SetWorldTM(const AZ::Transform& tm) override { m_wordTransform = tm; }
  73. //////////////////////////////////////////////////////////////////////////
  74. AZ::EntityId m_id{123};
  75. AZ::Transform m_localTransform;
  76. AZ::Transform m_wordTransform;
  77. AZ::Entity m_entity;
  78. };
  79. TEST_F(ScriptCanvasUnitTestEntityFunctions, GetEntityRight_Call_GetExpectedResult)
  80. {
  81. float scale = 123.f;
  82. auto actualResult = EntityFunctions::GetEntityRight(m_id, scale);
  83. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  84. EXPECT_THAT(actualResult, IsClose(AZ::Vector3(scale, 0, 0)));
  85. #else
  86. EXPECT_EQ(actualResult, AZ::Vector3(scale, 0, 0));
  87. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  88. }
  89. TEST_F(ScriptCanvasUnitTestEntityFunctions, GetEntityForward_Call_GetExpectedResult)
  90. {
  91. float scale = 123.f;
  92. auto actualResult = EntityFunctions::GetEntityForward(m_id, scale);
  93. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  94. EXPECT_THAT(actualResult, IsClose(AZ::Vector3(0, scale, 0)));
  95. #else
  96. EXPECT_EQ(actualResult, AZ::Vector3(0, scale, 0));
  97. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  98. }
  99. TEST_F(ScriptCanvasUnitTestEntityFunctions, GetEntityUp_Call_GetExpectedResult)
  100. {
  101. float scale = 123.f;
  102. auto actualResult = EntityFunctions::GetEntityUp(m_id, scale);
  103. #if AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  104. EXPECT_THAT(actualResult, IsClose(AZ::Vector3(0, 0, scale)));
  105. #else
  106. EXPECT_EQ(actualResult, AZ::Vector3(0, 0, scale));
  107. #endif // AZ_TRAIT_USE_PLATFORM_SIMD_NEON
  108. }
  109. TEST_F(ScriptCanvasUnitTestEntityFunctions, Rotate_Call_GetExpectedResult)
  110. {
  111. auto rotation = AZ::Vector3(180, 0, 0);
  112. EntityFunctions::Rotate(m_id, AZ::Vector3(180, 0, 0));
  113. EXPECT_EQ(m_wordTransform.GetRotation(), AZ::ConvertEulerDegreesToQuaternion(rotation));
  114. }
  115. TEST_F(ScriptCanvasUnitTestEntityFunctions, IsActive_Call_GetExpectedResult)
  116. {
  117. auto actualResult = EntityFunctions::IsActive(m_id);
  118. EXPECT_TRUE(actualResult);
  119. }
  120. TEST_F(ScriptCanvasUnitTestEntityFunctions, IsValid_Call_GetExpectedResult)
  121. {
  122. auto actualResult = EntityFunctions::IsValid(m_id);
  123. EXPECT_TRUE(actualResult);
  124. }
  125. TEST_F(ScriptCanvasUnitTestEntityFunctions, ToString_Call_GetExpectedResult)
  126. {
  127. auto actualResult = EntityFunctions::ToString(m_id);
  128. EXPECT_EQ(actualResult, m_id.ToString());
  129. }
  130. }