summaryrefslogtreecommitdiff
path: root/tests/auto/geotestplugin/qgeoroutingmanagerengine_test.h
blob: 98501e6cfd3e47bfccefecde9a7e834e64e3b9fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef QGEOROUTINGMANAGERENGINE_TEST_H
#define QGEOROUTINGMANAGERENGINE_TEST_H

#include <qgeoserviceprovider.h>
#include <qgeoroutingmanagerengine.h>
#include <QLocale>
#include <qgeoaddress.h>
#include <qgeoroutereply.h>
#include <QtLocation/private/qgeoroute_p.h>

#include <QDebug>
#include <QTimer>
#include <QTimerEvent>

QT_USE_NAMESPACE

class RouteReplyTest :public QGeoRouteReply
{
    Q_OBJECT

public:
    RouteReplyTest(QObject *parent=0) :QGeoRouteReply (QGeoRouteRequest(), parent)
    {}
    void  callSetError ( Error error, const QString & errorString ) {setError(error, errorString);}
    void  callSetFinished ( bool finished ) {setFinished(finished);}
    void  callSetRoutes(const QList<QGeoRoute> &routes) {setRoutes(routes);}
};

class QGeoRoutingManagerEngineTest: public QGeoRoutingManagerEngine
{
    Q_OBJECT
    RouteReplyTest* routeReply_;
    bool finishRequestImmediately_;
    int timerId_;
    QGeoRouteReply::Error errorCode_;
    QString errorString_;
    bool alternateGeoRouteImplementation_;

public:
    QGeoRoutingManagerEngineTest(const QVariantMap &parameters,
        QGeoServiceProvider::Error *error, QString *errorString) :
        QGeoRoutingManagerEngine(parameters),
        routeReply_(0),
        finishRequestImmediately_(true),
        timerId_(0),
        errorCode_(QGeoRouteReply::NoError),
        alternateGeoRouteImplementation_(false)
    {
        Q_UNUSED(error);
        Q_UNUSED(errorString);

        if (parameters.contains("gc_finishRequestImmediately")) {
            finishRequestImmediately_ = qvariant_cast<bool>(parameters.value("gc_finishRequestImmediately"));
        }

        if (parameters.contains("gc_alternateGeoRoute")) {
            alternateGeoRouteImplementation_ = qvariant_cast<bool>(parameters.value("gc_alternateGeoRoute"));
        }

        setLocale(QLocale (QLocale::German, QLocale::Germany));
        setSupportedFeatureTypes (
                    QGeoRouteRequest::NoFeature | QGeoRouteRequest::TollFeature |
                    QGeoRouteRequest::HighwayFeature | QGeoRouteRequest::PublicTransitFeature |
                    QGeoRouteRequest::FerryFeature | QGeoRouteRequest::TunnelFeature |
                    QGeoRouteRequest::DirtRoadFeature | QGeoRouteRequest::ParksFeature |
                    QGeoRouteRequest::MotorPoolLaneFeature );
        setSupportedFeatureWeights (
                    QGeoRouteRequest::NeutralFeatureWeight | QGeoRouteRequest::PreferFeatureWeight |
                    QGeoRouteRequest::RequireFeatureWeight | QGeoRouteRequest::AvoidFeatureWeight |
                    QGeoRouteRequest::DisallowFeatureWeight );
        setSupportedManeuverDetails (
                    QGeoRouteRequest::NoManeuvers | QGeoRouteRequest::BasicManeuvers);
        setSupportedRouteOptimizations (
                    QGeoRouteRequest::ShortestRoute | QGeoRouteRequest::FastestRoute |
                    QGeoRouteRequest::MostEconomicRoute | QGeoRouteRequest::MostScenicRoute);
        setSupportedSegmentDetails (
                    QGeoRouteRequest::NoSegmentData | QGeoRouteRequest::BasicSegmentData );
        setSupportedTravelModes (
                    QGeoRouteRequest::CarTravel | QGeoRouteRequest::PedestrianTravel |
                    QGeoRouteRequest::BicycleTravel | QGeoRouteRequest::PublicTransitTravel |
                    QGeoRouteRequest::TruckTravel );
    }

    QGeoRouteReply* calculateRoute(const QGeoRouteRequest& request) override
    {
        routeReply_ = new RouteReplyTest();
        connect(routeReply_, SIGNAL(aborted()), this, SLOT(requestAborted()));

        if (request.numberAlternativeRoutes() > 70) {
            errorCode_ = (QGeoRouteReply::Error)(request.numberAlternativeRoutes() - 70);
            errorString_ = "error";
        } else {
            errorCode_ = QGeoRouteReply::NoError;
            errorString_ = "";
        }
        setRoutes(request, routeReply_);
        if (finishRequestImmediately_) {
            if (errorCode_) {
                routeReply_->callSetError(errorCode_, errorString_);
            } else {
                routeReply_->callSetError(QGeoRouteReply::NoError, "no error");
                routeReply_->callSetFinished(true);
            }
        } else {
            // we only allow serialized requests in QML - previous must have been aborted or finished
            Q_ASSERT(timerId_ == 0);
            timerId_ = startTimer(200);
        }
        return static_cast<QGeoRouteReply*>(routeReply_);
    }

    void setRoutes(const QGeoRouteRequest& request, RouteReplyTest* reply)
    {
        QList<QGeoRoute> routes;
        int travelTime = 0;

        for (int i = 0; i < request.numberAlternativeRoutes(); ++i) {
            QGeoRoute route;
            route.setPath(request.waypoints());
            route.setTravelTime(travelTime);
            if (alternateGeoRouteImplementation_)
                route.setTravelTime(123456);

            if (request.departureTime().isValid()) {
                QVariantMap extendedAttributes = route.extendedAttributes();
                extendedAttributes["tst_departureTime"] = request.departureTime();
                route.setExtendedAttributes(extendedAttributes);
            }

            routes.append(route);
        }
        reply->callSetRoutes(routes);
    }

public Q_SLOTS:
    void requestAborted()
    {
        if (timerId_) {
            killTimer(timerId_);
            timerId_ = 0;
        }
        errorCode_ = QGeoRouteReply::NoError;
        errorString_ = "";
    }

protected:
     void timerEvent(QTimerEvent *event) override
     {
         Q_UNUSED(event);
         Q_ASSERT(timerId_ == event->timerId());
         Q_ASSERT(routeReply_);
         killTimer(timerId_);
         timerId_ = 0;
         if (errorCode_) {
             routeReply_->callSetError(errorCode_, errorString_);
             emit errorOccurred(routeReply_, errorCode_, errorString_);
         } else {
             routeReply_->callSetError(QGeoRouteReply::NoError, "no error");
             routeReply_->callSetFinished(true);
             emit finished(routeReply_);
         }
     }
};

#endif