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 <qgeoroutexmlparser.h>
30 #include <qgeocoordinate.h>
31 #include <qtest.h>
32 #include <qgeoroute.h>
33 
34 #include <QMetaType>
35 #include <QDebug>
36 #include <QFile>
37 #include <QSignalSpy>
38 
39 Q_DECLARE_METATYPE(QList<QGeoRoute>)
40 
41 QT_USE_NAMESPACE
42 
43 class tst_QGeoRouteXmlParser : public QObject
44 {
45     Q_OBJECT
46 
47 public:
tst_QGeoRouteXmlParser()48     tst_QGeoRouteXmlParser()
49         : start(0.0, 0.0),
50           end(1.0, 1.0)
51     {
52         qRegisterMetaType<QList<QGeoRoute> >();
53     }
54 
55 private:
56     // dummy values for creating the request object
57     QGeoCoordinate start;
58     QGeoCoordinate end;
59 
60 private slots:
test_realData1()61     void test_realData1()
62     {
63         QFile f(":/route1.xml");
64         if (!f.open(QIODevice::ReadOnly))
65             QFAIL("could not open route1.xml");
66 
67         QGeoRouteRequest req(start, end);
68         QGeoRouteXmlParser xp(req);
69         xp.setAutoDelete(false);
70 
71         QSignalSpy resultsSpy(&xp, SIGNAL(results(QList<QGeoRoute>)));
72 
73         xp.parse(f.readAll());
74 
75         QTRY_COMPARE(resultsSpy.count(), 1);
76 
77         QVariantList arguments = resultsSpy.first();
78 
79         // xml contains exactly 1 route
80         QList<QGeoRoute> results = arguments.at(0).value<QList<QGeoRoute> >();
81         QCOMPARE(results.size(), 1);
82         QGeoRoute route = results.first();
83 
84         QList<QGeoRouteSegment> segments;
85         // get all the segments on the route
86         segments << route.firstRouteSegment();
87         while (segments.last().isValid())
88             segments << segments.last().nextRouteSegment();
89 
90         // should be 9 segments in the list (last one invalid)
91         QCOMPARE(segments.size(), 9);
92 
93         // check the first maneuver is correct
94         QGeoManeuver first = segments.at(0).maneuver();
95         QCOMPARE(first.instructionText(), QStringLiteral("Head toward Logan Rd (95) on Padstow Rd (56). Go for 0.3 miles."));
96         QCOMPARE(first.position(), QGeoCoordinate(-27.5752144, 153.0879669));
97 
98         QCOMPARE(first.timeToNextInstruction(), 24);
99         QCOMPARE(first.distanceToNextInstruction(), 403.0);
100 
101         // check the last two maneuvers -- route1.xml has a directionless final maneuver
102         QGeoManeuver secondLast = segments.at(6).maneuver();
103         QVERIFY(secondLast.instructionText().contains("Turn right onto Bartley St"));
104         QCOMPARE(secondLast.position(), QGeoCoordinate(-27.4655991, 153.0231628));
105         QCOMPARE(secondLast.distanceToNextInstruction(), 181.0);
106         QCOMPARE(secondLast.timeToNextInstruction(), 41);
107 
108         QGeoManeuver last = segments.at(7).maneuver();
109         QVERIFY(last.instructionText().contains("Arrive at Bartley St"));
110         QCOMPARE(last.position(), QGeoCoordinate(-27.4650097, 153.0230255));
111         QCOMPARE(last.distanceToNextInstruction(), 0.0);
112         QCOMPARE(last.timeToNextInstruction(), 0);
113     }
114 
test_realData2()115     void test_realData2()
116     {
117         QFile f(":/route2.xml");
118         if (!f.open(QIODevice::ReadOnly))
119             QFAIL("could not open route2.xml");
120 
121         QGeoRouteRequest req(start, end);
122         QGeoRouteXmlParser xp(req);
123         xp.setAutoDelete(false);
124 
125         QSignalSpy resultsSpy(&xp, SIGNAL(results(QList<QGeoRoute>)));
126 
127         xp.parse(f.readAll());
128 
129         QTRY_COMPARE(resultsSpy.count(), 1);
130 
131         QVariantList arguments = resultsSpy.first();
132 
133         // xml contains exactly 1 route
134         QList<QGeoRoute> results = arguments.at(0).value<QList<QGeoRoute> >();
135         QCOMPARE(results.size(), 1);
136         QGeoRoute route = results.first();
137 
138         QList<QGeoRouteSegment> segments;
139         // get all the segments on the route
140         segments << route.firstRouteSegment();
141         while (segments.last().isValid())
142             segments << segments.last().nextRouteSegment();
143 
144         // should be 14 segments in the list (last one invalid)
145         QCOMPARE(segments.size(), 14);
146 
147         QCOMPARE(route.path().size(), 284);
148         QCOMPARE(route.path().at(57), QGeoCoordinate(-27.5530605, 153.0691223));
149         QCOMPARE(route.path().at(283), QGeoCoordinate(-27.4622307, 153.0397949));
150 
151         QVERIFY(segments.at(0).maneuver().instructionText().contains("Head toward Electronics St"));
152         QCOMPARE(segments.at(0).maneuver().direction(), QGeoManeuver::DirectionForward);
153         QVERIFY(segments.at(1).maneuver().instructionText().contains("Turn left onto Miles Platting"));
154         QCOMPARE(segments.at(1).maneuver().direction(), QGeoManeuver::DirectionLeft);
155         QVERIFY(segments.at(2).maneuver().instructionText().contains("Turn right onto Logan Rd"));
156         QCOMPARE(segments.at(2).maneuver().direction(), QGeoManeuver::DirectionRight);
157         QVERIFY(segments.at(3).maneuver().instructionText().contains("Take exit #14/M3/City"));
158         QCOMPARE(segments.at(3).maneuver().direction(), QGeoManeuver::DirectionLightLeft);
159         QVERIFY(segments.at(4).maneuver().instructionText().contains("Take exit #2/41"));
160         QCOMPARE(segments.at(4).maneuver().direction(), QGeoManeuver::DirectionLightLeft);
161         QVERIFY(segments.at(5).maneuver().instructionText().contains("Turn right onto Allen St"));
162         QCOMPARE(segments.at(5).maneuver().direction(), QGeoManeuver::DirectionRight);
163         QVERIFY(segments.at(6).maneuver().instructionText().contains("Bear right to stay on"));
164         QCOMPARE(segments.at(6).maneuver().direction(), QGeoManeuver::DirectionLightRight);
165         QVERIFY(segments.at(7).maneuver().instructionText().contains("Bear right onto Vulture St"));
166         QCOMPARE(segments.at(7).maneuver().direction(), QGeoManeuver::DirectionLightRight);
167     }
168 };
169 
170 QTEST_GUILESS_MAIN(tst_QGeoRouteXmlParser)
171 #include "tst_qgeoroutexmlparser.moc"
172 
173