Android Espresso等待查看,R.id值不断变化
我们应用程序的许多部分都有一些视频。在我的测试中,我正在等待视频完成:
onView(isRoot()).perform(waitId(R.id.activity_generic_video_video_control_next, 80000));
waitId() 是我从 StackOverflow 上的一篇文章中获取的一个函数,它在很长一段时间内都运行良好。现在由于某种原因它不起作用,我不确定它何时损坏或为什么损坏。
/** 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)) {
if (child.getId() != -1) {
Log.d(TAG, "child id " + child.getId() + " vs " + viewId + " vs " + R.id.activity_generic_video_video_control);
}
// found view with required ID and it's visible
if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
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();
}
};
}
正如您所看到的,代码循环一段时间,直到相关视图可见。有趣的是,我从 for 循环中的日志语句中获取以下输出:
child id 2131363297 vs 2131362267 vs 2131362265
第一个 # 是视图的 ID,给出了为什么要遍历显示树。第二个 # 是传递给函数的 ID。第三个#是同一视图的ID,直接引用R.id。
我可以说至少第二个和第三个数字应该相同,但事实并非如此。这是怎么回事?为什么 id 值会改变?
in many parts of our app we have some videos. and in my tests i am waiting for the video to be finished with this:
onView(isRoot()).perform(waitId(R.id.activity_generic_video_video_control_next, 80000));
waitId() is a function I snagged off of a post here on StackOverflow, and it worked very well for a long time. Now for some reason it's not working and I am not sure when this broke or why.
/** 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)) {
if (child.getId() != -1) {
Log.d(TAG, "child id " + child.getId() + " vs " + viewId + " vs " + R.id.activity_generic_video_video_control);
}
// found view with required ID and it's visible
if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
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();
}
};
}
As you can see, the code loops for a period of time until the view in question is visible. The interesting part is I am getting this output from my log statement in the for loop:
child id 2131363297 vs 2131362267 vs 2131362265
The first # is the ID of the view given why traversing the display tree. The second # is the ID that is passed into the function. The third # is the ID of the same view while referencing the R.id directly.
I can say that at least the 2nd and 3rd numbers should be the same, but they aren't. What's going on? Why is the id value changing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
activity_generic_video_video_control_next
资源是什么?如果是动画,它可以更改 res id。您应该收到一个错误解释:
由于您正在播放视频,如果重新绘制画布(连同按钮),则由以下原因引起:
What is the resource
activity_generic_video_video_control_next
? If it's an animation it can change res id.You should have gotten an error explaining this:
Since you're playing a video, if that canvas is redrawn (along with the button) it's caused by: