Skip to content

Commit a982806

Browse files
committed
Simplify Robolectric test startup logging
1 parent 38bb5d7 commit a982806

File tree

1 file changed

+5
-60
lines changed

1 file changed

+5
-60
lines changed

scripts/templates/HelloCodenameOneUiTest.java.tmpl

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import java.io.FileOutputStream;
1414
import java.io.FileWriter;
1515
import java.io.IOException;
1616
import java.io.PrintWriter;
17-
import java.util.concurrent.atomic.AtomicInteger;
1817
import org.junit.Test;
1918
import org.junit.runner.RunWith;
2019
import org.robolectric.Robolectric;
@@ -23,11 +22,10 @@ import org.robolectric.android.controller.ActivityController;
2322
import org.robolectric.annotation.Config;
2423
import org.robolectric.annotation.LooperMode;
2524
import org.robolectric.shadows.ShadowLooper;
26-
import org.robolectric.util.Scheduler;
2725

2826
@RunWith(RobolectricTestRunner.class)
2927
@Config(sdk = 30)
30-
@LooperMode(LooperMode.Mode.PAUSED)
28+
@LooperMode(LooperMode.Mode.LEGACY)
3129
public class @MAIN_NAME@UiTest {
3230

3331
@Test
@@ -45,16 +43,15 @@ public class @MAIN_NAME@UiTest {
4543
log("Activity made visible");
4644

4745
try {
48-
for (int attempt = 1; attempt <= 5; attempt++) {
49-
log("Scheduler pump attempt " + attempt + " (Display.isInitialized=" + Display.isInitialized() + ")");
50-
pumpSchedulers("attempt-" + attempt);
51-
}
46+
log("Idling main looper to process pending tasks");
47+
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
48+
log("Completed runUiThreadTasksIncludingDelayedTasks");
5249

5350
if (Display.isInitialized()) {
5451
log("Codename One display reports current form="
5552
+ Display.getInstance().getCurrent());
5653
} else {
57-
log("Codename One display still not initialized after pump attempts");
54+
log("Codename One display not initialized; continuing with screenshot capture");
5855
}
5956

6057
@MAIN_NAME@Stub activity = controller.get();
@@ -173,58 +170,6 @@ public class @MAIN_NAME@UiTest {
173170
appendLogLine(formatted);
174171
}
175172

176-
private static final AtomicInteger PUMP_INVOCATION_COUNTER = new AtomicInteger();
177-
178-
private static void pumpSchedulers(String label) {
179-
int invocation = PUMP_INVOCATION_COUNTER.incrementAndGet();
180-
long start = System.currentTimeMillis();
181-
boolean verbose = invocation <= 5 || invocation % 10 == 0;
182-
if (verbose) {
183-
log("pumpSchedulers[" + invocation + "] begin (" + label + ")");
184-
}
185-
try {
186-
ShadowLooper.idleMainLooper();
187-
if (verbose) {
188-
log("pumpSchedulers[" + invocation + "] after idleMainLooper");
189-
}
190-
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
191-
if (verbose) {
192-
log("pumpSchedulers[" + invocation + "] after runUiThreadTasksIncludingDelayedTasks");
193-
}
194-
flushScheduler(Robolectric.getForegroundThreadScheduler(), "foreground", invocation, verbose);
195-
flushScheduler(Robolectric.getBackgroundThreadScheduler(), "background", invocation, verbose);
196-
} catch (Throwable t) {
197-
log("pumpSchedulers[" + invocation + "] threw " + t);
198-
throw t;
199-
} finally {
200-
long elapsed = System.currentTimeMillis() - start;
201-
log("pumpSchedulers[" + invocation + "] finished after " + elapsed + "ms (" + label + ")");
202-
}
203-
}
204-
205-
private static void flushScheduler(Scheduler scheduler, String label, int invocation, boolean verbose) {
206-
if (scheduler == null) {
207-
if (verbose) {
208-
log("pumpSchedulers[" + invocation + "] no " + label + " scheduler available");
209-
}
210-
return;
211-
}
212-
int iterations = 0;
213-
while (scheduler.advanceToLastPostedRunnable()) {
214-
iterations++;
215-
if (iterations <= 5 || iterations % 50 == 0) {
216-
log("pumpSchedulers[" + invocation + "] ran " + label + " runnable #" + iterations);
217-
}
218-
if (iterations >= 5000) {
219-
log("pumpSchedulers[" + invocation + "] aborting " + label + " flush after " + iterations + " iterations");
220-
break;
221-
}
222-
}
223-
if (verbose) {
224-
log("pumpSchedulers[" + invocation + "] flushed " + label + " scheduler in " + iterations + " iterations");
225-
}
226-
}
227-
228173
private static synchronized void appendLogLine(String message) {
229174
File directory = resolveArtifactDirectoryForLogging();
230175
if (directory == null) {

0 commit comments

Comments
 (0)