package com.beaker.reciperoulette; import android.content.res.Resources; import android.view.View; import androidx.recyclerview.widget.RecyclerView; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; /** * https://gist.githubusercontent.com/baconpat/8405a88d04bd1942eb5e430d33e4faa2/raw/509dff7a4ef0fa9b2e302293822b54e11a35028d/RecycleViewMatcher.java */ public class RecyclerViewMatcher { private final int recyclerViewId; public RecyclerViewMatcher(int recyclerViewId) { this.recyclerViewId = recyclerViewId; } public Matcher atPosition(final int position) { return atPositionOnView(position, -1); } public Matcher atPositionOnView(final int position, final int targetViewId) { return new TypeSafeMatcher() { 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)", recyclerViewId); } } description.appendText("RecyclerView with id: " + idDescription + " at position: " + position); } public boolean matchesSafely(View view) { this.resources = view.getResources(); if (childView == null) { RecyclerView recyclerView = view.getRootView().findViewById(recyclerViewId); if (recyclerView != null && recyclerView.getId() == recyclerViewId) { RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position); if (viewHolder != null) { childView = viewHolder.itemView; } } else { return false; } } if (targetViewId == -1) { return view == childView; } else { View targetView = childView.findViewById(targetViewId); return view == targetView; } } }; } }