add tests for battery and gpuload sources

+ some tests refactoring
This commit is contained in:
Evgenii Alekseev 2016-06-09 12:45:21 +03:00
parent bc2071a493
commit 78b7a87c29
16 changed files with 269 additions and 44 deletions

View File

@ -40,6 +40,26 @@ BatterySource::~BatterySource()
}
QStringList BatterySource::getSources()
{
QStringList sources;
sources.append(QString("battery/ac"));
sources.append(QString("battery/bat"));
m_batteriesCount
= QDir(m_acpiPath)
.entryList(QStringList() << QString("BAT*"),
QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)
.count();
qCInfo(LOG_ESS) << "Init batteries count as" << m_batteriesCount;
for (int i = 0; i < m_batteriesCount; i++)
sources.append(QString("battery/bat%1").arg(i));
qCInfo(LOG_ESS) << "Sources list" << sources;
return sources;
}
QVariant BatterySource::data(QString source)
{
qCDebug(LOG_ESS) << "Source" << source;
@ -120,22 +140,3 @@ QStringList BatterySource::sources() const
{
return m_sources;
}
QStringList BatterySource::getSources()
{
QStringList sources;
sources.append(QString("battery/ac"));
sources.append(QString("battery/bat"));
m_batteriesCount
= QDir(m_acpiPath)
.entryList(QStringList() << QString("BAT*"),
QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)
.count();
qCInfo(LOG_ESS) << "Init batteries count as" << m_batteriesCount;
for (int i = 0; i < m_batteriesCount; i++)
sources.append(QString("battery/bat%1").arg(i));
qCInfo(LOG_ESS) << "Sources list" << sources;
return sources;
}

View File

@ -28,13 +28,13 @@ class BatterySource : public AbstractExtSysMonSource
public:
explicit BatterySource(QObject *parent, const QStringList args);
virtual ~BatterySource();
QStringList getSources();
QVariant data(QString source);
QVariantMap initialData(QString source) const;
void run();
QStringList sources() const;
private:
QStringList getSources();
// configuration and values
int m_batteriesCount = 0;
QString m_acpiPath;

View File

@ -77,7 +77,7 @@ QVariant GPULoadSource::data(QString source)
if (source == QString("gpu/load"))
run();
return m_value;
return m_values[source];
}
@ -140,7 +140,7 @@ void GPULoadSource::updateValue()
QString load = str.remove(QString("<gpu_util>"))
.remove(QString("</gpu_util>"))
.remove(QChar('%'));
m_value = load.toFloat();
m_values[QString("gpu/load")] = load.toFloat();
break;
}
} else if (m_device == QString("ati")) {
@ -150,7 +150,7 @@ void GPULoadSource::updateValue()
QString load
= str.split(QChar(' '), QString::SkipEmptyParts)[3].remove(
QChar('%'));
m_value = load.toFloat();
m_values[QString("gpu/load")] = load.toFloat();
break;
}
}

View File

@ -43,7 +43,7 @@ private:
// configuration and values
QString m_device;
QProcess *m_process = nullptr;
QVariant m_value;
QVariantHash m_values;
};

View File

@ -26,7 +26,7 @@ set(TEST_MODULES
abstractextitem extquotes extscript extupgrade extweather
abstractformatter datetimeformatter floatformatter noformatter scriptformatter
extitemaggregator
hddtempsource)
batterysource gpuloadsource gputempsource hddtempsource)
foreach (TEST_MODULE ${TEST_MODULES})
set(${TEST_MODULE}_HEADERS test${TEST_MODULE}.h)
set(${TEST_MODULE}_SOURCES test${TEST_MODULE}.cpp)

View File

