In the Espresso class:
@Rule
public IntentsTestRule<MainActivity> mIntentsRule = new IntentsTestRule<>(
MainActivity.class);
@Test
public void test_backButton(){
onView(withId(R.id.NEXT_ACTIVITY)).perform(scrollTo(), click());
Espresso.pressBack();
}
In action:
@Override
public void onBackPressed() {
Log.d("TEST_pressBack", "inside onBackPressed()");
do_something();
super.onBackPressed();
}
@Override
public void finish() {
Log.d("TEST_pressBack", "inside finish()");
super.finish();
}
When I call the Espresso test method, execution jumps directly to finish()
.
When I press the back button (with my hand) in Activity
, execution is done first in onBackPressed()
, and then before finish()
. How to test a function onBackPressed()
on Espresso? Thanks!
EDIT: This is my mistake. The problem was that in Activity, in which I wanted to call pressBack, an on-screen keyboard was opened. When the soft keyboard is open, the button does not call onBackPressed, but the keyboard does not appear instead. I tried with two keystrokes () in a line, and it worked correctly:
@Rule
public IntentsTestRule<MainActivity> mIntentsRule = new IntentsTestRule<>(
MainActivity.class);
@Test
public void test_backButton(){
onView(withId(R.id.NEXT_ACTIVITY)).perform(scrollTo(), click());
Espresso.pressBack();
Espresso.pressBack();
}
source
share