add last sources test for playersource

This commit is contained in:
Evgenii Alekseev 2016-06-18 05:08:14 +03:00
parent 0f1763cb81
commit 71ae832bcd
7 changed files with 245 additions and 60 deletions

View File

@ -44,7 +44,7 @@ PlayerSource::PlayerSource(QObject *parent, const QStringList args)
connect(m_mpdProcess,
static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(
&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 &current, 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(' '));
}

View File

@ -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 &current, 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;

View File

@ -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)

View File

@ -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/")); }));
}

View File

@ -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 <QtTest>
#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<QVariant> 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);

View File

@ -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 <QObject>
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 */

View File

@ -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);
}