diff options
author | Kevin Jin <kjin@google.com> | 2013-12-02 16:23:25 -0800 |
---|---|---|
committer | Kevin Jin <kjin@google.com> | 2013-12-03 10:57:52 -0800 |
commit | 9031ed9b636ccd3b942eefb85dbfae2aed9e4f11 (patch) | |
tree | c2600bd00f776803d3e0f25fc586337360281204 /src | |
parent | 5cf5f03c64b65b1f1ecd2140b8d6605ac05b6199 (diff) | |
download | droiddriver-9031ed9b636ccd3b942eefb85dbfae2aed9e4f11.tar.gz |
add ScrollStepStrategy.doScroll for customization
make SwipeAction margin ratios adjustable
Change-Id: I848471d1d72d0e1d7009f64ff242ff281cff7017
Diffstat (limited to 'src')
6 files changed, 83 insertions, 33 deletions
diff --git a/src/com/google/android/droiddriver/actions/SwipeAction.java b/src/com/google/android/droiddriver/actions/SwipeAction.java index 186b5dc..71241bb 100644 --- a/src/com/google/android/droiddriver/actions/SwipeAction.java +++ b/src/com/google/android/droiddriver/actions/SwipeAction.java @@ -123,6 +123,10 @@ public class SwipeAction extends ScrollAction { private final PhysicalDirection direction; private final boolean drag; private final int steps; + private final float topMarginRatio; + private final float leftMarginRatio; + private final float bottomMarginRatio; + private final float rightMarginRatio; /** * Defaults timeoutMillis to 1000 and no drag. @@ -139,26 +143,48 @@ public class SwipeAction extends ScrollAction { } /** + * Defaults all margin ratios to 0.1F. + */ + public SwipeAction(PhysicalDirection direction, int steps, boolean drag, long timeoutMillis) { + this(direction, SCROLL_STEPS, drag, timeoutMillis, 0.1F, 0.1F, 0.1F, 0.1F); + } + + /** * @param direction the scroll direction specifying where the content will * move, instead of the finger. * @param steps minimum 2; (steps-1) is the number of {@code ACTION_MOVE} that * will be injected between {@code ACTION_DOWN} and {@code ACTION_UP}. * @param drag whether this is a drag * @param timeoutMillis + * @param topMarginRatio margin ratio from top + * @param leftMarginRatio margin ratio from left + * @param bottomMarginRatio margin ratio from bottom + * @param rightMarginRatio margin ratio from right */ - public SwipeAction(PhysicalDirection direction, int steps, boolean drag, long timeoutMillis) { + public SwipeAction(PhysicalDirection direction, int steps, boolean drag, long timeoutMillis, + float topMarginRatio, float leftMarginRatio, float bottomMarginRatio, float rightMarginRatio) { super(timeoutMillis); this.direction = direction; this.steps = Ints.max(2, steps); this.drag = drag; + this.topMarginRatio = topMarginRatio; + this.bottomMarginRatio = bottomMarginRatio; + this.leftMarginRatio = leftMarginRatio; + this.rightMarginRatio = rightMarginRatio; } @Override public boolean perform(InputInjector injector, UiElement element) { Rect elementRect = element.getVisibleBounds(); - int swipeAreaHeightAdjust = (int) (elementRect.height() * 0.1); - int swipeAreaWidthAdjust = (int) (elementRect.width() * 0.1); + int topMargin = (int) (elementRect.height() * topMarginRatio); + int bottomMargin = (int) (elementRect.height() * bottomMarginRatio); + int leftMargin = (int) (elementRect.width() * leftMarginRatio); + int rightMargin = (int) (elementRect.width() * rightMarginRatio); + int adjustedbottom = elementRect.bottom - bottomMargin; + int adjustedTop = elementRect.top + topMargin; + int adjustedLeft = elementRect.left + leftMargin; + int adjustedRight = elementRect.right - rightMargin; int startX; int startY; int endX; @@ -167,26 +193,26 @@ public class SwipeAction extends ScrollAction { switch (direction) { case DOWN: startX = elementRect.centerX(); - startY = elementRect.bottom - swipeAreaHeightAdjust; + startY = adjustedbottom; endX = elementRect.centerX(); - endY = elementRect.top + swipeAreaHeightAdjust; + endY = adjustedTop; break; case UP: startX = elementRect.centerX(); - startY = elementRect.top + swipeAreaHeightAdjust; + startY = adjustedTop; endX = elementRect.centerX(); - endY = elementRect.bottom - swipeAreaHeightAdjust; + endY = adjustedbottom; break; case LEFT: - startX = elementRect.left + swipeAreaWidthAdjust; + startX = adjustedLeft; startY = elementRect.centerY(); - endX = elementRect.right - swipeAreaWidthAdjust; + endX = adjustedRight; endY = elementRect.centerY(); break; case RIGHT: - startX = elementRect.right - swipeAreaWidthAdjust; + startX = adjustedRight; startY = elementRect.centerY(); - endX = elementRect.left + swipeAreaHeightAdjust; + endX = adjustedLeft; endY = elementRect.centerY(); break; default: diff --git a/src/com/google/android/droiddriver/scroll/AccessibilityEventScrollStepStrategy.java b/src/com/google/android/droiddriver/scroll/AccessibilityEventScrollStepStrategy.java index e4d8ade..4ff3486 100644 --- a/src/com/google/android/droiddriver/scroll/AccessibilityEventScrollStepStrategy.java +++ b/src/com/google/android/droiddriver/scroll/AccessibilityEventScrollStepStrategy.java @@ -78,7 +78,8 @@ public class AccessibilityEventScrollStepStrategy implements ScrollStepStrategy private final UiAutomation uiAutomation; private final long scrollEventTimeoutMillis; private final DirectionConverter directionConverter; - private final EndData atEndData = new EndData(); + private final EndData endData = new EndData(); + private AccessibilityEvent event; public AccessibilityEventScrollStepStrategy(UiAutomation uiAutomation, long scrollEventTimeoutMillis, DirectionConverter converter) { @@ -91,33 +92,24 @@ public class AccessibilityEventScrollStepStrategy implements ScrollStepStrategy public boolean scroll(DroidDriver driver, Finder containerFinder, final PhysicalDirection direction) { // Check if we've reached end after last scroll. - if (atEndData.match(containerFinder, direction)) { + if (endData.match(containerFinder, direction)) { return false; } - final UiElement container = driver.on(containerFinder); - try { - AccessibilityEvent event = uiAutomation.executeAndWaitForEvent(new Runnable() { - @Override - public void run() { - SwipeAction.toScroll(direction).perform(container.getInjector(), container); - } - }, SCROLL_EVENT_FILTER, scrollEventTimeoutMillis); - - if (detectEnd(direction.axis(), event)) { - atEndData.set(containerFinder, direction); - Logs.log(Log.DEBUG, "reached scroll end"); - } - } catch (TimeoutException e) { - // If no TYPE_VIEW_SCROLLED event, no more scrolling is possible - return false; + doScroll(driver.on(containerFinder), direction); + if (detectEnd(direction.axis())) { + endData.set(containerFinder, direction); + Logs.log(Log.DEBUG, "reached scroll end"); } return true; } // Copied from UiAutomator. // AdapterViews have indices we can use to check for the beginning. - private static boolean detectEnd(Axis axis, AccessibilityEvent event) { + protected boolean detectEnd(Axis axis) { + if (event == null) { + return true; + } boolean foundEnd = false; if (event.getFromIndex() != -1 && event.getToIndex() != -1 && event.getItemCount() != -1) { foundEnd = event.getFromIndex() == 0 || (event.getItemCount() - 1) == event.getToIndex(); @@ -129,6 +121,7 @@ public class AccessibilityEventScrollStepStrategy implements ScrollStepStrategy } } event.recycle(); + event = null; return foundEnd; } @@ -146,10 +139,25 @@ public class AccessibilityEventScrollStepStrategy implements ScrollStepStrategy @Override public void beginScrolling(DroidDriver driver, Finder containerFinder, Finder itemFinder, PhysicalDirection direction) { - atEndData.reset(); + endData.reset(); } @Override public void endScrolling(DroidDriver driver, Finder containerFinder, Finder itemFinder, PhysicalDirection direction) {} + + @Override + public void doScroll(final UiElement container, final PhysicalDirection direction) { + event = null; + try { + event = uiAutomation.executeAndWaitForEvent(new Runnable() { + @Override + public void run() { + SwipeAction.toScroll(direction).perform(container.getInjector(), container); + } + }, SCROLL_EVENT_FILTER, scrollEventTimeoutMillis); + } catch (TimeoutException e) { + // If no TYPE_VIEW_SCROLLED event, no more scrolling is possible + } + } } diff --git a/src/com/google/android/droiddriver/scroll/BaseSentinelStrategy.java b/src/com/google/android/droiddriver/scroll/BaseSentinelStrategy.java index 4db5d96..c440bf2 100644 --- a/src/com/google/android/droiddriver/scroll/BaseSentinelStrategy.java +++ b/src/com/google/android/droiddriver/scroll/BaseSentinelStrategy.java @@ -101,4 +101,9 @@ public abstract class BaseSentinelStrategy implements SentinelStrategy { public String toString() { return String.format("{backwardGetter=%s, forwardGetter=%s}", backwardGetter, forwardGetter); } + + @Override + public void doScroll(UiElement container, PhysicalDirection direction) { + container.scroll(direction); + } } diff --git a/src/com/google/android/droiddriver/scroll/DynamicSentinelStrategy.java b/src/com/google/android/droiddriver/scroll/DynamicSentinelStrategy.java index 03f302c..673d26b 100644 --- a/src/com/google/android/droiddriver/scroll/DynamicSentinelStrategy.java +++ b/src/com/google/android/droiddriver/scroll/DynamicSentinelStrategy.java @@ -209,7 +209,7 @@ public class DynamicSentinelStrategy extends BaseSentinelStrategy { @Override public boolean scroll(DroidDriver driver, Finder containerFinder, PhysicalDirection direction) { UiElement oldSentinel = getOldSentinel(driver, containerFinder, direction); - oldSentinel.getParent().scroll(direction); + doScroll(oldSentinel.getParent(), direction); UiElement newSentinel = getSentinel(driver, containerFinder, direction); lastSentinel = newSentinel; return isUpdatedStrategy.isSentinelUpdated(newSentinel, oldSentinel); diff --git a/src/com/google/android/droiddriver/scroll/ScrollStepStrategy.java b/src/com/google/android/droiddriver/scroll/ScrollStepStrategy.java index d3474e5..9583def 100644 --- a/src/com/google/android/droiddriver/scroll/ScrollStepStrategy.java +++ b/src/com/google/android/droiddriver/scroll/ScrollStepStrategy.java @@ -16,6 +16,7 @@ package com.google.android.droiddriver.scroll; import com.google.android.droiddriver.DroidDriver; +import com.google.android.droiddriver.UiElement; import com.google.android.droiddriver.finders.Finder; import com.google.android.droiddriver.scroll.Direction.DirectionConverter; import com.google.android.droiddriver.scroll.Direction.PhysicalDirection; @@ -70,6 +71,16 @@ public interface ScrollStepStrategy { PhysicalDirection direction); /** + * Performs the scroll action on {@code container}. Subclasses can override + * this to customize the scroll action, for example, to adjust the scroll + * margins. + * + * @param container the container that can scroll + * @param direction + */ + void doScroll(UiElement container, PhysicalDirection direction); + + /** * {@inheritDoc} * * <p> diff --git a/src/com/google/android/droiddriver/scroll/StaticSentinelStrategy.java b/src/com/google/android/droiddriver/scroll/StaticSentinelStrategy.java index 3393e43..e8f1c76 100644 --- a/src/com/google/android/droiddriver/scroll/StaticSentinelStrategy.java +++ b/src/com/google/android/droiddriver/scroll/StaticSentinelStrategy.java @@ -58,7 +58,7 @@ public class StaticSentinelStrategy extends BaseSentinelStrategy { return false; } - container.scroll(direction); + doScroll(container, direction); return true; } } |