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/map/map_snapshotter.test.cpp | |
parent | deca8186147299b72061c1f2e67c13da8f07e0a6 (diff) | |
download | qtlocation-mapboxgl-9a0bc1b58b775209417582867c713b2015353a78.tar.gz |
[core] Add snapshotter unit tests
Diffstat (limited to 'test/map/map_snapshotter.test.cpp')
-rw-r--r-- | test/map/map_snapshotter.test.cpp | 138 |
1 files changed, 138 insertions, 0 deletions
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(); +} |