Added StubBase class and SituareService class test.
authorJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 28 Oct 2010 13:01:45 +0000 (16:01 +0300)
committerJussi Laitinen <jupe@l3l7588.ixonos.local>
Thu, 28 Oct 2010 13:01:45 +0000 (16:01 +0300)
17 files changed:
src/network/networkaccessmanager.cpp
src/network/networkaccessmanager.h
src/network/networkreply.h
src/situareservice/database.h
src/situareservice/situareservice.cpp
src/situareservice/situareservice.h
tests/situareservice/situareservice [new file with mode: 0755]
tests/situareservice/situareservice.pro [new file with mode: 0644]
tests/situareservice/testsituareservice.cpp [new file with mode: 0644]
tests/stubs/imagefetcherstub.h [new file with mode: 0644]
tests/stubs/methodcall.h [new file with mode: 0644]
tests/stubs/networkaccessmanagerstub.h [new file with mode: 0644]
tests/stubs/parameter.h [new file with mode: 0644]
tests/stubs/stubbase.cpp [new file with mode: 0644]
tests/stubs/stubbase.h [new file with mode: 0644]
tests/stubs/stubs.pro [new file with mode: 0644]
tests/tests.pro

index 5cfc140..b63dd8e 100644 (file)
@@ -103,7 +103,7 @@ void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
 
 QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request, bool onlineRequestsOnly)
 {
-    qDebug() << __PRETTY_FUNCTION__;
+    qWarning() << __PRETTY_FUNCTION__;
 
     //Disconnected from network, queue request and return empty reply.
     if (!m_networkHandler->isConnected()) {
index d5c96ac..f9776c9 100644 (file)
@@ -24,9 +24,9 @@
 
 #include <QList>
 #include <QHash>
-#include <QNetworkReply>
-#include <QNetworkRequest>
-#include <QAbstractNetworkCache>
+#include <QtNetwork/QNetworkReply>
+#include <QtNetwork/QNetworkRequest>
+#include <QtNetwork/QAbstractNetworkCache>
 
 class NetworkHandler;
 class QNetworkAccessManager;
index 2adb3d0..dc7d09a 100644 (file)
@@ -22,8 +22,8 @@
 #ifndef NETWORKREPLY_H
 #define NETWORKREPLY_H
 
-#include <QNetworkReply>
-#include <QNetworkRequest>
+#include <QtNetwork/QNetworkReply>
+#include <QtNetwork/QNetworkRequest>
 
 /**
 * @brief NetworkReply class.
index 8fce722..b82fdd0 100644 (file)
@@ -3,8 +3,8 @@
 
 #include <QObject>
 
-#include <QSqlDatabase>
-#include <QSqlError>
+#include <QtSql/QSqlDatabase>
+#include <QtSql/QSqlError>
 #include <QFile>
 #include <QStringList>
 
index 18ed17a..ebd6ec8 100644 (file)
@@ -23,7 +23,7 @@
 
 #include <QtAlgorithms>
 #include <QDebug>
-#include <QNetworkReply>
+#include <QtNetwork/QNetworkReply>
 #include <QPixmap>
 #include <QStringList>
 #include <QtGlobal>
 
 #include "situareservice.h"
 
-SituareService::SituareService(QObject *parent)
+SituareService::SituareService(NetworkAccessManager *networkManager, ImageFetcher *imageFetcher,
+                               QObject *parent)
         : QObject(parent),
         m_user(0)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    m_networkManager = new NetworkAccessManager(this);
+    m_networkManager = networkManager;
     connect(m_networkManager, SIGNAL(finished(QNetworkReply*)),
             this, SLOT(requestFinished(QNetworkReply*)), Qt::QueuedConnection);
 
-    m_imageFetcher = new ImageFetcher(new NetworkAccessManager(this), this);
+    m_imageFetcher = imageFetcher;
     connect(this, SIGNAL(fetchImage(QString, QUrl)),
             m_imageFetcher, SLOT(fetchImage(QString, QUrl)));
     connect(m_imageFetcher, SIGNAL(imageReceived(QString,QPixmap)),
index d4bed77..4a37722 100644 (file)
@@ -49,11 +49,17 @@ class SituareService : public QObject
 public:
 
     /**
+    * Unit test class
+    */
+    friend class TestSituareService;
+
+    /**
     * @brief Default constructor
     *
     * @param parent instance of parent
     */
-    SituareService(QObject *parent = 0);
+    SituareService(NetworkAccessManager *networkManager, ImageFetcher *imageFetcher,
+                   QObject *parent = 0);
 
     /**
     * @brief Destructor
@@ -64,7 +70,6 @@ public:
 /*******************************************************************************
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
-
     /**
     * @brief Retrieves people with similart interest (same tags).
     *
diff --git a/tests/situareservice/situareservice b/tests/situareservice/situareservice
new file mode 100755 (executable)
index 0000000..600e12b
Binary files /dev/null and b/tests/situareservice/situareservice differ
diff --git a/tests/situareservice/situareservice.pro b/tests/situareservice/situareservice.pro
new file mode 100644 (file)
index 0000000..bf57024
--- /dev/null
@@ -0,0 +1,52 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2010-07-09T12:32:33
+#
+#-------------------------------------------------
+
+QT       += testlib network sql
+
+CONFIG   += console
+CONFIG   -= app_bundle
+
+TEMPLATE = app
+
+INCLUDEPATH += /usr/include/qjson
+LIBS += -lqjson
+
+
+SOURCES += testsituareservice.cpp \
+    ../../src/situareservice/situareservice.cpp \
+    ../../src/user/user.cpp \
+    ../../tests/stubs/stubbase.cpp \
+    ../../src/facebookservice/facebookauthentication.cpp \
+    ../../src/facebookservice/facebookcredentials.cpp \
+    ../../src/situareservice/database.cpp \
+    ../../src/coordinates/geocoordinate.cpp \
+    ../../src/coordinates/scenecoordinate.cpp \
+    ../../src/ui/avatarimage.cpp \
+    ../../src/situareservice/notification.cpp
+
+DEFINES += SRCDIR=\\\"$$PWD/\\\"
+
+INCLUDEPATH += . \
+    ../../src/ \
+    ../../tests/stubs
+
+HEADERS += \
+    ../../src/situareservice/situareservice.h \
+    ../../src/user/user.h \
+    ../../tests/stubs/networkaccessmanagerstub.h \
+    ../../tests/stubs/imagefetcherstub.h \
+    ../../src/network/networkaccessmanager.h \
+    ../../src/situareservice/imagefetcher.h \
+    ../../tests/stubs/stubbase.h \
+    ../../src/facebookservice/facebookauthentication.h \
+    ../../src/facebookservice/facebookcredentials.h \
+    ../../src/situareservice/database.h \
+    ../../src/coordinates/geocoordinate.h \
+    ../../src/coordinates/scenecoordinate.h \
+    ../../src/ui/avatarimage.h \
+    ../../src/situareservice/notification.h
+
+DEFINES += QT_NO_DEBUG_OUTPUT
diff --git a/tests/situareservice/testsituareservice.cpp b/tests/situareservice/testsituareservice.cpp
new file mode 100644 (file)
index 0000000..312fe91
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+    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 <QtCore/QString>
+#include <QtTest/QtTest>
+
+#include "situareservice/situareservice.h"
+#include "networkaccessmanagerstub.h"
+#include "imagefetcherstub.h"
+
+class TestSituareService : public QObject
+{
+    Q_OBJECT
+
+private slots:
+    void init();
+    void cleanup();
+    void testSendRequest();
+
+private:
+    SituareService *m_situareService;
+    NetworkAccessManager *m_networkAccessManager;
+    ImageFetcher *m_imageFetcher;
+};
+
+void TestSituareService::init()
+{
+    m_networkAccessManager = new NetworkAccessManager();
+    m_imageFetcher = new ImageFetcher(m_networkAccessManager);
+    m_situareService = new SituareService(m_networkAccessManager, m_imageFetcher);
+
+    QVERIFY(m_networkAccessManager);
+    QVERIFY(m_imageFetcher);
+    QVERIFY(m_situareService);
+
+    networkAccessManagerStub->stubReset();
+    imageFetcherStub->stubReset();
+}
+
+void TestSituareService::cleanup()
+{
+    delete m_situareService;
+}
+
+void TestSituareService::testSendRequest()
+{
+    QUrl url("http://example.com");
+    QNetworkRequest request;
+    request.setUrl(url);
+
+    m_situareService->sendRequest(url, QString(), QString());
+
+    QCOMPARE(networkAccessManagerStub->stubCallCount("get"), 1);
+    QNetworkRequest getRequest = networkAccessManagerStub->stubLastCall().parameter<const QNetworkRequest &>(0);
+}
+
+QTEST_APPLESS_MAIN(TestSituareService);
+
+#include "testsituareservice.moc"
diff --git a/tests/stubs/imagefetcherstub.h b/tests/stubs/imagefetcherstub.h
new file mode 100644 (file)
index 0000000..fdef03a
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+   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 IMAGEFETCHERSTUB_H
+#define IMAGEFETCHERSTUB_H
+
+#include "../../src/situareservice/imagefetcher.h"
+#include "stubbase.h"
+
+class ImageFetcherStub : public StubBase
+{
+public:
+    virtual void ImageFetcherConstructor(NetworkAccessManager *manager, QObject *parent = 0);
+    virtual void fetchImage(const QString &id, const QUrl &url);
+    virtual void downloadFinished(QNetworkReply *reply);
+    virtual void startNextDownload();
+};
+
+void ImageFetcherStub::ImageFetcherConstructor(NetworkAccessManager *manager, QObject *parent)
+{
+    Q_UNUSED(manager)
+    Q_UNUSED(parent)
+}
+
+void ImageFetcherStub::fetchImage(const QString &id, const QUrl &url)
+{
+    QList<ParameterBase *> params;
+    params.append(new Parameter<const QString &>(id));
+    params.append(new Parameter<const QUrl &>(url));
+    stubMethodEntered("fetchImage");
+}
+
+void ImageFetcherStub::downloadFinished(QNetworkReply *reply)
+{
+    QList<ParameterBase *> params;
+    params.append(new Parameter<const QNetworkReply *>(reply));
+    stubMethodEntered("downloadFinished", params);
+}
+
+void ImageFetcherStub::startNextDownload()
+{
+    stubMethodEntered("startNextDownload");
+}
+
+//Create a stub instance
+ImageFetcherStub defaultImageFetcherStub;
+ImageFetcherStub *imageFetcherStub = &defaultImageFetcherStub;
+
+ImageFetcher::ImageFetcher(NetworkAccessManager *manager, QObject *parent)
+{
+    imageFetcherStub->ImageFetcherConstructor(manager, parent);
+}
+
+void ImageFetcher::fetchImage(const QString &id, const QUrl &url)
+{
+    imageFetcherStub->fetchImage(id, url);
+}
+
+void ImageFetcher::downloadFinished(QNetworkReply *reply)
+{
+    imageFetcherStub->downloadFinished(reply);
+}
+
+void ImageFetcher::startNextDownload()
+{
+    imageFetcherStub->startNextDownload();
+}
+
+#endif // IMAGEFETCHERSTUB_H
+
diff --git a/tests/stubs/methodcall.h b/tests/stubs/methodcall.h
new file mode 100644 (file)
index 0000000..60d330a
--- /dev/null
@@ -0,0 +1,83 @@
+/***************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (directui@nokia.com)
+**
+** This file is part of mhome.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at directui@nokia.com.
+**
+** This library is free software; you can redistribute it and/or
+** modify it under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation
+** and appearing in the file LICENSE.LGPL included in the packaging
+** of this file.
+**
+****************************************************************************/
+#ifndef STUBMETHOD_H
+#define STUBMETHOD_H
+
+#include <QString>
+
+class MethodCall
+{
+public:
+    MethodCall(const QString &name, QList<ParameterBase *> params, ParameterBase *returnValue)
+        : _name(name),
+          _params(params),
+          _returnValue(returnValue) {
+    }
+
+    virtual ~MethodCall() {
+        foreach(ParameterBase * p, _params) {
+            delete p;
+        }
+    }
+
+    QString name() {
+        return _name;
+    }
+
+    QList<ParameterBase *> params() {
+        return _params;
+    }
+
+    template <typename T>
+    T parameter(int number) {
+        if (number >= _params.count()) {
+            QString msg = QString("MethodCall::") + __func__ + ": method " + _name + " does not have parameter #" + QString::number(number) + ". Check your test code.";
+            qFatal(qPrintable(msg));
+        }
+        Parameter<T>* param = dynamic_cast<Parameter<T>* >(_params[number]);
+        if (!param) {
+            QString msg = QString("MethodCall::") + __func__ + ": failed dynamic_cast, check that parameter type matches parameter number";
+            qFatal(qPrintable(msg));
+        }
+        return param->data;
+    }
+
+    template <typename T>
+    T returnValue() {
+        Parameter<T>* value = dynamic_cast<Parameter<T>*>(_returnValue);
+
+        if (!value) {
+            QString msg = QString("MethodCall::") + __func__ + ": failed dynamic_cast, check that type matches return value";
+            qFatal(qPrintable(msg));
+        }
+        return value->data;
+    }
+
+    bool returnValueExists() {
+        return (_returnValue != NULL);
+    }
+
+private:
+    QString _name;
+    QList<ParameterBase *> _params;
+    ParameterBase *_returnValue;
+
+};
+
+#endif
diff --git a/tests/stubs/networkaccessmanagerstub.h b/tests/stubs/networkaccessmanagerstub.h
new file mode 100644 (file)
index 0000000..4451b75
--- /dev/null
@@ -0,0 +1,161 @@
+/*
+   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 NETWORKACCESSMANAGERSTUB_H
+#define NETWORKACCESSMANAGERSTUB_H
+
+#include <QtNetwork/QNetworkReply>
+#include <QtNetwork/QNetworkRequest>
+#include <QtNetwork/QAbstractNetworkCache>
+
+#include "../../src/network/networkaccessmanager.h"
+
+#include "stubbase.h"
+
+class NetworkAccessManagerStub : public StubBase
+{
+public:
+    virtual void NetworkAccessManagerConstructor(QObject *parent = 0);
+    virtual bool isConnected();
+    virtual QNetworkReply *get(const QNetworkRequest &request, bool onlineRequestsOnly = false);
+    virtual QNetworkReply *post(const QNetworkRequest &request, QByteArray &data,
+                         bool onlineRequestsOnly = false);
+    virtual void setCache(QAbstractNetworkCache *cache);
+    virtual QAbstractNetworkCache *cache() const;
+    virtual void connected();
+    virtual void disconnected();
+    virtual void downloadFinished(QNetworkReply *reply);
+};
+
+void NetworkAccessManagerStub::NetworkAccessManagerConstructor(QObject *parent)
+{
+    Q_UNUSED(parent)
+}
+
+bool NetworkAccessManagerStub::isConnected()
+{
+    return stubReturnValue<bool>("isConnected");
+}
+
+QNetworkReply *NetworkAccessManagerStub::get(const QNetworkRequest &request,
+                                             bool onlineRequestsOnly)
+{
+    QList<ParameterBase *> params;
+    params.append(new Parameter<const QNetworkRequest &>(request));
+    params.append(new Parameter<bool>(onlineRequestsOnly));
+    stubMethodEntered("get", params);
+
+    return stubReturnValue<QNetworkReply *>("get");
+}
+
+QNetworkReply *NetworkAccessManagerStub::post(const QNetworkRequest &request, QByteArray &data,
+                     bool onlineRequestsOnly)
+{
+    QList<ParameterBase *> params;
+    params.append(new Parameter<const QNetworkRequest &>(request));
+    params.append(new Parameter<QByteArray &>(data));
+    params.append(new Parameter<bool>(onlineRequestsOnly));
+    stubMethodEntered("post", params);
+
+    return stubReturnValue<QNetworkReply *>("post");
+}
+
+void NetworkAccessManagerStub::setCache(QAbstractNetworkCache *cache)
+{
+    QList<ParameterBase *> params;
+    params.append(new Parameter<QAbstractNetworkCache *>(cache));
+    stubMethodEntered("setCache", params);
+}
+
+QAbstractNetworkCache *NetworkAccessManagerStub::cache() const
+{
+    return stubReturnValue<QAbstractNetworkCache *>("cache");
+}
+
+void NetworkAccessManagerStub::connected()
+{
+    stubMethodEntered("connected");
+}
+
+void NetworkAccessManagerStub::disconnected()
+{
+    stubMethodEntered("disconnected");
+}
+
+void NetworkAccessManagerStub::downloadFinished(QNetworkReply *reply)
+{
+    QList<ParameterBase *> params;
+    params.append(new Parameter<QNetworkReply *>(reply));
+    stubMethodEntered("downloadFinished", params);
+}
+
+//Create a stub instance
+NetworkAccessManagerStub defaultNetworkAccessManagerStub;
+NetworkAccessManagerStub *networkAccessManagerStub = &defaultNetworkAccessManagerStub;
+
+//Create a proxy which calls the stub
+NetworkAccessManager::NetworkAccessManager(QObject *parent)
+{
+    networkAccessManagerStub->NetworkAccessManagerConstructor(parent);
+}
+
+bool NetworkAccessManager::isConnected()
+{
+    return networkAccessManagerStub->isConnected();
+}
+
+QNetworkReply *NetworkAccessManager::get(const QNetworkRequest &request, bool onlineRequestsOnly)
+{
+    return networkAccessManagerStub->get(request, onlineRequestsOnly);
+}
+
+QNetworkReply *NetworkAccessManager::post(const QNetworkRequest &request, QByteArray &data,
+                                          bool onlineRequestsOnly)
+{
+    return networkAccessManagerStub->post(request, data, onlineRequestsOnly);
+}
+
+void NetworkAccessManager::setCache(QAbstractNetworkCache *cache)
+{
+    networkAccessManagerStub->setCache(cache);
+}
+
+QAbstractNetworkCache *NetworkAccessManager::cache() const
+{
+    return networkAccessManagerStub->cache();
+}
+
+void NetworkAccessManager::connected()
+{
+    networkAccessManagerStub->connected();
+}
+
+void NetworkAccessManager::disconnected()
+{
+    networkAccessManagerStub->disconnected();
+}
+
+void NetworkAccessManager::downloadFinished(QNetworkReply *reply)
+{
+    networkAccessManagerStub->downloadFinished(reply);
+}
+
+#endif // NETWORKACCESSMANAGERSTUB_H
diff --git a/tests/stubs/parameter.h b/tests/stubs/parameter.h
new file mode 100644 (file)
index 0000000..3d16bf2
--- /dev/null
@@ -0,0 +1,42 @@
+/***************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (directui@nokia.com)
+**
+** This file is part of mhome.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at directui@nokia.com.
+**
+** This library is free software; you can redistribute it and/or
+** modify it under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation
+** and appearing in the file LICENSE.LGPL included in the packaging
+** of this file.
+**
+****************************************************************************/
+#ifndef PARAMETER_H
+#define PARAMETER_H
+
+class ParameterBase
+{
+public:
+    virtual ~ParameterBase() {
+        // Class needs to have at least one virtual function to be polymorphic
+        // (and thus enable dynamic_cast)
+    }
+protected:
+    ParameterBase() {
+    }
+};
+
+template <typename T>
+class Parameter : public ParameterBase
+{
+public:
+    Parameter(T value) : ParameterBase(), data(value) {
+    }
+    T data;
+};
+#endif
diff --git a/tests/stubs/stubbase.cpp b/tests/stubs/stubbase.cpp
new file mode 100644 (file)
index 0000000..cec0ee6
--- /dev/null
@@ -0,0 +1,105 @@
+/***************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (directui@nokia.com)
+**
+** This file is part of mhome.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at directui@nokia.com.
+**
+** This library is free software; you can redistribute it and/or
+** modify it under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation
+** and appearing in the file LICENSE.LGPL included in the packaging
+** of this file.
+**
+****************************************************************************/
+
+#include <assert.h>
+#include "stubbase.h"
+#include "methodcall.h"
+
+QList<MethodCall *> StubBase::stubCallHistory() const
+{
+    return _stubCallHistory;
+}
+
+void StubBase::stubReset() const
+{
+    foreach(ParameterBase * p, _stubReturnValues) {
+        delete p;
+    }
+
+    foreach(MethodCall * p, _stubCallHistory) {
+        delete p;
+    }
+
+    _stubReturnValues.clear();
+    _stubCallCounts.clear();
+    _stubCallHistory.clear();
+}
+
+int StubBase::stubCallCount(const QString &method) const
+{
+    return _stubCallCounts[method];
+}
+
+void StubBase::stubMethodEntered(const QString &methodName, QList<ParameterBase *> params) const
+{
+    MethodCall *method = new MethodCall(methodName, params, stubReturnValue(methodName));
+    _stubCallCounts[methodName] = _stubCallCounts[methodName] + 1;
+    _stubCallHistory.append(method);
+}
+
+void StubBase::stubMethodEntered(const QString &methodName) const
+{
+    MethodCall *method = new MethodCall(methodName, QList<ParameterBase *>(), stubReturnValue(methodName));
+    _stubCallCounts[methodName] = _stubCallCounts[methodName] + 1;
+    _stubCallHistory.append(method);
+}
+
+ParameterBase *StubBase::stubReturnValue(const QString &methodName) const
+{
+    ParameterBase *retVal = NULL;
+
+    if (_stubReturnValues.contains(methodName))
+        retVal = _stubReturnValues[methodName];
+
+    return retVal;
+}
+
+StubBase::~StubBase()
+{
+    stubReset();
+}
+
+MethodCall &StubBase::stubLastCall() const
+{
+    return *(_stubCallHistory.last());
+}
+
+MethodCall &StubBase::stubLastCallTo(const QString &method) const
+{
+    for (int i = _stubCallHistory.count() - 1; i >= 0; i--) {
+        if (_stubCallHistory.at(i)->name() == method) {
+            return *(_stubCallHistory.at(i));
+        }
+    }
+    qDebug() << "StubBase::lastCallTo: call not found to:" << method;
+    return *((MethodCall *)0);
+}
+
+QList<MethodCall *> StubBase::stubCallsTo(const QString &method) const
+{
+    QList<MethodCall *> calls;
+    for (int i = 0; i < _stubCallHistory.count(); i++) {
+        if (_stubCallHistory.at(i)->name() == method) {
+            calls.append(_stubCallHistory.at(i));
+        }
+    }
+    return calls;
+}
+
+
diff --git a/tests/stubs/stubbase.h b/tests/stubs/stubbase.h
new file mode 100644 (file)
index 0000000..33faec7
--- /dev/null
@@ -0,0 +1,108 @@
+/***************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (directui@nokia.com)
+**
+** This file is part of mhome.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at directui@nokia.com.
+**
+** This library is free software; you can redistribute it and/or
+** modify it under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation
+** and appearing in the file LICENSE.LGPL included in the packaging
+** of this file.
+**
+****************************************************************************/
+
+#ifndef STUBBASE_H
+#define STUBBASE_H
+
+#include <QList>
+#include <QVariant>
+#include <QMap>
+#include <QtDebug>
+#include "parameter.h"
+#include "methodcall.h"
+
+
+class StubBase
+{
+public:
+    // Return a list of method calls
+    QList<MethodCall *> stubCallHistory() const;
+
+    // Return a reference to the last method call
+    MethodCall &stubLastCall() const;
+
+    // Access parameters of the last method call
+    template <typename T>
+    T stubLastParameters(int number) const;
+
+    // Access parameters of the last method call
+    MethodCall &stubLastCallTo(const QString &method) const;
+
+    // Return a list of calls to a particular method call
+    QList<MethodCall *> stubCallsTo(const QString &method) const;
+
+    // Return the number of times a method has been called
+    int stubCallCount(const QString &method) const;
+
+    // Set the return value for next call of methodName
+    template <typename T>
+    void stubSetReturnValue(const QString &methodName, T value) const;
+
+    // Return the return value set for methodName
+    template <typename T>
+    T &stubReturnValue(const QString &methodName) const;
+
+
+
+    // For use by stubs
+    virtual ~StubBase();
+    void stubReset() const;
+    ParameterBase *stubReturnValue(const QString &methodName) const;
+    void stubMethodEntered(const QString &methodName, QList<ParameterBase *> params) const;
+    void stubMethodEntered(const QString &methodName) const;
+
+private:
+    mutable QMap<QString, ParameterBase *> _stubReturnValues;
+    mutable QMap<QString, int> _stubCallCounts;
+    mutable QList<MethodCall *> _stubCallHistory;
+
+};
+
+template <typename T>
+void StubBase::stubSetReturnValue(const QString &methodName, T value) const
+{
+    Parameter<T>* param = new Parameter<T>(value);
+    _stubReturnValues[methodName] = param;
+}
+
+template <typename T>
+T &StubBase::stubReturnValue(const QString &methodName) const
+{
+    if (! _stubReturnValues.contains(methodName)) {
+        stubSetReturnValue<T>(methodName, T());
+    }
+
+    ParameterBase *base = _stubReturnValues[methodName];
+    Parameter<T>* param = dynamic_cast<Parameter<T>*>(base);
+    if (!param) {
+        QString msg = QString("StubBase::") + __func__ + ": failed dynamic_cast, check that return value type matches the method; check also that you have used stubSetReturnValue(" + methodName + ")";
+        qFatal("%s", qPrintable(msg));
+    }
+    return param->data;
+
+}
+
+template <typename T>
+T StubBase::stubLastParameters(int number) const
+{
+    MethodCall &call = stubLastCall();
+    return call.parameter<T>(number);
+}
+
+#endif
diff --git a/tests/stubs/stubs.pro b/tests/stubs/stubs.pro
new file mode 100644 (file)
index 0000000..9095d1f
--- /dev/null
@@ -0,0 +1,9 @@
+HEADERS += \
+    stubbase.h \
+    methodcall.h \
+    parameter.h \
+    networkaccessmanagerstub.h \
+    imagefetcherstub.h
+
+SOURCES += \
+    stubbase.cpp
index 76e90c5..e77d782 100644 (file)
@@ -16,4 +16,5 @@ SUBDIRS = coordinates/geocoordinate \
           ui/avatarimage \
           user \
           routing/route \
-          routing/routesegment
+          routing/routesegment \
+          situareservice