Skip to content

Instantly share code, notes, and snippets.

@sdsantos
Last active February 27, 2020 15:54
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sdsantos/ea8da857978bb038ac88 to your computer and use it in GitHub Desktop.
Wait for condition helper for testing
public class Wait {
private static final int CHECK_INTERVAL = 100;
private static final int TIMEOUT = 10000;
public interface Condition {
boolean check();
}
private Condition mCondition;
public Wait(Condition condition) {
mCondition = condition;
}
public void waitForIt() {
boolean state = mCondition.check();
long startTime = System.currentTimeMillis();
while (!state) {
try {
Thread.sleep(CHECK_INTERVAL);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (System.currentTimeMillis() - startTime > TIMEOUT) {
throw new AssertionError("Wait timeout.");
}
state = mCondition.check();
}
}
}
@sdsantos
Copy link
Author

sdsantos commented Oct 7, 2015

Usage:

new Wait(new Wait.Condition() {
    @Override
    public boolean check() {
        return someView.isEnabled();
    }
}).waitForIt();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment