aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkjin <kjin@google.com>2016-03-09 15:21:51 -0800
committerPaul Duffin <paulduffin@google.com>2016-10-18 14:33:50 +0100
commit86fa19c2157314b5a303c5540f0f60cc8c7862cd (patch)
tree99e74851b5887c67a882472be5726e1ffa69535a
parent675040fd1a83ee2505e1109489adcc0d666f61ea (diff)
downloaddroiddriver-86fa19c2157314b5a303c5540f0f60cc8c7862cd.tar.gz
Fix code for more restrictive compiler [options]
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=116809572 (cherry picked from commit 04be547623ae035113c42565423b28bac425f889) Test: Build using Error Prone which fails without this patch Change-Id: I4e5925d9b8b8652d9eb3d155b9113be5dca4e35f
-rw-r--r--src/io/appium/droiddriver/finders/By.java135
-rw-r--r--src/io/appium/droiddriver/finders/Predicates.java83
-rw-r--r--src/io/appium/droiddriver/uiautomation/UiAutomationElement.java4
3 files changed, 135 insertions, 87 deletions
diff --git a/src/io/appium/droiddriver/finders/By.java b/src/io/appium/droiddriver/finders/By.java
index f8ac924..0b6a57f 100644
--- a/src/io/appium/droiddriver/finders/By.java
+++ b/src/io/appium/droiddriver/finders/By.java
@@ -18,6 +18,9 @@ package io.appium.droiddriver.finders;
import android.content.Context;
+import java.util.ArrayList;
+import java.util.List;
+
import io.appium.droiddriver.UiElement;
import io.appium.droiddriver.exceptions.ElementNotFoundException;
import io.appium.droiddriver.util.InstrumentationUtils;
@@ -28,14 +31,19 @@ import static io.appium.droiddriver.util.Preconditions.checkNotNull;
* Convenience methods to create commonly used finders.
*/
public class By {
+
private static final MatchFinder ANY = new MatchFinder(null);
- /** Matches any UiElement. */
+ /**
+ * Matches any UiElement.
+ */
public static MatchFinder any() {
return ANY;
}
- /** Matches a UiElement whose {@code attribute} is {@code true}. */
+ /**
+ * Matches a UiElement whose {@code attribute} is {@code true}.
+ */
public static MatchFinder is(Attribute attribute) {
return new MatchFinder(Predicates.attributeTrue(attribute));
}
@@ -47,74 +55,91 @@ public class By {
return new MatchFinder(Predicates.attributeFalse(attribute));
}
- /** Matches a UiElement by a resource id defined in the AUT. */
+ /**
+ * Matches a UiElement by a resource id defined in the AUT.
+ */
public static MatchFinder resourceId(int resourceId) {
Context targetContext = InstrumentationUtils.getInstrumentation().getTargetContext();
return resourceId(targetContext.getResources().getResourceName(resourceId));
}
/**
- * Matches a UiElement by the string representation of a resource id. This works for resources
- * not belonging to the AUT.
+ * Matches a UiElement by the string representation of a resource id. This works for resources not
+ * belonging to the AUT.
*/
public static MatchFinder resourceId(String resourceId) {
return new MatchFinder(Predicates.attributeEquals(Attribute.RESOURCE_ID, resourceId));
}
- /** Matches a UiElement by package name. */
+ /**
+ * Matches a UiElement by package name.
+ */
public static MatchFinder packageName(String name) {
return new MatchFinder(Predicates.attributeEquals(Attribute.PACKAGE, name));
}
- /** Matches a UiElement by the exact text. */
+ /**
+ * Matches a UiElement by the exact text.
+ */
public static MatchFinder text(String text) {
return new MatchFinder(Predicates.attributeEquals(Attribute.TEXT, text));
}
- /** Matches a UiElement whose text matches {@code regex}. */
+ /**
+ * Matches a UiElement whose text matches {@code regex}.
+ */
public static MatchFinder textRegex(String regex) {
return new MatchFinder(Predicates.attributeMatches(Attribute.TEXT, regex));
}
- /** Matches a UiElement whose text contains {@code substring}. */
+ /**
+ * Matches a UiElement whose text contains {@code substring}.
+ */
public static MatchFinder textContains(String substring) {
return new MatchFinder(Predicates.attributeContains(Attribute.TEXT, substring));
}
- /** Matches a UiElement by content description. */
+ /**
+ * Matches a UiElement by content description.
+ */
public static MatchFinder contentDescription(String contentDescription) {
return new MatchFinder(Predicates.attributeEquals(Attribute.CONTENT_DESC, contentDescription));
}
- /** Matches a UiElement whose content description contains {@code substring}. */
+ /**
+ * Matches a UiElement whose content description contains {@code substring}.
+ */
public static MatchFinder contentDescriptionContains(String substring) {
return new MatchFinder(Predicates.attributeContains(Attribute.CONTENT_DESC, substring));
}
- /** Matches a UiElement by class name. */
+ /**
+ * Matches a UiElement by class name.
+ */
public static MatchFinder className(String className) {
return new MatchFinder(Predicates.attributeEquals(Attribute.CLASS, className));
}
- /** Matches a UiElement by class name. */
+ /**
+ * Matches a UiElement by class name.
+ */
public static MatchFinder className(Class<?> clazz) {
return className(clazz.getName());
}
- /** Matches a UiElement that is selected. */
+ /**
+ * Matches a UiElement that is selected.
+ */
public static MatchFinder selected() {
return is(Attribute.SELECTED);
}
/**
- * Matches by XPath. When applied on an non-root element, it will not evaluate
- * above the context element.
- * <p>
- * XPath is the domain-specific-language for navigating a node tree. It is
- * ideal if the UiElement to match has a complex relationship with surrounding
- * nodes. For simple cases, {@link #withParent} or {@link #withAncestor} are
- * preferred, which can combine with other {@link MatchFinder}s in
- * {@link #allOf}. For complex cases like below, XPath is superior:
+ * Matches by XPath. When applied on an non-root element, it will not evaluate above the context
+ * element. <p> XPath is the domain-specific-language for navigating a node tree. It is ideal if
+ * the UiElement to match has a complex relationship with surrounding nodes. For simple cases,
+ * {@link #withParent} or {@link #withAncestor} are preferred, which can combine with other {@link
+ * MatchFinder}s in {@link #allOf}. For complex cases like below, XPath is superior:
*
* <pre>
* {@code
@@ -132,8 +157,8 @@ public class By {
* }
* </pre>
*
- * If we need to locate the RelativeLayout containing the album "Forever"
- * instead of a song or an artist named "Forever", this XPath works:
+ * If we need to locate the RelativeLayout containing the album "Forever" instead of a song or an
+ * artist named "Forever", this XPath works:
*
* <pre>
* {@code //*[LinearLayout/*[@text='Albums']]/RelativeLayout[*[@text='Forever']]}
@@ -147,34 +172,28 @@ public class By {
}
/**
- * Returns a finder that uses the UiElement returned by first Finder as
- * context for the second Finder.
- * <p>
- * typically first Finder finds the ancestor, then second Finder finds the
- * target UiElement, which is a descendant.
- * </p>
- * Note that if the first Finder matches multiple UiElements, only the first
- * match is tried, which usually is not what callers expect. In this case,
- * allOf(second, withAncesor(first)) may work.
+ * Returns a finder that uses the UiElement returned by first Finder as context for the second
+ * Finder. <p> typically first Finder finds the ancestor, then second Finder finds the target
+ * UiElement, which is a descendant. </p> Note that if the first Finder matches multiple
+ * UiElements, only the first match is tried, which usually is not what callers expect. In this
+ * case, allOf(second, withAncesor(first)) may work.
*/
public static ChainFinder chain(Finder first, Finder second) {
return new ChainFinder(first, second);
}
- private static Predicate<? super UiElement>[] getPredicates(MatchFinder... finders) {
- @SuppressWarnings("unchecked")
- Predicate<? super UiElement>[] predicates = new Predicate[finders.length];
+ private static List<Predicate<? super UiElement>> getPredicates(MatchFinder... finders) {
+ ArrayList<Predicate<? super UiElement>> predicates = new ArrayList<>(finders.length);
for (int i = 0; i < finders.length; i++) {
- predicates[i] = finders[i].predicate;
+ predicates.add(finders[i].predicate);
}
return predicates;
}
/**
- * Evaluates given {@code finders} in short-circuit fashion in the order
- * they are passed. Costly finders (for example those returned by with*
- * methods that navigate the node tree) should be passed after cheap finders
- * (for example the ByAttribute finders).
+ * Evaluates given {@code finders} in short-circuit fashion in the order they are passed. Costly
+ * finders (for example those returned by with* methods that navigate the node tree) should be
+ * passed after cheap finders (for example the ByAttribute finders).
*
* @return a finder that is the logical conjunction of given finders
*/
@@ -183,10 +202,9 @@ public class By {
}
/**
- * Evaluates given {@code finders} in short-circuit fashion in the order
- * they are passed. Costly finders (for example those returned by with*
- * methods that navigate the node tree) should be passed after cheap finders
- * (for example the ByAttribute finders).
+ * Evaluates given {@code finders} in short-circuit fashion in the order they are passed. Costly
+ * finders (for example those returned by with* methods that navigate the node tree) should be
+ * passed after cheap finders (for example the ByAttribute finders).
*
* @return a finder that is the logical disjunction of given finders
*/
@@ -195,8 +213,8 @@ public class By {
}
/**
- * Matches a UiElement whose parent matches the given parentFinder. For
- * complex cases, consider {@link #xpath}.
+ * Matches a UiElement whose parent matches the given parentFinder. For complex cases, consider
+ * {@link #xpath}.
*/
public static MatchFinder withParent(MatchFinder parentFinder) {
checkNotNull(parentFinder);
@@ -204,8 +222,8 @@ public class By {
}
/**
- * Matches a UiElement whose ancestor matches the given ancestorFinder. For
- * complex cases, consider {@link #xpath}.
+ * Matches a UiElement whose ancestor matches the given ancestorFinder. For complex cases,
+ * consider {@link #xpath}.
*/
public static MatchFinder withAncestor(MatchFinder ancestorFinder) {
checkNotNull(ancestorFinder);
@@ -213,8 +231,8 @@ public class By {
}
/**
- * Matches a UiElement which has a visible sibling matching the given
- * siblingFinder. This could be inefficient; consider {@link #xpath}.
+ * Matches a UiElement which has a visible sibling matching the given siblingFinder. This could be
+ * inefficient; consider {@link #xpath}.
*/
public static MatchFinder withSibling(MatchFinder siblingFinder) {
checkNotNull(siblingFinder);
@@ -222,8 +240,8 @@ public class By {
}
/**
- * Matches a UiElement which has a visible child matching the given
- * childFinder. This could be inefficient; consider {@link #xpath}.
+ * Matches a UiElement which has a visible child matching the given childFinder. This could be
+ * inefficient; consider {@link #xpath}.
*/
public static MatchFinder withChild(MatchFinder childFinder) {
checkNotNull(childFinder);
@@ -231,8 +249,8 @@ public class By {
}
/**
- * Matches a UiElement whose descendant (including self) matches the given
- * descendantFinder. This could be VERY inefficient; consider {@link #xpath}.
+ * Matches a UiElement whose descendant (including self) matches the given descendantFinder. This
+ * could be VERY inefficient; consider {@link #xpath}.
*/
public static MatchFinder withDescendant(final MatchFinder descendantFinder) {
checkNotNull(descendantFinder);
@@ -254,11 +272,14 @@ public class By {
});
}
- /** Matches a UiElement that does not match the provided {@code finder}. */
+ /**
+ * Matches a UiElement that does not match the provided {@code finder}.
+ */
public static MatchFinder not(MatchFinder finder) {
checkNotNull(finder);
return new MatchFinder(Predicates.not(finder.predicate));
}
- private By() {}
+ private By() {
+ }
}
diff --git a/src/io/appium/droiddriver/finders/Predicates.java b/src/io/appium/droiddriver/finders/Predicates.java
index 1b9ad80..f32b365 100644
--- a/src/io/appium/droiddriver/finders/Predicates.java
+++ b/src/io/appium/droiddriver/finders/Predicates.java
@@ -18,13 +18,17 @@ package io.appium.droiddriver.finders;
import android.text.TextUtils;
+import java.util.Arrays;
+
import io.appium.droiddriver.UiElement;
/**
* Static utility methods pertaining to {@code Predicate} instances.
*/
public final class Predicates {
- private Predicates() {}
+
+ private Predicates() {
+ }
private static final Predicate<Object> ANY = new Predicate<Object>() {
@Override
@@ -64,9 +68,9 @@ public final class Predicates {
}
/**
- * Returns a predicate that evaluates to {@code true} if both arguments
- * evaluate to {@code true}. The arguments are evaluated in order, and
- * evaluation will be "short-circuited" as soon as a false predicate is found.
+ * Returns a predicate that evaluates to {@code true} if both arguments evaluate to {@code true}.
+ * The arguments are evaluated in order, and evaluation will be "short-circuited" as soon as a
+ * false predicate is found.
*/
@SuppressWarnings("unchecked")
public static <T> Predicate<T> allOf(final Predicate<? super T> first,
@@ -92,13 +96,11 @@ public final class Predicates {
}
/**
- * Returns a predicate that evaluates to {@code true} if each of its
- * components evaluates to {@code true}. The components are evaluated in
- * order, and evaluation will be "short-circuited" as soon as a false
- * predicate is found.
+ * Returns a predicate that evaluates to {@code true} if each of its components evaluates to
+ * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited"
+ * as soon as a false predicate is found.
*/
- @SuppressWarnings("unchecked")
- public static <T> Predicate<T> allOf(final Predicate<? super T>... components) {
+ public static <T> Predicate<T> allOf(final Iterable<Predicate<? super T>> components) {
return new Predicate<T>() {
@Override
public boolean apply(T input) {
@@ -118,13 +120,21 @@ public final class Predicates {
}
/**
- * Returns a predicate that evaluates to {@code true} if any one of its
- * components evaluates to {@code true}. The components are evaluated in
- * order, and evaluation will be "short-circuited" as soon as a true predicate
- * is found.
+ * Returns a predicate that evaluates to {@code true} if each of its components evaluates to
+ * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited"
+ * as soon as a false predicate is found.
*/
- @SuppressWarnings("unchecked")
- public static <T> Predicate<T> anyOf(final Predicate<? super T>... components) {
+ @SafeVarargs
+ public static <T> Predicate<T> allOf(final Predicate<? super T>... components) {
+ return Predicates.<T>allOf(Arrays.asList(components));
+ }
+
+ /**
+ * Returns a predicate that evaluates to {@code true} if any one of its components evaluates to
+ * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited"
+ * as soon as a true predicate is found.
+ */
+ public static <T> Predicate<T> anyOf(final Iterable<Predicate<? super T>> components) {
return new Predicate<T>() {
@Override
public boolean apply(T input) {
@@ -144,8 +154,18 @@ public final class Predicates {
}
/**
- * Returns a predicate that evaluates to {@code true} on a {@link UiElement}
- * if its {@code attribute} is {@code true}.
+ * Returns a predicate that evaluates to {@code true} if any one of its components evaluates to
+ * {@code true}. The components are evaluated in order, and evaluation will be "short-circuited"
+ * as soon as a true predicate is found.
+ */
+ @SafeVarargs
+ public static <T> Predicate<T> anyOf(final Predicate<? super T>... components) {
+ return Predicates.<T>anyOf(Arrays.asList(components));
+ }
+
+ /**
+ * Returns a predicate that evaluates to {@code true} on a {@link UiElement} if its {@code
+ * attribute} is {@code true}.
*/
public static Predicate<UiElement> attributeTrue(final Attribute attribute) {
return new Predicate<UiElement>() {
@@ -163,8 +183,8 @@ public final class Predicates {
}
/**
- * Returns a predicate that evaluates to {@code true} on a {@link UiElement}
- * if its {@code attribute} is {@code false}.
+ * Returns a predicate that evaluates to {@code true} on a {@link UiElement} if its {@code
+ * attribute} is {@code false}.
*/
public static Predicate<UiElement> attributeFalse(final Attribute attribute) {
return new Predicate<UiElement>() {
@@ -182,8 +202,8 @@ public final class Predicates {
}
/**
- * Returns a predicate that evaluates to {@code true} on a {@link UiElement}
- * if its {@code attribute} equals {@code expected}.
+ * Returns a predicate that evaluates to {@code true} on a {@link UiElement} if its {@code
+ * attribute} equals {@code expected}.
*/
public static Predicate<UiElement> attributeEquals(final Attribute attribute,
final Object expected) {
@@ -202,10 +222,11 @@ public final class Predicates {
}
/**
- * Returns a predicate that evaluates to {@code true} on a {@link UiElement}
- * if its {@code attribute} matches {@code regex}.
+ * Returns a predicate that evaluates to {@code true} on a {@link UiElement} if its {@code
+ * attribute} matches {@code regex}.
*/
- public static Predicate<UiElement> attributeMatches(final Attribute attribute, final String regex) {
+ public static Predicate<UiElement> attributeMatches(final Attribute attribute,
+ final String regex) {
return new Predicate<UiElement>() {
@Override
public boolean apply(UiElement element) {
@@ -221,8 +242,8 @@ public final class Predicates {
}
/**
- * Returns a predicate that evaluates to {@code true} on a {@link UiElement}
- * if its {@code attribute} contains {@code substring}.
+ * Returns a predicate that evaluates to {@code true} on a {@link UiElement} if its {@code
+ * attribute} contains {@code substring}.
*/
public static Predicate<UiElement> attributeContains(final Attribute attribute,
final String substring) {
@@ -240,7 +261,8 @@ public final class Predicates {
};
}
- public static Predicate<UiElement> withParent(final Predicate<? super UiElement> parentPredicate) {
+ public static Predicate<UiElement> withParent(
+ final Predicate<? super UiElement> parentPredicate) {
return new Predicate<UiElement>() {
@Override
public boolean apply(UiElement element) {
@@ -277,7 +299,8 @@ public final class Predicates {
};
}
- public static Predicate<UiElement> withSibling(final Predicate<? super UiElement> siblingPredicate) {
+ public static Predicate<UiElement> withSibling(
+ final Predicate<? super UiElement> siblingPredicate) {
return new Predicate<UiElement>() {
@Override
public boolean apply(UiElement element) {
@@ -318,4 +341,6 @@ public final class Predicates {
}
};
}
+
+
}
diff --git a/src/io/appium/droiddriver/uiautomation/UiAutomationElement.java b/src/io/appium/droiddriver/uiautomation/UiAutomationElement.java
index cf7449e..c011749 100644
--- a/src/io/appium/droiddriver/uiautomation/UiAutomationElement.java
+++ b/src/io/appium/droiddriver/uiautomation/UiAutomationElement.java
@@ -141,7 +141,9 @@ public class UiAutomationElement extends BaseUiElement<AccessibilityNodeInfo, Ui
Rect parentBounds;
while (parent != null) {
parentBounds = parent.getBounds();
- visibleBounds.intersect(parentBounds);
+ if (!visibleBounds.intersect(parentBounds)) {
+ return new Rect();
+ }
parent = parent.getParent();
}
return visibleBounds;