From 86ed5b0e1dbe0dae375d2a438898f7f3ebac7896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Mon, 18 Mar 2019 11:36:49 +0100 Subject: [PATCH] [android] cache location layer IDs in a set instead of a list --- .../location/LocationLayerController.java | 17 ++++---- .../location/LocationLayerControllerTest.java | 40 +++++++++++-------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationLayerController.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationLayerController.java index f11acacf316..aa8a82bf6d6 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationLayerController.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationLayerController.java @@ -5,6 +5,7 @@ import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -18,7 +19,6 @@ import com.mapbox.mapboxsdk.style.layers.SymbolLayer; import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -70,7 +70,8 @@ final class LocationLayerController { private LocationComponentOptions options; private final OnRenderModeChangedListener internalRenderModeChangedListener; - private final List layerMap = new ArrayList<>(); + @VisibleForTesting + final Set layerSet = new HashSet<>(); private Feature locationFeature; private GeoJsonSource locationSource; @@ -112,7 +113,7 @@ void applyStyle(@NonNull LocationComponentOptions options) { removeLayers(); addLayers(newLayerBelowOption); if (isHidden) { - for (String layerId : layerMap) { + for (String layerId : layerSet) { setLayerVisibility(layerId, false); } } @@ -193,7 +194,7 @@ void show() { void hide() { isHidden = true; - for (String layerId : layerMap) { + for (String layerId : layerSet) { setLayerVisibility(layerId, false); } } @@ -257,14 +258,14 @@ private void addAccuracyLayer() { private void addLayerToMap(Layer layer, @NonNull String idBelowLayer) { style.addLayerBelow(layer, idBelowLayer); - layerMap.add(layer.getId()); + layerSet.add(layer.getId()); } private void removeLayers() { - for (String layerId : layerMap) { + for (String layerId : layerSet) { style.removeLayer(layerId); } - layerMap.clear(); + layerSet.clear(); } private void setBearingProperty(@NonNull String propertyId, float bearing) { @@ -351,7 +352,7 @@ private void styleForeground(LocationComponentOptions options) { } private void styleScaling(@NonNull LocationComponentOptions options) { - for (String layerId : layerMap) { + for (String layerId : layerSet) { Layer layer = style.getLayer(layerId); if (layer instanceof SymbolLayer) { layer.setProperties( diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.java index 10553700b38..6a44cf1f79d 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.java +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.java @@ -39,6 +39,7 @@ import static com.mapbox.mapboxsdk.location.MapboxAnimator.ANIMATOR_LAYER_COMPASS_BEARING; import static com.mapbox.mapboxsdk.location.MapboxAnimator.ANIMATOR_LAYER_GPS_BEARING; import static com.mapbox.mapboxsdk.location.MapboxAnimator.ANIMATOR_LAYER_LATLNG; +import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -159,6 +160,24 @@ public void onInitialization_accuracyLayerIsAdded() { verify(style).addLayerBelow(accuracyLayer, BACKGROUND_LAYER); } + @Test + public void onInitialization_numberOfCachedLayerIdsIsConstant() { + OnRenderModeChangedListener internalRenderModeChangedListener = mock(OnRenderModeChangedListener.class); + LayerSourceProvider sourceProvider = buildLayerProvider(); + GeoJsonSource locationSource = mock(GeoJsonSource.class); + when(sourceProvider.generateSource(any(Feature.class))).thenReturn(locationSource); + LayerBitmapProvider bitmapProvider = mock(LayerBitmapProvider.class); + LocationComponentOptions options = mock(LocationComponentOptions.class); + + LocationLayerController controller = + new LocationLayerController(mapboxMap, mapboxMap.getStyle(), sourceProvider, buildFeatureProvider(options), + bitmapProvider, options, internalRenderModeChangedListener); + + controller.initializeComponents(mapboxMap.getStyle(), options); + + assertEquals(5, controller.layerSet.size()); + } + @Test public void applyStyle_styleShadowWithValidElevation() { OnRenderModeChangedListener internalRenderModeChangedListener = mock(OnRenderModeChangedListener.class); @@ -300,22 +319,6 @@ public void applyStyle_layerBelowChanged() { LayerSourceProvider sourceProvider = buildLayerProvider(); when(sourceProvider.generateSource(any(Feature.class))).thenReturn(mock(GeoJsonSource.class)); - Layer bearingLayer = mock(Layer.class); - when(bearingLayer.getId()).thenReturn(BEARING_LAYER); - when(sourceProvider.generateLayer(BEARING_LAYER)).thenReturn(bearingLayer); - Layer foregroundLayer = mock(Layer.class); - when(foregroundLayer.getId()).thenReturn(FOREGROUND_LAYER); - when(sourceProvider.generateLayer(FOREGROUND_LAYER)).thenReturn(foregroundLayer); - Layer backgroundLayer = mock(Layer.class); - when(backgroundLayer.getId()).thenReturn(BACKGROUND_LAYER); - when(sourceProvider.generateLayer(BACKGROUND_LAYER)).thenReturn(backgroundLayer); - Layer shadowLayer = mock(Layer.class); - when(shadowLayer.getId()).thenReturn(SHADOW_LAYER); - when(sourceProvider.generateLayer(SHADOW_LAYER)).thenReturn(shadowLayer); - Layer accuracyLayer = mock(Layer.class); - when(accuracyLayer.getId()).thenReturn(ACCURACY_LAYER); - when(sourceProvider.generateAccuracyLayer()).thenReturn(accuracyLayer); - LocationComponentOptions options = mock(LocationComponentOptions.class); LayerBitmapProvider bitmapProvider = mock(LayerBitmapProvider.class); Bitmap bitmap = mock(Bitmap.class); @@ -606,18 +609,23 @@ private LayerSourceProvider buildLayerProvider() { LayerSourceProvider layerSourceProvider = mock(LayerSourceProvider.class); Layer shadowLayer = mock(Layer.class); + when(shadowLayer.getId()).thenReturn(SHADOW_LAYER); when(layerSourceProvider.generateLayer(SHADOW_LAYER)).thenReturn(shadowLayer); Layer backgroundLayer = mock(Layer.class); + when(backgroundLayer.getId()).thenReturn(BACKGROUND_LAYER); when(layerSourceProvider.generateLayer(BACKGROUND_LAYER)).thenReturn(backgroundLayer); Layer foregroundLayer = mock(Layer.class); + when(foregroundLayer.getId()).thenReturn(FOREGROUND_LAYER); when(layerSourceProvider.generateLayer(FOREGROUND_LAYER)).thenReturn(foregroundLayer); Layer bearingLayer = mock(Layer.class); + when(bearingLayer.getId()).thenReturn(BEARING_LAYER); when(layerSourceProvider.generateLayer(BEARING_LAYER)).thenReturn(bearingLayer); Layer accuracyLayer = mock(Layer.class); + when(accuracyLayer.getId()).thenReturn(ACCURACY_LAYER); when(layerSourceProvider.generateAccuracyLayer()).thenReturn(accuracyLayer); return layerSourceProvider; }