From 228f7a7a68af4000a9b7409712ca0d1994f5d7a8 Mon Sep 17 00:00:00 2001 From: hoisie Date: Tue, 16 Nov 2021 09:22:40 -0800 Subject: [PATCH] Use Activity's member Instrumentation in ActivityController Previously, ActivityController was using the global Instrumentation object, which is replaced after each test. This contributed to an issue where an Activity that leaked across tests caused illegal states to exist in ActivityLifecycleMonitor. Instead, use the Activity's own Instrumentation object, which will have a consistent view of the Activity's lifecycle. PiperOrigin-RevId: 410271639 --- .../robolectric/android/controller/ActivityController.java | 6 +++--- .../src/main/java/org/robolectric/shadows/_Activity_.java | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/shadows/framework/src/main/java/org/robolectric/android/controller/ActivityController.java b/shadows/framework/src/main/java/org/robolectric/android/controller/ActivityController.java index 1105f5d39f3..2bbaf4e6780 100644 --- a/shadows/framework/src/main/java/org/robolectric/android/controller/ActivityController.java +++ b/shadows/framework/src/main/java/org/robolectric/android/controller/ActivityController.java @@ -7,7 +7,6 @@ import static org.robolectric.util.reflector.Reflector.reflector; import android.app.Activity; -import android.app.ActivityThread; import android.app.Application; import android.app.Instrumentation; import android.content.ComponentName; @@ -502,8 +501,9 @@ public ActivityController recreate() { } } - private static Instrumentation getInstrumentation() { - return ((ActivityThread) RuntimeEnvironment.getActivityThread()).getInstrumentation(); + // Get the Instrumentation object scoped to the Activity. + private Instrumentation getInstrumentation() { + return _component_.getInstrumentation(); } /** Accessor interface for android.app.Activity.NonConfigurationInstances's internals. */ diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/_Activity_.java b/shadows/framework/src/main/java/org/robolectric/shadows/_Activity_.java index 450782f8c35..e327bcec8a5 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/_Activity_.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/_Activity_.java @@ -372,4 +372,7 @@ void setVoiceInteractor( @Accessor("mConfigChangeFlags") void setConfigChangeFlags(int value); + + @Accessor("mInstrumentation") + Instrumentation getInstrumentation(); }