diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2017-01-24 14:39:08 +0200 |
---|---|---|
committer | Thiago Marcos P. Santos <tmpsantos@gmail.com> | 2017-07-07 14:28:52 +0300 |
commit | 69c8f602de9ab087dd841dfabf33a72d46672673 (patch) | |
tree | 13f8e58db30927a88e5e71f45cd6c35b8bf502c8 | |
parent | 407fbc70b1dc6dab6b7aa5a4b9c57b4e08906c79 (diff) | |
download | qtlocation-mapboxgl-69c8f602de9ab087dd841dfabf33a72d46672673.tar.gz |
[tests] Add unit test for tile prefetching
-rw-r--r-- | cmake/test-files.cmake | 1 | ||||
-rw-r--r-- | test/fixtures/map/prefetch/empty.json | 5 | ||||
-rw-r--r-- | test/fixtures/map/prefetch/expected.png | bin | 0 -> 2198 bytes | |||
-rw-r--r-- | test/fixtures/map/prefetch/style.json | 24 | ||||
-rw-r--r-- | test/fixtures/map/prefetch/tile_green.png | bin | 0 -> 659 bytes | |||
-rw-r--r-- | test/fixtures/map/prefetch/tile_red.png | bin | 0 -> 659 bytes | |||
-rw-r--r-- | test/map/prefetch.test.cpp | 93 |
7 files changed, 123 insertions, 0 deletions
diff --git a/cmake/test-files.cmake b/cmake/test-files.cmake index edbf100fe2..4dec9e403d 100644 --- a/cmake/test-files.cmake +++ b/cmake/test-files.cmake @@ -28,6 +28,7 @@ set(MBGL_TEST_FILES # map test/map/map.test.cpp + test/map/prefetch.test.cpp test/map/transform.test.cpp # math diff --git a/test/fixtures/map/prefetch/empty.json b/test/fixtures/map/prefetch/empty.json new file mode 100644 index 0000000000..61a8fadcdb --- /dev/null +++ b/test/fixtures/map/prefetch/empty.json @@ -0,0 +1,5 @@ +{ + "version": 8, + "sources": {}, + "layers": [] +} diff --git a/test/fixtures/map/prefetch/expected.png b/test/fixtures/map/prefetch/expected.png Binary files differnew file mode 100644 index 0000000000..e1111b37f7 --- /dev/null +++ b/test/fixtures/map/prefetch/expected.png diff --git a/test/fixtures/map/prefetch/style.json b/test/fixtures/map/prefetch/style.json new file mode 100644 index 0000000000..b4e163888c --- /dev/null +++ b/test/fixtures/map/prefetch/style.json @@ -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" + }] +} diff --git a/test/fixtures/map/prefetch/tile_green.png b/test/fixtures/map/prefetch/tile_green.png Binary files differnew file mode 100644 index 0000000000..553cd10cd1 --- /dev/null +++ b/test/fixtures/map/prefetch/tile_green.png diff --git a/test/fixtures/map/prefetch/tile_red.png b/test/fixtures/map/prefetch/tile_red.png Binary files differnew file mode 100644 index 0000000000..5fa561fb92 --- /dev/null +++ b/test/fixtures/map/prefetch/tile_red.png diff --git a/test/map/prefetch.test.cpp b/test/map/prefetch.test.cpp new file mode 100644 index 0000000000..af2cd4e92f --- /dev/null +++ b/test/map/prefetch.test.cpp @@ -0,0 +1,93 @@ +#include <mbgl/test/util.hpp> +#include <mbgl/test/stub_file_source.hpp> + +#include <mbgl/gl/headless_backend.hpp> +#include <mbgl/gl/offscreen_view.hpp> +#include <mbgl/map/backend_scope.hpp> +#include <mbgl/map/map.hpp> +#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/style/style.hpp> +#include <mbgl/util/default_thread_pool.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; + +TEST(Map, PrefetchTiles) { + util::RunLoop runLoop; + HeadlessBackend backend(test::sharedDisplay()); + BackendScope scope(backend); + OffscreenView view(backend.getContext(), { 512, 512 }); + ThreadPool threadPool(4); + StubFileSource fileSource; + Map map(backend, view.getSize(), 1, fileSource, threadPool, MapMode::Still); + + std::vector<int> tiles; + + 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.getStyle().loadJSON(util::read_file("test/fixtures/map/prefetch/empty.json")); + map.getStyle().loadJSON(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, view)); + + ASSERT_TRUE(std::is_permutation(tiles.begin(), tiles.end(), expected.begin())); + ASSERT_FALSE(tiles.empty()); + }; + + // Check defaults, should be 4. + ASSERT_EQ(map.getPrefetchZoomDelta(), 4); + checkTilesForZoom(12, { 13, 13, 13, 13, 13, 13, 13, 13, 13, 9 }); + + // Setting it to 0 disables prefetching. + map.setPrefetchZoomDelta(0); + + // 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.setPrefetchZoomDelta(5); + checkTilesForZoom(12, { 13, 13, 13, 13, 13, 13, 13, 13, 13, 8 }); + + // Should clamp at `minzoom`. + map.setPrefetchZoomDelta(20); + checkTilesForZoom(10, { 11, 11, 11, 11, 11, 11, 11, 11, 11, 0 }); + + // Disabled again. + map.setPrefetchZoomDelta(0); + checkTilesForZoom(13, { 14, 14, 14, 14, 14, 14, 14, 14, 14 }); +} |