Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba160 committed Sep 7, 2024
1 parent 977b71b commit 9658157
Show file tree
Hide file tree
Showing 23 changed files with 512 additions and 5 deletions.
50 changes: 50 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,53 @@ endif()
# WORKAROUND: with qt 6.7: install for some reason links to local qt...
# install(TARGETS ddb_gui_qt5 DESTINATION "~/.local/lib/deadbeef")
install(FILES ${CMAKE_BINARY_DIR}/ddb_gui_qt5.so DESTINATION "~/.local/lib/deadbeef")


# TESTS

project(ddb_gui_q_test LANGUAGES CXX)

add_library(ddb_gui_q_test SHARED ${SOURCES} tests/Tests.cpp
tests/Tests.h
tests/DBApi/All_DBApi.h tests/DBApi/All_DBApi.cpp
tests/DBApi/t_PlaybackControl.h tests/DBApi/t_PlaybackControl.cpp
tests/DBApi/t_PlaylistManager.h tests/DBApi/t_PlaylistManager.cpp
tests/DBApi/t_ItemImporter.cpp
tests/DBApi/t_ItemImporter.h
tests/DBApi/t_Equalizer.h tests/DBApi/t_Equalizer.cpp)

find_package(Qt6 COMPONENTS REQUIRED Test)



target_link_libraries(ddb_gui_q_test PRIVATE dbapiplugin DDB2 GuiCommon DDB2plugin GuiCommonplugin Qt6::Core Qt::Gui Qt::Quick Qt::Concurrent Qt::Svg Qt::Test)
if (USE_WIDGETS)
target_link_libraries(ddb_gui_q_test PRIVATE dbapi Qt6::Widgets Qt6::QuickWidgets)
endif()

if (USE_CHARTS)
target_link_libraries(ddb_gui_q_test PRIVATE dbapi Qt6::Charts)
endif()

target_include_directories(ddb_gui_q_test PRIVATE ".")

target_compile_definitions(ddb_gui_q_test PRIVATE USE_WIDGETS=${USE_WIDGETS} QT_USE_STRINGBUILDER)
target_compile_definitions(ddb_gui_q_test PRIVATE USE_CACHE=${USE_COVERART_CACHE})

target_compile_options(ddb_gui_q_test PRIVATE "-gdwarf-4")

# fix for static qt
get_target_property(QT_TARGET_TYPE Qt6::Core TYPE)
if(${QT_TARGET_TYPE} STREQUAL "STATIC_LIBRARY")
find_package(Qt6QmlImportScanner REQUIRED)
qt6_import_qml_plugins(ddb_gui_q_test)
endif()

if(WIN32)
target_link_options(ddb_gui_q_test PRIVATE "-Wl,--export-all-symbols")
endif()


target_compile_definitions(ddb_gui_q_test PRIVATE Q_TEST=1)

install(FILES ${CMAKE_BINARY_DIR}/ddb_gui_q_test.so DESTINATION "~/.local/lib/deadbeef")
59 changes: 56 additions & 3 deletions QtGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include <QLocale>
#include <QQuickWindow>

#if Q_TEST
#include "tests/Tests.h"
#endif


//#include "DeadbeefTranslator.h"

Expand Down Expand Up @@ -66,7 +70,9 @@ QGuiApplication *app = nullptr;


static int pluginMessage_wrapper(uint32_t id, uintptr_t ctx, uint32_t p1, uint32_t p2) {
return api->pluginMessage(id, ctx, p1, p2);
if (api)
return api->pluginMessage(id, ctx, p1, p2);
return 0;
}

