aboutsummaryrefslogtreecommitdiff
path: root/libs/UiAutomatorLib/src/com
diff options
context:
space:
mode:
authorInsaf Latypov <saferif@google.com>2017-01-18 19:35:30 +0000
committerJinhui Wang <jinhuiw@google.com>2017-04-04 22:12:30 +0000
commitbf4a9f4be38aa50cae37e94b436ea805238a4892 (patch)
tree4406ce278dc438c5958285d2ee059ac4256ffdc0 /libs/UiAutomatorLib/src/com
parent5a5528e592436a6748fe35c19aefea465ab47467 (diff)
downloadAfwTestHarness-bf4a9f4be38aa50cae37e94b436ea805238a4892.tar.gz
Add scrolling using UiObject2 capabilities. DO NOT MERGE
Scrolling implemented in UiScrollable is considered obsolete, so a scrolling using modern UiObject2 is implemented. BUG:36898931 Change-Id: I086775447f371bb21b8345c28247e91d881d2086 (cherry picked from commit 38f581eb6ed227c0b8b8f67db2266c485e55873a)
Diffstat (limited to 'libs/UiAutomatorLib/src/com')
-rw-r--r--libs/UiAutomatorLib/src/com/android/afwtest/uiautomator/utils/WidgetUtils.java90
1 files changed, 88 insertions, 2 deletions
diff --git a/libs/UiAutomatorLib/src/com/android/afwtest/uiautomator/utils/WidgetUtils.java b/libs/UiAutomatorLib/src/com/android/afwtest/uiautomator/utils/WidgetUtils.java
index 5265354..58e8c3f 100644
--- a/libs/UiAutomatorLib/src/com/android/afwtest/uiautomator/utils/WidgetUtils.java
+++ b/libs/UiAutomatorLib/src/com/android/afwtest/uiautomator/utils/WidgetUtils.java
@@ -16,6 +16,7 @@
package com.android.afwtest.uiautomator.utils;
+import android.graphics.Rect;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.Direction;
import android.support.test.uiautomator.UiDevice;
@@ -44,6 +45,16 @@ public class WidgetUtils {
private static final long DEFAULT_UI_WAIT_TIME_MS = TimeUnit.SECONDS.toMillis(5);
/**
+ * Margin of swipe's starting and ending points inside the target.
+ */
+ private static final float DEFAULT_SWIPE_DEADZONE_PCT = 0.1f;
+
+ /**
+ * Swipe's length as object's dimension percentage.
+ */
+ private static final float DEFAULT_SWIPE_PCT = 0.7f;
+
+ /**
* Clicks on given {@link UiObject2} without throwing any exception.
*
* @param obj {@link UiObject2} to click
@@ -321,7 +332,7 @@ public class WidgetUtils {
}
/**
- * Scroll a scrollable UI widget so that an item with certain {@link TextView} text is in view.
+ * Scrolls a scrollable UI widget so that an item with certain {@link TextView} text is in view.
*
* @param container Selector for the scrollable widget.
* @param item Text that appears in the target item.
@@ -336,7 +347,7 @@ public class WidgetUtils {
}
/**
- * Scroll a scrollable UI widget so that a certain item is in view.
+ * Scrolls a scrollable UI widget so that a certain item is in view.
*
* @param container Selector for the scrollable widget.
* @param item Selector that specifies the target item.
@@ -359,6 +370,81 @@ public class WidgetUtils {
}
/**
+ * Scrolls vertically a scrollable UI widget so that a certain item is in view.
+ *
+ * @param uiDevice current {@link UiDevice}.
+ * @param container {@link BySelector} of a scrollable container.
+ * @param item {@link BySelector} of an item to scroll to.
+ * @return The target item as a {@link UiObject2} when it first appears, or {@code null} if
+ * not found.
+ * @throws UiObjectNotFoundException If the scrollable does not exist.
+ */
+ public static UiObject2 scrollToItem(UiDevice uiDevice,
+ BySelector container,
+ BySelector item) throws UiObjectNotFoundException {
+ return scrollToItem(uiDevice, container, item, false);
+ }
+
+ /**
+ * Scrolls a scrollable UI widget so that a certain item is in view.
+ *
+ * @param uiDevice current {@link UiDevice}.
+ * @param container {@link BySelector} of a scrollable container.
+ * @param item {@link BySelector} of an item to scroll to.
+ * @param isHorizontal {@code true} if scrollable is horizontal, {@code false} otherwise.
+ * @return The target item as a {@link UiObject2} when it first appears, or {@code null} if
+ * not found.
+ * @throws UiObjectNotFoundException If the scrollable does not exist.
+ */
+ public static UiObject2 scrollToItem(UiDevice uiDevice,
+ BySelector container,
+ BySelector item,
+ boolean isHorizontal) throws UiObjectNotFoundException {
+
+ // Find scrollable object
+ final UiObject2 scrollable = safeWait(uiDevice, container);
+ if (scrollable == null) {
+ throw new UiObjectNotFoundException("Cannot find scrollable " + container);
+ }
+
+ // Check if given container is scrollable
+ if (!scrollable.isScrollable()) {
+ throw new IllegalArgumentException("Given container is not scrollable");
+ }
+
+ setGestureMargins(scrollable, DEFAULT_SWIPE_DEADZONE_PCT);
+
+ // Determine scrolling direction
+ final Direction scrollDirection = isHorizontal ? Direction.RIGHT : Direction.DOWN;
+ final Direction oppositeDirection = Direction.reverse(scrollDirection);
+
+ // Scroll to the beginning of the scrollable
+ while (scrollable.scroll(oppositeDirection, DEFAULT_SWIPE_PCT));
+
+ // Scroll while target item is not visible and scrolling is still possible
+ UiObject2 foundItem;
+ while ((foundItem = scrollable.findObject(item)) == null &&
+ scrollable.scroll(scrollDirection, DEFAULT_SWIPE_PCT));
+ uiDevice.waitForIdle();
+ return foundItem;
+ }
+
+ /**
+ * Set target object's gesture margins.
+ * @param target target object.
+ * @param marginPct gesture margin as target's dimension percentage.
+ */
+ private static void setGestureMargins(UiObject2 target, float marginPct) {
+ final Rect bounds = target.getVisibleBounds();
+ final int horizontalMargin = (int)(bounds.width() * marginPct);
+ final int verticalMargin = (int)(bounds.height() * marginPct);
+ target.setGestureMargins(horizontalMargin,
+ verticalMargin,
+ horizontalMargin,
+ verticalMargin);
+ }
+
+ /**
* Gets properties of a {@link UiObject2} as a String, for debugging purpose.
*
* @param widget {@link UiObject2} to get properties from