Skip to content

Commit

Permalink
Make sure the Editor ticks during tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Sep 23, 2024
1 parent 460a559 commit 13c51b9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
2 changes: 0 additions & 2 deletions Source/CesiumRuntime/Private/Cesium3DTileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2062,8 +2062,6 @@ void ACesium3DTileset::Tick(float DeltaTime) {
return;
}

UE_LOG(LogCesium, Warning, TEXT("!!! TICKING %s"), *this->GetName());

if (!this->_pTileset) {
LoadTileset();

Expand Down
35 changes: 35 additions & 0 deletions Source/CesiumRuntime/Private/Tests/CesiumTestHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "CesiumGeoreference.h"
#include "Engine/Engine.h"

#if WITH_EDITOR
#include "Editor/EditorPerformanceSettings.h"
#endif

namespace CesiumTestHelpers {

UWorld* getGlobalWorldContext() {
Expand Down Expand Up @@ -63,6 +67,37 @@ FName getUniqueTag(UActorComponent* pComponent) {
return FName(FString::Printf(TEXT("%lld"), pComponent));
}

#if WITH_EDITOR
namespace {
size_t timesAllowingEditorTick = 0;
bool originalEditorTickState = true;
} // namespace
#endif

void pushAllowTickInEditor() {
#if WITH_EDITOR
if (timesAllowingEditorTick == 0) {
UEditorPerformanceSettings* pSettings =
GetMutableDefault<UEditorPerformanceSettings>();
originalEditorTickState = pSettings->bThrottleCPUWhenNotForeground;
pSettings->bThrottleCPUWhenNotForeground = false;
}

++timesAllowingEditorTick;
#endif
}

void popAllowTickInEditor() {
#if WITH_EDITOR
--timesAllowingEditorTick;
if (timesAllowingEditorTick == 0) {
UEditorPerformanceSettings* pSettings =
GetMutableDefault<UEditorPerformanceSettings>();
pSettings->bThrottleCPUWhenNotForeground = originalEditorTickState;
}
#endif
}

void trackForPlay(AActor* pEditorActor) {
pEditorActor->Tags.Add(getUniqueTag(pEditorActor));
}
Expand Down
14 changes: 14 additions & 0 deletions Source/CesiumRuntime/Private/Tests/CesiumTestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ FName getUniqueTag(AActor* pActor);
/// <returns>The unique tag.</returns>
FName getUniqueTag(UActorComponent* pComponent);

/// <summary>
/// By default, UE 5.3+ don't tick in a headless Editor, which is often used to
/// run tests. Call this at the start of a test that requires ticking to
/// override this default. Call popAllowTickInEditor after the test to restore
/// the default.
/// </summary>
void pushAllowTickInEditor();

/// <summary>
/// Call this after a test that needs working ticking to restore the default
/// state.
/// </summary>
void popAllowTickInEditor();

#if WITH_EDITOR

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ END_DEFINE_SPEC(FSampleHeightMostDetailedSpec)
void FSampleHeightMostDetailedSpec::Define() {
Describe("Cesium World Terrain", [this]() {
BeforeEach([this]() {
CesiumTestHelpers::pushAllowTickInEditor();

UWorld* pWorld = CesiumTestHelpers::getGlobalWorldContext();
pTileset = pWorld->SpawnActor<ACesium3DTileset>();
pTileset->SetIonAssetID(1);
Expand All @@ -36,6 +38,8 @@ void FSampleHeightMostDetailedSpec::Define() {

AfterEach(EAsyncExecution::TaskGraphMainThread, [this]() {
pTileset->Destroy();

CesiumTestHelpers::popAllowTickInEditor();
});

LatentIt(
Expand All @@ -59,13 +63,10 @@ void FSampleHeightMostDetailedSpec::Define() {
"works with a single position",
EAsyncExecution::TaskGraphMainThread,
[this](const FDoneDelegate& done) {
CesiumAsync::Promise<void> foo =
getAsyncSystem().createPromise<void>();

pTileset->SampleHeightMostDetailed(
{FVector(-105.1, 40.1, 1.0)},
FCesiumSampleHeightMostDetailedCallback::CreateLambda(
[this, done, foo](
[this, done](
ACesium3DTileset* pTileset,
const TArray<FCesiumSampleHeightResult>& result,
const TArray<FString>& warnings) {
Expand All @@ -88,17 +89,8 @@ void FSampleHeightMostDetailedSpec::Define() {
result[0].LongitudeLatitudeHeight.Z,
1.0,
1.0));
foo.resolve();
// done.ExecuteIfBound();
done.ExecuteIfBound();
}));

CesiumTestHelpers::waitFor(
done,
CesiumTestHelpers::getGlobalWorldContext(),
30.0f,
[future = foo.getFuture().share()]() {
return future.isReady();
});
});

LatentIt(
Expand Down

0 comments on commit 13c51b9

Please sign in to comment.