[android] - add unit tests for Style.java API

This commit is contained in:
Tobrun
2018-12-06 17:21:33 +02:00
committed by Tobrun
parent 4a769077af
commit 222a997584
10 changed files with 440 additions and 47 deletions

View File

@@ -14,6 +14,7 @@ dependencies {
implementation dependenciesList.okhttp3
testImplementation dependenciesList.junit
testImplementation dependenciesList.mockito
testImplementation dependenciesList.mockk
testImplementation dependenciesList.robolectric
testImplementation dependenciesList.kotlinLib
}

View File

@@ -164,6 +164,12 @@ public final class LocationComponent {
this.mapboxMap = mapboxMap;
}
// used for creating a spy
LocationComponent() {
//noinspection ConstantConditions
mapboxMap = null;
}
@VisibleForTesting
LocationComponent(@NonNull MapboxMap mapboxMap,
@NonNull LocationEngineCallback<LocationEngineResult> currentlistener,

View File

@@ -147,7 +147,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
GesturesManagerInteractionListener registerTouchListener = new GesturesManagerInteractionListener();
// callback for camera change events
final CameraChangeDispatcher cameraChangeDispatcher = new CameraChangeDispatcher();
final CameraChangeDispatcher cameraDispatcher = new CameraChangeDispatcher();
// setup components for MapboxMap creation
Projection proj = new Projection(nativeMapView);
@@ -161,20 +161,20 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
ShapeAnnotations shapeAnnotations = new ShapeAnnotationContainer(nativeMapView, annotationsArray);
AnnotationManager annotationManager = new AnnotationManager(this, annotationsArray, iconManager,
annotations, markers, polygons, polylines, shapeAnnotations);
Transform transform = new Transform(this, nativeMapView, cameraChangeDispatcher);
Transform transform = new Transform(this, nativeMapView, cameraDispatcher);
// MapboxMap
mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, proj, registerTouchListener,
annotationManager, cameraChangeDispatcher);
mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, proj, registerTouchListener, cameraDispatcher);
mapboxMap.injectAnnotationManager(annotationManager);
// user input
mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings,
annotationManager, cameraChangeDispatcher);
annotationManager, cameraDispatcher);
mapKeyListener = new MapKeyListener(transform, uiSettings, mapGestureDetector);
// compass
compassView.injectCompassAnimationListener(createCompassAnimationListener(cameraChangeDispatcher));
compassView.setOnClickListener(createCompassClickListener(cameraChangeDispatcher));
compassView.injectCompassAnimationListener(createCompassAnimationListener(cameraDispatcher));
compassView.setOnClickListener(createCompassClickListener(cameraDispatcher));
// LocationComponent
mapboxMap.injectLocationComponent(new LocationComponent(mapboxMap));

View File

