Espresso + Picasso + Spoon

Picasso uses threads to download images in the background. Even when loading from assets, there is a slight delay until it appears, which makes the photos not appear on the spoon grab. I could add a 1st sleep to the test, but I was wondering if there is a better way.

I tried to set Downloader

or RequestHandler

to return the image synchronously, but I think I need to set ExecutorService

which is using the main thread or AsyncTask

(so that espresso will wait), With modification we can use AsyncTask.THREAD_POOL_EXECUTOR

with MainThreadExecutor

, but I'm not sure how to do it for picasso ...

As a workaround, I've wrapped picasso in ImageUtil, which won't be used during instrumentation:

DebugModule {
    @Provide
    ImageUtil imageUtil() {
        if (isTest) {
            return TestImageUtil();
        } else {
            return PicassoImageUtil();
        }
    }
}

      

Any suggestions?

update: in picasso's code, trying to use Executor

instead ExecutorService

, I am stuck on service.shutdown()

.

+3


source to share


1 answer


I'll give you a general method of waiting for asynchronous tasks with espresso, because you can be sure this will reappear.

You don't have to use a thread to wait for something to happen. This can lead to flaking and your tests will be less effective. Espresso has been designed to avoid sleep challenges.

You also shouldn't be forcing something to be on the main thread, which isn't usually on the main thread. If this causes ANR, your test might fail due to an unexpected popup. There might also be a real error that only occurs with multithreading, but now when you force something to execute on the main thread, your tests might be missing.

You are on the right track replacing another wrapper for Picasso for testing. You need a way to connect to the beginning of the request (right before it disconnects from the main thread) and when the request is complete. Shell traversal is one way to do this.



To be notified when a request is complete, you can use a reverse version of the method in .

Now that you have the entry and exit points of your asynchronous task, you can use the CountingIdlingResource to keep your test from moving until the task is complete. It's as simple as incrementing the counter before starting the task and decrementing it when finished.

Here's a great example of how to use this class: https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/sample/src/androidTest/java/android/support/test/ testapp / AdvancedSynchronizationTest.java? autodive = 0% 2F% 2F

+4


source







All Articles