-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use custom matchers, no need to creater one
- Loading branch information
Showing
1 changed file
with
20 additions
and
34 deletions.
There are no files selected for viewing
54 changes: 20 additions & 34 deletions
54
library/src/main/java/com/schibsted/spain/barista/internal/matcher/TextMatcher.kt
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
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)) | ||
) | ||
) | ||
} |