Created
October 20, 2017 05:55
-
-
Save bapspatil/1bcccdc7226fb79dfcca1ce7927bbe94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 bapspatil | |
*/ | |
/* Add the following helper methods in the test file: | |
// Referencing the RecyclerView needed | |
public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) { | |
return new RecyclerViewMatcher(recyclerViewId); | |
} | |
// Tapping on a specific position that first scrolls and then taps | |
public static void tapRecyclerViewItem(int recyclerViewId, int position) { | |
onView(withId(recyclerViewId)).perform(scrollToPosition(position)); | |
onView(withRecyclerView(recyclerViewId).atPosition(position)).perform(click()); | |
} | |
*/ | |
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.findViewHolderForAdapterPosition(position).itemView; | |
} | |
else { | |
return false; | |
} | |
} | |
if (targetViewId == -1) { | |
return view == childView; | |
} else { | |
View targetView = childView.findViewById(targetViewId); | |
return view == targetView; | |
} | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment