Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ResettingStubberImpl to be called from any thread. #2328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions espresso/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ The following artifacts were released:

**New Features**

* Allow ResettingStubberImpl to be called from any thread.

**Breaking Changes**

**API Changes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair<Matcher<Intent>, ActivityResultFunction>> intentResponsePairs =
new ArrayList<Pair<Matcher<Intent>, ActivityResultFunction>>();
new CopyOnWriteArrayList<Pair<Matcher<Intent>, ActivityResultFunction>>();

private PackageManager packageManager;
private boolean isInitialized;
Expand All @@ -55,7 +54,6 @@ public boolean isInitialized() {

@Override
public void reset() {
checkMain();
intentResponsePairs.clear();
isInitialized = false;
}
Expand All @@ -70,15 +68,13 @@ public void setActivityResultFunctionForIntent(
Matcher<Intent> matcher, ActivityResultFunction result) {
checkState(isInitialized, "ResettingStubber must be initialized before calling this method");
checkNotNull(matcher);
checkMain();
intentResponsePairs.add(new Pair<Matcher<Intent>, 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<Pair<Matcher<Intent>, ActivityResultFunction>> reverseIterator =
intentResponsePairs.listIterator(intentResponsePairs.size());
while (reverseIterator.hasPrevious()) {
Expand All @@ -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<ResolveInfo> 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<ResolveInfo>();
}
return new ResolvedIntentImpl(intent, resolveInfos);
}

private static void checkMain() {
checkState(Looper.myLooper() == Looper.getMainLooper(), "Must be called on main thread.");
}
}
Loading