1 package android.platform.uiautomator_helpers
2 
3 import android.graphics.Point
4 import android.graphics.Rect
5 import androidx.test.uiautomator.Direction
6 import androidx.test.uiautomator.Direction.DOWN
7 import androidx.test.uiautomator.Direction.LEFT
8 import androidx.test.uiautomator.Direction.RIGHT
9 import androidx.test.uiautomator.Direction.UP
10 
11 /** Common utils to perform swipes. */
12 internal object SwipeUtils {
13 
14     /**
15      * Calculates start and end point taking into consideration first [marginPx], then [percent].
16      */
calculateStartEndPointnull17     fun calculateStartEndPoint(
18         rawBound: Rect,
19         direction: Direction,
20         percent: Float = 1.0f,
21         marginPx: Int = 0
22     ): Pair<Point, Point> {
23         val bounds = Rect(rawBound)
24         bounds.inset(marginPx, marginPx)
25         val centerX = bounds.centerX()
26         val centerY = bounds.centerY()
27         return when (direction) {
28             LEFT -> {
29                 Point(bounds.right, centerY) to
30                     Point(bounds.right - (bounds.width() * percent).toInt(), centerY)
31             }
32             RIGHT -> {
33                 Point(bounds.left, centerY) to
34                     Point(bounds.left + (bounds.width() * percent).toInt(), centerY)
35             }
36             UP -> {
37                 Point(centerX, bounds.bottom) to
38                     Point(centerX, bounds.bottom - (bounds.height() * percent).toInt())
39             }
40             DOWN -> {
41                 Point(centerX, bounds.top) to
42                     Point(centerX, bounds.top + (bounds.height() * percent).toInt())
43             }
44         }
45     }
46 }
47