summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-02-23 18:25:36 -0800
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-02-24 13:38:14 -0800
commit065cda83fab02aea85573d9c2d78e9121296582d (patch)
tree8fab316922a052496e67aa49d968224ffbbf0fca /test
parent5cefaeb85716b21f2bbe57165a244844114b8dbe (diff)
downloadqtlocation-mapboxgl-065cda83fab02aea85573d9c2d78e9121296582d.tar.gz
[core] Limit total number of offline Mapbox tiles
Diffstat (limited to 'test')
-rw-r--r--test/fixtures/offline/mapbox_source.style.json17
-rw-r--r--test/storage/offline_database.cpp57
-rw-r--r--test/storage/offline_download.cpp33
3 files changed, 107 insertions, 0 deletions
diff --git a/test/fixtures/offline/mapbox_source.style.json b/test/fixtures/offline/mapbox_source.style.json
new file mode 100644
index 0000000000..75bfd6667d
--- /dev/null
+++ b/test/fixtures/offline/mapbox_source.style.json
@@ -0,0 +1,17 @@
+{
+ "version": 8,
+ "sources": {
+ "inline": {
+ "type": "vector",
+ "maxzoom": 15,
+ "minzoom": 0,
+ "tiles": [ "mapbox://{z}-{x}-{y}.vector.pbf" ]
+ }
+ },
+ "layers": [{
+ "id": "fill",
+ "type": "fill",
+ "source": "inline",
+ "source-layer": "water"
+ }]
+}
diff --git a/test/storage/offline_database.cpp b/test/storage/offline_database.cpp
index d93945fb40..82e07c208a 100644
--- a/test/storage/offline_database.cpp
+++ b/test/storage/offline_database.cpp
@@ -603,3 +603,60 @@ TEST(OfflineDatabase, PutFailsWhenEvictionInsuffices) {
auto flo = dynamic_cast<FixtureLogObserver*>(observer.get());
EXPECT_EQ(1ul, flo->count({ EventSeverity::Warning, Event::Database, -1, "Unable to make space for entry" }));
}
+
+TEST(OfflineDatabase, OfflineMapboxTileCount) {
+ using namespace mbgl;
+
+ OfflineDatabase db(":memory:");
+ OfflineRegionDefinition definition { "http://example.com/style", LatLngBounds::hull({1, 2}, {3, 4}), 5, 6, 2.0 };
+ OfflineRegionMetadata metadata;
+
+ OfflineRegion region1 = db.createRegion(definition, metadata);
+ OfflineRegion region2 = db.createRegion(definition, metadata);
+
+ Resource nonMapboxTile = Resource::tile("http://example.com/", 1.0, 0, 0, 0);
+ Resource mapboxTile1 = Resource::tile("mapbox://tiles/1", 1.0, 0, 0, 0);
+ Resource mapboxTile2 = Resource::tile("mapbox://tiles/2", 1.0, 0, 0, 1);
+
+ Response response;
+ response.data = std::make_shared<std::string>("data");
+
+ // Count is initially zero.
+ EXPECT_EQ(0, db.getOfflineMapboxTileCount());
+
+ // Count stays the same after putting a non-tile resource.
+ db.putRegionResource(region1.getID(), Resource::style("http://example.com/"), response);
+ EXPECT_EQ(0, db.getOfflineMapboxTileCount());
+
+ // Count stays the same after putting a non-Mapbox tile.
+ db.putRegionResource(region1.getID(), nonMapboxTile, response);
+ EXPECT_EQ(0, db.getOfflineMapboxTileCount());
+
+ // Count increases after putting a Mapbox tile not used by another region.
+ db.putRegionResource(region1.getID(), mapboxTile1, response);
+ EXPECT_EQ(1, db.getOfflineMapboxTileCount());
+
+ // Count stays the same after putting a Mapbox tile used by another region.
+ db.putRegionResource(region2.getID(), mapboxTile1, response);
+ EXPECT_EQ(1, db.getOfflineMapboxTileCount());
+
+ // Count stays the same after putting a Mapbox tile used by the same region.
+ db.putRegionResource(region2.getID(), mapboxTile1, response);
+ EXPECT_EQ(1, db.getOfflineMapboxTileCount());
+
+ // Count stays the same after deleting a region when the tile is still used by another region.
+ db.deleteRegion(std::move(region2));
+ EXPECT_EQ(1, db.getOfflineMapboxTileCount());
+
+ // Count stays the same after the putting a non-offline Mapbox tile.
+ db.put(mapboxTile2, response);
+ EXPECT_EQ(1, db.getOfflineMapboxTileCount());
+
+ // Count increases after putting a pre-existing, but non-offline Mapbox tile.
+ db.putRegionResource(region1.getID(), mapboxTile2, response);
+ EXPECT_EQ(2, db.getOfflineMapboxTileCount());
+
+ // Count decreases after deleting a region when the tiles are not used by other regions.
+ db.deleteRegion(std::move(region1));
+ EXPECT_EQ(0, db.getOfflineMapboxTileCount());
+}
diff --git a/test/storage/offline_download.cpp b/test/storage/offline_download.cpp
index 73a6f80fcc..fce081b8ab 100644
--- a/test/storage/offline_download.cpp
+++ b/test/storage/offline_download.cpp
@@ -24,8 +24,13 @@ public:
if (responseErrorFn) responseErrorFn(error);
}
+ void mapboxTileCountLimitExceeded(uint64_t limit) override {
+ if (mapboxTileCountLimitExceededFn) mapboxTileCountLimitExceededFn(limit);
+ }
+
std::function<void (OfflineRegionStatus)> statusChangedFn;
std::function<void (Response::Error)> responseErrorFn;
+ std::function<void (uint64_t)> mapboxTileCountLimitExceededFn;
};
class OfflineTest {
@@ -347,3 +352,31 @@ TEST(OfflineDownload, RequestErrorsAreRetried) {
test.loop.run();
}
+
+TEST(OfflineDownload, TileCountLimitExceeded) {
+ OfflineTest test;
+ OfflineRegion region = test.createRegion();
+ OfflineDownload download(
+ region.getID(),
+ OfflineTilePyramidRegionDefinition("http://127.0.0.1:3000/offline/style.json", LatLngBounds::world(), 0.0, 0.0, 1.0),
+ test.db, test.fileSource);
+
+ test.db.setOfflineMapboxTileCountLimit(0);
+
+ test.fileSource.styleResponse = [&] (const Resource& resource) {
+ EXPECT_EQ("http://127.0.0.1:3000/offline/style.json", resource.url);
+ return test.response("offline/mapbox_source.style.json");
+ };
+
+ auto observer = std::make_unique<MockObserver>();
+
+ observer->mapboxTileCountLimitExceededFn = [&] (uint64_t limit) {
+ EXPECT_EQ(0, limit);
+ test.loop.stop();
+ };
+
+ download.setObserver(std::move(observer));
+ download.setState(OfflineRegionDownloadState::Active);
+
+ test.loop.run();
+}