diff options
author | Kevin Jin <kjin@google.com> | 2013-08-09 18:10:52 -0700 |
---|---|---|
committer | Kevin Jin <kjin@google.com> | 2013-08-09 18:10:52 -0700 |
commit | 1edf859ee1da92a5d03289c6398f982679c77049 (patch) | |
tree | f0265325a22b4f2ba8778dbbe77adb29a3efff9d /src/com | |
parent | 6f160bb942de53103e4f4ed54acaafe2da629fcf (diff) | |
download | droiddriver-1edf859ee1da92a5d03289c6398f982679c77049.tar.gz |
extract matches method from MatchFinder:
Instead of inheritance by overriding matches, use composition of
a predicate.
organize imports.
Change-Id: I1aa95cb14822fb27151cda8bdffd61580b49c5e3
Diffstat (limited to 'src/com')
6 files changed, 63 insertions, 51 deletions
diff --git a/src/com/google/android/droiddriver/finders/By.java b/src/com/google/android/droiddriver/finders/By.java index 152369a..0a4eee9 100644 --- a/src/com/google/android/droiddriver/finders/By.java +++ b/src/com/google/android/droiddriver/finders/By.java @@ -22,6 +22,11 @@ import com.google.android.droiddriver.UiElement; import com.google.common.annotations.Beta; import com.google.common.base.Joiner; import com.google.common.base.Objects; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.collect.Lists; + +import java.util.List; /** * Convenience methods to create commonly used finders. @@ -223,6 +228,15 @@ public class By { return new ChainFinder(parent, child); } + private static List<Predicate<? super UiElement>> getPredicates(MatchFinder... finders) { + List<Predicate<? super UiElement>> predicates = + Lists.newArrayListWithExpectedSize(finders.length); + for (MatchFinder finder : finders) { + predicates.add(finder.predicate); + } + return predicates; + } + // Hamcrest style finder aggregators /** * Evaluates given {@finders} in short-circuit fashion in the order @@ -233,17 +247,7 @@ public class By { * @return a finder that is the logical conjunction of given finders */ public static MatchFinder allOf(final MatchFinder... finders) { - return new MatchFinder() { - @Override - public boolean matches(UiElement element) { - for (MatchFinder finder : finders) { - if (!finder.matches(element)) { - return false; - } - } - return true; - } - + return new MatchFinder(Predicates.and(getPredicates(finders))) { @Override public String toString() { return "allOf(" + Joiner.on(",").join(finders) + ")"; @@ -260,17 +264,7 @@ public class By { * @return a finder that is the logical disjunction of given finders */ public static MatchFinder anyOf(final MatchFinder... finders) { - return new MatchFinder() { - @Override - public boolean matches(UiElement element) { - for (MatchFinder finder : finders) { - if (finder.matches(element)) { - return true; - } - } - return false; - } - + return new MatchFinder(Predicates.or(getPredicates(finders))) { @Override public String toString() { return "anyOf(" + Joiner.on(",").join(finders) + ")"; @@ -284,13 +278,13 @@ public class By { */ public static MatchFinder withParent(final MatchFinder parentFinder) { checkNotNull(parentFinder); - return new MatchFinder() { + return new MatchFinder(new Predicate<UiElement>() { @Override - public boolean matches(UiElement element) { + public boolean apply(UiElement element) { UiElement parent = element.getParent(); return parent != null && parentFinder.matches(parent); } - + }) { @Override public String toString() { return "withParent(" + parentFinder + ")"; @@ -304,9 +298,9 @@ public class By { */ public static MatchFinder withAncestor(final MatchFinder ancestorFinder) { checkNotNull(ancestorFinder); - return new MatchFinder() { + return new MatchFinder(new Predicate<UiElement>() { @Override - public boolean matches(UiElement element) { + public boolean apply(UiElement element) { UiElement parent = element.getParent(); while (parent != null) { if (ancestorFinder.matches(parent)) { @@ -316,7 +310,7 @@ public class By { } return false; } - + }) { @Override public String toString() { return "withAncestor(" + ancestorFinder + ")"; @@ -330,9 +324,9 @@ public class By { */ public static MatchFinder withSibling(final MatchFinder siblingFinder) { checkNotNull(siblingFinder); - return new MatchFinder() { + return new MatchFinder(new Predicate<UiElement>() { @Override - public boolean matches(UiElement element) { + public boolean apply(UiElement element) { UiElement parent = element.getParent(); if (parent == null) { return false; @@ -345,7 +339,7 @@ public class By { } return false; } - + }) { @Override public String toString() { return "withSibling(" + siblingFinder + ")"; diff --git a/src/com/google/android/droiddriver/finders/ByAttribute.java b/src/com/google/android/droiddriver/finders/ByAttribute.java index d7d8689..3356533 100644 --- a/src/com/google/android/droiddriver/finders/ByAttribute.java +++ b/src/com/google/android/droiddriver/finders/ByAttribute.java @@ -18,6 +18,7 @@ package com.google.android.droiddriver.finders; import static com.google.common.base.Preconditions.checkNotNull; import com.google.android.droiddriver.UiElement; +import com.google.common.base.Predicate; /** * Matches UiElement by a single attribute. @@ -27,20 +28,22 @@ public class ByAttribute<T> extends MatchFinder { private final MatchStrategy<? super T> strategy; private final T expected; - protected ByAttribute(Attribute attribute, MatchStrategy<? super T> strategy, T expected) { + protected ByAttribute(final Attribute attribute, final MatchStrategy<? super T> strategy, + final T expected) { + super(new Predicate<UiElement>() { + @Override + public boolean apply(UiElement element) { + T value = attribute.getValue(element); + return strategy.match(expected, value); + } + }); this.attribute = checkNotNull(attribute); this.strategy = checkNotNull(strategy); this.expected = checkNotNull(expected); } @Override - public boolean matches(UiElement element) { - T value = attribute.getValue(element); - return strategy.match(expected, value); - } - - @Override public String toString() { - return String.format("ByAttribute{%s %s %s}", attribute, strategy, expected); + return String.format("{%s %s %s}", attribute, strategy, expected); } } diff --git a/src/com/google/android/droiddriver/finders/MatchFinder.java b/src/com/google/android/droiddriver/finders/MatchFinder.java index 2818fde..33fe699 100644 --- a/src/com/google/android/droiddriver/finders/MatchFinder.java +++ b/src/com/google/android/droiddriver/finders/MatchFinder.java @@ -21,20 +21,23 @@ import android.util.Log; import com.google.android.droiddriver.UiElement; import com.google.android.droiddriver.exceptions.ElementNotFoundException; import com.google.android.droiddriver.util.Logs; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; /** * Traverses the UiElement tree and returns the first UiElement satisfying - * {@link #matches(UiElement)}. + * {@link #predicate}. */ public abstract class MatchFinder implements Finder { - /** - * Returns true if the {@code element} matches the implementing finder. The - * implementing finder should not poll. - * - * @param element The element to validate against - * @return true if the element matches - */ - public abstract boolean matches(UiElement element); + protected final Predicate<? super UiElement> predicate; + + protected MatchFinder(Predicate<? super UiElement> predicate) { + if (predicate == null) { + this.predicate = Predicates.alwaysTrue(); + } else { + this.predicate = predicate; + } + } /** * {@inheritDoc} @@ -61,4 +64,16 @@ public abstract class MatchFinder implements Finder { } throw new ElementNotFoundException(this); } + + /** + * Returns true if the {@code element} matches this finder. This can be used + * to test the exact match of {@code element} when this finder is used in + * {@link By#anyOf(MatchFinder...)}. + * + * @param element The element to validate against + * @return true if the element matches + */ + public final boolean matches(UiElement element) { + return predicate.apply(element); + } } diff --git a/src/com/google/android/droiddriver/scroll/AbstractSentinelStrategy.java b/src/com/google/android/droiddriver/scroll/AbstractSentinelStrategy.java index 345725f..b197b43 100644 --- a/src/com/google/android/droiddriver/scroll/AbstractSentinelStrategy.java +++ b/src/com/google/android/droiddriver/scroll/AbstractSentinelStrategy.java @@ -21,8 +21,8 @@ import com.google.android.droiddriver.exceptions.ElementNotFoundException; import com.google.android.droiddriver.finders.By; import com.google.android.droiddriver.finders.Finder; import com.google.android.droiddriver.scroll.Direction.LogicalDirection; -import com.google.android.droiddriver.scroll.Direction.PhysicalToLogicalConverter; import com.google.android.droiddriver.scroll.Direction.PhysicalDirection; +import com.google.android.droiddriver.scroll.Direction.PhysicalToLogicalConverter; import com.google.common.base.Predicate; import com.google.common.base.Predicates; diff --git a/src/com/google/android/droiddriver/scroll/DynamicSentinelStrategy.java b/src/com/google/android/droiddriver/scroll/DynamicSentinelStrategy.java index 3dca121..9dc1e3a 100644 --- a/src/com/google/android/droiddriver/scroll/DynamicSentinelStrategy.java +++ b/src/com/google/android/droiddriver/scroll/DynamicSentinelStrategy.java @@ -21,8 +21,8 @@ import com.google.android.droiddriver.DroidDriver; import com.google.android.droiddriver.UiElement; import com.google.android.droiddriver.exceptions.ElementNotFoundException; import com.google.android.droiddriver.finders.Finder; -import com.google.android.droiddriver.scroll.Direction.PhysicalToLogicalConverter; import com.google.android.droiddriver.scroll.Direction.PhysicalDirection; +import com.google.android.droiddriver.scroll.Direction.PhysicalToLogicalConverter; import com.google.android.droiddriver.util.Logs; import com.google.common.base.Objects; diff --git a/src/com/google/android/droiddriver/scroll/StaticSentinelStrategy.java b/src/com/google/android/droiddriver/scroll/StaticSentinelStrategy.java index e9225aa..844f0e9 100644 --- a/src/com/google/android/droiddriver/scroll/StaticSentinelStrategy.java +++ b/src/com/google/android/droiddriver/scroll/StaticSentinelStrategy.java @@ -21,8 +21,8 @@ import com.google.android.droiddriver.DroidDriver; import com.google.android.droiddriver.UiElement; import com.google.android.droiddriver.finders.Finder; import com.google.android.droiddriver.instrumentation.InstrumentationDriver; -import com.google.android.droiddriver.scroll.Direction.PhysicalToLogicalConverter; import com.google.android.droiddriver.scroll.Direction.PhysicalDirection; +import com.google.android.droiddriver.scroll.Direction.PhysicalToLogicalConverter; /** * Determines whether scrolling is possible by checking whether the last child |