/** * Gets an instance of the currently active (displayed) activity. * @param activityTestRule test rule * @param <T> activity class * @return activity instance */ public static <T extends Activity> T getCurrentActivity(@NonNull ActivityTestRule activityTestRule) { getInstrumentation().waitForIdleSync(); final Activity[] activity = new Activity[1]; try { activityTestRule.runOnUiThread(new Runnable() { @Override public void run() { java.util.Collection<Activity> activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED); activity[0] = Iterables.getOnlyElement(activites); }}); } catch (Throwable throwable) { throwable.printStackTrace(); } //noinspection unchecked return (T) activity[0]; }
@Override public boolean matchesSafely(final View testView) { if (!mTargetPredicate.apply(testView)) return false; Predicate<View> pred = new Predicate<View>() { @Override public boolean apply(@Nullable View targetView) { return mPredicate.apply(testView, targetView); } }; Iterable<View> traversal = Iterables.filter(breadthFirstViewTraversal(testView.getRootView()), new Predicate<View>() { @Override public boolean apply(@Nullable View input) { return input != testView && mTargetPredicate.apply(input); } }); if (Iterables.isEmpty(traversal)) { throw new NoMatchingViewException.Builder().withViewMatcher(mTarget).build(); // TODO: build an actual err msg } switch (mType) { case ANY: return Iterables.any(traversal, pred); case ALL: return Iterables.all(traversal, pred); case NONE: return !Iterables.any(traversal, pred); default: throw new IllegalStateException("impossible mType"); } }