lundi 23 novembre 2015

Instrumentation.ActivityMonitor returns the previous instance of activity

I have a test scenario which goes between two activities like this: ActivityHome -> Activity2 -> ActivityHome -> Activity2

The first time I go to Activity2 it shows "Text 1" and the second time I launch Activity2 it shows "Text 2". I can see on the screen the right text is shown of course.

Now I want to code this scenario using espresso. Here is my code:

// I'm already in ActivityHome
ActivityMonitor mActivity2Monitor =
                mInstrumentation.addMonitor(Activity2.class.getName(), null, false);
onView(withText("Launch Activity2")).perform(click());
Activity2 activity2;
if (mActivity2Monitor.getHits() < 1) {
    activity2 = mActivity2Monitor.waitForActivity();
} else {
    activity2 = mActivity2Monitor.getLastActivity();
}
// Check to make sure Text 1 is showns. This passes just fine
onView(withText("Finish Activity2")).perform(click());
// Wait for home activity
onView(withText("Launch Activity2")).perform(click());
if (mActivity2Monitor.getHits() < 2) {
    activity2 = mActivity2Monitor.waitForActivity();
} else {
    activity2 = mActivity2Monitor.getLastActivity();
}
// Check to make sure Text 2 is shown. This fails because while 
// the screen shows "Text 1", activity2 still has reference to 
// the previous launch of Activity2

Not sure what's wrong here? If I put a Thread.sleep(1000) before if (mActivity2Monitor.getHits() < 2) it will work, but I couldn't find any other way to make it work without Thread.sleep.

Any suggestion?

Thanks

Aucun commentaire:

Enregistrer un commentaire