Permalink
Browse files

created RecyclerViewMatcher for more generic solution

  • Loading branch information...
dannyroa committed May 10, 2015
1 parent b1c52f7 commit b67a9cced3d6b63656dfa31134d44b56245f793b
@@ -0,0 +1,70 @@
+package com.dannyroa.espresso_samples.recyclerview;
+
+import android.content.res.Resources;
+import android.support.v7.widget.RecyclerView;
+import android.view.View;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+/**
+ * Created by dannyroa on 5/10/15.
+ */
+public class RecyclerViewMatcher {
+ private final int recyclerViewId;
+
+ public RecyclerViewMatcher(int recyclerViewId) {
+ this.recyclerViewId = recyclerViewId;
+ }
+
+ public Matcher<View> atPosition(final int position) {
+ return atPositionOnView(position, -1);
+ }
+
+ public Matcher<View> atPositionOnView(final int position, final int targetViewId) {
+
+ return new TypeSafeMatcher<View>() {
+ Resources resources = null;
+ View childView;
+
+ public void describeTo(Description description) {
+ String idDescription = Integer.toString(recyclerViewId);
+ if (this.resources != null) {
+ try {
+ idDescription = this.resources.getResourceName(recyclerViewId);
+ } catch (Resources.NotFoundException var4) {
+ idDescription = String.format("%s (resource name not found)",
+ new Object[] { Integer.valueOf
+ (recyclerViewId) });
+ }
+ }
+
+ description.appendText("with id: " + idDescription);
+ }
+
+ public boolean matchesSafely(View view) {
+
+ this.resources = view.getResources();
+
+ if (childView == null) {
+ RecyclerView recyclerView =
+ (RecyclerView) view.getRootView().findViewById(recyclerViewId);
+ if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
+ childView = recyclerView.getChildAt(position);
+ }
+ else {
+ return false;
+ }
+ }
+
+ if (targetViewId == -1) {
+ return view == childView;
+ } else {
+ View targetView = childView.findViewById(targetViewId);
+ return view == targetView;
+ }
+
+ }
+ };
+ }
+}
@@ -6,7 +6,10 @@
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
+import static android.support.test.espresso.matcher.ViewMatchers.withText;
+import static com.dannyroa.espresso_samples.recyclerview.TestUtils.withRecyclerView;
/**
* Created by dannyroa on 5/8/15.
@@ -23,17 +26,24 @@ public RecyclerViewTest() {
getActivity();
}
+ public void testItemClick() {
+
+ onView(withRecyclerView(R.id.recycler_view).atPosition(1)).perform(click());
+
+ onView(withId(R.id.team_name)).check(matches(isDisplayed()));
+
+ }
+
public void testFollowButtonClick() {
onView(withId(R.id.recycler_view)).perform(TestUtils.actionOnItemViewAtPosition(1,
R.id.follow_button,
click()));
-
String followingText =
InstrumentationRegistry.getTargetContext().getString(R.string.following);
- onView(withId(R.id.recycler_view)).check(matches(TestUtils.withTextAtRecycleViewPosition(1,
- R.id.follow_button,
- followingText)));
+ onView(withRecyclerView(R.id.recycler_view).atPositionOnView(1, R.id.follow_button)).check(matches(withText(followingText)));
+
}
+
}
@@ -4,13 +4,10 @@
import android.support.test.espresso.PerformException;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
-import android.support.test.espresso.matcher.BoundedMatcher;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.espresso.util.HumanReadables;
import android.support.v7.widget.RecyclerView;
import android.view.View;
-import android.widget.TextView;
-import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
@@ -103,31 +100,10 @@ public void perform(UiController uiController, View view) {
}
}
- public static Matcher<View> withTextAtRecycleViewPosition(final int position,
- @IdRes final int viewId,
- final Matcher<String> stringMatcher) {
- return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
- public void describeTo(Description description) {
- description.appendText("with text: ");
- stringMatcher.describeTo(description);
- }
-
- public boolean matchesSafely(RecyclerView recyclerView) {
- TextView targetView =
- (TextView) recyclerView.getChildAt(position).findViewById(viewId);
+ public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {
- if (targetView != null) {
- return stringMatcher.matches(targetView.getText().toString());
- }
- return false;
- }
- };
+ return new RecyclerViewMatcher(recyclerViewId);
}
- public static Matcher<View> withTextAtRecycleViewPosition(int position,
- @IdRes final int viewId,
- String text) {
- return withTextAtRecycleViewPosition(position, viewId, Matchers.is(text));
- }
}

0 comments on commit b67a9cc

Please sign in to comment.