[android] - allow resetting a custom OkHttp client to the default implementation
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.mapbox.mapboxsdk;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class MapboxInjector {
|
||||
|
||||
public static void inject(Context context, String accessToken) {
|
||||
Mapbox mapbox = new Mapbox(context, accessToken);
|
||||
try {
|
||||
Field field = Mapbox.class.getDeclaredField("INSTANCE");
|
||||
field.setAccessible(true);
|
||||
field.set(mapbox, mapbox);
|
||||
} catch (Exception exception) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
try {
|
||||
Field field = Mapbox.class.getDeclaredField("INSTANCE");
|
||||
field.setAccessible(true);
|
||||
field.set(field, null);
|
||||
} catch (Exception exception) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,10 @@
|
||||
package com.mapbox.mapboxsdk;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.junit.After;
|
||||
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;
|
||||
@@ -32,13 +29,13 @@ public class MapboxTest {
|
||||
@Test
|
||||
public void testGetAccessToken() {
|
||||
final String accessToken = "pk.0000000001";
|
||||
injectMapboxSingleton(accessToken);
|
||||
MapboxInjector.inject(context, accessToken);
|
||||
assertSame(accessToken, Mapbox.getAccessToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplicationContext() {
|
||||
injectMapboxSingleton("pk.0000000001");
|
||||
MapboxInjector.inject(context, "pk.0000000001");
|
||||
assertNotNull(Mapbox.getApplicationContext());
|
||||
assertNotEquals(context, appContext);
|
||||
assertEquals(appContext, appContext);
|
||||
@@ -71,27 +68,7 @@ public class MapboxTest {
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
clearMapboxSingleton();
|
||||
MapboxInjector.clear();
|
||||
}
|
||||
|
||||
private void injectMapboxSingleton(String accessToken) {
|
||||
Mapbox mapbox = new Mapbox(context, accessToken);
|
||||
try {
|
||||
Field field = Mapbox.class.getDeclaredField("INSTANCE");
|
||||
field.setAccessible(true);
|
||||
field.set(mapbox, mapbox);
|
||||
} catch (Exception exception) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
private void clearMapboxSingleton() {
|
||||
try {
|
||||
Field field = Mapbox.class.getDeclaredField("INSTANCE");
|
||||
field.setAccessible(true);
|
||||
field.set(field, null);
|
||||
} catch (Exception exception) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.mapbox.mapboxsdk.module.http
|
||||
|
||||
import com.mapbox.mapboxsdk.MapboxInjector
|
||||
import io.mockk.mockk
|
||||
import junit.framework.Assert.assertEquals
|
||||
import okhttp3.OkHttpClient
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class HttpRequestUtilTest {
|
||||
|
||||
@Test
|
||||
fun replaceHttpClient() {
|
||||
MapboxInjector.inject(mockk(relaxed = true), "")
|
||||
|
||||
assertEquals(HttpRequestImpl.DEFAULT_CLIENT, HttpRequestImpl.client)
|
||||
|
||||
val httpMock = mockk<OkHttpClient>()
|
||||
HttpRequestUtil.setOkHttpClient(httpMock)
|
||||
assertEquals("Http client should have set to the mocked client",
|
||||
httpMock,
|
||||
HttpRequestImpl.client
|
||||
)
|
||||
|
||||
HttpRequestUtil.setOkHttpClient(null)
|
||||
assertEquals("Http client should have been reset to the default client",
|
||||
HttpRequestImpl.DEFAULT_CLIENT,
|
||||
HttpRequestImpl.client
|
||||
)
|
||||
|
||||
MapboxInjector.clear()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user