diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2020-03-02 15:36:13 +0200 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2020-03-05 17:43:37 +0200 |
commit | 9a0bc1b58b775209417582867c713b2015353a78 (patch) | |
tree | 1b4c5cc9074f2c260cdca4818bce2bfe9fde6fa2 /test | |
parent | deca8186147299b72061c1f2e67c13da8f07e0a6 (diff) | |
download | qtlocation-mapboxgl-9a0bc1b58b775209417582867c713b2015353a78.tar.gz |
[core] Add snapshotter unit tests
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 11 | ||||
-rw-r--r-- | test/fixtures/map/map_snapshotter_overlay/expected.png | bin | 0 -> 5379 bytes | |||
-rw-r--r-- | test/fixtures/map/online/style.json | 4 | ||||
-rw-r--r-- | test/map/map_snapshotter.test.cpp | 138 |
4 files changed, 151 insertions, 2 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 75d5ee3ef8..0248582c61 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -105,6 +105,17 @@ add_library( ${PROJECT_SOURCE_DIR}/test/util/url.test.cpp ) +# MapSnapshotter uses headless backend that is rendering image on a background thread. QT / macOS adaptation for headless backend creates +# new window and updates it's parameters, which is not allowed since macOS mohave 10.14. Following block disables snapshotter unit tests for +# QT on macOS. https://github.com/mapbox/mapbox-gl-native/issues/16267 +if(NOT (MBGL_WITH_QT AND CMAKE_SYSTEM_NAME STREQUAL Darwin)) + target_sources( + mbgl-test + PRIVATE + ${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/map/map_snapshotter.cpp ${PROJECT_SOURCE_DIR}/test/map/map_snapshotter.test.cpp + ) +endif() + if(MBGL_WITH_OPENGL) target_sources( mbgl-test diff --git a/test/fixtures/map/map_snapshotter_overlay/expected.png b/test/fixtures/map/map_snapshotter_overlay/expected.png Binary files differnew file mode 100644 index 0000000000..cf45c80237 --- /dev/null +++ b/test/fixtures/map/map_snapshotter_overlay/expected.png diff --git a/test/fixtures/map/online/style.json b/test/fixtures/map/online/style.json index 9543c477aa..921023526c 100644 --- a/test/fixtures/map/online/style.json +++ b/test/fixtures/map/online/style.json @@ -12,7 +12,7 @@ "id": "background", "type": "background", "paint": { - "background-color": "red" + "background-color": "green" } }, { "id": "water", @@ -20,7 +20,7 @@ "source": "mapbox", "source-layer": "water", "paint": { - "fill-pattern": "noise", + "fill-pattern": "a", "fill-antialias": false } }] diff --git a/test/map/map_snapshotter.test.cpp b/test/map/map_snapshotter.test.cpp new file mode 100644 index 0000000000..9d8eae8892 --- /dev/null +++ b/test/map/map_snapshotter.test.cpp @@ -0,0 +1,138 @@ +#include <mbgl/map/camera.hpp> +#include <mbgl/map/map_snapshotter.hpp> +#include <mbgl/storage/resource_options.hpp> +#include <mbgl/style/layers/fill_layer.hpp> +#include <mbgl/style/style.hpp> +#include <mbgl/test/util.hpp> +#include <mbgl/util/run_loop.hpp> +#include <mbgl/util/timer.hpp> + +using namespace mbgl; + +class SnapshotterObserver final : public MapSnapshotterObserver { +public: + ~SnapshotterObserver() = default; + void onDidFinishLoadingStyle() override { + if (didFinishLoadingStyleCallback) { + didFinishLoadingStyleCallback(); + } + } + std::function<void()> didFinishLoadingStyleCallback; +}; + +TEST(MapSnapshotter, setStyleJSON) { + util::RunLoop runLoop; + MapSnapshotter snapshotter(Size{32, 16}, 1.0f, ResourceOptions()); + snapshotter.setStyleJSON(R"JSON({ + "version": 8, + "layers": [{ + "id": "background", + "type": "background", + "paint": {"background-color": "green"} + }] + })JSON"); + + snapshotter.snapshot([&runLoop](std::exception_ptr ptr, + mbgl::PremultipliedImage image, + mbgl::MapSnapshotter::Attributions, + mbgl::MapSnapshotter::PointForFn, + mbgl::MapSnapshotter::LatLngForFn) { + EXPECT_EQ(nullptr, ptr); + EXPECT_EQ(32, image.size.width); + EXPECT_EQ(16, image.size.height); + runLoop.stop(); + }); + + runLoop.run(); +} + +TEST(MapSnapshotter, setSize) { + util::RunLoop runLoop; + MapSnapshotter snapshotter(Size{32, 16}, 1.0f, ResourceOptions()); + snapshotter.setStyleJSON(R"JSON({ + "version": 8, + "layers": [{ + "id": "background", + "type": "background", + "paint": {"background-color": "green"} + }] + })JSON"); + + snapshotter.setSize(Size{16, 32}); + + snapshotter.snapshot([&runLoop](std::exception_ptr ptr, + mbgl::PremultipliedImage image, + mbgl::MapSnapshotter::Attributions, + mbgl::MapSnapshotter::PointForFn, + mbgl::MapSnapshotter::LatLngForFn) { + EXPECT_EQ(nullptr, ptr); + EXPECT_EQ(16, image.size.width); + EXPECT_EQ(32, image.size.height); + runLoop.stop(); + }); + + runLoop.run(); +} + +TEST(MapSnapshotter, TEST_REQUIRES_SERVER(setStyleURL)) { + util::RunLoop runLoop; + MapSnapshotter snapshotter(Size{64, 32}, 1.0f, ResourceOptions()); + snapshotter.setStyleURL("http://127.0.0.1:3000/online/style.json"); + snapshotter.snapshot([&runLoop](std::exception_ptr ptr, + mbgl::PremultipliedImage image, + mbgl::MapSnapshotter::Attributions, + mbgl::MapSnapshotter::PointForFn, + mbgl::MapSnapshotter::LatLngForFn) { + EXPECT_EQ(nullptr, ptr); + EXPECT_EQ(64, image.size.width); + EXPECT_EQ(32, image.size.height); + runLoop.stop(); + }); + + runLoop.run(); +} + +TEST(MapSnapshotter, TEST_REQUIRES_SERVER(cancel)) { + util::RunLoop runLoop; + MapSnapshotter snapshotter(Size{1, 1}, 1.0f, ResourceOptions()); + snapshotter.setStyleURL("http://127.0.0.1:3000/online/style.json"); + snapshotter.snapshot([](std::exception_ptr, + mbgl::PremultipliedImage, + mbgl::MapSnapshotter::Attributions, + mbgl::MapSnapshotter::PointForFn, + mbgl::MapSnapshotter::LatLngForFn) { ASSERT_TRUE(false); }); + + snapshotter.cancel(); + + util::Timer timer; + timer.start(Milliseconds(100), Duration::zero(), [&runLoop] { runLoop.stop(); }); + + runLoop.run(); +} + +TEST(MapSnapshotter, TEST_REQUIRES_SERVER(runtimeStyling)) { + util::RunLoop runLoop; + SnapshotterObserver observer; + MapSnapshotter snapshotter(Size{256, 128}, 1.0f, ResourceOptions(), observer); + snapshotter.setStyleURL("http://127.0.0.1:3000/online/style.json"); + + observer.didFinishLoadingStyleCallback = [&snapshotter, &runLoop] { + auto fillLayer = std::make_unique<style::FillLayer>("green_fill", "mapbox"); + fillLayer->setSourceLayer("water"); + fillLayer->setFillColor(Color(0.25, 0.88, 0.82, 1.0)); + snapshotter.getStyle().addLayer(std::move(fillLayer)); + snapshotter.snapshot([&runLoop](std::exception_ptr ptr, + mbgl::PremultipliedImage image, + mbgl::MapSnapshotter::Attributions, + mbgl::MapSnapshotter::PointForFn, + mbgl::MapSnapshotter::LatLngForFn) { + EXPECT_EQ(nullptr, ptr); + EXPECT_EQ(256, image.size.width); + EXPECT_EQ(128, image.size.height); + test::checkImage("test/fixtures/map/map_snapshotter_overlay", image, 0.005, 0.1); + runLoop.stop(); + }); + }; + + runLoop.run(); +} |