Files
o3de/Gems/Prefab/PrefabBuilder/PrefabGroup/PrefabBehaviorTests.cpp
T
amzn-mike 71bb200e0a [LYN-7064] Hot Loading of Procedural Prefabs (#4923)
* Prefab hotloading - load and maintain asset references

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Working hotloading

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Clean up code, add comments

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Add missing includes

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Add event for template removal.

Clear ProceduralPrefabSystemComponent lookup when templates are removed.

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Add unit test.

Fix missing include, make sure source name is set

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Fix missing include

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Fix missing include

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Update comments to make ebus event behavior more clear

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Fix missing include

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Fix missing includes

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Fix missing include

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Fix temp directory usage which doesn't work on linux

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Fix temp directory usage which doesn't work on linux

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Use AZ::IO::Path for comparison.  Print paths on failure

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>

* Fix unit test - register asset with catalog so it can be looked up later

Signed-off-by: amzn-mike <80125227+amzn-mike@users.noreply.github.com>
2021-12-13 12:44:41 -08:00

168 lines
6.7 KiB
C++

/*
* 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 <PrefabBuilderTests.h>
#include <PrefabGroup/IPrefabGroup.h>
#include <PrefabGroup/PrefabGroup.h>
#include <PrefabGroup/PrefabGroupBehavior.h>
#include <AzTest/Utils.h>
#include <Tests/AssetSystemMocks.h>
#include <AzCore/Serialization/Json/JsonSystemComponent.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <AzCore/std/smart_ptr/make_shared.h>
#include <AzToolsFramework/Asset/AssetSystemComponent.h>
#include <AzToolsFramework/Prefab/Procedural/ProceduralPrefabAsset.h>
#include <SceneAPI/SceneCore/Containers/Scene.h>
#include <SceneAPI/SceneCore/Events/CallProcessorBus.h>
#include <SceneAPI/SceneCore/Events/ExportEventContext.h>
#include <SceneAPI/SceneCore/Events/ExportProductList.h>
#include <SceneAPI/SceneCore/SceneCoreStandaloneAllocator.h>
#include <PrefabGroup/PrefabBehaviorTests.inl>
namespace UnitTest
{
class PrefabBehaviorTests
: public PrefabBuilderTests
{
public:
static void SetUpTestCase()
{
// Allocator needed by SceneCore
if (!AZ::AllocatorInstance<AZ::SystemAllocator>().IsReady())
{
AZ::AllocatorInstance<AZ::SystemAllocator>().Create();
}
AZ::SceneAPI::SceneCoreStandaloneAllocator::Initialize(AZ::Environment::GetInstance());
}
static void TearDownTestCase()
{
AZ::SceneAPI::SceneCoreStandaloneAllocator::TearDown();
AZ::AllocatorInstance<AZ::SystemAllocator>().Destroy();
}
void SetUp() override
{
PrefabBuilderTests::SetUp();
m_prefabGroupBehavior = AZStd::make_unique<AZ::SceneAPI::Behaviors::PrefabGroupBehavior>();
m_prefabGroupBehavior->Activate();
// Mocking the asset system replacing the AssetSystem::AssetSystemComponent
AZ::Entity* systemEntity = m_app.FindEntity(AZ::SystemEntityId);
systemEntity->FindComponent<AzToolsFramework::AssetSystem::AssetSystemComponent>()->Deactivate();
using namespace testing;
ON_CALL(m_assetSystemRequestMock, GetSourceInfoBySourcePath(_, _, _)).WillByDefault([](auto* path, auto& info, auto&)
{
return PrefabBehaviorTests::OnGetSourceInfoBySourcePath(path, info);
});
m_assetSystemRequestMock.BusConnect();
}
void TearDown() override
{
m_assetSystemRequestMock.BusDisconnect();
m_prefabGroupBehavior->Deactivate();
m_prefabGroupBehavior.reset();
PrefabBuilderTests::TearDown();
}
static bool OnGetSourceInfoBySourcePath(AZStd::string_view sourcePath, AZ::Data::AssetInfo& assetInfo)
{
if (sourcePath == AZStd::string_view("mock"))
{
assetInfo.m_assetId = AZ::Uuid::CreateRandom();
assetInfo.m_assetType = azrtti_typeid<AZ::Prefab::ProceduralPrefabAsset>();
assetInfo.m_relativePath = "mock/path";
assetInfo.m_sizeBytes = 0;
}
return true;
}
struct TestPreExportEventContext
{
TestPreExportEventContext()
: m_scene("test_context")
{
using namespace AZ::SceneAPI::Events;
m_preExportEventContext = AZStd::make_unique<PreExportEventContext>(m_productList, m_outputDirectory, m_scene, "mock");
}
void SetOutputDirectory(AZStd::string outputDirectory)
{
using namespace AZ::SceneAPI::Events;
m_outputDirectory = AZStd::move(outputDirectory);
m_preExportEventContext = AZStd::make_unique<PreExportEventContext>(m_productList, m_outputDirectory, m_scene, "mock");
}
AZStd::unique_ptr<AZ::SceneAPI::Events::PreExportEventContext> m_preExportEventContext;
AZ::SceneAPI::Events::ExportProductList m_productList;
AZStd::string m_outputDirectory;
AZ::SceneAPI::Containers::Scene m_scene;
};
AZStd::unique_ptr<AZ::SceneAPI::Behaviors::PrefabGroupBehavior> m_prefabGroupBehavior;
testing::NiceMock<UnitTests::MockAssetSystemRequest> m_assetSystemRequestMock;
};
TEST_F(PrefabBehaviorTests, PrefabBehavior_EmptyContextIgnored_Works)
{
auto context = TestPreExportEventContext{};
auto result = AZ::SceneAPI::Events::ProcessingResult::Failure;
AZ::SceneAPI::Events::CallProcessorBus::BroadcastResult(
result,
&AZ::SceneAPI::Events::CallProcessorBus::Events::Process,
context.m_preExportEventContext.get());
EXPECT_EQ(result, AZ::SceneAPI::Events::ProcessingResult::Ignored);
}
TEST_F(PrefabBehaviorTests, PrefabBehavior_SimplePrefab_Works)
{
auto context = TestPreExportEventContext{};
// check for the file at <temp_directory>/mock/fake_prefab.procprefab
AZ::Test::ScopedAutoTempDirectory tempDir;
context.SetOutputDirectory(tempDir.GetDirectory());
auto jsonOutcome = AZ::JsonSerializationUtils::ReadJsonString(Data::jsonPrefab);
ASSERT_TRUE(jsonOutcome);
// Register the asset to generate an AssetId in the catalog
AZ::Data::AssetId assetId;
AZ::Data::AssetCatalogRequestBus::BroadcastResult(
assetId, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetIdByPath, "fake_prefab.procprefab",
azrtti_typeid<AZ::Prefab::ProceduralPrefabAsset>(), true);
auto prefabGroup = AZStd::make_shared<AZ::SceneAPI::SceneData::PrefabGroup>();
prefabGroup.get()->SetId(AZ::Uuid::CreateRandom());
prefabGroup.get()->SetName("fake_prefab");
prefabGroup.get()->SetPrefabDom(AZStd::move(jsonOutcome.GetValue()));
context.m_scene.GetManifest().AddEntry(prefabGroup);
context.m_scene.SetSource("mock", AZ::Uuid::CreateRandom());
auto result = AZ::SceneAPI::Events::ProcessingResult::Failure;
AZ::SceneAPI::Events::CallProcessorBus::BroadcastResult(
result,
&AZ::SceneAPI::Events::CallProcessorBus::Events::Process,
context.m_preExportEventContext.get());
EXPECT_EQ(result, AZ::SceneAPI::Events::ProcessingResult::Success);
AZStd::string pathStr;
AzFramework::StringFunc::Path::ConstructFull(tempDir.GetDirectory(), "mock/fake_prefab.procprefab", pathStr, true);
if (!AZ::IO::SystemFile::Exists(pathStr.c_str()))
{
AZ_Warning("testing", false, "The product asset (%s) is missing", pathStr.c_str());
}
}
}