/* * Copyright (c) Contributors to the Open 3D Engine Project. * For complete copyright and license terms please see the LICENSE at the root of this distribution. * * SPDX-License-Identifier: Apache-2.0 OR MIT * */ #include #include #include #include #include namespace UnitTest { TemplateId g_globalTemplateId = {}; AZStd::string g_globalPrefabString = ""; class PrefabScriptingTest : public PrefabTestFixture { void InitProperties() const { AZ::ComponentApplicationRequests* componentApplicationRequests = AZ::Interface::Get(); ASSERT_NE(componentApplicationRequests, nullptr); auto behaviorContext = componentApplicationRequests->GetBehaviorContext(); ASSERT_NE(behaviorContext, nullptr); behaviorContext->Property("g_globalTemplateId", BehaviorValueProperty(&g_globalTemplateId)); behaviorContext->Property("g_globalPrefabString", BehaviorValueProperty(&g_globalPrefabString)); g_globalTemplateId = TemplateId{}; g_globalPrefabString = AZStd::string{}; } void SetUpEditorFixtureImpl() override { InitProperties(); } void TearDownEditorFixtureImpl() override { g_globalPrefabString.set_capacity(0); // Free all memory } }; TEST_F(PrefabScriptingTest, PrefabScripting_CreatePrefab) { AZ::ScriptContext sc; auto behaviorContext = AZ::Interface::Get()->GetBehaviorContext(); sc.BindTo(behaviorContext); sc.Execute(R"LUA( my_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test") entities = vector_EntityId() entities:push_back(my_id) g_globalTemplateId = PrefabSystemScriptingBus.Broadcast.CreatePrefab(entities, "test.prefab") )LUA"); EXPECT_NE(g_globalTemplateId, TemplateId{}); auto prefabSystemComponentInterface = AZ::Interface::Get(); ASSERT_NE(prefabSystemComponentInterface, nullptr); TemplateReference templateRef = prefabSystemComponentInterface->FindTemplate(g_globalTemplateId); EXPECT_TRUE(templateRef); } TEST_F(PrefabScriptingTest, PrefabScripting_CreatePrefab_NoEntities) { AZ::ScriptContext sc; auto behaviorContext = AZ::Interface::Get()->GetBehaviorContext(); sc.BindTo(behaviorContext); sc.Execute(R"LUA( my_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test") entities = vector_EntityId() g_globalTemplateId = PrefabSystemScriptingBus.Broadcast.CreatePrefab(entities, "test.prefab") )LUA"); EXPECT_NE(g_globalTemplateId, TemplateId{}); auto prefabSystemComponentInterface = AZ::Interface::Get(); ASSERT_NE(prefabSystemComponentInterface, nullptr); TemplateReference templateRef = prefabSystemComponentInterface->FindTemplate(g_globalTemplateId); EXPECT_TRUE(templateRef); } TEST_F(PrefabScriptingTest, PrefabScripting_CreatePrefab_NoPath) { AZ::ScriptContext sc; auto behaviorContext = AZ::Interface::Get()->GetBehaviorContext(); sc.BindTo(behaviorContext); AZ_TEST_START_TRACE_SUPPRESSION; sc.Execute(R"LUA( my_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test") entities = vector_EntityId() template_id = PrefabSystemScriptingBus.Broadcast.CreatePrefab(entities, "") )LUA"); /* error: PrefabSystemComponent::CreateTemplateFromInstance - Attempted to create a prefab template from an instance without a source file path. Unable to proceed. error: Failed to create a Template associated with file path during CreatePrefab. error: Failed to create prefab */ AZ_TEST_STOP_TRACE_SUPPRESSION(3); } TEST_F(PrefabScriptingTest, PrefabScripting_SaveToString) { AZ::ScriptContext sc; auto behaviorContext = AZ::Interface::Get()->GetBehaviorContext(); sc.BindTo(behaviorContext); sc.Execute(R"LUA( my_id = EntityUtilityBus.Broadcast.CreateEditorReadyEntity("test") entities = vector_EntityId() entities:push_back(my_id) template_id = PrefabSystemScriptingBus.Broadcast.CreatePrefab(entities, "test.prefab") my_result = PrefabLoaderScriptingBus.Broadcast.SaveTemplateToString(template_id) if my_result:IsSuccess() then g_globalPrefabString = my_result:GetValue() end )LUA"); auto prefabSystemComponentInterface = AZ::Interface::Get(); prefabSystemComponentInterface->RemoveAllTemplates(); EXPECT_STRNE(g_globalPrefabString.c_str(), ""); TemplateId templateFromString = AZ::Interface::Get()->LoadTemplateFromString(g_globalPrefabString); EXPECT_NE(templateFromString, InvalidTemplateId); // Create another entity for comparison purposes AZ::EntityId entityId; AzToolsFramework::EntityUtilityBus::BroadcastResult( entityId, &AzToolsFramework::EntityUtilityBus::Events::CreateEditorReadyEntity, "test"); AZ::Entity* testEntity = AZ::Interface::Get()->FindEntity(entityId); // Instantiate the prefab we saved AZStd::unique_ptr instance = prefabSystemComponentInterface->InstantiatePrefab(templateFromString); EXPECT_NE(instance, nullptr); AZStd::vector loadedEntities; // Get the entities from the instance instance->GetConstEntities( [&loadedEntities](const AZ::Entity& entity) { loadedEntities.push_back(&entity); return true; }); // Make sure the instance has an entity with the same number of components as our test entity EXPECT_EQ(loadedEntities.size(), 1); EXPECT_EQ(loadedEntities[0]->GetComponents().size(), testEntity->GetComponents().size()); g_globalPrefabString.set_capacity(0); // Free all memory } }