From ec9548b6f9273d03119efd8025ae169224b23e63 Mon Sep 17 00:00:00 2001 From: Insaf Latypov Date: Mon, 3 Apr 2017 14:32:52 +0100 Subject: Added retries on getVisibleBounds call on scroll. Bug: 36898931 Change-Id: Icefe3bd2253491ea5ec714f1609801beb3d436e3 --- .../afwtest/uiautomator/utils/WidgetUtils.java | 32 +++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) 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 1c2b809..ae5b411 100644 --- a/libs/UiAutomatorLib/src/com/android/afwtest/uiautomator/utils/WidgetUtils.java +++ b/libs/UiAutomatorLib/src/com/android/afwtest/uiautomator/utils/WidgetUtils.java @@ -32,6 +32,7 @@ import android.widget.TextView; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; /** * Widget utils. @@ -439,7 +440,7 @@ public class WidgetUtils { * @param marginPct gesture margin as target's dimension percentage. */ private static void setGestureMargins(UiObject2 target, float marginPct) { - final Rect bounds = target.getVisibleBounds(); + final Rect bounds = callWithRetry(target::getVisibleBounds, DEFAULT_UI_ATTEMPTS_COUNT); final int horizontalMargin = (int)(bounds.width() * marginPct); final int verticalMargin = (int)(bounds.height() * marginPct); target.setGestureMargins(horizontalMargin, @@ -516,4 +517,33 @@ public class WidgetUtils { return null; } + + /** + * Executes provided action. In case the call throws + * {@link StaleObjectException} it is retried at most {@code maxAttempts} times. + * + * @param action action to execute. + * @param maxAttempts maximum attempt to perform. + * @param return type of provided action. + * @return value returned by provided action. + */ + public static T callWithRetry(Supplier action, int maxAttempts) { + if (maxAttempts <= 0) { + throw new IllegalArgumentException("maxAttempts must be positive integer value."); + } + + T result = null; + while ((maxAttempts--) > 0) { + try { + result = action.get(); + break; + } catch (StaleObjectException e) { + if (maxAttempts == 0) { + throw e; + } + } + } + + return result; + } } -- cgit v1.2.3