Gesture helpers
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
InstrumentationTestHelper is a Kotlin object that simulates touch gestures in instrumented tests. It builds
MotionEvent sequences and sends them with Instrumentation.sendPointerSync, obtained from
InstrumentationRegistry.getInstrumentation(). It therefore only works in instrumented tests (androidTest), and
must be called from the test thread, not from the main (UI) thread: sendPointerSync waits for the main thread to
process each event.
Functions
| Function | Description |
|---|---|
|
Sends an |
|
Two taps at the center of |
|
One-finger drag or scroll from |
|
Two-finger gesture: both pointers move linearly from their start to their end positions. Moving them apart zooms in, moving them together zooms out. |
Coordinates
drag and pinch take screen coordinates in pixels. Compute them from the view with getLocationOnScreen, as the
library’s instrumented tests do.
tap and doubleTap compute the center as ((v.left + v.right) / 2, (v.top + v.bottom) / 2). left, right,
top and bottom are relative to the view’s parent, so these helpers hit the view’s center only when its parent
starts at the window’s origin. For views nested deeper, prefer an Espresso click() or compute the screen position
yourself.
Event timing
-
draggenerates(|fromX - toX| / stepSize + |fromY - toY| / stepSize) / 2move events (integer division), so a smallerstepSizegives a smoother, slower drag. -
pinchsendsACTION_DOWNandACTION_POINTER_2_DOWN, thendurationMillis / 10ACTION_MOVEevents whose event times are spaced 10 ms apart, thenACTION_POINTER_2_UPandACTION_UP.
Example
Adapted from the library’s InstrumentationTestHelperTest:
import androidx.test.ext.junit.rules.activityScenarioRule
import com.irurueta.android.testutils.InstrumentationTestHelper
import org.junit.Rule
import org.junit.Test
class MyGestureTest {
@get:Rule
val activityScenarioRule = activityScenarioRule<MyActivity>()
@Test
fun gestures() {
var view: View? = null
activityScenarioRule.scenario.onActivity { activity ->
view = activity.findViewById(R.id.my_view)
}
val v = view!!
val xy = IntArray(2)
v.getLocationOnScreen(xy)
val centerX = xy[0] + v.width / 2
val centerY = xy[1] + v.height / 2
// drag from the center up to the top edge of the view
InstrumentationTestHelper.drag(centerX, centerY, centerX, xy[1])
// pinch: move two fingers apart horizontally
val distance = v.width / 6
InstrumentationTestHelper.pinch(
centerX - distance, centerY, centerX + distance, centerY,
centerX - 2 * distance, centerY, centerX + 2 * distance, centerY
)
InstrumentationTestHelper.tap(v)
InstrumentationTestHelper.doubleTap(v)
}
}