summaryrefslogtreecommitdiff
path: root/libraries/uiautomator-helpers/src/android/platform/uiautomator_helpers/BetterSwipe.kt
blob: d97a913137c6812d0555b4ffb66789e844ad3213 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.platform.uiautomator_helpers

import android.animation.TimeInterpolator
import android.graphics.Point
import android.graphics.PointF
import android.os.SystemClock
import android.os.SystemClock.sleep
import android.util.Log
import android.view.InputDevice
import android.view.MotionEvent
import android.view.MotionEvent.TOOL_TYPE_FINGER
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.google.common.truth.Truth.assertThat
import java.time.Duration
import java.time.temporal.ChronoUnit.MILLIS
import java.util.concurrent.atomic.AtomicInteger

private val DEFAULT_DURATION: Duration = Duration.of(500, MILLIS)
private val PAUSE_DURATION: Duration = Duration.of(250, MILLIS)
private val GESTURE_STEP = Duration.of(16, MILLIS)

/**
 * Allows fine control of swipes on the screen.
 *
 * Guarantees that all touches are dispatched, as opposed to [UiDevice] APIs, that might lose
 * touches in case of high load.
 *
 * It is possible to perform operation before the swipe finishes. Timestamp of touch events are set
 * according to initial time and duration.
 *
 * Example usage:
 * ```
 * val swipe = BetterSwipe.from(startPoint).to(intermediatePoint)
 *
 * assertThat(someUiState).isTrue();
 *
 * swipe.to(anotherPoint).release()
 * ```
 */
object BetterSwipe {

    private val lastPointerId = AtomicInteger(0)

    /** Starts a swipe from [start] at the current time. */
    @JvmStatic fun from(start: PointF) = Swipe(start)

    /** Starts a swipe from [start] at the current time. */
    @JvmStatic fun from(start: Point) = Swipe(PointF(start.x.toFloat(), start.y.toFloat()))

    /** Starts a swipe for each [starts] point at the current time. */
    @JvmStatic fun from(vararg starts: PointF) = Swipes(*starts)

    class Swipe internal constructor(start: PointF) {

        private val downTime = SystemClock.uptimeMillis()
        private val pointerId = lastPointerId.incrementAndGet()
        private var lastPoint: PointF = start
        private var lastTime: Long = downTime
        private var released = false

        init {
            log("Touch $pointerId started at $start")
            sendPointer(currentTime = downTime, action = MotionEvent.ACTION_DOWN, point = start)
        }

        /**
         * Swipes from the current point to [end] in [duration] using [interpolator] for the gesture
         * speed. Pass [FLING_GESTURE_INTERPOLATOR] for a fling-like gesture that may leave the
         * surface moving by inertia. Don't use it to drag objects to a precisely specified
         * position. [PRECISE_GESTURE_INTERPOLATOR] will result in a precise drag-like gesture not
         * triggering inertia.
         */
        @JvmOverloads
        fun to(
            end: PointF,
            duration: Duration = DEFAULT_DURATION,
            interpolator: TimeInterpolator = FLING_GESTURE_INTERPOLATOR
        ): Swipe {
            throwIfReleased()
            log(
                "Swiping from $lastPoint to $end in $duration " +
                    "using ${interpolator.javaClass.simpleName}"
            )
            lastTime = movePointer(duration = duration, from = lastPoint, to = end, interpolator)
            lastPoint = end
            return this
        }

        /**
         * Swipes from the current point to [end] in [duration] using [interpolator] for the gesture
         * speed. Pass [FLING_GESTURE_INTERPOLATOR] for a fling-like gesture that may leave the
         * surface moving by inertia. Don't use it to drag objects to a precisely specified
         * position. [PRECISE_GESTURE_INTERPOLATOR] will result in a precise drag-like gesture not
         * triggering inertia.
         */
        @JvmOverloads
        fun to(
            end: Point,
            duration: Duration = DEFAULT_DURATION,
            interpolator: TimeInterpolator = FLING_GESTURE_INTERPOLATOR
        ): Swipe {
            return to(PointF(end.x.toFloat(), end.y.toFloat()), duration, interpolator)
        }

        /** Sends the last point, simulating a finger pause. */
        fun pause(): Swipe {
            return to(PointF(lastPoint.x, lastPoint.y), PAUSE_DURATION)
        }

        /** Moves the pointer up, finishing the swipe. Further calls will result in an exception. */
        @JvmOverloads
        fun release(sync: Boolean = true) {
            throwIfReleased()
            log("Touch $pointerId released at $lastPoint")
            sendPointer(
                currentTime = lastTime,
                action = MotionEvent.ACTION_UP,
                point = lastPoint,
                sync = sync
            )
            lastPointerId.decrementAndGet()
            released = true
        }

