1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include <QCoreApplication>
30 #include <QString>
31 #include <QtTest/QtTest>
32
33 #include <QtLocation/QGeoServiceProvider>
34 #include <QtLocation/QPlaceManager>
35
36 #ifndef WAIT_UNTIL
37 #define WAIT_UNTIL(__expr) \
38 do { \
39 const int __step = 50; \
40 const int __timeout = 5000; \
41 if (!(__expr)) { \
42 QTest::qWait(0); \
43 } \
44 for (int __i = 0; __i < __timeout && !(__expr); __i+=__step) { \
45 QTest::qWait(__step); \
46 } \
47 } while (0)
48 #endif
49
50 Q_DECLARE_METATYPE(QPlaceIdReply *);
51
52 QT_USE_NAMESPACE
53
54 class tst_QPlaceManagerNokia : public QObject
55 {
56 Q_OBJECT
57 public:
58 tst_QPlaceManagerNokia();
59
60 private Q_SLOTS:
61 void initTestCase();
62 void unsupportedFunctions();
63
64 private:
65 bool checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError);
66 QGeoServiceProvider *provider;
67 QPlaceManager *placeManager;
68 };
69
tst_QPlaceManagerNokia()70 tst_QPlaceManagerNokia::tst_QPlaceManagerNokia()
71 {
72 }
73
initTestCase()74 void tst_QPlaceManagerNokia::initTestCase()
75 {
76 qRegisterMetaType<QPlaceIdReply *>();
77
78 QStringList providers = QGeoServiceProvider::availableServiceProviders();
79
80 QVariantMap params;
81 params.insert(QStringLiteral("here.app_id"), "stub");
82 params.insert(QStringLiteral("here.token"), "stub");
83 provider = new QGeoServiceProvider("here", params);
84 placeManager = provider->placeManager();
85 QVERIFY(placeManager);
86 }
87
unsupportedFunctions()88 void tst_QPlaceManagerNokia::unsupportedFunctions()
89 {
90 QPlace place;
91 place.setName(QStringLiteral("Brisbane"));
92 QPlaceIdReply *savePlaceReply = placeManager->savePlace(place);
93 QVERIFY(savePlaceReply);
94 QVERIFY(checkSignals(savePlaceReply, QPlaceReply::UnsupportedError));
95 QCOMPARE(savePlaceReply->operationType(), QPlaceIdReply::SavePlace);
96
97 QPlaceIdReply *removePlaceReply = placeManager->removePlace(place.placeId());
98 QVERIFY(removePlaceReply);
99 QVERIFY(checkSignals(removePlaceReply, QPlaceReply::UnsupportedError));
100 QCOMPARE(removePlaceReply->operationType(), QPlaceIdReply::RemovePlace);
101
102 QPlaceCategory category;
103 category.setName(QStringLiteral("Accommodation"));
104 QPlaceIdReply *saveCategoryReply = placeManager->saveCategory(category);
105 QVERIFY(saveCategoryReply);
106 QVERIFY(checkSignals(saveCategoryReply, QPlaceReply::UnsupportedError));
107 QCOMPARE(saveCategoryReply->operationType(), QPlaceIdReply::SaveCategory);
108
109 QPlaceIdReply *removeCategoryReply = placeManager->removeCategory(category.categoryId());
110 QVERIFY(removeCategoryReply);
111 QVERIFY(checkSignals(removeCategoryReply, QPlaceReply::UnsupportedError));
112 QCOMPARE(removeCategoryReply->operationType(), QPlaceIdReply::RemoveCategory);
113 }
114
checkSignals(QPlaceReply * reply,QPlaceReply::Error expectedError)115 bool tst_QPlaceManagerNokia::checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError)
116 {
117 QSignalSpy finishedSpy(reply, SIGNAL(finished()));
118 QSignalSpy errorSpy(reply, SIGNAL(error(QPlaceReply::Error,QString)));
119 QSignalSpy managerFinishedSpy(placeManager, SIGNAL(finished(QPlaceReply*)));
120 QSignalSpy managerErrorSpy(placeManager,SIGNAL(error(QPlaceReply*,QPlaceReply::Error,QString)));
121
122 if (expectedError != QPlaceReply::NoError) {
123 //check that we get an error signal from the reply
124 WAIT_UNTIL(errorSpy.count() == 1);
125 if (errorSpy.count() != 1) {
126 qWarning() << "Error signal for operation not received";
127 return false;
128 }
129
130 //check that we get the correct error from the reply's signal
131 QPlaceReply::Error actualError = qvariant_cast<QPlaceReply::Error>(errorSpy.at(0).at(0));
132 if (actualError != expectedError) {
133 qWarning() << "Actual error code in reply signal does not match expected error code";
134 qWarning() << "Actual error code = " << actualError;
135 qWarning() << "Expected error coe =" << expectedError;
136 return false;
137 }
138
139 //check that we get an error signal from the manager
140 WAIT_UNTIL(managerErrorSpy.count() == 1);
141 if (managerErrorSpy.count() !=1) {
142 qWarning() << "Error signal from manager for search operation not received";
143 return false;
144 }
145
146 //check that we get the correct reply instance in the error signal from the manager
147 if (qvariant_cast<QPlaceReply*>(managerErrorSpy.at(0).at(0)) != reply) {
148 qWarning() << "Reply instance in error signal from manager is incorrect";
149 return false;
150 }
151
152 //check that we get the correct error from the signal of the manager
153 actualError = qvariant_cast<QPlaceReply::Error>(managerErrorSpy.at(0).at(1));
154 if (actualError != expectedError) {
155 qWarning() << "Actual error code from manager signal does not match expected error code";
156 qWarning() << "Actual error code =" << actualError;
157 qWarning() << "Expected error code = " << expectedError;
158 return false;
159 }
160 }
161
162 //check that we get a finished signal
163 WAIT_UNTIL(finishedSpy.count() == 1);
164 if (finishedSpy.count() !=1) {
165 qWarning() << "Finished signal from reply not received";
166 return false;
167 }
168
169 if (reply->error() != expectedError) {
170 qWarning() << "Actual error code does not match expected error code";
171 qWarning() << "Actual error code: " << reply->error();
172 qWarning() << "Expected error code" << expectedError;
173 return false;
174 }
175
176 if (expectedError == QPlaceReply::NoError && !reply->errorString().isEmpty()) {
177 qWarning() << "Expected error was no error but error string was not empty";
178 qWarning() << "Error string=" << reply->errorString();
179 return false;
180 }
181
182 //check that we get the finished signal from the manager
183 WAIT_UNTIL(managerFinishedSpy.count() == 1);
184 if (managerFinishedSpy.count() != 1) {
185 qWarning() << "Finished signal from manager not received";
186 return false;
187 }
188
189 //check that the reply instance in the finished signal from the manager is correct
190 if (qvariant_cast<QPlaceReply *>(managerFinishedSpy.at(0).at(0)) != reply) {
191 qWarning() << "Reply instance in finished signal from manager is incorrect";
192 return false;
193 }
194
195 return true;
196 }
197
198 QTEST_GUILESS_MAIN(tst_QPlaceManagerNokia)
199
200 #include "tst_qplacemanager_nokia.moc"
201