In my test case there is a place where in the sort button could be on the menu overflow or outside as I have used
android:showAsAction=ifRoom
I test it on normal sized devices with onView(withId(R.id.sort)).perform(click()); but it fails on smaller screen devices. I read about
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());