summaryrefslogtreecommitdiff
path: root/tests/auto/qgeotiledmap/tst_qgeotiledmap.cpp
blob: 3843ed8f575b4762d997c59beb4de10a901248fa (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
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://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 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qgeotiledmap_test.h"
#include "qgeotilefetcher_test.h"
#include "qgeotiledmappingmanagerengine_test.h"
#include <QtCore/QString>
#include <QtTest/QtTest>
#include <QtTest/QSignalSpy>
#include <QtPositioning/private/qwebmercator_p.h>
#include <QtLocation/QGeoServiceProvider>
#include <QtLocation/private/qgeotiledmap_p.h>
#include <QtLocation/private/qgeomappingmanager_p.h>
#include <QtLocation/private/qgeocameracapabilities_p.h>

QT_USE_NAMESPACE

Q_DECLARE_METATYPE(QGeoTiledMap::PrefetchStyle)

class FetchTileCounter: public QObject
{
    Q_OBJECT
public Q_SLOTS:
    void tileFetched(const QGeoTileSpec& spec) {
        m_tiles << spec;
    }
public:
    QSet<QGeoTileSpec> m_tiles;
};

class tst_QGeoTiledMap : public QObject
{
    Q_OBJECT

public:
    tst_QGeoTiledMap();
    ~tst_QGeoTiledMap();

private:
    void waitForFetch(int count);

private Q_SLOTS:
    void initTestCase();
    void fetchTiles();
    void fetchTiles_data();

private:
    QScopedPointer<QGeoTiledMapTest> m_map;
    QScopedPointer<FetchTileCounter> m_tilesCounter;
    QGeoTileFetcherTest *m_fetcher;

};

tst_QGeoTiledMap::tst_QGeoTiledMap():
    m_fetcher(0)
{
}

tst_QGeoTiledMap::~tst_QGeoTiledMap()
{
}

void tst_QGeoTiledMap::initTestCase()
{
#if QT_CONFIG(library)
    // Set custom path since CI doesn't install test plugins
#ifdef Q_OS_WIN
    QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath() +
                                     QStringLiteral("/../../../../plugins"));
#else
    QCoreApplication::addLibraryPath(QCoreApplication::applicationDirPath() +
                                     QStringLiteral("/../../../plugins"));
#endif
#endif
      QVariantMap parameters;
      parameters["tileSize"] = 256;
      parameters["maxZoomLevel"] = 8;
      parameters["finishRequestImmediately"] = true;
      QGeoServiceProvider *provider = new QGeoServiceProvider("qmlgeo.test.plugin",parameters);
      provider->setAllowExperimental(true);
      QGeoMappingManager *mappingManager = provider->mappingManager();
      QVERIFY2(provider->error() == QGeoServiceProvider::NoError, "Could not load plugin: " + provider->errorString().toLatin1());
      m_map.reset(static_cast<QGeoTiledMapTest*>(mappingManager->createMap(this)));
      QVERIFY(m_map);
      m_map->setViewportSize(QSize(256, 256));
      m_fetcher = static_cast<QGeoTileFetcherTest*>(m_map->m_engine->tileFetcher());
      m_tilesCounter.reset(new FetchTileCounter());
      connect(m_fetcher, SIGNAL(tileFetched(const QGeoTileSpec&)), m_tilesCounter.data(), SLOT(tileFetched(const QGeoTileSpec&)));
}

