Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - add more tests, refactor map padding
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Dec 11, 2018
1 parent 5a4a7e7 commit 75914ac
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,17 +387,31 @@ public void rotateBy(double sx, double sy, double ex, double ey,
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,
padding[3] / pixelRatio,
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;
Expand Down Expand Up @@ -1115,7 +1129,10 @@ private native CameraPosition nativeGetCameraForGeometry(
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -121,10 +124,10 @@ public VisibleRegion getVisibleRegion(boolean ignorePadding) {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -150,7 +153,7 @@ class NativeMapViewTest {

@Test
@UiThreadTest
fun testPrefetchTilesTrue(){
fun testPrefetchTilesTrue() {
val expected = true
nativeMapView.prefetchesTiles = true
val actual = nativeMapView.prefetchesTiles
Expand All @@ -159,7 +162,7 @@ class NativeMapViewTest {

@Test
@UiThreadTest
fun testPrefetchTilesFalse(){
fun testPrefetchTilesFalse() {
val expected = false
nativeMapView.prefetchesTiles = false
val actual = nativeMapView.prefetchesTiles
Expand All @@ -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() {
Expand Down
Loading

0 comments on commit 75914ac

Please sign in to comment.