diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5f5a820..05adc49 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,7 @@ SET(CLI_TESTS # live tests require X11 display and/or audio output SET(LIVE_TESTS + live_audiodevice live_rendering live_window ) diff --git a/tests/live_audiodevice.cc b/tests/live_audiodevice.cc new file mode 100644 index 0000000..de1a72a --- /dev/null +++ b/tests/live_audiodevice.cc @@ -0,0 +1,61 @@ +#include +#include + +#include +#include + +#include "testing.h" + +using namespace SDL2pp; + +BEGIN_TEST(int, char*[]) + SDL sdl(SDL_INIT_AUDIO); + + static constexpr int samplerate = 48000; + + AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096); + + std::atomic_long callback_requests(0); + + AudioDevice device(NullOpt, 0, spec, [&callback_requests](Uint8* stream, int len) { + std::fill(stream, stream + len, 0); + ++callback_requests; + }); + + { + EXPECT_TRUE(device.GetStatus(), SDL_AUDIO_PAUSED); + EXPECT_TRUE(callback_requests == 0); + + long saved_reqs1 = callback_requests; + + device.Pause(false); + EXPECT_TRUE(device.GetStatus(), SDL_AUDIO_PLAYING); + + SDL_Delay(1000); + EXPECT_TRUE(callback_requests > saved_reqs1); + + device.Pause(true); + EXPECT_TRUE(device.GetStatus(), SDL_AUDIO_PLAYING); + + long saved_reqs2 = callback_requests; + + SDL_Delay(1000); + EXPECT_TRUE(callback_requests == saved_reqs2); + + device.Pause(false); + + long saved_reqs3; + { + AudioDevice::LockHandle lock = device.Lock(); + saved_reqs3 = callback_requests; + + SDL_Delay(1000); + + EXPECT_TRUE(callback_requests == saved_reqs3); + } + + SDL_Delay(1000); + + EXPECT_TRUE(callback_requests > saved_reqs3); + } +END_TEST()