Android Instrumentation Testing - UI Thread Issues

前端 未结 5 989
走了就别回头了
走了就别回头了 2020-12-30 22:16

I am trying to write an Instrumentation Test for my Android app.

I\'m running into some weird threading issues and I can\'t seem to find a solution.

My

5条回答
  •  被撕碎了的回忆
    2020-12-30 22:43

    The accepted answer describes what is going on perfectly.

    As an addition, in case someone is curious why Espresso's methods that touch the UI e.g. perform(ViewActions ...) don't need to do the same, it is simply because they end up doing it later for us.

    If you follow perform(ViewActions ...) you will find it ends up doing the following (in android.support.test.espresso.ViewInteraction):

    private void runSynchronouslyOnUiThread(Runnable action) {
        ...
        mainThreadExecutor.execute(uiTask);
        ...
    }
    

    That mainThreadExecutor is itself annotated with @MainThread.

    In other words, Espresso also needs to play by the same rules described by David on the accepted answer.

提交回复
热议问题