Using Espresso idling resource with multiple activities

前端 未结 2 992
青春惊慌失措
青春惊慌失措 2021-02-06 02:24

I have a firstActivity that launches the secondActivity, where in the secondActivity I have a loading Dialog (not AsyncTask), and I need to make Espresso wait until the dialog d

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 03:10

    There are a few problems here:

    1. Your isIdleNow() calls getCurrentActivity() which calls waitForIdleSync() and runTestOnUiThread(). isIdleNow Javadoc says: "Espresso will always call this method from the main thread, therefore it should be non-blocking and return immediately." So this won't work as is, but you could call getActivitiesInStage directly from isIdleNow.
    2. Your other issue is that you store the reference to ResourceCallback but never invoke onTransitionToIdle, also you should allow for the possibility of more than one ResourceCallback being registered and call onTransitionToIdle on all of the callbacks.

    You can do the following:

    1. Copy/Paste IdlingResource into your app as com.mycompany.IdlingResource.
    2. Then have your Activity implement that interface and make sure to call onTransitionToIdle when the dialog goes away and make sure isIdleNow returns false iff the dialog is showing.
    3. In your test code, write a "IdlingResourceAdapter" that wraps com.mycompany.IdlingResource and turns it into an Espresso IdlingResource and register that with Espresso.

    This will be simpler once this issue is implemented: https://code.google.com/p/android-test-kit/issues/detail?id=71

提交回复
热议问题