[android] - add resource usage to HttpRequest, add offline query parameter flag to http request (#14837)
This commit is contained in:
@@ -1,17 +1,36 @@
|
||||
package com.mapbox.mapboxsdk;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class MapboxInjector {
|
||||
|
||||
public static void inject(Context context, String accessToken) {
|
||||
private static final String FIELD_INSTANCE = "INSTANCE";
|
||||
private static final String FIELD_ACCOUNTS = "accounts";
|
||||
|
||||
public static void inject(@NonNull Context context, @NonNull String accessToken) {
|
||||
inject(context, accessToken, null);
|
||||
}
|
||||
|
||||
public static void inject(@NonNull Context context, @NonNull String accessToken, @Nullable String skuToken) {
|
||||
Mapbox mapbox = new Mapbox(context, accessToken);
|
||||
try {
|
||||
Field field = Mapbox.class.getDeclaredField("INSTANCE");
|
||||
field.setAccessible(true);
|
||||
field.set(mapbox, mapbox);
|
||||
Field instance = Mapbox.class.getDeclaredField(FIELD_INSTANCE);
|
||||
instance.setAccessible(true);
|
||||
instance.set(mapbox, mapbox);
|
||||
|
||||
Field accounts = Mapbox.class.getDeclaredField(FIELD_ACCOUNTS);
|
||||
accounts.setAccessible(true);
|
||||
|
||||
AccountsManager manager = mock(AccountsManager.class);
|
||||
when(manager.getSkuToken()).thenReturn(skuToken);
|
||||
accounts.set(mapbox, manager);
|
||||
} catch (Exception exception) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
@@ -19,7 +38,7 @@ public class MapboxInjector {
|
||||
|
||||
public static void clear() {
|
||||
try {
|
||||
Field field = Mapbox.class.getDeclaredField("INSTANCE");
|
||||
Field field = Mapbox.class.getDeclaredField(FIELD_INSTANCE);
|
||||
field.setAccessible(true);
|
||||
field.set(field, null);
|
||||
} catch (Exception exception) {
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.mapbox.mapboxsdk.module.http
|
||||
|
||||
import com.mapbox.mapboxsdk.MapboxInjector
|
||||
import com.mapbox.mapboxsdk.http.HttpRequestUrl
|
||||
import io.mockk.mockk
|
||||
import junit.framework.Assert.assertEquals
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class HttpRequestUrlTest {
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MapboxInjector.inject(mockk(relaxed = true), "pk.foobar", "foobar")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOfflineFlagMapboxCom() {
|
||||
val expected = "http://mapbox.com/path/of/no/return.pbf?sku=foobar&offline=true"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("mapbox.com", "http://mapbox.com/path/of/no/return.pbf", 0, true)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOfflineFlagMapboxCn() {
|
||||
val expected = "http://mapbox.cn/path/of/no/return.pbf?sku=foobar&offline=true"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("mapbox.cn", "http://mapbox.cn/path/of/no/return.pbf", 0, true)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOfflineFlagInvalidHost() {
|
||||
val expected = "http://foobar.com/path/of/no/return.pbf"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("foobar.com", "http://foobar.com/path/of/no/return.pbf", 0, true)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnlineMapboxCom() {
|
||||
val expected = "http://mapbox.com/path/of/no/return.pbf?sku=foobar"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("mapbox.com", "http://mapbox.com/path/of/no/return.pbf", 0, false)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnlineMapboxCn() {
|
||||
val expected = "http://mapbox.cn/path/of/no/return.pbf?sku=foobar"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("mapbox.cn", "http://mapbox.cn/path/of/no/return.pbf", 0, false)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnlineInvalidHost() {
|
||||
val expected = "http://foobar.com/path/of/no/return.pbf"
|
||||
val actual = HttpRequestUrl.buildResourceUrl("foobar.com", "http://foobar.com/path/of/no/return.pbf", 0, false)
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
MapboxInjector.clear()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user