| 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/QString> |
| 30 | #include <QtTest/QtTest> |
| 31 | #include <QtLocation/QPlaceSearchRequest> |
| 32 | #include <QtPositioning/QGeoCircle> |
| 33 | #include <QtPositioning/QGeoRectangle> |
| 34 | |
| 35 | QT_USE_NAMESPACE |
| 36 | |
| 37 | class tst_QPlaceSearchRequest : public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | public: |
| 42 | tst_QPlaceSearchRequest(); |
| 43 | |
| 44 | private Q_SLOTS: |
| 45 | void constructorTest(); |
| 46 | void searchTermTest(); |
| 47 | void categoriesTest(); |
| 48 | void boundingCircleTest(); |
| 49 | void boundingBoxTest(); |
| 50 | void searchAreaTest(); |
| 51 | void visibilityScopeTest(); |
| 52 | void relevanceHintTest(); |
| 53 | void searchContextTest(); |
| 54 | void operatorsTest(); |
| 55 | void clearTest(); |
| 56 | }; |
| 57 | |
| 58 | tst_QPlaceSearchRequest::tst_QPlaceSearchRequest() |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | void tst_QPlaceSearchRequest::constructorTest() |
| 63 | { |
| 64 | QPlaceSearchRequest testObj; |
| 65 | Q_UNUSED(testObj); |
| 66 | |
| 67 | QPlaceSearchRequest *testObjPtr = new QPlaceSearchRequest(testObj); |
| 68 | QVERIFY2(testObjPtr != NULL, "Copy constructor - null" ); |
| 69 | QVERIFY2(*testObjPtr == testObj, "Copy constructor - compare" ); |
| 70 | delete testObjPtr; |
| 71 | } |
| 72 | |
| 73 | void tst_QPlaceSearchRequest::searchTermTest() |
| 74 | { |
| 75 | QPlaceSearchRequest testObj; |
| 76 | QVERIFY2(testObj.searchTerm() == QString(), "Wrong default value" ); |
| 77 | testObj.setSearchTerm("testText" ); |
| 78 | QVERIFY2(testObj.searchTerm() == "testText" , "Wrong value returned" ); |
| 79 | } |
| 80 | |
| 81 | void tst_QPlaceSearchRequest::categoriesTest() |
| 82 | { |
| 83 | QPlaceSearchRequest testObj; |
| 84 | QVERIFY2(testObj.categories().count() == 0, "Wrong default value" ); |
| 85 | QPlaceCategory cat; |
| 86 | cat.setCategoryId("45346" ); |
| 87 | testObj.setCategory(cat); |
| 88 | QVERIFY2(testObj.categories().count() == 1, "Wrong categories count returned" ); |
| 89 | QVERIFY2(testObj.categories()[0] == cat, "Wrong category returned" ); |
| 90 | |
| 91 | testObj.setCategory(QPlaceCategory()); |
| 92 | QVERIFY(testObj.categories().isEmpty()); |
| 93 | } |
| 94 | |
| 95 | void tst_QPlaceSearchRequest::boundingCircleTest() |
| 96 | { |
| 97 | QPlaceSearchRequest query; |
| 98 | QVERIFY2(query.searchArea() == QGeoShape(), "Wrong default value" ); |
| 99 | QGeoCircle circle; |
| 100 | circle.setCenter(QGeoCoordinate(30,20)); |
| 101 | circle.setRadius(500.0); |
| 102 | query.setSearchArea(circle); |
| 103 | |
| 104 | QVERIFY(query.searchArea() != QGeoShape()); |
| 105 | QVERIFY(query.searchArea().type() == QGeoShape::CircleType); |
| 106 | QVERIFY(query.searchArea() == circle); |
| 107 | |
| 108 | QGeoCircle retrievedCircle = query.searchArea(); |
| 109 | QVERIFY2(retrievedCircle.center() == QGeoCoordinate(30,20), "Wrong value returned" ); |
| 110 | QVERIFY2(retrievedCircle.radius() == 500.0, "Wrong value returned" ); |
| 111 | query.clear(); |
| 112 | QVERIFY2(query.searchArea() == QGeoShape(), "Search area not cleared" ); |
| 113 | } |
| 114 | |
| 115 | void tst_QPlaceSearchRequest::boundingBoxTest() |
| 116 | { |
| 117 | QPlaceSearchRequest query; |
| 118 | QVERIFY2(query.searchArea() == QGeoShape(), "Wrong default value" ); |
| 119 | QGeoRectangle box; |
| 120 | |
| 121 | box.setTopLeft(QGeoCoordinate(30,20)); |
| 122 | box.setBottomRight(QGeoCoordinate(10,50)); |
| 123 | query.setSearchArea(box); |
| 124 | |
| 125 | QVERIFY(query.searchArea() != QGeoShape()); |
| 126 | QVERIFY(query.searchArea().type() == QGeoShape::RectangleType); |
| 127 | QVERIFY(query.searchArea() == box); |
| 128 | |
| 129 | QGeoRectangle retrievedBox = query.searchArea(); |
| 130 | QVERIFY2(retrievedBox.topLeft() == QGeoCoordinate(30,20), "Wrong value returned" ); |
| 131 | QVERIFY2(retrievedBox.bottomRight() == QGeoCoordinate(10,50), "Wrong value returned" ); |
| 132 | |
| 133 | query.clear(); |
| 134 | QVERIFY2(query.searchArea() == QGeoShape(), "Wrong cleared value returned" ); |
| 135 | } |
| 136 | |
| 137 | void tst_QPlaceSearchRequest::searchAreaTest() |
| 138 | { |
| 139 | //test assignment of new search area over an old search area |
| 140 | QPlaceSearchRequest *query = new QPlaceSearchRequest; |
| 141 | QGeoCircle circle; |
| 142 | circle.setCenter(QGeoCoordinate(30,20)); |
| 143 | circle.setRadius(500.0); |
| 144 | query->setSearchArea(circle); |
| 145 | |
| 146 | QVERIFY(query->searchArea() == circle); |
| 147 | QGeoRectangle box; |
| 148 | box.setTopLeft(QGeoCoordinate(30,20)); |
| 149 | box.setBottomRight(QGeoCoordinate(10,50)); |
| 150 | query->setSearchArea(box); |
| 151 | QVERIFY2(query->searchArea() == box, "New search area not assigned" ); |
| 152 | } |
| 153 | |
| 154 | void tst_QPlaceSearchRequest::visibilityScopeTest() |
| 155 | { |
| 156 | QPlaceSearchRequest query; |
| 157 | QVERIFY2(query.visibilityScope() == QLocation::UnspecifiedVisibility, "Wrong default value" ); |
| 158 | |
| 159 | query.setVisibilityScope(QLocation::DeviceVisibility); |
| 160 | QCOMPARE(query.visibilityScope(), QLocation::DeviceVisibility); |
| 161 | |
| 162 | query.setVisibilityScope(QLocation::DeviceVisibility | QLocation::PublicVisibility); |
| 163 | QVERIFY(query.visibilityScope() & QLocation::DeviceVisibility); |
| 164 | QVERIFY(!(query.visibilityScope() & QLocation::PrivateVisibility)); |
| 165 | QVERIFY(query.visibilityScope() & QLocation::PublicVisibility); |
| 166 | } |
| 167 | |
| 168 | void tst_QPlaceSearchRequest::relevanceHintTest() |
| 169 | { |
| 170 | QPlaceSearchRequest request; |
| 171 | QCOMPARE(request.relevanceHint(), QPlaceSearchRequest::UnspecifiedHint); |
| 172 | request.setRelevanceHint(QPlaceSearchRequest::DistanceHint); |
| 173 | QCOMPARE(request.relevanceHint(), QPlaceSearchRequest::DistanceHint); |
| 174 | request.setRelevanceHint(QPlaceSearchRequest::UnspecifiedHint); |
| 175 | QCOMPARE(request.relevanceHint(), QPlaceSearchRequest::UnspecifiedHint); |
| 176 | } |
| 177 | |
| 178 | void tst_QPlaceSearchRequest::searchContextTest() |
| 179 | { |
| 180 | QPlaceSearchRequest request; |
| 181 | QVERIFY(!request.searchContext().value<QUrl>().isValid()); |
| 182 | request.setSearchContext(QUrl(QStringLiteral("http://www.example.com/" ))); |
| 183 | QCOMPARE(request.searchContext().value<QUrl>(), QUrl(QStringLiteral("http://www.example.com/" ))); |
| 184 | } |
| 185 | |
| 186 | void tst_QPlaceSearchRequest::operatorsTest() |
| 187 | { |
| 188 | QPlaceSearchRequest testObj; |
| 189 | testObj.setSearchTerm(QStringLiteral("testValue" )); |
| 190 | QPlaceSearchRequest testObj2; |
| 191 | testObj2 = testObj; |
| 192 | QVERIFY2(testObj == testObj2, "Not copied correctly" ); |
| 193 | testObj2.setSearchTerm(QStringLiteral("abc" )); |
| 194 | QVERIFY2(testObj != testObj2, "Object should be different" ); |
| 195 | testObj2.setSearchTerm(QStringLiteral("testValue" )); |
| 196 | QVERIFY(testObj == testObj2); |
| 197 | |
| 198 | QGeoRectangle b1(QGeoCoordinate(20,20), QGeoCoordinate(10,30)); |
| 199 | QGeoRectangle b2(QGeoCoordinate(20,20), QGeoCoordinate(10,30)); |
| 200 | QGeoRectangle b3(QGeoCoordinate(40,40), QGeoCoordinate(10,40)); |
| 201 | |
| 202 | //testing that identical boxes match |
| 203 | testObj.setSearchArea(b1); |
| 204 | testObj2.setSearchArea(b2); |
| 205 | QVERIFY2(testObj == testObj2, "Identical box areas are not identified as matching" ); |
| 206 | |
| 207 | //test that different boxes do not match |
| 208 | testObj2.setSearchArea(b3); |
| 209 | QVERIFY2(testObj != testObj2, "Different box areas identified as matching" ); |
| 210 | |
| 211 | QGeoCircle c1(QGeoCoordinate(5,5),500); |
| 212 | QGeoCircle c2(QGeoCoordinate(5,5),500); |
| 213 | QGeoCircle c3(QGeoCoordinate(9,9),600); |
| 214 | |
| 215 | //test that identical cirlces match |
| 216 | testObj.setSearchArea(c1); |
| 217 | testObj2.setSearchArea(c2); |
| 218 | QVERIFY2(testObj == testObj2, "Identical circle areas are not identified as matching" ); |
| 219 | |
| 220 | //test that different circle don't match |
| 221 | testObj2.setSearchArea(c3); |
| 222 | QVERIFY2(testObj != testObj2, "Different circle areas identified as matching" ); |
| 223 | |
| 224 | //test that circles and boxes do not match |
| 225 | QGeoRectangle b4(QGeoCoordinate(20,20),QGeoCoordinate(10,30)); |
| 226 | QGeoCircle c4(QGeoCoordinate(20,20),500); |
| 227 | testObj.setSearchArea(b4); |
| 228 | testObj2.setSearchArea(c4); |
| 229 | QVERIFY2(testObj != testObj2, "Circle and box identified as matching" ); |
| 230 | |
| 231 | //test that identical visibility scopes match |
| 232 | testObj.clear(); |
| 233 | testObj2.clear(); |
| 234 | testObj.setVisibilityScope(QLocation::PublicVisibility); |
| 235 | testObj2.setVisibilityScope(QLocation::PublicVisibility); |
| 236 | QVERIFY2(testObj == testObj2, "Identical scopes not identified as matching" ); |
| 237 | |
| 238 | //test that different scopes do not match |
| 239 | testObj2.setVisibilityScope(QLocation::PrivateVisibility); |
| 240 | QVERIFY2(testObj != testObj2, "Different scopes identified as matching" ); |
| 241 | |
| 242 | //test that different search contexts do not match |
| 243 | testObj.clear(); |
| 244 | testObj2.clear(); |
| 245 | testObj2.setSearchContext(QUrl(QStringLiteral("http://www.example.com/" ))); |
| 246 | QVERIFY(testObj != testObj2); |
| 247 | } |
| 248 | |
| 249 | void tst_QPlaceSearchRequest::clearTest() |
| 250 | { |
| 251 | QPlaceSearchRequest req; |
| 252 | req.setSearchTerm("pizza" ); |
| 253 | req.setSearchArea(QGeoCircle(QGeoCoordinate(1,1), 5000)); |
| 254 | QPlaceCategory category; |
| 255 | category.setName("Fast Food" ); |
| 256 | req.setCategory(category); |
| 257 | req.setLimit(100); |
| 258 | |
| 259 | req.clear(); |
| 260 | QVERIFY(req.searchTerm().isEmpty()); |
| 261 | QVERIFY(req.searchArea() == QGeoShape()); |
| 262 | QVERIFY(req.categories().isEmpty()); |
| 263 | QVERIFY(req.limit() == -1); |
| 264 | } |
| 265 | |
| 266 | QTEST_APPLESS_MAIN(tst_QPlaceSearchRequest) |
| 267 | |
| 268 | #include "tst_qplacesearchrequest.moc" |
| 269 | |