From 866dacc37308c4b5381d7f01d5c56193f643b412 Mon Sep 17 00:00:00 2001 From: Michael Hoisie Date: Fri, 28 Apr 2023 00:10:52 -0700 Subject: [PATCH] Remove precondition check in ShadowActivityThread resetter 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 --- .../org/robolectric/RobolectricTestRunnerTest.java | 8 ++++++-- .../org/robolectric/shadows/ShadowActivityThread.java | 11 ++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/robolectric/src/test/java/org/robolectric/RobolectricTestRunnerTest.java b/robolectric/src/test/java/org/robolectric/RobolectricTestRunnerTest.java index a3ef0fcf9da..737af48498e 100644 --- a/robolectric/src/test/java/org/robolectric/RobolectricTestRunnerTest.java +++ b/robolectric/src/test/java/org/robolectric/RobolectricTestRunnerTest.java @@ -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; @@ -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(); } @@ -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"); } } diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowActivityThread.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowActivityThread.java index f1ffe507523..852919a4d84 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowActivityThread.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowActivityThread.java @@ -27,7 +27,6 @@ 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; @@ -35,6 +34,7 @@ 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; @@ -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(); + } } }