Search in sources :

Example 1 with UiController

use of androidx.test.espresso.UiController 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 UiController

use of androidx.test.espresso.UiController 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 UiController

use of androidx.test.espresso.UiController 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 4 with UiController

use of androidx.test.espresso.UiController 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)

Example 5 with UiController

use of androidx.test.espresso.UiController in project orgzly-android by orgzly.

the class EspressoUtils method setNumber.

public static ViewAction setNumber(final int num) {
    return new ViewAction() {

        @Override
        public void perform(UiController uiController, View view) {
            NumberPicker np = (NumberPicker) view;
            np.setValue(num);
        }

        @Override
        public String getDescription() {
            return "Set the passed number into the NumberPicker";
        }

        @Override
        public Matcher<View> getConstraints() {
            return ViewMatchers.isAssignableFrom(NumberPicker.class);
        }
    };
}
Also used : NumberPicker(android.widget.NumberPicker) ViewAction(androidx.test.espresso.ViewAction) UiController(androidx.test.espresso.UiController) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) Espresso.onView(androidx.test.espresso.Espresso.onView) TextView(android.widget.TextView) ListView(android.widget.ListView)

Aggregations

View (android.view.View)8 UiController (androidx.test.espresso.UiController)8 ViewAction (androidx.test.espresso.ViewAction)8 Espresso.onView (androidx.test.espresso.Espresso.onView)6 ListView (android.widget.ListView)3 TextView (android.widget.TextView)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3 TimeoutException (java.util.concurrent.TimeoutException)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 Matcher (org.hamcrest.Matcher)1