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

Commit

Permalink
[android] speed up ui tests
Browse files Browse the repository at this point in the history
- Make the idling resource use a push model instead of polling for the ready state.
  • Loading branch information
ivovandongen committed Sep 22, 2017
1 parent f6c6d45 commit c9c2e96
Showing 1 changed file with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
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
Expand All @@ -26,32 +34,23 @@ public String getName() {

@Override
public boolean isIdleNow() {
boolean idle = isMapboxMapReady();
if (idle && resourceCallback != null) {
resourceCallback.onTransitionToIdle();
}
return idle;
return mapboxMap != null;
}

@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
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();
}
}
}

0 comments on commit c9c2e96

Please sign in to comment.