summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src
diff options
context:
space:
mode:
authorTobrun <tobrun.van.nuland@gmail.com>2017-03-01 16:23:23 -0500
committerGitHub <noreply@github.com>2017-03-01 16:23:23 -0500
commit95fdf3ca0d52acfbee62f677c5fef2e4e180f720 (patch)
tree2f1620dfc6f3a8d99e7c4f18a5d7e6c96c87b299 /platform/android/MapboxGLAndroidSDKTestApp/src
parentcd5efab11e2ebd111428bba1144ff270b6cc51cc (diff)
downloadqtlocation-mapboxgl-95fdf3ca0d52acfbee62f677c5fef2e4e180f720.tar.gz
[android] - add unit test for Mapbox (#8228)
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src')
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java92
1 files changed, 92 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java
new file mode 100644
index 0000000000..0c1f28515d
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/MapboxTest.java
@@ -0,0 +1,92 @@
+package com.mapbox.mapboxsdk;
+
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+
+import com.mapbox.mapboxsdk.exceptions.InvalidAccessTokenException;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+
+import static junit.framework.TestCase.assertNotNull;
+import static junit.framework.TestCase.assertSame;
+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.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class MapboxTest {
+
+ private Context context;
+ private Context appContext;
+
+ @Before
+ public void before() {
+ context = mock(Context.class);
+ appContext = mock(Context.class);
+ when(context.getApplicationContext()).thenReturn(appContext);
+ }
+
+ @Test
+ public void testGetAccessToken() {
+ final String accessToken = "pk.0000000001";
+ injectMapboxSingleton(accessToken);
+ assertSame(accessToken, Mapbox.getAccessToken());
+ }
+
+ @Test(expected = InvalidAccessTokenException.class)
+ public void testGetInvalidAccessToken() {
+ final String accessToken = "dummy";
+ injectMapboxSingleton(accessToken);
+ assertSame(accessToken, Mapbox.getAccessToken());
+ }
+
+ @Test
+ public void testApplicationContext() {
+ injectMapboxSingleton("dummy");
+ assertNotNull(Mapbox.getApplicationContext());
+ assertNotEquals(context, appContext);
+ assertEquals(appContext, appContext);
+ }
+
+ @Test
+ public void testConnected() {
+ injectMapboxSingleton("dummy");
+
+ // test Android connectivity
+ ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
+ NetworkInfo networkInfo = mock(NetworkInfo.class);
+ when(appContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);
+ when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);
+ when(networkInfo.isConnected()).thenReturn(false);
+ assertFalse(Mapbox.isConnected());
+ when(networkInfo.isConnected()).thenReturn(true);
+ assertTrue(Mapbox.isConnected());
+
+ // test manual connectivity
+ Mapbox.setConnected(true);
+ assertTrue(Mapbox.isConnected());
+ Mapbox.setConnected(false);
+ assertFalse(Mapbox.isConnected());
+
+ // reset to Android connectivity
+ Mapbox.setConnected(null);
+ assertTrue(Mapbox.isConnected());
+ }
+
+ private void injectMapboxSingleton(String accessToken) {
+ Mapbox mapbox = new Mapbox(appContext, accessToken);
+ try {
+ Field field = Mapbox.class.getDeclaredField("INSTANCE");
+ field.setAccessible(true);
+ field.set(mapbox, mapbox);
+ } catch (Exception exception) {
+ throw new AssertionError();
+ }
+ }
+}