Files
o3de/Gems/SceneProcessing/Code/Tests/SceneBuilder/SceneBuilderPhasesTests.cpp
T
lumberyard-employee-dm 627012840d Update how Project Filepaths are calculated when not supplied via command line (#5194)
* Fixed the return value of the ConvertToAbsolutePath function

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Added the generated cmake_dependencies.*.setreg files to engine.pak (#5073)

* Copied the generated cmake_dependencies.*.setreg file to the Cache
directory

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removed the platform name from the bootstrap.game.*.setreg

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Update how the project related file paths are determined when not
supplied.

The project-path determination now goes back to only detecting a "project.json" file.
It no longer attempts to detect a "Cache" directory

The project-cache-path determination now in addition to checking the
project_cache_path key searches for a "Cache" directory.

The project-path defaults to executable folder if it cannot be detected.

The copying of generated executable folder Registry directory contents
to the product cache is now removed after the archive step.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the invocation of the AssetProcessor in Jenkins to supply an
absolute path to the project.

The project-path is no longer treated as relative to the engine root,
but instead relative to the current working directory at application
startup.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Added constant for the storing the name of Cache directory

Fixed typos and grammatical errors in the SettingsRegistryMergeUtils.cpp

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated UnitTest prepend the EngineRoot path to "AutomatedTesting" when
setting the project path.

This is needed now that the project-path isn't treated relative to the
EngineRoot if it is not absolute.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Fix AssetSeedManagerTest and PlatformAddressedAssetCatalogManagerTest

Instead of trying to used the AutomatedTesting directory as the project root, the temp directory created during the test is used as the project root.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Moved the setting of the project cache root folder and project
asset platform root folder into the `if (!projectCachePath.empty())`
block

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Removing the scan up logic for the "Cache" directory.

This is no longer needed to locate the project cache path in a Project Game Release Layout.

Because the project path defaults to the executable directory if, it is not found, the Cache directory will be set to the "Cache" directory within the executable directory.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2021-11-04 11:02:18 -05:00

247 lines
11 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 <gmock/gmock.h>
#include <AzCore/Component/Component.h>
#include <AzCore/Component/ComponentApplication.h>
#include <AzCore/Module/Environment.h>
#include <AzCore/RTTI/ReflectContext.h>
#include <AzCore/Settings/SettingsRegistryMergeUtils.h>
#include <AzCore/UnitTest/TestTypes.h>
#include <AzCore/UserSettings/UserSettingsComponent.h>
#include <AzCore/std/smart_ptr/make_shared.h>
#include <Application/ToolsApplication.h>
#include <AssetBuilderSDK/AssetBuilderSDK.h>
#include <SceneAPI/SceneCore/Components/ExportingComponent.h>
#include <SceneAPI/SceneCore/Components/GenerationComponent.h>
#include <SceneAPI/SceneCore/Components/LoadingComponent.h>
#include <SceneAPI/SceneCore/Components/Utilities/EntityConstructor.h>
#include <SceneAPI/SceneCore/Containers/Scene.h>
#include <SceneAPI/SceneCore/Events/ExportEventContext.h>
#include <SceneAPI/SceneCore/Events/GenerateEventContext.h>
#include <SceneAPI/SceneCore/Events/ImportEventContext.h>
#include <SceneAPI/SceneCore/Events/ProcessingResult.h>
#include <SceneAPI/SceneCore/Events/SceneSerializationBus.h>
#include <SceneAPI/SceneCore/Mocks/DataTypes/Groups/MockIGroup.h>
#include <SceneBuilder/SceneBuilderWorker.h>
// This component descriptor allows for a component to be created beforehand,
// and is then returned when an instance of that component is requested.
// This allows for a component to be pre-configured, via EXPECT calls.
template<class ComponentType>
class ComponentSingleton
: public AZ::ComponentDescriptorDefault<ComponentType>
{
public:
AZ::Component* CreateComponent() override { return m_component; }
void SetComponent(ComponentType* c) { m_component = c; }
private:
ComponentType* m_component = nullptr;
};
class TestLoadingComponent
: public AZ::SceneAPI::SceneCore::LoadingComponent
{
public:
AZ_RTTI(TestLoadingComponent, "{19B714CA-6AEF-414D-A91C-54E73DF69625}", AZ::SceneAPI::SceneCore::LoadingComponent, AZ::Component)
using DescriptorType = ComponentSingleton<TestLoadingComponent>;
AZ_COMPONENT_BASE(TestLoadingComponent, "{19B714CA-6AEF-414D-A91C-54E73DF69625}", AZ::SceneAPI::SceneCore::LoadingComponent)
static void Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* sc = azrtti_cast<AZ::SerializeContext*>(context); sc)
{
sc->Class<TestLoadingComponent, AZ::SceneAPI::SceneCore::LoadingComponent>()->Version(1);
}
}
TestLoadingComponent() { BindToCall(&TestLoadingComponent::Load); }
MOCK_CONST_METHOD1(Load, AZ::SceneAPI::Events::ProcessingResult(AZ::SceneAPI::Events::ImportEventContext& context));
};
class TestGenerationComponent
: public AZ::SceneAPI::SceneCore::GenerationComponent
{
public:
AZ_RTTI(TestGenerationComponent, "{3350BD61-2EB1-4F77-B1BD-D108795015EE}", AZ::SceneAPI::SceneCore::GenerationComponent, AZ::Component)
using DescriptorType = ComponentSingleton<TestGenerationComponent>;
AZ_COMPONENT_BASE(TestGenerationComponent, "{3350BD61-2EB1-4F77-B1BD-D108795015EE}", AZ::SceneAPI::SceneCore::GenerationComponent)
static void Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* sc = azrtti_cast<AZ::SerializeContext*>(context); sc)
{
sc->Class<TestGenerationComponent, AZ::SceneAPI::SceneCore::GenerationComponent>()->Version(1);
}
}
TestGenerationComponent() { BindToCall(&TestGenerationComponent::Generate); }
MOCK_CONST_METHOD1(Generate, AZ::SceneAPI::Events::ProcessingResult(AZ::SceneAPI::Events::GenerateEventContext& context));
};
class TestExportingComponent
: public AZ::SceneAPI::SceneCore::ExportingComponent
{
public:
AZ_RTTI(TestExportingComponent, "{EADA08AD-2068-4607-AA3D-8B17C59696D5}", AZ::SceneAPI::SceneCore::ExportingComponent, AZ::Component)
using DescriptorType = ComponentSingleton<TestExportingComponent>;
AZ_COMPONENT_BASE(TestExportingComponent, "{EADA08AD-2068-4607-AA3D-8B17C59696D5}", AZ::SceneAPI::SceneCore::ExportingComponent)
static void Reflect(AZ::ReflectContext* context)
{
if (AZ::SerializeContext* sc = azrtti_cast<AZ::SerializeContext*>(context); sc)
{
sc->Class<TestExportingComponent, AZ::SceneAPI::SceneCore::ExportingComponent>()->Version(1);
}
}
TestExportingComponent() { BindToCall(&TestExportingComponent::Export); }
MOCK_CONST_METHOD1(Export, AZ::SceneAPI::Events::ProcessingResult(const AZ::SceneAPI::Events::ExportEventContext& context));
};
// This scene loader handler mocks the LoadScene method, so that the user can
// control the Scene that is generated in the test. But the default handler
// used in production is also responsible for generating the Import events.
// This test is designed to test the order of the import phases, so a method is
// provided to generate those events.
class TestSceneSerializationHandler
: public AZ::SceneAPI::Events::SceneSerializationBus::Handler
{
public:
TestSceneSerializationHandler() { BusConnect(); }
~TestSceneSerializationHandler() override { BusDisconnect(); }
MOCK_METHOD2(LoadScene, AZStd::shared_ptr<AZ::SceneAPI::Containers::Scene>(const AZStd::string& sceneFilePath, AZ::Uuid sceneSourceGuid));
void GenerateImportEvents(const AZStd::string& assetFilePath, [[maybe_unused]] const AZ::Uuid& sourceGuid)
{
auto loaders = AZ::SceneAPI::SceneCore::EntityConstructor::BuildEntity("Scene Loading", azrtti_typeid<AZ::SceneAPI::SceneCore::LoadingComponent>());
auto scene = AZStd::make_shared<AZ::SceneAPI::Containers::Scene>("import scene");
AZ::SceneAPI::Events::ProcessingResultCombiner contextResult;
contextResult += AZ::SceneAPI::Events::Process<AZ::SceneAPI::Events::PreImportEventContext>(assetFilePath);
contextResult += AZ::SceneAPI::Events::Process<AZ::SceneAPI::Events::ImportEventContext>(assetFilePath, *scene);
contextResult += AZ::SceneAPI::Events::Process<AZ::SceneAPI::Events::PostImportEventContext>(*scene);
}
};
// This fixture attaches the SceneCore and SceneData libraries, and attaches
// the AZ::Environment to them
class SceneBuilderPhasesFixture
: public UnitTest::ScopedAllocatorSetupFixture
{
public:
void SetUp() override
{
AZ::SettingsRegistryInterface* registry = AZ::SettingsRegistry::Get();
auto projectPathKey =
AZ::SettingsRegistryInterface::FixedValueString(AZ::SettingsRegistryMergeUtils::BootstrapSettingsRootKey) + "/project_path";
AZ::IO::FixedMaxPath enginePath;
registry->Get(enginePath.Native(), AZ::SettingsRegistryMergeUtils::FilePathKey_EngineRootFolder);
registry->Set(projectPathKey, (enginePath / "AutomatedTesting").Native());
AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_AddRuntimeFilePaths(*registry);
m_app.Start(AZ::ComponentApplication::Descriptor());
// Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
// shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
// in the unit tests.
AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
m_app.RegisterComponentDescriptor(TestLoadingComponent::CreateDescriptor());
m_app.RegisterComponentDescriptor(TestGenerationComponent::CreateDescriptor());
m_app.RegisterComponentDescriptor(TestExportingComponent::CreateDescriptor());
m_sceneCoreModule = LoadSceneModule("SceneCore");
m_sceneDataModule = LoadSceneModule("SceneData");
}
void TearDown() override
{
m_app.Stop();
UnloadModule(m_sceneCoreModule);
UnloadModule(m_sceneDataModule);
}
private:
static AZStd::unique_ptr<AZ::DynamicModuleHandle> LoadSceneModule(const char* name)
{
auto module = AZ::DynamicModuleHandle::Create(name);
if (!module)
{
return {};
}
module->Load(false);
if (auto init = module->GetFunction<AZ::InitializeDynamicModuleFunction>(AZ::InitializeDynamicModuleFunctionName); init)
{
AZStd::invoke(init, AZ::Environment::GetInstance());
}
return module;
}
static void UnloadModule(AZStd::unique_ptr<AZ::DynamicModuleHandle>& module)
{
if (!module)
{
return;
}
if (auto uninit = module->GetFunction<AZ::UninitializeDynamicModuleFunction>(AZ::UninitializeDynamicModuleFunctionName); uninit)
{
AZStd::invoke(uninit);
}
module = nullptr;
}
AzToolsFramework::ToolsApplication m_app;
AZStd::unique_ptr<AZ::DynamicModuleHandle> m_sceneCoreModule;
AZStd::unique_ptr<AZ::DynamicModuleHandle> m_sceneDataModule;
};
TEST_F(SceneBuilderPhasesFixture, TestProcessingPhases)
{
auto scene = AZStd::make_shared<AZ::SceneAPI::Containers::Scene>("testScene");
scene->GetManifest().AddEntry(AZStd::make_shared<AZ::SceneAPI::DataTypes::MockIGroup>());
scene->SetManifestFilename("testScene.manifest");
TestSceneSerializationHandler sceneLoadingHandler;
EXPECT_CALL(sceneLoadingHandler, LoadScene(testing::_, testing::_))
.WillOnce(testing::DoAll(
testing::Invoke(&sceneLoadingHandler, &TestSceneSerializationHandler::GenerateImportEvents),
testing::Return(scene)
));
auto* loadingComponent = aznew TestLoadingComponent();
auto* generationComponent = aznew TestGenerationComponent();
auto* exportingComponent = aznew TestExportingComponent();
static_cast<ComponentSingleton<TestLoadingComponent>*>(TestLoadingComponent::CreateDescriptor())->SetComponent(loadingComponent);
static_cast<ComponentSingleton<TestGenerationComponent>*>(TestGenerationComponent::CreateDescriptor())->SetComponent(generationComponent);
static_cast<ComponentSingleton<TestExportingComponent>*>(TestExportingComponent::CreateDescriptor())->SetComponent(exportingComponent);
{
// Set up the order in which the event handlers should be called
::testing::InSequence sequence;
using ::testing::_;
EXPECT_CALL(*loadingComponent, Load(_))
.WillOnce(testing::Return(AZ::SceneAPI::Events::ProcessingResult::Success));
EXPECT_CALL(*generationComponent, Generate(_))
.WillOnce(testing::Return(AZ::SceneAPI::Events::ProcessingResult::Success));
EXPECT_CALL(*exportingComponent, Export(_))
.WillOnce(testing::Return(AZ::SceneAPI::Events::ProcessingResult::Success));
}
SceneBuilder::SceneBuilderWorker worker;
AssetBuilderSDK::ProcessJobResponse response;
worker.ProcessJob({}, response);
// The assertions set up before with the EXPECT_CALL calls are evaluated
// when the mock objects go out of scope
}