From 54d5f680a4cfce6b6f5d0c0e5576ea6ecfb9fa7c Mon Sep 17 00:00:00 2001 From: Jussi Laitinen Date: Thu, 1 Apr 2010 13:09:36 +0300 Subject: [PATCH] Created testmapfetcher.pro file. --- src/map/mapfetcher.h | 7 +- tests/testmap/networkaccessmanagermock.cpp | 112 ----------- tests/testmap/networkaccessmanagermock.h | 54 ------ tests/testmap/networkreplydummy.cpp | 22 --- tests/testmap/networkreplymock.cpp | 84 --------- tests/testmap/networkreplymock.h | 57 ------ tests/testmap/testUIMapFetcher.cpp | 3 - tests/testmap/testmapengine.cpp | 55 ------ tests/testmap/testmapfetcher.cpp | 186 ------------------- .../testmapfetcher/networkaccessmanagermock.cpp | 131 +++++++++++++ .../testmapfetcher/networkaccessmanagermock.h | 58 ++++++ tests/testmap/testmapfetcher/networkreplymock.cpp | 84 +++++++++ tests/testmap/testmapfetcher/networkreplymock.h | 57 ++++++ tests/testmap/testmapfetcher/testmapfetcher.cpp | 195 ++++++++++++++++++++ tests/testmap/testmapfetcher/testmapfetcher.pro | 10 + tests/testui/testUI.pro | 13 -- tests/testui/testui.cpp | 47 ----- 17 files changed, 539 insertions(+), 636 deletions(-) delete mode 100644 tests/testmap/networkaccessmanagermock.cpp delete mode 100644 tests/testmap/networkaccessmanagermock.h delete mode 100644 tests/testmap/networkreplydummy.cpp delete mode 100644 tests/testmap/networkreplymock.cpp delete mode 100644 tests/testmap/networkreplymock.h delete mode 100644 tests/testmap/testUIMapFetcher.cpp delete mode 100644 tests/testmap/testmapengine.cpp delete mode 100644 tests/testmap/testmapfetcher.cpp create mode 100644 tests/testmap/testmapfetcher/networkaccessmanagermock.cpp create mode 100644 tests/testmap/testmapfetcher/networkaccessmanagermock.h create mode 100644 tests/testmap/testmapfetcher/networkreplymock.cpp create mode 100644 tests/testmap/testmapfetcher/networkreplymock.h create mode 100644 tests/testmap/testmapfetcher/testmapfetcher.cpp create mode 100644 tests/testmap/testmapfetcher/testmapfetcher.pro delete mode 100644 tests/testui/testUI.pro delete mode 100755 tests/testui/testui.cpp diff --git a/src/map/mapfetcher.h b/src/map/mapfetcher.h index f39fa39..9aab8d8 100644 --- a/src/map/mapfetcher.h +++ b/src/map/mapfetcher.h @@ -25,11 +25,11 @@ #include #include +#include "mapfetcher.h" + class QNetworkReply; class QUrl; -#include "mapfetcher.h" - /** * @brief MapFetcher handles requests to get map tiles. * @@ -92,13 +92,14 @@ public slots: void downloadFinished(QNetworkReply *reply); /** - * @brief This slot is called when + * @brief This slot is called when next download is started. * * @fn startNextDownload */ void startNextDownload(); private: + QNetworkAccessManager *m_manager; QList currentDownloads; QQueue downloadQueue; diff --git a/tests/testmap/networkaccessmanagermock.cpp b/tests/testmap/networkaccessmanagermock.cpp deleted file mode 100644 index 315873c..0000000 --- a/tests/testmap/networkaccessmanagermock.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* - Situare - A location system for Facebook - Copyright (C) 2010 Ixonos Plc. Authors: - - Jussi Laitinen - jussi.laitinen@ixonos.com - - Situare is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - Situare 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 Situare; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - USA. -*/ - -#include -#include -#include -#include -#include -#include "networkaccessmanagermock.h" -#include "networkreplymock.h" - -NetworkAccessManagerMock::NetworkAccessManagerMock(QNetworkAccessManager *manager, QObject *parent) - : QNetworkAccessManager(parent) -{ - qDebug() << __PRETTY_FUNCTION__; - setCache(manager->cache()); - setCookieJar(manager->cookieJar()); - //setProxy(manager->proxy()); - setProxyFactory(manager->proxyFactory()); - - //connect(this, SIGNAL(error(QNetworkRequest*)), this, SLOT(generateError(QNetworkRequest))); -} - -void NetworkAccessManagerMock::setMode(int mode) -{ - this->mode = mode; -} - -NetworkReplyMock *NetworkAccessManagerMock::get(const QNetworkRequest &request) -{ - qDebug() << __PRETTY_FUNCTION__; - this->request = request; - reply = new NetworkReplyMock(this); - - //QTimer::singleShot(500, this, SLOT(generateError())); - - //QTimer::singleShot(500, this, SLOT(generateCorrectReply())); - - //QTimer::singleShot(0, this, SLOT(generateDelayedCorrectReply())); - - QTimer::singleShot(0, this, SLOT(generateCorruptedImageReply())); - - return reply; -} - -void NetworkAccessManagerMock::generateError() -{ - qDebug() << __PRETTY_FUNCTION__; - reply->setUrl(request.url()); - reply->setError(QNetworkReply::HostNotFoundError, "Host not found"); - emit finished(reply); -} - -void NetworkAccessManagerMock::generateCorrectReply() -{ - qDebug() << __PRETTY_FUNCTION__; - reply->setUrl(request.url()); - QImage image; - if (!image.load("res/images/static/testTile.png", 0)) - image = QImage(); - else - qDebug() << "image loaded"; - - QByteArray array; - QBuffer buffer(&array); - buffer.open(QBuffer::WriteOnly); - if (image.save(&buffer, "PNG")) - qDebug() << "image saved"; - buffer.close(); - reply->setData(array); - emit finished(reply); -} - -void NetworkAccessManagerMock::generateDelayedCorrectReply() -{ - qDebug() << __PRETTY_FUNCTION__; - QTimer::singleShot(4000, this, SLOT(generateCorrectReply())); -} - -void NetworkAccessManagerMock::generateCorruptedImageReply() -{ - qDebug() << __PRETTY_FUNCTION__; - reply->setUrl(request.url()); - QImage image; - if (!image.load("res/images/static/testTile.png", 0)) - image = QImage(); - else - qDebug() << "image loaded"; - - QByteArray array; - array.append("PNG.?ASDSADSDSADSAD"); - reply->setData(array); - emit finished(reply); -} diff --git a/tests/testmap/networkaccessmanagermock.h b/tests/testmap/networkaccessmanagermock.h deleted file mode 100644 index 52b496a..0000000 --- a/tests/testmap/networkaccessmanagermock.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - Situare - A location system for Facebook - Copyright (C) 2010 Ixonos Plc. Authors: - - Jussi Laitinen - jussi.laitinen@ixonos.com - - Situare is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - Situare 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 Situare; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - USA. -*/ - -#ifndef NETWORKACCESSMANAGERMOCK_H -#define NETWORKACCESSMANAGERMOCK_H - -#include -#include "networkreplymock.h" - -class NetworkAccessManagerMock : public QNetworkAccessManager -{ - Q_OBJECT - -public: - NetworkAccessManagerMock(QNetworkAccessManager *manager = 0, QObject *parent = 0); - - NetworkReplyMock *get(const QNetworkRequest &request); - - void setMode(int mode); - -signals: - void finished(NetworkReplyMock *reply); - -private slots: - void generateError(); - void generateCorrectReply(); - void generateDelayedCorrectReply(); - void generateCorruptedImageReply(); - -private: - NetworkReplyMock *reply; - QNetworkRequest request; - int mode; -}; - -#endif // NETWORKACCESSMANAGERMOCK_H diff --git a/tests/testmap/networkreplydummy.cpp b/tests/testmap/networkreplydummy.cpp deleted file mode 100644 index 1498cdd..0000000 --- a/tests/testmap/networkreplydummy.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - Situare - A location system for Facebook - Copyright (C) 2010 Ixonos Plc. Authors: - - Jussi Laitinen - jussi.laitinen@ixonos.com - - Situare is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - Situare 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 Situare; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - USA. -*/ - -#include diff --git a/tests/testmap/networkreplymock.cpp b/tests/testmap/networkreplymock.cpp deleted file mode 100644 index 26af540..0000000 --- a/tests/testmap/networkreplymock.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/* - Situare - A location system for Facebook - Copyright (C) 2010 Ixonos Plc. Authors: - - Jussi Laitinen - jussi.laitinen@ixonos.com - - Situare is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - Situare 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 Situare; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - USA. -*/ - -#include -#include - -#include "networkreplymock.h" - -NetworkReplyMock::NetworkReplyMock(QObject *parent) - : QNetworkReply(parent) - , m_offset(0) - -{ - -} - -void NetworkReplyMock::setError(NetworkError errorCode, const QString &errorString) -{ - qDebug() << __PRETTY_FUNCTION__; - QNetworkReply::setError(errorCode, errorString); -} - - -qint64 NetworkReplyMock::readData(char *data, qint64 maxLen) -{ - qDebug() << __PRETTY_FUNCTION__; - if (m_offset < m_content.size()) { - qint64 number = qMin(maxLen, m_content.size() - m_offset); - memcpy(data, m_content.constData() + m_offset, number); - m_offset += number; - return number; - } - else { - return -1; - } -} - -QByteArray NetworkReplyMock::readAll() -{ - return m_content; -} - -void NetworkReplyMock::setUrl(const QUrl &url) -{ - qDebug() << __PRETTY_FUNCTION__; - QNetworkReply::setUrl(url); -} - -void NetworkReplyMock::abort() -{ - qDebug() << __PRETTY_FUNCTION__; -} - -void NetworkReplyMock::test() -{ - qDebug() << __PRETTY_FUNCTION__; -} - -void NetworkReplyMock::setData(const QByteArray &content) -{ - qDebug() << __PRETTY_FUNCTION__; - setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png")); - setHeader(QNetworkRequest::ContentLengthHeader, QVariant(this->m_content.size())); - m_content.append(content); - qDebug() << "Content size: " << m_content.size(); -} diff --git a/tests/testmap/networkreplymock.h b/tests/testmap/networkreplymock.h deleted file mode 100644 index 54c81ee..0000000 --- a/tests/testmap/networkreplymock.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - Situare - A location system for Facebook - Copyright (C) 2010 Ixonos Plc. Authors: - - Jussi Laitinen - jussi.laitinen@ixonos.com - - Situare is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - Situare 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 Situare; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - USA. -*/ - -#ifndef NETWORKREPLYMOCK_H -#define NETWORKREPLYMOCK_H - -#include -#include - -class NetworkReplyMock : public QNetworkReply -{ - Q_OBJECT - -public: - NetworkReplyMock(QObject *parent = 0); - void abort(); - qint64 bytesAvailable() const - { - return m_content.size() - m_offset; - } - bool isSequential() const - { - return true; - } - void setError(NetworkError errorCode, const QString &errorString); - void setData(const QByteArray &content); - void setUrl(const QUrl &url); - void test(); - QByteArray readAll(); - -protected: - qint64 readData(char *data, qint64 maxlen); - -private: - QByteArray m_content; - qint64 m_offset; -}; - -#endif // NETWORKREPLYMOCK_H diff --git a/tests/testmap/testUIMapFetcher.cpp b/tests/testmap/testUIMapFetcher.cpp deleted file mode 100644 index 827a65a..0000000 --- a/tests/testmap/testUIMapFetcher.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include - - diff --git a/tests/testmap/testmapengine.cpp b/tests/testmap/testmapengine.cpp deleted file mode 100644 index bae45d1..0000000 --- a/tests/testmap/testmapengine.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include - -#include "mapengine.h" - -/** -* @brief Test class for MapEngine. -* -* @class TestMapEngine testmapengine.cpp "tests/testmapengine.cpp" -*/ -class TestMapEngine : public QObject -{ - Q_OBJECT -public: - TestMapEngine(); - -private slots: - void coordinatesToTiles(); - void longitudeFromTiles(); - void latitudeFromTiles(); - -private: - MapEngine *mapEngine; -}; - -TestMapEngine::TestMapEngine() -{ - mapEngine = new MapEngine(); -} - - -void TestMapEngine::coordinatesToTiles() -{ - QPoint point1 = mapEngine->latLonToTile(47.629, 7.262, 1); - QCOMPARE(point1, QPoint(1, 0)); - QPoint point2 = mapEngine->latLonToTile(65.013379, 25.472059, 13); - QCOMPARE(point2, QPoint(4675, 2131)); - QPoint point3 = mapEngine->latLonToTile(65.013379, 25.472059, 10); - QCOMPARE(point3, QPoint(584, 266)); -} - -void TestMapEngine::longitudeFromTiles() -{ - int longitude = (int)mapEngine->tileXToLongitude(4675, 13); - QCOMPARE(longitude, 25); -} - -void TestMapEngine::latitudeFromTiles() -{ - -} - -QTEST_MAIN(TestMapEngine) -#include "testmapengine.moc" diff --git a/tests/testmap/testmapfetcher.cpp b/tests/testmap/testmapfetcher.cpp deleted file mode 100644 index 205b2eb..0000000 --- a/tests/testmap/testmapfetcher.cpp +++ /dev/null @@ -1,186 +0,0 @@ -/* - Situare - A location system for Facebook - Copyright (C) 2010 Ixonos Plc. Authors: - - Jussi Laitinen - jussi.laitinen@ixonos.com - - Situare is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - Situare 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 Situare; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - USA. -*/ - -#include -#include -#include -#include -#include - -#include "mapfetcher.h" -#include "networkaccessmanagermock.h" - -/** -* @brief -* -* @class TestMapFetcher testmapfetcher.cpp "tests/testMap/testmapfetcher.cpp" -*/ -class TestMapFetcher : public QObject -{ - Q_OBJECT -public: - /** - * @brief TestMapFetcher is a test class for MapFetcher class. - * - * @fn TestMapFetcher - */ - TestMapFetcher(); - - -private slots: - /** - * @brief Tests fetchImage method with empty URL as parameter. - * - * @fn testFetchImageEmptyURL - */ - void testFetchImageEmptyURL(); - /** - * @brief Tests fetchImage method with incorrect URL as parameter. - * - * @fn testFetchImageIncorrectURL - */ - void testFetchImageIncorrectURL(); - /** - * @brief Tests fetchImage method with correct URL as parameter. - * - * @fn testFetchImageCorrectURL - */ - void testFetchImageCorrectURL(); - - void testFetchCorruptedImage(); - - /** - * @brief Tests fetchImage method 20 times with correct URLs - * as parameters. - * - * @fn testFetchImage20URLs - */ - void testFetchImage20URLs(); - /** - * @brief Tests fetchImage method 50 times with correct URLs - * as parameters. - * - * @fn testFetchImage50URLs - */ - void testFetchImage50URLs(); - -private: - MapFetcher *mapFetcher; -}; - -TestMapFetcher::TestMapFetcher() -{ - QNetworkAccessManager *manager = new QNetworkAccessManager(); - NetworkAccessManagerMock *managerMock = new NetworkAccessManagerMock(manager, this); - mapFetcher = new MapFetcher(this, managerMock); -} - -void TestMapFetcher::testFetchImageEmptyURL() -{ - QUrl url(""); - mapFetcher->fetchMapImage(url); -} - -void TestMapFetcher::testFetchImageIncorrectURL() -{ -// QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString))); -// -// QVERIFY(imageReceivedErrorSpy.isValid()); -// -// //Incorrect URL -// QUrl url1("http://tile.openstreetmap.org/7/63/22.gi"); -// qDebug() << QTime::currentTime().toString(); -// mapFetcher->fetchMapImage(url1); -// QTest::qWait(5000); -// QCOMPARE(imageReceivedErrorSpy.count(), 1); -} - -void TestMapFetcher::testFetchImageCorrectURL() -{ - QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); - - QVERIFY(imageReceivedSpy.isValid()); - - //Correct URL - QUrl url2("http://tile.openstreetmap.org/7/63/42.png"); - mapFetcher->fetchMapImage(url2); - QTest::qWait(2000); - QCOMPARE(imageReceivedSpy.count(), 1); - QList signalArgs2 = imageReceivedSpy.takeLast(); - QCOMPARE(url2, signalArgs2.at(0).toUrl()); -} - -void TestMapFetcher::testFetchCorruptedImage() -{ -// QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); -// -// QVERIFY(imageReceivedSpy.isValid()); -// -// //Correct URL -// QUrl url2("http://tile.openstreetmap.org/7/63/1.png"); -// mapFetcher->fetchMapImage(url2); -// QTest::qWait(1000); -// QCOMPARE(imageReceivedSpy.count(), 1); -// QList signalArgs2 = imageReceivedSpy.takeLast(); -// QCOMPARE(url2, signalArgs2.at(0).toUrl()); -} - -void TestMapFetcher::testFetchImage20URLs() -{ -// QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); -// QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString))); -// -// QVERIFY(imageReceivedSpy.isValid()); -// -// //20 requests -// for (int i = 1; i < 3; ++i) { -// for (int j = 0; j < 10; ++j) { -// QUrl url(QString("http://tile.openstreetmap.org/18/23/%1%2.png").arg(i).arg(j)); -// mapFetcher->fetchMapImage(url); -// } -// } -// QTest::qWait(2000); -// int totalCount = imageReceivedSpy.count() + imageReceivedErrorSpy.count(); -// QCOMPARE(totalCount, 20); -} - -void TestMapFetcher::testFetchImage50URLs() -{ -// QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); -// QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString))); -// -// QVERIFY(imageReceivedSpy.isValid()); -// -// //50 requests -// for (int i = 1; i < 6; ++i) { -// for (int j = 0; j < 10; ++j) { -// QUrl url(QString("http://tile.openstreetmap.org/18/24/%1%2.png").arg(i).arg(j)); -// mapFetcher->fetchMapImage(url); -// } -// } -// -// QTest::qWait(5000); -// int totalCount = imageReceivedSpy.count() + imageReceivedErrorSpy.count(); -// QCOMPARE(totalCount, 50); -} - -QTEST_MAIN(TestMapFetcher) -#include "testmapfetcher.moc" diff --git a/tests/testmap/testmapfetcher/networkaccessmanagermock.cpp b/tests/testmap/testmapfetcher/networkaccessmanagermock.cpp new file mode 100644 index 0000000..0a998c8 --- /dev/null +++ b/tests/testmap/testmapfetcher/networkaccessmanagermock.cpp @@ -0,0 +1,131 @@ +/* + Situare - A location system for Facebook + Copyright (C) 2010 Ixonos Plc. Authors: + + Jussi Laitinen - jussi.laitinen@ixonos.com + + Situare is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 2 as published by the Free Software Foundation. + + Situare 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 Situare; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + USA. +*/ + +#include +#include +#include +#include +#include +#include "networkaccessmanagermock.h" +#include "networkreplymock.h" + + + +NetworkAccessManagerMock::NetworkAccessManagerMock(QNetworkAccessManager *manager, QObject *parent) + : QNetworkAccessManager(parent) + , mode(0) +{ + qDebug() << __PRETTY_FUNCTION__; + setCache(manager->cache()); + setCookieJar(manager->cookieJar()); + //setProxy(manager->proxy()); + setProxyFactory(manager->proxyFactory()); + + //connect(this, SIGNAL(error(QNetworkRequest*)), this, SLOT(generateError(QNetworkRequest))); +} + +void NetworkAccessManagerMock::setMode(int mode) +{ + this->mode = mode; +} + +void NetworkAccessManagerMock::debug() +{ + qDebug() << __PRETTY_FUNCTION__; +} + +NetworkReplyMock *NetworkAccessManagerMock::get(const QNetworkRequest &request) +{ + qDebug() << __PRETTY_FUNCTION__; + this->request = request; + reply = new NetworkReplyMock(this); + + switch (this->mode) { + + case NetworkAccessManagerMock::CORRECT: + QTimer::singleShot(0, this, SLOT(generateCorrectReply())); + break; + case NetworkAccessManagerMock::INCORRECT: + QTimer::singleShot(0, this, SLOT(generateError())); + break; + case NetworkAccessManagerMock::DELAYED_CORRECT: + QTimer::singleShot(0, this, SLOT(generateDelayedCorrectReply())); + break; + case NetworkAccessManagerMock::CORRUPTED_IMAGE: + QTimer::singleShot(0, this, SLOT(generateCorruptedImageReply())); + break; + default: + QTimer::singleShot(0, this, SLOT(generateCorrectReply())); + break; + } + + return reply; +} + +void NetworkAccessManagerMock::generateError() +{ + qDebug() << __PRETTY_FUNCTION__; + reply->setUrl(request.url()); + reply->setError(QNetworkReply::HostNotFoundError, "Host not found"); + emit finished(reply); +} + +void NetworkAccessManagerMock::generateCorrectReply() +{ + qDebug() << __PRETTY_FUNCTION__; + reply->setUrl(request.url()); + QImage image; + if (!image.load("res/images/static/testTile.png", 0)) + image = QImage(); + else + qDebug() << "image loaded"; + + QByteArray array; + QBuffer buffer(&array); + buffer.open(QBuffer::WriteOnly); + if (image.save(&buffer, "PNG")) + qDebug() << "image saved"; + buffer.close(); + reply->setData(array); + emit finished(reply); +} + +void NetworkAccessManagerMock::generateDelayedCorrectReply() +{ + qDebug() << __PRETTY_FUNCTION__; + QTimer::singleShot(4000, this, SLOT(generateCorrectReply())); +} + +void NetworkAccessManagerMock::generateCorruptedImageReply() +{ + qDebug() << __PRETTY_FUNCTION__; + reply->setUrl(request.url()); + QImage image; + if (!image.load("res/images/static/testTile.png", 0)) + image = QImage(); + else + qDebug() << "image loaded"; + + QByteArray array; + array.append("PNG.?ASDSADSDSADSAD"); + reply->setData(array); + emit finished(reply); +} diff --git a/tests/testmap/testmapfetcher/networkaccessmanagermock.h b/tests/testmap/testmapfetcher/networkaccessmanagermock.h new file mode 100644 index 0000000..7e3c6ca --- /dev/null +++ b/tests/testmap/testmapfetcher/networkaccessmanagermock.h @@ -0,0 +1,58 @@ +/* + Situare - A location system for Facebook + Copyright (C) 2010 Ixonos Plc. Authors: + + Jussi Laitinen - jussi.laitinen@ixonos.com + + Situare is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 2 as published by the Free Software Foundation. + + Situare 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 Situare; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + USA. +*/ + +#ifndef NETWORKACCESSMANAGERMOCK_H +#define NETWORKACCESSMANAGERMOCK_H + +#include +#include "networkreplymock.h" + +class NetworkAccessManagerMock : public QNetworkAccessManager +{ + Q_OBJECT + +public: + NetworkAccessManagerMock(QNetworkAccessManager *manager = 0, QObject *parent = 0); + + NetworkReplyMock *get(const QNetworkRequest &request); + + void setMode(int mode); + + void debug(); + + enum {CORRECT, INCORRECT, DELAYED_CORRECT, CORRUPTED_IMAGE}; + +signals: + void finished(NetworkReplyMock *reply); + +private slots: + void generateError(); + void generateCorrectReply(); + void generateDelayedCorrectReply(); + void generateCorruptedImageReply(); + +private: + NetworkReplyMock *reply; + QNetworkRequest request; + int mode; +}; + +#endif // NETWORKACCESSMANAGERMOCK_H diff --git a/tests/testmap/testmapfetcher/networkreplymock.cpp b/tests/testmap/testmapfetcher/networkreplymock.cpp new file mode 100644 index 0000000..26af540 --- /dev/null +++ b/tests/testmap/testmapfetcher/networkreplymock.cpp @@ -0,0 +1,84 @@ +/* + Situare - A location system for Facebook + Copyright (C) 2010 Ixonos Plc. Authors: + + Jussi Laitinen - jussi.laitinen@ixonos.com + + Situare is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 2 as published by the Free Software Foundation. + + Situare 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 Situare; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + USA. +*/ + +#include +#include + +#include "networkreplymock.h" + +NetworkReplyMock::NetworkReplyMock(QObject *parent) + : QNetworkReply(parent) + , m_offset(0) + +{ + +} + +void NetworkReplyMock::setError(NetworkError errorCode, const QString &errorString) +{ + qDebug() << __PRETTY_FUNCTION__; + QNetworkReply::setError(errorCode, errorString); +} + + +qint64 NetworkReplyMock::readData(char *data, qint64 maxLen) +{ + qDebug() << __PRETTY_FUNCTION__; + if (m_offset < m_content.size()) { + qint64 number = qMin(maxLen, m_content.size() - m_offset); + memcpy(data, m_content.constData() + m_offset, number); + m_offset += number; + return number; + } + else { + return -1; + } +} + +QByteArray NetworkReplyMock::readAll() +{ + return m_content; +} + +void NetworkReplyMock::setUrl(const QUrl &url) +{ + qDebug() << __PRETTY_FUNCTION__; + QNetworkReply::setUrl(url); +} + +void NetworkReplyMock::abort() +{ + qDebug() << __PRETTY_FUNCTION__; +} + +void NetworkReplyMock::test() +{ + qDebug() << __PRETTY_FUNCTION__; +} + +void NetworkReplyMock::setData(const QByteArray &content) +{ + qDebug() << __PRETTY_FUNCTION__; + setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png")); + setHeader(QNetworkRequest::ContentLengthHeader, QVariant(this->m_content.size())); + m_content.append(content); + qDebug() << "Content size: " << m_content.size(); +} diff --git a/tests/testmap/testmapfetcher/networkreplymock.h b/tests/testmap/testmapfetcher/networkreplymock.h new file mode 100644 index 0000000..54c81ee --- /dev/null +++ b/tests/testmap/testmapfetcher/networkreplymock.h @@ -0,0 +1,57 @@ +/* + Situare - A location system for Facebook + Copyright (C) 2010 Ixonos Plc. Authors: + + Jussi Laitinen - jussi.laitinen@ixonos.com + + Situare is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 2 as published by the Free Software Foundation. + + Situare 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 Situare; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + USA. +*/ + +#ifndef NETWORKREPLYMOCK_H +#define NETWORKREPLYMOCK_H + +#include +#include + +class NetworkReplyMock : public QNetworkReply +{ + Q_OBJECT + +public: + NetworkReplyMock(QObject *parent = 0); + void abort(); + qint64 bytesAvailable() const + { + return m_content.size() - m_offset; + } + bool isSequential() const + { + return true; + } + void setError(NetworkError errorCode, const QString &errorString); + void setData(const QByteArray &content); + void setUrl(const QUrl &url); + void test(); + QByteArray readAll(); + +protected: + qint64 readData(char *data, qint64 maxlen); + +private: + QByteArray m_content; + qint64 m_offset; +}; + +#endif // NETWORKREPLYMOCK_H diff --git a/tests/testmap/testmapfetcher/testmapfetcher.cpp b/tests/testmap/testmapfetcher/testmapfetcher.cpp new file mode 100644 index 0000000..b2008fc --- /dev/null +++ b/tests/testmap/testmapfetcher/testmapfetcher.cpp @@ -0,0 +1,195 @@ +/* + Situare - A location system for Facebook + Copyright (C) 2010 Ixonos Plc. Authors: + + Jussi Laitinen - jussi.laitinen@ixonos.com + + Situare is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 2 as published by the Free Software Foundation. + + Situare 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 Situare; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + USA. +*/ + +#include +#include +#include +#include +#include + +#include "../../../src/map/mapfetcher.h" +#include "networkaccessmanagermock.h" +#include "networkreplymock.h" + +/** +* @brief +* +* @class TestMapFetcher testmapfetcher.cpp "tests/testMap/testmapfetcher.cpp" +*/ +class TestMapFetcher : public QObject +{ + Q_OBJECT +public: + /** + * @brief TestMapFetcher is a test class for MapFetcher class. + * + * @fn TestMapFetcher + */ + TestMapFetcher(); + + +private slots: + /** + * @brief Tests fetchImage method with empty URL as parameter. + * + * @fn testFetchImageEmptyURL + */ + void testFetchImageEmptyURL(); + /** + * @brief Tests fetchImage method with incorrect URL as parameter. + * + * @fn testFetchImageIncorrectURL + */ + void testFetchImageIncorrectURL(); + /** + * @brief Tests fetchImage method with correct URL as parameter. + * + * @fn testFetchImageCorrectURL + */ + void testFetchImageCorrectURL(); + + void testFetchCorruptedImage(); + + /** + * @brief Tests fetchImage method 20 times with correct URLs + * as parameters. + * + * @fn testFetchImage20URLs + */ + void testFetchImage20URLs(); + /** + * @brief Tests fetchImage method 50 times with correct URLs + * as parameters. + * + * @fn testFetchImage50URLs + */ + void testFetchImage50URLs(); + +private: + MapFetcher *mapFetcher; + NetworkAccessManagerMock *managerMock; +}; + +TestMapFetcher::TestMapFetcher() +{ + QNetworkAccessManager *manager = new QNetworkAccessManager(); + managerMock = new NetworkAccessManagerMock(manager, this); + mapFetcher = new MapFetcher(this, managerMock); +} + +void TestMapFetcher::testFetchImageEmptyURL() +{ + QUrl url(""); + mapFetcher->fetchMapImage(url); +} + +void TestMapFetcher::testFetchImageIncorrectURL() +{ + managerMock->setMode(NetworkAccessManagerMock::INCORRECT); + + QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString))); + + QVERIFY(imageReceivedErrorSpy.isValid()); + + //Incorrect URL + QUrl url1("http://tile.openstreetmap.org/7/63/22.gi"); + mapFetcher->fetchMapImage(url1); + QTest::qWait(1000); + QCOMPARE(imageReceivedErrorSpy.count(), 1); +} + +void TestMapFetcher::testFetchImageCorrectURL() +{ + managerMock->setMode(NetworkAccessManagerMock::CORRECT); + + QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); + + QVERIFY(imageReceivedSpy.isValid()); + + //Correct URL + QUrl url2("http://tile.openstreetmap.org/7/63/42.png"); + mapFetcher->fetchMapImage(url2); + QTest::qWait(2000); + QCOMPARE(imageReceivedSpy.count(), 1); + QList signalArgs2 = imageReceivedSpy.takeLast(); + QCOMPARE(url2, signalArgs2.at(0).toUrl()); +} + +void TestMapFetcher::testFetchCorruptedImage() +{ + QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); + + QVERIFY(imageReceivedSpy.isValid()); + + //Correct URL + QUrl url2("http://tile.openstreetmap.org/7/63/1.png"); + mapFetcher->fetchMapImage(url2); + QTest::qWait(1000); + QCOMPARE(imageReceivedSpy.count(), 1); + QList signalArgs2 = imageReceivedSpy.takeLast(); + QCOMPARE(url2, signalArgs2.at(0).toUrl()); +} + +void TestMapFetcher::testFetchImage20URLs() +{ + managerMock->setMode(NetworkAccessManagerMock::CORRECT); + + QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); + QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString))); + + QVERIFY(imageReceivedSpy.isValid()); + + //20 requests + for (int i = 1; i < 3; ++i) { + for (int j = 0; j < 10; ++j) { + QUrl url(QString("http://tile.openstreetmap.org/18/23/%1%2.png").arg(i).arg(j)); + mapFetcher->fetchMapImage(url); + } + } + QTest::qWait(2000); + int totalCount = imageReceivedSpy.count() + imageReceivedErrorSpy.count(); + QCOMPARE(totalCount, 20); +} + +void TestMapFetcher::testFetchImage50URLs() +{ + managerMock->setMode(NetworkAccessManagerMock::CORRECT); + + QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); + QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString))); + + QVERIFY(imageReceivedSpy.isValid()); + + //50 requests + for (int i = 1; i < 6; ++i) { + for (int j = 0; j < 10; ++j) { + QUrl url(QString("http://tile.openstreetmap.org/18/24/%1%2.png").arg(i).arg(j)); + mapFetcher->fetchMapImage(url); + } + } + + QTest::qWait(5000); + int totalCount = imageReceivedSpy.count() + imageReceivedErrorSpy.count(); + QCOMPARE(totalCount, 50); +} + +QTEST_MAIN(TestMapFetcher) +#include "testmapfetcher.moc" diff --git a/tests/testmap/testmapfetcher/testmapfetcher.pro b/tests/testmap/testmapfetcher/testmapfetcher.pro new file mode 100644 index 0000000..d0044d1 --- /dev/null +++ b/tests/testmap/testmapfetcher/testmapfetcher.pro @@ -0,0 +1,10 @@ +QT += network testlib + +HEADERS += networkreplymock.h \ + networkaccessmanagermock.h \ + ../../../src/map/mapfetcher.h +SOURCES += testmapfetcher.cpp \ + networkreplymock.cpp \ + networkreplydummy.cpp \ + networkaccessmanagermock.cpp \ + ../../../src/map/mapfetcher.cpp diff --git a/tests/testui/testUI.pro b/tests/testui/testUI.pro deleted file mode 100644 index 405e8f4..0000000 --- a/tests/testui/testUI.pro +++ /dev/null @@ -1,13 +0,0 @@ -###################################################################### -# Automatically generated by qmake (2.01a) Fri Mar 26 11:22:18 2010 -###################################################################### - -CONFIG += qtestlib -TEMPLATE = app -TARGET = -DEPENDPATH += . -INCLUDEPATH += . - -# Input -HEADERS += ../../src/ui/mainwindow.h testui.moc -SOURCES += testui.cpp ../../src/ui/mainwindow.cpp diff --git a/tests/testui/testui.cpp b/tests/testui/testui.cpp deleted file mode 100755 index 651ee5d..0000000 --- a/tests/testui/testui.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - Situare - A location system for Facebook - Copyright (C) 2010 Ixonos Plc. Authors: - - Henri Lampela - henri.lampela@ixonos.com - - Situare is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - version 2 as published by the Free Software Foundation. - - Situare 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 Situare; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - USA. - */ - -#include -#include -#include "../../src/ui/mainwindow.h" - -class TestUi: public QObject -{ - Q_OBJECT - -private slots: - void testUi(); - - -}; - -void TestUi::testUi() -{ - QLineEdit lineEdit; - - QTest::keyClicks(&lineEdit, "UI test"); - - QCOMPARE(lineEdit.text(), QString("UI test")); -} - - -QTEST_MAIN(TestUi) -#include "testui.moc" -- 1.7.9.5