From 5d960d5469d6fab94427d0f20c2d7efe48fc5894 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Tue, 24 Jan 2017 14:39:08 +0200 Subject: [PATCH] [tests] Add unit test for tile prefetching --- cmake/test-files.cmake | 1 + test/fixtures/map/prefetch/empty.json | 5 ++ test/fixtures/map/prefetch/expected.png | Bin 0 -> 2198 bytes test/fixtures/map/prefetch/style.json | 24 +++++ test/fixtures/map/prefetch/tile_green.png | Bin 0 -> 659 bytes test/fixtures/map/prefetch/tile_red.png | Bin 0 -> 659 bytes test/map/prefetch.test.cpp | 105 ++++++++++++++++++++++ 7 files changed, 135 insertions(+) create mode 100644 test/fixtures/map/prefetch/empty.json create mode 100644 test/fixtures/map/prefetch/expected.png create mode 100644 test/fixtures/map/prefetch/style.json create mode 100644 test/fixtures/map/prefetch/tile_green.png create mode 100644 test/fixtures/map/prefetch/tile_red.png create mode 100644 test/map/prefetch.test.cpp diff --git a/cmake/test-files.cmake b/cmake/test-files.cmake index edbf100fe23..4dec9e403dc 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 00000000000..61a8fadcdb0 --- /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 new file mode 100644 index 0000000000000000000000000000000000000000..e1111b37f7a7ea8b288d84fae61f761549e72cd9 GIT binary patch literal 2198 zcmeAS@N?(olHy`uVBq!ia0y~yU;;9k7&zE~)R&4YzZe)eVm)0PLn`LHy=KVBpulrr zLu38<2TXlxTvcUfXBsm&ocbrrz_2HsfkEK}1B1f^Mur9jW(EdFpaD%h3=AxiKog7@ z7z9#A6^w?%XnGjU7o+9FXgN7r9gJ3skmf<`_T8-i&i$Ph2yAaKc)I$ztaD0e0su`# B98>@R literal 0 HcmV?d00001 diff --git a/test/fixtures/map/prefetch/style.json b/test/fixtures/map/prefetch/style.json new file mode 100644 index 00000000000..b4e163888cf --- /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 new file mode 100644 index 0000000000000000000000000000000000000000..553cd10cd10223d2faca5f264311f16a3309ed59 GIT binary patch literal 659 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58911L)MWvCLm|H(?D8gCb z5n0T@z%2~Ij105pNB{-dOFVsD*&i}W@R+EW8{J|B3Q3l@MwB?`=jNv7l`uFLr6!i7 zrYMwWmSiZnd-?{1H}Z)C?fUKM;uunK>+NMnUIqq^BO93i&wJR*7RBiuHuFHVE+ZQQ za|7c6h6Dx!1_=fp25NI@V=9t+k(e~`3AF)|3gEyIe}?~!PC{x JWt~$(69B1DZ(jfa literal 0 HcmV?d00001 diff --git a/test/fixtures/map/prefetch/tile_red.png b/test/fixtures/map/prefetch/tile_red.png new file mode 100644 index 0000000000000000000000000000000000000000..5fa561fb92cc159539a420b54877157ff00a0575 GIT binary patch literal 659 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58911L)MWvCLm|H(?D8gCb z5n0T@z%2~Ij105pNB{-dOFVsD*&i}W@EEI2tJQr66p}1)jVN)>&&^HED`9XhN=+-b3 z3$qrBmmP2gQu&X J%Q~loCIGsVasB`R literal 0 HcmV?d00001 diff --git a/test/map/prefetch.test.cpp b/test/map/prefetch.test.cpp new file mode 100644 index 00000000000..efebc5ac474 --- /dev/null +++ b/test/map/prefetch.test.cpp @@ -0,0 +1,105 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +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 tiles; + + fileSource.response = [&] (const Resource& res) -> optional { + 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( + util::read_file("test/fixtures/map/prefetch/tile_green.png")); + } else { + response.data = std::make_shared( + util::read_file("test/fixtures/map/prefetch/tile_red.png")); + } + + return { std::move(response) }; + }; + + auto checkTilesForZoom = [&](int zoom, const std::vector& 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()); + }; + + // 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({}); + 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({}); + 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 }); +}