| 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 <QtCore/QMetaType> |
| 30 | #include <QString> |
| 31 | #include <QtTest/QtTest> |
| 32 | |
| 33 | #include <qgeoserviceprovider.h> |
| 34 | #include <qplacemanager.h> |
| 35 | |
| 36 | |
| 37 | #ifndef WAIT_UNTIL |
| 38 | #define WAIT_UNTIL(__expr) \ |
| 39 | do { \ |
| 40 | const int __step = 50; \ |
| 41 | const int __timeout = 5000; \ |
| 42 | if (!(__expr)) { \ |
| 43 | QTest::qWait(0); \ |
| 44 | } \ |
| 45 | for (int __i = 0; __i < __timeout && !(__expr); __i+=__step) { \ |
| 46 | QTest::qWait(__step); \ |
| 47 | } \ |
| 48 | } while (0) |
| 49 | #endif |
| 50 | |
| 51 | QT_USE_NAMESPACE |
| 52 | |
| 53 | class tst_QPlaceManager : public QObject |
| 54 | { |
| 55 | Q_OBJECT |
| 56 | |
| 57 | private Q_SLOTS: |
| 58 | void initTestCase(); |
| 59 | void cleanupTestCase(); |
| 60 | |
| 61 | void compatiblePlace(); |
| 62 | void testMetadata(); |
| 63 | void testLocales(); |
| 64 | void testMatchUnsupported(); |
| 65 | |
| 66 | private: |
| 67 | bool checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError); |
| 68 | |
| 69 | QGeoServiceProvider *provider; |
| 70 | QPlaceManager *placeManager; |
| 71 | }; |
| 72 | |
| 73 | void tst_QPlaceManager::initTestCase() |
| 74 | { |
| 75 | #if QT_CONFIG(library) |
| 76 | /* |
| 77 | * Set custom path since CI doesn't install test plugins |
| 78 | */ |
| 79 | #ifdef Q_OS_WIN |
| 80 | QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath() + |
| 81 | QStringLiteral("/../../../../plugins" )); |
| 82 | #else |
| 83 | QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath() |
| 84 | + QStringLiteral("/../../../plugins" )); |
| 85 | #endif |
| 86 | #endif |
| 87 | provider = 0; |
| 88 | |
| 89 | QStringList providers = QGeoServiceProvider::availableServiceProviders(); |
| 90 | QVERIFY(providers.contains("qmlgeo.test.plugin" )); |
| 91 | |
| 92 | provider = new QGeoServiceProvider("qmlgeo.test.plugin" ); |
| 93 | provider->setAllowExperimental(true); |
| 94 | QCOMPARE(provider->placesFeatures() & QGeoServiceProvider::OfflinePlacesFeature, |
| 95 | QGeoServiceProvider::OfflinePlacesFeature); |
| 96 | placeManager = provider->placeManager(); |
| 97 | QVERIFY(placeManager); |
| 98 | } |
| 99 | |
| 100 | void tst_QPlaceManager::testMetadata() |
| 101 | { |
| 102 | QCOMPARE(placeManager->managerName(), QStringLiteral("qmlgeo.test.plugin" )); |
| 103 | QCOMPARE(placeManager->managerVersion(), 100); |
| 104 | } |
| 105 | |
| 106 | void tst_QPlaceManager::testLocales() |
| 107 | { |
| 108 | QCOMPARE(placeManager->locales().count(), 1); |
| 109 | QCOMPARE(placeManager->locales().at(0), QLocale()); |
| 110 | |
| 111 | QLocale locale(QLocale::NorwegianBokmal, QLocale::Norway); |
| 112 | placeManager->setLocale(locale); |
| 113 | |
| 114 | QCOMPARE(placeManager->locales().at(0), locale); |
| 115 | |
| 116 | QList<QLocale> locales; |
| 117 | QLocale en_AU = QLocale(QLocale::English, QLocale::Australia); |
| 118 | QLocale en_UK = QLocale(QLocale::English, QLocale::UnitedKingdom); |
| 119 | locales << en_AU << en_UK; |
| 120 | placeManager->setLocales(locales); |
| 121 | QCOMPARE(placeManager->locales().count(), 2); |
| 122 | QCOMPARE(placeManager->locales().at(0), en_AU); |
| 123 | QCOMPARE(placeManager->locales().at(1), en_UK); |
| 124 | } |
| 125 | |
| 126 | void tst_QPlaceManager::testMatchUnsupported() |
| 127 | { |
| 128 | QPlaceMatchRequest request; |
| 129 | QPlaceMatchReply *reply = placeManager->matchingPlaces(request); |
| 130 | QVERIFY(checkSignals(reply, QPlaceReply::UnsupportedError)); |
| 131 | } |
| 132 | |
| 133 | void tst_QPlaceManager::compatiblePlace() |
| 134 | { |
| 135 | QPlace place; |
| 136 | place.setPlaceId(QStringLiteral("4-8-15-16-23-42" )); |
| 137 | place.setName(QStringLiteral("Island" )); |
| 138 | place.setVisibility(QLocation::PublicVisibility); |
| 139 | |
| 140 | QPlace compatPlace = placeManager->compatiblePlace(place); |
| 141 | QVERIFY(compatPlace.placeId().isEmpty()); |
| 142 | QCOMPARE(compatPlace.name(), QStringLiteral("Island" )); |
| 143 | QCOMPARE(compatPlace.visibility(), QLocation::UnspecifiedVisibility); |
| 144 | } |
| 145 | |
| 146 | void tst_QPlaceManager::cleanupTestCase() |
| 147 | { |
| 148 | delete provider; |
| 149 | } |
| 150 | |
| 151 | bool tst_QPlaceManager::checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError) |
| 152 | { |
| 153 | QSignalSpy finishedSpy(reply, SIGNAL(finished())); |
| 154 | QSignalSpy errorSpy(reply, SIGNAL(error(QPlaceReply::Error,QString))); |
| 155 | QSignalSpy managerFinishedSpy(placeManager, SIGNAL(finished(QPlaceReply*))); |
| 156 | QSignalSpy managerErrorSpy(placeManager,SIGNAL(error(QPlaceReply*,QPlaceReply::Error,QString))); |
| 157 | |
| 158 | if (expectedError != QPlaceReply::NoError) { |
| 159 | //check that we get an error signal from the reply |
| 160 | WAIT_UNTIL(errorSpy.count() == 1); |
| 161 | if (errorSpy.count() != 1) { |
| 162 | qWarning() << "Error signal for search operation not received" ; |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | //check that we get the correct error from the reply's signal |
| 167 | QPlaceReply::Error actualError = qvariant_cast<QPlaceReply::Error>(v: errorSpy.at(i: 0).at(i: 0)); |
| 168 | if (actualError != expectedError) { |
| 169 | qWarning() << "Actual error code in reply signal does not match expected error code" ; |
| 170 | qWarning() << "Actual error code = " << actualError; |
| 171 | qWarning() << "Expected error coe =" << expectedError; |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | //check that we get an error signal from the manager |
| 176 | WAIT_UNTIL(managerErrorSpy.count() == 1); |
| 177 | if (managerErrorSpy.count() !=1) { |
| 178 | qWarning() << "Error signal from manager for search operation not received" ; |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | //check that we get the correct reply instance in the error signal from the manager |
| 183 | if (qvariant_cast<QPlaceReply*>(v: managerErrorSpy.at(i: 0).at(i: 0)) != reply) { |
| 184 | qWarning() << "Reply instance in error signal from manager is incorrect" ; |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | //check that we get the correct error from the signal of the manager |
| 189 | actualError = qvariant_cast<QPlaceReply::Error>(v: managerErrorSpy.at(i: 0).at(i: 1)); |
| 190 | if (actualError != expectedError) { |
| 191 | qWarning() << "Actual error code from manager signal does not match expected error code" ; |
| 192 | qWarning() << "Actual error code =" << actualError; |
| 193 | qWarning() << "Expected error code = " << expectedError; |
| 194 | return false; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | //check that we get a finished signal |
| 199 | WAIT_UNTIL(finishedSpy.count() == 1); |
| 200 | if (finishedSpy.count() !=1) { |
| 201 | qWarning() << "Finished signal from reply not received" ; |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | if (reply->error() != expectedError) { |
| 206 | qWarning() << "Actual error code does not match expected error code" ; |
| 207 | qWarning() << "Actual error code: " << reply->error(); |
| 208 | qWarning() << "Expected error code" << expectedError; |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | if (expectedError == QPlaceReply::NoError && !reply->errorString().isEmpty()) { |
| 213 | qWarning() << "Expected error was no error but error string was not empty" ; |
| 214 | qWarning() << "Error string=" << reply->errorString(); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | //check that we get the finished signal from the manager |
| 219 | WAIT_UNTIL(managerFinishedSpy.count() == 1); |
| 220 | if (managerFinishedSpy.count() != 1) { |
| 221 | qWarning() << "Finished signal from manager not received" ; |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | //check that the reply instance in the finished signal from the manager is correct |
| 226 | if (qvariant_cast<QPlaceReply *>(v: managerFinishedSpy.at(i: 0).at(i: 0)) != reply) { |
| 227 | qWarning() << "Reply instance in finished signal from manager is incorrect" ; |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | QTEST_GUILESS_MAIN(tst_QPlaceManager) |
| 235 | |
| 236 | #include "tst_qplacemanager.moc" |
| 237 | |