Java Code Examples for android.support.test.espresso.util.TreeIterables

The following are top voted examples for showing how to use android.support.test.espresso.util.TreeIterables. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: ChimpCheck   File: SwipeActivity.java   View source code 6 votes vote down vote up
@Override
protected void onResume(){
    super.onResume();
    View root = this.getWindow().getDecorView();

    for(View v : TreeIterables.breadthFirstViewTraversal(root)){
        if(v.getId() != -1) {
            System.out.println("View: " + v.getResources().getResourceEntryName(v.getId()) + " " + v.isClickable());
        } else if(v.getContentDescription() != null){
            System.out.println("View: " + v.getContentDescription().toString() + " " + v.isClickable());
        } else{
            System.out.println("View: " + v.toString() + " No Des, No Id " + v.isClickable());
        }

    }
}
 
Example 2
Project: ChimpCheck   File: ActivityManager.java   View source code 6 votes vote down vote up
protected ArrayList<View> getAllViews() {
    View root = getDecorView();
    ArrayList<View> views = new ArrayList<>();
    for (View v : TreeIterables.breadthFirstViewTraversal(root)) {
        // Log.i("Chimp-needs-to-know","Active View: " + v.toString());
        views.add(v);
    }

    /*
    View focus = getActivityInstance().getCurrentFocus();
    if (focus != null) {
        for (View v : TreeIterables.breadthFirstViewTraversal(focus)) {
            Log.i("Chimp-needs-to-know", "In focus View: " + v.toString());
        }
    } */

    return views;
}
 
Example 3
Project: espresso-doppio   File: ComponentFinder.java   View source code 6 votes vote down vote up
private View matchView(View view, Matcher<View> matcher) {

        if (view == null) {
            return null;
        }

        if (matcher.matches(view)) {
            return view;
        }

        for (View child : TreeIterables.breadthFirstViewTraversal(view)) {

            if (matcher.matches(child)) {
                return child;
            }
        }
        return null;
    }
 
Example 4
Project: Runnest   File: EspressoTest.java   View source code 5 votes vote down vote up
public static ViewAction waitForMatch(final Matcher<View> aViewMatcher, final long timeout) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return "Waiting for view matching " + aViewMatcher;
        }

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

            final long startTime = System.currentTimeMillis();
            final long endTime = startTime + timeout;

            do {
                for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                    if (aViewMatcher.matches(child)) {
                        // found
                        return;
                    }
                }


                uiController.loopMainThreadForAtLeast(50);
            } while (System.currentTimeMillis() < endTime);

            //The action has timed out.
            throw new PerformException.Builder()
                    .withActionDescription(getDescription())
                    .withViewDescription("")
                    .withCause(new TimeoutException())
                    .build();
        }
    };
}
 
Example 5
Project: android-step-by-step   File: EspressoTools.java   View source code 5 votes vote down vote up
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();
        }
    };
}
 
Example 6
Project: BioStar2Android   File: ExtTest.java   View source code 5 votes vote down vote up
public static boolean isExist(final int viewId) {
    final Boolean[] result = {false};
    onView(isRoot()).perform(new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return "get text from edit text view:" + viewId;
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            final Matcher<View> viewMatcher = withId(viewId);
            uiController.loopMainThreadForAtLeast(500);

            for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
                    result[0] = true;
                    break;
                }
            }
        }
    });
    return result[0];
}
 
Example 7
Project: BioStar2Android   File: ExtTest.java   View source code 5 votes vote down vote up
public static Object getTag(final int viewId) {
    final Object[] result = {null};
    onView(isRoot()).perform(new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return "get text from edit text view:" + viewId;
        }

        @Override
        public void perform(UiController uiController, View view) {
            uiController.loopMainThreadUntilIdle();
            final Matcher<View> viewMatcher = withId(viewId);
            uiController.loopMainThreadForAtLeast(500);

            for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                if (viewMatcher.matches(child)) {
                    result[0] = child.getTag();
                }
            }
        }
    });
    return result[0];
}
 
Example 8
Project: yandex-money-sdk-android   File: MoreViewActions.java   View source code 5 votes vote down vote up
@Override
protected boolean isConditionMet(View view) {
    for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
        if (matcher.matches(child)) {
            return true;
        }
    }
    return false;
}
 
Example 9
Project: GitHub   File: Utils.java   View source code 4 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 10
Project: cat-is-a-dog   File: ViewActions.java   View source code 4 votes vote down vote up
/** 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();
        }
    };
}
 
Example 11
Project: EDSApp   File: MalariaEspressoActions.java   View source code 4 votes vote down vote up
/**
 * Looks for a view with the given ID for a given millis
 * @param viewId ID of the view you are looking for
 * @param millis Millis trying to find it
 * @return ViewAction so you can use like any other EspressoAction
 */
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();
        }
    };
}
 
Example 12
Project: malariapp   File: MalariaEspressoActions.java   View source code 4 votes vote down vote up
/**
 * Looks for a view with the given ID for a given millis
 * @param viewId ID of the view you are looking for
 * @param millis Millis trying to find it
 * @return ViewAction so you can use like any other EspressoAction
 */
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();
        }
    };
}
 
