Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

espresso long click on menu item and popup menu

UPDATE: 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?

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.

What I'm trying to do: 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.

What I've tried so far: 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

code: This is espresso test line that fails. I've used the same line of code before except with click() 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.

onData(anything()).inAdapterView(withId(R.id.listview_class))    
      .atPosition(mCourseCount).perform(longClick());

Upon failure, there exist a couple interesting lines in the stack trace

 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'

and later on

 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)

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):

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> 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;
        }
    });

In the line where the popup is first instantiated, listView.getChildAt(position) 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.

Any guidance would be appreciated. Please let me know if anymore clarification is needed.

like image 871
Hayden Braxton Avatar asked Oct 20 '22 00:10

Hayden Braxton


1 Answers

Found solution. I was accessing the list items incorrectly. Instead of

PopupMenu popupEdit = new PopupMenu(getActivity(), listView.getChildAt(position));

I changed to

PopupMenu popupEdit = new PopupMenu(getActivity(), listView.getChildAt(position - listView.getFirstVisiblePosition()));

I had to adjust the index of the position for when I was scrolled down farther in the list. I kind of hate answering my own questions on SO, but maybe this will help someone else.

like image 136
Hayden Braxton Avatar answered Nov 09 '22 06:11

Hayden Braxton