diff --git a/sources/extsysmonsources/playersource.cpp b/sources/extsysmonsources/playersource.cpp index c51511c..a686d8c 100644 --- a/sources/extsysmonsources/playersource.cpp +++ b/sources/extsysmonsources/playersource.cpp @@ -44,7 +44,7 @@ PlayerSource::PlayerSource(QObject *parent, const QStringList args) connect(m_mpdProcess, static_cast( &QProcess::finished), - [this](int, QProcess::ExitStatus) { return updateValue(); }); + [this](int, QProcess::ExitStatus) { return updateMpdValue(); }); m_mpdProcess->waitForFinished(0); m_mpdCached = defaultInfo(); } @@ -70,6 +70,27 @@ QVariant PlayerSource::data(QString source) } +QString PlayerSource::getAutoMpris() const +{ + QDBusMessage listServices = QDBusConnection::sessionBus().interface()->call( + QDBus::BlockWithGui, QString("ListNames")); + if (listServices.arguments().isEmpty()) + return QString(); + QStringList arguments = listServices.arguments().first().toStringList(); + + for (auto arg : arguments) { + if (!arg.startsWith(QString("org.mpris.MediaPlayer2."))) + continue; + qCInfo(LOG_ESS) << "Service found" << arg; + QString service = arg; + service.remove(QString("org.mpris.MediaPlayer2.")); + return service; + } + + return QString(); +} + + QVariantMap PlayerSource::initialData(QString source) const { qCDebug(LOG_ESS) << "Source" << source; @@ -212,7 +233,30 @@ QStringList PlayerSource::sources() const } -void PlayerSource::updateValue() +QString PlayerSource::buildString(const QString ¤t, const QString &value, + const int s) +{ + qCDebug(LOG_ESS) << "Current value" << current << "received" << value + << "will be stripped after" << s; + + int index = value.indexOf(current); + if ((current.isEmpty()) || ((index + s + 1) > value.count())) + return QString("%1").arg(value.left(s), s, QLatin1Char(' ')); + else + return QString("%1").arg(value.mid(index + 1, s), s, QLatin1Char(' ')); +} + + +QString PlayerSource::stripString(const QString &value, const int s) +{ + qCDebug(LOG_ESS) << "New value" << value << "will be stripped after" << s; + + return value.count() > s ? QString("%1\u2026").arg(value.left(s - 1)) + : value.leftJustified(s, QLatin1Char(' ')); +} + + +void PlayerSource::updateMpdValue() { qCInfo(LOG_ESS) << "Cmd returns" << m_mpdProcess->exitCode(); QString qdebug = QTextCodec::codecForMib(106) @@ -243,6 +287,8 @@ void PlayerSource::updateValue() } } } + + emit(dataReceived(m_mpdCached)); } @@ -259,27 +305,6 @@ QVariantHash PlayerSource::defaultInfo() const } -QString PlayerSource::getAutoMpris() const -{ - QDBusMessage listServices = QDBusConnection::sessionBus().interface()->call( - QDBus::BlockWithGui, QString("ListNames"), DBUS_CALL_TIMEOUT); - if (listServices.arguments().isEmpty()) - return QString(); - QStringList arguments = listServices.arguments().first().toStringList(); - - for (auto arg : arguments) { - if (!arg.startsWith(QString("org.mpris.MediaPlayer2."))) - continue; - qCInfo(LOG_ESS) << "Service found" << arg; - QString service = arg; - service.remove(QString("org.mpris.MediaPlayer2.")); - return service; - } - - return QString(); -} - - QVariantHash PlayerSource::getPlayerMpdInfo(const QString mpdAddress) const { qCDebug(LOG_ESS) << "MPD" << mpdAddress; @@ -362,26 +387,3 @@ QVariantHash PlayerSource::getPlayerMprisInfo(const QString mpris) const return info; } - - -QString PlayerSource::buildString(const QString current, const QString value, - const int s) const -{ - qCDebug(LOG_ESS) << "Current value" << current << "received" << value - << "will be stripped after" << s; - - int index = value.indexOf(current); - if ((current.isEmpty()) || ((index + s + 1) > value.count())) - return QString("%1").arg(value.left(s), s, QLatin1Char(' ')); - else - return QString("%1").arg(value.mid(index + 1, s), s, QLatin1Char(' ')); -} - - -QString PlayerSource::stripString(const QString value, const int s) const -{ - qCDebug(LOG_ESS) << "New value" << value << "will be stripped after" << s; - - return value.count() > s ? QString("%1\u2026").arg(value.left(s - 1)) - : value.leftJustified(s, QLatin1Char(' ')); -} diff --git a/sources/extsysmonsources/playersource.h b/sources/extsysmonsources/playersource.h index baf93fb..48fa39b 100644 --- a/sources/extsysmonsources/playersource.h +++ b/sources/extsysmonsources/playersource.h @@ -36,22 +36,22 @@ public: explicit PlayerSource(QObject *parent, const QStringList args); virtual ~PlayerSource(); QVariant data(QString source); + QString getAutoMpris() const; QVariantMap initialData(QString source) const; void run(); QStringList sources() const; + // additional method to build dynamic tags + static QString buildString(const QString ¤t, const QString &value, + const int s); + static QString stripString(const QString &value, const int s); private slots: - void updateValue(); + void updateMpdValue(); private: inline QVariantHash defaultInfo() const; - QString getAutoMpris() const; QVariantHash getPlayerMpdInfo(const QString mpdAddress) const; QVariantHash getPlayerMprisInfo(const QString mpris) const; - // additional method to build dynamic tags - QString buildString(const QString current, const QString value, - const int s) const; - QString stripString(const QString value, const int s) const; // configuration and values QString m_mpdAddress; QVariantHash m_mpdCached; diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index d7aabaa..493b378 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -26,7 +26,7 @@ set(TEST_MODULES abstractextitem extquotes extscript extupgrade extweather abstractformatter datetimeformatter floatformatter noformatter scriptformatter extitemaggregator - batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource processessource) + batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource) foreach (TEST_MODULE ${TEST_MODULES}) set(${TEST_MODULE}_HEADERS test${TEST_MODULE}.h) set(${TEST_MODULE}_SOURCES test${TEST_MODULE}.cpp) diff --git a/sources/test/testdesktopsource.cpp b/sources/test/testdesktopsource.cpp index 602efd8..4096d06 100644 --- a/sources/test/testdesktopsource.cpp +++ b/sources/test/testdesktopsource.cpp @@ -40,9 +40,10 @@ void TestDesktopSource::test_sources() { QCOMPARE(source->sources().count(), 4); // FIXME there is segfault here sometimes o_0 -// QVERIFY(std::all_of( -// source->sources().cbegin(), source->sources().cend(), -// [](const QString &src) { return src.startsWith(QString("desktop/")); })); + // QVERIFY(std::all_of( + // source->sources().cbegin(), source->sources().cend(), + // [](const QString &src) { return + // src.startsWith(QString("desktop/")); })); } diff --git a/sources/test/testplayersource.cpp b/sources/test/testplayersource.cpp new file mode 100644 index 0000000..b7c968e --- /dev/null +++ b/sources/test/testplayersource.cpp @@ -0,0 +1,131 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#include "testplayersource.h" + +#include + +#include "awtestlibrary.h" +#include "playersource.h" + + +void TestPlayerSource::initTestCase() +{ +} + + +void TestPlayerSource::cleanupTestCase() +{ +} + + +void TestPlayerSource::_test_sources(const PlayerSource *source) +{ + QVERIFY(source->sources().count() == 11); +} + + +void TestPlayerSource::test_buildString() +{ + QString randomString = AWTestLibrary::randomString(40); + QString str = PlayerSource::buildString(QString(), randomString, 20); + QCOMPARE(str.count(), 20); + + str = PlayerSource::buildString(str, randomString, 20); + QCOMPARE(str.count(), 20); + QCOMPARE(randomString.indexOf(str), 1); + + str = PlayerSource::buildString(QString(), AWTestLibrary::randomString(10), + 20); + QCOMPARE(str.count(), 20); +} + + +void TestPlayerSource::test_stripString() +{ + QString str = PlayerSource::buildString( + QString(), AWTestLibrary::randomString(40), 20); + QCOMPARE(str.count(), 20); + + str = PlayerSource::buildString(QString(), AWTestLibrary::randomString(10), + 20); + QCOMPARE(str.count(), 20); +} + + +void TestPlayerSource::test_autoMpris() +{ + QStringList args(QStringList() << QString("mpris") << mpdAddress + << QString::number(mpdPort) + << QString("auto") << QString::number(10)); + PlayerSource *source = new PlayerSource(this, args); + + QWARN("Will fail if no MPRIS supported player is run"); + QVERIFY(!source->getAutoMpris().isEmpty()); +} + + +void TestPlayerSource::test_mpd() +{ + QStringList args(QStringList() << QString("mpd") << mpdAddress + << QString::number(mpdPort) + << QString("auto") << QString::number(10)); + PlayerSource *source = new PlayerSource(this, args); + _test_sources(source); + + // init spy + QSignalSpy spy(source, SIGNAL(dataReceived(const QVariantHash &))); + QVariant firstValue = source->data(QString("player/title")); + + // check values + QVERIFY(spy.wait(5000)); + QList arguments = spy.takeFirst(); + QVariantHash secondValue = arguments.at(0).toHash(); + + // actually nothing to test here just print warning if no information found + if (secondValue[QString("player/title")].toString() == QString("unknown")) { + QWARN("No mpd found"); + } else { + QVERIFY(secondValue[QString("player/progress")].toInt() + < secondValue[QString("player/duration")].toInt()); + } +} + + +void TestPlayerSource::test_mpris() +{ + QStringList args(QStringList() << QString("mpris") << mpdAddress + << QString::number(mpdPort) + << QString("auto") << QString::number(10)); + PlayerSource *source = new PlayerSource(this, args); + _test_sources(source); + + QString value = source->data(QString("player/title")).toString(); + int progress = source->data(QString("player/progress")).toInt(); + int duration = source->data(QString("player/duration")).toInt(); + + // actually nothing to test here just print warning if no information found + if (value == QString("unknown")) { + QWARN("No mpris found"); + } else { + QVERIFY(progress < duration); + } +} + + +QTEST_MAIN(TestPlayerSource); diff --git a/sources/test/testplayersource.h b/sources/test/testplayersource.h new file mode 100644 index 0000000..ef70b6d --- /dev/null +++ b/sources/test/testplayersource.h @@ -0,0 +1,49 @@ +/*************************************************************************** + * This file is part of awesome-widgets * + * * + * awesome-widgets is free software: you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * awesome-widgets is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with awesome-widgets. If not, see http://www.gnu.org/licenses/ * + ***************************************************************************/ + + +#ifndef TESTPLAYERSOURCE_H +#define TESTPLAYERSOURCE_H + +#include + + +class PlayerSource; + +class TestPlayerSource : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // test + void _test_sources(const PlayerSource *source); + void test_buildString(); + void test_stripString(); + void test_autoMpris(); + void test_mpd(); + void test_mpris(); + +private: + QString mpdAddress = QString("localhost"); + int mpdPort = 6600; +}; + + +#endif /* TESTPLAYERSOURCE_H */ diff --git a/sources/test/testprocessessource.cpp b/sources/test/testprocessessource.cpp index 11280b5..5119418 100644 --- a/sources/test/testprocessessource.cpp +++ b/sources/test/testprocessessource.cpp @@ -40,16 +40,18 @@ void TestProcessesSource::test_sources() { QCOMPARE(source->sources().count(), 3); // FIXME there is segfault here sometimes o_0 -// QVERIFY(std::all_of( -// source->sources().cbegin(), source->sources().cend(), -// [](const QString &src) { return src.startsWith(QString("ps/")); })); + // QVERIFY(std::all_of( + // source->sources().cbegin(), source->sources().cend(), + // [](const QString &src) { return src.startsWith(QString("ps/")); + // })); } void TestProcessesSource::test_values() { QVERIFY(source->data(QString("ps/running/count")).toInt() > 0); - QVERIFY(source->data(QString("ps/running/list")).toStringList().count() > 0); + QVERIFY(source->data(QString("ps/running/list")).toStringList().count() + > 0); QVERIFY(source->data(QString("ps/total/count")).toInt() > 0); }