summaryrefslogtreecommitdiff
path: root/tests/auto/placemanager_utils
diff options
context:
space:
mode:
authorabcd <amos.choy@nokia.com>2012-06-05 18:27:12 +1000
committerQt by Nokia <qt-info@nokia.com>2012-06-12 04:08:33 +0200
commit2322243ceff82b5336eee0dbe1a1b8ae38bff383 (patch)
tree0d78874128e4c910b0ed247c876af560a397996a /tests/auto/placemanager_utils
parent4db98d8fccdb584f0a9598e35f55f1272c07cff5 (diff)
downloadqtlocation-2322243ceff82b5336eee0dbe1a1b8ae38bff383.tar.gz
Factor out common manager testing code into it's own class
Change-Id: I24ab2c94d01f8d0fbc53f13608bf1fd44af76916 Reviewed-by: Aaron McCarthy <aaron.mccarthy@nokia.com>
Diffstat (limited to 'tests/auto/placemanager_utils')
-rw-r--r--tests/auto/placemanager_utils/placemanager_utils.cpp339
-rw-r--r--tests/auto/placemanager_utils/placemanager_utils.h211
2 files changed, 550 insertions, 0 deletions
diff --git a/tests/auto/placemanager_utils/placemanager_utils.cpp b/tests/auto/placemanager_utils/placemanager_utils.cpp
new file mode 100644
index 00000000..3ba2b7ba
--- /dev/null
+++ b/tests/auto/placemanager_utils/placemanager_utils.cpp
@@ -0,0 +1,339 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used 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. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "placemanager_utils.h"
+
+#include <QtCore/QDebug>
+#include <QtLocation/QPlace>
+#include <QtLocation/QPlaceManager>
+#include <QtLocation/QPlaceSearchReply>
+#include <QtTest/QSignalSpy>
+#include <QtTest/QTest>
+
+//constant for timeout to verify signals
+const int PlaceManagerUtils::Timeout(10000);
+
+PlaceManagerUtils::PlaceManagerUtils(QObject *parent)
+ : QObject(parent), placeManager(0)
+{
+}
+
+bool PlaceManagerUtils::doSavePlace(QPlaceManager *manager,
+ const QPlace &place,
+ QPlaceReply::Error expectedError,
+ QString *placeId)
+{
+ Q_ASSERT(manager);
+ QPlaceIdReply *saveReply = manager->savePlace(place);
+ bool isSuccessful = checkSignals(saveReply, expectedError, manager);
+ if (placeId != 0) {
+ *placeId = saveReply->id();
+ }
+
+ if (saveReply->id().isEmpty() && expectedError == QPlaceReply::NoError) {
+ qWarning("ID is empty in reply for save operation");
+ qWarning() << "Error string = " << saveReply->errorString();
+ isSuccessful = false;
+ }
+
+ if (!isSuccessful)
+ qWarning() << "Error string = " << saveReply->errorString();
+
+ return isSuccessful;
+}
+
+void PlaceManagerUtils::doSavePlaces(QPlaceManager *manager, QList<QPlace> &places)
+{
+ QPlaceIdReply *saveReply;
+
+ foreach (QPlace place, places) {
+ saveReply = manager->savePlace(place);
+ QSignalSpy saveSpy(saveReply, SIGNAL(finished()));
+ QTRY_VERIFY_WITH_TIMEOUT(saveSpy.count() == 1, Timeout);
+ QCOMPARE(saveReply->error(), QPlaceReply::NoError);
+ saveSpy.clear();
+ }
+}
+
+void PlaceManagerUtils::doSavePlaces(QPlaceManager *manager, const QList<QPlace *> &places)
+{
+ QPlaceIdReply *saveReply;
+
+ static int count= 0;
+ foreach (QPlace *place, places) {
+ count++;
+ saveReply = manager->savePlace(*place);
+ QSignalSpy saveSpy(saveReply, SIGNAL(finished()));
+ QTRY_VERIFY_WITH_TIMEOUT(saveSpy.count() == 1, Timeout);
+ QCOMPARE(saveReply->error(), QPlaceReply::NoError);
+ place->setPlaceId(saveReply->id());
+ saveSpy.clear();
+ }
+}
+
+bool PlaceManagerUtils::doSearch(QPlaceManager *manager,
+ const QPlaceSearchRequest &request,
+ QList<QPlaceSearchResult> *results,
+ QPlaceReply::Error expectedError)
+{
+ QPlaceSearchReply *searchReply= manager->search(request);
+ bool success = checkSignals(searchReply, expectedError, manager);
+ *results = searchReply->results();
+ return success;
+}
+
+bool PlaceManagerUtils::doSearch(QPlaceManager *manager,
+ const QPlaceSearchRequest &request,
+ QList<QPlace> *results, QPlaceReply::Error expectedError)
+{
+ bool success = false;
+ results->clear();
+ QList<QPlaceSearchResult> searchResults;
+ success = doSearch(manager, request, &searchResults, expectedError);
+ foreach (const QPlaceSearchResult &searchResult, searchResults)
+ results->append(searchResult.place());
+ return success;
+}
+
+bool PlaceManagerUtils::doRemovePlace(QPlaceManager *manager,
+ const QPlace &place,
+ QPlaceReply::Error expectedError)
+{
+ QPlaceIdReply *removeReply = manager->removePlace(place.placeId());
+ bool isSuccessful = false;
+ isSuccessful = checkSignals(removeReply, expectedError, manager)
+ && (removeReply->id() == place.placeId());
+
+ if (!isSuccessful)
+ qWarning() << "Place removal unsuccessful errorString = " << removeReply->errorString();
+
+ return isSuccessful;
+}
+
+bool PlaceManagerUtils::doFetchDetails(QPlaceManager *manager,
+ QString placeId, QPlace *place,
+ QPlaceReply::Error expectedError)
+{
+ QPlaceDetailsReply *detailsReply = manager->getPlaceDetails(placeId);
+ bool success = checkSignals(detailsReply, expectedError, manager);
+ *place = detailsReply->place();
+
+ if (!success)
+ qDebug() << "Error string = " << detailsReply->errorString();
+
+ return success;
+}
+
+bool PlaceManagerUtils::doSaveCategory(QPlaceManager *manager,
+ const QPlaceCategory &category,
+ const QString &parentId,
+ QPlaceReply::Error expectedError,
+ QString *categoryId)
+{
+ QPlaceIdReply *idReply = manager->saveCategory(category, parentId);
+ bool isSuccessful = checkSignals(idReply, expectedError, manager)
+ && (idReply->error() == expectedError);
+
+ if (categoryId != 0)
+ *categoryId = idReply->id();
+
+ if (!isSuccessful)
+ qDebug() << "Error string =" << idReply->errorString();
+ return isSuccessful;
+}
+
+bool PlaceManagerUtils::doRemoveCategory(QPlaceManager *manager,
+ const QPlaceCategory &category,
+ QPlaceReply::Error expectedError)
+{
+ QPlaceIdReply *idReply = manager->removeCategory(category.categoryId());
+
+ bool isSuccessful = checkSignals(idReply, expectedError, manager) &&
+ (idReply->error() == expectedError);
+ return isSuccessful;
+}
+
+bool PlaceManagerUtils::doFetchCategory(QPlaceManager *manager,
+ const QString &categoryId,
+ QPlaceCategory *category,
+ QPlaceReply::Error expectedError)
+{
+ Q_ASSERT(category);
+ QPlaceReply * catInitReply = manager->initializeCategories();
+ bool isSuccessful = checkSignals(catInitReply, expectedError, manager);
+ *category = manager->category(categoryId);
+
+ if (!isSuccessful)
+ qDebug() << "Error initializing categories, error string = "
+ << catInitReply->errorString();
+
+ if (category->categoryId() != categoryId)
+ isSuccessful = false;
+ return isSuccessful;
+}
+
+bool PlaceManagerUtils::doMatch(QPlaceManager *manager,
+ const QPlaceMatchRequest &request,
+ QList<QPlace> *places,
+ QPlaceReply::Error expectedError)
+{
+ QPlaceMatchReply *reply = manager->matchingPlaces(request);
+ bool isSuccessful = checkSignals(reply, expectedError, manager) &&
+ (reply->error() == expectedError);
+ *places = reply->places();
+ if (!isSuccessful)
+ qDebug() << "Error for matching operation, error string = "
+ << reply->errorString();
+ return isSuccessful;
+}
+
+bool PlaceManagerUtils::checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError,
+ QPlaceManager *manager)
+{
+ Q_ASSERT(reply);
+ QSignalSpy finishedSpy(reply, SIGNAL(finished()));
+ QSignalSpy errorSpy(reply, SIGNAL(error(QPlaceReply::Error,QString)));
+ QSignalSpy managerFinishedSpy(manager, SIGNAL(finished(QPlaceReply*)));
+ QSignalSpy managerErrorSpy(manager,SIGNAL(error(QPlaceReply*,QPlaceReply::Error,QString)));
+
+ if (expectedError != QPlaceReply::NoError) {
+ //check that we get an error signal from the reply
+ WAIT_UNTIL(errorSpy.count() == 1);
+ if (errorSpy.count() != 1) {
+ qWarning() << "Error signal for search operation not received";
+ return false;
+ }
+
+ //check that we get the correct error from the reply's signal
+ QPlaceReply::Error actualError = qvariant_cast<QPlaceReply::Error>(errorSpy.at(0).at(0));
+ if (actualError != expectedError) {
+ qWarning() << "Actual error code in reply signal does not match expected error code";
+ qWarning() << "Actual error code = " << actualError;
+ qWarning() << "Expected error coe =" << expectedError;
+ return false;
+ }
+
+ //check that we get an error signal from the manager
+ WAIT_UNTIL(managerErrorSpy.count() == 1);
+ if (managerErrorSpy.count() !=1) {
+ qWarning() << "Error signal from manager for search operation not received";
+ return false;
+ }
+
+ //check that we get the correct reply instance in the error signal from the manager
+ if (qvariant_cast<QPlaceReply*>(managerErrorSpy.at(0).at(0)) != reply) {
+ qWarning() << "Reply instance in error signal from manager is incorrect";
+ return false;
+ }
+
+ //check that we get the correct error from the signal of the manager
+ actualError = qvariant_cast<QPlaceReply::Error>(managerErrorSpy.at(0).at(1));
+ if (actualError != expectedError) {
+ qWarning() << "Actual error code from manager signal does not match expected error code";
+ qWarning() << "Actual error code =" << actualError;
+ qWarning() << "Expected error code = " << expectedError;
+ return false;
+ }
+ }
+
+ //check that we get a finished signal
+ WAIT_UNTIL(finishedSpy.count() == 1);
+ if (finishedSpy.count() !=1) {
+ qWarning() << "Finished signal from reply not received";
+ return false;
+ }
+
+ if (reply->error() != expectedError) {
+ qWarning() << "Actual error code does not match expected error code";
+ qWarning() << "Actual error code: " << reply->error();
+ qWarning() << "Expected error code" << expectedError;
+ return false;
+ }
+
+ if (expectedError == QPlaceReply::NoError && !reply->errorString().isEmpty()) {
+ qWarning() << "Expected error was no error but error string was not empty";
+ qWarning() << "Error string=" << reply->errorString();
+ return false;
+ }
+
+ //check that we get the finished signal from the manager
+ WAIT_UNTIL(managerFinishedSpy.count() == 1);
+ if (managerFinishedSpy.count() != 1) {
+ qWarning() << "Finished signal from manager not received";
+ return false;
+ }
+
+ //check that the reply instance in the finished signal from the manager is correct
+ if (qvariant_cast<QPlaceReply *>(managerFinishedSpy.at(0).at(0)) != reply) {
+ qWarning() << "Reply instance in finished signal from manager is incorrect";
+ return false;
+ }
+
+ return true;
+}
+
+bool PlaceManagerUtils::compare(const QList<QPlace> &actualResults,
+ const QList<QPlace> &expectedResults)
+{
+ QSet<QString> actualIds;
+ foreach (const QPlace &place, actualResults)
+ actualIds.insert(place.placeId());
+
+ QSet<QString> expectedIds;
+ foreach (const QPlace &place, expectedResults)
+ expectedIds.insert(place.placeId());
+
+ bool isMatch = (actualIds == expectedIds);
+ if (actualResults.count() != expectedResults.count() || !isMatch) {
+ qWarning() << "comparison of results by name does not match";
+ qWarning() << "actual result ids: " << actualIds;
+ qWarning() << "expected result ids : " << expectedIds;
+ return false;
+ }
+
+ return isMatch;
+}
+
+void PlaceManagerUtils::setVisibility(QList<QPlace *> places, QtLocation::Visibility visibility)
+{
+ foreach (QPlace *place, places)
+ place->setVisibility(visibility);
+}
diff --git a/tests/auto/placemanager_utils/placemanager_utils.h b/tests/auto/placemanager_utils/placemanager_utils.h
new file mode 100644
index 00000000..30ec3565
--- /dev/null
+++ b/tests/auto/placemanager_utils/placemanager_utils.h
@@ -0,0 +1,211 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used 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. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef PLACEMANAGER_UTILS_H
+#define PLACEMANAGER_UTILS_H
+
+#include <QtCore/QString>
+#include <QtLocation/QPlaceReply>
+#include <QtLocation/qtlocation.h>
+
+#ifndef WAIT_UNTIL
+#define WAIT_UNTIL(__expr) \
+ do { \
+ const int __step = 50; \
+ const int __timeout = 10000; \
+ if (!(__expr)) { \
+ QTest::qWait(0); \
+ } \
+ for (int __i = 0; __i < __timeout && !(__expr); __i+=__step) { \
+ QTest::qWait(__step); \
+ } \
+ } while (0)
+#endif
+
+QT_BEGIN_NAMESPACE
+
+class QPlaceManager;
+class QPlace;
+class QPlaceSearchResult;
+class QPlaceSearchRequest;
+class QPlaceCategory;
+class QPlaceMatchRequest;
+
+QT_END_NAMESPACE
+
+class PlaceManagerUtils : public QObject
+{
+ Q_OBJECT
+public:
+ PlaceManagerUtils(QObject *parent = 0);
+
+ static bool doSavePlace(QPlaceManager *manager,
+ const QPlace &place,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError,
+ QString *placeId = 0);
+
+ static void doSavePlaces(QPlaceManager *manager, QList<QPlace> &places);
+
+ //sets the id for saved places
+ static void doSavePlaces(QPlaceManager *manager, const QList<QPlace *> &places);
+
+ static bool doSearch(QPlaceManager *manager, const QPlaceSearchRequest &request,
+ QList<QPlaceSearchResult> *results,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError);
+
+ static bool doSearch(QPlaceManager *manager, const QPlaceSearchRequest &request,
+ QList<QPlace> *results,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError);
+
+ static bool doRemovePlace(QPlaceManager *manager, const QPlace &place,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError);
+
+ static bool doFetchDetails(QPlaceManager *manager,
+ QString placeId,
+ QPlace *place,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError);
+
+ static bool doSaveCategory(QPlaceManager *manager,
+ const QPlaceCategory &category,
+ const QString &parentId,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError,
+ QString *categoryId = 0);
+
+ static bool doRemoveCategory(QPlaceManager *manager, const QPlaceCategory &category,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError);
+
+ static bool doFetchCategory(QPlaceManager *manager,
+ const QString &categoryId,
+ QPlaceCategory *category,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError);
+
+ static bool doMatch(QPlaceManager *manager,
+ const QPlaceMatchRequest &request,
+ QList<QPlace> *places,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError);
+
+ static bool checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError,
+ QPlaceManager *manager);
+
+ static bool compare(const QList<QPlace> &actualResults,
+ const QList<QPlace> &expectedResults);
+
+ static void setVisibility(QList<QPlace *>places, QtLocation::Visibility visibility);
+
+ static const int Timeout;
+
+protected:
+ bool doSavePlace(const QPlace &place,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError,
+ QString *placeId = 0) {
+ return doSavePlace(placeManager, place, expectedError, placeId);
+ }
+
+ void doSavePlaces(QList<QPlace> &places) {
+ return doSavePlaces(placeManager, places);
+ }
+
+ void doSavePlaces(const QList<QPlace *> &places) {
+ return doSavePlaces(placeManager, places);
+ }
+
+ bool doRemovePlace(const QPlace &place,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError)
+ {
+ return doRemovePlace(placeManager, place, expectedError);
+ }
+
+ bool doSearch(const QPlaceSearchRequest &request,
+ QList<QPlace> *results,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError) {
+ return doSearch(placeManager, request, results,expectedError);
+ }
+
+ bool doFetchDetails(QString placeId,
+ QPlace *place,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError) {
+ return doFetchDetails(placeManager, placeId, place, expectedError);
+ }
+
+ bool doSaveCategory(const QPlaceCategory &category,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError,
+ QString *categoryId = 0) {
+ return doSaveCategory(placeManager, category, QString(),
+ expectedError,categoryId);
+ }
+
+ bool doSaveCategory(const QPlaceCategory &category,
+ const QString &parentId,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError,
+ QString *categoryId = 0) {
+ return doSaveCategory(placeManager, category, parentId,
+ expectedError, categoryId);
+ }
+
+ bool doRemoveCategory(const QPlaceCategory &category,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError)
+ {
+ return doRemoveCategory(placeManager, category, expectedError);
+ }
+
+ bool doFetchCategory(const QString &categoryId,
+ QPlaceCategory *category,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError) {
+ return doFetchCategory(placeManager, categoryId,
+ category, expectedError);
+ }
+
+ bool doMatch(const QPlaceMatchRequest &request,
+ QList<QPlace> *places,
+ QPlaceReply::Error expectedError = QPlaceReply::NoError) {
+ return doMatch(placeManager, request,
+ places, expectedError);
+ }
+
+ bool checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError) {
+ return checkSignals(reply, expectedError, placeManager);
+ }
+
+ QPlaceManager *placeManager;
+};
+
+#endif
+