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

Commit

Permalink
[android] - instrumented unit tests, add map tests, refactor generate…
Browse files Browse the repository at this point in the history
…d layer tests
  • Loading branch information
tobrun committed Nov 23, 2018
1 parent 6bb1e97 commit d1de3af
Show file tree
Hide file tree
Showing 13 changed files with 2,081 additions and 2,745 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ final class NativeMapView {
private ViewCallback viewCallback;

// Used for map change callbacks
@Nullable
private StateCallback stateCallback;

// Device density
Expand Down Expand Up @@ -308,6 +309,10 @@ public double getPitch() {
return nativeGetPitch();
}

public void setPitch(double pitch) {
setPitch(pitch, 0);
}

public void setPitch(double pitch, long duration) {
if (checkState("setPitch")) {
return;
Expand Down Expand Up @@ -918,62 +923,86 @@ RectF getDensityDependantRectangle(final RectF rectangle) {

@Keep
private void onCameraWillChange(boolean animated) {
stateCallback.onCameraWillChange(animated);
if (stateCallback != null) {
stateCallback.onCameraWillChange(animated);
}
}

@Keep
private void onCameraIsChanging() {
stateCallback.onCameraIsChanging();
if (stateCallback != null) {
stateCallback.onCameraIsChanging();
}
}

@Keep
private void onCameraDidChange(boolean animated) {
stateCallback.onCameraDidChange(animated);
if (stateCallback != null) {
stateCallback.onCameraDidChange(animated);
}
}

@Keep
private void onWillStartLoadingMap() {
stateCallback.onWillStartLoadingMap();
if (stateCallback != null) {
stateCallback.onWillStartLoadingMap();
}
}

@Keep
private void onDidFinishLoadingMap() {
stateCallback.onDidFinishLoadingMap();
if (stateCallback != null) {
stateCallback.onDidFinishLoadingMap();
}
}

@Keep
private void onDidFailLoadingMap(String error) {
stateCallback.onDidFailLoadingMap(error);
if (stateCallback != null) {
stateCallback.onDidFailLoadingMap(error);
}
}

@Keep
private void onWillStartRenderingFrame() {
stateCallback.onWillStartRenderingFrame();
if (stateCallback != null) {
stateCallback.onWillStartRenderingFrame();
}
}

@Keep
private void onDidFinishRenderingFrame(boolean fully) {
stateCallback.onDidFinishRenderingFrame(fully);
if (stateCallback != null) {
stateCallback.onDidFinishRenderingFrame(fully);
}
}

@Keep
private void onWillStartRenderingMap() {
stateCallback.onWillStartRenderingMap();
if (stateCallback != null) {
stateCallback.onWillStartRenderingMap();
}
}

@Keep
private void onDidFinishRenderingMap(boolean fully) {
stateCallback.onDidFinishRenderingMap(fully);
if (stateCallback != null) {
stateCallback.onDidFinishRenderingMap(fully);
}
}

@Keep
private void onDidFinishLoadingStyle() {
stateCallback.onDidFinishLoadingStyle();
if (stateCallback != null) {
stateCallback.onDidFinishLoadingStyle();
}
}

@Keep
private void onSourceChanged(String sourceId) {
stateCallback.onSourceChanged(sourceId);
if (stateCallback != null) {
stateCallback.onSourceChanged(sourceId);
}
}

@Keep
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mapbox.mapboxsdk.maps

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import com.mapbox.mapboxsdk.style.layers.Layer
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
abstract class BaseLayerTest {
private lateinit var nativeMapView: NativeMapView

companion object {
const val WIDTH = 500
const val HEIGHT = WIDTH
}

fun before() {
val context = InstrumentationRegistry.getContext()
nativeMapView = NativeMapView(context, false, null, null, NativeMapViewTest.DummyRenderer(context))
nativeMapView.resizeView(WIDTH, HEIGHT)
}

fun setupLayer(layer: Layer) {
nativeMapView.addLayer(layer)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package com.mapbox.mapboxsdk.maps

import android.content.Context
import android.graphics.PointF
import android.support.test.InstrumentationRegistry
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.maps.renderer.MapRenderer
import junit.framework.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class NativeMapViewTest {

private lateinit var nativeMapView: NativeMapView

companion object {
const val DELTA = 0.000001
const val DELTA_BIG = 1.0
const val BEARING_TEST = 60.0
const val PITCH_TEST = 40.0
const val ZOOM_TEST = 16.0
const val WIDTH = 500
const val HEIGHT = WIDTH
val LATLNG_TEST = LatLng(12.0, 34.0)
}

@Before
@UiThreadTest
fun before() {
val context = InstrumentationRegistry.getContext()
nativeMapView = NativeMapView(context, false, null, null, DummyRenderer(context))
nativeMapView.resizeView(WIDTH, HEIGHT)
}

@Test
@UiThreadTest
fun testBearing() {
val expected = BEARING_TEST
nativeMapView.bearing = expected
val actual = nativeMapView.bearing
assertEquals("Bearing should match", expected, actual, DELTA)
}

@Test
@UiThreadTest
fun testLatLng() {
val expected = LATLNG_TEST
nativeMapView.latLng = expected
val actual = nativeMapView.latLng
assertEquals("Latitude should match", expected.latitude, actual.latitude, DELTA)
assertEquals("Longitude should match", expected.longitude, actual.longitude, DELTA)
}

@Test
@UiThreadTest
fun testLatLngDefault() {
val expected = LatLng()
val actual = nativeMapView.latLng
assertEquals("Latitude should match", expected.latitude, actual.latitude, DELTA)
assertEquals("Longitude should match", expected.longitude, actual.longitude, DELTA)
}


@Test
@UiThreadTest
fun testBearingDefault() {
val expected = 0.0
val actual = nativeMapView.bearing
assertEquals("Bearing should match", expected, actual, DELTA)
}

@Test
@UiThreadTest
fun testPitch() {
val expected = PITCH_TEST
nativeMapView.pitch = expected
val actual = nativeMapView.pitch
assertEquals("Pitch should match", expected, actual, DELTA)
}

@Test
@UiThreadTest
fun testPitchDefault() {
val expected = 0.0
val actual = nativeMapView.pitch
assertEquals("Pitch should match", expected, actual, DELTA)
}

@Test
@UiThreadTest
fun testZoom() {
val expected = ZOOM_TEST
nativeMapView.setZoom(expected, PointF(0.0f, 0.0f), 0)
val actual = nativeMapView.zoom
assertEquals("Zoom should match", expected, actual, DELTA)
}

@Test
@UiThreadTest
fun testZoomDefault() {
val expected = 0.0
val actual = nativeMapView.zoom
assertEquals("Zoom should match", expected, actual, DELTA)
}

@Test
@UiThreadTest
fun testJumpTo() {
val expected = CameraPosition.Builder()
.bearing(BEARING_TEST)
.target(LATLNG_TEST)
.tilt(PITCH_TEST)
.zoom(ZOOM_TEST)
.build()
nativeMapView.jumpTo(BEARING_TEST, LATLNG_TEST, PITCH_TEST, ZOOM_TEST)
val actual = nativeMapView.cameraPosition
assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, DELTA)
assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, DELTA)
assertEquals("Bearing should match", expected.bearing, actual.bearing, DELTA)
assertEquals("Pitch should match", expected.tilt, actual.tilt, DELTA)
assertEquals("Zoom should match", expected.zoom, actual.zoom, DELTA)
}

@Test
@UiThreadTest
fun testLatLngForPixel() {
val expected = LATLNG_TEST
nativeMapView.latLng = LATLNG_TEST
val actual = nativeMapView.latLngForPixel(
PointF((WIDTH / 2).toFloat(), (HEIGHT / 2).toFloat())
)
assertEquals("Latitude should match", expected.latitude, actual.latitude, DELTA_BIG)
assertEquals("Longitude should match", expected.longitude, actual.longitude, DELTA_BIG)
}

@Test
@UiThreadTest
fun testPixelForLatLng() {
val expected = PointF((WIDTH / 2).toFloat(), (HEIGHT / 2).toFloat())
nativeMapView.latLng = LATLNG_TEST
val actual = nativeMapView.pixelForLatLng(LATLNG_TEST)
assertEquals("X should match", expected.x.toDouble(), actual.x.toDouble(), DELTA_BIG)
assertEquals("Y should match", expected.y.toDouble(), actual.y.toDouble(), DELTA_BIG)
}

@Test
@UiThreadTest
fun testPrefetchTilesTrue(){
val expected = true
nativeMapView.prefetchesTiles = true
val actual = nativeMapView.prefetchesTiles
assertEquals("Flag should match", expected, actual)
}

@Test
@UiThreadTest
fun testPrefetchTilesFalse(){
val expected = false
nativeMapView.prefetchesTiles = false
val actual = nativeMapView.prefetchesTiles
assertEquals("Flag should match", expected, actual)
}

@Test
@UiThreadTest
fun testPrefetchTilesDefault(){
val expected = true
val actual = nativeMapView.prefetchesTiles
assertEquals("Flag should match", expected, actual)
}

class DummyRenderer(context: Context) : MapRenderer(context, null) {

override fun requestRender() {
//no-op
}

override fun queueEvent(runnable: Runnable?) {
//no-op
}
}
}
Loading

0 comments on commit d1de3af

Please sign in to comment.