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
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);
/ 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;
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)));
/**
-* @brief
+* @brief MapEngine controls Map.
*
* @class MapEngine mapengine.h "map/mapengine.h"
*/
*/
MapEngine(QObject *parent = 0);
-
/**
* @brief Transforms coordinates to tile x,y values.
*
* @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.
* @return qreal latitude
*/
qreal tileYToLatitude(int y, int zoom);
-
};
#endif // MAPENGINE_H
void MapFetcher::fetchMapImage(const QUrl &url)
{
- qDebug() << "fetchMapImage()";
- if (url.isEmpty())
+ qDebug() << __PRETTY_FUNCTION__;
+
+ if (url.isEmpty() || !url.isValid())
return;
//Limit parallel downloads
void MapFetcher::startNextDownload()
{
- qDebug() << "startNextDownload()";
+ qDebug() << __PRETTY_FUNCTION__;
+
if (downloadQueue.isEmpty())
return;
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();
void error(const QString &message);
public slots:
+
/**
* @brief This slot is called when network manager has finished
* the download.
*/
void downloadFinished(QNetworkReply *reply);
-private:
/**
* @brief This slot is called when
*
*/
void startNextDownload();
+private:
QNetworkAccessManager *m_manager;
QList<QNetworkReply *> currentDownloads;
QQueue<QUrl> downloadQueue;
--- /dev/null
+#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"
--- /dev/null
+/*
+ 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"