From af6ff702a13bc0c9953a39fde16ad9057ee5c787 Mon Sep 17 00:00:00 2001 From: Jussi Laitinen Date: Wed, 31 Mar 2010 14:22:13 +0300 Subject: [PATCH] Modified mapfetcher.cpp and mapfetcher.h files. --- Situare.pro | 15 ++-- src/map/mapengine.cpp | 9 +- src/map/mapengine.h | 15 +--- src/map/mapfetcher.cpp | 24 +++--- src/map/mapfetcher.h | 3 +- tests/testmap/testmapengine.cpp | 55 +++++++++++++ tests/testmap/testmapfetcher.cpp | 167 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 251 insertions(+), 37 deletions(-) create mode 100644 tests/testmap/testmapengine.cpp create mode 100644 tests/testmap/testmapfetcher.cpp diff --git a/Situare.pro b/Situare.pro index 0d2b02e..5ecef6c 100644 --- a/Situare.pro +++ b/Situare.pro @@ -6,17 +6,14 @@ TEMPLATE = app QT += network \ testlib INCLUDEPATH += . \ - src/tests/testMap/ \ + src/tests/testmap/ \ src/map/ -SOURCES += src/ui/mainwindow.cpp \ - src/map/mapfetcher.cpp \ +SOURCES += src/map/mapfetcher.cpp \ src/map/mapengine.cpp \ - tests/testMap/testmapfetcher.cpp \ - tests/testMap/networkaccessmanagermock.cpp \ - tests/testMap/networkreplymock.cpp + tests/testmap/testmapfetcher.cpp \ + tests/testmap/testmapengine.cpp # src/main.cpp \ -# tests/testMap/testmapengine.cpp -HEADERS += src/ui/mainwindow.h \ - src/map/mapfetcher.h \ + +HEADERS += src/map/mapfetcher.h \ src/map/mapengine.h diff --git a/src/map/mapengine.cpp b/src/map/mapengine.cpp index ba1151e..b8e37bf 100644 --- a/src/map/mapengine.cpp +++ b/src/map/mapengine.cpp @@ -27,12 +27,11 @@ MapEngine::MapEngine(QObject *parent) : QObject(parent) { - } -/*QPoint MapEngine::latLonToTile(qreal latitude, qreal longitude, int zoom) +QPoint MapEngine::latLonToTile(qreal latitude, qreal longitude, int zoom) { - //Power of two + qDebug() << __PRETTY_FUNCTION__; qreal z = static_cast(1 << zoom); qreal x = static_cast((longitude + 180.0) / 360.0); @@ -40,10 +39,11 @@ MapEngine::MapEngine(QObject *parent) / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0); return QPoint(qFloor(x*z), qFloor(y*z)); -}*/ +} qreal MapEngine::tileXToLongitude(int x, int zoom) { + qDebug() << __PRETTY_FUNCTION__; qreal z = static_cast(1 << zoom); qreal lon = x / z * 360.0 - 180.0; qDebug() << lon; @@ -52,6 +52,7 @@ qreal MapEngine::tileXToLongitude(int x, int zoom) qreal MapEngine::tileYToLatitude(int y, int zoom) { + qDebug() << __PRETTY_FUNCTION__; qreal z = static_cast(1 << zoom); qreal n = M_PI - 2 * M_PI * y / zoom; return 180.0 / (M_PI * atan(0.5 * exp(n) - exp(-n))); diff --git a/src/map/mapengine.h b/src/map/mapengine.h index 72b0ee1..6524eb6 100644 --- a/src/map/mapengine.h +++ b/src/map/mapengine.h @@ -26,7 +26,7 @@ /** -* @brief +* @brief MapEngine controls Map. * * @class MapEngine mapengine.h "map/mapengine.h" */ @@ -43,7 +43,6 @@ public: */ MapEngine(QObject *parent = 0); - /** * @brief Transforms coordinates to tile x,y values. * @@ -53,16 +52,7 @@ public: * @param zoom zoom level * @return QPoint tile x,y */ - static QPoint latLonToTile(qreal latitude, qreal longitude, int zoom) { - //Power of two - qreal z = static_cast(1 << zoom); - - qreal x = static_cast((longitude + 180.0) / 360.0); - qreal y = static_cast((1.0 - log(tan(latitude * M_PI / 180.0) + 1.0 - / cos(latitude * M_PI / 180.0)) / M_PI) / 2.0); - - return QPoint(qFloor(x*z), qFloor(y*z)); - } + QPoint latLonToTile(qreal latitude, qreal longitude, int zoom); /** * @brief Transforms tile x value to longitude. @@ -83,7 +73,6 @@ public: * @return qreal latitude */ qreal tileYToLatitude(int y, int zoom); - }; #endif // MAPENGINE_H diff --git a/src/map/mapfetcher.cpp b/src/map/mapfetcher.cpp index fa0c42c..da5f310 100644 --- a/src/map/mapfetcher.cpp +++ b/src/map/mapfetcher.cpp @@ -47,8 +47,9 @@ MapFetcher::MapFetcher(QObject *parent, QNetworkAccessManager *manager) void MapFetcher::fetchMapImage(const QUrl &url) { - qDebug() << "fetchMapImage()"; - if (url.isEmpty()) + qDebug() << __PRETTY_FUNCTION__; + + if (url.isEmpty() || !url.isValid()) return; //Limit parallel downloads @@ -60,7 +61,8 @@ void MapFetcher::fetchMapImage(const QUrl &url) void MapFetcher::startNextDownload() { - qDebug() << "startNextDownload()"; + qDebug() << __PRETTY_FUNCTION__; + if (downloadQueue.isEmpty()) return; @@ -71,24 +73,26 @@ void MapFetcher::startNextDownload() QNetworkRequest::PreferCache); QNetworkReply *reply = m_manager->get(request); - currentDownloads.append(reply); } void MapFetcher::downloadFinished(QNetworkReply *reply) { - qDebug() << "downloadFinished()"; - if (reply->error()) { - emit error(reply->errorString()); - } - else { + qDebug() << __PRETTY_FUNCTION__; + + if (reply->error() == QNetworkReply::NoError) { QImage image; QUrl url = reply->url(); - if (!image.load(reply, 0)) + + if (!image.load(reply, 0)) { image = QImage(); + } emit mapImageReceived(url, QPixmap::fromImage(image)); } + else { + emit error(reply->errorString()); + } currentDownloads.removeAll(reply); reply->deleteLater(); diff --git a/src/map/mapfetcher.h b/src/map/mapfetcher.h index 2874348..d3d8ff6 100644 --- a/src/map/mapfetcher.h +++ b/src/map/mapfetcher.h @@ -81,6 +81,7 @@ signals: void error(const QString &message); public slots: + /** * @brief This slot is called when network manager has finished * the download. @@ -90,7 +91,6 @@ public slots: */ void downloadFinished(QNetworkReply *reply); -private: /** * @brief This slot is called when * @@ -98,6 +98,7 @@ private: */ void startNextDownload(); +private: QNetworkAccessManager *m_manager; QList currentDownloads; QQueue downloadQueue; diff --git a/tests/testmap/testmapengine.cpp b/tests/testmap/testmapengine.cpp new file mode 100644 index 0000000..bae45d1 --- /dev/null +++ b/tests/testmap/testmapengine.cpp @@ -0,0 +1,55 @@ +#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 new file mode 100644 index 0000000..6166857 --- /dev/null +++ b/tests/testmap/testmapfetcher.cpp @@ -0,0 +1,167 @@ +/* + 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 "mapfetcher.h" +#include "tests/testMap/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(); + /** + * @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(); + mapFetcher = new MapFetcher(this, manager); +} + +void TestMapFetcher::testFetchImageEmptyURL() +{ + QUrl url(""); + mapFetcher->fetchMapImage(url); +} + +void TestMapFetcher::testFetchImageIncorrectURL() +{ + QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); + QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString))); + + QVERIFY(imageReceivedSpy.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() +{ + QSignalSpy imageReceivedSpy(mapFetcher, SIGNAL(mapImageReceived(QUrl,QPixmap))); + QSignalSpy imageReceivedErrorSpy(mapFetcher, SIGNAL(error(QString))); + + QVERIFY(imageReceivedSpy.isValid()); + + //Correct URL + QUrl url2("http://tile.openstreetmap.org/7/63/42.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/13/13/%1%2.png").arg(i).arg(j)); + mapFetcher->fetchMapImage(url); + } + } + QTest::qWait(1000); + 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/7/63/%1%2.png").arg(i).arg(j)); + mapFetcher->fetchMapImage(url); + } + } + QTest::qWait(2000); + int totalCount = imageReceivedSpy.count() + imageReceivedErrorSpy.count(); + QCOMPARE(totalCount, 50); + +} + +QTEST_MAIN(TestMapFetcher) +#include "testmapfetcher.moc" -- 1.7.9.5