【问题标题】:Android Espresso test for higher: view1 is higher than view2Android Espresso 测试更高:view1 高于 view2
【发布时间】:2017-03-04 14:25:11
【问题描述】:

我使用动画根据用户操作移动视图(view1view2)。 在特定情况下,这两个视图是重叠的。

我想确保 view1 高于 view2。 问题是 view1 不在 view2 之上(它们是重叠的)所以我不能使用断言 isAbove

我该如何测试这种情况?

【问题讨论】:

    标签: android ui-testing


    【解决方案1】:

    如果其他人需要,我找到了解决方案并将其发布在这里:

    我创建了一个 CustomViewAssertions 类并从 PositionAssertions 类中复制了相关部分:

    relativePositionOf - 未更改

    findView - 完全不同,因为它使用内部类

    getTopViewGroup - 未更改

    isRelativePosition - 更改比较逻辑

    位置 - 新的枚举值

    最终结果:

    public class CustomViewAssertions {
        public static ViewAssertion isHigher(Matcher<View> matcher) {
            return relativePositionOf(matcher, Position.HIGHER);
        }
    
        public static ViewAssertion isHigherOrSame(Matcher<View> matcher) {
            return relativePositionOf(matcher, Position.HIGHER_OR_SAME);
        }
    
        private static ViewAssertion relativePositionOf(final Matcher<View> viewMatcher,
                                                        final Position position) {
            return new ViewAssertion(){
                @Override
                public void check(final View foundView, NoMatchingViewException noViewException) {
                    StringDescription description = new StringDescription();
                    if (noViewException != null) {
                        description.appendText(String.format(
                                "' check could not be performed because view '%s' was not found.\n",
                                noViewException.getViewMatcherDescription()));
                        throw noViewException;
                    } else {
                        description.appendText("View:").appendText(HumanReadables.describe(foundView))
                                   .appendText(" is not ")
                                   .appendText(position.toString())
                                   .appendText(" view ")
                                   .appendText(viewMatcher.toString());
                        assertThat(description.toString(), isRelativePosition(foundView, findView(viewMatcher, getTopViewGroup(foundView)), position), is(true));
                    }
                }
            };
        }
    
        private static View findView(Matcher<View> viewMatcher, View topViewGroup) {
            Iterable<View> views = breadthFirstViewTraversal(topViewGroup);
            LinkedList<View> matchedViews = new LinkedList<>();
            for (View view : views) {
                if (viewMatcher.matches(view)) {
                    matchedViews.add(view);
                }
            }
            if (matchedViews.isEmpty()) {
                throw new NoMatchingViewException.Builder()
                        .withViewMatcher(viewMatcher)
                        .withRootView(topViewGroup)
                        .build();
            }
            if (matchedViews.size() == 1) {
                return matchedViews.get(0);
            }
    
            // Ambiguous!
            throw new AmbiguousViewMatcherException.Builder()
                    .withRootView(topViewGroup)
                    .withViewMatcher(viewMatcher)
                    .withView1(matchedViews.remove(0))
                    .withView2(matchedViews.remove(0))
                    .withOtherAmbiguousViews(matchedViews.toArray(new View[matchedViews.size()]))
                    .build();
        }
    
        private static ViewGroup getTopViewGroup(View view) {
            ViewParent currentParent = view.getParent();
            ViewGroup topView = null;
            while (currentParent != null) {
                if (currentParent instanceof ViewGroup) {
                    topView = (ViewGroup) currentParent;
                }
                currentParent = currentParent.getParent();
            }
            return topView;
        }
    
        private static boolean isRelativePosition(View view1, View view2, Position position) {
            int[] location1 = new int[2];
            int[] location2 = new int[2];
            view1.getLocationOnScreen(location1);
            view2.getLocationOnScreen(location2);
    
            switch (position) {
                case HIGHER:
                    return location1[1] < location2[1];
                case HIGHER_OR_SAME:
                    return location1[1] <= location2[1];
                default:
                    return false;
            }
        }
    
        private enum Position {
            HIGHER("higher"),
            HIGHER_OR_SAME("higher or same");
    
            private final String positionValue;
    
            Position(String value) {
                positionValue = value;
            }
    
            @Override
            public String toString() {
                return positionValue;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      相关资源
      最近更新 更多