aboutsummaryrefslogtreecommitdiff
path: root/libs/UiAutomatorLib/src/com/android/afwtest/uiautomator
diff options
context:
space:
mode:
Diffstat (limited to 'libs/UiAutomatorLib/src/com/android/afwtest/uiautomator')
-rw-r--r--libs/UiAutomatorLib/src/com/android/afwtest/uiautomator/utils/WidgetUtils.java32
1 files changed, 31 insertions, 1 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 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 <T> return type of provided action.
+ * @return value returned by provided action.
+ */
+ public static <T> T callWithRetry(Supplier<T> 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;
+ }
}