From: Jussi Laitinen Date: Thu, 1 Apr 2010 07:41:56 +0000 (+0300) Subject: Modified mapfetcher.ccp and mapfetcher.h. X-Git-Tag: v0.1~2^2~9 X-Git-Url: https://vcs.maemo.org/git/?p=situare;a=commitdiff_plain;h=2768f1209dcabe23ec94b700e77696682145b951 Modified mapfetcher.ccp and mapfetcher.h. --- diff --git a/Situare.pro b/Situare.pro index 0d057ce..2b09e4c 100644 --- a/Situare.pro +++ b/Situare.pro @@ -8,11 +8,16 @@ QT += network \ INCLUDEPATH += . \ src/tests/testmap/ \ src/map/ -SOURCES += src/map/mapfetcher.cpp \ +SOURCES += src/main.cpp \ +src/map/mapfetcher.cpp \ src/map/mapengine.cpp \ - tests/testmap/testmapfetcher.cpp \ +# tests/testmap/testmapfetcher.cpp \ # tests/testmap/testmapengine.cpp \ - src/ui/mainwindow.cpp + src/ui/mainwindow.cpp \ + tests/testmap/networkaccessmanagermock.cpp \ + tests/testmap/networkreplymock.cpp HEADERS += src/map/mapfetcher.h \ src/map/mapengine.h \ - src/ui/mainwindow.h + src/ui/mainwindow.h \ + tests/testmap/networkaccessmanagermock.h \ + tests/testmap/networkreplymock.h diff --git a/src/map/mapfetcher.cpp b/src/map/mapfetcher.cpp index 918c785..2c730b8 100644 --- a/src/map/mapfetcher.cpp +++ b/src/map/mapfetcher.cpp @@ -42,7 +42,7 @@ MapFetcher::MapFetcher(QObject *parent, QNetworkAccessManager *manager) m_manager->setCache(diskCache); - connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*))); + connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*))); //Change to QNetworkReply } void MapFetcher::fetchMapImage(const QUrl &url) @@ -78,7 +78,6 @@ void MapFetcher::startNextDownload() void MapFetcher::downloadFinished(QNetworkReply *reply) { qDebug() << __PRETTY_FUNCTION__; - if (reply->error() == QNetworkReply::NoError) { QImage image; QUrl url = reply->url(); diff --git a/src/map/mapfetcher.h b/src/map/mapfetcher.h index d3d8ff6..f39fa39 100644 --- a/src/map/mapfetcher.h +++ b/src/map/mapfetcher.h @@ -100,7 +100,7 @@ public slots: private: QNetworkAccessManager *m_manager; - QList currentDownloads; + QList currentDownloads; QQueue downloadQueue; }; diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 11015df..91a2563 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -58,9 +58,10 @@ void MainWindow::imageReceived(const QUrl &url, const QPixmap &image) void MainWindow::debug() { qDebug() << __PRETTY_FUNCTION__; - for (int j = 0; j < 10; ++j) { - mapFetcher->fetchMapImage(QUrl(QString("http://tile.openstreetmap.org/mapnik/13/4675/213%1.png").arg(j))); - } + for (int i = 0; i < 2; ++i) + for (int j = 0; j < 2; ++j) + mapFetcher->fetchMapImage(QUrl(QString("http://tile.openstreetmap.org/mapnik/13/467%1/213%2.png") + .arg(i).arg(j))); } MainWindow::~MainWindow() diff --git a/tests/testmap/networkaccessmanagermock.cpp b/tests/testmap/networkaccessmanagermock.cpp new file mode 100644 index 0000000..315873c --- /dev/null +++ b/tests/testmap/networkaccessmanagermock.cpp @@ -0,0 +1,112 @@ +/* + 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 new file mode 100644 index 0000000..52b496a --- /dev/null +++ b/tests/testmap/networkaccessmanagermock.h @@ -0,0 +1,54 @@ +/* + 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/networkreplymock.cpp b/tests/testmap/networkreplymock.cpp new file mode 100644 index 0000000..26af540 --- /dev/null +++ b/tests/testmap/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/networkreplymock.h b/tests/testmap/networkreplymock.h new file mode 100644 index 0000000..54c81ee --- /dev/null +++ b/tests/testmap/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.cpp b/tests/testmap/testmapfetcher.cpp index 64ad113..a18142d 100644 --- a/tests/testmap/testmapfetcher.cpp +++ b/tests/testmap/testmapfetcher.cpp @@ -23,8 +23,10 @@ #include #include #include +#include #include "mapfetcher.h" +#include "networkaccessmanagermock.h" /** * @brief @@ -87,7 +89,8 @@ private: TestMapFetcher::TestMapFetcher() { QNetworkAccessManager *manager = new QNetworkAccessManager(); - mapFetcher = new MapFetcher(this, manager); + NetworkAccessManagerMock *managerMock = new NetworkAccessManagerMock(manager, this); + mapFetcher = new MapFetcher(this, managerMock); } void TestMapFetcher::testFetchImageEmptyURL() @@ -119,7 +122,7 @@ void TestMapFetcher::testFetchImageCorrectURL() // //Correct URL // QUrl url2("http://tile.openstreetmap.org/7/63/42.png"); // mapFetcher->fetchMapImage(url2); -// QTest::qWait(1000); +// QTest::qWait(2000); // QCOMPARE(imageReceivedSpy.count(), 1); // QList signalArgs2 = imageReceivedSpy.takeLast(); // QCOMPARE(url2, signalArgs2.at(0).toUrl()); @@ -127,17 +130,17 @@ void TestMapFetcher::testFetchImageCorrectURL() 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()); +// 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()