aboutsummaryrefslogtreecommitdiff
path: root/src/test/AudioTest.cpp
blob: 48bba1b93e019ac656bda1676fbbd5c83ccd6dc0 (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
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
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <crepe/api/AudioSource.h>
#include <crepe/api/GameObject.h>
#include <crepe/manager/ComponentManager.h>
#include <crepe/manager/ResourceManager.h>
#include <crepe/system/AudioSystem.h>

using namespace std;
using namespace std::chrono_literals;
using namespace crepe;
using namespace testing;

class AudioTest : public Test {
private:
	class TestSoundContext : public SoundContext {
	public:
		MOCK_METHOD(SoundHandle, play, (Sound & resource), (override));
		MOCK_METHOD(void, stop, (const SoundHandle &), (override));
		MOCK_METHOD(void, set_volume, (const SoundHandle &, float), (override));
		MOCK_METHOD(void, set_loop, (const SoundHandle &, bool), (override));
	};

	class TestAudioSystem : public AudioSystem {
	public:
		using AudioSystem::AudioSystem;
		StrictMock<TestSoundContext> context;
		virtual SoundContext & get_context() { return this->context; }
	};

private:
	Mediator mediator;
	ComponentManager component_manager{mediator};
	ResourceManager resource_manager{mediator};

public:
	TestAudioSystem system{mediator};
	TestSoundContext & context = system.context;

private:
	GameObject entity = component_manager.new_object("name");

public:
	AudioSource & component = entity.add_component<AudioSource>("mwe/audio/bgm.ogg");
};

TEST_F(AudioTest, Default) {
	EXPECT_CALL(context, play(_)).Times(0);
	EXPECT_CALL(context, stop(_)).Times(0);
	EXPECT_CALL(context, set_volume(_, _)).Times(0);
	EXPECT_CALL(context, set_loop(_, _)).Times(0);
	system.update();
}

TEST_F(AudioTest, Play) {
	system.update();

	{
		InSequence seq;

		EXPECT_CALL(context, play(_)).Times(0);
		component.play();
	}

	{
		InSequence seq;

		EXPECT_CALL(context, play(_)).Times(1);
		system.update();
	}
}

TEST_F(AudioTest, Stop) {
	system.update();

	{
		InSequence seq;

		EXPECT_CALL(context, stop(_)).Times(0);
		component.stop();
	}

	{
		InSequence seq;

		EXPECT_CALL(context, stop(_)).Times(1);
		system.update();
	}
}

TEST_F(AudioTest, Volume) {
	system.update();

	{
		InSequence seq;

		EXPECT_CALL(context, set_volume(_, _)).Times(0);
		component.volume += 0.2;
	}

	{
		InSequence seq;

		EXPECT_CALL(context, set_volume(_, component.volume)).Times(1);
		system.update();
	}
}

TEST_F(AudioTest, Looping) {
	system.update();

	{
		InSequence seq;

		EXPECT_CALL(context, set_loop(_, _)).Times(0);
		component.loop = !component.loop;
	}

	{
		InSequence seq;

		EXPECT_CALL(context, set_loop(_, component.loop)).Times(1);
		system.update();
	}
}

TEST_F(AudioTest, StopOnDeactivate) {
	system.update();

	{
		InSequence seq;

		EXPECT_CALL(context, stop(_)).Times(1);
		component.active = false;
		system.update();
	}
}

TEST_F(AudioTest, PlayOnActive) {
	component.active = false;
	component.play_on_awake = true;
	system.update();

	{
		InSequence seq;

		EXPECT_CALL(context, play(_)).Times(1);
		component.active = true;
		system.update();
	}
}

TEST_F(AudioTest, PlayImmediately) {
	component.play_on_awake = false;
	component.play();

	EXPECT_CALL(context, play(_)).Times(1);

	system.update();
}