Skip to content

Instantly share code, notes, and snippets.

@clivejefferies
Last active January 13, 2017 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clivejefferies/2c8701ef70dd8b30cc3b62a3762acdb7 to your computer and use it in GitHub Desktop.
Save clivejefferies/2c8701ef70dd8b30cc3b62a3762acdb7 to your computer and use it in GitHub Desktop.
An Idling resource class for Android Espresso testing that gets the current activity which can then be used to determine idle status. Useful when there are multiple activities.
import android.app.Activity;
import android.support.test.espresso.IdlingResource;
import android.support.test.espresso.core.deps.guava.collect.Iterables;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.support.test.runner.lifecycle.Stage;
/**
* Created by clivejefferies on 13/01/2017.
*
* This class can be used to monitor idling resources
*/
public class RequestIdlingResource implements IdlingResource {
private ResourceCallback resourceCallback;
private boolean isIdle;
@Override
public String getName() {
return RequestIdlingResource.class.getName();
}
@Override
public boolean isIdleNow() {
if (isIdle) return true;
Activity activity = getCurrentActivity();
if (activity == null) return false;
idlingCheck(activity);
if (isIdle) {
resourceCallback.onTransitionToIdle();
}
return isIdle;
}
private Activity getCurrentActivity() {
final Activity[] activity = new Activity[1];
java.util.Collection<Activity> activities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
activity[0] = Iterables.getOnlyElement(activities);
return activity[0];
}
@Override
public void registerIdleTransitionCallback(
ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}
public void idlingCheck(Activity activity)
{
/*
Look up something (view or method call) on the activity to determine if it is idle or busy
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment