Barista icon indicating copy to clipboard operation
Barista copied to clipboard

assertDisplayed(MainActivity.class)

Open rocboronat opened this issue 8 years ago • 5 comments

Do a sexy way to assure that the app is showing a given Activity

rocboronat avatar Nov 13 '17 15:11 rocboronat

With Espresso intents?

alorma avatar Nov 13 '17 15:11 alorma

Fiuf! I remember doing some hacks to accomplish this, and we finally decided to change it to check for a specific root view instead (each activity would have a defined root view id).

But we can revisit this, maybe our improved knowledge of Espresso will let us find a good solution 💪

Sloy avatar Nov 13 '17 15:11 Sloy

We use this:

@NonNull
private Matcher<Intent> getMatcherOpenMainActivity() {
     return hasComponent(DiscoverActivity.class.getName());
}

With Espresso intents is easy to do, but it will make barista depends on it

alorma avatar Nov 13 '17 15:11 alorma

Revisiting some code from some library of mine I just saw an apparently clean way to do this. I'm not sure where did I get the idea from, but it something like this:

fun getCurrentActivity(): Activity? {
    var activity: Activity? = null

    getInstrumentation().waitForIdleSync()
    getInstrumentation().runOnMainSync {
        val activities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED)
        if (activities.isEmpty()) {
            activity = null
        } else {
            activity = Iterables.getOnlyElement(activities)
        }
    }
    return activity
}

It doesn't depend on any view or ViewAction to get the Context, nor requires to add any id to the layout root.

I'm not sure how flaky would it be, I guess we can trust in waitForIdleSync and the ActivityLifecycleMonitorRegistry. Right?

I'll leave it here in case anyone want to contribute and add it to the API.

Sloy avatar Dec 15 '17 15:12 Sloy

hi @Sloy looks like we've added the dependency on espresso intents so, are we still looking to add this feature?

rohitkaradkar avatar Oct 11 '19 09:10 rohitkaradkar