Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the integration tests a bit more deterministic #9546

Merged
merged 8 commits into from
Dec 17, 2023
31 changes: 8 additions & 23 deletions source/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ this program. If not, see <https://www.gnu.org/licenses/>.
#include "System.h"
#include "SystemEntry.h"
#include "Test.h"
#include "TestContext.h"
#include "Visual.h"
#include "Weather.h"
#include "Wormhole.h"
Expand Down Expand Up @@ -549,22 +548,8 @@ void Engine::Step(bool isActive)
if(!wasActive)
activeCommands.Clear();
else
{
// Do a testing step if we got an active testContext from main.cpp.
// Main.cpp will transfer the context every step where it wants the
// engine to handle the testing.
if(testContext)
{
const Test *runningTest = testContext->CurrentTest();
if(runningTest)
runningTest->Step(*testContext, player, activeCommands);
}
ai.UpdateKeys(player, activeCommands);
}
}
// Clear the testContext every step. Main.cpp will provide the context before
// every step where it expects the Engine to handle testing.
testContext = nullptr;

wasActive = isActive;
Audio::Update(center);
Expand Down Expand Up @@ -1009,6 +994,14 @@ void Engine::Go()



// Give a command on behalf of the player, used for integration tests.
void Engine::GiveCommand(const Command &command)
{
activeCommands.Set(command);
}



// Pass the list of game events to MainPanel for handling by the player, and any
// UI element generation.
list<ShipEvent> &Engine::Events()
Expand Down Expand Up @@ -1189,14 +1182,6 @@ void Engine::Draw() const



// Set the given TestContext in the next step of the Engine.
void Engine::SetTestContext(TestContext &newTestContext)
{
testContext = &newTestContext;
}



