[android] speed up ui tests
- Make the idling resource use a push model instead of polling for the ready state.
This commit is contained in:
committed by
Ivo van Dongen
parent
18bb6da109
commit
bef58c38a8
@@ -3,20 +3,28 @@ package com.mapbox.mapboxsdk.testapp.utils;
|
||||
import android.app.Activity;
|
||||
import android.support.test.espresso.IdlingResource;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
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 {
|
||||
public class OnMapReadyIdlingResource implements IdlingResource, OnMapReadyCallback {
|
||||
|
||||
private final Activity activity;
|
||||
private MapboxMap mapboxMap;
|
||||
private IdlingResource.ResourceCallback resourceCallback;
|
||||
private Activity activity;
|
||||
|
||||
public OnMapReadyIdlingResource(Activity activity) {
|
||||
this.activity = activity;
|
||||
try {
|
||||
Field field = activity.getClass().getDeclaredField("mapView");
|
||||
field.setAccessible(true);
|
||||
((MapView) field.get(activity)).getMapAsync(this);
|
||||
} catch (Exception err) {
|
||||
throw new RuntimeException(err);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -26,11 +34,7 @@ public class OnMapReadyIdlingResource implements IdlingResource {
|
||||
|
||||
@Override
|
||||
public boolean isIdleNow() {
|
||||
boolean idle = isMapboxMapReady();
|
||||
if (idle && resourceCallback != null) {
|
||||
resourceCallback.onTransitionToIdle();
|
||||
}
|
||||
return idle;
|
||||
return mapboxMap != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -38,20 +42,15 @@ public class OnMapReadyIdlingResource implements IdlingResource {
|
||||
this.resourceCallback = resourceCallback;
|
||||
}
|
||||
|
||||
private boolean isMapboxMapReady() {
|
||||
try {
|
||||
Field field = activity.getClass().getDeclaredField("mapboxMap");
|
||||
field.setAccessible(true);
|
||||
mapboxMap = (MapboxMap) field.get(activity);
|
||||
Timber.e("isMapboxReady called with value %s", (mapboxMap != null));
|
||||
return mapboxMap != null;
|
||||
} catch (Exception exception) {
|
||||
Timber.e(exception, "could not reflect");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public MapboxMap getMapboxMap() {
|
||||
return mapboxMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMapReady(MapboxMap mapboxMap) {
|
||||
this.mapboxMap = mapboxMap;
|
||||
if (resourceCallback != null) {
|
||||
resourceCallback.onTransitionToIdle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user