Skip to content

Commit

Permalink
fix: Avoid throwing an exception if range bounds are corrupted (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Aug 10, 2021
1 parent 4f7a273 commit e3ce25d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -71,17 +71,16 @@ public static String toUuid(AccessibilityNodeInfo info) {
}

@Nullable
public static Range<Integer> getSelectionRange(@Nullable AccessibilityNodeInfo nodeInfo) {
public static Pair<Integer, Integer> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -147,12 +147,12 @@ private static void putAttribute(Map<Attribute, Object> attribs, Attribute key,
case SCROLLABLE:
return node.isScrollable();
case SELECTION_START: {
Range<Integer> selectionRange = AxNodeInfoHelper.getSelectionRange(node);
return selectionRange == null ? null : selectionRange.getLower();
Pair<Integer, Integer> selectionRange = AxNodeInfoHelper.getSelectionRange(node);
return selectionRange == null ? null : selectionRange.first;
}
case SELECTION_END: {
Range<Integer> selectionRange = AxNodeInfoHelper.getSelectionRange(node);
return selectionRange == null ? null : selectionRange.getUpper();
Pair<Integer, Integer> selectionRange = AxNodeInfoHelper.getSelectionRange(node);
return selectionRange == null ? null : selectionRange.second;
}
case SELECTED:
return node.isSelected();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,10 +135,10 @@ public String getAttribute(String attr) throws UiObjectNotFoundException {
break;
case SELECTION_END:
case SELECTION_START:
Range<Integer> selectionRange = AxNodeInfoHelper.getSelectionRange(toAxNodeInfo(element));
Pair<Integer, Integer> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,9 +131,9 @@ public String getAttribute(String attr) throws UiObjectNotFoundException {
}
case SELECTION_END:
case SELECTION_START:
Range<Integer> selectionRange = AxNodeInfoHelper.getSelectionRange(toAxNodeInfo(element));
Pair<Integer, Integer> 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);
Expand Down

0 comments on commit e3ce25d

Please sign in to comment.