Modified mapfetcher.cpp and mapfetcher.h files.
authorJussi Laitinen <jussi.laitinen@ixonos.com>
Wed, 31 Mar 2010 11:22:13 +0000 (14:22 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Wed, 31 Mar 2010 11:22:13 +0000 (14:22 +0300)
Situare.pro
src/map/mapengine.cpp
src/map/mapengine.h
src/map/mapfetcher.cpp
src/map/mapfetcher.h
tests/testmap/testmapengine.cpp [new file with mode: 0644]
tests/testmap/testmapfetcher.cpp [new file with mode: 0644]

index 0d2b02e..5ecef6c 100644 (file)
@@ -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
index ba1151e..b8e37bf 100644 (file)
 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<qreal>(1 << zoom);
 
     qreal x = static_cast<qreal>((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<qreal>(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<qreal>(1 << zoom);
     qreal n = M_PI - 2 * M_PI * y / zoom;
     return 180.0 / (M_PI * atan(0.5 * exp(n) - exp(-n)));
index 72b0ee1..6524eb6 100644 (file)
@@ -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<qreal>(1 << zoom);
-
-        qreal x = static_cast<qreal>((longitude + 180.0) / 360.0);
-        qreal y = static_cast<qreal>((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
index fa0c42c..da5f310 100644 (file)
@@ -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();
index 2874348..d3d8ff6 100644 (file)
@@ -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<QNetworkReply *> currentDownloads;
     QQueue<QUrl> downloadQueue;
diff --git a/tests/testmap/testmapengine.cpp b/tests/testmap/testmapengine.cpp
new file mode 100644 (file)
index 0000000..bae45d1
--- /dev/null
@@ -0,0 +1,55 @@
+#include <QtTest/QtTest>
+#include <QPointF>
+#include <QDebug>
+
+#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 (file)
index 0000000..6166857
--- /dev/null
@@ -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 <QtTest/QtTest>
+#include <QUrl>
+#include <QImage>
+#include <QNetworkAccessManager>
+
+#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<QVariant> 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"