From e3ce25db6cdbc8032afe39232cc62d58ad2c1bb5 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 10 Aug 2021 15:27:10 +0200 Subject: [PATCH] fix: Avoid throwing an exception if range bounds are corrupted (#437) --- .../io/appium/uiautomator2/core/AxNodeInfoHelper.java | 11 +++++------ .../appium/uiautomator2/model/UiElementSnapshot.java | 10 +++++----- .../appium/uiautomator2/model/UiObject2Element.java | 6 +++--- .../io/appium/uiautomator2/model/UiObjectElement.java | 6 +++--- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/appium/uiautomator2/core/AxNodeInfoHelper.java b/app/src/main/java/io/appium/uiautomator2/core/AxNodeInfoHelper.java index 0dfa8c989..8c0b215c1 100644 --- a/app/src/main/java/io/appium/uiautomator2/core/AxNodeInfoHelper.java +++ b/app/src/main/java/io/appium/uiautomator2/core/AxNodeInfoHelper.java @@ -22,7 +22,7 @@ import android.graphics.Rect; import android.os.Build; import android.os.Bundle; -import android.util.Range; +import android.util.Pair; import android.view.accessibility.AccessibilityNodeInfo; import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; @@ -71,17 +71,16 @@ public static String toUuid(AccessibilityNodeInfo info) { } @Nullable - public static Range getSelectionRange(@Nullable AccessibilityNodeInfo nodeInfo) { + public static Pair getSelectionRange(@Nullable AccessibilityNodeInfo nodeInfo) { if (nodeInfo == null) { return null; } int selectionStart = nodeInfo.getTextSelectionStart(); int selectionEnd = nodeInfo.getTextSelectionEnd(); - if (selectionStart >= 0 && selectionStart != selectionEnd) { - return new Range<>(selectionStart, selectionEnd); - } - return null; + return selectionStart >= 0 && selectionStart != selectionEnd + ? new Pair<>(selectionStart, selectionEnd) + : null; } @Nullable diff --git a/app/src/main/java/io/appium/uiautomator2/model/UiElementSnapshot.java b/app/src/main/java/io/appium/uiautomator2/model/UiElementSnapshot.java index 07309cb58..8c02bdc5a 100644 --- a/app/src/main/java/io/appium/uiautomator2/model/UiElementSnapshot.java +++ b/app/src/main/java/io/appium/uiautomator2/model/UiElementSnapshot.java @@ -17,7 +17,7 @@ package io.appium.uiautomator2.model; import android.annotation.TargetApi; -import android.util.Range; +import android.util.Pair; import android.view.accessibility.AccessibilityNodeInfo; import android.widget.Toast; @@ -147,12 +147,12 @@ private static void putAttribute(Map attribs, Attribute key, case SCROLLABLE: return node.isScrollable(); case SELECTION_START: { - Range selectionRange = AxNodeInfoHelper.getSelectionRange(node); - return selectionRange == null ? null : selectionRange.getLower(); + Pair selectionRange = AxNodeInfoHelper.getSelectionRange(node); + return selectionRange == null ? null : selectionRange.first; } case SELECTION_END: { - Range selectionRange = AxNodeInfoHelper.getSelectionRange(node); - return selectionRange == null ? null : selectionRange.getUpper(); + Pair selectionRange = AxNodeInfoHelper.getSelectionRange(node); + return selectionRange == null ? null : selectionRange.second; } case SELECTED: return node.isSelected(); diff --git a/app/src/main/java/io/appium/uiautomator2/model/UiObject2Element.java b/app/src/main/java/io/appium/uiautomator2/model/UiObject2Element.java index 10d43ebfc..a7951ae82 100644 --- a/app/src/main/java/io/appium/uiautomator2/model/UiObject2Element.java +++ b/app/src/main/java/io/appium/uiautomator2/model/UiObject2Element.java @@ -16,7 +16,7 @@ package io.appium.uiautomator2.model; -import android.util.Range; +import android.util.Pair; import android.view.accessibility.AccessibilityNodeInfo; import androidx.annotation.Nullable; @@ -135,10 +135,10 @@ public String getAttribute(String attr) throws UiObjectNotFoundException { break; case SELECTION_END: case SELECTION_START: - Range selectionRange = AxNodeInfoHelper.getSelectionRange(toAxNodeInfo(element)); + Pair selectionRange = AxNodeInfoHelper.getSelectionRange(toAxNodeInfo(element)); result = selectionRange == null ? null - : (dstAttribute == Attribute.SELECTION_END ? selectionRange.getUpper() : selectionRange.getLower()); + : (dstAttribute == Attribute.SELECTION_END ? selectionRange.second : selectionRange.first); break; default: throw generateNoAttributeException(attr); diff --git a/app/src/main/java/io/appium/uiautomator2/model/UiObjectElement.java b/app/src/main/java/io/appium/uiautomator2/model/UiObjectElement.java index 54f2c31da..12cb4cd62 100644 --- a/app/src/main/java/io/appium/uiautomator2/model/UiObjectElement.java +++ b/app/src/main/java/io/appium/uiautomator2/model/UiObjectElement.java @@ -16,7 +16,7 @@ package io.appium.uiautomator2.model; -import android.util.Range; +import android.util.Pair; import android.view.accessibility.AccessibilityNodeInfo; import androidx.annotation.Nullable; @@ -131,9 +131,9 @@ public String getAttribute(String attr) throws UiObjectNotFoundException { } case SELECTION_END: case SELECTION_START: - Range selectionRange = AxNodeInfoHelper.getSelectionRange(toAxNodeInfo(element)); + Pair selectionRange = AxNodeInfoHelper.getSelectionRange(toAxNodeInfo(element)); result = selectionRange == null ? null - : (dstAttribute == Attribute.SELECTION_END ? selectionRange.getUpper() : selectionRange.getLower()); + : (dstAttribute == Attribute.SELECTION_END ? selectionRange.second : selectionRange.first); break; default: throw generateNoAttributeException(attr);