From afde9a47e7b6bcd525d463479af1d9da6b5a391d Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Fri, 15 Sep 2023 19:55:39 +1000 Subject: [PATCH] Add some origin shift tests. --- .../Tests/CesiumOriginShiftComponent.spec.cpp | 136 ++++++++++++++++++ .../Public/CesiumOriginShiftComponent.h | 2 +- 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 Source/CesiumRuntime/Private/Tests/CesiumOriginShiftComponent.spec.cpp diff --git a/Source/CesiumRuntime/Private/Tests/CesiumOriginShiftComponent.spec.cpp b/Source/CesiumRuntime/Private/Tests/CesiumOriginShiftComponent.spec.cpp new file mode 100644 index 000000000..e7f73d759 --- /dev/null +++ b/Source/CesiumRuntime/Private/Tests/CesiumOriginShiftComponent.spec.cpp @@ -0,0 +1,136 @@ +#if WITH_EDITOR + +#include "CesiumGeoreference.h" +#include "CesiumGlobeAnchorComponent.h" +#include "CesiumOriginShiftComponent.h" +#include "CesiumSubLevelComponent.h" +#include "CesiumTestHelpers.h" +#include "Editor.h" +#include "Engine/StaticMeshActor.h" +#include "Engine/World.h" +#include "EngineUtils.h" +#include "LevelInstance/LevelInstanceActor.h" +#include "Misc/AutomationTest.h" +#include "Tests/AutomationEditorCommon.h" + +BEGIN_DEFINE_SPEC( + FCesiumOriginShiftComponentSpec, + "Cesium.OriginShiftComponent", + EAutomationTestFlags::ApplicationContextMask | + EAutomationTestFlags::ProductFilter) + +TObjectPtr pWorld; +TObjectPtr pGeoreference; +TObjectPtr pOriginShiftActor; +TObjectPtr pOriginShiftComponent; +FDelegateHandle subscriptionPostPIEStarted; + +END_DEFINE_SPEC(FCesiumOriginShiftComponentSpec) + +using namespace CesiumTestHelpers; + +void FCesiumOriginShiftComponentSpec::Define() { + BeforeEach([this]() { + if (IsValid(pWorld)) { + // Only run the below once in order to save time loading/unloading + // levels for every little test. + return; + } + + pWorld = FAutomationEditorCommonUtils::CreateNewMap(); + + pOriginShiftActor = pWorld->SpawnActor(); + pOriginShiftActor->SetMobility(EComponentMobility::Movable); + trackForPlay(pOriginShiftActor); + + pOriginShiftComponent = Cast( + pOriginShiftActor->AddComponentByClass( + UCesiumOriginShiftComponent::StaticClass(), + false, + FTransform::Identity, + false)); + trackForPlay(pOriginShiftComponent); + + pGeoreference = nullptr; + for (TActorIterator it(pWorld); it; ++it) { + pGeoreference = *it; + } + + trackForPlay(pGeoreference); + }); + + AfterEach([this]() {}); + + It("automatically adds a globe anchor to go with the origin shift", [this]() { + UCesiumGlobeAnchorComponent* pGlobeAnchor = + pOriginShiftActor->FindComponentByClass(); + TestNotNull("pGlobeAnchor", pGlobeAnchor); + }); + + Describe( + "does not shift origin when in between sub-levels when mode is SwitchSubLevelsOnly", + [this]() { + LatentBeforeEach( + EAsyncExecution::TaskGraphMainThread, + [this](const FDoneDelegate& done) { + subscriptionPostPIEStarted = + FEditorDelegates::PostPIEStarted.AddLambda( + [done](bool isSimulating) { done.Execute(); }); + FRequestPlaySessionParams params{}; + GEditor->RequestPlaySession(params); + }); + BeforeEach(EAsyncExecution::TaskGraphMainThread, [this]() { + FEditorDelegates::PostPIEStarted.Remove(subscriptionPostPIEStarted); + + findInPlay(pOriginShiftActor) + ->SetActorLocation(FVector(10000.0, 20000.0, 300.0)); + }); + It("", [this]() { + TestEqual( + "location", + findInPlay(pOriginShiftActor)->GetActorLocation(), + FVector(10000.0, 20000.0, 300.0)); + }); + AfterEach(EAsyncExecution::TaskGraphMainThread, [this]() { + GEditor->RequestEndPlayMap(); + }); + }); + + Describe( + "shifts origin by changing georeference when mode is ChangeCesiumGeoreference", + [this]() { + LatentBeforeEach( + EAsyncExecution::TaskGraphMainThread, + [this](const FDoneDelegate& done) { + subscriptionPostPIEStarted = + FEditorDelegates::PostPIEStarted.AddLambda( + [done](bool isSimulating) { done.Execute(); }); + FRequestPlaySessionParams params{}; + GEditor->RequestPlaySession(params); + }); + BeforeEach(EAsyncExecution::TaskGraphMainThread, [this]() { + FEditorDelegates::PostPIEStarted.Remove(subscriptionPostPIEStarted); + + findInPlay(pOriginShiftComponent) + ->SetMode(ECesiumOriginShiftMode::ChangeCesiumGeoreference); + + // Move to roughtly the other side of the Earth. + FVector location = FVector( + 0.0, + 0.0, + -CesiumGeospatial::Ellipsoid::WGS84.getMaximumRadius()); + findInPlay(pOriginShiftActor)->SetActorLocation(location); + }); + It("", [this]() { + TestEqual( + "location", + findInPlay(pOriginShiftActor)->GetActorLocation(), + FVector::Zero()); + }); + AfterEach(EAsyncExecution::TaskGraphMainThread, [this]() { + GEditor->RequestEndPlayMap(); + }); + }); +} + +#endif // #if WITH_EDITOR diff --git a/Source/CesiumRuntime/Public/CesiumOriginShiftComponent.h b/Source/CesiumRuntime/Public/CesiumOriginShiftComponent.h index ca4408781..16cd875e2 100644 --- a/Source/CesiumRuntime/Public/CesiumOriginShiftComponent.h +++ b/Source/CesiumRuntime/Public/CesiumOriginShiftComponent.h @@ -36,7 +36,7 @@ enum class ECesiumOriginShiftMode : uint8 { * CesiumGlobeAnchorComponent will appear to move whenever the origin changes. * * When using this mode, all Cesium3DTileset instances as well as any Actors - * with a CesiumGlobeAnchorComponent need to be marked Moveable, because these + * with a CesiumGlobeAnchorComponent need to be marked Movable, because these * objects _will_ be moved when the origin is shifted. */ ChangeCesiumGeoreference,