        /** Moves the pointer by [delta], sending the event at [currentTime]. */
        internal fun moveBy(delta: PointF, currentTime: Long, sync: Boolean) {
            val targetPoint = PointF(lastPoint.x + delta.x, lastPoint.y + delta.y)
            sendPointer(currentTime, MotionEvent.ACTION_MOVE, targetPoint, sync)
            lastTime = currentTime
            lastPoint = targetPoint
        }

        private fun throwIfReleased() {
            check(!released) { "Trying to perform a swipe operation after pointer released" }
        }

        private fun sendPointer(
            currentTime: Long,
            action: Int,
            point: PointF,
            sync: Boolean = true
        ) {
            val event = getMotionEvent(downTime, currentTime, action, point, pointerId)
            check(
                getInstrumentation()
                    .uiAutomation
                    .injectInputEvent(event, sync, /* waitForAnimations= */ false)
            ) {
                "Touch injection failed"
            }
            event.recycle()
        }

        /** Returns the time when movement finished. */
        private fun movePointer(
            duration: Duration,
            from: PointF,
            to: PointF,
            interpolator: TimeInterpolator
        ): Long {
            val stepTimeMs = GESTURE_STEP.toMillis()
            val durationMs = duration.toMillis()
            val steps = durationMs / stepTimeMs
            val startTime = lastTime
            var currentTime = lastTime
            for (i in 0 until steps) {
                sleep(stepTimeMs)
                currentTime += stepTimeMs
                val progress = interpolator.getInterpolation(i / (steps - 1f))
                val point = from.lerp(progress, to)
                sendPointer(currentTime, MotionEvent.ACTION_MOVE, point)
            }
            assertThat(currentTime).isEqualTo(startTime + stepTimeMs * steps)
            return currentTime
        }
    }

    /** Collection of swipes. This can be used to simulate multitouch. */
    class Swipes internal constructor(vararg starts: PointF) {

        private var lastTime: Long = SystemClock.uptimeMillis()
        private val swipes: List<Swipe> = starts.map { Swipe(it) }

        /** Moves all the swipes by [delta], in [duration] time with constant speed. */
        fun moveBy(delta: PointF, duration: Duration = DEFAULT_DURATION): Swipes {
            log("Moving ${swipes.size} touches by $delta")

            val stepTimeMs = GESTURE_STEP.toMillis()
            val durationMs = duration.toMillis()
            val steps = durationMs / stepTimeMs
            val startTime = lastTime
            var currentTime = lastTime
            val stepDelta = PointF(delta.x / steps, delta.y / steps)
            (1..steps).forEach { _ ->
                sleep(stepTimeMs)
                currentTime += stepTimeMs
                swipes.forEach { swipe ->
                    // Sending the move events as not "sync". Otherwise the method waits for them
                    // to be displatched. As here we're sending many that are supposed to happen at
                    // the same time, we don't want the method to
                    // wait after each single injection.
                    swipe.moveBy(stepDelta, currentTime, sync = false)
                }
            }
            assertThat(currentTime).isEqualTo(startTime + stepTimeMs * steps)
            lastTime = currentTime
            return this
        }

        /** Moves pointers up, finishing the swipe. Further calls will result in an exception. */
        fun release() {
            swipes.forEach { it.release(sync = false) }
        }
    }

    private fun log(s: String) = Log.d("BetterSwipe", s)
}

private fun getMotionEvent(
    downTime: Long,
    eventTime: Long,
    action: Int,
    p: PointF,
    pointerId: Int,
): MotionEvent {
    val properties =
        MotionEvent.PointerProperties().apply {
            id = pointerId
            toolType = TOOL_TYPE_FINGER
        }
    val coordinates =
        MotionEvent.PointerCoords().apply {
            pressure = 1f
            size = 1f
            x = p.x
            y = p.y
        }
    return MotionEvent.obtain(
        /* downTime= */ downTime,
        /* eventTime= */ eventTime,
        /* action= */ action,
        /* pointerCount= */ 1,
        /* pointerProperties= */ arrayOf(properties),
        /* pointerCoords= */ arrayOf(coordinates),
        /* metaState= */ 0,
        /* buttonState= */ 0,
        /* xPrecision= */ 1.0f,
        /* yPrecision= */ 1.0f,
        /* deviceId= */ 0,
        /* edgeFlags= */ 0,
        /* source= */ InputDevice.SOURCE_TOUCHSCREEN,
        /* flags= */ 0
    )
}

private fun PointF.lerp(amount: Float, b: PointF) =
    PointF(lerp(x, b.x, amount), lerp(y, b.y, amount))

private fun lerp(start: Float, stop: Float, amount: Float): Float = start + (stop - start) * amount

/**
 * Interpolator for a fling-like gesture that may leave the surface moving by inertia. Don't use it
 * to drag objects to a precisely specified position.
 */
val FLING_GESTURE_INTERPOLATOR = LinearInterpolator()

/** Interpolator for a precise drag-like gesture not triggering inertia. */
val PRECISE_GESTURE_INTERPOLATOR = DecelerateInterpolator()