assertDisplayed(MainActivity.class)
Do a sexy way to assure that the app is showing a given Activity
With Espresso intents?
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 💪
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
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.
hi @Sloy looks like we've added the dependency on espresso intents so, are we still looking to add this feature?