diff --git a/selfdrive/ui/SConscript b/selfdrive/ui/SConscript index 669c214746714a..a80d1bc38628c8 100644 --- a/selfdrive/ui/SConscript +++ b/selfdrive/ui/SConscript @@ -47,6 +47,7 @@ qt_env.Program("soundd/_soundd", ["soundd/main.cc", "soundd/sound.cc"], LIBS=qt_ if GetOption('test'): qt_env.Program("tests/playsound", "tests/playsound.cc", LIBS=base_libs) qt_env.Program('tests/test_sound', ['tests/test_runner.cc', 'soundd/sound.cc', 'tests/test_sound.cc'], LIBS=qt_libs) + qt_env.Program('tests/test_sound_volume', ['tests/test_runner.cc', 'soundd/sound.cc', 'tests/test_sound_volume.cc'], LIBS=qt_libs) qt_env.SharedLibrary("qt/python_helpers", ["qt/qt_window.cc"], LIBS=qt_libs) diff --git a/selfdrive/ui/tests/.gitignore b/selfdrive/ui/tests/.gitignore index 26335744f3daf4..65019d1f1d22e0 100644 --- a/selfdrive/ui/tests/.gitignore +++ b/selfdrive/ui/tests/.gitignore @@ -1,4 +1,5 @@ test playsound test_sound +test_sound_volume test_translations diff --git a/selfdrive/ui/tests/test_sound_volume.cc b/selfdrive/ui/tests/test_sound_volume.cc new file mode 100644 index 00000000000000..4902f41e97c476 --- /dev/null +++ b/selfdrive/ui/tests/test_sound_volume.cc @@ -0,0 +1,63 @@ +#include +#include +#include + +#include "catch2/catch.hpp" +#include "selfdrive/ui/soundd/sound.h" + +void controls_thread(int loop_count) { + PubMaster pm({"carState", "controlsState", "deviceState"}); + MessageBuilder deviceStateMsg; + auto deviceState = deviceStateMsg.initEvent().initDeviceState(); + deviceState.setStarted(true); + + const int DT_CTRL = 10; // ms + + // speeds (volume levels) + const std::vector vEgos = {0, 20, 0, 20,}; + + for (float vEgo : vEgos) { + printf("\n## changing volume to %.1f\n\n", vEgo); + MessageBuilder carStateMsg; + auto carState = carStateMsg.initEvent().initCarState(); + carState.setVEgo(vEgo); + pm.send("carState", carStateMsg); + + for (int i = 0; i < loop_count; ++i) { + // send no alert sound + for (int j = 0; j < 1000 / DT_CTRL; ++j) { + MessageBuilder msg; + msg.initEvent().initControlsState(); + pm.send("carState", carStateMsg); + pm.send("controlsState", msg); + pm.send("deviceState", deviceStateMsg); + QThread::msleep(DT_CTRL); + } + + printf("playing engage.wav\n"); + for (int j = 0; j < 1000 / DT_CTRL; ++j) { + MessageBuilder msg; + auto cs = msg.initEvent().initControlsState(); + cs.setAlertSound(AudibleAlert::ENGAGE); + cs.setAlertType("engage.wav"); + pm.send("controlsState", msg); + pm.send("deviceState", deviceStateMsg); + QThread::msleep(DT_CTRL); + } + } + } + + QThread::currentThread()->quit(); +} + +TEST_CASE("test soundd changing volume") { + QEventLoop loop; + Sound sound; + const int test_loop_cnt = 2; + + QThread t; + QObject::connect(&t, &QThread::started, [=]() { controls_thread(test_loop_cnt); }); + QObject::connect(&t, &QThread::finished, [&]() { loop.quit(); }); + t.start(); + loop.exec(); +}