How do I make Espresso wait until Data Binding has updated the View with the data-model?

后端 未结 2 973
别那么骄傲
别那么骄傲 2020-12-14 04:59

I am running Espresso tests on my Android application. The test is flaky. It can reliable assert that the data-model is updated. My problem is that the ViewMatchers can\'t m

相关标签:
2条回答
  • 2020-12-14 05:03

    Espresso does waitForIdle before executing view checks. waitForIdle goes thought IdlingRegistry and waits until every IdlingResource is idle.

    LoopingIdlingResource is used in Espresso by default. It waits until looper doesn't have messages in queue, which means that it is idle.

    However DataBinding uses different approach to schedule an update, it uses Choreographer.postFrameCallback. So updates are not posted into looper queue and Espresso will not wait for them.

    In such cases you should register your own IdlingResource. You can find in googlesamples/android-architecture-components nice sample how to implement custom DataBindingIdlingResource and DataBindingIdlingResourceRule that will sets the idle resource before executing tests.

    So you have to copy these classes DataBindingIdlingResourceRule and DataBindingIdlingResource into your tests.

    And add the following rule into your test class:

    @Rule
    @JvmField
    val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule(activityRule)
    
    0 讨论(0)
  • 2020-12-14 05:26

    Edit: This is an old answer. Please use Roshak's

    The bug report mentioned using reflection to change ViewDataBinding.USE_CHOREOGRAPHER to false for the tests, so here is the solution I came up with:

    public static void setFinalStatic(Field field, Object newValue) throws Exception {
        field.setAccessible(true);
    
        Field modifiersField;
        try {
            modifiersField = Field.class.getDeclaredField("accessFlags");
        } catch(NoSuchFieldException e) {
            //This is an emulator JVM  ¯\_(ツ)_/¯
            modifiersField = Field.class.getDeclaredField("modifiers");
        }
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    
        field.set(null, newValue);
    }
    

    Then, just define an ActivityTestRule for the Activity under test, and override its beforeActivityLaunched(). It is necessary to do this before the activity is launched (as opposed to in a @Before annotation) because ViewDataBinding will initialize a Looper if it doesn't use CHOREOGRAPHER.

    @Override
    protected void beforeActivityLaunched() {
        super.beforeActivityLaunched();
        //Because we are using data-binding, we avoid using CHOREOGRAPHER
        try {
            ReflectionUtils.setFinalStatic(
               ViewDataBinding.class.getDeclaredField("USE_CHOREOGRAPHER"), false);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    }
    

    This way, you can get rid of that Thread.sleep()

    0 讨论(0)
提交回复
热议问题