Reputation: 1669
I'm trying to run a series of tests using Espresso for Android. It appears that between runs the Activities are NOT being closed. Whatever the app state after one test, its left for the next test.
I need to run each of my tests from a fresh app start. In Robotium this is handled using solo.finishOpenedActivites() in the tearDown() method.
http://robotium.googlecode.com/svn/doc/com/robotium/solo/Solo.html#finishOpenedActivities()
How can this be accomplished with Espresso?
Upvotes: 13
Views: 7835
Reputation: 2195
The solution Sebastian Gröbler wrote works perfectly. Nevertheless, the libraries have been renamed in the latest Espresso and Android Test Library, here is the same class written for Espresso 2.x
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitor;
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import android.support.test.runner.lifecycle.Stage;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
public final class ActivityFinisher implements Runnable {
public static void finishOpenActivities() {
new Handler(Looper.getMainLooper()).post(new ActivityFinisher());
}
private final ActivityLifecycleMonitor activityLifecycleMonitor;
private ActivityFinisher() {
this.activityLifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance();
}
@Override
public void run() {
final List<Activity> activities = new ArrayList<Activity>();
for (final Stage stage : EnumSet.range(Stage.CREATED, Stage.STOPPED)) {
activities.addAll(activityLifecycleMonitor.getActivitiesInStage(stage));
}
for (final Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
Upvotes: 4
Reputation: 311
The problem with the supplied fix on the bug report is, that this will only be executed on finish of the whole suite. If you want to have a clean activity stack after each test you need to manually do something. I wrote a little class that does pretty much the same as the fix on the above mentioned ticket, but can be executed at any point in time.
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitor;
import com.google.android.apps.common.testing.testrunner.ActivityLifecycleMonitorRegistry;
import com.google.android.apps.common.testing.testrunner.Stage;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
public final class ActivityFinisher implements Runnable {
public static void finishOpenActivities() {
new Handler(Looper.getMainLooper()).post(new ActivityFinisher());
}
private ActivityLifecycleMonitor activityLifecycleMonitor;
public ActivityFinisher() {
this.activityLifecycleMonitor = ActivityLifecycleMonitorRegistry.getInstance();
}
@Override
public void run() {
final List<Activity> activities = new ArrayList<Activity>();
for (final Stage stage : EnumSet.range(Stage.CREATED, Stage.STOPPED)) {
activities.addAll(activityLifecycleMonitor.getActivitiesInStage(stage));
}
for (final Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
Upvotes: 10
Reputation: 6008
The GoogleInstrumentation actually tries to finish all open activities, but fails to do it properly due to a silly bug: https://code.google.com/p/android-test-kit/issues/detail?id=66
The patch linked in the issue fixes this problem.
Upvotes: 2