等待带有espresso的查看传呼机动画吗? [英] Wait for view pager animations with espresso?
问题描述
尝试使用ViewPager进行一些测试.
Trying to do some tests with a ViewPager.
我想在选项卡之间滑动,并且在滑动完成之前,我不想继续.但是似乎没有一种方法可以关闭视图分页器的动画(禁用了开发人员选项下的所有动画).
I want to swipe between tabs, and I don't want to continue until the swipe is complete. But there doesn't appear to be a way to turn off the animation for the view pager (all animations under the developer options are disabled).
因此,这总是会导致测试失败,因为视图分页器尚未完成其动画,因此视图尚未完全显示:
So this always results in a test failure, because the view pager hasn't completed it's animation, and so the view is not completely displayed yet:
// swipe left
onView(withId(R.id.viewpager)).check(matches(isDisplayed())).perform(swipeLeft());
// check to ensure that the next tab is completely visible.
onView(withId(R.id.next_tab)).check(matches(isCompletelyDisplayed()));
是否有一种优雅甚至是推荐的方法来执行此操作,或者我是否坚持在其中进行一些定时的等待?
Is there an elegant or maybe even recommended way to do this, or am I stuck putting some kind of timed wait in there?
推荐答案
IdlingResource
@Simas建议实际上很容易实现:
The IdlingResource
@Simas suggests is actually pretty simple to implement:
public class ViewPagerIdlingResource implements IdlingResource {
private final String mName;
private boolean mIdle = true; // Default to idle since we can't query the scroll state.
private ResourceCallback mResourceCallback;
public ViewPagerIdlingResource(ViewPager viewPager, String name) {
viewPager.addOnPageChangeListener(new ViewPagerListener());
mName = name;
}
@Override
public String getName() {
return mName;
}
@Override
public boolean isIdleNow() {
return mIdle;
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
mResourceCallback = resourceCallback;
}
private class ViewPagerListener extends ViewPager.SimpleOnPageChangeListener {
@Override
public void onPageScrollStateChanged(int state) {
mIdle = (state == ViewPager.SCROLL_STATE_IDLE
// Treat dragging as idle, or Espresso will block itself when swiping.
|| state == ViewPager.SCROLL_STATE_DRAGGING);
if (mIdle && mResourceCallback != null) {
mResourceCallback.onTransitionToIdle();
}
}
}
}
这篇关于等待带有espresso的查看传呼机动画吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!