@SuppressWarnings("unchecked")
  @TargetApi(11)
  @FixWhenMinSdkVersion(11) // Will be able to get rid of the contextual menu
  private void openContextualActionBarOrMenuAndInvokeItem(
      ListView listView, int position, int menuItemTextId) {
    String menuItemText = activityTestRule.getActivity().getString(menuItemTextId);

    onView(equalTo(listView.getChildAt(position))).perform(longClick());

    // Force a UI synchronization by checking a trivial statement about the Root view
    onView(isRoot()).check(ViewAssertions.matches(isDisplayed()));
    // Now either the old fashioned context menu or CAB is open...
    // Check that the CAB appears to be active
    ActionMode actionMode = activityTestRule.getActivity().actionMode;
    assertThat(actionMode).isNotNull();

    // In either case, find the requested menu item and click it
    try {
      // Could be a text menu or an image menu (with content descriptions matching the text)
      onView(
              allOf(
                  isDisplayed(),
                  anyOf(withText(menuItemText), withContentDescription(menuItemText))))
          .perform(click());
    } catch (NoMatchingViewException e) {

      // Perhaps the menu item is buried in the overflow menu. Try opening that.
      // We can't use openActionBarOverflowOrOptionsMenu because a Toolbar is used instead of the
      // default ActionBar, so when the CAB show up it will be show on a different ActionBar that
      // overlay the Toolbar and there will be two different "OverflowMenuButton" on the Toolbar and
      // the CAB, which make the Espresso get an ambiguous exception failure.
      onView(
              allOf(
                  withClassName(endsWith("OverflowMenuButton")),
                  withParent(withParent(withClassName(endsWith("ActionBarContextView"))))))
          .perform(click());

      // Overflow menu items are only shown as text, and do not have the original MenuItem's id
      onView(withText(menuItemText)).perform(click());
    }
  }
This function return the original pin code from the styled pin code, which may has a space in
the middle of the string

@param pin The styled pin code
@return The original pin code with no spaces
javatests/com/google/android/apps/authenticator/AuthenticatorActivityTest.java