下面列出了android.app.Instrumentation#waitForMonitorWithTimeout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Waits until {@link TestBrowser} is launched and resumed, and returns it.
*
* @param launchRunnable Runnable that should start the activity.
*/
public static TestBrowser getBrowserActivityWhenLaunched(Runnable launchRunnable) {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
Instrumentation.ActivityMonitor monitor
= instrumentation.addMonitor(TestBrowser.class.getName(), null, false);
launchRunnable.run();
TestBrowser activity =
(TestBrowser) instrumentation.waitForMonitorWithTimeout(monitor, 3000);
assertNotNull("TestBrowser wasn't launched", activity);
// ActivityMonitor is triggered in onCreate and in onResume, which can lead to races when
// launching several activity instances. So wait for onResume before returning.
boolean resumed = activity.waitForResume(3000);
assertTrue("TestBrowser didn't reach onResume", resumed);
return activity;
}
/**
* Waits until {@link TestBrowser} is launched and resumed, and returns it.
*
* @param launchRunnable Runnable that should start the activity.
*/
public static TestBrowser getBrowserActivityWhenLaunched(Runnable launchRunnable) {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
Instrumentation.ActivityMonitor monitor
= instrumentation.addMonitor(TestBrowser.class.getName(), null, false);
launchRunnable.run();
TestBrowser activity =
(TestBrowser) instrumentation.waitForMonitorWithTimeout(monitor, 3000);
assertNotNull("TestBrowser wasn't launched", activity);
// ActivityMonitor is triggered in onCreate and in onResume, which can lead to races when
// launching several activity instances. So wait for onResume before returning.
boolean resumed = activity.waitForResume(3000);
assertTrue("TestBrowser didn't reach onResume", resumed);
return activity;
}