diff options
Diffstat (limited to 'src/test/AudioTest.cpp')
-rw-r--r-- | src/test/AudioTest.cpp | 174 |
1 files changed, 126 insertions, 48 deletions
diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index 2a2d0e1..9c3cb9c 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -1,6 +1,5 @@ -#include "util/Log.h" #include <gtest/gtest.h> -#include <future> +#include <gmock/gmock.h> #include <crepe/manager/ComponentManager.h> #include <crepe/manager/ResourceManager.h> @@ -14,62 +13,141 @@ using namespace crepe; using namespace testing; class AudioTest : public Test { +private: + class TestSoundContext : public SoundContext { + public: + MOCK_METHOD(Sound::Handle, play, (Sound & resource), (override)); + MOCK_METHOD(void, stop, (Sound::Handle &), (override)); + MOCK_METHOD(void, set_volume, (Sound &, Sound::Handle &, float), (override)); + MOCK_METHOD(void, set_loop, (Sound &, Sound::Handle &, bool), (override)); + }; + + class TestAudioSystem : public AudioSystem { + public: + using AudioSystem::AudioSystem; + StrictMock<TestSoundContext> context; + virtual SoundContext & get_context() { + return this->context; + } + }; + +private: Mediator mediator; -public: ComponentManager component_manager{mediator}; ResourceManager resource_manager{mediator}; - AudioSystem system {mediator}; +public: + TestAudioSystem system {mediator}; + TestSoundContext & context = system.context; private: GameObject entity = component_manager.new_object("name"); public: - AudioSource & bgm = entity.add_component<AudioSource>("mwe/audio/bgm.ogg"); - AudioSource & sfx1 = entity.add_component<AudioSource>("mwe/audio/sfx1.wav"); - AudioSource & sfx2 = entity.add_component<AudioSource>("mwe/audio/sfx2.wav"); - AudioSource & sfx3 = entity.add_component<AudioSource>("mwe/audio/sfx3.wav"); - - void SetUp() override { - bgm.play_on_awake = true; - } + AudioSource & component = entity.add_component<AudioSource>("mwe/audio/bgm.ogg"); }; TEST_F(AudioTest, Default) { - bool example_done = false; - - future example = async([&](){ - // Start the background track. This happens automatically due to the play_on_awake property - // being true. The following call is optional and doesn't start two simultanious voices if - // left in: - // bgm.play(); - - // Play each sample sequentially while pausing and resuming the background track - this_thread::sleep_for(500ms); - sfx1.play(); - this_thread::sleep_for(500ms); - sfx2.play(); - bgm.active = false; - this_thread::sleep_for(500ms); - sfx3.play(); - bgm.active = true; - this_thread::sleep_for(500ms); - - // Play all samples simultaniously - sfx1.play(); - sfx2.play(); - sfx3.play(); - this_thread::sleep_for(1000ms); - }); - - future system_loop = async([&](){ - while (!example_done) { - auto next = chrono::steady_clock::now() + 25ms; - system.update(); - this_thread::sleep_until(next); - } - }); + 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; - example.wait(); - example_done = true; - system_loop.wait(); + 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(); + } } |