-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewActions.kt
62 lines (52 loc) · 2.1 KB
/
ViewActions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.tazkiyatech.utils.espresso
import android.view.View
import androidx.test.espresso.PerformException
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.ViewInteraction
import androidx.test.espresso.util.HumanReadables
import org.hamcrest.Matcher
import org.hamcrest.Matchers
import java.util.concurrent.TimeoutException
object ViewActions {
/**
* Creates a [ViewAction] that waits up to [timeout] milliseconds
* for the [View] that is the subject of this [ViewAction] to match the given [Matcher].
*
* Usage example:
* ```
* onView(withId(R.id.someView)).perform(waitForMatch(isDisplayed()))
* ```
*
* @param matcher The [Matcher] to wait for.
* @param timeout The length of time in milliseconds to wait for.
* @return A [ViewAction] that waits up to [timeout] milliseconds
* for the [View] that is the subject of this [ViewAction] to match the given [Matcher].
*/
fun waitForMatch(matcher: Matcher<View>, timeout: Long = 3000L): ViewAction {
return WaitForMatchAction(matcher, timeout)
}
private class WaitForMatchAction(
private val matcher: Matcher<View>,
private val timeout: Long
) : ViewAction {
override fun getConstraints(): Matcher<View> {
return Matchers.any(View::class.java)
}
override fun getDescription(): String {
return "wait up to $timeout milliseconds for the view to match $matcher"
}
override fun perform(uiController: UiController, view: View) {
val endTime = System.currentTimeMillis() + timeout
do {
if (matcher.matches(view)) return
uiController.loopMainThreadForAtLeast(50)
} while (System.currentTimeMillis() < endTime)
throw PerformException.Builder()
.withActionDescription(description)
.withCause(TimeoutException("Waited $timeout milliseconds"))
.withViewDescription(HumanReadables.describe(view))
.build()
}
}
}