BoundaryLayer extends KujakuLayer { @Override public boolean removeLayerOnMap(@NonNull MapboxMap mapboxMap) { setRemoved(true); Style style = mapboxMap.getStyle(); if (style != null && style.isFullyLoaded()) { removeLayers(style) ; removeSources(style); return true; } else { Log.e(TAG, "Could not remove the layers & source because the the style is null or not fully loaded"); return false; } } BoundaryLayer(@NonNull KujakuLayer.Builder builder); void updateLineLayerProperties(@NonNull PropertyValue<?>... properties); @Override void addLayerToMap(@NonNull MapboxMap mapboxMap); @Override void enableLayerOnMap(@NonNull MapboxMap mapboxMap); @Override void disableLayerOnMap(@NonNull MapboxMap mapboxMap); @Override @NonNull String[] getLayerIds(); @Override boolean removeLayerOnMap(@NonNull MapboxMap mapboxMap); @Override void updateFeatures(@NonNull FeatureCollection featureCollection); @Override FeatureCollection getFeatureCollection();  }
@Test public void removeLayerOnMapShouldReturnFalseWhenStyleIsNotReady() { float textSize = 20f; float boundaryWidth = 6f; int colorInt = Color.GREEN; String labelProperty = "district-name"; FeatureCollection featureCollection = FeatureCollection.fromFeatures(new ArrayList<Feature>()); BoundaryLayer.Builder builder = new BoundaryLayer.Builder(featureCollection) .setLabelProperty(labelProperty) .setLabelTextSize(textSize) .setLabelColorInt(colorInt) .setBoundaryColor(colorInt) .setBoundaryWidth(boundaryWidth); BoundaryLayer boundaryLayer = builder.build(); MapboxMap mapboxMap = Mockito.mock(MapboxMap.class); Style style = Mockito.mock(Style.class); Mockito.doReturn(false) .when(style) .isFullyLoaded(); Mockito.doReturn(style) .when(mapboxMap) .getStyle(); assertFalse(boundaryLayer.removeLayerOnMap(mapboxMap)); }
@Test public void removeLayerOnMapShouldReturnTrueWhenStyleIsReadyAndRemoveLayersAndSources() { float textSize = 20f; float boundaryWidth = 6f; int colorInt = Color.GREEN; String labelProperty = "district-name"; FeatureCollection featureCollection = FeatureCollection.fromFeatures(new ArrayList<Feature>()); BoundaryLayer.Builder builder = new BoundaryLayer.Builder(featureCollection) .setLabelProperty(labelProperty) .setLabelTextSize(textSize) .setLabelColorInt(colorInt) .setBoundaryColor(colorInt) .setBoundaryWidth(boundaryWidth); BoundaryLayer boundaryLayer = builder.build(); assertNull(ReflectionHelpers.getField(boundaryLayer, "boundarySource")); MapboxMap mapboxMap = Mockito.mock(MapboxMap.class); Style style = Mockito.mock(Style.class); SymbolLayer boundaryLabelLayer = Mockito.mock(SymbolLayer.class); LineLayer boundaryLineLayer = Mockito.mock(LineLayer.class); GeoJsonSource boundarySource = new GeoJsonSource("some-id"); GeoJsonSource boundaryLabelsSource = new GeoJsonSource("some-id-2"); Mockito.doReturn(true) .when(style) .isFullyLoaded(); Mockito.doReturn(style) .when(mapboxMap) .getStyle(); ReflectionHelpers.setField(boundaryLayer, "boundaryLabelLayer", boundaryLabelLayer); ReflectionHelpers.setField(boundaryLayer, "boundaryLineLayer", boundaryLineLayer); ReflectionHelpers.setField(boundaryLayer, "boundarySource", boundarySource); ReflectionHelpers.setField(boundaryLayer, "boundaryLabelsSource", boundaryLabelsSource); assertTrue(boundaryLayer.removeLayerOnMap(mapboxMap)); Mockito.verify(style, Mockito.times(1)) .removeLayer(ArgumentMatchers.eq(boundaryLabelLayer)); Mockito.verify(style, Mockito.times(1)) .removeLayer(ArgumentMatchers.eq(boundaryLineLayer)); Mockito.verify(style, Mockito.times(1)) .removeSource(ArgumentMatchers.eq(boundarySource)); Mockito.verify(style, Mockito.times(1)) .removeSource(ArgumentMatchers.eq(boundaryLabelsSource)); }