private static void selectDialogButton(Instrumentation instrumentation) {
    // If the dialog contains too much text it will scroll and the buttons at the bottom will only
    // get selected once it scrolls to the very bottom.
    // So far, 6 x DPAD_DOWN seems to do the trick for our app...
    instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
    instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
    instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
    instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
    instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
    instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
  }
Selects a button at the bottom of a dialog. This assumes that a dialog is currently displayed
in the foreground.
javatests/com/google/android/apps/authenticator/testing/TestUtilities.java