title: espresso long click on menu item and popup menu
body: <p><b>UPDATE:</b> it seems that when just using the app, at some random times, the app will crash when I long click, especially on items toward the bottom of the screen. Any ideas on why that might be?</p>
  
  <p>So there's a listview in my app. If you longclick a listview item, then a popup menu, anchored to that item which you've longclicked, appears with options to delete or edit the menu item. Just using the app, everything works fine. You can long click any item in the list, the underlying associated data is deleted and the listview is refreshed without that item that you just deleted.</p>
  
  <p><b> What I'm trying to do</b>:
  Espresso does not like my list view and long clicking though. I'm simply trying to test that the popup menu appears, but I can't even get espresso to longclick successfully. I'm thinking the issue has something to do with the popup menu. So for starters, I'm just trying to get espresso to longclick without getting upset at me. Once espresso longclicks successfully, I think I can figure out how check whether or not the popup is displayed.</p>
  
  <p><b>What I've tried so far</b>: I read that turning off animations is supposed to help espresso be less flaky. I turned off all the animations I could and the problem still persists. I can even watch the test run on my phone and see that it is longclicking the correct menu item, but when the longclick is complete and the popup menu should appear, the test fails</p>
  
  <p><b>code</b>:
  This is espresso test line that fails. I've used the same line of code before except with <code>click()</code> and the app does what it's supposed to and espresso is happy. mCourseCount is simply the index of the last item in the list. That's not the issue. I and update the same index in several other tests and all works fine.</p>
  
  <pre><code>onData(anything()).inAdapterView(withId(R.id.listview_class))    
        .atPosition(mCourseCount).perform(longClick());
  </code></pre>
  
  <p>Upon failure, there exist a couple interesting lines in the stack trace</p>
  
  <pre><code> android.support.test.espresso.PerformException: Error performing 'long
   click' on view ' displaying data matching: ANYTHING within adapter vieW
   matching: with id: com.cmsc355.classcompass.classcompass:id/listview_class'
  </code></pre>
  
  <p>and later on</p>
  
  <pre><code> Caused by: java.lang.IllegalStateException: MenuPopupHelper cannot be used
   without an anchor at
   com.android.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:101)
   at android.widget.PopupMenu.show(PopupMenu.java:108)at
   com.cmsc355.classcompass.classcompass.CourseMenuFragment$2.
   onItemLongClick(CourseMenuFragment.java:91)
  </code></pre>
  
  <p>This last bit of the stack trace is confusing because I defintely set up the popup menu with an anchor as follows (this is from CourseMenuFragment around line 91):</p>
  
  <pre><code>    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(AdapterView&lt;?&gt; parent, final View view, final int position, long id) {
  
          PopupMenu popupEdit = new PopupMenu(getActivity(), listView.getChildAt(position));
          popupEdit.getMenuInflater().inflate(R.menu.course_longclick_popup, popupEdit.getMenu());
          popupEdit.show();
  
          popupEdit.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
              @Override
              public boolean onMenuItemClick(MenuItem item) {
  
              if (item.getItemId() == R.id.edit_course) {
  
              } else if (item.getItemId() == R.id.delete_course) {
                  throwCourseDeleteAlert(position);
                  listView.setAdapter(mCourseNameAdapter);
              }
              return true;
              }
          });
          return true;
          }
      });
  </code></pre>
  
  <p>In the line where the popup is first instantiated, <code>listView.getChildAt(position)</code> define the anchor for the popup menu. Maybe there's a problem with this? But as mentioned, everything works completely fine as expected when just interacting with the myself.</p>
  
  <p>Any guidance would be appreciated. Please let me know if anymore clarification is needed.</p>