使用 Espresso 进行测试时使用 ViewProperyAnimators 时的竞争条件

Race conditions when using ViewProperyAnimators while testing with Espresso

我有一个简单的 Espresso 测试,由于我认为是动画问题而未能断言可见性。我已禁用设备上的所有动画设置。

bannerLayout.animate()
        .y(0)
        .setInterpolator(new DecelerateInterpolator(1));

我在 Espresso 中的断言是

onView(withId(R.id.banner_layout)).check(matches(not(isDisplayed())));

在断言使测试通过之前添加休眠。

我发现明确强制启动动画而不是依赖框架来启动它解决了这个问题。

bannerLayout.animate()
    .y(0)
    .setInterpolator(new DecelerateInterpolator(1))
    .start();

Android docs状态:

void start ()

Starts the currently pending property animations immediately. Calling start() is optional because all animations start automatically at the next opportunity. However, if the animations are needed to start immediately and synchronously (not at the time when the next event is processed by the hierarchy, which is when the animations would begin otherwise), then this method can be used.

对于简单的动画,这应该没问题。如果您有一些复杂的性能问题,这可能不是要走的路。