// Select the object the player clicked on.
void Engine::Click(const Point &from, const Point &to, bool hasShift, bool hasControl)
{
Expand Down
10 changes: 3 additions & 7 deletions source/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class Projectile;
class Ship;
class ShipEvent;
class Sprite;
class TestContext;
class Visual;
class Weather;

Expand Down Expand Up @@ -80,16 +79,16 @@ class Engine {
// Begin the next step of calculations.
void Go();

// Give a command on behalf of the player, used for integration tests.
void GiveCommand(const Command &command);

// Get any special events that happened in this step.
// MainPanel::Step will clear this list.
std::list<ShipEvent> &Events();

// Draw a frame.
void Draw() const;

// Set the given TestContext in the next step of the Engine.
void SetTestContext(TestContext &newTestContext);

// Select the object the player clicked on.
void Click(const Point &from, const Point &to, bool hasShift, bool hasControl);
void RClick(const Point &point);
Expand Down Expand Up @@ -270,9 +269,6 @@ class Engine {
std::set<std::string> asteroidsScanned;
bool isAsteroidCatalogComplete = false;

// Input, Output and State handling for automated tests.
TestContext *testContext = nullptr;

Zoom zoom;
// Tracks the next zoom change so that objects aren't drawn at different zooms in a single frame.
Zoom nextZoom;
Expand Down
15 changes: 7 additions & 8 deletions source/MainPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ bool MainPanel::AllowsFastForward() const noexcept



Engine &MainPanel::GetEngine()
{
return engine;
}



// Only override the ones you need; the default action is to return false.
bool MainPanel::KeyDown(SDL_Keycode key, Uint16 mod, const Command &command, bool isNewPress)
{
Expand All @@ -212,14 +219,6 @@ bool MainPanel::KeyDown(SDL_Keycode key, Uint16 mod, const Command &command, boo



// Forward the given TestContext to the Engine under MainPanel.
void MainPanel::SetTestContext(TestContext &testContext)
{
engine.SetTestContext(testContext);
}



bool MainPanel::Click(int x, int y, int clicks)
{
// Don't respond to clicks if another panel is active.
Expand Down
6 changes: 3 additions & 3 deletions source/MainPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ class MainPanel : public Panel {
// The hail panel calls this when it closes.
void OnBribeCallback(const Government *bribed);

// Forward the given TestContext to the Engine under MainPanel.
virtual void SetTestContext(TestContext &testContext) override;

// The main panel allows fast-forward.
bool AllowsFastForward() const noexcept final;

// Get the underlying game engine used by the game.
Engine &GetEngine();


protected:
// Only override the ones you need; the default action is to return false.
Expand Down
7 changes: 0 additions & 7 deletions source/Panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ bool Panel::ZoneClick(const Point &point)



// Forward the given TestContext to the Engine under MainPanel.
void Panel::SetTestContext(TestContext &testContext)
{
}



// Panels will by default not allow fast-forward. The ones that do allow
// it will override this (virtual) function and return true.
bool Panel::AllowsFastForward() const noexcept
Expand Down
3 changes: 0 additions & 3 deletions source/Panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ class Panel {
// so, apply that zone's action and return true.
bool ZoneClick(const Point &point);

// Forward the given TestContext to the Engine under MainPanel.
virtual void SetTestContext(TestContext &testContext);

// Is fast-forward allowed to be on when this panel is on top of the GUI stack?
virtual bool AllowsFastForward() const noexcept;

Expand Down
14 changes: 0 additions & 14 deletions source/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ namespace {
{Test::TestStep::Type::INPUT, "input"},
{Test::TestStep::Type::LABEL, "label"},
{Test::TestStep::Type::NAVIGATE, "navigate"},
{Test::TestStep::Type::WATCHDOG, "watchdog"},
};

template<class K, class... Args>
Expand Down Expand Up @@ -286,9 +285,6 @@ void Test::LoadSequence(const DataNode &node)
}
}
break;
case TestStep::Type::WATCHDOG:
step.watchdog = child.Size() >= 2 ? child.Value(1) : 0;
break;
default:
child.PrintTrace("Error: unknown step type in test");
status = Status::BROKEN;
Expand Down Expand Up @@ -433,12 +429,6 @@ void Test::Step(TestContext &context, PlayerInfo &player, Command &commandToGive

while(context.callstack.back().step < steps.size() && !continueGameLoop)
{
// Fail if we encounter a watchdog timeout
if(context.watchdog == 1)
Fail(context, player, "watchdog timeout");
else if(context.watchdog > 1)
--(context.watchdog);

const TestStep &stepToRun = steps[context.callstack.back().step];
switch(stepToRun.stepType)
{
Expand Down Expand Up @@ -513,10 +503,6 @@ void Test::Step(TestContext &context, PlayerInfo &player, Command &commandToGive
player.SetTravelDestination(stepToRun.travelDestination);
++(context.callstack.back().step);
break;
case TestStep::Type::WATCHDOG:
context.watchdog = stepToRun.watchdog;
++(context.callstack.back().step);
break;
default:
Fail(context, player, "Unknown step type");
break;
Expand Down
5 changes: 0 additions & 5 deletions source/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ class Test {
LABEL,
// Instructs the game to set navigation / travel plan to a target system
NAVIGATE,
// Sets the watchdog timer. No value or zero disables the watchdog. Non-zero gives
// a watchdog in number of frames/steps.
WATCHDOG,
};


Expand All @@ -98,8 +95,6 @@ class Test {
std::string jumpOnTrueTarget;
std::string jumpOnFalseTarget;

unsigned int watchdog = 0;

// Input variables.
Command command;
std::set<std::string> inputKeys;
Expand Down
2 changes: 0 additions & 2 deletions source/TestContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ friend class Test;
// Reference to the currently running test and test-step within the test.
std::vector<ActiveTestStep> callstack;

// Teststep to run.
unsigned int watchdog = 0;
std::set<ActiveTestStep> branchesSinceGameStep;
};

Expand Down
Loading