Skip to content

Commit

Permalink
Remove precondition check in ShadowActivityThread resetter
Browse files Browse the repository at this point in the history
Previously, the ShadowActivityThread resetter would crash in a precondition
check if the ActivityThread has not been set.

Generally the ActivityThread should be getting set, except if an exception
occurs during test initialization. if an exception does occur early in
initialization, it is not very useful to throw an exception in the resetter, as
it causes the original exception message to be replaced with the Exception.

Instead, just skip the resetter logic if the ActivityThread is detected to be
null.

Updates #8160

PiperOrigin-RevId: 527803222
  • Loading branch information
hoisie committed May 2, 2023
1 parent 9551b04 commit 866dacc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Expand Up @@ -50,6 +50,7 @@
import org.robolectric.annotation.Config.Implementation;
import org.robolectric.annotation.experimental.LazyApplication;
import org.robolectric.annotation.experimental.LazyApplication.LazyLoad;
import org.robolectric.config.ConfigurationRegistry;
import org.robolectric.internal.AndroidSandbox.TestEnvironmentSpec;
import org.robolectric.internal.ResourcesMode;
import org.robolectric.internal.ShadowProvider;
Expand Down Expand Up @@ -163,10 +164,10 @@ public void failureInResetterDoesntBreakAllTests() throws Exception {
assertThat(events)
.containsExactly(
"started: first",
"failure: ShadowActivityThread.reset: ActivityThread not set",
"failure: fake error in setUpApplicationState",
"finished: first",
"started: second",
"failure: ShadowActivityThread.reset: ActivityThread not set",
"failure: fake error in setUpApplicationState",
"finished: second")
.inOrder();
}
Expand Down Expand Up @@ -337,6 +338,9 @@ public AndroidTestEnvironmentWithFailingSetUp(
@Override
public void setUpApplicationState(Method method,
Configuration configuration, AndroidManifest appManifest) {
// ConfigurationRegistry.instance is required for resetters.
Config config = configuration.get(Config.class);
ConfigurationRegistry.instance = new ConfigurationRegistry(configuration.map());
throw new RuntimeException("fake error in setUpApplicationState");
}
}
Expand Down
Expand Up @@ -27,14 +27,14 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nonnull;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.ReflectorObject;
import org.robolectric.annotation.Resetter;
import org.robolectric.util.Logger;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.ForType;
Expand Down Expand Up @@ -281,7 +281,12 @@ private interface ActivityClientRecordReflector {
@Resetter
public static void reset() {
Object activityThread = RuntimeEnvironment.getActivityThread();
Objects.requireNonNull(activityThread, "ShadowActivityThread.reset: ActivityThread not set");
reflector(_ActivityThread_.class, activityThread).getActivities().clear();
if (activityThread == null) {
Logger.warn(
"RuntimeEnvironment.getActivityThread() is null, an error likely occurred during test"
+ " initialization.");
} else {
reflector(_ActivityThread_.class, activityThread).getActivities().clear();
}
}
}

0 comments on commit 866dacc

Please sign in to comment.