Android浓缩咖啡测试更高:view1高于view2

Android浓缩咖啡测试更高:view1高于view2,android,ui-testing,Android,Ui Testing,我使用动画根据用户操作移动视图(view1,view2)。 在特定情况下,两个视图重叠 我想确保view1高于view2。 问题是view1不在view2之上(它们是重叠的),因此我不能使用断言isAbove 如何测试该场景?我找到了解决方案,如果其他人需要,我会将其发布在这里: 我创建了一个CustomViewAssertions类,并从PositionAssertions类复制了相关部分: 的相对位置未更改 findView-完全不同,因为它使用内部类 getTopViewGroup-未更改

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

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


如何测试该场景?

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

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

的相对位置未更改

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;
        }
    }
}
公共类CustomViewAssertions{
公共静态视图断言isHigher(匹配器匹配器){
返回相对位置(匹配器,位置更高);
}
公共静态ViewAssertion IshighErrorSame(匹配器匹配器){
返回相对位置(匹配器、位置。较高或相同);
}
私有静态ViewAssertion relativePositionOf(最终匹配器viewMatcher,
最终职位(职位){
返回新的ViewAssertion(){
@凌驾
公共无效检查(最终视图foundView,NoMatchingViewException NovieWeException){
StringDescription description=新的StringDescription();
if(noViewException!=null){
description.appendText(String.format(
“'由于找不到视图'%s',无法执行检查。\n”,
noViewException.getViewMatcherDescription());
抛出新奇的例外;
}否则{
description.appendText(“视图:”).appendText(HumanReadables.descriple(foundView))
.appendText(“不是”)
.appendText(position.toString())
.appendText(“视图”)
.appendText(viewMatcher.toString());
断言(description.toString(),isRelativePosition(foundView,findView(viewMatcher,getTopViewGroup(foundView)),position)为(true));
}
}
};
}
私有静态视图findView(匹配器视图匹配器、视图topViewGroup){
Iterable views=横向首视图遍历(topViewGroup);
LinkedList matchedViews=新建LinkedList();
用于(视图:视图){
if(viewMatcher.matches(视图)){
匹配视图。添加(视图);
}
}
if(matchedViews.isEmpty()){
抛出新的NoMatchingViewException.Builder()
.带viewMatcher(viewMatcher)
.withRootView(TopView组)
.build();
}
if(matchedViews.size()==1){
返回matchedViews.get(0);
}
//暧昧!
抛出新的模糊ViewMatcherException.Builder()
.withRootView(TopView组)
.带viewMatcher(viewMatcher)
.withView1(匹配视图。删除(0))
.withView2(匹配视图。删除(0))
.With其他模糊视图(matchedViews.toArray(新视图[matchedViews.size()]))
.build();
}
私有静态视图组getTopViewGroup(视图视图){
ViewParent currentParent=view.getParent();
ViewGroup topView=null;
while(currentParent!=null){
if(视图组的当前父实例){
topView=(视图组)currentParent;
}
currentParent=currentParent.getParent();
}
返回俯视图;
}
专用静态布尔值isRelativePosition(视图视图1、视图视图2、位置){
int[]位置1=新int[2];
int[]位置2=新int[2];
视图1.getLocationOnScreen(位置1);
视图2.getLocationOnScreen(位置2);
开关(位置){
高一级:
返回位置1[1]<位置2[1];
大小写较高或相同:
返回位置1[1]