@ -0,0 +1,61 @@
/***************************************************************************
* 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 "testbatterysource.h"
#include <QtTest>
#include "awtestlibrary.h"
#include "batterysource.h"
void TestBatterySource::initTestCase()
{
source = new BatterySource(this, QStringList() << acpiPath);
}
void TestBatterySource::cleanupTestCase()
{
delete source;
}
void TestBatterySource::test_sources()
{
QVERIFY(source->sources().count() >= 2);
}
void TestBatterySource::test_battery()
{
if (source->sources().count() == 2)
QSKIP("No battery found, test will be skipped");
QStringList batteries = source->sources();
std::for_each(batteries.begin(), batteries.end(), [this](const QString bat) {
QVariant value = source->data(bat);
if (bat == QString("battery/ac"))
QCOMPARE(value.type(), QVariant::Bool);
else
QVERIFY((value.toFloat() >= battery.first) && (value.toFloat() <= battery.second));
});
}
QTEST_MAIN(TestBatterySource);

View File

@ -0,0 +1,46 @@
/***************************************************************************
* 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 TESTBATTERYSOURCE_H
#define TESTBATTERYSOURCE_H
#include <QObject>
class BatterySource;
class TestBatterySource : public QObject
{
Q_OBJECT
private slots:
// initialization
void initTestCase();
void cleanupTestCase();
// test
void test_sources();
void test_battery();
private:
BatterySource *source = nullptr;
QString acpiPath = QString("/sys/class/power_supply/");
QPair<int, int> battery = QPair<int, int>(0, 100);
};
#endif /* TESTBATTERYSOURCE_H */

View File

@ -53,9 +53,6 @@ void TestExtQuotes::test_run()
// init spy
QSignalSpy spy(extQuotes, SIGNAL(dataReceived(const QVariantHash &)));
QVariantHash firstValue = extQuotes->run();
QCOMPARE(firstValue[extQuotes->tag(QString("ask"))].toDouble(), 0.0);
QCOMPARE(firstValue[extQuotes->tag(QString("bid"))].toDouble(), 0.0);
QCOMPARE(firstValue[extQuotes->tag(QString("price"))].toDouble(), 0.0);
// check values
QVERIFY(spy.wait(5000));
@ -67,6 +64,9 @@ void TestExtQuotes::test_run()
cache[QString("price")]
= arguments.at(0).toHash()[extQuotes->tag(QString("price"))];
QCOMPARE(firstValue[extQuotes->tag(QString("ask"))].toDouble(), 0.0);
QCOMPARE(firstValue[extQuotes->tag(QString("bid"))].toDouble(), 0.0);
QCOMPARE(firstValue[extQuotes->tag(QString("price"))].toDouble(), 0.0);
for (auto type : types) {
qDebug() << "Test type" << type;
QVERIFY((cache[type].toDouble() > price.first)

View File

@ -60,12 +60,13 @@ void TestExtScript::test_run()
// init spy
QSignalSpy spy(extScript, SIGNAL(dataReceived(const QVariantHash &)));
QVariantHash firstValue = extScript->run();
QCOMPARE(firstValue[extScript->tag(QString("custom"))].toString(),
QString(""));
// check values
QVERIFY(spy.wait(5000));
QList<QVariant> arguments = spy.takeFirst();
QCOMPARE(firstValue[extScript->tag(QString("custom"))].toString(),
QString(""));
QCOMPARE(
arguments.at(0).toHash()[extScript->tag(QString("custom"))].toString(),
QString("\n%1").arg(randomString));

View File

@ -57,11 +57,12 @@ void TestExtUpgrade::test_run()
// init spy
QSignalSpy spy(extUpgrade, SIGNAL(dataReceived(const QVariantHash &)));
QVariantHash firstValue = extUpgrade->run();
QCOMPARE(firstValue[extUpgrade->tag(QString("pkgcount"))].toInt(), 0);
// check values
QVERIFY(spy.wait(5000));
QList<QVariant> arguments = spy.takeFirst();
QCOMPARE(firstValue[extUpgrade->tag(QString("pkgcount"))].toInt(), 0);
QCOMPARE(
arguments.at(0).toHash()[extUpgrade->tag(QString("pkgcount"))].toInt(),
randomStrings.count());

View File

@ -0,0 +1,65 @@
/***************************************************************************
* 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 "testgpuloadsource.h"
#include <QtTest>
#include "awtestlibrary.h"
#include "gpuloadsource.h"
void TestGPULoadSource::initTestCase()
{
device = GPULoadSource::autoGpu();
QVERIFY(!device.isEmpty());
source = new GPULoadSource(this, QStringList() << device);
}
void TestGPULoadSource::cleanupTestCase()
{
delete source;
}
void TestGPULoadSource::test_sources()
{
QCOMPARE(source->sources(), QStringList() << src);
}
void TestGPULoadSource::test_gpuload()
{
if (device == QString("disable"))
QSKIP("Not supported device, test will be skipped");
QSignalSpy spy(source, SIGNAL(dataReceived(const QVariantHash &)));
float firstValue = source->data(src).toFloat();
QVERIFY(spy.wait(5000));
QVariantHash arguments = spy.takeFirst().at(0).toHash();
float secondValue = arguments[src].toFloat();
QCOMPARE(firstValue, 0.0f);
QVERIFY((secondValue >= load.first) && (secondValue <= load.second));
}
QTEST_MAIN(TestGPULoadSource);

View File

@ -0,0 +1,47 @@
/***************************************************************************
* 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 TESTGPULOADSOURCE_H
#define TESTGPULOADSOURCE_H
#include <QObject>
class GPULoadSource;
class TestGPULoadSource : public QObject
{
Q_OBJECT
private slots:
// initialization
void initTestCase();
void cleanupTestCase();
// test
void test_sources();
void test_gpuload();
private:
GPULoadSource *source = nullptr;
QString device;
QString src = QString("gpu/load");
QPair<float, float> load = QPair<float, float>(0.0f, 100.0f);
};
#endif /* TESTGPULOADSOURCE_H */

View File

@ -29,19 +29,19 @@ void TestGPUTemperatureSource::initTestCase()
device = GPUTemperatureSource::autoGpu();
QVERIFY(!device.isEmpty());
gputempSource = new GPUTemperatureSource(this, QStringList() << device);
source = new GPUTemperatureSource(this, QStringList() << device);
}
void TestGPUTemperatureSource::cleanupTestCase()
{
delete gputempSource;
delete source;
}
void TestGPUTemperatureSource::test_sources()
{
QCOMPARE(gputempSource->sources(), QStringList() << source);
QCOMPARE(source->sources(), QStringList() << src);
}
@ -50,13 +50,14 @@ void TestGPUTemperatureSource::test_gputemp()
if (device == QString("disable"))
QSKIP("Not supported device, test will be skipped");
QSignalSpy spy(gputempSource, SIGNAL(dataReceived(const QVariantHash &)));
float firstValue = gputempSource->data(source).toFloat();
QCOMPARE(firstValue, 0.0f);
QSignalSpy spy(source, SIGNAL(dataReceived(const QVariantHash &)));
float firstValue = source->data(src).toFloat();
QVERIFY(spy.wait(5000));
QVariantHash arguments = spy.takeFirst().at(0).toHash();
float secondValue = arguments[source].toFloat();
float secondValue = arguments[src].toFloat();
QCOMPARE(firstValue, 0.0f);
QVERIFY((secondValue >= temp.first) && (secondValue <= temp.second));
}

