add tests for awkeys

This commit is contained in:
Evgenii Alekseev 2016-06-28 11:19:33 +03:00
parent bee8e2f180
commit 5cd5272f10
7 changed files with 245 additions and 11 deletions

View File

@ -19,6 +19,7 @@
#ifndef AWACTIONS_H #ifndef AWACTIONS_H
#define AWACTIONS_H #define AWACTIONS_H
#include <QMap>
#include <QObject> #include <QObject>

View File

@ -189,10 +189,16 @@ QString AWKeyOperations::infoByKey(QString key) const
{ {
qCDebug(LOG_AW) << "Requested key" << key; qCDebug(LOG_AW) << "Requested key" << key;
QString stripped = key;
stripped.remove(QRegExp(QString("\\d+")));
QString output = QString("(none)");
// FIXME undefined behaviour
if (key.startsWith(QString("bar"))) if (key.startsWith(QString("bar")))
return graphicalItems->itemByTag(key, QString("bar"))->uniq(); return graphicalItems->itemByTag(key, stripped)->uniq();
else if (key.startsWith(QString("custom"))) else if (key.startsWith(QString("custom")))
return extScripts->itemByTag(key, QString("custom"))->uniq(); return extScripts->itemByTag(key, stripped)->uniq();
else if (key.contains(QRegExp(QString("^hdd[rw]")))) else if (key.contains(QRegExp(QString("^hdd[rw]"))))
return QString("%1").arg( return QString("%1").arg(
m_devices[QString("disk")] m_devices[QString("disk")]
@ -213,17 +219,17 @@ QString AWKeyOperations::infoByKey(QString key) const
m_devices[QString("net")] m_devices[QString("net")]
[key.remove(QRegExp(QString("^(down|up)"))).toInt()]); [key.remove(QRegExp(QString("^(down|up)"))).toInt()]);
else if (key.startsWith(QString("pkgcount"))) else if (key.startsWith(QString("pkgcount")))
return extUpgrade->itemByTag(key, QString("pkgcount"))->uniq(); return extUpgrade->itemByTag(key, stripped)->uniq();
else if (key.contains(QRegExp(QString("(^|perc)(ask|bid|price)(chg|)")))) else if (key.contains(QRegExp(QString("(^|perc)(ask|bid|price)(chg|)"))))
return extQuotes->itemByTag(key, QString("ask"))->uniq(); return extQuotes->itemByTag(key, stripped)->uniq();
else if (key.contains(QRegExp( else if (key.contains(QRegExp(
QString("(weather|weatherId|humidity|pressure|temperature)")))) QString("(weather|weatherId|humidity|pressure|temperature)"))))
return extWeather->itemByTag(key, QString("weather"))->uniq(); return extWeather->itemByTag(key, stripped)->uniq();
else if (key.startsWith(QString("temp"))) else if (key.startsWith(QString("temp")))
return QString("%1").arg( return QString("%1").arg(
m_devices[QString("temp")][key.remove(QString("temp")).toInt()]); m_devices[QString("temp")][key.remove(QString("temp")).toInt()]);
return QString("(none)"); return output;
} }

View File

@ -177,8 +177,10 @@ QString AWKeys::valueByKey(QString key) const
{ {
qCDebug(LOG_AW) << "Requested value for key" << key; qCDebug(LOG_AW) << "Requested value for key" << key;
key.remove(QRegExp(QString("^bar[0-9]{1,}"))); QString trueKey
return aggregator->formatter(values[key], key); = key.startsWith(QString("bar")) ? keyOperator->infoByKey(key) : key;
return aggregator->formatter(values[trueKey], trueKey);
} }

View File