static void initializeQApp() {
Expand Down Expand Up @@ -190,6 +196,10 @@ static int pluginStart() {
api = new DBApi(nullptr, deadbeef_internal);
plugin.plugin.message = pluginMessage_wrapper;

#if Q_TEST
runTests(api);
#else

startWidgets();

if (!USE_WIDGETS || DBAPI->conf_get_int("qgui.show_qtquick_window", false))
Expand All @@ -200,8 +210,10 @@ static int pluginStart() {

unloadQuick();
unloadWidgets();

delete api;
#endif
DBApi *api_del = api;
api = nullptr;
delete api_del;
delete app;

DBAPI->sendmessage(DB_EV_TERMINATE, 0, 0, 0);
Expand Down Expand Up @@ -247,4 +259,45 @@ extern "C" {
qt_plugin.register_widget = registerWidget;
return DB_PLUGIN(&plugin);
}

#if Q_TEST
DB_plugin_t *ddb_gui_q_test_load(DB_functions_t *Api) {
deadbeef_internal = Api;
plugin.plugin.api_vmajor = 1;
plugin.plugin.api_vminor = 9;
plugin.plugin.version_major = 1;
plugin.plugin.version_minor = 9;
plugin.plugin.type = DB_PLUGIN_GUI;
plugin.plugin.id = "qt5";
plugin.plugin.name = "Qt user interface";
plugin.plugin.descr = "Qt user interface";
plugin.plugin.copyright =
"ddb_gui_qt5 - Qt user interface\n"
"Copyright (C) 2010 Anton Novikov <tonn.post@gmail.com>\n"
"Copyright (C) 2011 Semen Minyushov <semikmsv@gmail.com>\n"
"Copyright (C) 2013 Karjavin Roman <redpunk231@gmail.com>\n"
"Copyright (C) 2019-2021 Jakub Wasylków <kuba_160@protonmail.com>\n"
"\n"
"This program is free software; you can redistribute it and/or\n"
"modify it under the terms of the GNU General Public License\n"
"as published by the Free Software Foundation; either version 2\n"
"of the License, or (at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n";
plugin.plugin.website = "https://github.com/kuba160/ddb_gui_qt5";
plugin.plugin.start = pluginStart;
plugin.plugin.stop = pluginStop;
plugin.plugin.connect = pluginConnect;
plugin.plugin.message = nullptr;
qt_plugin.register_widget = registerWidget;
return DB_PLUGIN(&plugin);
}
#endif
}
2 changes: 1 addition & 1 deletion dbapi/Equalizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Equalizer : public QObject
inline ddb_dsp_context_t * get_supereq();
public:
explicit Equalizer(QObject *parent, DB_functions_t *api);
Q_PROPERTY(bool available READ getEqAvailable NOTIFY eqAvailableChanged)
Q_PROPERTY(bool available READ getEqAvailable CONSTANT)
Q_PROPERTY(bool enabled READ getEqEnabled WRITE setEqEnabled NOTIFY eqEnabledChanged)

Q_PROPERTY(QVariantList values READ getEq WRITE setEq NOTIFY eqChanged)
Expand Down
1 change: 1 addition & 0 deletions dbapi/ItemImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <DBApi.h>
#include <QFutureWatcher>
#define USE_CONCURRENT 1
#ifdef USE_CONCURRENT
#include <QtConcurrent>
#endif
Expand Down
13 changes: 13 additions & 0 deletions dbapi/PlaybackControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ bool PlaybackControl::getStopAfterCurrent() {
void PlaybackControl::setStopAfterCurrent(bool value) {
deadbeef->conf_set_int("playlist.stop_after_current", value);
deadbeef->sendmessage (DB_EV_CONFIGCHANGED, 0, 0, 0);
m_stop_after_current = value;
emit stopAfterCurrentChanged();
}

bool PlaybackControl::getStopAfterAlbum() {
return m_stop_after_album;
}

void PlaybackControl::setStopAfterAlbum(bool value) {
deadbeef->conf_set_int("playlist.stop_after_album", value);
deadbeef->sendmessage (DB_EV_CONFIGCHANGED, 0, 0, 0);
m_stop_after_album = value;
emit stopAfterAlbumChanged();
}

QString PlaybackControl::tf_current(const QString &format) {
Expand Down
4 changes: 3 additions & 1 deletion dbapi/PlaybackControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PlaybackControl : public QObject
Q_PROPERTY(repeat repeat READ getRepeat WRITE setRepeat NOTIFY repeatChanged)
Q_PROPERTY(playback_state state MEMBER m_state NOTIFY stateChanged)
Q_PROPERTY(bool stop_after_current READ getStopAfterCurrent WRITE setStopAfterCurrent NOTIFY stopAfterCurrentChanged)
Q_PROPERTY(bool stop_after_album MEMBER m_stop_after_album NOTIFY stopAfterAlbumChanged)
Q_PROPERTY(bool stop_after_album READ getStopAfterAlbum WRITE setStopAfterAlbum NOTIFY stopAfterAlbumChanged)
//Q_PROPERTY(DB_playItem_t * currentTrack READ getCurrentTrack NOTIFY currentTrackChanged)
Q_PROPERTY(quint32 currentTrackIdx READ getCurrentTrackIdx WRITE setCurrentTrackIdx NOTIFY currentTrackChanged) // NOTE: won't be changed when track is moved

Expand Down Expand Up @@ -102,6 +102,8 @@ public slots:

bool getStopAfterCurrent();
void setStopAfterCurrent(bool value);
bool getStopAfterAlbum();
void setStopAfterAlbum(bool value);

public slots:
QString tf_current(const QString &format);
Expand Down
14 changes: 14 additions & 0 deletions tests/DBApi/All_DBApi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "All_DBApi.h"
#include "../Tests.h"

#include "t_PlaybackControl.h"
#include "t_PlaylistManager.h"
#include "t_ItemImporter.h"
#include "t_Equalizer.h"

void runTests_DBApi(DBApi *Api) {
Q_TEST_RUN(t_PlaybackControl, Api);
Q_TEST_RUN(t_ItemImporter, Api);
Q_TEST_RUN(t_PlaylistManager, Api);
Q_TEST_RUN(t_Equalizer, Api);
}
11 changes: 11 additions & 0 deletions tests/DBApi/All_DBApi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef ALL_DBAPI_H
#define ALL_DBAPI_H

#include <QObject>
#include <QTest>
#include <dbapi/DBApi.h>

void runTests_DBApi(DBApi *Api);

#endif // ALL_DBAPI_H

48 changes: 48 additions & 0 deletions tests/DBApi/t_Equalizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "t_Equalizer.h"
#include <QTest>
#include <QSignalSpy>

#define EQ (&api->eq)

t_Equalizer::t_Equalizer(DBApi *Api)
: QObject{Api}
{
api = Api;
}

void t_Equalizer::eqEnableTest() {
if (!EQ->getEqAvailable()) {
QSKIP("Equalizer unavailable");
}

bool curr = EQ->getEqEnabled();

QSignalSpy spy(EQ, &Equalizer::eqEnabledChanged);
EQ->setEqEnabled(!curr);
spy.wait(1);
QCOMPARE(EQ->getEqEnabled(), !curr);
QCOMPARE(spy.count(), 1);
EQ->setEqEnabled(curr);
spy.wait(1);
}

void t_Equalizer::eqSetTest() {
if (!EQ->getEqAvailable()) {
QSKIP("Equalizer unavailable");
}

QVariantList eq_orig = EQ->getEq();

QVariantList eq_test;
for(int i = 0 ; i < eq_orig.length(); i++) {
eq_test.append(i%2 ? 20.0 : -20.0);
}

QSignalSpy spy(EQ, &Equalizer::eqChanged);
EQ->setEq(eq_test);
spy.wait(1);
QCOMPARE(EQ->getEq(), eq_test);
QCOMPARE(spy.count(), 1);
EQ->setEq(eq_orig);
spy.wait(1);
}
24 changes: 24 additions & 0 deletions tests/DBApi/t_Equalizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef T_EQUALIZER_H
#define T_EQUALIZER_H

#include <QObject>
#include "dbapi/DBApi.h"

class t_Equalizer : public QObject
{
Q_OBJECT
public:
explicit t_Equalizer(DBApi *Api);

private:
DBApi *api;

private slots:

void eqEnableTest();
void eqSetTest();

signals:
};

#endif // T_EQUALIZER_H
43 changes: 43 additions & 0 deletions tests/DBApi/t_ItemImporter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "t_ItemImporter.h"
#include <QTest>

#define PM (&api->playlist)

t_ItemImporter::t_ItemImporter(DBApi *Api)
: QObject{Api}
{
api = Api;
}

void t_ItemImporter::addFileTest() {
QFuture ft = PM->runFileImport({"/home/kuba/sources/ddb_gui_qt6/tests/audio/silence.mp3"});
ft.waitForFinished();
PlayItemIterator iter = ft.result();
int i = 0;
while(iter.getNextIter()) {
i++;
}
QCOMPARE(i, 1);
}

void t_ItemImporter::addFolderTest() {
QFuture ft = PM->runFolderImport({"/home/kuba/sources/ddb_gui_qt6/tests/audio/"});
ft.waitForFinished();
PlayItemIterator iter = ft.result();
int i = 0;
while(iter.getNextIter()) {
i++;
}
QCOMPARE(i, 3);
}

void t_ItemImporter::addPlaylistTest() {
QFuture ft = PM->runPlaylistImport({"/home/kuba/sources/ddb_gui_qt6/tests/audio.m3u"});
ft.waitForFinished();
PlayItemIterator iter = ft.result();
int i = 0;
while(iter.getNextIter()) {
i++;
}
QCOMPARE(i, 3);
}
24 changes: 24 additions & 0 deletions tests/DBApi/t_ItemImporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef T_ITEMIMPORTER_H
#define T_ITEMIMPORTER_H

#include <QObject>
#include "dbapi/DBApi.h"

class t_ItemImporter : public QObject
{
Q_OBJECT
public:
explicit t_ItemImporter(DBApi *Api);

private:
DBApi *api;

private slots:
void addFolderTest();
void addFileTest();
void addPlaylistTest();

signals:
};

#endif // T_ITEMIMPORTER_H
Loading

0 comments on commit 9658157

Please sign in to comment.