Код: Выделить всё
private class ViewPropertyChangeCallback(private val matcher: Matcher, private val view: View) : IdlingResource, ViewTreeObserver.OnDrawListener {
private lateinit var callback: IdlingResource.ResourceCallback
private var matched = false
override fun getName() = "View property change callback"
override fun isIdleNow() = matched
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback) {
this.callback = callback
}
override fun onDraw() {
matched = matcher.matches(view)
callback.onTransitionToIdle()
}
}
fun waitUntil(matcher: Matcher): ViewAction = object : ViewAction {
override fun getConstraints(): Matcher {
return Matchers.any(View::class.java)
}
override fun getDescription(): String {
return StringDescription().let {
matcher.describeTo(it)
"wait until: $it"
}
}
override fun perform(uiController: UiController, view: View) {
if (!matcher.matches(view)) {
ViewPropertyChangeCallback(matcher, view).run {
try {
IdlingRegistry.getInstance().register(this)
view.viewTreeObserver.addOnDrawListener(this)
uiController.loopMainThreadUntilIdle()
} finally {
view.viewTreeObserver.removeOnDrawListener(this)
IdlingRegistry.getInstance().unregister(this)
}
}
}
}
}
fun waitForElement(matcher: Matcher) {
onView(isRoot())
.perform(waitUntil(hasDescendant(matcher)))
}
fun waitElement(element: Matcher, matching: Matcher = isDisplayed()) {
waitForElement(Matchers.allOf(element, matching))
}
Код: Выделить всё
fun waitForButton() {
waitElement(withId(R.id.buttonId))
}
например:
Код: Выделить всё
private val buttonElement = KButton {withId(R.id.buttonId)}
fun waitForButton() {
buttonElement.waitElement()
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... with-kakao