Skip to content

Instantly share code, notes, and snippets.

@tri-fraga
Last active July 11, 2019 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tri-fraga/d508acd82fa4b131e602768fbb74cdc4 to your computer and use it in GitHub Desktop.
Save tri-fraga/d508acd82fa4b131e602768fbb74cdc4 to your computer and use it in GitHub Desktop.
diff --git a/app/src/main/java/io/appium/uiautomator2/core/AccessibilityNodeInfoHelpers.java b/app/src/main/java/io/appium/uiautomator2/core/AccessibilityNodeInfoHelpers.java
index d131173..e68658f 100644
--- a/app/src/main/java/io/appium/uiautomator2/core/AccessibilityNodeInfoHelpers.java
+++ b/app/src/main/java/io/appium/uiautomator2/core/AccessibilityNodeInfoHelpers.java
@@ -36,6 +36,8 @@ import static io.appium.uiautomator2.utils.StringHelpers.charSequenceToString;
* This class contains static helper methods to work with {@link AccessibilityNodeInfo}
*/
public class AccessibilityNodeInfoHelpers {
+
+ private final static int MAX_DEPTH = 70;
@Nullable
public static Range<Integer> getSelectionRange(@Nullable AccessibilityNodeInfo nodeInfo) {
@@ -75,14 +77,24 @@ public class AccessibilityNodeInfoHelpers {
}
return charSequenceToString(nodeInfo.getText(), replaceNull);
}
-
- /**
+
+ /**
* Returns the node's bounds clipped to the size of the display
*
* @return null if node is null, else a Rect containing visible bounds
*/
@SuppressLint("CheckResult")
public static Rect getVisibleBounds(@Nullable AccessibilityNodeInfo node) {
+ return getVisibleBounds(node, 0);
+ }
+
+ /**
+ * Returns the node's bounds clipped to the size of the display, limited to a given depth
+ *
+ * @return null if node is null, else a Rect containing visible bounds
+ */
+ @SuppressLint("CheckResult")
+ private static Rect getVisibleBounds(@Nullable AccessibilityNodeInfo node, int depth) {
if (node == null) {
return null;
}
@@ -96,16 +108,18 @@ public class AccessibilityNodeInfoHelpers {
Rect screen = new Rect(0, 0, uiDevice.getDisplayWidth(), uiDevice.getDisplayHeight());
ret.intersect(screen);
- // Find the visible bounds of our first scrollable ancestor
- for (AccessibilityNodeInfo ancestor = node.getParent(); ancestor != null; ancestor = ancestor.getParent()) {
+ // Find the visible bounds of our first scrollable ancestor
+ for (AccessibilityNodeInfo ancestor = node.getParent(); ancestor != null && depth < MAX_DEPTH; ancestor = ancestor.getParent()) {
// If this ancestor is scrollable
if (ancestor.isScrollable()) {
// Trim any portion of the bounds that are hidden by the non-visible portion of our
// ancestor
- Rect ancestorRect = getVisibleBounds(ancestor);
+ Rect ancestorRect = getVisibleBounds(ancestor, 0);
ret.intersect(ancestorRect);
break;
}
+
+ depth++;
}
return ret;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment