From 6c2d53de6eb95458c9698c08ada2e29ece75f005 Mon Sep 17 00:00:00 2001 From: Brett Chabot Date: Mon, 17 Nov 2025 10:06:33 -0800 Subject: [PATCH] Remove tracing from `Espresso.onIdle`. Espresso.onIdle can be called many times through the life of a test, creating memory issues for long running tests and providing questionable value. With this commit, the `Espresso.onIdle` method no longer creates a tracing span. Corresponding tests for the span are also removed. PiperOrigin-RevId: 833388360 --- espresso/CHANGELOG.md | 1 + .../java/androidx/test/espresso/Espresso.java | 60 +++++++++---------- .../androidx/test/espresso/EspressoTest.java | 6 -- 3 files changed, 30 insertions(+), 37 deletions(-) diff --git a/espresso/CHANGELOG.md b/espresso/CHANGELOG.md index 3763cf58b..89a4f06b1 100644 --- a/espresso/CHANGELOG.md +++ b/espresso/CHANGELOG.md @@ -18,6 +18,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 **New Features** diff --git a/espresso/core/java/androidx/test/espresso/Espresso.java b/espresso/core/java/androidx/test/espresso/Espresso.java index 83f2b03c9..3eadd0efb 100644 --- a/espresso/core/java/androidx/test/espresso/Espresso.java +++ b/espresso/core/java/androidx/test/espresso/Espresso.java @@ -323,39 +323,37 @@ private static void waitUntilNextFrame(int times) { * @throws AppNotIdleException when app does not go Idle within the master policies timeout. */ public static T onIdle(Callable action) { - try (Span ignored = tracer.beginSpan("Espresso.onIdle")) { - if (Thread.currentThread().equals(Looper.getMainLooper().getThread())) { - BASE.controlledLooper().drainMainThreadUntilIdle(); - BASE.uiController().loopMainThreadUntilIdle(); - try { - return action.call(); - } catch (Exception e) { - throw new RuntimeException("Callable action in onIdle reported an exception.", e); - } - } - FutureTask actionTask = new FutureTask<>(action); - ListenableFutureTask idleFuture = - ListenableFutureTask.create( - () -> { - BASE.uiController().loopMainThreadUntilIdle(); - return null; - }); - Executor mainThreadExecutor = BASE.mainThreadExecutor(); - idleFuture.addListener(actionTask, mainThreadExecutor); - mainThreadExecutor.execute(idleFuture); + if (Thread.currentThread().equals(Looper.getMainLooper().getThread())) { BASE.controlledLooper().drainMainThreadUntilIdle(); - + BASE.uiController().loopMainThreadUntilIdle(); try { - idleFuture.get(); - return actionTask.get(); - } catch (InterruptedException ie) { - throw new RuntimeException(ie); - } catch (ExecutionException ee) { - if (ee.getCause() instanceof AppNotIdleException) { - throw (AppNotIdleException) ee.getCause(); - } else { - throw new RuntimeException(ee); - } + return action.call(); + } catch (Exception e) { + throw new RuntimeException("Callable action in onIdle reported an exception.", e); + } + } + FutureTask actionTask = new FutureTask<>(action); + ListenableFutureTask idleFuture = + ListenableFutureTask.create( + () -> { + BASE.uiController().loopMainThreadUntilIdle(); + return null; + }); + Executor mainThreadExecutor = BASE.mainThreadExecutor(); + idleFuture.addListener(actionTask, mainThreadExecutor); + mainThreadExecutor.execute(idleFuture); + BASE.controlledLooper().drainMainThreadUntilIdle(); + + try { + idleFuture.get(); + return actionTask.get(); + } catch (InterruptedException ie) { + throw new RuntimeException(ie); + } catch (ExecutionException ee) { + if (ee.getCause() instanceof AppNotIdleException) { + throw (AppNotIdleException) ee.getCause(); + } else { + throw new RuntimeException(ee); } } } diff --git a/espresso/core/javatests/androidx/test/espresso/EspressoTest.java b/espresso/core/javatests/androidx/test/espresso/EspressoTest.java index 58e293dfc..1ef054b3b 100644 --- a/espresso/core/javatests/androidx/test/espresso/EspressoTest.java +++ b/espresso/core/javatests/androidx/test/espresso/EspressoTest.java @@ -428,9 +428,6 @@ public void onData_throwsFromScenarioOnActivity() { @Test public void onIdle_worksFromScenarioOnActivity() { rule.getScenario().onActivity(activity -> onIdle()); - - assertThat( - tracer.getSpans(), contains("beginSpan: Espresso.onIdle", "+-endSpan: Espresso.onIdle")); } @Test @@ -445,9 +442,6 @@ public void onIdle_worksFromMainThread() throws Exception { }); latch.await(); - - assertThat( - tracer.getSpans(), contains("beginSpan: Espresso.onIdle", "+-endSpan: Espresso.onIdle")); } private static class DummyIdlingResource implements IdlingResource {