ST_engine  0.3-ALPHA
audio_manager_tests.cpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #include <gtest/gtest.h>
11 #include <audio_manager.hpp>
12 
14 class audio_manager_test : public ::testing::Test {
15 
16 protected:
17 
18  void set_chunks(ska::bytell_hash_map<uint16_t, Mix_Chunk *> *assets) {
19  test_mngr->chunks_ptr = assets;
20  }
21 
22  void set_music(ska::bytell_hash_map<uint16_t, Mix_Music *> *assets) {
23  test_mngr->music_ptr = assets;
24  }
25 
26  void play_sound(uint16_t arg, uint8_t volume, uint8_t loops) {
27  test_mngr->play_sound(arg, volume, loops);
28  }
29 
30  void play_music(uint16_t arg, uint8_t volume, uint8_t loops) {
31  test_mngr->play_music(arg, volume, loops);
32  }
33 
34  audio_manager *test_mngr{};
35  message_bus *msg_bus{};
36  task_manager *task_mngr{};
37 
38  static void SetUpTestCase() {
39  SDL_Init(SDL_INIT_VIDEO);
40  Mix_OpenAudio(22050, AUDIO_F32SYS, 2, 1024);
41  Mix_Init(MIX_INIT_OGG);
42  }
43 
44  static void TearDownTestCase() {
45  while (Mix_Init(0)) {
46  Mix_Quit();
47  }
48  Mix_CloseAudio();
49  SDL_Quit();
50  }
51 
52 
53  void SetUp() override {
54  msg_bus = new message_bus();
55  task_mngr = new task_manager(0);
56  test_mngr = new audio_manager(*task_mngr, *msg_bus);
57  }
58 
59  void TearDown() override {
60  delete test_mngr;
61  delete msg_bus;
62  delete task_mngr;
63  }
64 };
65 
66 TEST_F(audio_manager_test, test_play_wav_full_volume) {
67  auto test_wav = Mix_LoadWAV("test_sound.wav");
68  ASSERT_TRUE(test_wav);
69  ska::bytell_hash_map<uint16_t, Mix_Chunk *> assets;
70  assets[1] = test_wav;
71  set_chunks(&assets);
72  play_sound(1, MIX_MAX_VOLUME, 0);
73  SDL_Delay(1000);
74  Mix_FreeChunk(test_wav);
75 }
76 
77 TEST_F(audio_manager_test, test_play_wav_half_volume) {
78  auto test_wav = Mix_LoadWAV("test_sound.wav");
79  ASSERT_TRUE(test_wav);
80  ska::bytell_hash_map<uint16_t, Mix_Chunk *> assets;
81  assets[1] = test_wav;
82  set_chunks(&assets);
83  play_sound(1, MIX_MAX_VOLUME / 2, 0);
84  SDL_Delay(1000);
85  Mix_FreeChunk(test_wav);
86 }
87 
88 TEST_F(audio_manager_test, test_play_wav_looping) {
89  auto test_wav = Mix_LoadWAV("test_sound.wav");
90  ASSERT_TRUE(test_wav);
91  ska::bytell_hash_map<uint16_t, Mix_Chunk *> assets;
92  assets[1] = test_wav;
93  set_chunks(&assets);
94  play_sound(1, MIX_MAX_VOLUME, 3);
95  SDL_Delay(2000);
96  Mix_FreeChunk(test_wav);
97 }
98 
99 TEST_F(audio_manager_test, test_play_ogg_full_volume) {
100  auto test_ogg = Mix_LoadMUS("test_music.ogg");
101  ASSERT_TRUE(test_ogg);
102  ska::bytell_hash_map<uint16_t, Mix_Music *> assets;
103  assets[1] = test_ogg;
104  set_music(&assets);
105  play_music(1, MIX_MAX_VOLUME, 3);
106  SDL_Delay(10000);
107  Mix_FreeMusic(test_ogg);
108 }
109 
110 TEST_F(audio_manager_test, test_play_ogg_half_volume) {
111  auto test_ogg = Mix_LoadMUS("test_music.ogg");
112  ASSERT_TRUE(test_ogg);
113  ska::bytell_hash_map<uint16_t, Mix_Music *> assets;
114  assets[1] = test_ogg;
115  set_music(&assets);
116  play_music(1, MIX_MAX_VOLUME / 2, 3);
117  SDL_Delay(10000);
118  Mix_FreeMusic(test_ogg);
119 }
120 
121 int main(int argc, char **argv) {
122  ::testing::InitGoogleTest(&argc, argv);
123  return RUN_ALL_TESTS();
124 }
Tests fixture for the audio_manager.
This object is responsible for playing sounds and music.
The central messaging system of the engine. All subsystem make extensive use of it.
Definition: message_bus.hpp:29
The Task Manager handles all things multi-threaded in the engine.