// 1. Define the custom ViewAction
class AppendTextAction(private val value: String) : ViewAction {
override fun getConstraints() = allOf(isDisplayed(), isAssignableFrom(TextView::class.java))
override fun perform(uiController: UiController, view: View) {
(view as TextView).apply { this.text = "${text}$value" }
uiController.loopMainThreadUntilIdle()
}
}
// 2. Extend UltronEspressoInteraction
fun <T> UltronEspressoInteraction<T>.appendText(text: String) = apply {
executeAction(
operationBlock = getInteractionActionBlock(AppendTextAction(text)),
name = "Append text '$text' to ${getInteractionMatcher()}",
description = "${interaction!!::class.simpleName} APPEND_TEXT to ${getInteractionMatcher()} during $timeoutMs ms",
)
}
// 3. Make it native for all matchers/interactions
fun Matcher<View>.appendText(text: String) = UltronEspressoInteraction(onView(this)).appendText(text)
fun ViewInteraction.appendText(text: String) = UltronEspressoInteraction(this).appendText(text)
fun DataInteraction.appendText(text: String) = UltronEspressoInteraction(this).appendText(text)
// Usage
withId(R.id.text_input).appendText("some text to append")