aboutsummaryrefslogtreecommitdiff
path: root/src/test/LoopManagerTest.cpp
blob: 55ccbb302941d24f1b76ac29824d585a8deed920 (plain)
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
#include <chrono>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#define private public
#define protected public
#include <crepe/api/LoopManager.h>
#include <crepe/manager/EventManager.h>
#include <crepe/manager/LoopTimerManager.h>
using namespace std::chrono;
using namespace crepe;

class LoopManagerTest : public ::testing::Test {
protected:
	class TestGameLoop : public crepe::LoopManager {
	public:
		MOCK_METHOD(void, fixed_update, (), (override));
		MOCK_METHOD(void, update, (), (override));
		MOCK_METHOD(void, render, (), (override));
	};

	TestGameLoop test_loop;
	void SetUp() override {}
};

TEST_F(LoopManagerTest, FixedUpdate) {
	// Arrange
	test_loop.loop_timer.set_target_fps(60);

	// Set expectations for the mock calls
	EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60));
	EXPECT_CALL(test_loop, update).Times(::testing::Exactly(60));
	EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50));

	// Start the loop in a separate thread
	std::thread loop_thread([&]() { test_loop.start(); });

	// Let the loop run for exactly 1 second
	std::this_thread::sleep_for(std::chrono::seconds(1));

	// Stop the game loop
	test_loop.game_running = false;
	// Wait for the loop thread to finish
	loop_thread.join();

	// Test finished
}
TEST_F(LoopManagerTest, ScaledFixedUpdate) {
	// Arrange
	test_loop.loop_timer.set_target_fps(60);

	// Set expectations for the mock calls
	EXPECT_CALL(test_loop, render).Times(::testing::Exactly(60));
	EXPECT_CALL(test_loop, update).Times(::testing::Exactly(60));
	EXPECT_CALL(test_loop, fixed_update).Times(::testing::Exactly(50));

	// Start the loop in a separate thread
	std::thread loop_thread([&]() { test_loop.start(); });

	// Let the loop run for exactly 1 second
	std::this_thread::sleep_for(std::chrono::seconds(1));

	// Stop the game loop
	test_loop.game_running = false;
	// Wait for the loop thread to finish
	loop_thread.join();

	// Test finished
}
TEST_F(LoopManagerTest, ShutDown) {
	// Arrange
	test_loop.loop_timer.set_target_fps(60);

	EXPECT_CALL(test_loop, render).Times(::testing::AtLeast(1));
	EXPECT_CALL(test_loop, update).Times(::testing::AtLeast(1));
	EXPECT_CALL(test_loop, fixed_update).Times(::testing::AtLeast(1));
	// Start the loop in a separate thread
	std::thread loop_thread([&]() { test_loop.start(); });
	std::this_thread::sleep_for(std::chrono::milliseconds(1));
	test_loop.event_manager.trigger_event<ShutDownEvent>(ShutDownEvent{});
	// Wait for the loop thread to finish
	loop_thread.join();

	// Test finished
}