diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2018-12-10 10:40:17 +0100 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2018-12-11 18:08:16 +0100 |
commit | 75914acb6b66402539fd0913aed0aebe722b2faa (patch) | |
tree | 207203400ca38a8a7b101357a52773fbea824f11 /platform/android | |
parent | 5a4a7e7ea87532894aea0b21f58bcc807060d0e4 (diff) | |
download | qtlocation-mapboxgl-75914acb6b66402539fd0913aed0aebe722b2faa.tar.gz |
[android] - add more tests, refactor map padding
Diffstat (limited to 'platform/android')
6 files changed, 226 insertions, 32 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java index 891b040d6b..bdbca4fbca 100755 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java @@ -387,10 +387,11 @@ final class NativeMapView { nativeRotateBy(sx / pixelRatio, sy / pixelRatio, ex, ey, duration); } - public void setContentPadding(int[] padding) { + public void setContentPadding(float[] padding) { if (checkState("setContentPadding")) { return; } + // TopLeftBottomRight nativeSetContentPadding( padding[1] / pixelRatio, padding[0] / pixelRatio, @@ -398,6 +399,19 @@ final class NativeMapView { padding[2] / pixelRatio); } + public float[] getContentPadding() { + if (checkState("getContentPadding")) { + return new float[] {0, 0, 0, 0}; + } + float[] topLeftBottomRight = nativeGetContentPadding(); + return new float[]{ + topLeftBottomRight[1] * pixelRatio, + topLeftBottomRight[0] * pixelRatio, + topLeftBottomRight[3] * pixelRatio, + topLeftBottomRight[2] * pixelRatio + }; + } + public void setBearing(double degrees) { if (checkState("setBearing")) { return; @@ -1115,7 +1129,10 @@ final class NativeMapView { private native void nativeRotateBy(double sx, double sy, double ex, double ey, long duration); @Keep - private native void nativeSetContentPadding(double top, double left, double bottom, double right); + private native void nativeSetContentPadding(float top, float left, float bottom, float right); + + @Keep + private native float[] nativeGetContentPadding(); @Keep private native void nativeSetBearing(double degrees, long duration); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Projection.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Projection.java index 8e3092ae1d..c455ae606f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Projection.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Projection.java @@ -3,7 +3,6 @@ package com.mapbox.mapboxsdk.maps; import android.graphics.PointF; import android.support.annotation.FloatRange; import android.support.annotation.NonNull; - import com.mapbox.geojson.Point; import com.mapbox.mapboxsdk.constants.GeometryConstants; import com.mapbox.mapboxsdk.geometry.LatLng; @@ -32,7 +31,11 @@ public class Projection { void setContentPadding(int[] contentPadding) { this.contentPadding = contentPadding; - nativeMapView.setContentPadding(contentPadding); + float[] output = new float[contentPadding.length]; + for (int i = 0; i < contentPadding.length; i++) { + output[i] = contentPadding[i]; + } + nativeMapView.setContentPadding(output); } int[] getContentPadding() { @@ -121,10 +124,10 @@ public class Projection { top = 0; bottom = nativeMapView.getHeight(); } else { - left = contentPadding[0]; - right = nativeMapView.getWidth() - contentPadding[2]; - top = contentPadding[1]; - bottom = nativeMapView.getHeight() - contentPadding[3]; + left = (float) contentPadding[0]; + right = (float) (nativeMapView.getWidth() - contentPadding[2]); + top = (float) contentPadding[1]; + bottom = (float) (nativeMapView.getHeight() - contentPadding[3]); } LatLng center = fromScreenLocation(new PointF(left + (right - left) / 2, top + (bottom - top) / 2)); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt index baac389bda..c48b99964f 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt @@ -7,7 +7,10 @@ import android.support.test.annotation.UiThreadTest import android.support.test.runner.AndroidJUnit4 import com.mapbox.mapboxsdk.camera.CameraPosition import com.mapbox.mapboxsdk.geometry.LatLng +import com.mapbox.mapboxsdk.geometry.LatLngBounds +import com.mapbox.mapboxsdk.geometry.ProjectedMeters import com.mapbox.mapboxsdk.maps.renderer.MapRenderer +import com.mapbox.mapboxsdk.testapp.utils.TestConstants import junit.framework.Assert.assertEquals import org.junit.Before import org.junit.Test @@ -33,7 +36,7 @@ class NativeMapViewTest { @UiThreadTest fun before() { val context = InstrumentationRegistry.getContext() - nativeMapView = NativeMapView(context, false, null, null, DummyRenderer(context)) + nativeMapView = NativeMapView(context, 2.0f, false, null, null, DummyRenderer(context)) nativeMapView.resizeView(WIDTH, HEIGHT) } @@ -150,7 +153,7 @@ class NativeMapViewTest { @Test @UiThreadTest - fun testPrefetchTilesTrue(){ + fun testPrefetchTilesTrue() { val expected = true nativeMapView.prefetchesTiles = true val actual = nativeMapView.prefetchesTiles @@ -159,7 +162,7 @@ class NativeMapViewTest { @Test @UiThreadTest - fun testPrefetchTilesFalse(){ + fun testPrefetchTilesFalse() { val expected = false nativeMapView.prefetchesTiles = false val actual = nativeMapView.prefetchesTiles @@ -168,12 +171,171 @@ class NativeMapViewTest { @Test @UiThreadTest - fun testPrefetchTilesDefault(){ + fun testPrefetchTilesDefault() { val expected = true val actual = nativeMapView.prefetchesTiles assertEquals("Flag should match", expected, actual) } + @Test + @UiThreadTest + fun testSetStyleUrl() { + val expected = Style.DARK + nativeMapView.styleUrl = expected + val actual = nativeMapView.styleUrl + assertEquals("Style URL should match", expected, actual) + } + + @Test + @UiThreadTest + fun testSetStyleJson() { + val expected = "{}" + nativeMapView.styleJson = expected + val actual = nativeMapView.styleJson + assertEquals("Style JSON should match", expected, actual) + } + + @Test + @UiThreadTest + fun testSetContentPadding() { + val expected = floatArrayOf(1.0f, 2.0f, 3.0f, 4.0f) + nativeMapView.contentPadding = expected + val actual = nativeMapView.contentPadding + assertEquals("Left should match", expected[0], actual[0]) + assertEquals("Top should match", expected[1], actual[1]) + assertEquals("Right should match", expected[2], actual[2]) + assertEquals("Bottom should match", expected[3], actual[3]) + } + + @Test + @UiThreadTest + fun testSetMinZoom() { + val expected = 12.0 + nativeMapView.minZoom = expected + val actual = nativeMapView.minZoom + assertEquals("Min zoom should match", expected, actual) + } + + @Test + @UiThreadTest + fun testSetMaxZoom() { + val expected = 12.0 + nativeMapView.maxZoom = expected + val actual = nativeMapView.maxZoom + assertEquals("Max zoom should match", expected, actual) + } + + @Test + @UiThreadTest + fun testGetProjectedMetersAtLatitude() { + val expected = 38986.83510557766 + val actual = nativeMapView.getMetersPerPixelAtLatitude(5.0) + assertEquals("Get projected meters should match", expected, actual) + } + + @Test + @UiThreadTest + fun testLatLngForProjectedMeters() { + val expected = LatLng(0.01796630538796444, 0.02694945852363162) + val actual = nativeMapView.latLngForProjectedMeters(ProjectedMeters(2000.0, 3000.0)) + assertEquals("Get LatLng for projected meters", expected, actual) + } + + @Test + @UiThreadTest + fun testFlyTo() { + val expected = CameraPosition.Builder() + .zoom(12.0) + .tilt(30.0) + .target(LatLng(12.0, 14.0)) + .bearing(20.0) + .build() + nativeMapView.flyTo(expected.bearing, expected.target, 0, expected.tilt, expected.zoom) + val actual = nativeMapView.cameraPosition + assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) + assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) + assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) + } + + @Test + @UiThreadTest + fun testEaseTo() { + val expected = CameraPosition.Builder() + .zoom(12.0) + .tilt(30.0) + .target(LatLng(12.0, 14.0)) + .bearing(20.0) + .build() + nativeMapView.easeTo(expected.bearing, expected.target, 0, expected.tilt, expected.zoom, false) + val actual = nativeMapView.cameraPosition + assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) + assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) + assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) + } + + @Test + @UiThreadTest + fun testResetPosition() { + val expected = CameraPosition.Builder() + .zoom(0.0) + .tilt(0.0) + .target(LatLng(0.0, 0.0)) + .bearing(0.0) + .build() + nativeMapView.jumpTo(1.0, LatLng(1.0, 2.0), 23.0, 12.0) + nativeMapView.resetPosition() + val actual = nativeMapView.cameraPosition + assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) + assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) + assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) + } + + @Test + @UiThreadTest + fun testGetCameraForLatLngBounds() { + val expected = CameraPosition.Builder() + .zoom(3.5258764777024005) + .tilt(0.0) + .target(LatLng(23.182767623652808, 13.999999999994088)) + .bearing(0.0) + .build() + val actual = nativeMapView.getCameraForLatLngBounds( + LatLngBounds.from(30.0, 12.0, 16.0, 16.0), + intArrayOf(0, 0, 0, 0), + 0.0, + 0.0 + ) + assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) + assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) + assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) + } + + @Test + @UiThreadTest + fun testMoveBy() { + val expected = CameraPosition.Builder() + .zoom(0.0) + .tilt(0.0) + .target(LatLng(4.21494310024160, -4.218749958739409)) + .bearing(0.0) + .build() + nativeMapView.moveBy(12.0, 12.0) + val actual = nativeMapView.cameraPosition + assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA) + assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA) + assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA) + assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA) + } + class DummyRenderer(context: Context) : MapRenderer(context, null) { override fun requestRender() { diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/VisibleRegionTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/VisibleRegionTest.kt index f0d62dd370..e70005c6d7 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/VisibleRegionTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/VisibleRegionTest.kt @@ -2,8 +2,6 @@ package com.mapbox.mapboxsdk.testapp.maps import android.graphics.PointF import android.support.test.espresso.UiController -import com.mapbox.mapboxsdk.camera.CameraPosition -import com.mapbox.mapboxsdk.camera.CameraUpdate import com.mapbox.mapboxsdk.camera.CameraUpdateFactory import com.mapbox.mapboxsdk.geometry.LatLng import com.mapbox.mapboxsdk.maps.MapView @@ -67,10 +65,10 @@ class VisibleRegionTest : BaseActivityTest() { ) mapboxMap.setPadding( - (mapView.width / 4f).toInt(), - (mapView.height / 4f).toInt(), - (mapView.width / 4f).toInt(), - (mapView.height / 4f).toInt()) + mapView.width / 4, + mapView.height / 4, + mapView.width / 4, + mapView.height / 4) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } @@ -96,7 +94,7 @@ class VisibleRegionTest : BaseActivityTest() { mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f) ) - mapboxMap.setPadding((mapView.width / 4f).toInt(), 0, 0, 0) + mapboxMap.setPadding(mapView.width / 4, 0, 0, 0) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } @@ -122,7 +120,7 @@ class VisibleRegionTest : BaseActivityTest() { mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f) ) - mapboxMap.setPadding(0, (mapView.height / 4f).toInt(), 0, 0) + mapboxMap.setPadding(0, mapView.height / 4, 0, 0) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } @@ -148,7 +146,7 @@ class VisibleRegionTest : BaseActivityTest() { mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f) ) - mapboxMap.setPadding(0, 0, (mapView.width / 4f).toInt(), 0) + mapboxMap.setPadding(0, 0, mapView.width / 4, 0) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } @@ -174,7 +172,7 @@ class VisibleRegionTest : BaseActivityTest() { mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f) ) - mapboxMap.setPadding(0, 0, 0, (mapView.height / 4f).toInt()) + mapboxMap.setPadding(0, 0, 0, mapView.height / 4) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } @@ -222,10 +220,10 @@ class VisibleRegionTest : BaseActivityTest() { ) mapboxMap.setPadding( - (mapView.width / 4f).toInt(), - (mapView.height / 4f).toInt(), - (mapView.width / 4f).toInt(), - (mapView.height / 4f).toInt()) + mapView.width / 4, + mapView.height / 4, + mapView.width / 4, + mapView.height / 4) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } @@ -251,7 +249,7 @@ class VisibleRegionTest : BaseActivityTest() { mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f) ) - mapboxMap.setPadding((mapView.width / 4f).toInt(), 0, 0, 0) + mapboxMap.setPadding(mapView.width / 4, 0, 0, 0) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } @@ -277,7 +275,7 @@ class VisibleRegionTest : BaseActivityTest() { mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f) ) - mapboxMap.setPadding(0, (mapView.height / 4f).toInt(), 0, 0) + mapboxMap.setPadding(0, mapView.height / 4, 0, 0) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } @@ -303,7 +301,7 @@ class VisibleRegionTest : BaseActivityTest() { mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f) ) - mapboxMap.setPadding(0, 0, (mapView.width / 4f).toInt(), 0) + mapboxMap.setPadding(0, 0, mapView.width / 4, 0) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } @@ -329,7 +327,7 @@ class VisibleRegionTest : BaseActivityTest() { mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f) ) - mapboxMap.setPadding(0, 0, 0, (mapView.height / 4f).toInt()) + mapboxMap.setPadding(0, 0, 0, mapView.height / 4) val visibleRegion = mapboxMap.projection.getVisibleRegion(false) val filtered = latLngs.filter { visibleRegion.latLngBounds.contains(it) } diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index d908b14d36..a750baa62c 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -429,10 +429,21 @@ void NativeMapView::setVisibleCoordinateBounds(JNIEnv& env, const jni::Array<jni map->easeTo(cameraOptions, animationOptions); } -void NativeMapView::setContentPadding(JNIEnv&, double top, double left, double bottom, double right) { +void NativeMapView::setContentPadding(JNIEnv&, float top, float left, float bottom, float right) { insets = {top, left, bottom, right}; } +jni::Local<jni::Array<jni::jfloat>> NativeMapView::getContentPadding(JNIEnv& env) { + auto result = jni::Array<jni::jfloat>::New(env, 4); + std::vector<jfloat> vect; + vect.push_back(insets.top()); + vect.push_back(insets.left()); + vect.push_back(insets.bottom()); + vect.push_back(insets.right()); + result.SetRegion<std::vector<jni::jfloat>>(env, 0, vect); + return result; +} + void NativeMapView::scheduleSnapshot(jni::JNIEnv&) { mapRenderer.requestSnapshot([&](PremultipliedImage image) { auto _env = android::AttachEnv(); @@ -1005,6 +1016,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) { METHOD(&NativeMapView::resetNorth, "nativeResetNorth"), METHOD(&NativeMapView::setVisibleCoordinateBounds, "nativeSetVisibleCoordinateBounds"), METHOD(&NativeMapView::setContentPadding, "nativeSetContentPadding"), + METHOD(&NativeMapView::getContentPadding, "nativeGetContentPadding"), METHOD(&NativeMapView::scheduleSnapshot, "nativeTakeSnapshot"), METHOD(&NativeMapView::getCameraPosition, "nativeGetCameraPosition"), METHOD(&NativeMapView::updateMarker, "nativeUpdateMarker"), diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index 57676d3edf..b1f8354d46 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -138,7 +138,9 @@ public: void setVisibleCoordinateBounds(JNIEnv&, const jni::Array<jni::Object<LatLng>>&, const jni::Object<RectF>&, jni::jdouble, jni::jlong); - void setContentPadding(JNIEnv&, double, double, double, double); + void setContentPadding(JNIEnv&, float, float, float, float); + + jni::Local<jni::Array<jni::jfloat>> getContentPadding(JNIEnv&); void scheduleSnapshot(jni::JNIEnv&); |