Espresso long presses a menu item and pop-up menu

UPDATE: it seems that when using the application at some random time, the application will crash when I click the button, especially on the elements at the bottom of the screen. Any ideas on why this might be?

So in my application there is a list. If you look at a list item for a long time, a pop-up menu appears that is tied to the item that you have crossed out a long time ago, with options for deleting or editing the menu item. Just using the application, everything works fine. You can long click on any item in the list, the underlying related data will be deleted, and the list will be updated without this item that you just deleted.

What I'm trying to do : Espresso doesn't like my list browsing and long press. I'm just trying to verify that a pop-up menu appears, but I can't even get espresso for a long click. I think the problem has something to do with the popup menu. So for starters, I'm just trying to get the espresso to chat for a long time without getting upset. Once espresso longclicks is successful, I think I can figure out how to check if a popup is displayed.

What I have tried so far : I read that turning off the animation should help the espresso to be less flaky. I turned off all animations that I could, and the problem still persists. I can even watch the test run on my phone and see that it is a long click on the correct menu item, but when the long click is completed and the pop-up menu should appear, the test fails

code : This is an espresso test line that fails. I used the same line of code except with click() , and the application does what it should have been, and the espresso is happy. mCourseCount is just the index of the last item in the list. It's not a problem. I update the same index in several other tests, and everything works fine.

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

When a stack trace fails, there are several interesting lines

  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 then

  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 misleading because I defintely configured the anchor popup menu 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 created, listView.getChildAt(position) defines the anchor for the popup menu. Maybe the problem is this? But, as already mentioned, everything works fine, as expected, when you just interact with yourself.

Any guidance would be greatly appreciated. Please let me know if clarification is no longer required.

+5
source share
1 answer

Found a solution. I have accessed list items incorrectly. Instead

 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 position index when I scrolled further in the list. I hate answering my own questions, but maybe this will help someone else.

+3
source

Source: https://habr.com/ru/post/1233915/


All Articles