diff --git a/sources/awesomewidgets/extweather.cpp b/sources/awesomewidgets/extweather.cpp index 6fda40b..0aabd44 100644 --- a/sources/awesomewidgets/extweather.cpp +++ b/sources/awesomewidgets/extweather.cpp @@ -134,6 +134,7 @@ void ExtWeather::setCity(const QString _city) qCDebug(LOG_LIB) << "City" << _city; m_city = _city; + initUrl(); } @@ -142,6 +143,7 @@ void ExtWeather::setCountry(const QString _country) qCDebug(LOG_LIB) << "Country" << _country; m_country = _country; + initUrl(); } @@ -185,16 +187,6 @@ void ExtWeather::readConfiguration() } bumpApi(AWEWAPI); - - // init query - m_url = QUrl(YAHOO_WEATHER_URL); - QUrlQuery params; - params.addQueryItem(QString("format"), QString("json")); - params.addQueryItem(QString("env"), - QString("store://datatables.org/alltableswithkeys")); - params.addQueryItem(QString("q"), - QString(YAHOO_WEATHER_QUERY).arg(m_city, m_country)); - m_url.setQuery(params); } @@ -359,6 +351,22 @@ void ExtWeather::weatherReplyReceived(QNetworkReply *reply) values[tag(QString("humidity"))] = 0; values[tag(QString("pressure"))] = 0.0; } + + emit(dataReceived(values)); +} + + +void ExtWeather::initUrl() +{ + // init query + m_url = QUrl(YAHOO_WEATHER_URL); + QUrlQuery params; + params.addQueryItem(QString("format"), QString("json")); + params.addQueryItem(QString("env"), + QString("store://datatables.org/alltableswithkeys")); + params.addQueryItem(QString("q"), + QString(YAHOO_WEATHER_QUERY).arg(m_city, m_country)); + m_url.setQuery(params); } diff --git a/sources/awesomewidgets/extweather.h b/sources/awesomewidgets/extweather.h index f02cebb..6b60583 100644 --- a/sources/awesomewidgets/extweather.h +++ b/sources/awesomewidgets/extweather.h @@ -73,6 +73,7 @@ private: QUrl m_url; bool isRunning = false; Ui::ExtWeather *ui = nullptr; + void initUrl(); void translate(); // properties QString m_city = QString("London"); diff --git a/sources/test/CMakeLists.txt b/sources/test/CMakeLists.txt index 5a0bace..9e0c2f6 100644 --- a/sources/test/CMakeLists.txt +++ b/sources/test/CMakeLists.txt @@ -29,4 +29,9 @@ set(EXTUPGRADE_SOURCES testextupgrade.cpp) add_executable(${SUBPROJECT}-extupgrade ${EXTUPGRADE_HEADERS} ${EXTUPGRADE_SOURCES}) target_link_libraries(${SUBPROJECT}-extupgrade ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) add_test(NAME "ExtUpgrade" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extupgrade) - +# extweather +set(EXTWEATHER_HEADERS testextweather.h) +set(EXTWEATHER_SOURCES testextweather.cpp) +add_executable(${SUBPROJECT}-extweather ${EXTWEATHER_HEADERS} ${EXTWEATHER_SOURCES}) +target_link_libraries(${SUBPROJECT}-extweather ${PROJECT_LIBRARY} ${Qt_LIBRARIES} ${Qt5Test_LIBRARIES}) +add_test(NAME "ExtWeather" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${SUBPROJECT}-extweather) diff --git a/sources/test/testextweather.cpp b/sources/test/testextweather.cpp new file mode 100644 index 0000000..97f8a7f --- /dev/null +++ b/sources/test/testextweather.cpp @@ -0,0 +1,140 @@ +/*************************************************************************** + * 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 "testextweather.h" + +#include + +#include "extweather.h" + + +void TestExtWeather::initTestCase() +{ + extWeather = new ExtWeather(nullptr); + extWeather->setInterval(1); + extWeather->setCity(city); + extWeather->setCountry(country); + extWeather->setNumber(0); + + extWeather->run(); +} + + +void TestExtWeather::cleanupTestCase() +{ + delete extWeather; +} + + +void TestExtWeather::test_values() +{ + QCOMPARE(extWeather->interval(), 1); + QCOMPARE(extWeather->number(), 0); + QCOMPARE(extWeather->city(), city); + QCOMPARE(extWeather->country(), country); +} + + +void TestExtWeather::test_run() +{ + // init spy + QSignalSpy spy(extWeather, SIGNAL(dataReceived(const QVariantHash &))); + QVariantHash firstValue = extWeather->run(); + + // check values + QVERIFY(spy.wait(5000)); + QVariantHash arguments = spy.takeFirst().at(0).toHash(); + QEXPECT_FAIL("", "WeatherID should not be 0", Continue); + QCOMPARE(arguments[extWeather->tag(QString("weatherId"))].toInt(), 0); + QVERIFY((arguments[extWeather->tag(QString("humidity"))].toInt() + > humidity.first) + && (arguments[extWeather->tag(QString("humidity"))].toInt() + < humidity.second)); + QEXPECT_FAIL("", "https://yahoo.uservoice.com/forums/207813-us-weather/" + "suggestions/14209233-invalid-pressure-calculation", + Continue); + QVERIFY((arguments[extWeather->tag(QString("pressure"))].toFloat() + > pressure.first) + && (arguments[extWeather->tag(QString("pressure"))].toInt() + < pressure.second)); + QVERIFY((arguments[extWeather->tag(QString("temperature"))].toFloat() + > temp.first) + && (arguments[extWeather->tag(QString("temperature"))].toInt() + < temp.second)); + // image should be only one symbol here + QCOMPARE(arguments[extWeather->tag(QString("weather"))].toString().count(), + 1); +} + + +void TestExtWeather::test_ts() +{ + extWeather->setTs(1); + // init spy + QSignalSpy spy(extWeather, SIGNAL(dataReceived(const QVariantHash &))); + QVariantHash firstValue = extWeather->run(); + + // check values + QVERIFY(spy.wait(5000)); + QVariantHash arguments = spy.takeFirst().at(0).toHash(); + QEXPECT_FAIL("", "WeatherID should not be 0", Continue); + QCOMPARE(arguments[extWeather->tag(QString("weatherId"))].toInt(), 0); + QCOMPARE(arguments[extWeather->tag(QString("humidity"))].toInt(), 0); + QCOMPARE(arguments[extWeather->tag(QString("pressure"))].toFloat(), 0.0f); + QVERIFY((arguments[extWeather->tag(QString("temperature"))].toFloat() + > temp.first) + && (arguments[extWeather->tag(QString("temperature"))].toInt() + < temp.second)); + // image should be only one symbol here + QCOMPARE(arguments[extWeather->tag(QString("weather"))].toString().count(), + 1); +} + + +void TestExtWeather::test_image() +{ + extWeather->setImage(true); + // init spy + QSignalSpy spy(extWeather, SIGNAL(dataReceived(const QVariantHash &))); + QVariantHash firstValue = extWeather->run(); + + // check values + QVERIFY(spy.wait(5000)); + QVariantHash arguments = spy.takeFirst().at(0).toHash(); + QVERIFY( + arguments[extWeather->tag(QString("weather"))].toString().startsWith( + QString("copy(QString("/dev/null"), 1); + + QCOMPARE(newExtWeather->interval(), extWeather->interval()); + QCOMPARE(newExtWeather->city(), extWeather->city()); + QCOMPARE(newExtWeather->country(), extWeather->country()); + QCOMPARE(newExtWeather->ts(), extWeather->ts()); + QCOMPARE(newExtWeather->image(), extWeather->image()); + QCOMPARE(newExtWeather->number(), 1); + + delete newExtWeather; +} + + +QTEST_MAIN(TestExtWeather); diff --git a/sources/test/testextweather.h b/sources/test/testextweather.h new file mode 100644 index 0000000..1b1638a --- /dev/null +++ b/sources/test/testextweather.h @@ -0,0 +1,56 @@ +/*************************************************************************** + * 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 TESTEXTWEATHER_H +#define TESTEXTWEATHER_H + +#include +#include + + +class ExtWeather; + +class TestExtWeather : public QObject +{ + Q_OBJECT + +private slots: + // initialization + void initTestCase(); + void cleanupTestCase(); + // test + void test_values(); + void test_run(); + void test_ts(); + void test_image(); + void test_copy(); + +private: + ExtWeather *extWeather = nullptr; + QString city = QString("London"); + QString country = QString("uk"); + // humidity is in percents + QPair humidity = QPair(0, 100); + // pressure should be about 1 atm + QPair pressure = QPair(500.0f, 1500.0f); + // dont know about temperature, but I suppose it will be between -40 and 40 + QPair temp = QPair(-40.0f, 40.0f); +}; + + +#endif /* TESTEXTWEATHER_H */