OpenRCT2/test/tests/ReplayTests.cpp

119 lines
3.3 KiB
C++
Raw Normal View History

2018-12-11 09:56:51 +01:00
/*****************************************************************************
* Copyright (c) 2014-2023 OpenRCT2 developers
2018-12-11 09:56:51 +01:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "TestData.h"
#include <gtest/gtest.h>
#include <openrct2/Context.h>
#include <openrct2/Game.h>
#include <openrct2/GameState.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/ReplayManager.h>
#include <openrct2/audio/AudioContext.h>
#include <openrct2/core/File.h>
#include <openrct2/core/FileScanner.h>
#include <openrct2/core/Path.hpp>
#include <openrct2/core/String.hpp>
#include <openrct2/platform/Platform.h>
2018-12-11 09:56:51 +01:00
#include <openrct2/ride/Ride.h>
#include <string>
using namespace OpenRCT2;
2018-12-13 09:59:03 +01:00
struct ReplayTestData
2018-12-11 09:56:51 +01:00
{
2018-12-13 09:59:03 +01:00
std::string name;
std::string filePath;
};
// NOTE: gtests expects the name to have no special characters.
static std::string sanitizeTestName(const std::string& name)
{
std::string nameOnly = Path::GetFileNameWithoutExtension(name);
std::string res;
for (char c : nameOnly)
2018-12-11 09:56:51 +01:00
{
2021-10-28 10:54:09 +02:00
if (isalnum(static_cast<unsigned char>(c)))
2018-12-13 09:59:03 +01:00
res += c;
2018-12-11 09:56:51 +01:00
}
2018-12-13 09:59:03 +01:00
return res;
}
2018-12-11 09:56:51 +01:00
2018-12-13 09:59:03 +01:00
static std::vector<ReplayTestData> GetReplayFiles()
2018-12-11 09:56:51 +01:00
{
2018-12-13 09:59:03 +01:00
std::vector<ReplayTestData> res;
std::string basePath = TestData::GetBasePath();
std::string replayPath = Path::Combine(basePath, u8"replays");
std::string replayPathPattern = Path::Combine(replayPath, u8"*.parkrep");
2018-12-13 09:59:03 +01:00
std::vector<std::string> files;
auto scanner = Path::ScanDirectory(replayPathPattern, true);
2018-12-13 09:59:03 +01:00
while (scanner->Next())
2018-12-11 09:56:51 +01:00
{
2018-12-13 09:59:03 +01:00
ReplayTestData test;
test.name = sanitizeTestName(scanner->GetFileInfo()->Name);
test.filePath = scanner->GetPath();
res.push_back(std::move(test));
2018-12-11 09:56:51 +01:00
}
2018-12-13 09:59:03 +01:00
return res;
}
class ReplayTests : public testing::TestWithParam<ReplayTestData>
{
protected:
};
2018-12-11 09:56:51 +01:00
2018-12-13 09:59:03 +01:00
TEST_P(ReplayTests, RunReplay)
{
2018-12-11 09:56:51 +01:00
gOpenRCT2Headless = true;
gOpenRCT2NoGraphics = true;
Platform::CoreInit();
2018-12-11 09:56:51 +01:00
2018-12-13 09:59:03 +01:00
auto testData = GetParam();
auto replayFile = testData.filePath;
2018-12-11 09:56:51 +01:00
2018-12-13 09:59:03 +01:00
auto context = CreateContext();
bool initialised = context->Initialise();
ASSERT_TRUE(initialised);
2018-12-11 09:56:51 +01:00
2018-12-13 09:59:03 +01:00
auto gs = context->GetGameState();
ASSERT_NE(gs, nullptr);
2018-12-11 09:56:51 +01:00
2018-12-13 09:59:03 +01:00
IReplayManager* replayManager = context->GetReplayManager();
ASSERT_NE(replayManager, nullptr);
bool startedReplay = replayManager->StartPlayback(replayFile);
ASSERT_TRUE(startedReplay);
while (replayManager->IsReplaying())
{
gs->UpdateLogic();
if (replayManager->IsPlaybackStateMismatching())
break;
2018-12-11 09:56:51 +01:00
}
ASSERT_FALSE(replayManager->IsReplaying());
ASSERT_FALSE(replayManager->IsPlaybackStateMismatching());
2018-12-11 09:56:51 +01:00
}
2018-12-13 09:59:03 +01:00
2018-12-17 10:34:02 +01:00
static void PrintTo(const ReplayTestData& testData, std::ostream* os)
2018-12-17 09:44:13 +01:00
{
*os << testData.filePath;
}
2018-12-13 09:59:03 +01:00
struct PrintReplayParameter
{
template<class ParamType> std::string operator()(const testing::TestParamInfo<ParamType>& info) const
{
auto data = static_cast<ReplayTestData>(info.param);
return data.name;
}
};
2022-01-02 22:37:39 +01:00
INSTANTIATE_TEST_SUITE_P(Replay, ReplayTests, testing::ValuesIn(GetReplayFiles()), PrintReplayParameter());