Last active
February 11, 2020 14:17
-
-
Save NinoDLC/6dc283456c92a9daa1c57d6b834642cb to your computer and use it in GitHub Desktop.
This file contains hidden or 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.view.View; | |
| import androidx.annotation.IdRes; | |
| import androidx.annotation.IntRange; | |
| import androidx.annotation.NonNull; | |
| import androidx.annotation.Nullable; | |
| import androidx.recyclerview.widget.RecyclerView; | |
| import androidx.test.espresso.NoMatchingViewException; | |
| import androidx.test.espresso.ViewAssertion; | |
| import androidx.test.espresso.matcher.ViewMatchers; | |
| import org.hamcrest.Matcher; | |
| public class RecyclerViewItemAssertion implements ViewAssertion { | |
| @IntRange(from = 0) | |
| private final int position; | |
| @IdRes | |
| private final int viewId; | |
| @NonNull | |
| private final Matcher<View> matcher; | |
| public RecyclerViewItemAssertion(@IntRange(from = 0) int position, @IdRes int viewId, @NonNull Matcher<View> matcher) { | |
| this.viewId = viewId; | |
| this.position = position; | |
| this.matcher = matcher; | |
| } | |
| @Override | |
| public void check(View view, @Nullable NoMatchingViewException noViewFoundException) { | |
| if (noViewFoundException != null) { | |
| throw noViewFoundException; | |
| } | |
| if (!(view instanceof RecyclerView)) { | |
| throw new IllegalStateException("The asserted view is not RecyclerView"); | |
| } | |
| RecyclerView recyclerView = (RecyclerView) view; | |
| RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForLayoutPosition(position); | |
| if (viewHolder == null) { | |
| throw new IllegalStateException("No ViewHolder found for layout position : " + position); | |
| } | |
| View childView = viewHolder.itemView.findViewById(viewId); | |
| if (childView == null) { | |
| throw new IllegalStateException("No view found with id : " + recyclerView.getResources().getResourceEntryName(viewId)); | |
| } | |
| ViewMatchers.assertThat(childView, matcher); | |
| } | |
| } | |
| // UTILISATION : | |
| /* onView(withId(R.id.meeting_rv)).check( | |
| new RecyclerViewItemAssertion( | |
| positionOnRecyclerView, | |
| R.id.meeting_item_tv_participants, | |
| withText(participants) | |
| ) | |
| ); | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment