diff --git a/espresso/CHANGELOG.md b/espresso/CHANGELOG.md index a0993d9c1..f444d0b08 100644 --- a/espresso/CHANGELOG.md +++ b/espresso/CHANGELOG.md @@ -20,6 +20,8 @@ The following artifacts were released: **New Features** +* Allow ResettingStubberImpl to be called from any thread. + **Breaking Changes** **API Changes** diff --git a/espresso/intents/java/androidx/test/espresso/intent/ResettingStubberImpl.java b/espresso/intents/java/androidx/test/espresso/intent/ResettingStubberImpl.java index 6f540f39d..4ee1d0103 100644 --- a/espresso/intents/java/androidx/test/espresso/intent/ResettingStubberImpl.java +++ b/espresso/intents/java/androidx/test/espresso/intent/ResettingStubberImpl.java @@ -23,20 +23,19 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; -import android.os.Looper; import android.util.Pair; import androidx.test.platform.app.InstrumentationRegistry; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; +import java.util.concurrent.CopyOnWriteArrayList; import org.hamcrest.Matcher; /** Implementation of {@link ResettingStubber} */ public final class ResettingStubberImpl implements ResettingStubber { - // Should be accessed only from main thread private List, ActivityResultFunction>> intentResponsePairs = - new ArrayList, ActivityResultFunction>>(); + new CopyOnWriteArrayList, ActivityResultFunction>>(); private PackageManager packageManager; private boolean isInitialized; @@ -55,7 +54,6 @@ public boolean isInitialized() { @Override public void reset() { - checkMain(); intentResponsePairs.clear(); isInitialized = false; } @@ -70,15 +68,13 @@ public void setActivityResultFunctionForIntent( Matcher matcher, ActivityResultFunction result) { checkState(isInitialized, "ResettingStubber must be initialized before calling this method"); checkNotNull(matcher); - checkMain(); intentResponsePairs.add(new Pair, ActivityResultFunction>(matcher, result)); } @Override - public ActivityResult getActivityResultForIntent(Intent intent) { + public synchronized ActivityResult getActivityResultForIntent(Intent intent) { checkState(isInitialized, "ResettingStubber must be initialized before calling this method"); checkNotNull(intent); - checkMain(); ListIterator, ActivityResultFunction>> reverseIterator = intentResponsePairs.listIterator(intentResponsePairs.size()); while (reverseIterator.hasPrevious()) { @@ -99,14 +95,10 @@ ResolvedIntent resolveIntent(Intent intent) { // why-does-the-flag-specified-in-queryintentactivities-method-is-set-to-zero and // http://developer.android.com/training/basics/intents/sending.html List resolveInfos = packageManager.queryIntentActivities(intent, 0); - if (null == resolveInfos) { + if (resolveInfos == null) { // Gingerbread returns null here if nothing resolves, other APIs return an empty list. resolveInfos = new ArrayList(); } return new ResolvedIntentImpl(intent, resolveInfos); } - - private static void checkMain() { - checkState(Looper.myLooper() == Looper.getMainLooper(), "Must be called on main thread."); - } }