[android] - avoid failing CI tests when device under test doesn't have a valid internet connection
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.location.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.support.test.espresso.IdlingResource;
|
||||
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class OnMapReadyIdlingResource implements IdlingResource, OnMapReadyCallback {
|
||||
|
||||
private MapboxMap mapboxMap;
|
||||
private MapView mapView;
|
||||
private IdlingResource.ResourceCallback resourceCallback;
|
||||
|
||||
public OnMapReadyIdlingResource(Activity activity) {
|
||||
new Handler(Looper.getMainLooper()).post(() -> {
|
||||
try {
|
||||
Field field = activity.getClass().getDeclaredField("mapView");
|
||||
field.setAccessible(true);
|
||||
mapView = ((MapView) field.get(activity));
|
||||
mapView.getMapAsync(this);
|
||||
} catch (Exception err) {
|
||||
throw new RuntimeException(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isIdleNow() {
|
||||
return mapboxMap != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
|
||||
this.resourceCallback = resourceCallback;
|
||||
}
|
||||
|
||||
public MapView getMapView() {
|
||||
return mapView;
|
||||
}
|
||||
|
||||
public MapboxMap getMapboxMap() {
|
||||
return mapboxMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMapReady(MapboxMap mapboxMap) {
|
||||
this.mapboxMap = mapboxMap;
|
||||
if (resourceCallback != null) {
|
||||
resourceCallback.onTransitionToIdle();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,22 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.support.test.espresso.Espresso;
|
||||
import android.support.test.espresso.IdlingRegistry;
|
||||
import android.support.test.espresso.IdlingResourceTimeoutException;
|
||||
import android.support.test.espresso.ViewInteraction;
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
|
||||
import com.mapbox.mapboxsdk.Mapbox;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction;
|
||||
import com.mapbox.mapboxsdk.testapp.action.WaitAction;
|
||||
import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.TestName;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
import static android.support.test.espresso.Espresso.onView;
|
||||
@@ -59,7 +53,10 @@ public abstract class BaseActivityTest {
|
||||
}
|
||||
|
||||
protected void validateTestSetup() {
|
||||
Assert.assertTrue("Device is not connected to the Internet.", isConnected(rule.getActivity()));
|
||||
if (!Mapbox.isConnected()) {
|
||||
Timber.e("Not connected to the internet while running test");
|
||||
}
|
||||
|
||||
checkViewIsDisplayed(R.id.mapView);
|
||||
Assert.assertNotNull(mapboxMap);
|
||||
}
|
||||
@@ -82,13 +79,6 @@ public abstract class BaseActivityTest {
|
||||
onView(withId(R.id.mapView)).perform(new WaitAction(waitTime));
|
||||
}
|
||||
|
||||
static boolean isConnected(Context context) {
|
||||
ConnectivityManager connectivityManager
|
||||
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
|
||||
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
|
||||
}
|
||||
|
||||
protected ViewInteraction onMapView() {
|
||||
return onView(withId(R.id.mapView));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user