Last active
May 4, 2016 06:19
-
-
Save cattaka/eaed6c7aac77902522f7 to your computer and use it in GitHub Desktop.
waitForOnResume for JUnit
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
/** 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