summaryrefslogtreecommitdiff
path: root/tests/auto/qgeomapcontroller/tst_qgeomapcontroller.cpp
blob: d0b1baff4c5ff51e2d22fa5542f2cf62f1f201bf (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore/QString>
#include <QtTest/QtTest>
#include <QSignalSpy>

#include <QtLocation/private/qgeomapdata_p.h>
#include <QtLocation/private/qgeomapcontroller_p.h>

// cross-reference test plugin, where concrete subclasses are needed
// in order to create a concrete mapcontroller
#include "../geotestplugin/qgeoserviceproviderplugin_test.h"
#include "../geotestplugin/qgeotiledmapdata_test.h"
#include "../geotestplugin/qgeotiledmappingmanagerengine_test.h"

QT_USE_NAMESPACE

class tst_QGeoMapController : public QObject
{
    Q_OBJECT

public:
    tst_QGeoMapController();
    ~tst_QGeoMapController();

private:
    QGeoTiledMapDataTest *map_;
    QSignalSpy *signalCenterChanged_;
    QSignalSpy *signalBearingChanged_;
    QSignalSpy *signalTiltChanged_;
    QSignalSpy *signalRollChanged_;
    QSignalSpy *signalZoomChanged_;
    void clearSignalSpies();

private Q_SLOTS:
    void signalsConstructedTest();
    void constructorTest_data();
    void constructorTest();
    void centerTest();
    void bearingTest();
    void tiltTest();
    void rollTest();
    void panTest();
    void zoomTest();
};

tst_QGeoMapController::tst_QGeoMapController()
{
    // unlike low level classes, geomapcontroller is built up from several parent classes
    // so, in order to test it, we need to create these parent classes for it to link to
    // such as a GeoMapData
    QGeoServiceProviderFactoryTest serviceProviderTest; // empty constructor

    // TODO: check whether the default constructors of these objects allow the create to work
    QVariantMap parameterMap;
    QGeoServiceProvider::Error mappingError;
    QString mappingErrorString;

    QGeoTiledMappingManagerEngineTest *mapEngine = static_cast<QGeoTiledMappingManagerEngineTest*>(serviceProviderTest.createMappingManagerEngine(parameterMap, &mappingError, &mappingErrorString));
    map_ = new QGeoTiledMapDataTest(mapEngine);
    map_->resize(100, 100);


    signalCenterChanged_ = new QSignalSpy(map_->mapController(), SIGNAL(centerChanged(QGeoCoordinate)));
    signalBearingChanged_ = new QSignalSpy(map_->mapController(), SIGNAL(bearingChanged(qreal)));
    signalTiltChanged_ = new QSignalSpy(map_->mapController(), SIGNAL(tiltChanged(qreal)));
    signalRollChanged_ = new QSignalSpy(map_->mapController(), SIGNAL(rollChanged(qreal)));
    signalZoomChanged_ = new QSignalSpy(map_->mapController(), SIGNAL(zoomChanged(qreal)));
}

void tst_QGeoMapController::clearSignalSpies()
{
    signalCenterChanged_->clear();
    signalBearingChanged_->clear();
    signalTiltChanged_->clear();
    signalRollChanged_->clear();
    signalZoomChanged_->clear();
}

tst_QGeoMapController::~tst_QGeoMapController()
{
    delete signalCenterChanged_;
    delete signalBearingChanged_;
    delete signalTiltChanged_;
    delete signalRollChanged_;
    delete signalZoomChanged_;
    delete map_;
}

void tst_QGeoMapController::signalsConstructedTest()
{
    QVERIFY(signalCenterChanged_->isValid());
    QVERIFY(signalBearingChanged_->isValid());
    QVERIFY(signalTiltChanged_->isValid());
    QVERIFY(signalRollChanged_->isValid());
    QVERIFY(signalZoomChanged_->isValid());
}

void tst_QGeoMapController::constructorTest_data()
{
    QTest::addColumn<QGeoCoordinate>("center");
    QTest::addColumn<double>("bearing");
    QTest::addColumn<double>("tilt");
    QTest::addColumn<double>("roll");
    QTest::addColumn<double>("zoom");
    QTest::newRow("zeros") << QGeoCoordinate() << 0.0 << 0.0 << 0.0 << 0.0;
    QTest::newRow("valid") << QGeoCoordinate(10.0, 20.5, 30.8) << 0.1 << 0.2 << 0.3 << 2.0;
    QTest::newRow("negative values") << QGeoCoordinate(-50, -20, 100) << -0.1 << -0.2 << -0.3 << 1.0;
}

void tst_QGeoMapController::constructorTest()
{
    QFETCH(QGeoCoordinate, center);
    QFETCH(double, bearing);
    QFETCH(double, tilt);
    QFETCH(double, roll);
    QFETCH(double, zoom);

    // test whether the map controller picks up the camera data
    // from the map during construction
    QGeoCameraData cameraData;
    cameraData.setCenter(center);
    cameraData.setBearing(bearing);
    cameraData.setTilt(tilt);
    cameraData.setRoll(roll);
    cameraData.setZoomLevel(zoom);
    map_->setCameraData(cameraData);
    QGeoMapController mapController(map_);

    // make sure the values come out the same
    // also make sure the values match what they were actually set to
    QCOMPARE(mapController.center(), cameraData.center());
    QCOMPARE(mapController.center(), center);
    QCOMPARE(mapController.zoom(), cameraData.zoomLevel());
    QCOMPARE(mapController.zoom(), zoom);

    if (map_->cameraCapabilities().supportsBearing()){
        QCOMPARE(mapController.bearing(), cameraData.bearing());
        QCOMPARE(mapController.bearing(), bearing);
    }
    if (map_->cameraCapabilities().supportsTilting()){
        QCOMPARE(mapController.tilt(), cameraData.tilt());
        QCOMPARE(mapController.tilt(), tilt);
    }
    if (map_->cameraCapabilities().supportsRolling()){
        QCOMPARE(mapController.roll(), cameraData.roll());
        QCOMPARE(mapController.roll(), roll);
    }
}

void tst_QGeoMapController::centerTest()
{
    QGeoCameraData cameraData;
    cameraData.setCenter(QGeoCoordinate(10.0,-20.4,30.8));
    map_->setCameraData(cameraData);
    QGeoMapController mapController(map_);
    QCOMPARE(mapController.center(),QGeoCoordinate(10.0,-20.4,30.8));

    QGeoCoordinate coord(10.0,20.4,30.8);
    clearSignalSpies();
    mapController.setCenter(coord);

    // check correct signal is triggered
    QCOMPARE(signalCenterChanged_->count(),1);
    QCOMPARE(signalBearingChanged_->count(),0);
    QCOMPARE(signalTiltChanged_->count(),0);
    QCOMPARE(signalRollChanged_->count(),0);
    QCOMPARE(signalZoomChanged_->count(),0);

    QCOMPARE(mapController.center(),QGeoCoordinate(10.0,20.4,30.8));

    mapController.setCenter(QGeoCoordinate(10.0,20.4,30.9));
    QCOMPARE(mapController.center(),QGeoCoordinate(10.0,20.4,30.9));
}

void tst_QGeoMapController::bearingTest()
{
    if (map_->cameraCapabilities().supportsBearing()){
        qreal bearing = 1.4;

        QGeoCameraData cameraData;
        cameraData.setBearing(bearing);
        map_->setCameraData(cameraData);
        QGeoMapController mapController(map_);
        QCOMPARE(mapController.bearing(),bearing);

        clearSignalSpies();
        mapController.setBearing(-1.5);
        QCOMPARE(mapController.bearing(),-1.5);

        // check correct signal is triggered
        QCOMPARE(signalCenterChanged_->count(),0);
        QCOMPARE(signalBearingChanged_->count(),1);
        QCOMPARE(signalTiltChanged_->count(),0);
        QCOMPARE(signalRollChanged_->count(),0);
        QCOMPARE(signalZoomChanged_->count(),0);
    }
}

void tst_QGeoMapController::tiltTest()
{
    if (map_->cameraCapabilities().supportsTilting()){
        qreal tilt = map_->cameraCapabilities().maximumTilt();

        QGeoCameraData cameraData;
        cameraData.setTilt(tilt);
        map_->setCameraData(cameraData);
        QGeoMapController mapController(map_);
        QCOMPARE(mapController.tilt(),tilt);

        tilt = map_->cameraCapabilities().minimumTilt();
        clearSignalSpies();
        mapController.setTilt(tilt);
        QCOMPARE(mapController.tilt(),tilt);

        // check correct signal is triggered
        QCOMPARE(signalCenterChanged_->count(),0);
        QCOMPARE(signalBearingChanged_->count(),0);
        QCOMPARE(signalTiltChanged_->count(),1);
        QCOMPARE(signalRollChanged_->count(),0);
        QCOMPARE(signalZoomChanged_->count(),0);
    }
}

void tst_QGeoMapController::rollTest()
{
    if (map_->cameraCapabilities().supportsRolling()){
        qreal roll = 1.4;

        QGeoCameraData cameraData;
        cameraData.setRoll(roll);
        map_->setCameraData(cameraData);
        QGeoMapController mapController(map_);
        QCOMPARE(mapController.roll(),roll);

        clearSignalSpies();
        mapController.setRoll(-1.5);
        QCOMPARE(mapController.roll(),-1.5);

        // check correct signal is triggered
        QCOMPARE(signalCenterChanged_->count(),0);
        QCOMPARE(signalBearingChanged_->count(),0);
        QCOMPARE(signalTiltChanged_->count(),0);
        QCOMPARE(signalRollChanged_->count(),1);
        QCOMPARE(signalZoomChanged_->count(),0);
    }
}

void tst_QGeoMapController::panTest()
{
    QGeoMapController mapController(map_);

    mapController.setCenter(QGeoCoordinate(-1.0,-2.4,3.8));

    // check that pan of zero leaves the camera centre unaltered
    mapController.pan(0, 0);
    QCOMPARE(mapController.center().altitude(), 3.8);
    QCOMPARE(mapController.center().latitude(), -1.0);
    QCOMPARE(mapController.center().longitude(), -2.4);

    qreal dx = 13.1;
    qreal dy = -9.3;
    clearSignalSpies();
    mapController.pan(dx, dy);

    // rather than verify the exact new position, we check that the position has changed and the altitude
    // is unaffected
    QCOMPARE(mapController.center().altitude(), 3.8);
    QVERIFY(qFuzzyCompare(mapController.center().latitude(), -1.0) == false);
    QVERIFY(qFuzzyCompare(mapController.center().longitude(), -2.4) == false);

    // check correct signal is triggered
    QCOMPARE(signalCenterChanged_->count(),1);
    QCOMPARE(signalBearingChanged_->count(),0);
    QCOMPARE(signalTiltChanged_->count(),0);
    QCOMPARE(signalRollChanged_->count(),0);
    QCOMPARE(signalZoomChanged_->count(),0);
}

void tst_QGeoMapController::zoomTest()
{
    QGeoCameraData cameraData;
    cameraData.setZoomLevel(1.4);
    map_->setCameraData(cameraData);
    QGeoMapController mapController(map_);

    QCOMPARE(mapController.zoom(),1.4);
    mapController.setZoom(1.4);
    QCOMPARE(mapController.zoom(),1.4);
    clearSignalSpies();
    mapController.setZoom(1.5);
    QCOMPARE(mapController.zoom(),1.5);

    // check correct signal is triggered
    QCOMPARE(signalCenterChanged_->count(),0);
    QCOMPARE(signalBearingChanged_->count(),0);
    QCOMPARE(signalTiltChanged_->count(),0);
    QCOMPARE(signalRollChanged_->count(),0);
    QCOMPARE(signalZoomChanged_->count(),1);
}


QTEST_APPLESS_MAIN(tst_QGeoMapController)

#include "tst_qgeomapcontroller.moc"