Coil: Idling resource for espresso

Created on 10 Jul 2020  路  2Comments  路  Source: coil-kt/coil

Is your feature request related to a problem? Please describe.
Espresso UI tests that do depend on images loading, for instance before taking screenshot or asserting some bitmap computation.

Describe the solution you'd like
In order to signal ongoing image loading to Espresso, Coil can expose an IdlingResource.

Additional context
Glide and Picasso doesn't provide an out-of-the-box solution but workarounds exist:
https://lcdsmao.dev/synchronize-glide-loading-in-test-with-idlingresource/
https://gist.github.com/sebaslogen/0b2fdea3f322c730e04b0af7285fcd28

enhancement help wanted

Most helpful comment

Wouldn't that be solved by wrapping your CoroutineDispatcher around an IdlingThreadPoolExecutor?

Something like:

private val idlingThreadPool = IdlingThreadPoolExecutor(
        "coroutinesDispatchersThreadPool",
        Runtime.getRuntime().availableProcessors(),
        Runtime.getRuntime().availableProcessors(),
        0L,
        TimeUnit.MILLISECONDS,
        LinkedBlockingQueue(),
        Executors.defaultThreadFactory()
    )

And when you build your ImageLoader instance

override fun newImageLoader(): ImageLoader =
        ImageLoader.Builder(context)
            .dispatcher(idlingThreadPool.asCoroutineDispatcher()) 
           .build()

All 2 comments

Wouldn't that be solved by wrapping your CoroutineDispatcher around an IdlingThreadPoolExecutor?

Something like:

private val idlingThreadPool = IdlingThreadPoolExecutor(
        "coroutinesDispatchersThreadPool",
        Runtime.getRuntime().availableProcessors(),
        Runtime.getRuntime().availableProcessors(),
        0L,
        TimeUnit.MILLISECONDS,
        LinkedBlockingQueue(),
        Executors.defaultThreadFactory()
    )

And when you build your ImageLoader instance

override fun newImageLoader(): ImageLoader =
        ImageLoader.Builder(context)
            .dispatcher(idlingThreadPool.asCoroutineDispatcher()) 
           .build()

Indeed, will try that. Didn't know about IdlingThreadPoolExecutor, it seems to solve the problem in an even better way.

Thanks, issue to be closed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vegeta2102 picture vegeta2102  路  6Comments

RubenGonzalezGonzalez picture RubenGonzalezGonzalez  路  6Comments

SourabhSNath picture SourabhSNath  路  3Comments

adesamp picture adesamp  路  3Comments

dafi picture dafi  路  5Comments