/** @return success */
 public boolean invokeContextMenuAction4ListItemId(
     String methodExt, long listItemId, ContextMenuItem menuItem) throws InterruptedException {
   final String method = "invokeContextMenuAction4ListItemId";
   boolean success = false;
   long id2 = listItemId;
   String msg = "";
   for (long attempt = 1; attempt < 4; attempt++) {
     TestSuite.waitForIdleSync(mInstrumentation);
     int position = getPositionOfListItemId(listItemId);
     msg =
         "listItemId="
             + listItemId
             + "; menu Item="
             + menuItem
             + "; position="
             + position
             + "; attempt="
             + attempt;
     MyLog.v(this, msg);
     if (getListItemIdAtPosition(position) == listItemId) {
       selectListPosition(methodExt, position);
       if (invokeContextMenuAction(methodExt, mActivity, position, menuItem.getId())) {
         id2 = getListItemIdAtPosition(position);
         if (id2 == listItemId) {
           success = true;
           break;
         } else {
           MyLog.i(
               methodExt,
               method + "; Position changed, now pointing to listItemId=" + id2 + "; " + msg);
         }
       }
     }
     ;
   }
   MyLog.v(methodExt, method + " ended " + success + "; " + msg);
   TestSuite.waitForIdleSync(mInstrumentation);
   return success;
 }
 private void longClickAtPosition(final String methodExt, final int position)
     throws InterruptedException {
   final View view = getListView().getChildAt(position);
   assertTrue("View at list position " + position + " exists", view != null);
   mInstrumentation.runOnMainSync(
       new Runnable() {
         @Override
         public void run() {
           MyLog.v(methodExt, "performLongClick on " + view + " at position " + position);
           view.performLongClick();
         }
       });
   TestSuite.waitForIdleSync(mInstrumentation);
 }
  public void clickView(final String methodExt, final View view) throws InterruptedException {
    assertTrue(view != null);

    Runnable clicker =
        new Runnable() {
          @Override
          public void run() {
            MyLog.v(methodExt, "Before click view");
            view.performClick();
          }
        };

    mInstrumentation.runOnMainSync(clicker);
    MyLog.v(methodExt, "After click view");
    TestSuite.waitForIdleSync(mInstrumentation);
  }
 public void clickListAtPosition(final String methodExt, final int position)
     throws InterruptedException {
   final String method = "clickListAtPosition";
   mInstrumentation.runOnMainSync(
       new Runnable() {
         // See
         // http://stackoverflow.com/questions/8094268/android-listview-performitemclick
         @Override
         public void run() {
           long listItemId = mActivity.getListAdapter().getItemId(position);
           MyLog.v(methodExt, method + "; on performClick, listItemId=" + listItemId);
           getListView()
               .performItemClick(
                   getListView().getAdapter().getView(position, null, null), position, listItemId);
         }
       });
   MyLog.v(methodExt, method + " ended");
   TestSuite.waitForIdleSync(mInstrumentation);
 }
 /**
  * InstrumentationTestCase.getInstrumentation().invokeContextMenuAction doesn't work properly
  *
  * @return success
  *     <p>Note: This method cannot be invoked on the main thread. See
  *     https://github.com/google/google-authenticator-android/blob/master/tests/src/com/google/android/apps/authenticator/TestUtilities.java
  */
 private boolean invokeContextMenuAction(
     final String methodExt, final MyListActivity activity, int position, final int menuItemId)
     throws InterruptedException {
   final String method = "invokeContextMenuAction";
   MyLog.v(
       methodExt, method + " started on menuItemId=" + menuItemId + " at position=" + position);
   boolean success = false;
   int position1 = position;
   for (long attempt = 1; attempt < 4; attempt++) {
     longClickAtPosition(methodExt, position1);
     if (mActivity.getPositionOfContextMenu() == position) {
       success = true;
       break;
     }
     MyLog.i(
         methodExt,
         method
             + "; Context menu created for position "
             + mActivity.getPositionOfContextMenu()
             + " instead of "
             + position
             + "; was set to "
             + position1
             + "; attempt "
             + attempt);
     position1 = position + (position1 - mActivity.getPositionOfContextMenu());
   }
   if (success) {
     mInstrumentation.runOnMainSync(
         new Runnable() {
           @Override
           public void run() {
             MyLog.v(methodExt, method + "; before performContextMenuIdentifierAction");
             activity.getWindow().performContextMenuIdentifierAction(menuItemId, 0);
           }
         });
     TestSuite.waitForIdleSync(mInstrumentation);
   }
   MyLog.v(methodExt, method + " ended " + success);
   return success;
 }
 public void selectListPosition(final String methodExt, final int positionIn)
     throws InterruptedException {
   final String method = "selectListPosition";
   MyLog.v(methodExt, method + " started; position=" + positionIn);
   mInstrumentation.runOnMainSync(
       new Runnable() {
         @Override
         public void run() {
           int position = positionIn;
           ListAdapter la = getListView().getAdapter();
           if (la.getCount() <= position) {
             position = la.getCount() - 1;
           }
           MyLog.v(
               methodExt, method + " on setSelection " + position + " of " + (la.getCount() - 1));
           getListView().setSelectionFromTop(position, 0);
         }
       });
   TestSuite.waitForIdleSync(mInstrumentation);
   MyLog.v(methodExt, method + " ended");
 }