[android] - move MapboxMapTest to java unit test

This commit is contained in:
tobrun
2018-12-11 16:28:27 +01:00
committed by Tobrun
parent aedbdc21d0
commit b1ae6c9051
3 changed files with 111 additions and 232 deletions

View File

@@ -1,47 +0,0 @@
package com.mapbox.mapboxsdk.maps;
import android.support.annotation.Nullable;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.mock;
public class MapboxMapTest {
@Nullable
private MapboxMap mapboxMap;
@Before
public void beforeTest() {
mapboxMap = new MapboxMap(mock(NativeMapView.class),
mock(Transform.class),
mock(UiSettings.class),
mock(Projection.class),
mock(MapboxMap.OnGesturesManagerInteractionListener.class),
mock(CameraChangeDispatcher.class)
);
mapboxMap.injectAnnotationManager(mock(AnnotationManager.class));
}
@Test(expected = IllegalArgumentException.class)
public void testAnimateCameraChecksDurationPositive() {
mapboxMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(30.0, 30.0)),
0, null);
}
@Test(expected = IllegalArgumentException.class)
public void testEaseCameraChecksDurationPositive() {
mapboxMap.easeCamera(CameraUpdateFactory.newLatLng(new LatLng(30.0, 30.0)),
0, null);
}
@After
public void afterTest() {
mapboxMap = null;
}
}

View File

@@ -0,0 +1,108 @@
package com.mapbox.mapboxsdk.maps
import com.mapbox.mapboxsdk.camera.CameraPosition
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.geometry.LatLngBounds
import com.mapbox.mapboxsdk.style.layers.TransitionOptions
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import junit.framework.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class MapboxMapTest {
private lateinit var mapboxMap: MapboxMap
private lateinit var nativeMapView: NativeMapView
@Before
fun setup() {
val cameraChangeDispatcher = spyk<CameraChangeDispatcher>()
val mapView = mockk<MapView>()
nativeMapView = mockk()
mapboxMap = MapboxMap(nativeMapView, Transform(mapView, nativeMapView, cameraChangeDispatcher), null, null, null, cameraChangeDispatcher)
every { nativeMapView.styleUrl = any() } answers {}
every { nativeMapView.transitionDuration = any() } answers {}
every { nativeMapView.transitionDelay = any() } answers {}
every { nativeMapView.isDestroyed } returns false
every { nativeMapView.cameraPosition } returns CameraPosition.DEFAULT
every { nativeMapView.cancelTransitions() } answers {}
every { nativeMapView.jumpTo(any(), any(), any(), any()) } answers {}
every { nativeMapView.minZoom = any() } answers {}
every { nativeMapView.maxZoom = any() } answers {}
every { nativeMapView.setOnFpsChangedListener(any()) } answers {}
every { nativeMapView.prefetchesTiles = any() } answers {}
every { nativeMapView.setLatLngBounds(any()) } answers {}
mapboxMap.injectLocationComponent(spyk())
mapboxMap.setStyle(Style.MAPBOX_STREETS)
mapboxMap.onFinishLoadingStyle()
}
@Test
fun testTransitionOptions() {
val expected = TransitionOptions(100, 200)
mapboxMap.style?.transition = expected
verify { nativeMapView.transitionDelay = 200 }
verify { nativeMapView.transitionDuration = 100 }
}
@Test
fun testMoveCamera() {
val callback = mockk<MapboxMap.CancelableCallback>()
every { callback.onFinish() } answers {}
val target = LatLng(1.0, 2.0)
val expected = CameraPosition.Builder().target(target).build()
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(expected), callback)
verify { nativeMapView.jumpTo(-1.0, target, -1.0, -1.0) }
verify { callback.onFinish() }
}
@Test
fun testMinZoom() {
mapboxMap.setMinZoomPreference(10.0)
verify { nativeMapView.minZoom = 10.0 }
}
@Test
fun testMaxZoom() {
mapboxMap.setMaxZoomPreference(10.0)
verify { nativeMapView.maxZoom = 10.0 }
}
@Test
fun testFpsListener() {
val fpsChangedListener = mockk<MapboxMap.OnFpsChangedListener>()
mapboxMap.onFpsChangedListener = fpsChangedListener
assertEquals("Listener should match", fpsChangedListener, mapboxMap.onFpsChangedListener)
}
@Test
fun testTilePrefetch() {
mapboxMap.prefetchesTiles = true
verify { nativeMapView.prefetchesTiles = true }
}
@Test
fun testCameraForLatLngBounds() {
val bounds = LatLngBounds.Builder().include(LatLng()).include(LatLng(1.0, 1.0)).build()
mapboxMap.setLatLngBoundsForCameraTarget(bounds)
verify { nativeMapView.setLatLngBounds(bounds) }
}
@Test(expected = IllegalArgumentException::class)
fun testAnimateCameraChecksDurationPositive() {
mapboxMap.animateCamera(CameraUpdateFactory.newLatLng(LatLng(30.0, 30.0)), 0, null)
}
@Test(expected = IllegalArgumentException::class)
fun testEaseCameraChecksDurationPositive() {
mapboxMap.easeCamera(CameraUpdateFactory.newLatLng(LatLng(30.0, 30.0)), 0, null)
}
}

View File

