Skip to content

Commit 38bb5d7

Browse files
committed
Expand Robolectric logging for UI test startup
1 parent 6e39cdc commit 38bb5d7

File tree

1 file changed

+47
-65
lines changed

1 file changed

+47
-65
lines changed

scripts/templates/HelloCodenameOneUiTest.java.tmpl

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package @PACKAGE@;
22

3-
import static org.junit.Assert.assertNotNull;
43
import static org.junit.Assert.assertTrue;
54

65
import android.graphics.Bitmap;
@@ -9,7 +8,6 @@ import android.util.DisplayMetrics;
98
import android.view.View;
109

1110
import com.codename1.ui.Display;
12-
import com.codename1.ui.Form;
1311

1412
import java.io.File;
1513
import java.io.FileOutputStream;
@@ -35,54 +33,63 @@ public class @MAIN_NAME@UiTest {
3533
@Test
3634
public void mainFormScreenshotContainsRenderedContent() throws Exception {
3735
log("Starting Robolectric activity for screenshot test");
38-
ActivityController<@MAIN_NAME@Stub> controller = Robolectric.buildActivity(@[email protected]).setup().visible();
39-
pumpSchedulers();
36+
ActivityController<@MAIN_NAME@Stub> controller = Robolectric.buildActivity(@[email protected]);
37+
log("ActivityController created");
38+
controller.create();
39+
log("Activity created (Display.isInitialized=" + Display.isInitialized() + ")");
40+
controller.start();
41+
log("Activity started");
42+
controller.resume();
43+
log("Activity resumed");
44+
controller.visible();
45+
log("Activity made visible");
46+
4047
try {
41-
log("Waiting for Codename One main form to be displayed");
42-
Form displayed = waitForDisplayedForm(10000L);
43-
assertNotNull("Codename One form should be displayed", displayed);
48+
for (int attempt = 1; attempt <= 5; attempt++) {
49+
log("Scheduler pump attempt " + attempt + " (Display.isInitialized=" + Display.isInitialized() + ")");
50+
pumpSchedulers("attempt-" + attempt);
51+
}
52+
53+
if (Display.isInitialized()) {
54+
log("Codename One display reports current form="
55+
+ Display.getInstance().getCurrent());
56+
} else {
57+
log("Codename One display still not initialized after pump attempts");
58+
}
4459

4560
@MAIN_NAME@Stub activity = controller.get();
46-
log("Captured Codename One form; capturing decor view screenshot");
61+
log("Capturing decor view screenshot without additional waits");
4762
Bitmap screenshot = captureScreenshot(activity);
48-
assertNotNull("Screenshot capture should succeed", screenshot);
63+
assertTrue("Screenshot capture should succeed", screenshot != null);
64+
log("Screenshot dimensions=" + screenshot.getWidth() + "x" + screenshot.getHeight());
4965
assertTrue("Screenshot width should be positive", screenshot.getWidth() > 0);
5066
assertTrue("Screenshot height should be positive", screenshot.getHeight() > 0);
51-
assertTrue("Screenshot should contain rendered content beyond the background", hasRenderableContent(screenshot));
67+
boolean hasContent = hasRenderableContent(screenshot);
68+
log("Screenshot content analysis result=" + hasContent);
69+
assertTrue("Screenshot should contain rendered content beyond the background", hasContent);
5270

5371
File screenshotFile = saveScreenshot(screenshot);
5472
log("Screenshot stored at " + screenshotFile.getAbsolutePath());
5573
assertTrue("Screenshot file should exist", screenshotFile.isFile());
5674
assertTrue("Screenshot file should not be empty", screenshotFile.length() > 0L);
5775
} finally {
58-
controller.pause().stop().destroy();
76+
controller.pause();
77+
log("Activity paused");
78+
controller.stop();
79+
log("Activity stopped");
80+
controller.destroy();
81+
log("Activity destroyed");
5982
}
6083
}
6184

62-
private static Form waitForDisplayedForm(long timeoutMillis) throws InterruptedException {
63-
long start = System.currentTimeMillis();
64-
long nextLog = start;
65-
while (System.currentTimeMillis() - start < timeoutMillis) {
66-
pumpSchedulers();
67-
if (Display.isInitialized()) {
68-
Form current = Display.getInstance().getCurrent();
69-
if (current != null) {
70-
return current;
71-
}
72-
}
73-
Thread.sleep(50L);
74-
long now = System.currentTimeMillis();
75-
if (now >= nextLog) {
76-
log("Still waiting for Codename One form; displayInitialized=" + Display.isInitialized());
77-
nextLog = now + 1000L;
78-
}
85+
private static Bitmap captureScreenshot(@MAIN_NAME@Stub activity) {
86+
View decorView = activity.getWindow().getDecorView();
87+
log("Decor view instance=" + decorView);
88+
if (decorView == null) {
89+
throw new AssertionError("Activity decor view was null");
7990
}
80-
log("Timed out waiting for Codename One form; Display.isInitialized=" + Display.isInitialized());
81-
throw new AssertionError("Timed out waiting for Codename One form to be displayed");
82-
}
83-
84-
private static Bitmap captureScreenshot(@MAIN_NAME@Stub activity) throws InterruptedException {
85-
View decorView = waitForDecorViewWithDimensions(activity, 10000L);
91+
ensureViewHasLayout(decorView);
92+
log("Decor view layout dimensions after ensure: " + decorView.getWidth() + "x" + decorView.getHeight());
8693
Bitmap bitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Bitmap.Config.ARGB_8888);
8794
Canvas canvas = new Canvas(bitmap);
8895
decorView.draw(canvas);
@@ -147,40 +154,17 @@ public class @MAIN_NAME@UiTest {
147154
}
148155
}
149156

150-
private static View waitForDecorViewWithDimensions(@MAIN_NAME@Stub activity, long timeoutMillis) throws InterruptedException {
151-
long start = System.currentTimeMillis();
152-
long nextLog = start;
153-
while (System.currentTimeMillis() - start < timeoutMillis) {
154-
View decorView = activity.getWindow().getDecorView();
155-
if (decorView != null) {
156-
ensureViewHasLayout(decorView);
157-
if (decorView.getWidth() > 0 && decorView.getHeight() > 0) {
158-
return decorView;
159-
}
160-
}
161-
pumpSchedulers();
162-
Thread.sleep(50L);
163-
long now = System.currentTimeMillis();
164-
if (now >= nextLog) {
165-
log("Waiting for decor view layout; decorPresent=" + (decorView != null)
166-
+ " width=" + (decorView != null ? decorView.getWidth() : 0)
167-
+ " height=" + (decorView != null ? decorView.getHeight() : 0));
168-
nextLog = now + 1000L;
169-
}
170-
}
171-
log("Timed out waiting for decor view layout");
172-
throw new AssertionError("Timed out waiting for decor view layout");
173-
}
174-
175157
private static void ensureViewHasLayout(View view) {
176158
if (view.getWidth() > 0 && view.getHeight() > 0) {
159+
log("ensureViewHasLayout: view already laid out with size " + view.getWidth() + "x" + view.getHeight());
177160
return;
178161
}
179162
DisplayMetrics metrics = view.getResources().getDisplayMetrics();
180163
int widthSpec = View.MeasureSpec.makeMeasureSpec(metrics.widthPixels, View.MeasureSpec.EXACTLY);
181164
int heightSpec = View.MeasureSpec.makeMeasureSpec(metrics.heightPixels, View.MeasureSpec.EXACTLY);
182165
view.measure(widthSpec, heightSpec);
183166
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
167+
log("ensureViewHasLayout: forced layout to " + view.getWidth() + "x" + view.getHeight());
184168
}
185169

186170
private static void log(String message) {
@@ -191,12 +175,12 @@ public class @MAIN_NAME@UiTest {
191175

192176
private static final AtomicInteger PUMP_INVOCATION_COUNTER = new AtomicInteger();
193177

194-
private static void pumpSchedulers() {
178+
private static void pumpSchedulers(String label) {
195179
int invocation = PUMP_INVOCATION_COUNTER.incrementAndGet();
196180
long start = System.currentTimeMillis();
197181
boolean verbose = invocation <= 5 || invocation % 10 == 0;
198182
if (verbose) {
199-
log("pumpSchedulers[" + invocation + "] begin");
183+
log("pumpSchedulers[" + invocation + "] begin (" + label + ")");
200184
}
201185
try {
202186
ShadowLooper.idleMainLooper();
@@ -213,10 +197,8 @@ public class @MAIN_NAME@UiTest {
213197
log("pumpSchedulers[" + invocation + "] threw " + t);
214198
throw t;
215199
} finally {
216-
if (verbose) {
217-
long elapsed = System.currentTimeMillis() - start;
218-
log("pumpSchedulers[" + invocation + "] finished after " + elapsed + "ms");
219-
}
200+
long elapsed = System.currentTimeMillis() - start;
201+
log("pumpSchedulers[" + invocation + "] finished after " + elapsed + "ms (" + label + ")");
220202
}
221203
}
222204

0 commit comments

Comments
 (0)