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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2023, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#include "app/app.h"
#include "common/make_unique.h"
#include "common/system/system_other.h"
#include <functional>
#include <memory>
#include <gtest/gtest.h>
#include <hippomocks.h>
using namespace HippoMocks;
namespace ph = std::placeholders;
using TimeUtils::TimeStamp;
class CApplicationWrapper : public CApplication
{
public:
CApplicationWrapper(CSystemUtils* systemUtils)
: CApplication(systemUtils)
{
SDL_Init(0);
m_eventQueue = MakeUnique<CEventQueue>();
}
~CApplicationWrapper()
{
SDL_Quit();
}
Event CreateUpdateEvent(TimeStamp timestamp) override
{
return CApplication::CreateUpdateEvent(timestamp);
}
};
class CApplicationUT : public testing::Test
{
protected:
CApplicationUT() :
m_systemUtils(nullptr),
m_currentTime(0)
{}
~CApplicationUT() NOEXCEPT
{}
void SetUp() override;
void TearDown() override;
void NextInstant(long long diff);
TimeStamp GetCurrentTimeStamp();
void TestCreateUpdateEvent(long long relTimeExact, long long absTimeExact,
float relTime, float absTime,
long long relTimeReal, long long absTimeReal);
protected:
std::unique_ptr<CApplicationWrapper> m_app;
MockRepository m_mocks;
CSystemUtils* m_systemUtils;
private:
long long m_currentTime;
};
void CApplicationUT::SetUp()
{
m_systemUtils = m_mocks.Mock<CSystemUtils>();
m_mocks.OnCall(m_systemUtils, CSystemUtils::GetDataPath).Return("");
m_mocks.OnCall(m_systemUtils, CSystemUtils::GetLangPath).Return("");
m_mocks.OnCall(m_systemUtils, CSystemUtils::GetSaveDir).Return("");
m_mocks.OnCall(m_systemUtils, CSystemUtils::GetCurrentTimeStamp).Do(std::bind(&CApplicationUT::GetCurrentTimeStamp, this));
m_app = MakeUnique<CApplicationWrapper>(m_systemUtils);
}
void CApplicationUT::TearDown()
{
m_app.reset();
}
TimeStamp CApplicationUT::GetCurrentTimeStamp()
{
return TimeStamp{ TimeStamp::duration{m_currentTime}};
}
void CApplicationUT::NextInstant(long long diff)
{
m_currentTime += diff;
}
void CApplicationUT::TestCreateUpdateEvent(long long relTimeExact, long long absTimeExact,
float relTime, float absTime,
long long relTimeReal, long long absTimeReal)
{
TimeStamp now = GetCurrentTimeStamp();
Event event = m_app->CreateUpdateEvent(now);
EXPECT_EQ(EVENT_FRAME, event.type);
EXPECT_FLOAT_EQ(relTime, event.rTime);
EXPECT_FLOAT_EQ(relTime, m_app->GetRelTime());
EXPECT_FLOAT_EQ(absTime, m_app->GetAbsTime());
EXPECT_EQ(relTimeExact, m_app->GetExactRelTime());
EXPECT_EQ(absTimeExact, m_app->GetExactAbsTime());
EXPECT_EQ(relTimeReal, m_app->GetRealRelTime());
EXPECT_EQ(absTimeReal, m_app->GetRealAbsTime());
}
TEST_F(CApplicationUT, UpdateEventTimeCalculation_SimulationSuspended)
{
m_app->SuspendSimulation();
Event event = m_app->CreateUpdateEvent(GetCurrentTimeStamp());
EXPECT_EQ(EVENT_NULL, event.type);
}
TEST_F(CApplicationUT, UpdateEventTimeCalculation_NormalOperation)
{
// 1st update
long long relTimeExact = 1111;
long long absTimeExact = relTimeExact;
float relTime = relTimeExact / 1e9f;
float absTime = absTimeExact / 1e9f;
long long relTimeReal = relTimeExact;
long long absTimeReal = absTimeExact;
NextInstant(relTimeReal);
TestCreateUpdateEvent(relTimeExact, absTimeExact, relTime, absTime, relTimeReal, absTimeReal);
// 2nd update
relTimeExact = 2222;
absTimeExact += relTimeExact;
relTime = relTimeExact / 1e9f;
absTime = absTimeExact / 1e9f;
relTimeReal = relTimeExact;
absTimeReal = absTimeExact;
NextInstant(relTimeReal);
TestCreateUpdateEvent(relTimeExact, absTimeExact, relTime, absTime, relTimeReal, absTimeReal);
}
TEST_F(CApplicationUT, UpdateEventTimeCalculation_NegativeTimeOperation)
{
// 1st update
long long relTimeExact = 2222;
long long absTimeExact = relTimeExact;
float relTime = relTimeExact / 1e9f;
float absTime = absTimeExact / 1e9f;
long long relTimeReal = relTimeExact;
long long absTimeReal = absTimeExact;
NextInstant(relTimeReal);
TestCreateUpdateEvent(relTimeExact, absTimeExact, relTime, absTime, relTimeReal, absTimeReal);
// 2nd update
NextInstant(-1111);
Event event = m_app->CreateUpdateEvent(GetCurrentTimeStamp());
EXPECT_EQ(EVENT_NULL, event.type);
}
TEST_F(CApplicationUT, UpdateEventTimeCalculation_ChangingSimulationSpeed)
{
m_app->SetSimulationSpeed(2.0f);
// 1st update -- speed 2x
long long relTimeReal = 100;
long long absTimeReal = relTimeReal;
long long relTimeExact = relTimeReal*2;
long long absTimeExact = absTimeReal*2;
float relTime = relTimeExact / 1e9f;
float absTime = absTimeExact / 1e9f;
NextInstant(relTimeReal);
TestCreateUpdateEvent(relTimeExact, absTimeExact, relTime, absTime, relTimeReal, absTimeReal);
// 2nd update -- speed 2x
relTimeReal = 200;
absTimeReal += relTimeReal;
relTimeExact = relTimeReal*2;
absTimeExact += relTimeReal*2;
relTime = relTimeExact / 1e9f;
absTime = absTimeExact / 1e9f;
NextInstant(relTimeReal);
TestCreateUpdateEvent(relTimeExact, absTimeExact, relTime, absTime, relTimeReal, absTimeReal);
// 3rd update -- speed 4x
m_app->SetSimulationSpeed(4.0f);
relTimeReal = 300;
absTimeReal += relTimeReal;
relTimeExact = relTimeReal*4;
absTimeExact += relTimeReal*4;
relTime = relTimeExact / 1e9f;
absTime = absTimeExact / 1e9f;
NextInstant(relTimeReal);
TestCreateUpdateEvent(relTimeExact, absTimeExact, relTime, absTime, relTimeReal, absTimeReal);
// 4th update -- speed 1x
m_app->SetSimulationSpeed(1.0f);
relTimeReal = 400;
absTimeReal += relTimeReal;
relTimeExact = relTimeReal;
absTimeExact += relTimeReal;
relTime = relTimeExact / 1e9f;
absTime = absTimeExact / 1e9f;
NextInstant(relTimeReal);
TestCreateUpdateEvent(relTimeExact, absTimeExact, relTime, absTime, relTimeReal, absTimeReal);
}
TEST_F(CApplicationUT, UpdateEventTimeCalculation_SuspendingAndResumingSimulation)
{
// 1st update -- simulation enabled
long long relTimeReal = 1000;
long long absTimeReal = relTimeReal;
long long relTimeExact = relTimeReal;
long long absTimeExact = absTimeReal;
float relTime = relTimeExact / 1e9f;
float absTime = absTimeExact / 1e9f;
NextInstant(relTimeReal);
TestCreateUpdateEvent(relTimeExact, absTimeExact, relTime, absTime, relTimeReal, absTimeReal);
// 2nd update -- simulation suspended
m_app->SuspendSimulation();
long long suspensionTime = 5000;
NextInstant(suspensionTime);
// 3rd update -- simulation resumed
m_app->ResumeSimulation();
relTimeReal = 200;
absTimeReal += relTimeReal;
relTimeExact = relTimeReal;
absTimeExact += relTimeReal;
relTime = relTimeExact / 1e9f;
absTime = absTimeExact / 1e9f;
NextInstant(relTimeReal);
TestCreateUpdateEvent(relTimeExact, absTimeExact, relTime, absTime, relTimeReal, absTimeReal);
}
|