Skip to content

Instantly share code, notes, and snippets.

@cattaka
Last active May 4, 2016 06:19
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 cattaka/eaed6c7aac77902522f7 to your computer and use it in GitHub Desktop.
Save cattaka/eaed6c7aac77902522f7 to your computer and use it in GitHub Desktop.
waitForOnResume for JUnit
/** These codes are licensed under CC0. */
/** Fragmentが画面上に表示されてonResumeが呼ばれるまで待つ */
private void waitForOnResume(final Fragment fragment, int timeout) throws Throwable {
waitForTrue(this, new TestUtils.BooleanFunc() {
@Override
public boolean run() {
Rect r = new Rect();
View view = fragment.getView();
if (view != null) {
view.getGlobalVisibleRect(r);
}
return r.width() >0 && r.height() > 0 && fragment.isResumed();
}
}, timeout);
}
/** UIスレッド上でtrueになるまで待つ */
public static void waitForTrue(InstrumentationTestCase testCase, final BooleanFunc func, int timeout) throws Throwable {
long l = SystemClock.elapsedRealtime();
do {
final AtomicBoolean flag = new AtomicBoolean();
testCase.runTestOnUiThread(new Runnable() {
@Override
public void run() {
flag.set(func.run());
}
});
if (flag.get()) {
break;
}
SystemClock.sleep(20);
} while (SystemClock.elapsedRealtime() - l < timeout);
}
public interface BooleanFunc {
public boolean run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment