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

[android] - add unit test for Mapbox #8228

Merged
merged 1 commit into from
Mar 1, 2017
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 @@ -43,7 +43,7 @@ public static synchronized Mapbox getInstance(@NonNull Context context, @NonNull
return INSTANCE;
}

private Mapbox(@NonNull Context context, @NonNull String accessToken) {
Mapbox(@NonNull Context context, @NonNull String accessToken) {
this.context = context;
this.accessToken = accessToken;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}