View File

@ -37,10 +37,10 @@ private slots:
void test_gputemp();
private:
GPUTemperatureSource *gputempSource = nullptr;
GPUTemperatureSource *source = nullptr;
QString device;
QString source = QString("gpu/temperature");
QPair<float, float> temp = QPair<float, float>(0.0f, 40.0f);
QString src = QString("gpu/temperature");
QPair<float, float> temp = QPair<float, float>(0.0f, 120.0f);
};

View File

@ -60,12 +60,13 @@ void TestHDDTemperatureSource::test_hddtemp()
QSignalSpy spy(hddtempSource,
SIGNAL(dataReceived(const QVariantHash &)));
float firstValue = hddtempSource->data(device).toFloat();
QCOMPARE(firstValue, 0.0f);
QVERIFY(spy.wait(5000));
QVariantHash arguments = spy.takeFirst().at(0).toHash();
device.remove(QString("hdd/temperature"));
float secondValue = arguments[device].toFloat();
QCOMPARE(firstValue, 0.0f);
QVERIFY((secondValue >= temp.first) && (secondValue <= temp.second));
});
}
@ -77,12 +78,13 @@ void TestHDDTemperatureSource::test_smartctl()
QSignalSpy spy(smartctlSource,
SIGNAL(dataReceived(const QVariantHash &)));
float firstValue = smartctlSource->data(device).toFloat();
QCOMPARE(firstValue, 0.0f);
QVERIFY(spy.wait(5000));
QVariantHash arguments = spy.takeFirst().at(0).toHash();
device.remove(QString("hdd/temperature"));
float secondValue = arguments[device].toFloat();
QCOMPARE(firstValue, 0.0f);
QVERIFY((secondValue >= temp.first) && (secondValue <= temp.second));
});
}

View File

@ -43,7 +43,7 @@ private:
QStringList devices;
QString hddtempCmd = QString("sudo hddtemp");
QString smartctlCmd = QString("sudo smartctl -a");
QPair<float, float> temp = QPair<float, float>(0.0f, 40.0f);
QPair<float, float> temp = QPair<float, float>(0.0f, 120.0f);
};