Created
January 17, 2017 19:08
-
-
Save reline/e3bb29e264c5022aba12214a5f782e6d to your computer and use it in GitHub Desktop.
Espresso Idling Resource that waits until either a certain condition is met or a given time has passed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.test.espresso.IdlingResource; | |
public class ConditionalTimeoutIdlingResource implements IdlingResource { | |
private final long startTime; | |
private final long waitingTime; | |
private ResourceCallback resourceCallback; | |
private ConditionalResourceCallback conditionalResourceCallback = new ConditionalResourceCallback() { | |
@Override | |
public boolean meetsCondition() { | |
return false; // default | |
} | |
}; | |
public ConditionalTimeoutIdlingResource(long waitingTime) { | |
this.startTime = System.currentTimeMillis(); | |
this.waitingTime = waitingTime; | |
} | |
@Override | |
public String getName() { | |
return ConditionalTimeoutIdlingResource.class.getName() + ":" + waitingTime; | |
} | |
@Override | |
public boolean isIdleNow() { | |
long elapsed = System.currentTimeMillis() - startTime; | |
boolean idle = elapsed >= waitingTime || conditionalResourceCallback.meetsCondition(); | |
if (idle) { | |
resourceCallback.onTransitionToIdle(); | |
} | |
return idle; | |
} | |
@Override | |
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) { | |
this.resourceCallback = resourceCallback; | |
} | |
public void registerConditionalTransitionCallback(ConditionalResourceCallback conditionalResourceCallback) { | |
this.conditionalResourceCallback = conditionalResourceCallback; | |
} | |
public interface ConditionalResourceCallback { | |
boolean meetsCondition(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment