Skip to content

Commit eefd815

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Only drain main looper when it is paused.
ControlledLooper.drainMainThreadUntilIdle was created so Robolectric can manually execute tasks when main looper is paused. Now with the introduction of INSTRUMENTATION_TEST looper mode with a free running looper, it doesn't make sense to always idle the main looper, and could lead to deviation of behavior between a test run with Robolectric INSTRUMENTATION_TEST mode and real android. This commit changes drainMainThreadUntilIdle so its a no-op if main looper is not paused. PiperOrigin-RevId: 703634485
1 parent f5ff2c3 commit eefd815

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

espresso/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ The following artifacts were released:
1616

1717
**Bug Fixes**
1818

19+
* Fix deadlock in espresso in Robolectric INSTRUMENTATION_TEST + paused looper.
20+
1921
**New Features**
2022

2123
**Breaking Changes**

espresso/core/java/androidx/test/espresso/base/ThreadPoolExecutorExtractor.java

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import android.os.Handler;
2020
import android.os.Looper;
2121
import androidx.annotation.Nullable;
22+
import androidx.test.internal.platform.ServiceLoaderWrapper;
23+
import androidx.test.internal.platform.os.ControlledLooper;
2224
import java.lang.reflect.Field;
2325
import java.util.concurrent.Callable;
2426
import java.util.concurrent.CountDownLatch;
@@ -42,6 +44,10 @@ final class ThreadPoolExecutorExtractor {
4244
private static final String MODERN_ASYNC_TASK_FIELD_NAME = "THREAD_POOL_EXECUTOR";
4345
private final Handler mainHandler;
4446

47+
private final ControlledLooper controlledLooper =
48+
ServiceLoaderWrapper.loadSingleService(
49+
ControlledLooper.class, () -> ControlledLooper.NO_OP_CONTROLLED_LOOPER);
50+
4551
@Inject
4652
ThreadPoolExecutorExtractor(Looper looper) {
4753
mainHandler = new Handler(looper);
@@ -85,6 +91,7 @@ public void run() {
8591
}
8692
});
8793
try {
94+
controlledLooper.drainMainThreadUntilIdle();
8895
latch.await();
8996
} catch (InterruptedException ie) {
9097
if (!futureToRun.isDone()) {

0 commit comments

Comments
 (0)