Java Code Examples for android.app.Instrumentation#waitForMonitorWithTimeout()

The following examples show how to use android.app.Instrumentation#waitForMonitorWithTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtil.java    From android-browser-helper with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: TestUtil.java    From custom-tabs-client with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}