Synchronizes Espresso UI tests with background { } async operations to prevent flaky tests caused by race conditions.
AsyncTaskCallbackinterface inAsyncUtils.ktdefinesonTaskStarted()/onTaskCompleted()AsyncOperationcalls these hooks during task lifecycleglobalAsyncTaskCallbackis a@Volatileglobal that tests can setAsyncTaskIdlingResourceimplements bothIdlingResourceandAsyncTaskCallback- Espresso waits until counter reaches 0 before proceeding
Tests using UITestFixture get this automatically:
@Before
fun setup() {
fixture.setup(waitForAsyncTasks = true) // Registers idling resource
}
@After
fun teardown() {
fixture.cleanup() // Unregisters and nulls globalAsyncTaskCallback
}None. The callback defaults to null, so production code just does a few no-op null checks.