gpt4 book ai didi

Android Espresso 测试更高 : view1 is higher than view2

转载 作者:行者123 更新时间:2023-11-30 00:40:55 34 4
gpt4 key购买 nike

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

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

我如何测试该场景?

最佳答案

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

我创建了一个 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;
}
}
}

关于Android Espresso 测试更高 : view1 is higher than view2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42597101/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com