Example usage for android.app Instrumentation waitForIdleSync

List of usage examples for android.app Instrumentation waitForIdleSync

Introduction

In this page you can find the example usage for android.app Instrumentation waitForIdleSync.

Prototype

public void waitForIdleSync() 

Source Link

Document

Synchronously wait for the application to be idle.

Usage

From source file:Main.java

private static void changeOrientation(final Activity activity, Instrumentation instrumentation,
        final int orientation) {
    activity.setRequestedOrientation(orientation);
    if (instrumentation != null) {
        instrumentation.waitForIdleSync();
    }/*from   w ww .j av  a 2 s  . c  o m*/
}

From source file:com.scvngr.levelup.core.test.TestThreadingUtils.java

/**
 * Helper method to wait for an action to occur.
 *
 * @param instrumentation the test {@link Instrumentation}.
 * @param activity the activity for the test being run.
 * @param latchRunnable the runnable that will check the condition and signal success via its
 * {@link java.util.concurrent.CountDownLatch}.
 * @param timeoutMillis the timeout duration in milliseconds.
 * @param isMainThreadRunnable Determine whether or not the runnable must be invoked on the main
 * thread./*w w w. j a  v a 2 s. c o  m*/
 * @return true if the action happened before the timeout, false otherwise.
 */
public static boolean waitForAction(@NonNull final Instrumentation instrumentation,
        @NonNull final Activity activity, @NonNull final LatchRunnable latchRunnable, final long timeoutMillis,
        final boolean isMainThreadRunnable) {
    final long endTime = SystemClock.elapsedRealtime() + timeoutMillis;
    boolean result = true;

    while (true) {
        if (isMainThreadRunnable) {
            runOnMainSync(instrumentation, activity, latchRunnable);
        } else {
            latchRunnable.run();
            instrumentation.waitForIdleSync();
        }

        if (latchRunnable.getCount() == 0) {
            break;
        }

        if (SystemClock.elapsedRealtime() >= endTime) {
            result = false;
            break;
        }

        SystemClock.sleep(WAIT_SLEEP_MILLIS);
    }

    return result;
}

From source file:com.scvngr.levelup.core.test.TestThreadingUtils.java

/**
 * Runs a runnable on the main thread, but also catches any errors thrown on the main thread and
 * re-throws them on the test thread so they can be displayed more easily.
 *
 * @param instrumentation the {@link Instrumentation} for the test.
 * @param activity the {@link Activity} that this this test is running in.
 * @param runnable the runnable to run.//from  w w  w  .j a  v a  2s  .c om
 */
public static void runOnMainSync(@NonNull final Instrumentation instrumentation,
        @NonNull final Activity activity, @NonNull final Runnable runnable) {
    if (activity.getMainLooper().equals(Looper.myLooper())) {
        runnable.run();
    } else {
        final FutureAssertionError futureError = new FutureAssertionError();
        instrumentation.runOnMainSync(new Runnable() {

            @Override
            public void run() {
                try {
                    runnable.run();
                } catch (final AssertionError e) {
                    futureError.setAssertionError(e);
                }
            }
        });
        futureError.throwPendingAssertionError();

        instrumentation.waitForIdleSync();
    }
}

From source file:org.catrobat.catroid.uitest.util.UiTestUtils.java

/**
 * Returns to the main screen./*  w w w.j  a  va 2  s.co  m*/
 * This method should be called in tearDown() in tests which use Robotium.
 * See explanation here:
 * http://stackoverflow.com/questions/7851351/robotium-in-the-suite-of-tests-each-next-test-is-
 * affected-by-the-previous-test
 */
public static void goBackToHome(Instrumentation instrumentation) {
    boolean more = true;
    while (more) {
        try {
            instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
            instrumentation.waitForIdleSync();
        } catch (SecurityException e) { // Done, at Home.
            more = false;
        }
    }
}