@ -30,7 +30,7 @@ set(TEST_MODULES
abstractformatter datetimeformatter floatformatter noformatter scriptformatter abstractformatter datetimeformatter floatformatter noformatter scriptformatter
extitemaggregator extitemaggregator
batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource
awconfighelper awkeycache awpatternfunctions awupdatehelper awconfighelper awkeycache awkeys awpatternfunctions awupdatehelper
dpplugin) dpplugin)
foreach (TEST_MODULE ${TEST_MODULES}) foreach (TEST_MODULE ${TEST_MODULES})
set(${TEST_MODULE}_HEADERS test${TEST_MODULE}.h) set(${TEST_MODULE}_HEADERS test${TEST_MODULE}.h)
@ -39,6 +39,18 @@ foreach (TEST_MODULE ${TEST_MODULES})
set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awconfighelper.cpp) set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awconfighelper.cpp)
elseif (TEST_MODULE MATCHES "awkeycache") elseif (TEST_MODULE MATCHES "awkeycache")
set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeycache.cpp) set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeycache.cpp)
elseif (TEST_MODULE MATCHES "awkeys")
set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awactions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awdataaggregator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awdataengineaggregator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awformatterhelper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeycache.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeyoperations.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeys.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeysaggregator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awpatternfunctions.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awupdatehelper.cpp
${PROJECT_TRDPARTY_DIR}/fontdialog/fontdialog.cpp)
elseif (TEST_MODULE MATCHES "awpatternfunctions") elseif (TEST_MODULE MATCHES "awpatternfunctions")
set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awformatterhelper.cpp set(${TEST_MODULE}_SOURCES ${${TEST_MODULE}_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awformatterhelper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeysaggregator.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../awesome-widget/plugin/awkeysaggregator.cpp

View File

@ -20,8 +20,8 @@
#include <QtTest> #include <QtTest>
#include "awtestlibrary.h"
#include "awconfighelper.h" #include "awconfighelper.h"
#include "awtestlibrary.h"
void TestAWConfigHelper::initTestCase() void TestAWConfigHelper::initTestCase()
@ -55,7 +55,8 @@ void TestAWConfigHelper::test_exportConfiguration()
void TestAWConfigHelper::test_importConfiguration() void TestAWConfigHelper::test_importConfiguration()
{ {
QVariantMap imported = plugin->importConfiguration(filename, true, true, true); QVariantMap imported
= plugin->importConfiguration(filename, true, true, true);
QVariantMap converted; QVariantMap converted;
for (auto key : map.keys()) for (auto key : map.keys())
converted[key] = map.value(key); converted[key] = map.value(key);

161
sources/test/testawkeys.cpp Normal file
View File

@ -0,0 +1,161 @@
/***************************************************************************
* 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 "testawkeys.h"
#include <QtTest>
#include "awkeys.h"
#include "awtestlibrary.h"
void TestAWKeys::initTestCase()
{
plugin = new AWKeys(this);
// tooltip init
QVariantMap tooltipSettings;
tooltipSettings[QString("tooltipNumber")] = 1000;
tooltipSettings[QString("useTooltipBackground")] = true;
tooltipSettings[QString("tooltipBackground")] = QString("#ffffff");
tooltipSettings[QString("cpuTooltip")] = true;
tooltipSettings[QString("cpuclTooltip")] = true;
tooltipSettings[QString("memTooltip")] = true;
tooltipSettings[QString("swapTooltip")] = true;
tooltipSettings[QString("downkbTooltip")] = true;
tooltipSettings[QString("upkbTooltip")] = true;
tooltipSettings[QString("batTooltip")] = true;
tooltipSettings[QString("cpuTooltipColor")] = QString("#ffffff");
tooltipSettings[QString("cpuclTooltipColor")] = QString("#ffffff");
tooltipSettings[QString("memTooltipColor")] = QString("#ffffff");
tooltipSettings[QString("swapTooltipColor")] = QString("#ffffff");
tooltipSettings[QString("downkbTooltipColor")] = QString("#ffffff");
tooltipSettings[QString("upkbTooltipColor")] = QString("#ffffff");
tooltipSettings[QString("batTooltipColor")] = QString("#ffffff");
tooltipSettings[QString("batInTooltipColor")] = QString("#ffffff");
tooltipSettings[QString("acOnline")] = QString("(*)");
tooltipSettings[QString("notify")] = false;
plugin->initDataAggregator(tooltipSettings);
// aggregator init
plugin->setAggregatorProperty("acOffline", QString("( )"));
plugin->setAggregatorProperty("acOnline", QString("(*)"));
plugin->setAggregatorProperty("customTime", QString("$hh"));
plugin->setAggregatorProperty("customUptime", QString("$hh"));
plugin->setAggregatorProperty("tempUnits", QString("Celsius"));
plugin->setAggregatorProperty("translate", false);
plugin->initKeys(pattern, interval, 0, false);
}
void TestAWKeys::cleanupTestCase()
{
delete plugin;
}
void TestAWKeys::test_hddDevices()
{
QVERIFY(!plugin->getHddDevices().isEmpty());
}
void TestAWKeys::test_dictKeys()
{
QStringList keys = plugin->dictKeys();
QVERIFY(!keys.isEmpty());
QStringList sorted = plugin->dictKeys(true);
QVERIFY(!sorted.isEmpty());
QEXPECT_FAIL("", "Sorted and non-sorted lists should differ", Continue);
QCOMPARE(keys, sorted);
pattern = QString("$%1").arg(sorted.join(QString("\n$")));
}
void TestAWKeys::test_pattern()
{
plugin->initKeys(pattern, interval, 0, false);
QSignalSpy spy(plugin, SIGNAL(needTextToBeUpdated(const QString)));
QVERIFY(spy.wait(5 * interval));
QString text = spy.takeFirst().at(0).toString();
QEXPECT_FAIL("", "Pattern should be parsed", Continue);
QCOMPARE(text, pattern);
QStringList keys = plugin->dictKeys(true);
for (auto key : keys)
QVERIFY(!text.contains(key));
}
void TestAWKeys::test_tooltip()
{
QSignalSpy spy(plugin, SIGNAL(needToolTipToBeUpdated(const QString)));
QVERIFY(spy.wait(5 * interval));
QString text = spy.takeFirst().at(0).toString();
QVERIFY(text.startsWith(QString("<img")));
}
void TestAWKeys::test_wrapNewLines()
{
QSignalSpy spy(plugin, SIGNAL(needTextToBeUpdated(const QString)));
QVERIFY(spy.wait(5 * interval));
QString text = spy.takeFirst().at(0).toString();
QVERIFY(!text.contains("<br>") && text.contains("\n"));
plugin->setWrapNewLines(true);
QVERIFY(spy.wait(5 * interval));
text = spy.takeFirst().at(0).toString();
QVERIFY(text.contains("<br>") && !text.contains("\n"));
}
void TestAWKeys::test_infoByKey()
{
int notEmpty = 0;
QStringList keys = plugin->dictKeys(true);
for (auto key : keys) {
QString info = plugin->infoByKey(key);
QVERIFY(!info.isEmpty());
// append non-empty field count
if (info != QString("(none)"))
notEmpty++;
}
QVERIFY(notEmpty > 0);
}
void TestAWKeys::test_valueByKey()
{
int notEmpty = 0;
QStringList keys = plugin->dictKeys(true);
for (auto key : keys) {
if (!plugin->valueByKey(key).isEmpty())
notEmpty++;
}
QVERIFY(notEmpty > 0);
}
QTEST_MAIN(TestAWKeys);

51
sources/test/testawkeys.h Normal file
View File

@ -0,0 +1,51 @@
/***************************************************************************
* 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 TESTAWKEYS_H
#define TESTAWKEYS_H
#include <QObject>
class AWKeys;
class TestAWKeys : public QObject
{
Q_OBJECT
private slots:
// initialization
void initTestCase();
void cleanupTestCase();
// test
void test_hddDevices();
void test_dictKeys();
void test_pattern();
void test_tooltip();
void test_wrapNewLines();
void test_infoByKey();
void test_valueByKey();
private:
AWKeys *plugin = nullptr;
QString pattern;
int interval = 1000;
};
#endif /* TESTAWKEYS_H */