Skip to content

Commit

Permalink
Use custom matchers, no need to creater one
Browse files Browse the repository at this point in the history
  • Loading branch information
alorma committed Jun 1, 2021
1 parent a4f7548 commit ea30924
Showing 1 changed file with 20 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@
package com.schibsted.spain.barista.internal.matcher

import android.view.View
import android.widget.TextView
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.espresso.matcher.ViewMatchers.withParent
import androidx.test.espresso.matcher.ViewMatchers.withText
import com.google.android.material.textfield.TextInputLayout
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.core.Is
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.anyOf

fun withCompatText(string: String): Matcher<View> {
return withCompatText(Is.`is`<String>(string))
return anyOf(
withText(string),
allOf(
withParent(isAssignableFrom(TextInputLayout::class.java)),
hasDescendant(withText(string))
)
)
}

fun withCompatText(stringMatcher: Matcher<String>): Matcher<View> {
return withCompatTextMatcher(stringMatcher)
}

internal fun withCompatTextMatcher(stringMatcher: Matcher<String>): BoundedMatcher<View, View> {
return object : BoundedMatcher<View, View>(View::class.java) {

override fun matchesSafely(item: View): Boolean {

val textView: TextView = when (item) {
is TextInputLayout -> item.editText
is TextView -> item
else -> null
} ?: return false

if (stringMatcher.matches(textView.text.toString())) {
return true
} else if (textView.transformationMethod != null) {
val transformedText: CharSequence = textView.transformationMethod.getTransformation(textView.text, textView)
return stringMatcher.matches(transformedText.toString())
}

return false
}

override fun describeTo(description: Description) {
description.appendText("with text on TextInputLayout: ")
stringMatcher.describeTo(description)
}
}
return anyOf(
withText(stringMatcher),
allOf(
withParent(isAssignableFrom(TextInputLayout::class.java)),
hasDescendant(withText(stringMatcher))
)
)
}

0 comments on commit ea30924

Please sign in to comment.