Skip to content

Commit

Permalink
Accept repeated views (#38)
Browse files Browse the repository at this point in the history
* Created test to reproduce the issue in #37

#37

* Fixed failing test with firstOf matcher

* Create failing test to reproduce wrong behavior with firstOf

* Fixed failing test by using allOf matcher

By including the isDisplayed condition as a requirement for onView, we filter any views that matches the text but are not displayed.
A bit tricky though.

* Renamed firstOf to firstViewOf

Because the usage is different from allOf. It's not a matcher filter, but a view filter.

* Remove an unneeded parenthesis

* Handle assertDisplayed for repeated texts by string resource

* Added precondition for gone view in repeated views test
  • Loading branch information
Sloy authored Apr 4, 2017
1 parent 535108e commit 47f0b32
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static android.support.test.espresso.matcher.ViewMatchers.withHint;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.schibsted.spain.barista.custom.HelperMatchers.firstViewOf;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.fail;

Expand All @@ -29,14 +31,14 @@ public static void assertDisplayed(int id) {
if (isIdResource(id)) {
onView(withId(id)).check(matches(isDisplayed()));
} else if (isStringResource(id)) {
onView(withText(id)).check(matches(isDisplayed()));
onView(firstViewOf(allOf(withText(id), isDisplayed()))).check(matches(isDisplayed()));
} else {
throw new BaristaArgumentTypeException();
}
}

public static void assertDisplayed(String text) {
onView(withText(text)).check(matches(isDisplayed()));
onView(firstViewOf(allOf(withText(text), isDisplayed()))).check(matches(isDisplayed()));
}

public static void assertNotExist(int id) {
Expand Down Expand Up @@ -128,4 +130,4 @@ private static boolean isIdResource(int id) {
private static boolean isStringResource(int id) {
return RESOURCE_TYPE_CHECKER.isStringResource(id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,24 @@ public void describeTo(final Description description) {
}
};
}

public static <T> Matcher<T> firstViewOf(final Matcher<T> matcher) {
return new BaseMatcher<T>() {
private boolean isFirst = true;

@Override
public boolean matches(final Object item) {
if (isFirst && matcher.matches(item)) {
isFirst = false;
return true;
}
return false;
}

@Override
public void describeTo(final Description description) {
description.appendText("should return first matching item");
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public void checkVisibleViews_breaksWhenNeeded() {
}
}

@Test
public void checkVisible_withRepeatedViews() throws Exception {
assertNotDisplayed(R.id.repeated_view_1_gone);

assertDisplayed("Repeated");
assertDisplayed(R.string.repeated);
}

@Test
public void checkInvisibleViews() {
assertNotDisplayed(R.id.invisible_view);
Expand Down
19 changes: 19 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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:text="@string/repeated"
/>
<TextView
android:id="@+id/repeated_view_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/repeated"
/>
<TextView
android:id="@+id/repeated_view_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/repeated"
/>

<TextView
android:id="@+id/invisible_view"
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<resources>
<string name="app_name">Barista</string>
<string name="hello_world">Hello world!</string>
<string name="repeated">Repeated</string>
<string name="im_invisible">I\'m invisible!</string>
<string name="unknown">Unknown</string>
<string name="enabled_button">Enabled button</string>
Expand Down

0 comments on commit 47f0b32

Please sign in to comment.