File: ReplayTests.cpp

package info (click to toggle)
openrct2 0.4.3%2Bds-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 67,880 kB
  • sloc: cpp: 549,527; ansic: 1,322; sh: 441; python: 269; xml: 180; php: 34; makefile: 19
file content (118 lines) | stat: -rw-r--r-- 3,376 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*****************************************************************************
 * Copyright (c) 2014-2022 OpenRCT2 developers
 *
 * 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>
#include <openrct2/ride/Ride.h>
#include <string>

using namespace OpenRCT2;

struct ReplayTestData
{
    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)
    {
        if (isalnum(static_cast<unsigned char>(c)))
            res += c;
    }
    return res;
}

static std::vector<ReplayTestData> GetReplayFiles()
{
    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");
    std::vector<std::string> files;

    auto scanner = Path::ScanDirectory(replayPathPattern, true);
    while (scanner->Next())
    {
        ReplayTestData test;
        test.name = sanitizeTestName(scanner->GetFileInfo()->Name);
        test.filePath = scanner->GetPath();
        res.push_back(std::move(test));
    }
    return res;
}

class ReplayTests : public testing::TestWithParam<ReplayTestData>
{
protected:
};

TEST_P(ReplayTests, RunReplay)
{
    gOpenRCT2Headless = true;
    gOpenRCT2NoGraphics = true;
    Platform::CoreInit();

    auto testData = GetParam();
    auto replayFile = testData.filePath;

    auto context = CreateContext();
    bool initialised = context->Initialise();
    ASSERT_TRUE(initialised);

    auto gs = context->GetGameState();
    ASSERT_NE(gs, nullptr);

    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;
    }
    ASSERT_FALSE(replayManager->IsReplaying());
    ASSERT_FALSE(replayManager->IsPlaybackStateMismatching());
}

static void PrintTo(const ReplayTestData& testData, std::ostream* os)
{
    *os << testData.filePath;
}

struct PrintReplayParameter
{
    template<class ParamType> std::string operator()(const testing::TestParamInfo<ParamType>& info) const
    {
        auto data = static_cast<ReplayTestData>(info.param);
        return data.name;
    }
};

INSTANTIATE_TEST_SUITE_P(Replay, ReplayTests, testing::ValuesIn(GetReplayFiles()), PrintReplayParameter());