Android Open Source - PrettyBundle Ext View Actions






From Project

Back to project page PrettyBundle.

License

The source code is released under:

Apache License

If you think the Android project PrettyBundle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tale.prettybundle.sample.espresso;
/* w w w.  j a v  a2  s  .co  m*/
import android.support.test.espresso.PerformException;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.espresso.action.GeneralLocation;
import android.support.test.espresso.action.GeneralSwipeAction;
import android.support.test.espresso.action.Press;
import android.support.test.espresso.action.Swipe;
import android.support.test.espresso.util.HumanReadables;
import android.support.test.espresso.util.TreeIterables;
import android.view.View;

import org.hamcrest.Matcher;

import java.util.concurrent.TimeoutException;

import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.CoreMatchers.anything;

public class ExtViewActions {

    /**
     * Perform action of waiting for a specific view id.
     * <p/>
     * E.g.:
     * onView(isRoot()).perform(waitId(R.id.dialogEditor, Sampling.SECONDS_15));
     *
     * @param viewId
     * @param millis
     * @return
     */
    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();
            }
        };
    }

    /**
     * Perform action of waiting for a specific time. Useful when you need
     * to wait for animations to end and Espresso fails at waiting.
     * <p/>
     * E.g.:
     * onView(isRoot()).perform(waitAtLeast(Sampling.SECONDS_15));
     *
     * @param millis
     * @return
     */
    public static ViewAction waitAtLeast(final long millis) {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return anything();
            }

            @Override
            public String getDescription() {
                return "wait for at least " + millis + " millis.";
            }

            @Override
            public void perform(final UiController uiController, final View view) {
                uiController.loopMainThreadUntilIdle();
                uiController.loopMainThreadForAtLeast(millis);
            }
        };
    }

    public static ViewAction waitForSoftKeyboard() {
        return waitAtLeast(500);
    }

    public static ViewAction swipeTop() {
        return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.BOTTOM_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER);
    }

    public static ViewAction swipeBottom() {
        return new GeneralSwipeAction(Swipe.FAST, GeneralLocation.TOP_CENTER, GeneralLocation.BOTTOM_CENTER, Press.FINGER);
    }

    /**
     * Perform action of waiting until UI thread is free.
     * <p/>
     * E.g.:
     * onView(isRoot()).perform(waitUntilIdle());
     *
     * @return
     */
    public static ViewAction waitUntilIdle() {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return anything();
            }

            @Override
            public String getDescription() {
                return "wait until UI thread is free";
            }

            @Override
            public void perform(final UiController uiController, final View view) {
                uiController.loopMainThreadUntilIdle();
            }
        };
    }

}




Java Source Code List

com.tale.prettybundle.ExtraBinderProvider.java
com.tale.prettybundle.ExtraBinder.java
com.tale.prettybundle.Extra.java
com.tale.prettybundle.Injector.java
com.tale.prettybundle.PrettyBundle.java
com.tale.prettybundle.internal.ExtraAnnotatedClass.java
com.tale.prettybundle.internal.ExtraClassesGrouped.java
com.tale.prettybundle.internal.ExtraInjectorClassBuilder.java
com.tale.prettybundle.internal.ExtraUtilityClassBuilder.java
com.tale.prettybundle.internal.PrettyBundleProcessor.java
com.tale.prettybundle.internal.SupportedType.java
com.tale.prettybundle.sample.ActivitiesClassTest.java
com.tale.prettybundle.sample.ActivitiesTest.java
com.tale.prettybundle.sample.ApplicationTest.java
com.tale.prettybundle.sample.BaseActivityUnitTestCase.java
com.tale.prettybundle.sample.InjectArrayExtrasTest.java
com.tale.prettybundle.sample.InjectParcelableExtrasTest.java
com.tale.prettybundle.sample.InjectPrimaryTypeExtrasFragmentTest.java
com.tale.prettybundle.sample.InjectPrimaryTypeExtrasTest.java
com.tale.prettybundle.sample.InjectStringExtrasTest.java
com.tale.prettybundle.sample.MainActivity.java
com.tale.prettybundle.sample.MenuActivity.java
com.tale.prettybundle.sample.TestArrayExtrasActivity.java
com.tale.prettybundle.sample.TestExtraOnFragmentActivity.java
com.tale.prettybundle.sample.TestParcelableActivity.java
com.tale.prettybundle.sample.TestPrimaryTypeDisplayActivity.java
com.tale.prettybundle.sample.TestPrimaryTypeSetterActivity.java
com.tale.prettybundle.sample.TestStringExtra1Activity.java
com.tale.prettybundle.sample.TestStringExtra2Activity.java
com.tale.prettybundle.sample.data.Person.java
com.tale.prettybundle.sample.espresso.ExtViewActions.java
com.tale.prettybundle.sample.fragments.TestPrimaryExtraFragment.java
com.tale.prettybundle.sample.services.TestPrimaryTypeService.java