Espresso - How to check if one of the view is displayed

后端 未结 10 2002
心在旅途
心在旅途 2021-02-03 17:22

In my test, after one action, there are two possible views which can appear and both of them are correct. How can I check if one of the view is displayed. For a single view I ca

10条回答
  •  [愿得一人]
    2021-02-03 17:37

    Utility class which allows to check if view is visible, gone or invisible:

    public class ExtraAssertions {
        public static ViewAssertion isVisible() {
            return new ViewAssertion() {
                public void check(View view, NoMatchingViewException noView) {
                    assertThat(view, new VisibilityMatcher(View.VISIBLE));
                }
            };
        }
    
        public static ViewAssertion isGone() {
            return new ViewAssertion() {
                public void check(View view, NoMatchingViewException noView) {
                    assertThat(view, new VisibilityMatcher(View.GONE));
                }
            };
        }
    
        public static ViewAssertion isInvisible() {
            return new ViewAssertion() {
                public void check(View view, NoMatchingViewException noView) {
                    assertThat(view, new VisibilityMatcher(View.INVISIBLE));
                }
            };
        }
    
        private static class VisibilityMatcher extends BaseMatcher {
    
            private int visibility;
    
            public VisibilityMatcher(int visibility) {
                this.visibility = visibility;
            }
    
            @Override public void describeTo(Description description) {
                String visibilityName;
                if (visibility == View.GONE) visibilityName = "GONE";
                else if (visibility == View.VISIBLE) visibilityName = "VISIBLE";
                else visibilityName = "INVISIBLE";
                description.appendText("View visibility must has equals " + visibilityName);
            }
    
            @Override public boolean matches(Object o) {
    
                if (o == null) {
                    if (visibility == View.GONE || visibility == View.INVISIBLE) return true;
                    else if (visibility == View.VISIBLE) return false;
                }
    
                if (!(o instanceof View))
                    throw new IllegalArgumentException("Object must be instance of View. Object is instance of " + o);
                return ((View) o).getVisibility() == visibility;
            }
        }
    }
    

    And usage could look like this:

    onView(withId(R.id.text_message)).check(isVisible());
    

    Another view assertion which could help to check extra visibility properties of a view and its parents: it checks visibility, isAttachedToWindow, alpha:

    class IsVisible : ViewAssertion {
        override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
            ViewMatchers.assertThat(
                    "View is not visible. " +
                            "visibility: ${view.visibility}, " +
                            "isAttachedToWindow: ${view.isAttachedToWindow}, " +
                            "alpha: ${view.alpha}",
                    true, `is`(isViewTreeVisible(view)))
        }
    
        private fun isViewTreeVisible(view: View?): Boolean {
            return if (view != null) {
                val viewVisible = view.isAttachedToWindow && view.visibility == View.VISIBLE && view.alpha == 1.0f
    
                if (view.parent !is View) viewVisible
                else viewVisible && isViewTreeVisible(view.parent as View)
            } else {
                true
            }
        }
    }
    

提交回复
热议问题