Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions espresso/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
60 changes: 29 additions & 31 deletions espresso/core/java/androidx/test/espresso/Espresso.java
Original file line number Diff line number Diff line change
Expand Up @@ -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> T onIdle(Callable<T> 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<T> actionTask = new FutureTask<>(action);
ListenableFutureTask<Void> 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<T> actionTask = new FutureTask<>(action);
ListenableFutureTask<Void> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Loading