public static void waitForWindowFocus(View view) throws InterruptedException, TimeoutException {
    long deadline = SystemClock.uptimeMillis() + UI_ACTION_EFFECT_TIMEOUT_MILLIS;
    while (!view.hasWindowFocus()) {
      long millisTillDeadline = deadline - SystemClock.uptimeMillis();
      if (millisTillDeadline < 0) {
        throw new TimeoutException("Timed out while waiting for window focus");
      }
      Thread.sleep(50);
    }
  }
Waits until the window which contains the provided view has focus.
javatests/com/google/android/apps/authenticator/testing/TestUtilities.java