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 QtLocation module 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_qgeomaneuver.h"
30 #include <QtLocation/private/qgeomaneuver_p.h>
31 
32 class QGeoManeuverPrivateDefaultAlt : public QGeoManeuverPrivateDefault
33 {
34 public:
QGeoManeuverPrivateDefaultAlt()35     QGeoManeuverPrivateDefaultAlt() {}
QGeoManeuverPrivateDefaultAlt(const QGeoManeuverPrivateDefaultAlt & other)36     QGeoManeuverPrivateDefaultAlt(const QGeoManeuverPrivateDefaultAlt &other)
37         : QGeoManeuverPrivateDefault(other) {}
~QGeoManeuverPrivateDefaultAlt()38     ~QGeoManeuverPrivateDefaultAlt() {}
39 
text() const40     QString text() const override
41     {
42         return QStringLiteral("QGeoManeuverPrivateDefaultAlt"); // To identify this is actually a QGeoManeuverPrivateDefaultAlt
43     }
44 };
45 
46 class QGeoManeuverAlt : public QGeoManeuver
47 {
48 public:
QGeoManeuverAlt()49     QGeoManeuverAlt()
50     : QGeoManeuver(QSharedDataPointer<QGeoManeuverPrivate>(new QGeoManeuverPrivateDefaultAlt()))
51     {
52     }
53 };
54 
tst_QGeoManeuver()55 tst_QGeoManeuver::tst_QGeoManeuver()
56 {
57 }
58 
initTestCase()59 void tst_QGeoManeuver::initTestCase()
60 {
61 
62 }
63 
cleanupTestCase()64 void tst_QGeoManeuver::cleanupTestCase()
65 {
66 
67 }
68 
init()69 void tst_QGeoManeuver::init()
70 {
71 
72     qgeomaneuver = new QGeoManeuver();
73 }
74 
cleanup()75 void tst_QGeoManeuver::cleanup()
76 {
77     delete qgeomaneuver;
78 }
79 
constructor()80 void tst_QGeoManeuver::constructor()
81 {
82     QString empty ="";
83 
84     QVERIFY(!qgeomaneuver->isValid());
85     QCOMPARE(qgeomaneuver->direction(),QGeoManeuver::NoDirection);
86     QCOMPARE(qgeomaneuver->distanceToNextInstruction(), qreal(0.0));
87     QCOMPARE(qgeomaneuver->instructionText(),empty);
88     QCOMPARE(qgeomaneuver->timeToNextInstruction(),0);
89 }
90 
copy_constructor()91 void tst_QGeoManeuver::copy_constructor()
92 {
93     QGeoManeuver *qgeomaneuvercopy = new QGeoManeuver (*qgeomaneuver);
94 
95     QCOMPARE(*qgeomaneuver,*qgeomaneuvercopy);
96 
97     delete qgeomaneuvercopy;
98 }
99 
destructor()100 void tst_QGeoManeuver::destructor()
101 {
102     QGeoManeuver *qgeomaneuvercopy;
103 
104     qgeomaneuvercopy = new QGeoManeuver();
105     delete qgeomaneuvercopy;
106 
107     qgeomaneuvercopy = new QGeoManeuver(*qgeomaneuver);
108     delete qgeomaneuvercopy;
109 }
110 
direction()111 void tst_QGeoManeuver::direction()
112 {
113     QFETCH(QGeoManeuver::InstructionDirection,direction);
114 
115     qgeomaneuver->setDirection(direction);
116 
117     QCOMPARE(qgeomaneuver->direction(),direction);
118 }
direction_data()119 void tst_QGeoManeuver::direction_data()
120 {
121     QTest::addColumn<QGeoManeuver::InstructionDirection>("direction");
122 
123     QTest::newRow("instruction1") << QGeoManeuver::NoDirection;
124     QTest::newRow("instruction2") << QGeoManeuver::DirectionForward;
125     QTest::newRow("instruction3") << QGeoManeuver::DirectionBearRight;
126     QTest::newRow("instruction4") << QGeoManeuver::DirectionLightRight;
127     QTest::newRow("instruction5") << QGeoManeuver::DirectionRight;
128     QTest::newRow("instruction6") << QGeoManeuver::DirectionHardRight;
129     QTest::newRow("instruction7") << QGeoManeuver::DirectionUTurnRight;
130     QTest::newRow("instruction8") << QGeoManeuver::DirectionUTurnLeft;
131     QTest::newRow("instruction9") << QGeoManeuver::DirectionHardLeft;
132     QTest::newRow("instruction10") << QGeoManeuver::DirectionLeft;
133     QTest::newRow("instruction11") << QGeoManeuver::DirectionLightLeft;
134     QTest::newRow("instruction12") << QGeoManeuver::DirectionBearLeft;
135 }
136 
distanceToNextInstruction()137 void tst_QGeoManeuver::distanceToNextInstruction()
138 {
139     qreal distance = 0.0;
140     qgeomaneuver->setDistanceToNextInstruction(distance);
141 
142     QCOMPARE (qgeomaneuver->distanceToNextInstruction(), distance);
143 
144     distance = -3423.4324;
145 
146     QVERIFY (qgeomaneuver->distanceToNextInstruction() != distance);
147 
148     qgeomaneuver->setDistanceToNextInstruction(distance);
149     QCOMPARE (qgeomaneuver->distanceToNextInstruction(),distance);
150 }
151 
instructionText()152 void tst_QGeoManeuver::instructionText()
153 {
154     QString text = "After 50m turn left";
155 
156     qgeomaneuver->setInstructionText(text);
157 
158     QCOMPARE (qgeomaneuver->instructionText(),text);
159 
160     text="After 40m, turn left";
161     QVERIFY (qgeomaneuver->instructionText() != text);
162 
163 }
164 
position()165 void tst_QGeoManeuver::position()
166 {
167     QFETCH(double, latitude);
168     QFETCH(double, longitude);
169 
170     qgeocoordinate = new QGeoCoordinate (latitude,longitude);
171 
172     qgeomaneuver->setPosition(*qgeocoordinate);
173 
174     QCOMPARE(qgeomaneuver->position(),*qgeocoordinate);
175 
176     delete qgeocoordinate;
177 }
178 
position_data()179 void tst_QGeoManeuver::position_data()
180 {
181     QTest::addColumn<double>("latitude");
182     QTest::addColumn<double>("longitude");
183 
184     QTest::newRow("invalid0") << -12220.0 << 0.0;
185     QTest::newRow("invalid1") << 0.0 << 181.0;
186 
187     QTest::newRow("correct0") << 0.0 << 0.0;
188     QTest::newRow("correct1") << 90.0 << 0.0;
189     QTest::newRow("correct2") << 0.0 << 180.0;
190     QTest::newRow("correct3") << -90.0 << 0.0;
191     QTest::newRow("correct4") << 0.0 << -180.0;
192     QTest::newRow("correct5") << 45.0 << 90.0;
193 }
194 
timeToNextInstruction()195 void tst_QGeoManeuver::timeToNextInstruction()
196 {
197     int time = 0;
198     qgeomaneuver->setTimeToNextInstruction(time);
199 
200     QCOMPARE (qgeomaneuver->timeToNextInstruction(),time);
201 
202     time = 35;
203 
204     QVERIFY (qgeomaneuver->timeToNextInstruction() != time);
205 
206     qgeomaneuver->setTimeToNextInstruction(time);
207     QCOMPARE (qgeomaneuver->timeToNextInstruction(),time);
208 }
209 
waypoint()210 void tst_QGeoManeuver::waypoint()
211 {
212     QFETCH(double, latitude);
213     QFETCH(double, longitude);
214 
215     qgeocoordinate = new QGeoCoordinate (latitude,longitude);
216 
217     qgeomaneuver->setWaypoint(*qgeocoordinate);
218 
219     QCOMPARE(qgeomaneuver->waypoint(),*qgeocoordinate);
220 
221     qgeocoordinate->setLatitude(30.3);
222     QVERIFY(qgeomaneuver->waypoint() != *qgeocoordinate);
223 
224 
225     delete qgeocoordinate;
226 }
waypoint_data()227 void tst_QGeoManeuver::waypoint_data()
228 {
229     QTest::addColumn<double>("latitude");
230     QTest::addColumn<double>("longitude");
231 
232     QTest::newRow("invalid0") << -12220.0 << 0.0;
233     QTest::newRow("invalid1") << 0.0 << 181.0;
234 
235     QTest::newRow("correct0") << 0.0 << 0.0;
236     QTest::newRow("correct1") << 90.0 << 0.0;
237     QTest::newRow("correct2") << 0.0 << 180.0;
238     QTest::newRow("correct3") << -90.0 << 0.0;
239     QTest::newRow("correct4") << 0.0 << -180.0;
240     QTest::newRow("correct5") << 45.0 << 90.0;
241 }
242 
isValid()243 void tst_QGeoManeuver::isValid()
244 {
245     QVERIFY(!qgeomaneuver->isValid());
246     qgeomaneuver->setDirection(QGeoManeuver::DirectionBearLeft);
247     QVERIFY(qgeomaneuver->isValid());
248 }
249 
operators()250 void tst_QGeoManeuver::operators(){
251 
252     QGeoManeuver *qgeomaneuvercopy = new QGeoManeuver(*qgeomaneuver);
253 
254     QVERIFY(qgeomaneuver->operator ==(*qgeomaneuvercopy));
255     QVERIFY(!qgeomaneuver->operator !=(*qgeomaneuvercopy));
256 
257     qgeomaneuver->setDirection(QGeoManeuver::DirectionBearLeft);
258     qgeomaneuver->setInstructionText("Turn left in 50m");
259     qgeomaneuver->setTimeToNextInstruction(60);
260     qgeomaneuver->setDistanceToNextInstruction(560.45);
261 
262     qgeomaneuvercopy->setDirection(QGeoManeuver::DirectionForward);
263     qgeomaneuvercopy->setInstructionText("Turn left in 80m");
264     qgeomaneuvercopy->setTimeToNextInstruction(70);
265     qgeomaneuvercopy->setDistanceToNextInstruction(56065.45);
266 
267    QVERIFY(!(qgeomaneuver->operator ==(*qgeomaneuvercopy)));
268    QVERIFY(qgeomaneuver->operator !=(*qgeomaneuvercopy));
269 
270     *qgeomaneuvercopy = qgeomaneuvercopy->operator =(*qgeomaneuver);
271     QVERIFY(qgeomaneuver->operator ==(*qgeomaneuvercopy));
272     QVERIFY(!qgeomaneuver->operator !=(*qgeomaneuvercopy));
273 
274     delete qgeomaneuvercopy;
275 }
276 
alternateImplementation()277 void tst_QGeoManeuver::alternateImplementation()
278 {
279     QGeoManeuver qgeomaneuvercopy = *qgeomaneuver;
280 
281     QVERIFY(qgeomaneuvercopy == (*qgeomaneuver));
282 
283     qgeomaneuvercopy.setDirection(QGeoManeuver::DirectionForward);
284     qgeomaneuvercopy.setInstructionText("Turn left in 80m");
285     qgeomaneuvercopy.setTimeToNextInstruction(70);
286     qgeomaneuvercopy.setDistanceToNextInstruction(56065.45);
287 
288     QVERIFY(qgeomaneuvercopy != (*qgeomaneuver));
289 
290     QGeoManeuverAlt mAlt;
291     QGeoManeuver m = mAlt;
292 
293     QCOMPARE(m.instructionText(), "QGeoManeuverPrivateDefaultAlt");
294     m = qgeomaneuvercopy;
295     QCOMPARE(m.instructionText(), "Turn left in 80m");
296     m = mAlt;
297     QCOMPARE(m.instructionText(), "QGeoManeuverPrivateDefaultAlt");
298 }
299 
300 
301 QTEST_APPLESS_MAIN(tst_QGeoManeuver);
302