Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActivityController.close that transitions Activity to destroyed state #6864

Merged
merged 1 commit into from Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -379,6 +379,77 @@ public void windowFocusChanged() {
assertThat(controller.get().hasWindowFocus()).isTrue();
}

@Test
public void close_transitionsActivityStateToDestroyed() {
Robolectric.buildActivity(MyActivity.class).close();
assertThat(transcript).isEmpty();
transcript.clear();

Robolectric.buildActivity(MyActivity.class).create().close();
assertThat(transcript)
.containsExactly("onCreate", "finishedOnCreate", "onDestroy", "finishedOnDestroy");
transcript.clear();

Robolectric.buildActivity(MyActivity.class).create().start().close();
assertThat(transcript)
.containsExactly(
"onCreate",
"finishedOnCreate",
"onStart",
"finishedOnStart",
"onStop",
"finishedOnStop",
"onDestroy",
"finishedOnDestroy");
transcript.clear();

Robolectric.buildActivity(MyActivity.class).setup().close();
assertThat(transcript)
.containsExactly(
"onCreate",
"finishedOnCreate",
"onStart",
"finishedOnStart",
"onPostCreate",
"finishedOnPostCreate",
"onResume",
"finishedOnResume",
"onPostResume",
"finishedOnPostResume",
"onPause",
"finishedOnPause",
"onStop",
"finishedOnStop",
"onDestroy",
"finishedOnDestroy");
}

@Test
public void close_tryWithResources_getsDestroyed() {
try (ActivityController<MyActivity> ignored =
Robolectric.buildActivity(MyActivity.class).setup()) {
// no-op
}
assertThat(transcript)
.containsExactly(
"onCreate",
"finishedOnCreate",
"onStart",
"finishedOnStart",
"onPostCreate",
"finishedOnPostCreate",
"onResume",
"finishedOnResume",
"onPostResume",
"finishedOnPostResume",
"onPause",
"finishedOnPause",
"onStop",
"finishedOnStop",
"onDestroy",
"finishedOnDestroy");
}

public static class MyActivity extends Activity {
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Expand Down
Expand Up @@ -44,7 +44,7 @@
*/
@SuppressWarnings("NewApi")
public class ActivityController<T extends Activity>
extends ComponentController<ActivityController<T>, T> {
extends ComponentController<ActivityController<T>, T> implements AutoCloseable {

enum LifecycleState {
INITIAL,
Expand Down Expand Up @@ -541,6 +541,38 @@ private Instrumentation getInstrumentation() {
return _component_.getInstrumentation();
}

/**
* Transitions the underlying Activity to the 'destroyed' state by progressing through the
* appropriate lifecycle events. It frees up any resources and makes the Activity eligible for GC.
*/
@Override
public void close() {

LifecycleState originalState = currentState;

switch (originalState) {
case INITIAL:
case DESTROYED:
return;
case RESUMED:
pause();
// fall through
case PAUSED:
// fall through
case RESTARTED:
// fall through
case STARTED:
stop();
// fall through
case STOPPED:
// fall through
case CREATED:
break;
}

destroy();
}

/** Accessor interface for android.app.Activity.NonConfigurationInstances's internals. */
@ForType(className = "android.app.Activity$NonConfigurationInstances")
interface _NonConfigurationInstances_ {
Expand Down