Skip to content
Merged
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 @@ -23,6 +23,8 @@ The following artifacts were released:

**New Features**

* Add `doesNotHaveFlag` and `doesNotHaveFlags` matchers to `IntentMatchers`.

**Breaking Changes**

**API Changes**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ package androidx.test.espresso.intent.matcher {
method public static org.hamcrest.Matcher<android.content.Intent!>! anyIntent();
method public static org.hamcrest.Matcher<android.content.Intent!> doesNotHaveExtraWithKey(String);
method public static org.hamcrest.Matcher<android.content.Intent!> doesNotHaveExtraWithKey(org.hamcrest.Matcher<java.lang.String!>);
method public static org.hamcrest.Matcher<android.content.Intent!>! doesNotHaveFlag(int);
method public static org.hamcrest.Matcher<android.content.Intent!>! doesNotHaveFlags(int);
method public static org.hamcrest.Matcher<android.content.Intent!>! doesNotHaveFlags(int...!);
method public static org.hamcrest.Matcher<android.content.Intent!>! filterEquals(android.content.Intent!);
method public static org.hamcrest.Matcher<android.content.Intent!>! hasAction(String!);
method public static org.hamcrest.Matcher<android.content.Intent!>! hasAction(org.hamcrest.Matcher<java.lang.String!>!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,34 @@ public boolean matchesSafely(Intent intent) {
};
}

public static Matcher<Intent> doesNotHaveFlag(int flag) {
return doesNotHaveFlags(flag);
}

public static Matcher<Intent> doesNotHaveFlags(int... flags) {
int allFlags = 0;
for (int i : flags) {
allFlags |= i;
}
return doesNotHaveFlags(allFlags);
}

public static Matcher<Intent> doesNotHaveFlags(final int flags) {

return new TypeSafeMatcher<Intent>() {
@Override
public void describeTo(Description description) {
description.appendText("does not have flags: " + Integer.toHexString(flags));
}

@Override
public boolean matchesSafely(Intent intent) {
int intentFlags = intent.getFlags();
return ((intentFlags & flags) == 0);
}
};
}

/** Matches an intent if it {@link Intent#filterEquals(Intent)} the expected intent. */
public static Matcher<Intent> filterEquals(Intent expectedIntent) {
return new TypeSafeMatcher<Intent>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.espresso.intent.matcher.BundleMatchers.hasEntry;
import static androidx.test.espresso.intent.matcher.IntentMatchers.doesNotHaveExtraWithKey;
import static androidx.test.espresso.intent.matcher.IntentMatchers.doesNotHaveFlag;
import static androidx.test.espresso.intent.matcher.IntentMatchers.doesNotHaveFlags;
import static androidx.test.espresso.intent.matcher.IntentMatchers.filterEquals;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasCategories;
Expand Down Expand Up @@ -473,6 +475,50 @@ public void hasFlagsWithCustomFlagsDoesNotMatch() {
assertFalse((hasFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | 8)).matches(intent));
}

@Test
public void doesNotHaveFlagsWithSingleFlag() {
Intent intent = new Intent();
assertTrue(doesNotHaveFlags(0).matches(intent));
assertTrue(doesNotHaveFlags(0).matches(new Intent().setFlags(1)));
assertTrue(doesNotHaveFlag(Intent.FLAG_GRANT_READ_URI_PERMISSION).matches(intent));
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
assertFalse(doesNotHaveFlag(Intent.FLAG_GRANT_READ_URI_PERMISSION).matches(intent));
assertFalse(doesNotHaveFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION).matches(intent));
}

@Test
public void doesNotHaveFlagsWithMultipleFlags() {
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION | Intent.FLAG_ACTIVITY_NO_HISTORY);
assertTrue(doesNotHaveFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS).matches(intent));
assertFalse(doesNotHaveFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION).matches(intent));
assertFalse(doesNotHaveFlags(Intent.FLAG_ACTIVITY_NO_HISTORY).matches(intent));
assertFalse(
doesNotHaveFlags(
Intent.FLAG_DEBUG_LOG_RESOLUTION | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
.matches(intent));
assertFalse(
doesNotHaveFlags(
Intent.FLAG_DEBUG_LOG_RESOLUTION, Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
.matches(intent));
assertTrue(
doesNotHaveFlags(
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS,
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)
.matches(intent));
}

@Test
public void doesNotHaveFlagsWithCustomFlags() {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION | 8);
assertTrue((doesNotHaveFlags(16)).matches(intent));
assertTrue((doesNotHaveFlags(Intent.FLAG_ACTIVITY_NO_HISTORY, 16)).matches(intent));
assertFalse((doesNotHaveFlags(8 | 2)).matches(intent));
assertFalse((doesNotHaveFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION, 4)).matches(intent));
assertFalse((doesNotHaveFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION, 8, 4)).matches(intent));
}

@Test
public void filterEqualsMatches() {
Intent intent =
Expand Down
Loading