@@ -58,31 +58,27 @@ public final class MapboxMap {
private static final String TAG = "Mbgl-MapboxMap";
private final NativeMapView nativeMapView;
private final UiSettings uiSettings;
private final Projection projection;
private final Transform transform;
private final AnnotationManager annotationManager;
private final CameraChangeDispatcher cameraChangeDispatcher;
private final OnGesturesManagerInteractionListener onGesturesManagerInteractionListener;
private final List<Style.OnStyleLoaded> styleLoadedCallbacks = new ArrayList<>();
private LocationComponent locationComponent;
private AnnotationManager annotationManager;
@Nullable
private MapboxMap.OnFpsChangedListener onFpsChangedListener;
private final List<Style.OnStyleLoaded> styleLoadedCallbacks = new ArrayList<>();
@Nullable
private Style style;
MapboxMap(NativeMapView map, Transform transform, UiSettings ui, Projection projection,
OnGesturesManagerInteractionListener listener, AnnotationManager annotations,
CameraChangeDispatcher cameraChangeDispatcher) {
OnGesturesManagerInteractionListener listener, CameraChangeDispatcher cameraChangeDispatcher) {
this.nativeMapView = map;
this.uiSettings = ui;
this.projection = projection;
this.annotationManager = annotations.bind(this);
this.transform = transform;
this.onGesturesManagerInteractionListener = listener;
this.cameraChangeDispatcher = cameraChangeDispatcher;
@@ -762,7 +758,7 @@ public final class MapboxMap {
* will be triggered.
* </p>
*
* @param style The bundled style
* @param style The bundled style
* @param callback The callback to be invoked when the style has loaded
* @see Style
*/
@@ -793,7 +789,7 @@ public final class MapboxMap {
* and {@link MapView.OnDidFailLoadingMapListener} callback will be triggered.
* </p>
*
* @param builder The style builder
* @param builder The style builder
* @param callback The callback to be invoked when the style has loaded
* @see Style
*/
@@ -820,7 +816,7 @@ public final class MapboxMap {
}
}
private void notifyStyleLoaded() {
void notifyStyleLoaded() {
if (style != null) {
style.onDidFinishLoadingStyle();
locationComponent.onFinishLoadingStyle();
@@ -1814,6 +1810,10 @@ public final class MapboxMap {
this.locationComponent = locationComponent;
}
void injectAnnotationManager(AnnotationManager annotationManager) {
this.annotationManager = annotationManager.bind(this);
}
/**
* Returns the {@link LocationComponent} that can be used to display user's location on the map.
* <p>

View File

@@ -603,6 +603,22 @@ public class Style {
return styleJson;
}
List<Source> getSources() {
return sources;
}
List<LayerWrapper> getLayers() {
return layers;
}
List<ImageWrapper> getImages() {
return images;
}
TransitionOptions getTransitionOptions() {
return transitionOptions;
}
/**
* Build the composed style.
*/
@@ -610,41 +626,28 @@ public class Style {
return new Style(this, nativeMapView);
}
private class ImageWrapper {
private Bitmap bitmap;
private String id;
private boolean sdf;
class ImageWrapper {
Bitmap bitmap;
String id;
boolean sdf;
ImageWrapper(String id, Bitmap bitmap, boolean sdf) {
this.id = id;
this.bitmap = bitmap;
this.sdf = sdf;
}
public Bitmap getBitmap() {
return bitmap;
}
public String getId() {
return id;
}
public boolean isSdf() {
return sdf;
}
}
private class LayerWrapper {
private Layer layer;
class LayerWrapper {
Layer layer;
LayerWrapper(Layer layer) {
this.layer = layer;
}
}
private class LayerAboveWrapper extends LayerWrapper {
private String aboveLayer;
class LayerAboveWrapper extends LayerWrapper {
String aboveLayer;
LayerAboveWrapper(Layer layer, String aboveLayer) {
super(layer);
@@ -652,9 +655,8 @@ public class Style {
}
}
private class LayerBelowWrapper extends LayerWrapper {
private String belowLayer;
class LayerBelowWrapper extends LayerWrapper {
String belowLayer;
LayerBelowWrapper(Layer layer, String belowLayer) {
super(layer);
@@ -662,9 +664,8 @@ public class Style {
}
}
private class LayerAtWrapper extends LayerWrapper {
private int index;
class LayerAtWrapper extends LayerWrapper {
int index;
LayerAtWrapper(Layer layer, int index) {
super(layer);

View File

@@ -23,9 +23,9 @@ public class MapboxMapTest {
mock(UiSettings.class),
mock(Projection.class),
mock(MapboxMap.OnGesturesManagerInteractionListener.class),
mock(AnnotationManager.class),
mock(CameraChangeDispatcher.class)
);
mapboxMap.injectAnnotationManager(mock(AnnotationManager.class));
}
@Test(expected = IllegalArgumentException.class)

View File

@@ -0,0 +1,106 @@
package com.mapbox.mapboxsdk.maps
import android.graphics.Bitmap
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import com.mapbox.mapboxsdk.style.layers.TransitionOptions
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class StyleBuilderTest {
@Test
fun testFromUrl() {
val expected = Style.MAPBOX_STREETS
val builder = Style.Builder()
builder.fromUrl(expected)
assertEquals(expected, builder.url)
}
@Test
fun testFromJson() {
val expected = "{}"
val builder = Style.Builder()
builder.fromJson(expected)
assertEquals(expected, builder.json)
}
@Test
fun testWithLayer() {
val layer = mockk<SymbolLayer>()
val builder = Style.Builder()
builder.withLayer(layer)
assertEquals(layer, builder.layers[0].layer)
}
@Test
fun testWithLayerAt() {
val expectedIndex = 5
val layer = mockk<SymbolLayer>()
val builder = Style.Builder()
builder.withLayerAt(layer, expectedIndex)
assertEquals(layer, builder.layers[0].layer)
assertEquals(expectedIndex, (builder.layers[0] as Style.Builder.LayerAtWrapper).index)
}
@Test
fun testWithLayerAbove() {
val expectedAbove = "above"
val layer = mockk<SymbolLayer>()
val builder = Style.Builder()
builder.withLayerAbove(layer, expectedAbove)
assertEquals(layer, builder.layers[0].layer)
assertEquals(expectedAbove, (builder.layers[0] as Style.Builder.LayerAboveWrapper).aboveLayer)
}
@Test
fun testWithLayerBelow() {
val expectedBelow = "above"
val layer = mockk<SymbolLayer>()
val builder = Style.Builder()
builder.withLayerBelow(layer, expectedBelow)
assertEquals(layer, builder.layers[0].layer)
assertEquals(expectedBelow, (builder.layers[0] as Style.Builder.LayerBelowWrapper).belowLayer)
}
@Test
fun testWithSource() {
val source = mockk<GeoJsonSource>()
val builder = Style.Builder()
builder.withSource(source)
assertEquals(source, builder.sources[0])
}
@Test
fun testWithImage() {
val bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8)
val builder = Style.Builder()
builder.withImage("id", bitmap)
assertEquals(bitmap, builder.images[0].bitmap)
assertEquals("id", builder.images[0].id)
assertEquals(false, builder.images[0].sdf)
}
@Test
fun testWithImageSdf() {
val bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8)
val builder = Style.Builder()
builder.withImage("id", bitmap, true)
assertEquals(bitmap, builder.images[0].bitmap)
assertEquals("id", builder.images[0].id)
assertEquals(true, builder.images[0].sdf)
}
@Test
fun testWithTransitionOptions() {
val transitionOptions = TransitionOptions(100, 200)
val builder = Style.Builder()
builder.withTransition(transitionOptions)
assertEquals(100, builder.transitionOptions.duration)
assertEquals(200, builder.transitionOptions.delay)
}
}

View File

@@ -0,0 +1,275 @@
package com.mapbox.mapboxsdk.maps
import android.graphics.Bitmap
import com.mapbox.mapboxsdk.constants.MapboxConstants
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import com.mapbox.mapboxsdk.style.layers.TransitionOptions
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import io.mockk.every
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class StyleTest {
private lateinit var mapboxMap: MapboxMap
private lateinit var nativeMapView: NativeMapView
@Before
fun setup() {
nativeMapView = mockk()
mapboxMap = MapboxMap(nativeMapView, null, null, null, null, null)
every { nativeMapView.styleUrl = any() } answers {}
every { nativeMapView.styleJson = any() } answers {}
every { nativeMapView.addLayerBelow(any(), any()) } answers {}
every { nativeMapView.addLayerAbove(any(), any()) } answers {}
every { nativeMapView.addLayerAt(any(), any()) } answers {}
every { nativeMapView.addSource(any()) } answers {}
every { nativeMapView.addImage(any(), any(), any()) } answers {}
every { nativeMapView.transitionDuration = any() } answers {}
every { nativeMapView.transitionDelay = any() } answers {}
mapboxMap.injectLocationComponent(spyk())
}
@Test
fun testFromUrl() {
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
}
@Test
fun testFromJson() {
val builder = Style.Builder().fromJson("{}")
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleJson = "{}" }
}
@Test
fun testWithLayer() {
val layer = mockk<SymbolLayer>()
every { layer.id } returns "1"
val builder = Style.Builder().withLayer(layer)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.addLayerBelow(layer, MapboxConstants.LAYER_ID_ANNOTATIONS) }
}
@Test
fun testWithLayerAbove() {
val layer = mockk<SymbolLayer>()
every { layer.id } returns "1"
val builder = Style.Builder().withLayerAbove(layer, "id")
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.addLayerAbove(layer, "id") }
}
@Test
fun testWithLayerBelow() {
val layer = mockk<SymbolLayer>()
every { layer.id } returns "1"
val builder = Style.Builder().withLayerBelow(layer, "id")
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.addLayerBelow(layer, "id") }
}
@Test
fun testWithLayerAt() {
val layer = mockk<SymbolLayer>()
every { layer.id } returns "1"
val builder = Style.Builder().withLayerAt(layer, 1)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.addLayerAt(layer, 1) }
}
@Test
fun testWithSource() {
val source = mockk<GeoJsonSource>()
every { source.id } returns "1"
val builder = Style.Builder().withSource(source)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.addSource(source) }
}
@Test
fun testWithTransitionOptions() {
val transitionOptions = TransitionOptions(100, 200)
val builder = Style.Builder().withTransition(transitionOptions)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.transitionDuration = 100 }
verify(exactly = 1) { nativeMapView.transitionDelay = 200 }
}
@Test
fun testWithImage() {
val image = mockk<Bitmap>()
val builder = Style.Builder().withImage("id", image)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.addImage("id", image, false) }
}
@Test
fun testWithImageSdf() {
val image = mockk<Bitmap>()
val builder = Style.Builder().withImage("id", image, true)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.addImage("id", image, true) }
}
@Test
fun testWithFromLoadingSource() {
val source = mockk<GeoJsonSource>()
every { source.id } returns "1"
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS).withSource(source)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { nativeMapView.addSource(source) }
}
@Test
fun testWithFromLoadingLayer() {
val layer = mockk<SymbolLayer>()
every { layer.id } returns "1"
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS).withLayer(layer)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { nativeMapView.addLayerBelow(layer, MapboxConstants.LAYER_ID_ANNOTATIONS) }
}
@Test
fun testWithFromLoadingLayerAt() {
val layer = mockk<SymbolLayer>()
every { layer.id } returns "1"
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS).withLayerAt(layer, 1)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { nativeMapView.addLayerAt(layer, 1) }
}
@Test
fun testWithFromLoadingLayerBelow() {
val layer = mockk<SymbolLayer>()
every { layer.id } returns "1"
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS).withLayerBelow(layer, "below")
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { nativeMapView.addLayerBelow(layer, "below") }
}
@Test
fun testWithFromLoadingLayerAbove() {
val layer = mockk<SymbolLayer>()
every { layer.id } returns "1"
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS).withLayerBelow(layer, "below")
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { nativeMapView.addLayerBelow(layer, "below") }
}
@Test
fun testWithFromLoadingTransitionOptions() {
val transitionOptions = TransitionOptions(100, 200)
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS).withTransition(transitionOptions)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { nativeMapView.transitionDuration = 100 }
verify(exactly = 1) { nativeMapView.transitionDelay = 200 }
}
@Test
fun testWithFromImage() {
val bitmap = mockk<Bitmap>()
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS).withImage("id", bitmap)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { nativeMapView.addImage("id", bitmap, false) }
}
@Test
fun testWithFromImageSdf() {
val bitmap = mockk<Bitmap>()
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS).withImage("id", bitmap, true)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { nativeMapView.addImage("id", bitmap, true) }
}
@Test
fun testFromCallback() {
val callback = mockk<Style.OnStyleLoaded>()
every { callback.onStyleLoaded(any()) } answers {}
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS)
mapboxMap.setStyle(builder, callback)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { callback.onStyleLoaded(any()) }
}
@Test
fun testWithCallback() {
val callback = mockk<Style.OnStyleLoaded>()
every { callback.onStyleLoaded(any()) } answers {}
val source = mockk<GeoJsonSource>()
every { source.id } returns "1"
val builder = Style.Builder().withSource(source)
mapboxMap.setStyle(builder, callback)
verify(exactly = 1) { nativeMapView.addSource(source) }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { callback.onStyleLoaded(any()) }
}
@Test
fun testGetAsyncWith() {
val callback = mockk<Style.OnStyleLoaded>()
every { callback.onStyleLoaded(any()) } answers {}
mapboxMap.getStyle(callback)
val source = mockk<GeoJsonSource>()
every { source.id } returns "1"
val builder = Style.Builder().withSource(source)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.addSource(source) }
verify(exactly = 1) { callback.onStyleLoaded(any()) }
}
@Test
fun testGetAsyncFrom() {
val callback = mockk<Style.OnStyleLoaded>()
every { callback.onStyleLoaded(any()) } answers {}
mapboxMap.getStyle(callback)
val source = mockk<GeoJsonSource>()
every { source.id } returns "1"
val builder = Style.Builder().fromJson("{}")
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleJson = "{}" }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { callback.onStyleLoaded(any()) }
}
@Test
fun testGetAsyncWithFrom() {
val callback = mockk<Style.OnStyleLoaded>()
every { callback.onStyleLoaded(any()) } answers {}
mapboxMap.getStyle(callback)
val source = mockk<GeoJsonSource>()
every { source.id } returns "1"
val builder = Style.Builder().fromUrl(Style.MAPBOX_STREETS).withSource(source)
mapboxMap.setStyle(builder)
verify(exactly = 1) { nativeMapView.styleUrl = Style.MAPBOX_STREETS }
mapboxMap.notifyStyleLoaded()
verify(exactly = 1) { nativeMapView.addSource(source) }
verify(exactly = 1) { callback.onStyleLoaded(any()) }
}
}

View File

@@ -18,6 +18,7 @@ ext {
leakCanary : '1.6.1',
junit : '4.12',
mockito : '2.18.3',
mockk : '1.8.13.kotlin13',
robolectric : '3.8',
timber : '4.7.1',
okhttp : '3.11.0',
@@ -38,6 +39,7 @@ ext {
junit : "junit:junit:${versions.junit}",
mockito : "org.mockito:mockito-core:${versions.mockito}",
mockk : "io.mockk:mockk:${versions.mockk}",
robolectric : "org.robolectric:robolectric:${versions.robolectric}",
testRunner : "com.android.support.test:runner:${versions.testRunner}",

View File

@@ -39,5 +39,7 @@
"SymbolLayerActivity",
"LocationFragmentActivity",
"MergeOfflineRegionsActivity",
"NestedViewPagerActivity"
"NestedViewPagerActivity",
"DebugModeActivity",
"TextureViewDebugModeActivity"
]