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

Cache location layer IDs in a set instead of a list #14141

Merged
merged 1 commit into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -70,7 +70,8 @@ final class LocationLayerController {
private LocationComponentOptions options;
private final OnRenderModeChangedListener internalRenderModeChangedListener;

private final List<String> layerMap = new ArrayList<>();
@VisibleForTesting
final Set<String> layerSet = new HashSet<>();
private Feature locationFeature;
private GeoJsonSource locationSource;

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -193,7 +194,7 @@ void show() {

void hide() {
isHidden = true;
for (String layerId : layerMap) {
for (String layerId : layerSet) {
setLayerVisibility(layerId, false);
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down