Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
110 lines (91 sloc) 4.15 KB
package com.github.johnnysc.tutor.presentation.core
import android.content.res.Resources
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import org.hamcrest.Description
import org.hamcrest.TypeSafeMatcher
/**
* @author Asatryan on 21.06.19
*/
class RecyclerViewMatcher(private val recyclerViewId: Int) {
fun atPosition(position: Int) = atPositionOnView(position, -1)
private fun atPositionOnView(position: Int, targetViewId: Int) =
object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null
override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
idDescription = try {
this.resources!!.getResourceName(recyclerViewId)
} catch (e: Resources.NotFoundException) {
String.format("%s (resource name not found)", recyclerViewId)
}
}
description.appendText("RecyclerView with id: $idDescription at position: $position")
}
override fun matchesSafely(view: View): Boolean {
this.resources = view.resources
if (childView == null) {
val recyclerView = view.rootView.findViewById(recyclerViewId) as RecyclerView
if (recyclerView.id == recyclerViewId) {
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
if (viewHolder != null) {
childView = viewHolder.itemView
}
} else {
return false
}
}
return if (targetViewId == -1) {
view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
view === targetView
}
}
}
}
class ListItemRecyclerViewMatcher(private val recyclerViewId: Int, private val childIds: List<Int>) {
fun atPosition(position: Int) = atPositionOnView(position, -1)
private fun atPositionOnView(position: Int, targetViewId: Int) =
object : TypeSafeMatcher<View>() {
var resources: Resources? = null
var childView: View? = null
override fun describeTo(description: Description) {
var idDescription = Integer.toString(recyclerViewId)
if (this.resources != null) {
idDescription = try {
this.resources!!.getResourceName(recyclerViewId)
} catch (e: Resources.NotFoundException) {
String.format("%s (resource name not found)", recyclerViewId)
}
}
description.appendText("RecyclerView with id: $idDescription at position: $position")
}
override fun matchesSafely(view: View): Boolean {
this.resources = view.resources
if (childView == null) {
val recyclerView = view.rootView.findViewById(recyclerViewId) as RecyclerView
if (recyclerView.id == recyclerViewId) {
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
if (viewHolder != null) {
childView = viewHolder.itemView
childIds.forEach { item ->
val v = childView?.findViewById<View>(item)
v?.let { childView = v }
}
}
} else {
return false
}
}
return if (targetViewId == -1) {
view === childView
} else {
val targetView = childView!!.findViewById<View>(targetViewId)
view === targetView
}
}
}
}
You can’t perform that action at this time.