From 2fd9d4a1feee88741d2a869c1b6333085e7fe58e Mon Sep 17 00:00:00 2001 From: Aleksandar Stojiljkovic Date: Tue, 17 Dec 2019 15:04:17 +0200 Subject: Include padding when testing camera scale in cameraForLatLngBounds Repurpose LatLngBoundsToCameraWithBearingAndPitch to test scaling and camera setup, both with and without padding. This adds testing of path not covered in mapbox/mapbox-gl-native-ios#59. --- src/mbgl/map/map.cpp | 5 ++++- test/map/map.test.cpp | 34 ++++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index cb31d434fa..9cd9fdc15f 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -213,7 +213,10 @@ CameraOptions cameraForLatLngs(const std::vector& latLngs, const Transfo ScreenCoordinate centerPixel = nePixel + swPixel; centerPixel /= 2.0; - return CameraOptions().withCenter(transform.screenCoordinateToLatLng(centerPixel)).withPadding(padding).withZoom(zoom); + return CameraOptions() + .withCenter(transform.screenCoordinateToLatLng(centerPixel)) + .withPadding(padding) + .withZoom(zoom); } CameraOptions Map::cameraForLatLngs(const std::vector& latLngs, const EdgeInsets& padding, optional bearing, optional pitch) const { diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index 9e37f97b8b..31cc85619e 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -5,26 +5,27 @@ #include #include -#include #include -#include #include -#include -#include +#include +#include +#include #include +#include #include -#include -#include -#include -#include -#include +#include #include #include #include #include #include #include +#include +#include #include +#include +#include +#include using namespace mbgl; using namespace mbgl::style; @@ -177,7 +178,7 @@ TEST(Map, LatLngBoundsToCameraWithBearing) { EXPECT_NEAR(virtualCamera.bearing.value_or(0), 35.0, 1e-5); } -TEST(Map, LatLngBoundsToCameraWithBearingAndPitch) { +TEST(Map, LatLngBoundsToCameraWithBearingPitchAndPadding) { MapTest<> test; test.map.jumpTo(CameraOptions().withCenter(LatLng { 40.712730, -74.005953 }).withZoom(16.0)); @@ -189,6 +190,19 @@ TEST(Map, LatLngBoundsToCameraWithBearingAndPitch) { EXPECT_NEAR(*virtualCamera.zoom, 13.66272, 1e-5); ASSERT_DOUBLE_EQ(*virtualCamera.pitch, 20.0); EXPECT_NEAR(virtualCamera.bearing.value_or(0), 35.0, 1e-5); + + const EdgeInsets padding = EdgeInsets{10, 20, 30, 40}; + const CameraOptions virtualCameraPadded = test.map.cameraForLatLngBounds(bounds, padding, 35, 20); + ASSERT_TRUE(bounds.contains(*virtualCameraPadded.center)); + ASSERT_DOUBLE_EQ(virtualCameraPadded.center->latitude(), virtualCamera.center->latitude()); + ASSERT_DOUBLE_EQ(virtualCameraPadded.center->longitude(), virtualCamera.center->longitude()); + + const Size size = test.map.getMapOptions().size(); + const auto scaleChange = std::min((size.width - padding.left() - padding.right()) / size.width, + (size.height - padding.top() - padding.bottom()) / size.height); + ASSERT_DOUBLE_EQ(*virtualCameraPadded.zoom, *virtualCamera.zoom + util::log2(scaleChange)); + ASSERT_DOUBLE_EQ(*virtualCameraPadded.pitch, *virtualCamera.pitch); + ASSERT_DOUBLE_EQ(*virtualCameraPadded.bearing, *virtualCamera.bearing); } TEST(Map, LatLngsToCamera) { -- cgit v1.2.1