Skip to content

Commit 23d486b

Browse files
authored
Fixed CI to avoid dynamic classloading (#4152)
* Fixed CI to avoid dynamic classloading Also used callSerially to avoid blocking the start method on Android * Removed redundant coverage generated by codex * Cleaned up test code * Ugh * No idea * Giving tests more time * Trying to fix the browser component delay logic * Fixing a lifecycle NPE * Increased timer since the Android emulator is very slow and seems to be missing images
1 parent 03e033f commit 23d486b

9 files changed

+132
-577
lines changed

scripts/device-runner-app/main/HelloCodenameOne.java

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import com.codename1.ui.Label;
1111
import com.codename1.ui.layouts.BorderLayout;
1212
import com.codename1.ui.layouts.BoxLayout;
13+
import com.codename1.ui.CN;
1314

1415
import com.codenameone.examples.hellocodenameone.tests.Cn1ssDeviceRunner;
1516
import com.codenameone.examples.hellocodenameone.tests.Cn1ssDeviceRunnerReporter;
1617

1718
public class HelloCodenameOne {
1819
private Form current;
19-
private Form mainForm;
2020
private static boolean deviceRunnerExecuted;
2121

2222
public void init(Object context) {
@@ -28,11 +28,13 @@ public void start() {
2828
current.show();
2929
return;
3030
}
31-
if (!deviceRunnerExecuted) {
31+
Form c = CN.getCurrentForm();
32+
if (!deviceRunnerExecuted || c == null) {
3233
deviceRunnerExecuted = true;
33-
new Cn1ssDeviceRunner().runSuite();
34+
CN.callSerially(() -> new Cn1ssDeviceRunner().runSuite());
35+
return;
3436
}
35-
showMainForm();
37+
c.show();
3638
}
3739

3840
public void stop() {
@@ -42,59 +44,4 @@ public void stop() {
4244
public void destroy() {
4345
// Nothing to clean up for this sample
4446
}
45-
46-
private void showMainForm() {
47-
if (mainForm == null) {
48-
mainForm = new Form("Main Screen", new BorderLayout());
49-
50-
Container content = new Container(BoxLayout.y());
51-
content.getAllStyles().setBgColor(0x1f2937);
52-
content.getAllStyles().setBgTransparency(255);
53-
content.getAllStyles().setPadding(6, 6, 6, 6);
54-
content.getAllStyles().setFgColor(0xf9fafb);
55-
56-
Label heading = new Label("Hello Codename One");
57-
heading.getAllStyles().setFgColor(0x38bdf8);
58-
heading.getAllStyles().setMargin(0, 4, 0, 0);
59-
60-
Label body = new Label("Instrumentation main activity preview");
61-
body.getAllStyles().setFgColor(0xf9fafb);
62-
63-
Button openBrowser = new Button("Open Browser Screen");
64-
openBrowser.addActionListener(evt -> showBrowserForm());
65-
66-
content.add(heading);
67-
content.add(body);
68-
content.add(openBrowser);
69-
70-
mainForm.add(BorderLayout.CENTER, content);
71-
}
72-
current = mainForm;
73-
mainForm.show();
74-
}
75-
76-
private void showBrowserForm() {
77-
Form browserForm = new Form("Browser Screen", new BorderLayout());
78-
79-
BrowserComponent browser = new BrowserComponent();
80-
browser.setPage(buildBrowserHtml(), null);
81-
browserForm.add(BorderLayout.CENTER, browser);
82-
browserForm.getToolbar().addMaterialCommandToLeftBar(
83-
"Back",
84-
FontImage.MATERIAL_ARROW_BACK,
85-
evt -> showMainForm()
86-
);
87-
88-
current = browserForm;
89-
browserForm.show();
90-
}
91-
92-
private String buildBrowserHtml() {
93-
return "<html><head><meta charset='utf-8'/>"
94-
+ "<style>body{margin:0;font-family:sans-serif;background:#0e1116;color:#f3f4f6;}"
95-
+ ".container{padding:24px;text-align:center;}h1{font-size:24px;margin-bottom:12px;}"
96-
+ "p{font-size:16px;line-height:1.4;}span{color:#4cc9f0;}</style></head>"
97-
+ "<body><div class='container'><h1>Codename One</h1>"
98-
+ "<p>BrowserComponent <span>instrumentation</span> test content.</p></div></body></html>";
99-
}
10047
}
Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
package com.codenameone.examples.hellocodenameone.tests;
22

3-
import com.codename1.testing.AbstractTest;
43
import com.codename1.ui.Component;
54
import com.codename1.ui.Form;
65
import com.codename1.ui.layouts.BorderLayout;
76

8-
abstract class AbstractGraphicsScreenshotTest extends AbstractTest {
7+
abstract class AbstractGraphicsScreenshotTest extends BaseTest {
98
protected abstract Component createContent();
109

1110
protected abstract String screenshotName();
1211

1312
@Override
1413
public boolean runTest() throws Exception {
15-
final Form[] holder = new Form[1];
16-
Cn1ssDeviceRunnerHelper.runOnEdtSync(() -> {
17-
Form form = new Form("Graphics", new BorderLayout());
18-
form.add(BorderLayout.CENTER, createContent());
19-
holder[0] = form;
20-
form.show();
21-
});
22-
23-
Cn1ssDeviceRunnerHelper.waitForMillis(1200);
24-
25-
final boolean[] result = new boolean[1];
26-
Cn1ssDeviceRunnerHelper.runOnEdtSync(() -> {
27-
if (holder[0] != null) {
28-
holder[0].revalidate();
29-
holder[0].repaint();
30-
}
31-
result[0] = Cn1ssDeviceRunnerHelper.emitCurrentFormScreenshot(screenshotName());
32-
});
33-
return result[0];
14+
Form form = createForm("Graphics", new BorderLayout(), screenshotName());
15+
form.add(BorderLayout.CENTER, createContent());
16+
form.show();
17+
return waitForDone();
3418
}
3519
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.codenameone.examples.hellocodenameone.tests;
2+
3+
import com.codename1.testing.AbstractTest;
4+
import com.codename1.ui.Form;
5+
import com.codename1.ui.util.UITimer;
6+
import com.codename1.ui.layouts.Layout;
7+
import com.codename1.testing.TestUtils;
8+
9+
public abstract class BaseTest extends AbstractTest {
10+
private boolean done;
11+
12+
protected Form createForm(String title, Layout layout, final String imageName) {
13+
return new Form(title, layout) {
14+
@Override
15+
protected void onShowCompleted() {
16+
registerReadyCallback(this, () -> {
17+
Cn1ssDeviceRunnerHelper.emitCurrentFormScreenshot(imageName);
18+
done = true;
19+
});
20+
}
21+
};
22+
}
23+
24+
protected void registerReadyCallback(Form parent, Runnable run) {
25+
// Android misses some images when the time is lower
26+
UITimer.timer(1500, false, parent, run);
27+
}
28+
29+
protected boolean waitForDone() {
30+
int timeout = 100;
31+
while(!done) {
32+
TestUtils.waitFor(20);
33+
timeout--;
34+
if(timeout == 0) {
35+
return false;
36+
}
37+
}
38+
// give the test a few additional milliseconds for the screenshot emission
39+
TestUtils.waitFor(100);
40+
return true;
41+
}
42+
}

scripts/device-runner-app/tests/BrowserComponentScreenshotTest.java

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,26 @@
44
import com.codename1.testing.TestUtils;
55
import com.codename1.ui.BrowserComponent;
66
import com.codename1.ui.Form;
7+
import com.codename1.ui.CN;
78
import com.codename1.ui.layouts.BorderLayout;
89

9-
public class BrowserComponentScreenshotTest extends AbstractTest {
10+
public class BrowserComponentScreenshotTest extends BaseTest {
11+
private BrowserComponent browser;
1012
@Override
1113
public boolean runTest() throws Exception {
12-
final boolean[] supported = new boolean[1];
13-
Cn1ssDeviceRunnerHelper.runOnEdtSync(() -> supported[0] = BrowserComponent.isNativeBrowserSupported());
14-
if (!supported[0]) {
15-
TestUtils.log("BrowserComponent native support unavailable; skipping screenshot test");
14+
if (!BrowserComponent.isNativeBrowserSupported()) {
1615
return true;
1716
}
17+
Form form = createForm("Browser Test", new BorderLayout(), "BrowserComponent");
18+
browser = new BrowserComponent();
19+
browser.setPage(buildHtml(), null);
20+
form.add(BorderLayout.CENTER, browser);
21+
form.show();
22+
return waitForDone();
23+
}
1824

19-
final boolean[] loadFinished = new boolean[1];
20-
final Form[] formHolder = new Form[1];
21-
Cn1ssDeviceRunnerHelper.runOnEdtSync(() -> {
22-
Form form = new Form("Browser Test", new BorderLayout());
23-
BrowserComponent browser = new BrowserComponent();
24-
browser.addWebEventListener(BrowserComponent.onLoad, evt -> loadFinished[0] = true);
25-
browser.setPage(buildHtml(), null);
26-
form.add(BorderLayout.CENTER, browser);
27-
formHolder[0] = form;
28-
form.show();
29-
});
30-
31-
for (int elapsed = 0; elapsed < 15000 && !loadFinished[0]; elapsed += 200) {
32-
TestUtils.waitFor(200);
33-
}
34-
if (!loadFinished[0]) {
35-
TestUtils.log("BrowserComponent content did not finish loading in time");
36-
return false;
37-
}
38-
39-
Cn1ssDeviceRunnerHelper.waitForMillis(3000);
40-
41-
final boolean[] result = new boolean[1];
42-
Cn1ssDeviceRunnerHelper.runOnEdtSync(() -> {
43-
Form current = formHolder[0];
44-
if (current != null) {
45-
current.revalidate();
46-
current.repaint();
47-
}
48-
result[0] = Cn1ssDeviceRunnerHelper.emitCurrentFormScreenshot("BrowserComponent");
49-
});
50-
return result[0];
25+
protected void registerReadyCallback(Form parent, final Runnable run) {
26+
browser.addWebEventListener(BrowserComponent.onLoad, evt -> CN.callSerially(run));
5127
}
5228

5329
private static String buildHtml() {

scripts/device-runner-app/tests/Cn1ssDeviceRunner.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,40 @@
44
import com.codename1.testing.TestReporting;
55
import com.codename1.ui.Display;
66
import com.codename1.ui.Form;
7+
import com.codename1.testing.AbstractTest;
78

89
public final class Cn1ssDeviceRunner extends DeviceRunner {
9-
private static final String[] TEST_CLASSES = new String[] {
10-
MainScreenScreenshotTest.class.getName(),
11-
BrowserComponentScreenshotTest.class.getName(),
12-
MediaPlaybackScreenshotTest.class.getName(),
13-
GraphicsPipelineScreenshotTest.class.getName(),
14-
GraphicsShapesAndGradientsScreenshotTest.class.getName(),
15-
GraphicsStateAndTextScreenshotTest.class.getName(),
16-
GraphicsTransformationsScreenshotTest.class.getName(),
17-
GraphicsMethodsScreenshotTest.class.getName()
10+
private static final AbstractTest[] TEST_CLASSES = new AbstractTest[] {
11+
new MainScreenScreenshotTest(),
12+
new GraphicsPipelineScreenshotTest(),
13+
new GraphicsShapesAndGradientsScreenshotTest(),
14+
new GraphicsStateAndTextScreenshotTest(),
15+
new GraphicsTransformationsScreenshotTest(),
16+
new BrowserComponentScreenshotTest(),
17+
new MediaPlaybackScreenshotTest()
1818
};
1919

2020
public void runSuite() {
21-
for (String testClass : TEST_CLASSES) {
22-
runTest(testClass);
21+
for (AbstractTest testClass : TEST_CLASSES) {
22+
log("CN1SS:INFO:suite starting test=" + testClass);
23+
try {
24+
testClass.prepare();
25+
testClass.runTest();
26+
testClass.cleanup();
27+
log("CN1SS:INFO:suite finished test=" + testClass);
28+
} catch (Throwable t) {
29+
log("CN1SS:ERR:suite test=" + testClass + " failed=" + t);
30+
t.printStackTrace();
31+
}
2332
}
33+
log("CN1SS:SUITE:FINISHED");
2434
TestReporting.getInstance().testExecutionFinished(getClass().getName());
2535
}
2636

37+
private static void log(String msg) {
38+
System.out.println(msg);
39+
}
40+
2741
@Override
2842
protected void startApplicationInstance() {
2943
Cn1ssDeviceRunnerHelper.runOnEdtSync(() -> {

0 commit comments

Comments
 (0)