From d61c6697d212c75462e297411bb99b942a433610 Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Tue, 17 Oct 2017 18:50:21 -0700 Subject: [core] API Render test for CustomVectorSource --- cmake/test-files.cmake | 1 + src/mbgl/style/custom_tile_loader.cpp | 7 +- test/api/custom_vector_source.test.cpp | 72 +++++++++++++++++++++ .../custom_vector_source/grid/expected.png | Bin 0 -> 8302 bytes 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 test/api/custom_vector_source.test.cpp create mode 100644 test/fixtures/custom_vector_source/grid/expected.png diff --git a/cmake/test-files.cmake b/cmake/test-files.cmake index 8d3e8bd346..3e1dbb8fd0 100644 --- a/cmake/test-files.cmake +++ b/cmake/test-files.cmake @@ -16,6 +16,7 @@ set(MBGL_TEST_FILES test/api/annotations.test.cpp test/api/api_misuse.test.cpp test/api/custom_layer.test.cpp + test/api/custom_vector_source.test.cpp test/api/query.test.cpp test/api/recycle_map.cpp test/api/zoom_history.cpp diff --git a/src/mbgl/style/custom_tile_loader.cpp b/src/mbgl/style/custom_tile_loader.cpp index b7120a2a5c..09d43373d6 100644 --- a/src/mbgl/style/custom_tile_loader.cpp +++ b/src/mbgl/style/custom_tile_loader.cpp @@ -10,9 +10,7 @@ CustomTileLoader::CustomTileLoader(const TileFunction& fetchTileFn, const TileFu void CustomTileLoader::fetchTile(const OverscaledTileID& tileID, ActorRef callbackRef) { auto cachedTileData = dataCache.find(tileID.canonical); - if (cachedTileData == dataCache.end()) { - invokeTileFetch(tileID.canonical); - } else { + if (cachedTileData != dataCache.end()) { callbackRef.invoke(&SetTileDataFunction::operator(), *(cachedTileData->second)); } auto tileCallbacks = tileCallbackMap.find(tileID.canonical); @@ -28,6 +26,9 @@ void CustomTileLoader::fetchTile(const OverscaledTileID& tileID, ActorRefsecond.emplace_back(std::make_tuple(tileID.overscaledZ, tileID.wrap, callbackRef)); } + if (cachedTileData == dataCache.end()) { + invokeTileFetch(tileID.canonical); + } } void CustomTileLoader::cancelTile(const OverscaledTileID& tileID) { diff --git a/test/api/custom_vector_source.test.cpp b/test/api/custom_vector_source.test.cpp new file mode 100644 index 0000000000..76445c1500 --- /dev/null +++ b/test/api/custom_vector_source.test.cpp @@ -0,0 +1,72 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace mbgl; +using namespace mbgl::style; + + +TEST(CustomVectorSource, Grid) { + util::RunLoop loop; + + DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); + ThreadPool threadPool(4); + float pixelRatio { 1 }; + HeadlessFrontend frontend { pixelRatio, fileSource, threadPool }; + Map map(frontend, MapObserver::nullObserver(), frontend.getSize(), pixelRatio, fileSource, + threadPool, MapMode::Still); + map.getStyle().loadJSON(util::read_file("test/fixtures/api/water.json")); + map.setLatLngZoom({ 37.8, -122.5 }, 10); + + CustomVectorSource::Options options; + options.fetchTileFunction = [&map](const mbgl::CanonicalTileID& tileID) { + double gridSpacing = 0.1; + FeatureCollection features; + const LatLngBounds bounds(tileID); + for (double y = ceil(bounds.north() / gridSpacing) * gridSpacing; y >= floor(bounds.south() / gridSpacing) * gridSpacing; y -= gridSpacing) { + + mapbox::geojson::line_string gridLine; + gridLine.emplace_back(bounds.west(), y); + gridLine.emplace_back(bounds.east(), y); + + features.emplace_back(gridLine); + } + + for (double x = floor(bounds.west() / gridSpacing) * gridSpacing; x <= ceil(bounds.east() / gridSpacing) * gridSpacing; x += gridSpacing) { + mapbox::geojson::line_string gridLine; + gridLine.emplace_back(x, bounds.south()); + gridLine.emplace_back(x, bounds.north()); + + features.emplace_back(gridLine); + } + auto source = static_cast(map.getStyle().getSource("custom")); + if (source) { + source->setTileData(tileID, features); + } + }; + + map.getStyle().addSource(std::make_unique("custom", options)); + + auto fillLayer = std::make_unique("landcover", "mapbox"); + fillLayer->setSourceLayer("landcover"); + fillLayer->setFillColor(Color{ 1.0, 1.0, 0.0, 1.0 }); + map.getStyle().addLayer(std::move(fillLayer)); + + auto layer = std::make_unique("grid", "custom"); + layer->setLineColor(Color{ 1.0, 1.0, 1.0, 1.0 }); + map.getStyle().addLayer(std::move(layer)); + + test::checkImage("test/fixtures/custom_vector_source/grid", frontend.render(map), 0.0006, 0.1); +} diff --git a/test/fixtures/custom_vector_source/grid/expected.png b/test/fixtures/custom_vector_source/grid/expected.png new file mode 100644 index 0000000000..628a3b11fb Binary files /dev/null and b/test/fixtures/custom_vector_source/grid/expected.png differ -- cgit v1.2.1