Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Commit

Permalink
Adds assertVisible(Matcher) and assertNotVisible(Matcher) methods (Ad…
Browse files Browse the repository at this point in the history
…evintaSpain#271)

* Added new visibility assertions with custom matchers

* Added tests for the assertVisible(CustomMatcher) and assertNotVisible(CustomMatcher) methods

* Added static imports to improve readability

* Fixed variable names

* Updated readme with the new methods
  • Loading branch information
danieldisu authored and Sloy committed Jan 18, 2019
1 parent 134aef8 commit 658ead8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,16 @@ assertDisplayed("Hello world");
assertDisplayed(R.string.hello_world);
assertDisplayed(R.id.button);
assertDisplayed(R.id.button, "Hello world")
// you can also pass custom matchers
assertDisplayed(withTagValue(is("tagName")))

// ...or not?
assertNotDisplayed("Hello world");
assertNotDisplayed(R.string.hello_world);
assertNotDisplayed(R.id.button);
assertNotDisplayed(R.id.button, "Hello world")
// you can also pass custom matchers
assertNotDisplayed(withTagValue(is("tagName")))

// Is this view enabled?
assertEnabled("Hello world");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ import android.support.annotation.IdRes
import android.support.annotation.StringRes
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.assertion.ViewAssertions.doesNotExist
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.espresso.matcher.ViewMatchers.isDisplayed
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.test.espresso.matcher.ViewMatchers.withText
import android.view.View
import com.schibsted.spain.barista.internal.assertAny
import com.schibsted.spain.barista.internal.matcher.TextColorMatcher
import com.schibsted.spain.barista.internal.util.resourceMatcher
import org.hamcrest.CoreMatchers.containsString
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.not

object BaristaVisibilityAssertions {

@JvmStatic
fun assertDisplayed(viewMatcher: Matcher<View>) {
viewMatcher.assertAny(isDisplayed())
}

@JvmStatic
fun assertDisplayed(viewId: Int) {
viewId.resourceMatcher().assertAny(isDisplayed())
Expand Down Expand Up @@ -55,6 +64,12 @@ object BaristaVisibilityAssertions {
withText(text).assertAny(not(isDisplayed()))
}

@JvmStatic
fun assertNotDisplayed(viewMatcher: Matcher<View>) {
viewMatcher.assertAny(not(isDisplayed()))
}


@JvmStatic
fun assertNotDisplayed(@IdRes viewId: Int, text: String) {
viewId.resourceMatcher().assertAny(not(withText(text)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.matcher.ViewMatchers.withTagValue;
import static com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertDisplayed;
import static com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertNotDisplayed;
import static com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertNotExist;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.ThrowableAssert.catchThrowable;
import static org.hamcrest.CoreMatchers.is;

@RunWith(AndroidJUnit4.class)
public class VisibilityAssertionsTest {
Expand Down Expand Up @@ -84,7 +86,7 @@ public void checkDisplayedIdAndText_failsWhenTextIsNotTheExpected() {
spyFailureHandlerRule.assertEspressoFailures(1);
assertThat(thrown).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\")");
+ "(with text: is \"This is not the text you are looking for\")");
}

@Test
Expand Down Expand Up @@ -150,4 +152,69 @@ public void checkNotExist_failsWhenViewExists() {
assertThat(thrown).isInstanceOf(AssertionError.class)
.hasMessageContaining("View is present in the hierarchy");
}

@Test
public void assertVisibleWithCustomMatcher_Fails_WhenTheMatcherDoesNotFindTheGivenPredicate() {
String aTagValueThatDoesNotExistsInTheView = "notPresentTagValue";
Throwable thrown = catchThrowable(() ->
assertDisplayed(withTagValue(is(aTagValueThatDoesNotExistsInTheView))));

spyFailureHandlerRule.assertEspressoFailures(1);
assertThat(thrown).isInstanceOf(BaristaException.class)
.hasMessage("No view matching (with tag value: is \"notPresentTagValue\") was found");
}

@Test
public void assertVisibleWithCustomMatcher_Fails_WhenTheMatcherDoesFindTheGivenPredicateButViewIsHidden() {
String nonExistingTag = "presentTagValueHidden";
Throwable thrown = catchThrowable(() ->
assertDisplayed(withTagValue(is(nonExistingTag))));

spyFailureHandlerRule.assertEspressoFailures(1);
assertThat(thrown).isInstanceOf(BaristaException.class)
.hasMessage("View (with tag value: is \"presentTagValueHidden\") didn't match condition (is displayed on the screen to the user)");
}

@Test
public void assertVisibleWithCustomMatcher_Succeeds_WhenTheMatcherMatchesAViewThatIsShown() {
String existingTag = "presentTagValue";

assertDisplayed(withTagValue(is(existingTag)));

spyFailureHandlerRule.assertNoEspressoFailures();
}

@Test
public void assertNotVisibleWithCustomMatcher_Fails_WhenTheMatcherDoesNotFindTheGivenPredicate() {
String nonExistingTag = "notPresentTagValue";

Throwable thrown = catchThrowable(() ->
assertNotDisplayed(withTagValue(is(nonExistingTag))));

spyFailureHandlerRule.assertEspressoFailures(1);
assertThat(thrown).isInstanceOf(BaristaException.class)
.hasMessage("No view matching (with tag value: is \"notPresentTagValue\") was found");
}

@Test
public void assertVisibleWithCustomMatcher_Succeeds_WhenTheMatcherMatchesAViewThatIsHidden() {
String hiddenTag = "presentTagValueHidden";

assertNotDisplayed(withTagValue(is(hiddenTag)));

spyFailureHandlerRule.assertNoEspressoFailures();
}

@Test
public void assertVisibleWithCustomMatcher_Fails_WhenTheMatcherMatchesAViewThatIsNotHidden() {
String shownTag = "presentTagValue";

Throwable thrown = catchThrowable(() ->
assertNotDisplayed(withTagValue(is(shownTag))));

spyFailureHandlerRule.assertEspressoFailures(1);
String expectedMessage =
"View (with tag value: is \"presentTagValue\") didn't match condition (not is displayed on the screen to the user)";
assertThat(thrown).isInstanceOf(BaristaException.class).hasMessage(expectedMessage);
}
}
2 changes: 2 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
android:id="@+id/visible_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="presentTagValue"
android:text="@string/hello_world"/>
<TextView
android:id="@+id/repeated_view_1_gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:tag="presentTagValueHidden"
android:text="@string/repeated"
/>
<TextView
Expand Down

0 comments on commit 658ead8

Please sign in to comment.