BehaviorEntityTests.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "FrameworkApplicationFixture.h"
  9. #include <AzCore/Component/Component.h>
  10. #include <AzFramework/Entity/BehaviorEntity.h>
  11. #include <AzFramework/Entity/GameEntityContextBus.h>
  12. // some fake components to test with
  13. static constexpr AZ::TypeId HatComponentTypeId{ "{EADEF936-E987-4BF3-9651-A42251827628}" };
  14. class HatConfig : public AZ::ComponentConfig
  15. {
  16. public:
  17. AZ_CLASS_ALLOCATOR(HatConfig, AZ::SystemAllocator)
  18. AZ_RTTI(HatConfig, "{A3129800-43DF-48CA-9BC3-77632241B8ED}", ComponentConfig);
  19. float m_brimWidth = 1.f;
  20. };
  21. class HatComponent : public AZ::Component
  22. {
  23. public:
  24. AZ_COMPONENT(HatComponent, HatComponentTypeId);
  25. static void Reflect(AZ::ReflectContext*) {}
  26. void Activate() override {}
  27. void Deactivate() override {}
  28. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override
  29. {
  30. if (auto config = azrtti_cast<const HatConfig*>(baseConfig))
  31. {
  32. m_config = *config;
  33. return true;
  34. }
  35. return false;
  36. }
  37. bool WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const override
  38. {
  39. if (auto outConfig = azrtti_cast<HatConfig*>(outBaseConfig))
  40. {
  41. *outConfig = m_config;
  42. return true;
  43. }
  44. return false;
  45. }
  46. HatConfig m_config;
  47. };
  48. static constexpr AZ::TypeId EarComponentTypeId{ "{1F741BC1-451F-445F-891B-1204D6A434D0}" };
  49. class EarComponent : public AZ::Component
  50. {
  51. public:
  52. AZ_COMPONENT(EarComponent, EarComponentTypeId);
  53. static void Reflect(AZ::ReflectContext*) {}
  54. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services) { services.push_back(AZ::Crc32("EarService")); }
  55. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services) { services.push_back(AZ::Crc32("EarService")); }
  56. void Activate() override {}
  57. void Deactivate() override {}
  58. };
  59. static constexpr AZ::TypeId DeactivateDuringActivationComponentTypeId{ "{E18A3FFE-FA61-4682-A6C2-FB065D5DDDD2}" };
  60. class DeactivateDuringActivationComponent : public AZ::Component
  61. {
  62. public:
  63. AZ_COMPONENT(DeactivateDuringActivationComponent , DeactivateDuringActivationComponentTypeId);
  64. static void Reflect(AZ::ReflectContext*) {}
  65. void Activate() override
  66. {
  67. AzFramework::BehaviorEntity behaviorEntity{ GetEntityId() };
  68. behaviorEntity.Deactivate();
  69. }
  70. void Deactivate() override {}
  71. };
  72. class BehaviorEntityTest
  73. : public UnitTest::FrameworkApplicationFixture
  74. {
  75. protected:
  76. void SetUp() override
  77. {
  78. m_appDescriptor.m_enableScriptReflection = true;
  79. FrameworkApplicationFixture::SetUp();
  80. m_application->RegisterComponentDescriptor(HatComponent::CreateDescriptor());
  81. m_application->RegisterComponentDescriptor(EarComponent::CreateDescriptor());
  82. m_application->RegisterComponentDescriptor(DeactivateDuringActivationComponent::CreateDescriptor());
  83. AzFramework::GameEntityContextRequestBus::BroadcastResult(m_rawEntity, &AzFramework::GameEntityContextRequestBus::Events::CreateGameEntity, "Hat");
  84. m_behaviorEntity = AzFramework::BehaviorEntity(m_rawEntity->GetId());
  85. }
  86. void TearDown() override
  87. {
  88. FrameworkApplicationFixture::TearDown();
  89. }
  90. AZ::Entity* m_rawEntity = nullptr;
  91. AzFramework::BehaviorEntity m_behaviorEntity;
  92. };
  93. TEST_F(BehaviorEntityTest, FixtureSanityCheck_Succeeds)
  94. {
  95. EXPECT_NE(nullptr, m_rawEntity);
  96. }
  97. TEST_F(BehaviorEntityTest, GetName_Succeeds)
  98. {
  99. EXPECT_EQ(m_rawEntity->GetName(), m_behaviorEntity.GetName());
  100. }
  101. TEST_F(BehaviorEntityTest, SetName_Succeeds)
  102. {
  103. AZStd::string targetName = "Colden";
  104. m_behaviorEntity.SetName(targetName.c_str());
  105. EXPECT_EQ(targetName, m_rawEntity->GetName());
  106. }
  107. TEST_F(BehaviorEntityTest, GetOwningContextId_MatchesGameEntityContextId)
  108. {
  109. AzFramework::EntityContextId gameEntityContextId = AzFramework::EntityContextId::CreateNull();
  110. AzFramework::GameEntityContextRequestBus::BroadcastResult(gameEntityContextId, &AzFramework::GameEntityContextRequestBus::Events::GetGameEntityContextId);
  111. EXPECT_EQ(m_behaviorEntity.GetOwningContextId(), gameEntityContextId);
  112. EXPECT_FALSE(m_behaviorEntity.GetOwningContextId().IsNull());
  113. }
  114. TEST_F(BehaviorEntityTest, Exists_ForActualEntity_True)
  115. {
  116. EXPECT_TRUE(m_behaviorEntity.Exists());
  117. }
  118. TEST_F(BehaviorEntityTest, Exists_ForDeletedEntity_False)
  119. {
  120. delete m_rawEntity;
  121. EXPECT_FALSE(m_behaviorEntity.Exists());
  122. }
  123. TEST_F(BehaviorEntityTest, IsActivated_ForNewEntity_False)
  124. {
  125. EXPECT_EQ(AZ::Entity::State::Init, m_rawEntity->GetState()); // sanity check
  126. EXPECT_FALSE(m_behaviorEntity.IsActivated());
  127. }
  128. TEST_F(BehaviorEntityTest, IsActivated_ForActivatedEntity_True)
  129. {
  130. m_rawEntity->Activate();
  131. EXPECT_EQ(AZ::Entity::State::Active, m_rawEntity->GetState()); // sanity check
  132. EXPECT_TRUE(m_behaviorEntity.IsActivated());
  133. }
  134. TEST_F(BehaviorEntityTest, Activate_Succeeds)
  135. {
  136. m_behaviorEntity.Activate();
  137. EXPECT_EQ(AZ::Entity::State::Active, m_rawEntity->GetState());
  138. }
  139. TEST_F(BehaviorEntityTest, Deactivate_ForActivatedEntity_Succeeds)
  140. {
  141. m_rawEntity->Activate();
  142. EXPECT_EQ(AZ::Entity::State::Active, m_rawEntity->GetState()); // sanity check
  143. m_behaviorEntity.Deactivate();
  144. EXPECT_EQ(AZ::Entity::State::Init, m_rawEntity->GetState());
  145. }
  146. TEST_F(BehaviorEntityTest, Deactivate_ForActivatingEntity_SucceedsOneTickLater)
  147. {
  148. // this component calls BehaviorEntity::Deactivate() during Activate().
  149. // activate should succeed, and a deactivate should be queued on TickBus
  150. m_rawEntity->CreateComponent(DeactivateDuringActivationComponentTypeId);
  151. m_rawEntity->Activate();
  152. EXPECT_EQ(AZ::Entity::State::Active, m_rawEntity->GetState());
  153. m_application->Tick();
  154. EXPECT_EQ(AZ::Entity::State::Init, m_rawEntity->GetState());
  155. }
  156. TEST_F(BehaviorEntityTest, CreateComponent_Succeeds)
  157. {
  158. AzFramework::BehaviorComponentId componentId = m_behaviorEntity.CreateComponent(HatComponentTypeId);
  159. EXPECT_TRUE(componentId.IsValid());
  160. AZ::Component* rawComponent = m_rawEntity->FindComponent(componentId);
  161. EXPECT_NE(nullptr, rawComponent);
  162. EXPECT_EQ(HatComponentTypeId, azrtti_typeid(rawComponent));
  163. }
  164. TEST_F(BehaviorEntityTest, CreateComponent_WithNonexistentType_Fails)
  165. {
  166. // We expect an assert in Entity::CreateComponent()
  167. UnitTest::TestRunner::Instance().StartAssertTests();
  168. AzFramework::BehaviorComponentId componentId = m_behaviorEntity.CreateComponent(AZ::TypeId::CreateNull());
  169. UnitTest::TestRunner::Instance().StopAssertTests();
  170. EXPECT_FALSE(componentId.IsValid());
  171. EXPECT_EQ(0, m_rawEntity->GetComponents().size());
  172. }
  173. TEST_F(BehaviorEntityTest, CreateComponent_WithIncompatibleType_Fails)
  174. {
  175. AzFramework::BehaviorComponentId componentId1 = m_behaviorEntity.CreateComponent(EarComponentTypeId);
  176. AzFramework::BehaviorComponentId componentId2 = m_behaviorEntity.CreateComponent(EarComponentTypeId);
  177. EXPECT_TRUE(componentId1.IsValid());
  178. EXPECT_FALSE(componentId2.IsValid());
  179. EXPECT_EQ(1, m_rawEntity->GetComponents().size());
  180. EXPECT_NE(nullptr, m_rawEntity->FindComponent(componentId1));
  181. }
  182. TEST_F(BehaviorEntityTest, DestroyComponent_Succeeds)
  183. {
  184. AZ::Component* rawComponent = m_rawEntity->CreateComponent(HatComponentTypeId);
  185. EXPECT_NE(nullptr, rawComponent); // sanity check
  186. bool destroyed = m_behaviorEntity.DestroyComponent(rawComponent->GetId());
  187. EXPECT_TRUE(destroyed);
  188. EXPECT_TRUE(m_rawEntity->GetComponents().empty());
  189. }
  190. TEST_F(BehaviorEntityTest, GetComponents_ReturnsAllComponents)
  191. {
  192. AZ::Component* rawComponent1 = m_rawEntity->CreateComponent(HatComponentTypeId);
  193. AZ::Component* rawComponent2 = m_rawEntity->CreateComponent(EarComponentTypeId);
  194. AZStd::vector<AzFramework::BehaviorComponentId> componentIds = m_behaviorEntity.GetComponents();
  195. EXPECT_EQ(2, componentIds.size());
  196. EXPECT_NE(componentIds.end(), AZStd::find(componentIds.begin(), componentIds.end(), AzFramework::BehaviorComponentId(rawComponent1->GetId())));
  197. EXPECT_NE(componentIds.end(), AZStd::find(componentIds.begin(), componentIds.end(), AzFramework::BehaviorComponentId(rawComponent2->GetId())));
  198. }
  199. TEST_F(BehaviorEntityTest, FindComponentOfType_Succeeds)
  200. {
  201. m_rawEntity->CreateComponent(HatComponentTypeId);
  202. AZ::Component* rawComponent2 = m_rawEntity->CreateComponent(EarComponentTypeId);
  203. AzFramework::BehaviorComponentId foundComponentId = m_behaviorEntity.FindComponentOfType(azrtti_typeid(rawComponent2));
  204. EXPECT_EQ(rawComponent2->GetId(), AZ::ComponentId(foundComponentId));
  205. }
  206. TEST_F(BehaviorEntityTest, FindComponentOfType_ForNonexistentComponent_ReturnsInvalidComponentId)
  207. {
  208. AzFramework::BehaviorComponentId foundComponentId = m_behaviorEntity.FindComponentOfType(HatComponentTypeId);
  209. EXPECT_FALSE(foundComponentId.IsValid());
  210. }
  211. TEST_F(BehaviorEntityTest, FindAllComponentsOfType_Succeeds)
  212. {
  213. m_rawEntity->CreateComponent(HatComponentTypeId);
  214. AZ::Component* rawComponent2 = m_rawEntity->CreateComponent(EarComponentTypeId);
  215. m_rawEntity->CreateComponent(HatComponentTypeId);
  216. AZ::Component* rawComponent4 = m_rawEntity->CreateComponent(EarComponentTypeId);
  217. AZStd::vector<AzFramework::BehaviorComponentId> componentIds = m_behaviorEntity.FindAllComponentsOfType(EarComponentTypeId);
  218. EXPECT_EQ(2, componentIds.size());
  219. EXPECT_NE(componentIds.end(), AZStd::find(componentIds.begin(), componentIds.end(), AzFramework::BehaviorComponentId(rawComponent2->GetId())));
  220. EXPECT_NE(componentIds.end(), AZStd::find(componentIds.begin(), componentIds.end(), AzFramework::BehaviorComponentId(rawComponent4->GetId())));
  221. }
  222. TEST_F(BehaviorEntityTest, GetComponentType_Succeeds)
  223. {
  224. AZ::Component* rawComponent = m_rawEntity->CreateComponent(HatComponentTypeId);
  225. EXPECT_EQ(azrtti_typeid(rawComponent), m_behaviorEntity.GetComponentType(rawComponent->GetId()));
  226. }
  227. TEST_F(BehaviorEntityTest, GetComponentName_Succeeds)
  228. {
  229. AZ::Component* rawComponent = m_rawEntity->CreateComponent(HatComponentTypeId);
  230. EXPECT_EQ(m_behaviorEntity.GetComponentName(rawComponent->GetId()), "HatComponent");
  231. }
  232. TEST_F(BehaviorEntityTest, SetComponentConfiguration_Succeeds)
  233. {
  234. HatComponent* rawComponent = m_rawEntity->CreateComponent<HatComponent>();
  235. HatConfig customConfig;
  236. customConfig.m_brimWidth = 5.f;
  237. bool configSuccess = m_behaviorEntity.SetComponentConfiguration(rawComponent->GetId(), customConfig);
  238. EXPECT_TRUE(configSuccess);
  239. EXPECT_EQ(customConfig.m_brimWidth, rawComponent->m_config.m_brimWidth);
  240. }
  241. TEST_F(BehaviorEntityTest, GetComponentConfiguration_Succeeds)
  242. {
  243. HatComponent* rawComponent = m_rawEntity->CreateComponent<HatComponent>();
  244. rawComponent->m_config.m_brimWidth = 12.f;
  245. HatConfig retrievedConfig;
  246. bool configSuccess = m_behaviorEntity.GetComponentConfiguration(rawComponent->GetId(), retrievedConfig);
  247. EXPECT_TRUE(configSuccess);
  248. EXPECT_EQ(rawComponent->m_config.m_brimWidth, retrievedConfig.m_brimWidth);
  249. }