Skip to content

Commit 1956c39

Browse files
committed
Prevent Robolectric wait loops from stalling
1 parent 741827c commit 1956c39

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

scripts/templates/HelloCodenameOneUiTest.java.tmpl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import static org.junit.Assert.assertTrue;
55

66
import android.graphics.Bitmap;
77
import android.graphics.Canvas;
8-
import android.os.SystemClock;
98
import android.util.DisplayMetrics;
109
import android.view.View;
1110

@@ -36,7 +35,7 @@ public class @MAIN_NAME@UiTest {
3635
pumpSchedulers();
3736
try {
3837
log("Waiting for Codename One main form to be displayed");
39-
Form displayed = waitForDisplayedForm(5000L);
38+
Form displayed = waitForDisplayedForm(10000L);
4039
assertNotNull("Codename One form should be displayed", displayed);
4140

4241
@MAIN_NAME@Stub activity = controller.get();
@@ -57,9 +56,9 @@ public class @MAIN_NAME@UiTest {
5756
}
5857

5958
private static Form waitForDisplayedForm(long timeoutMillis) throws InterruptedException {
60-
long deadline = SystemClock.uptimeMillis() + timeoutMillis;
61-
long nextLog = SystemClock.uptimeMillis();
62-
while (SystemClock.uptimeMillis() < deadline) {
59+
long start = System.currentTimeMillis();
60+
long nextLog = start;
61+
while (System.currentTimeMillis() - start < timeoutMillis) {
6362
pumpSchedulers();
6463
if (Display.isInitialized()) {
6564
Form current = Display.getInstance().getCurrent();
@@ -68,7 +67,7 @@ public class @MAIN_NAME@UiTest {
6867
}
6968
}
7069
Thread.sleep(50L);
71-
long now = SystemClock.uptimeMillis();
70+
long now = System.currentTimeMillis();
7271
if (now >= nextLog) {
7372
log("Still waiting for Codename One form; displayInitialized=" + Display.isInitialized());
7473
nextLog = now + 1000L;
@@ -79,7 +78,7 @@ public class @MAIN_NAME@UiTest {
7978
}
8079

8180
private static Bitmap captureScreenshot(@MAIN_NAME@Stub activity) throws InterruptedException {
82-
View decorView = waitForDecorViewWithDimensions(activity, 5000L);
81+
View decorView = waitForDecorViewWithDimensions(activity, 10000L);
8382
Bitmap bitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Bitmap.Config.ARGB_8888);
8483
Canvas canvas = new Canvas(bitmap);
8584
decorView.draw(canvas);
@@ -130,9 +129,9 @@ public class @MAIN_NAME@UiTest {
130129
}
131130

132131
private static View waitForDecorViewWithDimensions(@MAIN_NAME@Stub activity, long timeoutMillis) throws InterruptedException {
133-
long deadline = SystemClock.uptimeMillis() + timeoutMillis;
134-
long nextLog = SystemClock.uptimeMillis();
135-
while (SystemClock.uptimeMillis() < deadline) {
132+
long start = System.currentTimeMillis();
133+
long nextLog = start;
134+
while (System.currentTimeMillis() - start < timeoutMillis) {
136135
View decorView = activity.getWindow().getDecorView();
137136
if (decorView != null) {
138137
ensureViewHasLayout(decorView);
@@ -142,9 +141,10 @@ public class @MAIN_NAME@UiTest {
142141
}
143142
pumpSchedulers();
144143
Thread.sleep(50L);
145-
long now = SystemClock.uptimeMillis();
144+
long now = System.currentTimeMillis();
146145
if (now >= nextLog) {
147-
log("Waiting for decor view layout; width=" + (decorView != null ? decorView.getWidth() : 0)
146+
log("Waiting for decor view layout; decorPresent=" + (decorView != null)
147+
+ " width=" + (decorView != null ? decorView.getWidth() : 0)
148148
+ " height=" + (decorView != null ? decorView.getHeight() : 0));
149149
nextLog = now + 1000L;
150150
}
@@ -169,6 +169,7 @@ public class @MAIN_NAME@UiTest {
169169
}
170170

171171
private static void pumpSchedulers() {
172+
ShadowLooper.idleMainLooper();
172173
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
173174
Robolectric.flushForegroundThreadScheduler();
174175
Robolectric.flushBackgroundThreadScheduler();

0 commit comments

Comments
 (0)