PrefabScriptingTests.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <AzToolsFramework/ToolsComponents/TransformComponent.h>
  9. #include <Entity/EntityUtilityComponent.h>
  10. #include <Prefab/PrefabTestComponent.h>
  11. #include <Prefab/PrefabTestDomUtils.h>
  12. #include <Prefab/PrefabTestFixture.h>
  13. namespace UnitTest
  14. {
  15. TemplateId g_globalTemplateId = {};
  16. AZStd::string g_globalPrefabString = "";
  17. class PrefabScriptingTest : public PrefabTestFixture
  18. {
  19. void InitProperties() const
  20. {
  21. AZ::ComponentApplicationRequests* componentApplicationRequests = AZ::Interface<AZ::ComponentApplicationRequests>::Get();
  22. ASSERT_NE(componentApplicationRequests, nullptr);
  23. auto behaviorContext = componentApplicationRequests->GetBehaviorContext();
  24. ASSERT_NE(behaviorContext, nullptr);
  25. behaviorContext->Property("g_globalTemplateId", BehaviorValueProperty(&g_globalTemplateId));
  26. behaviorContext->Property("g_globalPrefabString", BehaviorValueProperty(&g_globalPrefabString));
  27. g_globalTemplateId = TemplateId{};
  28. g_globalPrefabString = AZStd::string{};
  29. }
  30. void SetUpEditorFixtureImpl() override
  31. {
  32. InitProperties();
  33. }
  34. void TearDownEditorFixtureImpl() override
  35. {
  36. g_globalPrefabString.set_capacity(0); // Free all memory
  37. }
  38. };
  39. TEST_F(PrefabScriptingTest, PrefabScripting_CreatePrefab)
  40. {
  41. AZ::ScriptContext sc;
  42. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  43. sc.BindTo(behaviorContext);
  44. sc.Execute(R"LUA(
  45. my_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  46. entities = vector_EntityId()
  47. entities:push_back(my_id)
  48. g_globalTemplateId = PrefabSystemScriptingBus.Broadcast.CreatePrefab(entities, "test.prefab")
  49. )LUA");
  50. EXPECT_NE(g_globalTemplateId, TemplateId{});
  51. auto prefabSystemComponentInterface = AZ::Interface<PrefabSystemComponentInterface>::Get();
  52. ASSERT_NE(prefabSystemComponentInterface, nullptr);
  53. TemplateReference templateRef = prefabSystemComponentInterface->FindTemplate(g_globalTemplateId);
  54. EXPECT_TRUE(templateRef);
  55. }
  56. TEST_F(PrefabScriptingTest, PrefabScripting_CreatePrefab_NoEntities)
  57. {
  58. AZ::ScriptContext sc;
  59. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  60. sc.BindTo(behaviorContext);
  61. sc.Execute(R"LUA(
  62. my_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  63. entities = vector_EntityId()
  64. g_globalTemplateId = PrefabSystemScriptingBus.Broadcast.CreatePrefab(entities, "test.prefab")
  65. )LUA");
  66. EXPECT_NE(g_globalTemplateId, TemplateId{});
  67. auto prefabSystemComponentInterface = AZ::Interface<PrefabSystemComponentInterface>::Get();
  68. ASSERT_NE(prefabSystemComponentInterface, nullptr);
  69. TemplateReference templateRef = prefabSystemComponentInterface->FindTemplate(g_globalTemplateId);
  70. EXPECT_TRUE(templateRef);
  71. }
  72. TEST_F(PrefabScriptingTest, PrefabScripting_CreatePrefab_NoPath)
  73. {
  74. AZ::ScriptContext sc;
  75. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  76. sc.BindTo(behaviorContext);
  77. AZ_TEST_START_TRACE_SUPPRESSION;
  78. sc.Execute(R"LUA(
  79. my_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  80. entities = vector_EntityId()
  81. template_id = PrefabSystemScriptingBus.Broadcast.CreatePrefab(entities, "")
  82. )LUA");
  83. /*
  84. error: PrefabSystemComponent::CreateTemplateFromInstance - Attempted to create a prefab template from an instance without a source file path. Unable to proceed.
  85. error: Failed to create a Template associated with file path during CreatePrefab.
  86. error: Failed to create prefab
  87. */
  88. AZ_TEST_STOP_TRACE_SUPPRESSION(3);
  89. }
  90. TEST_F(PrefabScriptingTest, PrefabScripting_SaveToString)
  91. {
  92. AZ::ScriptContext sc;
  93. auto behaviorContext = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->GetBehaviorContext();
  94. sc.BindTo(behaviorContext);
  95. sc.Execute(R"LUA(
  96. my_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test")
  97. entities = vector_EntityId()
  98. entities:push_back(my_id)
  99. template_id = PrefabSystemScriptingBus.Broadcast.CreatePrefab(entities, "test.prefab")
  100. my_result = PrefabLoaderScriptingBus.Broadcast.SaveTemplateToString(template_id)
  101. if my_result:IsSuccess() then
  102. g_globalPrefabString = my_result:GetValue()
  103. end
  104. )LUA");
  105. auto prefabSystemComponentInterface = AZ::Interface<PrefabSystemComponentInterface>::Get();
  106. prefabSystemComponentInterface->RemoveAllTemplates();
  107. EXPECT_STRNE(g_globalPrefabString.c_str(), "");
  108. TemplateId templateFromString = AZ::Interface<PrefabLoaderInterface>::Get()->LoadTemplateFromString(g_globalPrefabString);
  109. EXPECT_NE(templateFromString, InvalidTemplateId);
  110. // Create another entity for comparison purposes
  111. AZ::EntityId entityId;
  112. AzToolsFramework::EntityUtilityBus::BroadcastResult(
  113. entityId, &AzToolsFramework::EntityUtilityBus::Events::CreateEditorReadyEntity, "test");
  114. AZ::Entity* testEntity = AZ::Interface<AZ::ComponentApplicationRequests>::Get()->FindEntity(entityId);
  115. // Instantiate the prefab we saved
  116. AZStd::unique_ptr<Instance> instance = prefabSystemComponentInterface->InstantiatePrefab(templateFromString);
  117. EXPECT_NE(instance, nullptr);
  118. AZStd::vector<const AZ::Entity*> loadedEntities;
  119. // Get the entities from the instance
  120. instance->GetConstEntities(
  121. [&loadedEntities](const AZ::Entity& entity)
  122. {
  123. loadedEntities.push_back(&entity);
  124. return true;
  125. });
  126. // Make sure the instance has an entity with the same number of components as our test entity
  127. EXPECT_EQ(loadedEntities.size(), 1);
  128. EXPECT_EQ(loadedEntities[0]->GetComponents().size(), testEntity->GetComponents().size());
  129. g_globalPrefabString.set_capacity(0); // Free all memory
  130. }
  131. }