From 99ed7f2ecc3e6dc469950d070c1f235596aadbf2 Mon Sep 17 00:00:00 2001 From: AndroidX Test Team Date: Mon, 11 May 2026 11:30:28 -0700 Subject: [PATCH] As recommended in the idling resource analysis doc, IdlingResource implementations should call onTransitionToIdle after configuring isIdleNow. The instrumentation implementation previously threw an IllegalStateException if it observed a transition to idle without onTransitionToIdle being called first. This is downgraded to a warning to avoid flakiness and support the recommended order of operations. Documentation in IdlingResource is updated to reflect this recommendation. PiperOrigin-RevId: 913798608 --- espresso/CHANGELOG.md | 1 + .../androidx/test/espresso/base/IdlingResourceRegistry.java | 3 ++- .../java/androidx/test/espresso/IdlingResource.java | 6 ++---- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/espresso/CHANGELOG.md b/espresso/CHANGELOG.md index 89a4f06b1..212950801 100644 --- a/espresso/CHANGELOG.md +++ b/espresso/CHANGELOG.md @@ -19,6 +19,7 @@ The following artifacts were released: * Replace now-unnecessary reflection from TestLooperManagerCompat when using Android SDK 36 APIs * Don't suppress AppNotIdleException if dumpThreadStates throws. * Remove Espresso.onIdle tracing +* Downgrade IdlingResourceRegistry potential race condition exception to warning. **New Features** diff --git a/espresso/core/java/androidx/test/espresso/base/IdlingResourceRegistry.java b/espresso/core/java/androidx/test/espresso/base/IdlingResourceRegistry.java index 05f4e9423..8d3e1b672 100644 --- a/espresso/core/java/androidx/test/espresso/base/IdlingResourceRegistry.java +++ b/espresso/core/java/androidx/test/espresso/base/IdlingResourceRegistry.java @@ -547,7 +547,8 @@ private void handleRaceCondition(Message m) { if (is.idle) { // it was a race... i is now idle, everything is fine... } else { - throw new IllegalStateException( + Log.w( + TAG, String.format( Locale.ROOT, "Resource %s isIdleNow() is returning true, but a message indicating that the " diff --git a/espresso/idling_resource/java/androidx/test/espresso/IdlingResource.java b/espresso/idling_resource/java/androidx/test/espresso/IdlingResource.java index 6618a37f5..2cb11f285 100644 --- a/espresso/idling_resource/java/androidx/test/espresso/IdlingResource.java +++ b/espresso/idling_resource/java/androidx/test/espresso/IdlingResource.java @@ -63,10 +63,8 @@ public interface ResourceCallback { /** * Called when the resource goes from busy to idle. * - *

If the idle state is updated on a thread other than the main thread, this method should be - * called before the state is updated to return true. Failing to do so can cause a race - * condition where Espresso throws a consistency error due to observing {@link #isIdleNow()} - * returning true before the callback is invoked. + *

It is recommended to call this method after the idle state is updated to return + * true. */ public void onTransitionToIdle(); }