Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[tests] Add unit test for tile prefetching
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpsantos committed Jan 25, 2017
1 parent 3b27f0d commit a991620
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmake/test-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(MBGL_TEST_FILES

# map
test/map/map.test.cpp
test/map/prefetch.test.cpp
test/map/transform.test.cpp

# math
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/map/prefetch/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": 8,
"sources": {},
"layers": []
}
Binary file added test/fixtures/map/prefetch/expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions test/fixtures/map/prefetch/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": 8,
"name": "Test",
"sources": {
"raster": {
"type": "raster",
"tiles": [ "{z}" ],
"tileSize": 256,
"maxzoom": 20,
"minzoom": 0
}
},
"layers": [{
"id": "background",
"type": "background",
"paint": {
"background-color": "blue"
}
}, {
"id": "raster",
"type": "raster",
"source": "raster"
}]
}
Binary file added test/fixtures/map/prefetch/tile_green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/map/prefetch/tile_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions test/map/prefetch.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <mbgl/test/util.hpp>
#include <mbgl/test/stub_file_source.hpp>

#include <mbgl/map/map.hpp>
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/default_thread_pool.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>

#include <algorithm>
#include <string>
#include <vector>

using namespace mbgl;
using namespace mbgl::style;
using namespace std::literals::string_literals;

struct MapTest {
util::RunLoop runLoop;
HeadlessBackend backend { test::sharedDisplay() };
OffscreenView view { backend.getContext(), { 512, 512 } };
StubFileSource fileSource;
ThreadPool threadPool { 4 };
};

TEST(Map, PrefetchTiles) {
MapTest test{};

Map map(test.backend, test.view.size, 1, test.fileSource, test.threadPool, MapMode::Still);

std::vector<int> tiles;

test.fileSource.response = [&] (const Resource& res) -> optional<Response> {
Response response;

auto zoom = std::stoi(res.url);
tiles.push_back(zoom);

// Return a red tile for prefetched tiles or green to the actual tile.
// The end rendering result should be all green because the map is only
// considered fully rendered when only ideal tiles are shown.
if (zoom == int(map.getZoom()) + 1) {
response.data = std::make_shared<std::string>(
util::read_file("test/fixtures/map/prefetch/tile_green.png"));
} else {
response.data = std::make_shared<std::string>(
util::read_file("test/fixtures/map/prefetch/tile_red.png"));
}

return { std::move(response) };
};

auto checkTilesForZoom = [&](int zoom, const std::vector<int>& expected) {
tiles.clear();

// Force tile reloading.
map.setStyleJSON(util::read_file("test/fixtures/map/prefetch/empty.json"));
map.setStyleJSON(util::read_file("test/fixtures/map/prefetch/style.json"));

map.setLatLngZoom({ 40.726989, -73.992857 }, zoom); // Manhattan

// Should always render the ideal tiles (i.e. a green map)
test::checkImage("test/fixtures/map/prefetch", test::render(map, test.view));

ASSERT_TRUE(std::is_permutation(tiles.begin(), tiles.end(), expected.begin()));
ASSERT_FALSE(tiles.empty());
};

// No prefetching, raster tiles will use ideal
// tiles instead of the actual zoom level, that is
// why the zoom levels for non-prefetched tiles are
// not the same.
checkTilesForZoom(10, { 11, 11, 11, 11, 11, 11, 11, 11, 11 });

map.setFixedPrefetchZoom(4);
checkTilesForZoom(12, { 13, 13, 13, 13, 13, 13, 13, 13, 13, 4 });

map.setFixedPrefetchZoom(0);
checkTilesForZoom(10, { 11, 11, 11, 11, 11, 11, 11, 11, 11, 0 });

// Should not prefetch, current zoom has lower zoom level.
map.setFixedPrefetchZoom(12);
checkTilesForZoom(9, { 10, 10, 10, 10, 10, 10, 10, 10, 10 });

// Disabled.
map.setFixedPrefetchZoom(-1);
checkTilesForZoom(17, { 18, 18, 18, 18, 18, 18, 18, 18, 18 });

map.setDynamicPrefetchZoomDelta(4);
checkTilesForZoom(12, { 13, 13, 13, 13, 13, 13, 13, 13, 13, 9 });

// Should clamp at `minzoom`.
map.setDynamicPrefetchZoomDelta(20);
checkTilesForZoom(10, { 11, 11, 11, 11, 11, 11, 11, 11, 11, 0 });

// Disabled.
map.setDynamicPrefetchZoomDelta(-1);
checkTilesForZoom(13, { 14, 14, 14, 14, 14, 14, 14, 14, 14 });

// Fixed takes precedence over dynamic.
map.setDynamicPrefetchZoomDelta(2);
map.setFixedPrefetchZoom(0);
checkTilesForZoom(15, { 16, 16, 16, 16, 16, 16, 16, 16, 16, 0 });
}

0 comments on commit a991620

Please sign in to comment.