Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Close all activities on @after
private static void closeAllActivities(Instrumentation instrumentation) throws Exception {
final int NUMBER_OF_RETRIES = 100;
int i = 0;
while (closeActivity(instrumentation)) {
if (i++ > NUMBER_OF_RETRIES) {
// throw new AssertionError("Limit of retries excesses");
break;
}
}
}
private static <X> X callOnMainSync(Instrumentation instrumentation, final Callable<X> callable) throws Exception {
final AtomicReference<X> retAtomic = new AtomicReference<>();
final AtomicReference<Throwable> exceptionAtomic = new AtomicReference<>();
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
try {
retAtomic.set(callable.call());
} catch (Throwable e) {
exceptionAtomic.set(e);
}
}
});
final Throwable exception = exceptionAtomic.get();
if (exception != null) {
Throwables.propagateIfInstanceOf(exception, Exception.class);
Throwables.propagate(exception);
}
return retAtomic.get();
}
private static boolean closeActivity(Instrumentation instrumentation) throws Exception {
final Boolean activityClosed = callOnMainSync(instrumentation, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
final Set<Activity> activities =
getActivitiesInStages(Stage.RESUMED, Stage.STARTED, Stage.PAUSED, Stage.STOPPED, Stage.CREATED);
activities.removeAll(getActivitiesInStages(Stage.DESTROYED));
if (activities.size() > 0) {
final Activity activity = activities.iterator().next();
activity.finish();
return true;
} else {
return false;
}
}
});
if (activityClosed) {
instrumentation.waitForIdleSync();
}
return activityClosed;
}
private static Set<Activity> getActivitiesInStages(Stage... stages) {
final Set<Activity> activities = Sets.newHashSet();
final ActivityLifecycleMonitor instance = ActivityLifecycleMonitorRegistry.getInstance();
for (Stage stage : stages) {
final Collection<Activity> activitiesInStage = instance.getActivitiesInStage(stage);
if (activitiesInStage != null) {
activities.addAll(activitiesInStage);
}
}
return activities;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment