private static boolean isViewAndAllItsParentsVisible(View view) {
    if (view.getVisibility() != View.VISIBLE) {
      return false;
    }
    ViewParent parent = view.getParent();
    if (!(parent instanceof View)) {
      // This View is the root of the View hierarche, and it's visible (checked above)
      return true;
    }
    // This View itself is actually visible only all of its parents are visible.
    return isViewAndAllItsParentsVisible((View) parent);
  }
Invokes the {@link Activity}'s {@code onActivityResult} on the UI thread and blocks (with a
timeout) the calling thread until the invocation completes. If the calling thread is the UI
thread, the {@code finish} is invoked directly and without a timeout.
javatests/com/google/android/apps/authenticator/testing/TestUtilities.java