Skip to content

Commit

Permalink
Added limit to avoid stackoverflows when calculating visible bounds (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tri-fraga authored and mykola-mokhnach committed Jul 11, 2019
1 parent 54bc83c commit a5fb28c
Showing 1 changed file with 15 additions and 3 deletions.
Expand Up @@ -36,6 +36,8 @@
* This class contains static helper methods to work with {@link AccessibilityNodeInfo}
*/
public class AccessibilityNodeInfoHelpers {
// https://github.com/appium/appium/issues/12892
private final static int MAX_DEPTH = 70;

@Nullable
public static Range<Integer> getSelectionRange(@Nullable AccessibilityNodeInfo nodeInfo) {
Expand Down Expand Up @@ -83,6 +85,16 @@ public static String getText(@Nullable AccessibilityNodeInfo nodeInfo, boolean r
*/
@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 by the MAX_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;
}
Expand All @@ -96,13 +108,13 @@ public static Rect getVisibleBounds(@Nullable AccessibilityNodeInfo node) {
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, depth);
ret.intersect(ancestorRect);
break;
}
Expand Down

0 comments on commit a5fb28c

Please sign in to comment.