Lazy load view components in MapView. (#141)

* Lazy load view components in MapView.

* Fix failing tests.

* Fix style.

* Lazy inflating view components.

* Remove debug logging.

* Fix android ui tests.
This commit is contained in:
Peng Liu
2020-01-29 15:03:42 +02:00
committed by GitHub
parent a5477963ae
commit aea886e741
7 changed files with 281 additions and 146 deletions

View File

@@ -5,20 +5,20 @@ import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.collection.LongSparseArray;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.TextureView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.collection.LongSparseArray;
import com.mapbox.android.gestures.AndroidGesturesManager;
import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.Mapbox;
@@ -73,14 +73,20 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
private View renderView;
private AttributionClickListener attributionClickListener;
private MapboxMapOptions mapboxMapOptions;
MapboxMapOptions mapboxMapOptions;
private MapRenderer mapRenderer;
private boolean destroyed;
@Nullable
private CompassView compassView;
private PointF focalPoint;
private ImageView attrView;
private ImageView logoView;
// callback for focal point invalidation
private final FocalPointInvalidator focalInvalidator = new FocalPointInvalidator();
// callback for registering touch listeners
private final GesturesManagerInteractionListener registerTouchListener = new GesturesManagerInteractionListener();
// callback for camera change events
private final CameraChangeDispatcher cameraDispatcher = new CameraChangeDispatcher();
@Nullable
private MapGestureDetector mapGestureDetector;
@@ -131,14 +137,6 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
mapboxMapOptions = options;
// inflate view
View view = LayoutInflater.from(context).inflate(R.layout.mapbox_mapview_internal, this);
compassView = view.findViewById(R.id.compassView);
attrView = view.findViewById(R.id.attributionView);
attrView.setImageDrawable(BitmapUtils.getDrawableFromRes(getContext(), R.drawable.mapbox_info_bg_selector));
logoView = view.findViewById(R.id.logoView);
logoView.setImageDrawable(BitmapUtils.getDrawableFromRes(getContext(), R.drawable.mapbox_logo_icon));
// add accessibility support
setContentDescription(context.getString(R.string.mapbox_mapActionDescription));
setWillNotDraw(false);
@@ -149,18 +147,11 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
Context context = getContext();
// callback for focal point invalidation
final FocalPointInvalidator focalInvalidator = new FocalPointInvalidator();
focalInvalidator.addListener(createFocalPointChangeListener());
// callback for registering touch listeners
GesturesManagerInteractionListener registerTouchListener = new GesturesManagerInteractionListener();
// callback for camera change events
final CameraChangeDispatcher cameraDispatcher = new CameraChangeDispatcher();
// setup components for MapboxMap creation
Projection proj = new Projection(nativeMapView, this);
UiSettings uiSettings = new UiSettings(proj, focalInvalidator, compassView, attrView, logoView, getPixelRatio());
UiSettings uiSettings = new UiSettings(proj, focalInvalidator, getPixelRatio(), this);
LongSparseArray<Annotation> annotationsArray = new LongSparseArray<>();
IconManager iconManager = new IconManager(nativeMapView);
Annotations annotations = new AnnotationContainer(nativeMapView, annotationsArray);
@@ -169,30 +160,23 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
Polylines polylines = new PolylineContainer(nativeMapView, annotationsArray);
ShapeAnnotations shapeAnnotations = new ShapeAnnotationContainer(nativeMapView, annotationsArray);
AnnotationManager annotationManager = new AnnotationManager(this, annotationsArray, iconManager,
annotations, markers, polygons, polylines, shapeAnnotations);
annotations, markers, polygons, polylines, shapeAnnotations);
Transform transform = new Transform(this, nativeMapView, cameraDispatcher);
// MapboxMap
List<MapboxMap.OnDeveloperAnimationListener> developerAnimationListeners = new ArrayList<>();
mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, proj, registerTouchListener, cameraDispatcher,
developerAnimationListeners);
developerAnimationListeners);
mapboxMap.injectAnnotationManager(annotationManager);
// user input
mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings,
annotationManager, cameraDispatcher);
annotationManager, cameraDispatcher);
mapKeyListener = new MapKeyListener(transform, uiSettings, mapGestureDetector);
// compass
compassView.injectCompassAnimationListener(createCompassAnimationListener(cameraDispatcher));
compassView.setOnClickListener(createCompassClickListener(cameraDispatcher));
// LocationComponent
mapboxMap.injectLocationComponent(new LocationComponent(mapboxMap, transform, developerAnimationListeners));
// inject widgets with MapboxMap
attrView.setOnClickListener(attributionClickListener = new AttributionClickListener(context, mapboxMap));
// Ensure this view is interactable
setClickable(true);
setLongClickable(true);
@@ -213,6 +197,44 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
mapCallback.initialised();
}
protected CompassView initialiseCompassView() {
compassView = new CompassView(this.getContext());
addView(compassView);
compassView.setTag("compassView");
compassView.getLayoutParams().width = LayoutParams.WRAP_CONTENT;
compassView.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
compassView.setContentDescription(getResources().getString(R.string.mapbox_compassContentDescription));
compassView.injectCompassAnimationListener(createCompassAnimationListener(cameraDispatcher));
compassView.setOnClickListener(createCompassClickListener(cameraDispatcher));
return compassView;
}
protected ImageView initialiseAttributionView() {
ImageView attrView = new ImageView(this.getContext());
addView(attrView);
attrView.setTag("attrView");
attrView.getLayoutParams().width = LayoutParams.WRAP_CONTENT;
attrView.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
attrView.setAdjustViewBounds(true);
attrView.setClickable(true);
attrView.setFocusable(true);
attrView.setContentDescription(getResources().getString(R.string.mapbox_attributionsIconContentDescription));
attrView.setImageDrawable(BitmapUtils.getDrawableFromRes(getContext(), R.drawable.mapbox_info_bg_selector));
// inject widgets with MapboxMap
attrView.setOnClickListener(attributionClickListener = new AttributionClickListener(getContext(), mapboxMap));
return attrView;
}
protected ImageView initialiseLogoView() {
ImageView logoView = new ImageView(this.getContext());
addView(logoView);
logoView.setTag("logoView");
logoView.getLayoutParams().width = LayoutParams.WRAP_CONTENT;
logoView.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
logoView.setImageDrawable(BitmapUtils.getDrawableFromRes(getContext(), R.drawable.mapbox_logo_icon));
return logoView;
}
private FocalPointChangeListener createFocalPointChangeListener() {
return new FocalPointChangeListener() {
@Override
@@ -223,7 +245,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
}
private MapboxMap.OnCompassAnimationListener createCompassAnimationListener(@NonNull final CameraChangeDispatcher
cameraChangeDispatcher) {
cameraChangeDispatcher) {
return new MapboxMap.OnCompassAnimationListener() {
@Override
public void onCompassAnimation() {
@@ -232,7 +254,9 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
@Override
public void onCompassAnimationFinished() {
compassView.isAnimating(false);
if (compassView != null) {
compassView.isAnimating(false);
}
cameraChangeDispatcher.onCameraIdle();
}
};
@@ -289,7 +313,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
TextureView textureView = new TextureView(getContext());
boolean translucentSurface = options.getTranslucentTextureSurface();
mapRenderer = new TextureViewMapRenderer(getContext(),
textureView, localFontFamily, translucentSurface) {
textureView, localFontFamily, translucentSurface) {
@Override
protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.onSurfaceCreated();
@@ -316,7 +340,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
boolean crossSourceCollisions = mapboxMapOptions.getCrossSourceCollisions();
nativeMapView = new NativeMapView(
getContext(), getPixelRatio(), crossSourceCollisions, this, mapChangeReceiver, mapRenderer
getContext(), getPixelRatio(), crossSourceCollisions, this, mapChangeReceiver, mapRenderer
);
}
@@ -1225,7 +1249,7 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
public void setGesturesManager(AndroidGesturesManager gesturesManager, boolean attachDefaultListeners,
boolean setDefaultMutuallyExclusives) {
mapGestureDetector.setGesturesManager(
getContext(), gesturesManager, attachDefaultListeners, setDefaultMutuallyExclusives);
getContext(), gesturesManager, attachDefaultListeners, setDefaultMutuallyExclusives);
}
@Override
@@ -1235,8 +1259,8 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
}
private class MapCallback implements OnDidFinishLoadingStyleListener,
OnDidFinishRenderingFrameListener, OnDidFinishLoadingMapListener,
OnCameraIsChangingListener, OnCameraDidChangeListener, OnDidFailLoadingMapListener {
OnDidFinishRenderingFrameListener, OnDidFinishLoadingMapListener,
OnCameraIsChangingListener, OnCameraDidChangeListener, OnDidFailLoadingMapListener {
private final List<OnMapReadyCallback> onMapReadyCallbackList = new ArrayList<>();

View File

@@ -7,17 +7,19 @@ import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import androidx.annotation.ColorInt;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.UiThread;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.camera.CameraPosition;
@@ -34,18 +36,23 @@ public final class UiSettings {
@NonNull
private final FocalPointChangeListener focalPointChangeListener;
@NonNull
private final Projection projection;
private final MapView mapView;
@NonNull
private final CompassView compassView;
private final Projection projection;
@VisibleForTesting
@Nullable
CompassView compassView;
private final int[] compassMargins = new int[4];
@NonNull
private final ImageView attributionsView;
@VisibleForTesting
@Nullable
ImageView attributionsView;
private final int[] attributionsMargins = new int[4];
private AttributionDialogManager attributionDialogManager;
@NonNull
private final View logoView;
@VisibleForTesting
@Nullable
ImageView logoView;
private final int[] logoMargins = new int[4];
private final float pixelRatio;
@@ -79,23 +86,34 @@ public final class UiSettings {
@Nullable
private PointF userProvidedFocalPoint;
@VisibleForTesting
boolean isCompassInitialized = false;
@VisibleForTesting
boolean isAttributionInitialized = false;
@VisibleForTesting
boolean isLogoInitialized = false;
private double clockwiseBearing;
UiSettings(@NonNull Projection projection, @NonNull FocalPointChangeListener listener,
@NonNull CompassView compassView, @NonNull ImageView attributionsView, @NonNull View logoView,
float pixelRatio) {
float pixelRatio, MapView mapView) {
this.projection = projection;
this.focalPointChangeListener = listener;
this.compassView = compassView;
this.attributionsView = attributionsView;
this.logoView = logoView;
this.pixelRatio = pixelRatio;
this.mapView = mapView;
}
void initialise(@NonNull Context context, @NonNull MapboxMapOptions options) {
Resources resources = context.getResources();
initialiseGestures(options);
initialiseCompass(options, resources);
initialiseLogo(options, resources);
initialiseAttribution(context, options);
if (options.getCompassEnabled()) {
initialiseCompass(options, resources);
}
if (options.getLogoEnabled()) {
initialiseLogo(options, resources);
}
if (options.getAttributionEnabled()) {
initialiseAttribution(context, options);
}
}
void onSaveInstanceState(@NonNull Bundle outState) {
@@ -163,6 +181,8 @@ public final class UiSettings {
}
private void initialiseCompass(MapboxMapOptions options, @NonNull Resources resources) {
isCompassInitialized = true;
compassView = mapView.initialiseCompassView();
setCompassEnabled(options.getCompassEnabled());
setCompassGravity(options.getCompassGravity());
int[] compassMargins = options.getCompassMargins();
@@ -192,6 +212,11 @@ public final class UiSettings {
}
private void restoreCompass(Bundle savedInstanceState) {
boolean compassEnabled = savedInstanceState.getBoolean(MapboxConstants.STATE_COMPASS_ENABLED);
if (compassEnabled && !isCompassInitialized) {
compassView = mapView.initialiseCompassView();
isCompassInitialized = true;
}
setCompassEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_COMPASS_ENABLED));
setCompassGravity(savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_GRAVITY));
setCompassMargins(savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_LEFT),
@@ -200,10 +225,12 @@ public final class UiSettings {
savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM));
setCompassFadeFacingNorth(savedInstanceState.getBoolean(MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH));
setCompassImage(BitmapUtils.getDrawableFromByteArray(
compassView.getContext(), savedInstanceState.getByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)));
mapView.getContext(), savedInstanceState.getByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)));
}
private void initialiseLogo(MapboxMapOptions options, @NonNull Resources resources) {
isLogoInitialized = true;
logoView = mapView.initialiseLogoView();
setLogoEnabled(options.getLogoEnabled());
setLogoGravity(options.getLogoGravity());
setLogoMargins(resources, options.getLogoMargins());
@@ -229,6 +256,11 @@ public final class UiSettings {
}
private void restoreLogo(Bundle savedInstanceState) {
boolean logoEnabled = savedInstanceState.getBoolean(MapboxConstants.STATE_LOGO_ENABLED);
if (logoEnabled && !isLogoInitialized) {
logoView = mapView.initialiseLogoView();
isLogoInitialized = true;
}
setLogoEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_LOGO_ENABLED));
setLogoGravity(savedInstanceState.getInt(MapboxConstants.STATE_LOGO_GRAVITY));
setLogoMargins(savedInstanceState.getInt(MapboxConstants.STATE_LOGO_MARGIN_LEFT),
@@ -238,6 +270,8 @@ public final class UiSettings {
}
private void initialiseAttribution(@NonNull Context context, MapboxMapOptions options) {
isAttributionInitialized = true;
attributionsView = mapView.initialiseAttributionView();
setAttributionEnabled(options.getAttributionEnabled());
setAttributionGravity(options.getAttributionGravity());
setAttributionMargins(context, options.getAttributionMargins());
@@ -269,6 +303,11 @@ public final class UiSettings {
}
private void restoreAttribution(Bundle savedInstanceState) {
boolean attributionEnabled = savedInstanceState.getBoolean(MapboxConstants.STATE_ATTRIBUTION_ENABLED);
if (attributionEnabled && !isAttributionInitialized) {
attributionsView = mapView.initialiseAttributionView();
isAttributionInitialized = true;
}
setAttributionEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_ATTRIBUTION_ENABLED));
setAttributionGravity(savedInstanceState.getInt(MapboxConstants.STATE_ATTRIBUTION_GRAVITY));
setAttributionMargins(savedInstanceState.getInt(MapboxConstants.STATE_ATTRIBUTION_MARGIN_LEFT),
@@ -289,7 +328,13 @@ public final class UiSettings {
* @param compassEnabled True to enable the compass; false to disable the compass.
*/
public void setCompassEnabled(boolean compassEnabled) {
compassView.setEnabled(compassEnabled);
if (compassEnabled && !isCompassInitialized) {
initialiseCompass(mapView.mapboxMapOptions, mapView.getContext().getResources());
}
if (compassView != null) {
compassView.setEnabled(compassEnabled);
compassView.update(clockwiseBearing);
}
}
/**
@@ -298,7 +343,11 @@ public final class UiSettings {
* @return True if the compass is enabled; false if the compass is disabled.
*/
public boolean isCompassEnabled() {
return compassView.isEnabled();
if (compassView != null) {
return compassView.isEnabled();
} else {
return false;
}
}
/**
@@ -312,7 +361,9 @@ public final class UiSettings {
*/
@UiThread
public void setCompassGravity(int gravity) {
setWidgetGravity(compassView, gravity);
if (compassView != null) {
setWidgetGravity(compassView, gravity);
}
}
/**
@@ -324,7 +375,9 @@ public final class UiSettings {
* @param compassFadeFacingNorth True to enable the fading animation; false to disable it
*/
public void setCompassFadeFacingNorth(boolean compassFadeFacingNorth) {
compassView.fadeCompassViewFacingNorth(compassFadeFacingNorth);
if (compassView != null) {
compassView.fadeCompassViewFacingNorth(compassFadeFacingNorth);
}
}
/**
@@ -336,7 +389,9 @@ public final class UiSettings {
* @param compass the drawable to show as image compass
*/
public void setCompassImage(@NonNull Drawable compass) {
compassView.setCompassImage(compass);
if (compassView != null) {
compassView.setCompassImage(compass);
}
}
/**
@@ -345,7 +400,11 @@ public final class UiSettings {
* @return True if the compass will fade, false if it remains visible
*/
public boolean isCompassFadeWhenFacingNorth() {
return compassView.isFadeCompassViewFacingNorth();
if (compassView != null) {
return compassView.isFadeCompassViewFacingNorth();
} else {
return false;
}
}
/**
@@ -354,7 +413,11 @@ public final class UiSettings {
* @return The gravity
*/
public int getCompassGravity() {
return ((FrameLayout.LayoutParams) compassView.getLayoutParams()).gravity;
if (compassView != null) {
return ((FrameLayout.LayoutParams) compassView.getLayoutParams()).gravity;
} else {
return -1;
}
}
/**
@@ -368,7 +431,9 @@ public final class UiSettings {
*/
@UiThread
public void setCompassMargins(@Px int left, @Px int top, @Px int right, @Px int bottom) {
setWidgetMargins(compassView, compassMargins, left, top, right, bottom);
if (compassView != null) {
setWidgetMargins(compassView, compassMargins, left, top, right, bottom);
}
}
/**
@@ -416,14 +481,20 @@ public final class UiSettings {
*
* @return the drawable used as compass image
*/
@NonNull
@Nullable
public Drawable getCompassImage() {
return compassView.getCompassImage();
if (compassView != null) {
return compassView.getCompassImage();
} else {
return null;
}
}
void update(@NonNull CameraPosition cameraPosition) {
double clockwiseBearing = -cameraPosition.bearing;
compassView.update(clockwiseBearing);
clockwiseBearing = -cameraPosition.bearing;
if (compassView != null) {
compassView.update(clockwiseBearing);
}
}
/**
@@ -435,7 +506,12 @@ public final class UiSettings {
* @param enabled True to enable the logo; false to disable the logo.
*/
public void setLogoEnabled(boolean enabled) {
logoView.setVisibility(enabled ? View.VISIBLE : View.GONE);
if (enabled && !isLogoInitialized) {
initialiseLogo(mapView.mapboxMapOptions, mapView.getContext().getResources());
}
if (logoView != null) {
logoView.setVisibility(enabled ? View.VISIBLE : View.GONE);
}
}
/**
@@ -444,7 +520,11 @@ public final class UiSettings {
* @return True if the logo is enabled; false if the logo is disabled.
*/
public boolean isLogoEnabled() {
return logoView.getVisibility() == View.VISIBLE;
if (logoView != null) {
return logoView.getVisibility() == View.VISIBLE;
} else {
return false;
}
}
/**
@@ -457,7 +537,9 @@ public final class UiSettings {
* @param gravity Android SDK Gravity.
*/
public void setLogoGravity(int gravity) {
setWidgetGravity(logoView, gravity);
if (logoView != null) {
setWidgetGravity(logoView, gravity);
}
}
/**
@@ -466,7 +548,11 @@ public final class UiSettings {
* @return The gravity
*/
public int getLogoGravity() {
return ((FrameLayout.LayoutParams) logoView.getLayoutParams()).gravity;
if (logoView != null) {
return ((FrameLayout.LayoutParams) logoView.getLayoutParams()).gravity;
} else {
return -1;
}
}
/**
@@ -479,7 +565,9 @@ public final class UiSettings {
* @param bottom The bottom margin in pixels.
*/
public void setLogoMargins(@Px int left, @Px int top, @Px int right, @Px int bottom) {
setWidgetMargins(logoView, logoMargins, left, top, right, bottom);
if (logoView != null) {
setWidgetMargins(logoView, logoMargins, left, top, right, bottom);
}
}
/**
@@ -531,7 +619,12 @@ public final class UiSettings {
* @param enabled True to enable the attribution; false to disable the attribution.
*/
public void setAttributionEnabled(boolean enabled) {
attributionsView.setVisibility(enabled ? View.VISIBLE : View.GONE);
if (enabled && !isAttributionInitialized) {
initialiseAttribution(mapView.getContext(), mapView.mapboxMapOptions);
}
if (attributionsView != null) {
attributionsView.setVisibility(enabled ? View.VISIBLE : View.GONE);
}
}
/**
@@ -540,7 +633,11 @@ public final class UiSettings {
* @return True if the attribution is enabled; false if the attribution is disabled.
*/
public boolean isAttributionEnabled() {
return attributionsView.getVisibility() == View.VISIBLE;
if (attributionsView != null) {
return attributionsView.getVisibility() == View.VISIBLE;
} else {
return false;
}
}
@@ -575,7 +672,9 @@ public final class UiSettings {
* @param gravity Android SDK Gravity.
*/
public void setAttributionGravity(int gravity) {
setWidgetGravity(attributionsView, gravity);
if (attributionsView != null) {
setWidgetGravity(attributionsView, gravity);
}
}
/**
@@ -584,7 +683,11 @@ public final class UiSettings {
* @return The gravity
*/
public int getAttributionGravity() {
return ((FrameLayout.LayoutParams) attributionsView.getLayoutParams()).gravity;
if (attributionsView != null) {
return ((FrameLayout.LayoutParams) attributionsView.getLayoutParams()).gravity;
} else {
return -1;
}
}
/**
@@ -596,7 +699,9 @@ public final class UiSettings {
* @param bottom The bottom margin in pixels.
*/
public void setAttributionMargins(@Px int left, @Px int top, @Px int right, @Px int bottom) {
setWidgetMargins(attributionsView, attributionsMargins, left, top, right, bottom);
if (attributionsView != null) {
setWidgetMargins(attributionsView, attributionsMargins, left, top, right, bottom);
}
}
/**
@@ -608,6 +713,9 @@ public final class UiSettings {
*/
public void setAttributionTintColor(@ColorInt int tintColor) {
// Check that the tint color being passed in isn't transparent.
if (attributionsView == null) {
return;
}
if (Color.alpha(tintColor) == 0) {
ColorUtils.setTintList(attributionsView,
ContextCompat.getColor(attributionsView.getContext(), R.color.mapbox_blue));

View File

@@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<com.mapbox.mapboxsdk.maps.widgets.CompassView
android:id="@+id/compassView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/mapbox_compassContentDescription"/>
<ImageView
android:visibility="gone"
android:id="@+id/logoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null"/>
<ImageView
android:visibility="gone"
android:id="@+id/attributionView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:clickable="true"
android:focusable="true"
android:contentDescription="@string/mapbox_attributionsIconContentDescription"/>
</merge>

View File

@@ -1,5 +1,7 @@
package com.mapbox.mapboxsdk.maps;
import android.content.Context;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
@@ -21,6 +23,14 @@ import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
public class UiSettingsTest {
@InjectMocks
MapView mapview = mock(MapView.class);
@InjectMocks
Context context = mock(Context.class);
@InjectMocks
Resources resources = mock(Resources.class);
@InjectMocks
Projection projection = mock(Projection.class);
@@ -32,7 +42,7 @@ public class UiSettingsTest {
CompassView compassView = mock(CompassView.class);
@InjectMocks
ImageView imageView = mock(ImageView.class);
ImageView attributionView = mock(ImageView.class);
@InjectMocks
ImageView logoView = mock(ImageView.class);
@@ -41,10 +51,18 @@ public class UiSettingsTest {
FrameLayout.LayoutParams layoutParams = mock(FrameLayout.LayoutParams.class);
private UiSettings uiSettings;
private MapboxMapOptions mapboxMapOptions;
@Before
public void beforeTest() {
uiSettings = new UiSettings(projection, focalPointChangeListener, compassView, imageView, logoView, 1);
uiSettings = new UiSettings(projection, focalPointChangeListener, 1, mapview);
uiSettings.compassView = this.compassView;
uiSettings.attributionsView = this.attributionView;
uiSettings.logoView = this.logoView;
when(compassView.getContext()).thenReturn(context);
when(attributionView.getContext()).thenReturn(context);
when(logoView.getContext()).thenReturn(context);
when(context.getResources()).thenReturn(resources);
}
@Test
@@ -53,12 +71,18 @@ public class UiSettingsTest {
}
@Test
public void testCompassEnabled() {
public void testGetCompassEnabled() {
when(compassView.isEnabled()).thenReturn(true);
uiSettings.setCompassEnabled(true);
assertEquals("Compass should be enabled", true, uiSettings.isCompassEnabled());
}
@Test
public void testSetCompassEnabled() {
uiSettings.isCompassInitialized = true;
uiSettings.setCompassEnabled(true);
verify(compassView).setEnabled(true);
}
@Test
public void testCompassDisabled() {
uiSettings.setCompassEnabled(false);
@@ -75,7 +99,7 @@ public class UiSettingsTest {
@Test
public void testCompassMargins() {
when(projection.getContentPadding()).thenReturn(new int[] {0, 0, 0, 0});
when(projection.getContentPadding()).thenReturn(new int[]{0, 0, 0, 0});
when(compassView.getLayoutParams()).thenReturn(layoutParams);
layoutParams.leftMargin = 1;
layoutParams.topMargin = 2;
@@ -99,6 +123,7 @@ public class UiSettingsTest {
@Test
public void testLogoEnabled() {
uiSettings.isLogoInitialized = true;
uiSettings.setLogoEnabled(true);
assertEquals("Logo should be enabled", true, uiSettings.isLogoEnabled());
}
@@ -120,7 +145,7 @@ public class UiSettingsTest {
@Test
public void testLogoMargins() {
when(projection.getContentPadding()).thenReturn(new int[] {0, 0, 0, 0});
when(projection.getContentPadding()).thenReturn(new int[]{0, 0, 0, 0});
when(logoView.getLayoutParams()).thenReturn(layoutParams);
layoutParams.leftMargin = 1;
layoutParams.topMargin = 2;
@@ -135,21 +160,22 @@ public class UiSettingsTest {
@Test
public void testAttributionEnabled() {
when(imageView.getVisibility()).thenReturn(View.VISIBLE);
uiSettings.isAttributionInitialized = true;
when(attributionView.getVisibility()).thenReturn(View.VISIBLE);
uiSettings.setAttributionEnabled(true);
assertEquals("Attribution should be enabled", true, uiSettings.isAttributionEnabled());
}
@Test
public void testAttributionDisabled() {
when(imageView.getVisibility()).thenReturn(View.GONE);
when(attributionView.getVisibility()).thenReturn(View.GONE);
uiSettings.setAttributionEnabled(false);
assertEquals("Attribution should be disabled", false, uiSettings.isAttributionEnabled());
}
@Test
public void testAttributionGravity() {
when(imageView.getLayoutParams()).thenReturn(layoutParams);
when(attributionView.getLayoutParams()).thenReturn(layoutParams);
layoutParams.gravity = Gravity.END;
uiSettings.setAttributionGravity(Gravity.END);
assertEquals("Attribution gravity should be same", Gravity.END, uiSettings.getAttributionGravity());
@@ -157,8 +183,8 @@ public class UiSettingsTest {
@Test
public void testAttributionMargins() {
when(imageView.getLayoutParams()).thenReturn(layoutParams);
when(projection.getContentPadding()).thenReturn(new int[] {0, 0, 0, 0});
when(attributionView.getLayoutParams()).thenReturn(layoutParams);
when(projection.getContentPadding()).thenReturn(new int[]{0, 0, 0, 0});
layoutParams.leftMargin = 1;
layoutParams.topMargin = 2;
layoutParams.rightMargin = 3;
@@ -359,14 +385,14 @@ public class UiSettingsTest {
public void testDisableRotateWhenScalingEnabled() {
uiSettings.setDisableRotateWhenScaling(true);
assertEquals("Rotate disabling should be enabled", true,
uiSettings.isDisableRotateWhenScaling());
uiSettings.isDisableRotateWhenScaling());
}
@Test
public void testDisableRotateWhenScalingDisabled() {
uiSettings.setDisableRotateWhenScaling(false);
assertEquals("Rotate disabling should be disabled", false,
uiSettings.isDisableRotateWhenScaling());
uiSettings.isDisableRotateWhenScaling());
}
@Test
@@ -379,7 +405,7 @@ public class UiSettingsTest {
public void testIncreaseScaleThresholdWhenRotatingDisabled() {
uiSettings.setIncreaseScaleThresholdWhenRotating(false);
assertEquals("Scale threshold increase should be disabled", false,
uiSettings.isIncreaseScaleThresholdWhenRotating());
uiSettings.isIncreaseScaleThresholdWhenRotating());
}
@Test
@@ -404,7 +430,7 @@ public class UiSettingsTest {
public void testAreAllGesturesEnabled() {
uiSettings.setAllGesturesEnabled(true);
assertEquals("All gestures check should return true", true,
uiSettings.areAllGesturesEnabled());
uiSettings.areAllGesturesEnabled());
}
@Test
@@ -412,20 +438,20 @@ public class UiSettingsTest {
uiSettings.setAllGesturesEnabled(true);
uiSettings.setScrollGesturesEnabled(false);
assertEquals("All gestures check should return false", false,
uiSettings.areAllGesturesEnabled());
uiSettings.areAllGesturesEnabled());
}
@Test
public void testZoomRateDefaultValue() {
assertEquals("Default zoom rate should be 1.0f", 1.0f,
uiSettings.getZoomRate(), 0);
uiSettings.getZoomRate(), 0);
}
@Test
public void testZoomRate() {
uiSettings.setZoomRate(0.83f);
assertEquals("Zoom rate should be 0.83f", 0.83f,
uiSettings.getZoomRate(), 0);
uiSettings.getZoomRate(), 0);
}
@Test

View File

@@ -35,9 +35,11 @@ import static androidx.test.espresso.intent.matcher.IntentMatchers.hasData;
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withTagValue;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.anything;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assume.assumeTrue;
@@ -56,10 +58,10 @@ public class AttributionTest extends EspressoTest {
validateTestSetup();
// Default
onView(withId(R.id.attributionView)).check(matches(isDisplayed()));
onView(withTagValue(is("attrView"))).check(matches(isDisplayed()));
// Disabled
onView(withId(R.id.attributionView))
onView(withTagValue(is("attrView")))
.perform(new DisableAction(mapboxMap))
.check(matches(not(isDisplayed())));
}
@@ -73,7 +75,7 @@ public class AttributionTest extends EspressoTest {
}
// click on View to open dialog
onView(withId(R.id.attributionView)).perform(click());
onView(withTagValue(is("attrView"))).perform(click());
onView(withText(R.string.mapbox_attributionsDialogTitle)).check(matches(isDisplayed()));
// test for trigger url intent
@@ -94,7 +96,7 @@ public class AttributionTest extends EspressoTest {
}
// click on View to open dialog
onView(withId(R.id.attributionView)).perform(click());
onView(withTagValue(is("attrView"))).perform(click());
onView(withText(R.string.mapbox_attributionsDialogTitle)).check(matches(isDisplayed()));
// test for trigger url intent
@@ -115,7 +117,7 @@ public class AttributionTest extends EspressoTest {
}
// click on View to open dialog
onView(withId(R.id.attributionView)).perform(click());
onView(withTagValue(is("attrView"))).perform(click());
onView(withText(R.string.mapbox_attributionsDialogTitle)).check(matches(isDisplayed()));
// test for trigger url intent
@@ -137,7 +139,7 @@ public class AttributionTest extends EspressoTest {
validateTestSetup();
// click on View to open dialog
onView(withId(R.id.attributionView)).perform(click());
onView(withTagValue(is("attrView"))).perform(click());
onView(withText(R.string.mapbox_attributionsDialogTitle)).inRoot(isDialog()).check(matches(isDisplayed()));
// click on item to open second dialog

View File

@@ -3,10 +3,10 @@ package com.mapbox.mapboxsdk.testapp.maps.widgets;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.action.WaitAction;
import com.mapbox.mapboxsdk.testapp.activity.EspressoTest;
import com.mapbox.mapboxsdk.testapp.utils.TestConstants;
import org.junit.Ignore;
import org.junit.Test;
@@ -14,8 +14,9 @@ import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withTagValue;
import static com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
@@ -24,7 +25,7 @@ public class CompassViewTest extends EspressoTest {
@Test
public void testDefault() {
validateTestSetup();
onView(withId(R.id.compassView)).check(matches(not(isDisplayed())));
onView(withTagValue(is("compassView"))).check(matches(not(isDisplayed())));
}
@Test
@@ -41,7 +42,7 @@ public class CompassViewTest extends EspressoTest {
));
uiController.loopMainThreadForAtLeast(500);
});
onView(withId(R.id.compassView)).check(matches(isDisplayed()));
onView(withTagValue(is("compassView"))).check(matches(isDisplayed()));
}
@Test
@@ -55,9 +56,9 @@ public class CompassViewTest extends EspressoTest {
.target(new LatLng())
.build()
)));
onView(withId(R.id.compassView)).perform(click());
onView(withTagValue(is("compassView"))).perform(click());
WaitAction.invoke(500);
onView(withId(R.id.compassView)).check(matches(not(isDisplayed())));
onView(withTagValue(is("compassView"))).check(matches(not(isDisplayed())));
invoke(mapboxMap, (uiController, mapboxMap) -> {
CameraPosition cameraPosition = mapboxMap.getCameraPosition();
assertEquals("Camera bearing should face north, ", 0, cameraPosition.bearing, TestConstants.BEARING_DELTA);

View File

@@ -1,11 +1,11 @@
package com.mapbox.mapboxsdk.testapp.maps.widgets;
import android.view.View;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import android.view.View;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.activity.EspressoTest;
import org.hamcrest.Matcher;
@@ -14,7 +14,8 @@ import org.junit.Test;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withTagValue;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
public class LogoTest extends EspressoTest {
@@ -22,16 +23,16 @@ public class LogoTest extends EspressoTest {
@Test
public void testDefault() {
validateTestSetup();
onView(withId(R.id.logoView)).check(matches(isDisplayed()));
onView(withTagValue(is("logoView"))).check(matches(isDisplayed()));
}
@Test
public void testDisabled() {
validateTestSetup();
onView(withId(R.id.logoView))
.perform(new DisableAction(mapboxMap))
.check(matches(not(isDisplayed())));
onView(withTagValue(is("logoView")))
.perform(new DisableAction(mapboxMap))
.check(matches(not(isDisplayed())));
}
private class DisableAction implements ViewAction {