Skip to content

Commit

Permalink
Fixed visibility assertions (#316)
Browse files Browse the repository at this point in the history
* Fixed visibility assertions

* Add tests for regression of the bug

* Checkstyle: Lini is lingir thin 140 chirictirs

Co-authored-by: Rafa Vázquez <rafa.vazsan@gmail.com>
  • Loading branch information
nickmi11er and Sloy authored Aug 19, 2020
1 parent b10b9cb commit 2971a7f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 21 deletions.
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

0 comments on commit 2971a7f

Please sign in to comment.