Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix UnsupportedOperationException if a leaked Activity.recreate is ca…
…lled Previously, ActivityController.recreate determined the current state of the underlying Activity by calling the global ActivityLifecycleMonitorRegistry.getLifecycleStageOf This method is unreliable for leaked Activities as a new ActivityLifecycleMonitor is created before each test. This means that, for a leaked Activity, the ActivityLifecycleMonitor from the previous test has knowledge if its lifecycle state, not the new one. When the Activity state was queried using the current ActivityLifecycleMonitor, an UnsupportedOperationException occurred. To fix this, avoid using ActivityLifecycleMonitor as the source of truth for Activity State in ActivityController, and instead add a new member variable to ActivityController that maintains the state. Having ActivityController rely on ActivityLifecycleMonitor for Activity state is circuitous -- AndroidX Test is already using ActivityController to drive Activity lifecycles, so the source-of-truth state should exist inside of ActivityController itself. PiperOrigin-RevId: 410168815
- Loading branch information
Showing
2 changed files
with
100 additions
and
31 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...tric/src/test/java/org/robolectric/android/controller/ActivityControllerRecreateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.robolectric.android.controller; | ||
|
||
import android.app.Activity; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.Robolectric; | ||
|
||
/** | ||
* This test captures an issue where {@link ActivityController#recreate()} would throw an {@link | ||
* UnsupportedOperationException} if an Activity from a previous test was recreated. | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ActivityControllerRecreateTest { | ||
private static final AtomicReference<ActivityController<Activity>> createdActivity = | ||
new AtomicReference<>(); | ||
|
||
@Before | ||
public void setUp() { | ||
createdActivity.compareAndSet(null, Robolectric.buildActivity(Activity.class).create()); | ||
} | ||
|
||
@Test | ||
public void failsTryingToRecreateActivityFromOtherTest1() { | ||
createdActivity.get().recreate(); | ||
} | ||
|
||
@Test | ||
public void failsTryingToRecreateActivityFromOtherTest2() { | ||
createdActivity.get().recreate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters