+/*
+ 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 <QNetworkAccessManager>
+#include <QDebug>
+#include <QTimer>
+#include <QImage>
+#include <QBuffer>
+#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);
+}