| 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 "tst_qgeoroute.h" |
| 30 | #include "../geotestplugin/qgeoroutingmanagerengine_test.h" |
| 31 | |
| 32 | |
| 33 | tst_QGeoRoute::tst_QGeoRoute() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | void tst_QGeoRoute::initTestCase() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void tst_QGeoRoute::cleanupTestCase() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void tst_QGeoRoute::init() |
| 46 | { |
| 47 | qgeoroute = new QGeoRoute(); |
| 48 | qgeocoordinate = new QGeoCoordinate(); |
| 49 | } |
| 50 | |
| 51 | void tst_QGeoRoute::cleanup() |
| 52 | { |
| 53 | delete qgeoroute; |
| 54 | delete qgeocoordinate; |
| 55 | } |
| 56 | |
| 57 | void tst_QGeoRoute::constructor() |
| 58 | { |
| 59 | QString empty = "" ; |
| 60 | QGeoRectangle *boundingbox = new QGeoRectangle(); |
| 61 | |
| 62 | QCOMPARE(qgeoroute->bounds(), *boundingbox); |
| 63 | QCOMPARE(qgeoroute->distance(), qreal(0.0)); |
| 64 | QCOMPARE(qgeoroute->path().size(), 0); |
| 65 | QCOMPARE(qgeoroute->routeId(), empty); |
| 66 | QCOMPARE(qgeoroute->travelTime(), 0); |
| 67 | |
| 68 | delete boundingbox; |
| 69 | } |
| 70 | |
| 71 | void tst_QGeoRoute::copy_constructor() |
| 72 | { |
| 73 | QGeoRoute *qgeoroutecopy = new QGeoRoute(*qgeoroute); |
| 74 | QCOMPARE(*qgeoroute, *qgeoroutecopy); |
| 75 | |
| 76 | // CoW |
| 77 | qreal distance = qgeoroute->distance(); |
| 78 | qgeoroutecopy->setDistance(distance + 10.0); |
| 79 | |
| 80 | QVERIFY(*qgeoroute == *qgeoroutecopy); // QGeoRoute uses a QExplicitlySharedDataPointer. no implicit detach() |
| 81 | |
| 82 | delete qgeoroutecopy; |
| 83 | } |
| 84 | |
| 85 | void tst_QGeoRoute::destructor() |
| 86 | { |
| 87 | QGeoRoute *qgeoroutecopy; |
| 88 | |
| 89 | qgeoroutecopy = new QGeoRoute(); |
| 90 | delete qgeoroutecopy; |
| 91 | |
| 92 | qgeoroutecopy = new QGeoRoute(*qgeoroute); |
| 93 | delete qgeoroutecopy; |
| 94 | } |
| 95 | |
| 96 | void tst_QGeoRoute::bounds() |
| 97 | { |
| 98 | qgeocoordinate->setLatitude(13.3851); |
| 99 | qgeocoordinate->setLongitude(52.5312); |
| 100 | |
| 101 | QGeoRectangle *qgeoboundingbox = new QGeoRectangle(*qgeocoordinate,0.4,0.4); |
| 102 | |
| 103 | qgeoroute->setBounds(*qgeoboundingbox); |
| 104 | |
| 105 | QCOMPARE(qgeoroute->bounds(), *qgeoboundingbox); |
| 106 | |
| 107 | qgeoboundingbox->setWidth(23.1); |
| 108 | |
| 109 | QVERIFY(qgeoroute->bounds().width() != qgeoboundingbox->width()); |
| 110 | |
| 111 | delete qgeoboundingbox; |
| 112 | } |
| 113 | |
| 114 | void tst_QGeoRoute::distance() |
| 115 | { |
| 116 | qreal distance = 0.0; |
| 117 | |
| 118 | qgeoroute->setDistance(distance); |
| 119 | QCOMPARE(qgeoroute->distance(), distance); |
| 120 | |
| 121 | distance = 34.4324; |
| 122 | QVERIFY(qgeoroute->distance() != distance); |
| 123 | |
| 124 | qgeoroute->setDistance(distance); |
| 125 | QCOMPARE(qgeoroute->distance(), distance); |
| 126 | } |
| 127 | |
| 128 | void tst_QGeoRoute::path() |
| 129 | { |
| 130 | QFETCH(QList<double>, coordinates); |
| 131 | |
| 132 | QList<QGeoCoordinate> path; |
| 133 | |
| 134 | for (int i = 0; i < coordinates.size(); i += 2) { |
| 135 | path.append(t: QGeoCoordinate(coordinates.at(i),coordinates.at(i: i+1))); |
| 136 | } |
| 137 | |
| 138 | qgeoroute->setPath(path); |
| 139 | |
| 140 | QList<QGeoCoordinate> pathRetrieved = qgeoroute->path(); |
| 141 | QCOMPARE(pathRetrieved, path); |
| 142 | |
| 143 | for (int i = 0; i < pathRetrieved.size(); i++) { |
| 144 | QCOMPARE(pathRetrieved.at(i), path.at(i)); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void tst_QGeoRoute::path_data() |
| 149 | { |
| 150 | QTest::addColumn<QList<double> >(name: "coordinates" ); |
| 151 | |
| 152 | QList<double> coordinates; |
| 153 | |
| 154 | coordinates << 0.0 << 0.0; |
| 155 | QTest::newRow(dataTag: "path1" ) << coordinates ; |
| 156 | |
| 157 | coordinates << -23.23 << 54.45345; |
| 158 | QTest::newRow(dataTag: "path2" ) << coordinates ; |
| 159 | |
| 160 | coordinates << -85.4324 << -121.343; |
| 161 | QTest::newRow(dataTag: "path3" ) << coordinates ; |
| 162 | |
| 163 | coordinates << 1.323 << 12.323; |
| 164 | QTest::newRow(dataTag: "path4" ) << coordinates ; |
| 165 | |
| 166 | coordinates << 1324.323 << 143242.323; |
| 167 | QTest::newRow(dataTag: "path5" ) << coordinates ; |
| 168 | } |
| 169 | |
| 170 | void tst_QGeoRoute::request() |
| 171 | { |
| 172 | qgeocoordinate->setLatitude(65.654); |
| 173 | qgeocoordinate->setLongitude(0.4324); |
| 174 | |
| 175 | QGeoCoordinate *qgeocoordinatecopy = new QGeoCoordinate(34.54 , -21.32); |
| 176 | |
| 177 | QList<QGeoCoordinate> path; |
| 178 | path.append(t: *qgeocoordinate); |
| 179 | path.append(t: *qgeocoordinatecopy); |
| 180 | |
| 181 | qgeorouterequest = new QGeoRouteRequest(path); |
| 182 | |
| 183 | qgeoroute->setRequest(*qgeorouterequest); |
| 184 | |
| 185 | QCOMPARE(qgeoroute->request(), *qgeorouterequest); |
| 186 | |
| 187 | QGeoCoordinate *qgeocoordinatecopy2 = new QGeoCoordinate(4.7854 , -121.32); |
| 188 | path.append(t: *qgeocoordinatecopy2); |
| 189 | |
| 190 | QGeoRouteRequest *qgeorouterequestcopy = new QGeoRouteRequest(path); |
| 191 | |
| 192 | QVERIFY(qgeoroute->request() != *qgeorouterequestcopy); |
| 193 | |
| 194 | delete qgeocoordinatecopy; |
| 195 | delete qgeocoordinatecopy2; |
| 196 | delete qgeorouterequest; |
| 197 | delete qgeorouterequestcopy; |
| 198 | } |
| 199 | |
| 200 | void tst_QGeoRoute::routeId() |
| 201 | { |
| 202 | QString text = "routeId 4504" ; |
| 203 | |
| 204 | qgeoroute->setRouteId(text); |
| 205 | |
| 206 | QCOMPARE(qgeoroute->routeId(), text); |
| 207 | |
| 208 | text = "routeId 1111" ; |
| 209 | QVERIFY(qgeoroute->routeId() != text); |
| 210 | |
| 211 | } |
| 212 | |
| 213 | void tst_QGeoRoute::firstrouteSegments() |
| 214 | { |
| 215 | qgeoroutesegment = new QGeoRouteSegment(); |
| 216 | qgeoroutesegment->setDistance(35.453); |
| 217 | qgeoroutesegment->setTravelTime(56); |
| 218 | |
| 219 | qgeoroute->setFirstRouteSegment(*qgeoroutesegment); |
| 220 | |
| 221 | QCOMPARE(qgeoroute->firstRouteSegment(), *qgeoroutesegment); |
| 222 | |
| 223 | QGeoRouteSegment *qgeoroutesegmentcopy = new QGeoRouteSegment (); |
| 224 | qgeoroutesegmentcopy->setDistance(435.432); |
| 225 | qgeoroutesegmentcopy->setTravelTime(786); |
| 226 | |
| 227 | QVERIFY(qgeoroute->firstRouteSegment() != *qgeoroutesegmentcopy); |
| 228 | |
| 229 | delete qgeoroutesegment; |
| 230 | delete qgeoroutesegmentcopy; |
| 231 | |
| 232 | } |
| 233 | |
| 234 | void tst_QGeoRoute::travelMode() |
| 235 | { |
| 236 | QFETCH(QGeoRouteRequest::TravelMode, mode); |
| 237 | |
| 238 | qgeoroute->setTravelMode(mode); |
| 239 | QCOMPARE(qgeoroute->travelMode(), mode); |
| 240 | } |
| 241 | void tst_QGeoRoute::travelMode_data() |
| 242 | { |
| 243 | QTest::addColumn<QGeoRouteRequest::TravelMode>(name: "mode" ); |
| 244 | |
| 245 | QTest::newRow(dataTag: "mode1" ) << QGeoRouteRequest::CarTravel; |
| 246 | QTest::newRow(dataTag: "mode2" ) << QGeoRouteRequest::PedestrianTravel; |
| 247 | QTest::newRow(dataTag: "mode3" ) << QGeoRouteRequest::BicycleTravel; |
| 248 | QTest::newRow(dataTag: "mode4" ) << QGeoRouteRequest::PublicTransitTravel; |
| 249 | QTest::newRow(dataTag: "mode5" ) << QGeoRouteRequest::TruckTravel; |
| 250 | } |
| 251 | |
| 252 | void tst_QGeoRoute::travelTime() |
| 253 | { |
| 254 | int time = 0; |
| 255 | qgeoroute->setTravelTime(time); |
| 256 | |
| 257 | QCOMPARE (qgeoroute->travelTime(), time); |
| 258 | |
| 259 | time = 35; |
| 260 | |
| 261 | QVERIFY (qgeoroute->travelTime() != time); |
| 262 | |
| 263 | qgeoroute->setTravelTime(time); |
| 264 | QCOMPARE (qgeoroute->travelTime(), time); |
| 265 | } |
| 266 | |
| 267 | void tst_QGeoRoute::operators() |
| 268 | { |
| 269 | QGeoRoute *qgeoroutecopy = new QGeoRoute(*qgeoroute); |
| 270 | |
| 271 | QVERIFY(qgeoroute->operator ==(*qgeoroutecopy)); |
| 272 | QVERIFY(!qgeoroute->operator !=(*qgeoroutecopy)); |
| 273 | |
| 274 | qgeoroute->setDistance(543.324); // QExplicitlySharedDataPointer does not detach implicitly. |
| 275 | qgeoroute->setRouteId("RouteId 111" ); |
| 276 | qgeoroute->setTravelMode(QGeoRouteRequest::PedestrianTravel); |
| 277 | qgeoroute->setTravelTime(10); |
| 278 | |
| 279 | qgeoroutecopy->setDistance(12.21); |
| 280 | qgeoroutecopy->setRouteId("RouteId 666" ); |
| 281 | qgeoroutecopy->setTravelMode(QGeoRouteRequest::BicycleTravel); |
| 282 | qgeoroutecopy->setTravelTime(99); |
| 283 | |
| 284 | QEXPECT_FAIL("" , "QGeoRoute equality operators broken" , Continue); |
| 285 | QVERIFY(!(qgeoroute->operator ==(*qgeoroutecopy))); |
| 286 | QEXPECT_FAIL("" , "QGeoRoute equality operators broken" , Continue); |
| 287 | QVERIFY(qgeoroute->operator !=(*qgeoroutecopy)); |
| 288 | |
| 289 | *qgeoroutecopy = qgeoroutecopy->operator =(other: *qgeoroute); |
| 290 | QVERIFY(qgeoroute->operator ==(*qgeoroutecopy)); |
| 291 | QVERIFY(!qgeoroute->operator !=(*qgeoroutecopy)); |
| 292 | |
| 293 | |
| 294 | QGeoRouteAlt rAlt; |
| 295 | QGeoRoute r; |
| 296 | QCOMPARE(rAlt.travelTime(), 123456); |
| 297 | QCOMPARE(r.travelTime(), 0); |
| 298 | r = rAlt; |
| 299 | QCOMPARE(r.travelTime(), 123456); |
| 300 | |
| 301 | delete qgeoroutecopy; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | |
| 306 | QTEST_APPLESS_MAIN(tst_QGeoRoute); |
| 307 | |