@@ -23,14 +23,6 @@ import org.robolectric.annotation.Config;
2323import org.robolectric.annotation.LooperMode;
2424import org.robolectric.shadows.ShadowLooper;
2525
26- import java.util.concurrent.Callable;
27- import java.util.concurrent.ExecutionException;
28- import java.util.concurrent.ExecutorService;
29- import java.util.concurrent.Executors;
30- import java.util.concurrent.Future;
31- import java.util.concurrent.TimeUnit;
32- import java.util.concurrent.TimeoutException;
33-
3426@RunWith(RobolectricTestRunner.class)
3527@Config(sdk = 30)
3628@LooperMode(LooperMode.Mode.LEGACY)
@@ -45,7 +37,9 @@ public class @MAIN_NAME@UiTest {
4537 log("Activity created (Display.isInitialized=" + Display.isInitialized() + ")");
4638 controller.start();
4739 log("Activity started");
48- resumeWithPumpedMainLooper(controller);
40+ log("Calling controller.resume() on Robolectric thread");
41+ controller.resume();
42+ log("controller.resume() completed");
4943 log("Display initialized after resume: " + Display.isInitialized());
5044 controller.visible();
5145 log("Activity made visible");
@@ -84,45 +78,6 @@ public class @MAIN_NAME@UiTest {
8478 }
8579 }
8680
87- private static void resumeWithPumpedMainLooper(ActivityController<@MAIN_NAME@Stub> controller)
88- throws InterruptedException, ExecutionException, TimeoutException {
89- log("Preparing to resume activity with pumped main looper");
90- ExecutorService executor = Executors.newSingleThreadExecutor();
91- Future<Void> future = executor.submit(new Callable<Void>() {
92- @Override
93- public Void call() {
94- log("Invoking controller.resume()");
95- controller.resume();
96- log("controller.resume() returned");
97- return null;
98- }
99- });
100-
101- long deadlineNanos = System.nanoTime() + TimeUnit.SECONDS.toNanos(60);
102- int iterations = 0;
103- try {
104- while (!future.isDone()) {
105- if (System.nanoTime() >= deadlineNanos) {
106- log("controller.resume() did not finish before deadline; cancelling");
107- future.cancel(true);
108- throw new TimeoutException("controller.resume() timed out");
109- }
110- ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
111- if (++iterations % 50 == 0) {
112- log("Still waiting for controller.resume() after " + iterations + " pump iterations");
113- }
114- Thread.sleep(20L);
115- }
116- future.get(5L, TimeUnit.SECONDS);
117- log("Activity resumed");
118- } catch (TimeoutException | InterruptedException | ExecutionException ex) {
119- log("Failed while resuming activity: " + ex);
120- throw ex;
121- } finally {
122- executor.shutdownNow();
123- }
124- }
125-
12681 private static Bitmap captureScreenshot(@MAIN_NAME@Stub activity) {
12782 View decorView = activity.getWindow().getDecorView();
12883 log("Decor view instance=" + decorView);
0 commit comments