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

Fixed visibility assertions #316

Merged
merged 4 commits into from
Aug 19, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ object BaristaVisibilityAssertions {

@JvmStatic
fun assertDisplayed(@IdRes viewId: Int, text: String) {
viewId.resourceMatcher().assertAny(withText(text))
viewId.resourceMatcher().assertAny(allOf(isDisplayed(), withText(text)))
}

@JvmStatic
fun assertDisplayed(@IdRes viewId: Int, @StringRes stringId: Int) {
viewId.resourceMatcher().assertAny(withText(stringId))
viewId.resourceMatcher().assertAny(allOf(isDisplayed(), withText(stringId)))
}

@JvmStatic
Expand Down Expand Up @@ -75,12 +75,12 @@ object BaristaVisibilityAssertions {

@JvmStatic
fun assertNotDisplayed(@IdRes viewId: Int, text: String) {
viewId.resourceMatcher().assertAny(not(withText(text)))
viewId.resourceMatcher().assertAny(not(allOf(isDisplayed(), withText(text))))
}

@JvmStatic
fun assertNotDisplayed(@IdRes viewId: Int, @StringRes stringId: Int) {
viewId.resourceMatcher().assertAny(not(withText(stringId)))
viewId.resourceMatcher().assertAny(not(allOf(isDisplayed(), withText(stringId))))
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,39 @@ public void checkNotDisplayed_withGoneView() {

@Test
public void checkDisplayedViews_failsWhenInvisible() {
Throwable thrown = catchThrowable(() -> assertDisplayed(R.id.invisible_view));

spyFailureHandlerRule.assertEspressoFailures(1);
assertThat(thrown).isInstanceOf(BaristaException.class)
Throwable thrown1 = catchThrowable(() -> assertDisplayed(R.id.invisible_view));
assertThat(thrown1).isInstanceOf(BaristaException.class)
.hasMessage("View (with id: com.schibsted.spain.barista.sample:id/invisible_view) "
+ "didn't match condition (is displayed on the screen to the user)");

Throwable thrown2 = catchThrowable(() -> assertDisplayed(R.string.im_invisible)); // test fails
assertThat(thrown2).isInstanceOf(BaristaException.class)
.hasMessageMatching("View \\(with string from resource id: <(\\d+)>\\[im_invisible\\] value: I'm invisible!\\) "
+ "didn't match condition \\(is displayed on the screen to the user\\)");

Throwable thrown3 = catchThrowable(() -> assertDisplayed("I'm invisible!"));
assertThat(thrown3).isInstanceOf(BaristaException.class)
.hasMessage("View (with text: is \"I'm invisible!\") "
+ "didn't match condition (is displayed on the screen to the user)");

spyFailureHandlerRule.assertEspressoFailures(3);
}

@Test
public void checkDisplayed_failsWhenNotExists() {
Throwable thrown = catchThrowable(() -> assertDisplayed(R.string.not_exists));

spyFailureHandlerRule.assertEspressoFailures(1);
Throwable thrown = catchThrowable(() -> assertDisplayed(R.id.no));
assertThat(thrown).isInstanceOf(BaristaException.class)
.hasMessageContaining("No view matching (with string from resource id")
.hasMessageContaining("[not_exists] value: Not exists) was found");
.hasMessageContaining("No view matching (with id: com.schibsted.spain.barista.sample:id/no) was found");

Throwable thrown2 = catchThrowable(() -> assertDisplayed(R.string.not_exists));
assertThat(thrown2).isInstanceOf(BaristaException.class)
.hasMessageMatching("No view matching \\(with string from resource id: <(\\d+)>\\[not_exists\\] value: Not exists\\) was found");

Throwable thrown3 = catchThrowable(() -> assertDisplayed("Not exists"));
assertThat(thrown3).isInstanceOf(BaristaException.class)
.hasMessageContaining("No view matching (with text: is \"Not exists\") was found");

spyFailureHandlerRule.assertEspressoFailures(3);
}

@Test
Expand All @@ -81,21 +98,46 @@ public void checkDisplayedIdAndText() {

@Test
public void checkDisplayedIdAndText_failsWhenTextIsNotTheExpected() {
Throwable thrown = catchThrowable(() -> assertDisplayed(R.id.visible_view, "This is not the text you are looking for"));

spyFailureHandlerRule.assertEspressoFailures(1);
Throwable thrown = catchThrowable(() -> assertDisplayed(R.id.visible_view, R.string.not_exists));
assertThat(thrown).isInstanceOf(BaristaException.class)
.hasMessageMatching("View \\(with id: com.schibsted.spain.barista.sample:id/visible_view\\) didn't match condition "
+ "\\(\\(is displayed on the screen to the user "
+ "and with string from resource id: <(\\d+)>\\[not_exists\\] value: Not exists\\)\\)");

Throwable thrown2 = catchThrowable(() -> assertDisplayed(R.id.visible_view, "This is not the text you are looking for"));
assertThat(thrown2).isInstanceOf(BaristaException.class)
.hasMessage("View (with id: com.schibsted.spain.barista.sample:id/visible_view) didn't match condition "
+ "(with text: is \"This is not the text you are looking for\")");
+ "((is displayed on the screen to the user and with text: is \"This is not the text you are looking for\"))");

spyFailureHandlerRule.assertEspressoFailures(2);
}

@Test
public void checkDisplayedIdAndText_failsWhenViewDoesNotExist() {
Throwable thrown = catchThrowable(() -> assertDisplayed(R.id.not_exists, "This is not the text you are looking for"));
public void checkDisplayedIdAndText_failsWhenInvisible() {
Throwable thrown = catchThrowable(() -> assertDisplayed(R.id.invisible_view, R.string.im_invisible));
assertThat(thrown).isInstanceOf(BaristaException.class)
.hasMessageMatching("View \\(with id: com.schibsted.spain.barista.sample:id/invisible_view\\) didn't match condition "
+ "\\(\\(is displayed on the screen to the user and with string from resource id: <(\\d+)>\\)\\)");

spyFailureHandlerRule.assertEspressoFailures(1);
Throwable thrown2 = catchThrowable(() -> assertDisplayed(R.id.invisible_view, "I'm invisible!"));
assertThat(thrown2).isInstanceOf(BaristaException.class)
.hasMessage("View (with id: com.schibsted.spain.barista.sample:id/invisible_view) didn't match condition "
+ "((is displayed on the screen to the user and with text: is \"I'm invisible!\"))");

spyFailureHandlerRule.assertEspressoFailures(2);
}

@Test
public void checkDisplayedIdAndText_failsWhenViewDoesNotExist() {
Throwable thrown = catchThrowable(() -> assertDisplayed(R.id.not_exists, R.string.im_invisible));
assertThat(thrown).isInstanceOf(BaristaException.class)
.hasMessage("No view matching (with id: com.schibsted.spain.barista.sample:id/not_exists) was found");

Throwable thrown2 = catchThrowable(() -> assertDisplayed(R.id.not_exists, "This is not the text you are looking for"));
assertThat(thrown2).isInstanceOf(BaristaException.class)
.hasMessage("No view matching (with id: com.schibsted.spain.barista.sample:id/not_exists) was found");

spyFailureHandlerRule.assertEspressoFailures(2);
}

@Test
Expand Down Expand Up @@ -123,7 +165,7 @@ public void checkNotDisplayedIdAndText_failsWhenTextMatches() {
spyFailureHandlerRule.assertEspressoFailures(1);
assertThat(thrown).isInstanceOf(BaristaException.class)
.hasMessage("View (with id: com.schibsted.spain.barista.sample:id/visible_view) "
+ "didn't match condition (not with text: is \"Hello world!\")");
+ "didn't match condition (not (is displayed on the screen to the user and with text: is \"Hello world!\"))");
}

@Test
Expand Down