Example 13
Project: BioStar2Android   File: ExtTest.java   View source code 4 votes vote down vote up
public static ViewAction waitInvisibleGoneId(final int viewId, final long millis) {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isRoot();
            }

            @Override
            public String getDescription() {
                return "wait view 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);
//                Log.e("test", "waitInvisibleGoneId view id:" + viewId);
                uiController.loopMainThreadForAtLeast(200);
                do {
                    View find = null;
                    for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
//                        if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
//                          Log.e("test","original view id:"+viewId+" child view id:"+child.getId());
                        if (viewMatcher.matches(child)) {
                            find = child;
                            Log.e("test", "macthch  visible: " + child.getVisibility());
                        }

                    }
                    if (find != null && find.getVisibility() == View.VISIBLE) {
//                            Log.e("test", "find startTime:" + startTime);
//                            Log.e("test","find endTime:"+endTime);
//                            Log.e("test","find System.currentTimeMillis():"+System.currentTimeMillis());
                    } else {
                        Log.e("test", "waitInvisibleGoneId wait time :" + (System.currentTimeMillis() - startTime) / 1000 + " id:" + viewId);
                        return;
                    }
//                    Log.e("test","startTime:"+startTime);
//                    Log.e("test","endTime:"+endTime);
                    uiController.loopMainThreadForAtLeast(50);
                }
                while (System.currentTimeMillis() < endTime);

                Log.e("test", "waitInvisibleGoneId time out :" + (System.currentTimeMillis() - startTime) / 1000 + " id:" + viewId);
                throw new PerformException.Builder()
                        .withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new TimeoutException())
                        .build();
            }
        };
    }
 
Example 14
Project: BioStar2Android   File: ExtTest.java   View source code 4 votes vote down vote up
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 view 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);
                Log.e("test", "waitId original view id:" + viewId);
                do {
                    for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
//                        if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
                        //  Log.e("test","original view id:"+viewId+" child view id:"+child.getId());
                        if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
//                            Log.e("test", "find startTime:" + startTime);
//                            Log.e("test","find endTime:"+endTime);
//                            Log.e("test","find System.currentTimeMillis():"+System.currentTimeMillis());
                            Log.e("test", "waitId wait time :" + (System.currentTimeMillis() - startTime) / 1000 + " id:" + viewId);
                            return;
                        }
                    }
//                    Log.e("test","startTime:"+startTime);
//                    Log.e("test","endTime:"+endTime);
                    uiController.loopMainThreadForAtLeast(50);
                }
                while (System.currentTimeMillis() < endTime);

                Log.e("test", "waitId time out :" + (System.currentTimeMillis() - startTime) / 1000 + " id:" + viewId);
                throw new PerformException.Builder()
                        .withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new TimeoutException())
                        .build();
            }
        };
    }
 
Example 15
Project: PrettyBundle   File: ExtViewActions.java   View source code 4 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 16
Project: generator-android-starter   File: _ViewActions.java   View source code 4 votes vote down vote up
/**
 * 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();
        }
    };
}
 
Example 17
Project: readingtracker   File: TestHelpers.java   View source code 4 votes vote down vote up
/**
 * Performs screenshot of activity with specified view
 * use like onView(isRoot()).perform(screenshot(R.id.MainActivity));
 * @param viewId view to look at
 * @param testTag description of screenshoshot
 * @return
 */
public static ViewAction screenshot(final int viewId, final String testTag) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isRoot();
        }

        @Override
        public String getDescription() {
            return "screenshoting current activity with root id  <" + viewId + "> .";
        }

        @Override
        public void perform(final UiController uiController, final View view) {
            uiController.loopMainThreadUntilIdle();
            final Matcher<View> viewMatcher = withId(viewId);


            for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                // found view with required ID
                if (viewMatcher.matches(child)) {
                    Activity host=(Activity) child.getContext();
                    try {
                        Screenshot.capture(testTag+"_"+host.getLocalClassName().toString(),host);
                    } catch (IOException e) {
                        //something bad happened.no screenshot.
                        Log.d(TAG,e.toString());
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                    return;
                }
            }

            // nothing found
            throw new PerformException.Builder()
                    .withActionDescription(this.getDescription())
                    .withViewDescription(HumanReadables.describe(view))
                    .withCause(new NoRootViewFoundWithIdException())
                    .build();
        }
    };
}
 
Example 18
Project: readingtracker   File: TestHelpers.java   View source code 4 votes vote down vote up
/**
 * Perform action of waiting for a specific view id
 * based off https://stackoverflow.com/questions/21417954/espresso-thread-sleep
 * @param viewId - view to look for
 * @param millis - how long to wait for in milliseconds
 */
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();
        }
    };
}
 
Example 19
Project: u2020-mvp   File: ViewActions.java   View source code 4 votes vote down vote up
/**
 * 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();
        }
    };
}