@@ -11,21 +11,13 @@ import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.exceptions.InvalidMarkerPositionException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity;
import com.mapbox.mapboxsdk.testapp.utils.TestConstants;
import com.mapbox.mapboxsdk.testapp.utils.ViewUtils;
import org.hamcrest.Matcher;
import org.junit.Ignore;
import org.junit.Test;
import timber.log.Timber;
import java.util.ArrayList;
import java.util.List;
@@ -33,7 +25,6 @@ import java.util.List;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.mapbox.mapboxsdk.testapp.utils.TestConstants.LAT_LNG_DELTA;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;
@@ -45,7 +36,9 @@ import static org.junit.Assert.assertTrue;
* Methods executed on MapboxMap are called from a ViewAction to ensure correct synchronisation
* with the application UI-thread.
* </p>
* @deprecated remove this file when removing deprecated annotations
*/
@Deprecated
public class MapboxMapTest extends BaseActivityTest {
@Override
@@ -59,113 +52,6 @@ public class MapboxMapTest extends BaseActivityTest {
assertNotNull("mapboxMap should not be null", mapboxMap);
}
//
// Style wide transition options
//
@Test
public void testTransitionDuration() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
long transitionDuration = 600;
mapboxMap.getStyle().setTransition(new TransitionOptions(transitionDuration, 0));
assertEquals(
"TransitionDuration should match",
transitionDuration,
mapboxMap.getStyle().getTransition().getDuration(),
0
);
}));
}
@Test
public void testTransitionDelay() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
long transitionDelay = 50;
mapboxMap.getStyle().setTransition(new TransitionOptions(600, transitionDelay));
assertEquals(
"TransitionDelay should match",
transitionDelay,
mapboxMap.getStyle().getTransition().getDelay(),
0
);
}));
}
//
// Camera tests
//
@Test
public void testCameraPositionOnFinish() {
ViewUtils.checkViewIsDisplayed(R.id.mapView);
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
final LatLng latLng = new LatLng(30.0, 30.0);
mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(latLng), new MapboxMap.CancelableCallback() {
@Override
public void onCancel() {
}
@Override
public void onFinish() {
LatLng cameraPositionLatLng = mapboxMap.getCameraPosition().target;
Timber.d(cameraPositionLatLng.toString());
assertEquals(cameraPositionLatLng.getLatitude(), latLng.getLatitude(), LAT_LNG_DELTA);
assertEquals(cameraPositionLatLng.getLongitude(), latLng.getLongitude(), LAT_LNG_DELTA);
}
});
}));
}
@Test
public void testCameraForLatLngBounds() {
ViewUtils.checkViewIsDisplayed(R.id.mapView);
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
// set
mapboxMap.setLatLngBoundsForCameraTarget(
new LatLngBounds.Builder().include(new LatLng()).include(new LatLng(1, 1)).build());
// reset
mapboxMap.setLatLngBoundsForCameraTarget(null);
}));
}
//
// MinZoomLevel
//
@Test
public void testMinZoom() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
mapboxMap.setMinZoomPreference(10);
assertEquals("MinZoom should match", 10, mapboxMap.getMinZoomLevel(), 10);
}));
}
@Test
public void testMaxZoom() {
validateTestSetup();
final double zoom = 10;
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
mapboxMap.setMaxZoomPreference(zoom);
assertEquals("MaxZoom should match", zoom, mapboxMap.getMaxZoomLevel(), 10);
}));
}
@Test
@Ignore
public void testInitialZoomLevels() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
assertEquals("MaxZoom should match", MapboxConstants.MAXIMUM_ZOOM, mapboxMap.getMaxZoomLevel(),
TestConstants.ZOOM_DELTA);
assertEquals("MinZoom should match", MapboxConstants.MINIMUM_ZOOM, mapboxMap.getMinZoomLevel(),
TestConstants.ZOOM_DELTA);
}));
}
//
// InfoWindow
//
@@ -198,59 +84,6 @@ public class MapboxMapTest extends BaseActivityTest {
}));
}
//
// setters/getters interfaces
//
@Test
public void testFpsListener() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
MapboxMap.OnFpsChangedListener fpsChangedListener = fps -> {
};
mapboxMap.setOnFpsChangedListener(fpsChangedListener);
assertEquals("FpsListener should match", fpsChangedListener, mapboxMap.getOnFpsChangedListener());
}));
}
@Test
public void testInfoWindowClickListener() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
MapboxMap.OnInfoWindowClickListener clickListener = marker -> false;
mapboxMap.setOnInfoWindowClickListener(clickListener);
assertEquals(
"InfoWidowClickListener should match", clickListener, mapboxMap.getOnInfoWindowClickListener()
);
}));
}
@Test
public void testInfoWindowCloseListener() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
MapboxMap.OnInfoWindowCloseListener listener = marker -> {
};
mapboxMap.setOnInfoWindowCloseListener(listener);
assertEquals("InfoWindowCloseListener should match", listener, mapboxMap.getOnInfoWindowCloseListener());
}));
}
@Test
public void testInfoWindowLongClickListener() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
MapboxMap.OnInfoWindowLongClickListener listener = marker -> {
};
mapboxMap.setOnInfoWindowLongClickListener(listener);
assertEquals("InfoWindowLongClickListener should match", listener,
mapboxMap.getOnInfoWindowLongClickListener());
}));
}
//
// Annotations
//
@@ -626,21 +459,6 @@ public class MapboxMapTest extends BaseActivityTest {
}));
}
// Tile pre-fetching
@Test
public void testTilePrefetch() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapboxMapAction((uiController, view) -> {
mapboxMap.setPrefetchesTiles(true);
assertTrue(mapboxMap.getPrefetchesTiles());
mapboxMap.setPrefetchesTiles(false);
assertFalse(mapboxMap.getPrefetchesTiles());
}));
}
//
public class MapboxMapAction implements ViewAction {
private InvokeViewAction invokeViewAction;
@@ -668,4 +486,4 @@ public class MapboxMapTest extends BaseActivityTest {
interface InvokeViewAction {
void onViewAction(UiController uiController, View view);
}
}
}