summaryrefslogtreecommitdiff
path: root/libraries/tapl-common/src/android/platform/test/scenario/tapl_common/TaplUiObject.kt
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/tapl-common/src/android/platform/test/scenario/tapl_common/TaplUiObject.kt')
-rw-r--r--libraries/tapl-common/src/android/platform/test/scenario/tapl_common/TaplUiObject.kt85
1 files changed, 85 insertions, 0 deletions
diff --git a/libraries/tapl-common/src/android/platform/test/scenario/tapl_common/TaplUiObject.kt b/libraries/tapl-common/src/android/platform/test/scenario/tapl_common/TaplUiObject.kt
index 28d997b27..694c75df2 100644
--- a/libraries/tapl-common/src/android/platform/test/scenario/tapl_common/TaplUiObject.kt
+++ b/libraries/tapl-common/src/android/platform/test/scenario/tapl_common/TaplUiObject.kt
@@ -15,7 +15,12 @@
*/
package android.platform.test.scenario.tapl_common
+import android.graphics.Point
+import android.graphics.Rect
+import android.platform.uiautomator_helpers.BetterSwipe
+import android.platform.uiautomator_helpers.PRECISE_GESTURE_INTERPOLATOR
import androidx.test.uiautomator.By
+import androidx.test.uiautomator.Direction
import androidx.test.uiautomator.UiObject2
import androidx.test.uiautomator.Until
@@ -25,6 +30,34 @@ import androidx.test.uiautomator.Until
* @param [name] Name of the object for diags
*/
class TaplUiObject constructor(val uiObject: UiObject2, private val name: String) {
+ // Margins used for gestures (avoids touching too close to the object's edge).
+ private var mMarginLeft = 5
+ private var mMarginTop = 5
+ private var mMarginRight = 5
+ private var mMarginBottom = 5
+
+ /** Sets the margins used for gestures in pixels. */
+ fun setGestureMargin(margin: Int) {
+ setGestureMargins(margin, margin, margin, margin)
+ }
+
+ /** Sets the margins used for gestures in pixels. */
+ fun setGestureMargins(left: Int, top: Int, right: Int, bottom: Int) {
+ mMarginLeft = left
+ mMarginTop = top
+ mMarginRight = right
+ mMarginBottom = bottom
+ }
+
+ /** Returns this object's visible bounds with the margins removed. */
+ private fun getVisibleBoundsForGestures(): Rect {
+ val ret: Rect = uiObject.visibleBounds
+ ret.left = ret.left + mMarginLeft
+ ret.top = ret.top + mMarginTop
+ ret.right = ret.right - mMarginRight
+ ret.bottom = ret.bottom - mMarginBottom
+ return ret
+ }
/** Wait for the object to become clickable and enabled, then clicks the object. */
fun click() {
@@ -47,4 +80,56 @@ class TaplUiObject constructor(val uiObject: UiObject2, private val name: String
)
return TaplUiObject(childObject, childObjectName)
}
+
+ /**
+ * Performs a horizontal or vertical swipe over an area.
+ *
+ * @param area The area to swipe over.
+ * @param direction The direction in which to swipe.
+ * @param percent The size of the swipe as a percentage of the total area.
+ */
+ private fun scrollRect(area: Rect, direction: Direction, percent: Float) {
+ val start: Point
+ val end: Point
+ when (direction) {
+ Direction.LEFT -> {
+ start = Point(area.right, area.centerY())
+ end = Point(area.right - (area.width() * percent).toInt(), area.centerY())
+ }
+ Direction.RIGHT -> {
+ start = Point(area.left, area.centerY())
+ end = Point(area.left + (area.width() * percent).toInt(), area.centerY())
+ }
+ Direction.UP -> {
+ start = Point(area.centerX(), area.bottom)
+ end = Point(area.centerX(), area.bottom - (area.height() * percent).toInt())
+ }
+ Direction.DOWN -> {
+ start = Point(area.centerX(), area.top)
+ end = Point(area.centerX(), area.top + (area.height() * percent).toInt())
+ }
+ else -> throw RuntimeException()
+ }
+
+ BetterSwipe.from(start).to(end, interpolator = PRECISE_GESTURE_INTERPOLATOR).release()
+ }
+
+ /**
+ * Performs a scroll gesture on this object.
+ *
+ * @param direction The direction in which to scroll.
+ * @param percent The distance to scroll as a percentage of this object's visible size.
+ */
+ fun scroll(direction: Direction, percent: Float) {
+ require(percent >= 0.0f) { "Percent must be greater than 0.0f" }
+ require(percent <= 1.0f) { "Percent must be less than 1.0f" }
+
+ // To scroll, we swipe in the opposite direction
+ val swipeDirection: Direction = Direction.reverse(direction)
+
+ // Scroll by performing repeated swipes
+ val bounds: Rect = getVisibleBoundsForGestures()
+ val segment = Math.min(percent, 1.0f)
+ scrollRect(bounds, swipeDirection, segment)
+ }
}