diff options
author | Łukasz Paczos <lukas.paczos@gmail.com> | 2019-04-08 16:01:58 +0200 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2019-04-09 10:52:24 +0200 |
commit | f22c014b91b0b1f0774247ae38ec31dc5ecf0f9d (patch) | |
tree | 8c1d275ed9331db3d7aaf521ca0f6d1e7df5a618 | |
parent | 104091cd9302c1377b9e4ffb56fc47fac36a2024 (diff) | |
download | qtlocation-mapboxgl-f22c014b91b0b1f0774247ae38ec31dc5ecf0f9d.tar.gz |
[android] check for Mapbox#INSTANCE when initializing the MapView
4 files changed, 54 insertions, 2 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java index b761c85e1c..7879404593 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/Mapbox.java @@ -180,4 +180,11 @@ public final class Mapbox { accessToken = accessToken.trim().toLowerCase(MapboxConstants.MAPBOX_LOCALE); return accessToken.length() != 0 && (accessToken.startsWith("pk.") || accessToken.startsWith("sk.")); } + + /** + * Internal use. Check if the {@link Mapbox#INSTANCE} is present. + */ + public static boolean hasInstance() { + return INSTANCE != null; + } }
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 23ce2bdef0..5a410c247e 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -26,6 +26,7 @@ import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.constants.MapboxConstants; +import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException; import com.mapbox.mapboxsdk.location.LocationComponent; import com.mapbox.mapboxsdk.maps.renderer.MapRenderer; import com.mapbox.mapboxsdk.maps.renderer.glsurfaceview.GLSurfaceViewMapRenderer; @@ -120,6 +121,10 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { return; } + if (!Mapbox.hasInstance()) { + throw new MapboxConfigurationException(); + } + // hide surface until map is fully loaded #10990 setForeground(new ColorDrawable(options.getForegroundLoadColor())); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java index ce1f1f7d27..e1f929c0f1 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java @@ -3,8 +3,9 @@ package com.mapbox.mapboxsdk.maps.renderer; import android.content.Context; import android.support.annotation.CallSuper; import android.support.annotation.Keep; - import android.support.annotation.NonNull; + +import com.mapbox.mapboxsdk.LibraryLoader; import com.mapbox.mapboxsdk.log.Logger; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.storage.FileSource; @@ -22,6 +23,10 @@ import javax.microedition.khronos.opengles.GL10; @Keep public abstract class MapRenderer implements MapRendererScheduler { + static { + LibraryLoader.load(); + } + private static final String TAG = "Mbgl-MapRenderer"; // Holds the pointer to the native peer after initialisation diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java index 127967d580..a4b5736729 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java @@ -1,9 +1,19 @@ package com.mapbox.mapboxsdk; import android.content.Context; +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.util.DisplayMetrics; + +import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException; +import com.mapbox.mapboxsdk.maps.MapView; + import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static junit.framework.TestCase.assertNotNull; import static junit.framework.TestCase.assertSame; @@ -11,6 +21,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -19,6 +32,9 @@ public class MapboxTest { private Context context; private Context appContext; + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Before public void before() { context = mock(Context.class); @@ -66,9 +82,28 @@ public class MapboxTest { assertFalse(Mapbox.isAccessTokenValid("blabla")); } + @Test + public void testNoInstance() { + DisplayMetrics displayMetrics = mock(DisplayMetrics.class); + Resources resources = mock(Resources.class); + when(resources.getDisplayMetrics()).thenReturn(displayMetrics); + when(context.getResources()).thenReturn(resources); + TypedArray typedArray = mock(TypedArray.class); + when(context.obtainStyledAttributes(nullable(AttributeSet.class), any(int[].class), anyInt(), anyInt())) + .thenReturn(typedArray); + + expectedException.expect(MapboxConfigurationException.class); + expectedException.expectMessage( + "\nUsing MapView requires calling Mapbox.getInstance(Context context, String accessToken) before " + + "inflating or creating the view. The access token parameter is required when using a Mapbox service." + + "\nPlease see https://www.mapbox.com/help/create-api-access-token/ to learn how to create one." + + "\nMore information in this guide https://www.mapbox.com/help/first-steps-android-sdk/#access-tokens." + ); + new MapView(context); + } + @After public void after() { MapboxInjector.clear(); } - } |