-/*
- Situare - A location system for Facebook
- Copyright (C) 2010 Ixonos Plc. Authors:
-
- Henri Lampela - henri.lampela@ixonos.com
- 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 <QDebug>
-#include <QBuffer>
-#include "networkaccessmanagermock.h"
-
-NetworkAccessManagerMock::NetworkAccessManagerMock(QNetworkAccessManager *manager, QObject *parent)
- : QNetworkAccessManager(parent)
- , mode(0)
-{
- qDebug() << __PRETTY_FUNCTION__;
-}
-
-NetworkReplyMock *NetworkAccessManagerMock::createRequest(Operation op, const QNetworkRequest &request,
- QIODevice *outgoingData)
-{
- qDebug() << __PRETTY_FUNCTION__;
-
- reply = new NetworkReplyMock(this);
- this->request = request;
-
- switch (this->mode) {
-
- case NetworkAccessManagerMock::CORRECT:
- QTimer::singleShot(0, this, SLOT(generateCorrectReply()));
- break;
- case NetworkAccessManagerMock::INCORRECT:
- QTimer::singleShot(0, this, SLOT(generateError()));
- break;
- case NetworkAccessManagerMock::DELAYED_CORRECT:
- QTimer::singleShot(0, this, SLOT(generateDelayedCorrectReply()));
- break;
- default:
- QTimer::singleShot(0, this, SLOT(generateCorrectReply()));
- break;
- }
- return reply;
-}
-
-NetworkAccessManagerMock::~NetworkAccessManagerMock()
-{
- delete reply;
-}
-
-NetworkReplyMock *NetworkAccessManagerMock::get(const QNetworkRequest &request)
-{
- qDebug() << __PRETTY_FUNCTION__;
- this->request = request;
-
- return reply;
-}
-
-void NetworkAccessManagerMock::setMode(int mode)
-{
- this->mode = mode;
-}
-
-void NetworkAccessManagerMock::generateError()
-{
- qDebug() << __PRETTY_FUNCTION__;
- reply->setUrl(request.url());
- reply->setError(QNetworkReply::HostNotFoundError, "Host not found");
- emit QNetworkAccessManager::finished(reply);
-}
-
-void NetworkAccessManagerMock::generateCorrectReply()
-{
- qDebug() << __PRETTY_FUNCTION__;
- reply->setUrl(request.url());
- reply->setError(QNetworkReply::NoError, "No error");
-// QImage image;
-// if (!image.load(":testTile.png", 0)) {
-// qDebug() << "could not load image";
-// 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 QNetworkAccessManager::finished(reply);
-}
-
-void NetworkAccessManagerMock::generateDelayedCorrectReply()
-{
- qDebug() << __PRETTY_FUNCTION__;
- QTimer::singleShot(4000, this, SLOT(generateCorrectReply()));
-}