void tst_QGeoTiledMap::fetchTiles()
{
    QFETCH(double, zoomLevel);
    QFETCH(int, visibleCount);
    QFETCH(int, prefetchCount);
    QFETCH(QGeoTiledMap::PrefetchStyle, style);
    QFETCH(int, nearestNeighbourLayer);

    m_map->setPrefetchStyle(style);

    QGeoCameraData camera;
    camera.setCenter(QWebMercator::mercatorToCoord(QDoubleVector2D( 0.5 ,  0.5 )));

    //prev_visible
    camera.setZoomLevel(zoomLevel-1);
    m_map->clearData();
    m_tilesCounter->m_tiles.clear();
    m_map->setCameraData(camera);
    waitForFetch(visibleCount);
    QSet<QGeoTileSpec> prev_visible = m_tilesCounter->m_tiles;

    //visible + prefetch
    camera.setZoomLevel(zoomLevel);
    m_map->clearData();
    m_tilesCounter->m_tiles.clear();
    m_map->setCameraData(camera);
    waitForFetch(visibleCount);
    QSet<QGeoTileSpec> visible = m_tilesCounter->m_tiles;
    m_map->clearData();
    m_tilesCounter->m_tiles.clear();
    m_map->prefetchData();
    waitForFetch(prefetchCount);
    QSet<QGeoTileSpec> prefetched = m_tilesCounter->m_tiles;

    //next visible
    camera.setZoomLevel(zoomLevel + 1);
    m_map->clearData();
    m_tilesCounter->m_tiles.clear();
    m_map->setCameraData(camera);
    waitForFetch(visibleCount);
    QSet<QGeoTileSpec> next_visible = m_tilesCounter->m_tiles;

    QVERIFY2(visibleCount == visible.size(), "visible count incorrect");
    QVERIFY2(prefetchCount == prefetched.size(), "prefetch count incorrect");
    QSetIterator<QGeoTileSpec> i(visible);
    while (i.hasNext())
        QVERIFY2(prefetched.contains(i.next()),"visible tile missing from prefetched tiles");

    //for zoomLevels wihtout fractions more tiles are fetched for current zoomlevel due to ViewExpansion
    if (qCeil(zoomLevel) != zoomLevel && style == QGeoTiledMap::PrefetchNeighbourLayer && nearestNeighbourLayer < zoomLevel)
        QVERIFY2(prefetched == prev_visible + visible, "wrongly prefetched tiles");

    if (qCeil(zoomLevel) != zoomLevel && style == QGeoTiledMap::PrefetchNeighbourLayer && nearestNeighbourLayer > zoomLevel)
        QVERIFY2(prefetched == next_visible + visible, "wrongly prefetched tiles");

    if (qCeil(zoomLevel) != zoomLevel && style == QGeoTiledMap::PrefetchTwoNeighbourLayers)
        QVERIFY2(prefetched == prev_visible + visible + next_visible, "wrongly prefetched tiles");
}

void tst_QGeoTiledMap::fetchTiles_data()
{
    QTest::addColumn<double>("zoomLevel");
    QTest::addColumn<int>("visibleCount");
    QTest::addColumn<int>("prefetchCount");
    QTest::addColumn<QGeoTiledMap::PrefetchStyle>("style");
    QTest::addColumn<int>("nearestNeighbourLayer");
    QTest::newRow("zoomLevel: 4 , visible count: 4 : prefetch count: 16") << 4.0 << 4 << 4 + 16 << QGeoTiledMap::PrefetchNeighbourLayer << 3;
    QTest::newRow("zoomLevel: 4.06 , visible count: 4 : prefetch count: 4") << 4.06 << 4 << 4 + 4 << QGeoTiledMap::PrefetchNeighbourLayer << 3;
    QTest::newRow("zoomLevel: 4.1 , visible count: 4 : prefetch count: 4") << 4.1 << 4 << 4 + 4 << QGeoTiledMap::PrefetchNeighbourLayer << 3;
    QTest::newRow("zoomLevel: 4.5 , visible count: 4 : prefetch count: 4") << 4.5 << 4 << 4 + 4 << QGeoTiledMap::PrefetchNeighbourLayer << 3;
    QTest::newRow("zoomLevel: 4.6 , visible count: 4 : prefetch count: 4") << 4.6 << 4 << 4 + 4 << QGeoTiledMap::PrefetchNeighbourLayer << 5;
    QTest::newRow("zoomLevel: 4.9 , visible count: 4 : prefetch count: 4") << 4.9 << 4 <<4 + 4 << QGeoTiledMap::PrefetchNeighbourLayer << 5;
    QTest::newRow("zoomLevel: 4 , visible count: 4 : prefetch count: 4") << 4.0 << 4 << 16 + 4 + 4 << QGeoTiledMap::PrefetchTwoNeighbourLayers << 3;
    QTest::newRow("zoomLevel: 4.1 , visible count: 4 : prefetch count: 4") << 4.1 << 4 << 4 + 4 + 4 << QGeoTiledMap::PrefetchTwoNeighbourLayers << 3;
    QTest::newRow("zoomLevel: 4.6 ,visible count: 4 : prefetch count: 4") << 4.6 << 4 << 4 + 4  + 4 << QGeoTiledMap::PrefetchTwoNeighbourLayers << 5;
}

void tst_QGeoTiledMap::waitForFetch(int count)
{
    int timeout = 0;
    while (m_tilesCounter->m_tiles.count() < count && timeout < count) {
        //250ms for each tile fetch
        QTest::qWait(250);
        timeout++;
    }
}

QTEST_MAIN(tst_QGeoTiledMap)

#include "tst_qgeotiledmap.moc"