Search in sources :

Example 1 with ViewAction

use of androidx.test.espresso.ViewAction in project AntennaPod by AntennaPod.

the class EspressoTestUtils method waitForView.

/**
 * Perform action of waiting for a specific view id.
 * https://stackoverflow.com/a/49814995/
 * @param viewMatcher The view to wait for.
 * @param millis The timeout of until when to wait for.
 */
public static ViewAction waitForView(final Matcher<View> viewMatcher, final long millis) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return "wait for a specific view for " + millis + " millis.";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
            final long startTime = System.currentTimeMillis();
            final long endTime = startTime + millis;
            do {
                for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                    // found view with required ID
                    if (viewMatcher.matches(child)) {
                        return;
                    }
                }
                uiController.loopMainThreadForAtLeast(50);
            } while (System.currentTimeMillis() < endTime);
            // timeout happens
            throw new PerformException.Builder().withActionDescription(this.getDescription()).withViewDescription(HumanReadables.describe(view)).withCause(new TimeoutException()).build();
        }
    };
}
Also used : ViewAction(androidx.test.espresso.ViewAction) UiController(androidx.test.espresso.UiController) View(android.view.View) Espresso.onView(androidx.test.espresso.Espresso.onView) TimeoutException(java.util.concurrent.TimeoutException) ConditionTimeoutException(org.awaitility.core.ConditionTimeoutException)

Example 2 with ViewAction

use of androidx.test.espresso.ViewAction in project collect by opendatakit.

the class ActivityHelpers method getActivity.

public static Activity getActivity() {
    final Activity[] currentActivity = new Activity[1];
    Espresso.onView(AllOf.allOf(ViewMatchers.withId(android.R.id.content), isDisplayed())).perform(new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(View.class);
        }

        @Override
        public String getDescription() {
            return "getting text from a TextView";
        }

        @Override
        public void perform(UiController uiController, View view) {
            if (view.getContext() instanceof Activity) {
                Activity activity1 = (Activity) view.getContext();
                currentActivity[0] = activity1;
            }
        }
    });
    return currentActivity[0];
}
Also used : ViewAction(androidx.test.espresso.ViewAction) Matcher(org.hamcrest.Matcher) Activity(android.app.Activity) UiController(androidx.test.espresso.UiController) View(android.view.View)

Example 3 with ViewAction

use of androidx.test.espresso.ViewAction in project Rocket by mozilla-tw.

the class GetTextViewMatcher method getText.

public static String getText(final Matcher<View> matcher) {
    final String[] stringHolder = { null };
    onView(matcher).perform(new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(TextView.class);
        }

        @Override
        public String getDescription() {
            return "getting text from a TextView";
        }

        @Override
        public void perform(UiController uiController, View view) {
            TextView tv = (TextView) view;
            stringHolder[0] = tv.getText().toString();
        }
    });
    return stringHolder[0];
}
Also used : ViewAction(androidx.test.espresso.ViewAction) Matcher(org.hamcrest.Matcher) UiController(androidx.test.espresso.UiController) TextView(android.widget.TextView) TextView(android.widget.TextView) Espresso.onView(androidx.test.espresso.Espresso.onView) View(android.view.View)

Example 4 with ViewAction

use of androidx.test.espresso.ViewAction in project BottomNavigation by Ashok-Varma.

the class Utils method waitId.

/**
 * Perform action of waiting for a specific view id.
 */
public static ViewAction waitId(final int viewId, final long millis) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
            final long startTime = System.currentTimeMillis();
            final long endTime = startTime + millis;
            final Matcher<View> viewMatcher = withId(viewId);
            do {
                for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                    // found view with required ID
                    if (viewMatcher.matches(child)) {
                        return;
                    }
                }
                uiController.loopMainThreadForAtLeast(50);
            } while (System.currentTimeMillis() < endTime);
            // timeout happens
            throw new PerformException.Builder().withActionDescription(this.getDescription()).withViewDescription(HumanReadables.describe(view)).withCause(new TimeoutException()).build();
        }
    };
}
Also used : ViewAction(androidx.test.espresso.ViewAction) UiController(androidx.test.espresso.UiController) View(android.view.View) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with ViewAction

use of androidx.test.espresso.ViewAction in project AntennaPod by AntennaPod.

the class EspressoTestUtils method clickChildViewWithId.

/**
 * Perform action of waiting for a specific view id.
 * https://stackoverflow.com/a/30338665/
 * @param id The id of the child to click.
 */
public static ViewAction clickChildViewWithId(@IdRes final int id) {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return null;
        }

        @Override
        public String getDescription() {
            return "Click on a child view with specified id.";
        }

        @Override
        public void perform(UiController uiController, View view) {
            View v = view.findViewById(id);
            v.performClick();
        }
    };
}
Also used : ViewAction(androidx.test.espresso.ViewAction) UiController(androidx.test.espresso.UiController) View(android.view.View) Espresso.onView(androidx.test.espresso.Espresso.onView)

Aggregations

View (android.view.View)10 UiController (androidx.test.espresso.UiController)10 ViewAction (androidx.test.espresso.ViewAction)10 Espresso.onView (androidx.test.espresso.Espresso.onView)8 TextView (android.widget.TextView)4 RecyclerView (androidx.recyclerview.widget.RecyclerView)4 ListView (android.widget.ListView)3 TimeoutException (java.util.concurrent.TimeoutException)2 Matcher (org.hamcrest.Matcher)2 Activity (android.app.Activity)1 Spanned (android.text.Spanned)1 ClickableSpan (android.text.style.ClickableSpan)1 NumberPicker (android.widget.NumberPicker)1 RatingBar (android.widget.RatingBar)1 CloseKeyboardAction (androidx.test.espresso.action.CloseKeyboardAction)1 ViewMatchers.withHint (androidx.test.espresso.matcher.ViewMatchers.withHint)1 ConditionTimeoutException (org.awaitility.core.ConditionTimeoutException)1