EntityUtilityComponentTests.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 <AzFramework/Entity/BehaviorEntity.h>
  9. #include <AzTest/AzTest.h>
  10. #include <AzToolsFramework/Application/ToolsApplication.h>
  11. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  12. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  13. #include <Entity/EntityUtilityComponent.h>
  14. #include <ToolsComponents/TransformComponent.h>
  15. namespace UnitTest
  16. {
  17. // Global variables for communicating between Lua test code and C++
  18. AZ::EntityId g_globalEntityId = AZ::EntityId{};
  19. AZStd::string g_globalString = "";
  20. AzFramework::BehaviorComponentId g_globalComponentId = {};
  21. AZStd::vector<AzToolsFramework::ComponentDetails> g_globalComponentDetails = {};
  22. bool g_globalBool = false;
  23. class EntityUtilityComponentTests
  24. : public ToolsApplicationFixture<>
  25. {
  26. void InitProperties()
  27. {
  28. AZ::ComponentApplicationRequests* componentApplicationRequests = AZ::Interface<AZ::ComponentApplicationRequests>::Get();
  29. ASSERT_NE(componentApplicationRequests, nullptr);
  30. auto behaviorContext = componentApplicationRequests->GetBehaviorContext();
  31. ASSERT_NE(behaviorContext, nullptr);
  32. behaviorContext->Property("g_globalEntityId", BehaviorValueProperty(&g_globalEntityId));
  33. behaviorContext->Property("g_globalString", BehaviorValueProperty(&g_globalString));
  34. behaviorContext->Property("g_globalComponentId", BehaviorValueProperty(&g_globalComponentId));
  35. behaviorContext->Property("g_globalBool", BehaviorValueProperty(&g_globalBool));
  36. behaviorContext->Property("g_globalComponentDetails", BehaviorValueProperty(&g_globalComponentDetails));
  37. g_globalEntityId = AZ::EntityId{};
  38. g_globalString = AZStd::string{};
  39. g_globalComponentId = AzFramework::BehaviorComponentId{};
  40. g_globalBool = false;
  41. g_globalComponentDetails = AZStd::vector<AzToolsFramework::ComponentDetails>{};
  42. }
  43. void SetUpEditorFixtureImpl() override
  44. {
  45. InitProperties();
  46. }
  47. void TearDownEditorFixtureImpl() override
  48. {
  49. g_globalString.set_capacity(0); // Free all memory
  50. g_globalComponentDetails.set_capacity(0);
  51. }
  52. };
  53. TEST_F(EntityUtilityComponentTests, CreateEntity)
  54. {
  55. AZ::ScriptContext sc;
  56. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  57. sc.BindTo(behaviorContext);
  58. sc.Execute(R"LUA(
  59. g_globalEntityId = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  60. my_entity = Entity(g_globalEntityId)
  61. g_globalString = my_entity:GetName()
  62. )LUA");
  63. EXPECT_NE(g_globalEntityId, AZ::EntityId{});
  64. EXPECT_STREQ(g_globalString.c_str(), "test");
  65. AZ::Entity* entity = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->FindEntity(g_globalEntityId);
  66. ASSERT_NE(entity, nullptr);
  67. // Test cleaning up, make sure the entity is destroyed
  68. AzToolsFramework::EntityUtilityBus::Broadcast(&AzToolsFramework::EntityUtilityBus::Events::ResetEntityContext);
  69. entity = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->FindEntity(g_globalEntityId);
  70. ASSERT_EQ(entity, nullptr);
  71. }
  72. TEST_F(EntityUtilityComponentTests, CreateEntityEmptyName)
  73. {
  74. AZ::ScriptContext sc;
  75. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  76. sc.BindTo(behaviorContext);
  77. sc.Execute(R"LUA(
  78. g_globalEntityId = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("")
  79. )LUA");
  80. EXPECT_NE(g_globalEntityId, AZ::EntityId{});
  81. AZ::Entity* entity = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->FindEntity(g_globalEntityId);
  82. ASSERT_NE(entity, nullptr);
  83. }
  84. TEST_F(EntityUtilityComponentTests, FindComponent)
  85. {
  86. AZ::ScriptContext sc;
  87. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  88. sc.BindTo(behaviorContext);
  89. sc.Execute(R"LUA(
  90. ent_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  91. g_globalComponentId = EntityUtilityBus.Broadcast.GetOrAddComponentByTypeName(ent_id, "27F1E1A1-8D9D-4C3B-BD3A-AFB9762449C0 TransformComponent")
  92. )LUA");
  93. EXPECT_TRUE(g_globalComponentId.IsValid());
  94. }
  95. TEST_F(EntityUtilityComponentTests, InvalidComponentName)
  96. {
  97. AZ::ScriptContext sc;
  98. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  99. sc.BindTo(behaviorContext);
  100. AZ_TEST_START_TRACE_SUPPRESSION;
  101. sc.Execute(R"LUA(
  102. ent_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  103. g_globalComponentId = EntityUtilityBus.Broadcast.GetOrAddComponentByTypeName(ent_id, "ThisIsNotAComponent-Error")
  104. )LUA");
  105. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  106. EXPECT_FALSE(g_globalComponentId.IsValid());
  107. }
  108. TEST_F(EntityUtilityComponentTests, InvalidComponentId)
  109. {
  110. AZ::ScriptContext sc;
  111. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  112. sc.BindTo(behaviorContext);
  113. AZ_TEST_START_TRACE_SUPPRESSION;
  114. sc.Execute(R"LUA(
  115. ent_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  116. g_globalComponentId = EntityUtilityBus.Broadcast.GetOrAddComponentByTypeName(ent_id, "{1234-hello-world-this-is-not-an-id}")
  117. )LUA");
  118. AZ_TEST_STOP_TRACE_SUPPRESSION(1); // Should get 1 error stating the type id is not valid
  119. EXPECT_FALSE(g_globalComponentId.IsValid());
  120. }
  121. TEST_F(EntityUtilityComponentTests, CreateComponent)
  122. {
  123. AZ::ScriptContext sc;
  124. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  125. sc.BindTo(behaviorContext);
  126. sc.Execute(R"LUA(
  127. ent_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  128. g_globalComponentId = EntityUtilityBus.Broadcast.GetOrAddComponentByTypeName(ent_id, "ScriptEditorComponent")
  129. )LUA");
  130. EXPECT_TRUE(g_globalComponentId.IsValid());
  131. }
  132. TEST_F(EntityUtilityComponentTests, UpdateComponent)
  133. {
  134. AZ::ScriptContext sc;
  135. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  136. sc.BindTo(behaviorContext);
  137. sc.Execute(R"LUA(
  138. g_globalEntityId = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  139. comp_id = EntityUtilityBus.Broadcast.GetOrAddComponentByTypeName(g_globalEntityId, "{27F1E1A1-8D9D-4C3B-BD3A-AFB9762449C0} TransformComponent")
  140. json_update = [[
  141. {
  142. "Transform Data": { "Rotate": [0.0, 0.1, 180.0] }
  143. }
  144. ]]
  145. g_globalBool = EntityUtilityBus.Broadcast.UpdateComponentForEntity(g_globalEntityId, comp_id, json_update);
  146. )LUA");
  147. EXPECT_TRUE(g_globalBool);
  148. EXPECT_NE(g_globalEntityId, AZ::EntityId(AZ::EntityId::InvalidEntityId));
  149. AZ::Entity* entity = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->FindEntity(g_globalEntityId);
  150. auto* transformComponent = entity->FindComponent<AzToolsFramework::Components::TransformComponent>();
  151. ASSERT_NE(transformComponent, nullptr);
  152. AZ::Vector3 localRotation = transformComponent->GetLocalRotationQuaternion().GetEulerDegrees();
  153. EXPECT_EQ(localRotation, AZ::Vector3(.0f, 0.1f, 180.0f));
  154. }
  155. TEST_F(EntityUtilityComponentTests, GetComponentJson)
  156. {
  157. AZ::ScriptContext sc;
  158. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  159. sc.BindTo(behaviorContext);
  160. sc.Execute(R"LUA(
  161. g_globalString = EntityUtilityBus.Broadcast.GetComponentDefaultJson("ScriptEditorComponent")
  162. )LUA");
  163. EXPECT_STRNE(g_globalString.c_str(), "");
  164. }
  165. TEST_F(EntityUtilityComponentTests, GetComponentJsonDoesNotExist)
  166. {
  167. AZ::ScriptContext sc;
  168. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  169. sc.BindTo(behaviorContext);
  170. AZ_TEST_START_TRACE_SUPPRESSION;
  171. sc.Execute(R"LUA(
  172. g_globalString = EntityUtilityBus.Broadcast.GetComponentDefaultJson("404")
  173. )LUA");
  174. AZ_TEST_STOP_TRACE_SUPPRESSION(1); // 1 error: Failed to find component id for type name 404
  175. EXPECT_STREQ(g_globalString.c_str(), "");
  176. }
  177. TEST_F(EntityUtilityComponentTests, SearchComponents)
  178. {
  179. AZ::ScriptContext sc;
  180. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  181. sc.BindTo(behaviorContext);
  182. sc.Execute(R"LUA(
  183. g_globalComponentDetails = EntityUtilityBus.Broadcast.FindMatchingComponents("Transform*")
  184. )LUA");
  185. // There should be 2 transform components
  186. EXPECT_EQ(g_globalComponentDetails.size(), 2);
  187. }
  188. TEST_F(EntityUtilityComponentTests, SearchComponentsNotFound)
  189. {
  190. AZ::ScriptContext sc;
  191. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  192. sc.BindTo(behaviorContext);
  193. sc.Execute(R"LUA(
  194. g_globalComponentDetails = EntityUtilityBus.Broadcast.FindMatchingComponents("404")
  195. )LUA");
  196. EXPECT_EQ(g_globalComponentDetails.size(), 0);
  197. }
  198. }