| 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/QtPlugin> |
| 30 | #include <QtCore/QMetaType> |
| 31 | #include <QtCore/QString> |
| 32 | #include <QtLocation/QGeoServiceProviderFactory> |
| 33 | #include <QtLocation/QGeoServiceProvider> |
| 34 | #include <QtLocation/QPlaceManager> |
| 35 | #include <QtLocation/QPlaceManagerEngine> |
| 36 | #include <QtLocation/QPlaceSearchReply> |
| 37 | #include <QtTest/QtTest> |
| 38 | |
| 39 | QT_USE_NAMESPACE |
| 40 | |
| 41 | class tst_QPlaceManagerUnsupported : public QObject |
| 42 | { |
| 43 | Q_OBJECT |
| 44 | |
| 45 | private Q_SLOTS: |
| 46 | void initTestCase(); |
| 47 | void cleanupTestCase(); |
| 48 | |
| 49 | void testMetadata(); |
| 50 | void testLocales(); |
| 51 | |
| 52 | void testGetPlaceDetails(); |
| 53 | void testGetPlaceContent(); |
| 54 | void testSearch(); |
| 55 | void testSearchSuggestions(); |
| 56 | |
| 57 | void testSavePlace(); |
| 58 | void testRemovePlace(); |
| 59 | void testSaveCategory(); |
| 60 | void testRemoveCategory(); |
| 61 | |
| 62 | void testCategories(); |
| 63 | |
| 64 | void compatiblePlace(); |
| 65 | |
| 66 | void testMatchUnsupported(); |
| 67 | |
| 68 | private: |
| 69 | void checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError, bool *failed); |
| 70 | bool checkSignals(QPlaceReply *reply, QPlaceReply::Error expectedError); |
| 71 | |
| 72 | QGeoServiceProvider *m_provider; |
| 73 | QPlaceManager *m_manager; |
| 74 | }; |
| 75 | |
| 76 | void tst_QPlaceManagerUnsupported::initTestCase() |
| 77 | { |
| 78 | #if QT_CONFIG(library) |
| 79 | /* |
| 80 | * Set custom path since CI doesn't install test plugins |
| 81 | */ |
| 82 | #ifdef Q_OS_WIN |
| 83 | QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath() + |
| 84 | QStringLiteral("/../../../../plugins" )); |
| 85 | #else |
| 86 | QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath() |
| 87 | + QStringLiteral("/../../../plugins" )); |
| 88 | #endif |
| 89 | #endif |
| 90 | |
| 91 | m_provider = 0; |
| 92 | m_manager = 0; |
| 93 | |
| 94 | QStringList providers = QGeoServiceProvider::availableServiceProviders(); |
| 95 | QVERIFY(providers.contains("test.places.unsupported" )); |
| 96 | |
| 97 | m_provider = new QGeoServiceProvider("test.places.unsupported" ); |
| 98 | QVERIFY(m_provider); |
| 99 | QCOMPARE(m_provider->error(), QGeoServiceProvider::NotSupportedError); |
| 100 | m_provider->setAllowExperimental(true); |
| 101 | QCOMPARE(m_provider->error(), QGeoServiceProvider::NoError); |
| 102 | |
| 103 | m_manager = m_provider->placeManager(); |
| 104 | QVERIFY(m_manager); |
| 105 | } |
| 106 | |
| 107 | void tst_QPlaceManagerUnsupported::cleanupTestCase() |
| 108 | { |
| 109 | delete m_provider; |
| 110 | } |
| 111 | |
| 112 | void tst_QPlaceManagerUnsupported::testMetadata() |
| 113 | { |
| 114 | QCOMPARE(m_manager->managerName(), QStringLiteral("test.places.unsupported" )); |
| 115 | QCOMPARE(m_manager->managerVersion(), 1); |
| 116 | QCOMPARE(m_provider->placesFeatures(), QGeoServiceProvider::NoPlacesFeatures); |
| 117 | } |
| 118 | |
| 119 | void tst_QPlaceManagerUnsupported::testLocales() |
| 120 | { |
| 121 | QVERIFY(m_manager->locales().isEmpty()); |
| 122 | |
| 123 | QLocale locale(QLocale::NorwegianBokmal, QLocale::Norway); |
| 124 | m_manager->setLocale(locale); |
| 125 | |
| 126 | QVERIFY(m_manager->locales().isEmpty()); |
| 127 | |
| 128 | QList<QLocale> locales; |
| 129 | QLocale en_AU = QLocale(QLocale::English, QLocale::Australia); |
| 130 | QLocale en_UK = QLocale(QLocale::English, QLocale::UnitedKingdom); |
| 131 | locales << en_AU << en_UK; |
| 132 | m_manager->setLocales(locales); |
| 133 | |
| 134 | QVERIFY(m_manager->locales().isEmpty()); |
| 135 | } |
| 136 | |
| 137 | void tst_QPlaceManagerUnsupported::testGetPlaceDetails() |
| 138 | { |
| 139 | QPlaceDetailsReply *reply = m_manager->getPlaceDetails(placeId: QString()); |
| 140 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | void tst_QPlaceManagerUnsupported::testGetPlaceContent() |
| 145 | { |
| 146 | QPlaceContentReply *reply = m_manager->getPlaceContent(request: QPlaceContentRequest()); |
| 147 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | void tst_QPlaceManagerUnsupported::testSearch() |
| 152 | { |
| 153 | QPlaceSearchReply *reply = m_manager->search(query: QPlaceSearchRequest()); |
| 154 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | void tst_QPlaceManagerUnsupported::testSearchSuggestions() |
| 159 | { |
| 160 | QPlaceSearchSuggestionReply *reply = m_manager->searchSuggestions(request: QPlaceSearchRequest()); |
| 161 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | void tst_QPlaceManagerUnsupported::testSavePlace() |
| 166 | { |
| 167 | QPlaceIdReply *reply = m_manager->savePlace(place: QPlace()); |
| 168 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | void tst_QPlaceManagerUnsupported::testRemovePlace() |
| 173 | { |
| 174 | QPlaceIdReply *reply = m_manager->removePlace(placeId: QString()); |
| 175 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | void tst_QPlaceManagerUnsupported::testSaveCategory() |
| 180 | { |
| 181 | QPlaceIdReply *reply = m_manager->saveCategory(category: QPlaceCategory()); |
| 182 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | void tst_QPlaceManagerUnsupported::testRemoveCategory() |
| 187 | { |
| 188 | QPlaceIdReply *reply = m_manager->removeCategory(categoryId: QString()); |
| 189 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | void tst_QPlaceManagerUnsupported::testCategories() |
| 194 | { |
| 195 | QPlaceReply *reply = m_manager->initializeCategories(); |
| 196 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 197 | return; |
| 198 | |
| 199 | QVERIFY(m_manager->childCategoryIds().isEmpty()); |
| 200 | QVERIFY(m_manager->parentCategoryId(QString()).isEmpty()); |
| 201 | QCOMPARE(m_manager->category(QString()), QPlaceCategory()); |
| 202 | } |
| 203 | |
| 204 | void tst_QPlaceManagerUnsupported::compatiblePlace() |
| 205 | { |
| 206 | QPlace place; |
| 207 | place.setPlaceId(QStringLiteral("4-8-15-16-23-42" )); |
| 208 | place.setName(QStringLiteral("Island" )); |
| 209 | place.setVisibility(QLocation::PublicVisibility); |
| 210 | |
| 211 | QPlace compatPlace = m_manager->compatiblePlace(place); |
| 212 | QCOMPARE(compatPlace, QPlace()); |
| 213 | } |
| 214 | |
| 215 | void tst_QPlaceManagerUnsupported::testMatchUnsupported() |
| 216 | { |
| 217 | QPlaceMatchReply *reply = m_manager->matchingPlaces(request: QPlaceMatchRequest()); |
| 218 | if (!checkSignals(reply, expectedError: QPlaceReply::UnsupportedError)) |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | void tst_QPlaceManagerUnsupported::checkSignals(QPlaceReply *reply, |
| 223 | QPlaceReply::Error expectedError, bool *failed) |
| 224 | { |
| 225 | *failed = true; |
| 226 | |
| 227 | QSignalSpy finishedSpy(reply, SIGNAL(finished())); |
| 228 | QSignalSpy errorSpy(reply, SIGNAL(error(QPlaceReply::Error,QString))); |
| 229 | QSignalSpy managerFinishedSpy(m_manager, SIGNAL(finished(QPlaceReply*))); |
| 230 | QSignalSpy managerErrorSpy(m_manager,SIGNAL(error(QPlaceReply*,QPlaceReply::Error,QString))); |
| 231 | |
| 232 | if (expectedError != QPlaceReply::NoError) { |
| 233 | //check that we get an error signal from the reply |
| 234 | QTRY_VERIFY(errorSpy.count() == 1); |
| 235 | |
| 236 | //check that we get the correct error from the reply's signal |
| 237 | QPlaceReply::Error actualError = qvariant_cast<QPlaceReply::Error>(v: errorSpy.at(i: 0).at(i: 0)); |
| 238 | QCOMPARE(actualError, expectedError); |
| 239 | |
| 240 | //check that we get an error signal from the manager |
| 241 | QTRY_VERIFY(managerErrorSpy.count() == 1); |
| 242 | |
| 243 | //check that we get the correct reply instance in the error signal from the manager |
| 244 | QPlaceReply *managerReply = qvariant_cast<QPlaceReply*>(v: managerErrorSpy.at(i: 0).at(i: 0)); |
| 245 | QCOMPARE(managerReply, reply); |
| 246 | |
| 247 | //check that we get the correct error from the signal of the manager |
| 248 | actualError = qvariant_cast<QPlaceReply::Error>(v: managerErrorSpy.at(i: 0).at(i: 1)); |
| 249 | QCOMPARE(actualError, expectedError); |
| 250 | } |
| 251 | |
| 252 | //check that we get a finished signal |
| 253 | QTRY_VERIFY(finishedSpy.count() == 1); |
| 254 | |
| 255 | QCOMPARE(reply->error(), expectedError); |
| 256 | |
| 257 | QCOMPARE(reply->errorString().isEmpty(), expectedError == QPlaceReply::NoError); |
| 258 | |
| 259 | //check that we get the finished signal from the manager |
| 260 | QTRY_VERIFY(managerFinishedSpy.count() == 1); |
| 261 | |
| 262 | //check that the reply instance in the finished signal from the manager is correct |
| 263 | QPlaceReply *managerReply = qvariant_cast<QPlaceReply *>(v: managerFinishedSpy.at(i: 0).at(i: 0)); |
| 264 | QCOMPARE(managerReply, reply); |
| 265 | |
| 266 | *failed = false; |
| 267 | } |
| 268 | |
| 269 | bool tst_QPlaceManagerUnsupported::checkSignals(QPlaceReply *reply, |
| 270 | QPlaceReply::Error expectedError) |
| 271 | { |
| 272 | bool failed; |
| 273 | checkSignals(reply, expectedError, failed: &failed); |
| 274 | return failed; |
| 275 | } |
| 276 | |
| 277 | QTEST_GUILESS_MAIN(tst_QPlaceManagerUnsupported) |
| 278 | |
| 279 | #include "tst_qplacemanager_unsupported.moc" |
| 280 | |