From 5cd5272f1027262b12646293ed1d913c7a4edd9f Mon Sep 17 00:00:00 2001 From: arcan1s Date: Tue, 28 Jun 2016 11:19:33 +0300 Subject: [PATCH] add tests for awkeys --- sources/awesome-widget/plugin/awactions.h | 1 + .../awesome-widget/plugin/awkeyoperations.cpp | 18 +- sources/awesome-widget/plugin/awkeys.cpp | 6 +- sources/test/CMakeLists.txt | 14 +- sources/test/testawconfighelper.cpp | 5 +- sources/test/testawkeys.cpp | 161 ++++++++++++++++++ sources/test/testawkeys.h | 51 ++++++ 7 files changed, 245 insertions(+), 11 deletions(-) create mode 100644 sources/test/testawkeys.cpp create mode 100644 sources/test/testawkeys.h diff --git a/sources/awesome-widget/plugin/awactions.h b/sources/awesome-widget/plugin/awactions.h index 2f14650..7185667 100644 --- a/sources/awesome-widget/plugin/awactions.h +++ b/sources/awesome-widget/plugin/awactions.h @@ -19,6 +19,7 @@ #ifndef AWACTIONS_H #define AWACTIONS_H +#include #include diff --git a/sources/awesome-widget/plugin/awkeyoperations.cpp b/sources/awesome-widget/plugin/awkeyoperations.cpp index 1e23e18..811dff0 100644 --- a/sources/awesome-widget/plugin/awkeyoperations.cpp +++ b/sources/awesome-widget/plugin/awkeyoperations.cpp @@ -189,10 +189,16 @@ QString AWKeyOperations::infoByKey(QString key) const { 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"))) - return graphicalItems->itemByTag(key, QString("bar"))->uniq(); + return graphicalItems->itemByTag(key, stripped)->uniq(); 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]")))) return QString("%1").arg( m_devices[QString("disk")] @@ -213,17 +219,17 @@ QString AWKeyOperations::infoByKey(QString key) const m_devices[QString("net")] [key.remove(QRegExp(QString("^(down|up)"))).toInt()]); 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|)")))) - return extQuotes->itemByTag(key, QString("ask"))->uniq(); + return extQuotes->itemByTag(key, stripped)->uniq(); else if (key.contains(QRegExp( 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"))) return QString("%1").arg( m_devices[QString("temp")][key.remove(QString("temp")).toInt()]); - return QString("(none)"); + return output; } diff --git a/sources/awesome-widget/plugin/awkeys.cpp b/sources/awesome-widget/plugin/awkeys.cpp index cfb9c7b..0ccb090 100644 --- a/sources/awesome-widget/plugin/awkeys.cpp +++ b/sources/awesome-widget/plugin/awkeys.cpp @@ -177,8 +177,10 @@ QString AWKeys::valueByKey(QString key) const { qCDebug(LOG_AW) << "Requested value for key" << key; - key.remove(QRegExp(QString("^bar[0-9]{1,}"))); - return aggregator->formatter(values[key], key); + QString trueKey + = key.startsWith(QString("bar")) ? keyOperator->infoByKey(key) : key; + + return aggregator->formatter(values[trueKey], trueKey); } diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index a21acad..0197ad9 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -30,7 +30,7 @@ set(TEST_MODULES abstractformatter datetimeformatter floatformatter noformatter scriptformatter extitemaggregator batterysource desktopsource gpuloadsource gputempsource hddtempsource networksource playersource processessource - awconfighelper awkeycache awpatternfunctions awupdatehelper + awconfighelper awkeycache awkeys awpatternfunctions awupdatehelper dpplugin) foreach (TEST_MODULE ${TEST_MODULES}) 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) elseif (TEST_MODULE MATCHES "awkeycache") 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") 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 diff --git a/sources/test/testawconfighelper.cpp b/sources/test/testawconfighelper.cpp index 0f7c3a7..ac9716e 100644 --- a/sources/test/testawconfighelper.cpp +++ b/sources/test/testawconfighelper.cpp @@ -20,8 +20,8 @@ #include -#include "awtestlibrary.h" #include "awconfighelper.h" +#include "awtestlibrary.h" void TestAWConfigHelper::initTestCase() @@ -55,7 +55,8 @@ void TestAWConfigHelper::test_exportConfiguration() void TestAWConfigHelper::test_importConfiguration() { - QVariantMap imported = plugin->importConfiguration(filename, true, true, true); + QVariantMap imported + = plugin->importConfiguration(filename, true, true, true); QVariantMap converted; for (auto key : map.keys()) converted[key] = map.value(key); diff --git a/sources/test/testawkeys.cpp b/sources/test/testawkeys.cpp new file mode 100644 index 0000000..b20acc6 --- /dev/null +++ b/sources/test/testawkeys.cpp @@ -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 + +#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("") && text.contains("\n")); + + plugin->setWrapNewLines(true); + QVERIFY(spy.wait(5 * interval)); + text = spy.takeFirst().at(0).toString(); + QVERIFY(text.contains("
") && !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); diff --git a/sources/test/testawkeys.h b/sources/test/testawkeys.h new file mode 100644 index 0000000..3c5a2fd --- /dev/null +++ b/sources/test/testawkeys.h @@ -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 + + +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 */