Skip to content

Commit c28c339

Browse files
Port 9 sample apps to core unit tests (#4226)
Ported the following samples to unit tests in maven/core-unittests: - TextSelectionSample - ToastBarSample - ToolbarRTLTest - UIFragmentSample - UnicodeFontsSample - UnitTests - UpdateTextAreaWhileEditingTest - VerticalAlignTTFFontTest2798 - VideoPlayerSample Adapted logic to run in headless environment using UITestBase. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 14af4d5 commit c28c339

9 files changed

+848
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.components.SpanLabel;
4+
import com.codename1.ui.CN;
5+
import static com.codename1.ui.CN.*;
6+
import com.codename1.ui.Container;
7+
import com.codename1.ui.Display;
8+
import com.codename1.ui.Form;
9+
import com.codename1.ui.Label;
10+
import com.codename1.ui.TextArea;
11+
import com.codename1.ui.TextField;
12+
import com.codename1.ui.TextSelection;
13+
import com.codename1.ui.layouts.BoxLayout;
14+
import com.codename1.ui.plaf.Border;
15+
import com.codename1.junit.UITestBase;
16+
import com.codename1.junit.FormTest;
17+
import static com.codename1.ui.ComponentSelector.$;
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
public class TextSelectionSampleTest extends UITestBase {
21+
22+
@FormTest
23+
public void testTextSelectionSample() {
24+
Form hi = new Form("Hi World", BoxLayout.y());
25+
hi.setScrollableY(false);
26+
TextSelection sel = hi.getTextSelection();
27+
final boolean[] selectionChanged = new boolean[1];
28+
sel.addTextSelectionListener(e->{
29+
selectionChanged[0] = true;
30+
});
31+
32+
sel.setEnabled(true);
33+
Label label = new Label("This label should be selectable");
34+
label.setTextSelectionEnabled(true);
35+
Label label2 = new Label("Some more text");
36+
label2.setTextSelectionEnabled(true);
37+
hi.add(label);
38+
hi.add(new TextField("Hello Universe"));
39+
hi.add(label2);
40+
hi.add(new Label("Hi World"));
41+
42+
Container cnt = new Container(BoxLayout.x());
43+
cnt.setScrollableX(true);
44+
cnt.getStyle().setBorder(Border.createLineBorder(1, 0x0));
45+
cnt.setPreferredH(CN.convertToPixels(5));
46+
cnt.setPreferredW(CN.convertToPixels(20));
47+
48+
TextArea ta = new TextArea();
49+
ta.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
50+
ta.setEnabled(false);
51+
ta.setRows(6);
52+
hi.add(ta);
53+
54+
SpanLabel sl = new SpanLabel();
55+
sl.setText(ta.getText());
56+
sl.setTextSelectionEnabled(true);
57+
hi.add(sl);
58+
59+
TextField tf = new TextField();
60+
tf.setText("Hello World. This is a test field");
61+
tf.setEnabled(false);
62+
hi.add(tf);
63+
64+
Label l = new Label("This is a test with some long text to see if this works.");
65+
l.setTextSelectionEnabled(true);
66+
cnt.add(l);
67+
68+
Container cntY = new Container(BoxLayout.y());
69+
cntY.setScrollableY(true);
70+
cntY.getStyle().setBorder(Border.createLineBorder(1, 0x0));
71+
for (int i=0; i<50; i++) {
72+
Label li = new Label("List item "+i);
73+
li.setTextSelectionEnabled(true);
74+
cntY.add(li);
75+
}
76+
hi.add(cnt);
77+
hi.add(cntY);
78+
79+
$(cnt, cntY).selectAllStyles().setMarginMillimeters(4);
80+
81+
hi.show();
82+
waitForForm(hi);
83+
84+
// Verification
85+
assertTrue(label.isTextSelectionEnabled(), "Label should have text selection enabled");
86+
assertTrue(sl.isTextSelectionEnabled(), "SpanLabel should have text selection enabled");
87+
assertNotNull(hi.getTextSelection(), "Form should have a TextSelection object");
88+
}
89+
90+
private void waitForForm(Form form) {
91+
long start = System.currentTimeMillis();
92+
while (System.currentTimeMillis() - start < 3000) {
93+
if (Display.getInstance().getCurrent() == form) {
94+
return;
95+
}
96+
try {
97+
Thread.sleep(20);
98+
} catch (InterruptedException e) {
99+
// Ignore
100+
}
101+
}
102+
fail("Form did not become current in time");
103+
}
104+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.components.ToastBar;
4+
import com.codename1.ui.Button;
5+
import com.codename1.ui.CN;
6+
import com.codename1.ui.Display;
7+
import com.codename1.ui.Form;
8+
import com.codename1.junit.UITestBase;
9+
import com.codename1.junit.FormTest;
10+
import com.codename1.ui.layouts.BoxLayout;
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
public class ToastBarSampleTest extends UITestBase {
14+
15+
@FormTest
16+
public void testToastBarSample() {
17+
CN.setProperty("Component.revalidateOnStyleChange", "false");
18+
CN.setProperty("Form.revalidateFromRoot", "false");
19+
20+
Form hi = new Form("Hi World", BoxLayout.y());
21+
Button btn = new Button("Test");
22+
23+
final boolean[] toastShown = new boolean[1];
24+
// We can't easily intercept ToastBar.showInfoMessage in a test without UI interaction or mocking,
25+
// but we can verify the button action doesn't crash.
26+
27+
btn.addActionListener(evt->{
28+
ToastBar.showInfoMessage("This is a toastbar message");
29+
toastShown[0] = true;
30+
});
31+
hi.add(btn);
32+
hi.show();
33+
waitForForm(hi);
34+
35+
btn.pressed();
36+
btn.released();
37+
38+
assertTrue(toastShown[0], "Button action listener should have been triggered");
39+
}
40+
41+
private void waitForForm(Form form) {
42+
long start = System.currentTimeMillis();
43+
while (System.currentTimeMillis() - start < 3000) {
44+
if (Display.getInstance().getCurrent() == form) {
45+
return;
46+
}
47+
try {
48+
Thread.sleep(20);
49+
} catch (InterruptedException e) {
50+
// Ignore
51+
}
52+
}
53+
fail("Form did not become current in time");
54+
}
55+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.ui.Command;
4+
import com.codename1.ui.Display;
5+
import com.codename1.ui.Form;
6+
import com.codename1.ui.Label;
7+
import com.codename1.ui.Toolbar;
8+
import com.codename1.junit.UITestBase;
9+
import com.codename1.junit.FormTest;
10+
import com.codename1.ui.events.ActionEvent;
11+
import com.codename1.ui.layouts.BoxLayout;
12+
import com.codename1.ui.plaf.UIManager;
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
public class ToolbarRTLSampleTest extends UITestBase {
16+
17+
@FormTest
18+
public void testToolbarRTL() {
19+
UIManager.getInstance().getLookAndFeel().setRTL(true);
20+
try {
21+
Form hi = new Form("Hi World", BoxLayout.y());
22+
Toolbar tb = new Toolbar();
23+
hi.setToolbar(tb);
24+
25+
final boolean[] actionPerformed = new boolean[1];
26+
Command cmd = new Command("Test") {
27+
public void actionPerformed(ActionEvent e) {
28+
actionPerformed[0] = true;
29+
}
30+
};
31+
32+
tb.addCommandToLeftSideMenu(cmd);
33+
hi.add(new Label("Hi World"));
34+
hi.show();
35+
waitForForm(hi);
36+
37+
assertTrue(UIManager.getInstance().getLookAndFeel().isRTL(), "LookAndFeel should be RTL");
38+
39+
// Simulate command execution
40+
cmd.actionPerformed(new ActionEvent(tb));
41+
assertTrue(actionPerformed[0], "Command action should be executed");
42+
43+
} finally {
44+
UIManager.getInstance().getLookAndFeel().setRTL(false);
45+
}
46+
}
47+
48+
private void waitForForm(Form form) {
49+
long start = System.currentTimeMillis();
50+
while (System.currentTimeMillis() - start < 3000) {
51+
if (Display.getInstance().getCurrent() == form) {
52+
return;
53+
}
54+
try {
55+
Thread.sleep(20);
56+
} catch (InterruptedException e) {
57+
// Ignore
58+
}
59+
}
60+
fail("Form did not become current in time");
61+
}
62+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.ui.Button;
4+
import com.codename1.ui.ComboBox;
5+
import com.codename1.ui.Display;
6+
import com.codename1.ui.Form;
7+
import com.codename1.ui.TextArea;
8+
import com.codename1.ui.Toolbar;
9+
import com.codename1.ui.UIFragment;
10+
import com.codename1.junit.UITestBase;
11+
import com.codename1.junit.FormTest;
12+
import com.codename1.ui.layouts.BorderLayout;
13+
import com.codename1.ui.layouts.BoxLayout;
14+
import com.codename1.ui.plaf.RoundRectBorder;
15+
import static com.codename1.ui.ComponentSelector.$;
16+
import static org.junit.jupiter.api.Assertions.*;
17+
18+
public class UIFragmentSampleTest extends UITestBase {
19+
20+
@FormTest
21+
public void testUIFragmentSample() {
22+
Form f = new Form("Test Fragments", BoxLayout.y());
23+
TextArea ta = new TextArea();
24+
ta.setMaxSize(5000);
25+
26+
String[] examples = new String[]{
27+
"<borderAbs><$button1 constraint='center'/><xng constraint='south'><$button2/><$button3/><$button4/></xng></borderAbs>",
28+
"{centerAbs:$button1, south:{xng:[$button2, $button3, $button4]}}"
29+
};
30+
31+
ComboBox<String> cb = new ComboBox<>(examples);
32+
cb.addActionListener(e->{
33+
ta.setText(examples[cb.getSelectedIndex()]);
34+
});
35+
36+
ta.setText("<borderAbs><$button1 constraint='center'/><xng constraint='south'><$button2/><$button3/><$button4/></xng></borderAbs>");
37+
Button b = new Button("Compile");
38+
final boolean[] compiled = new boolean[1];
39+
40+
b.addActionListener(e->{
41+
Form f2 = new Form("Result", new BorderLayout());
42+
f2.setToolbar(new Toolbar());
43+
44+
Button b1 = new Button("Button 1");
45+
Button b2 = new Button("Button 2");
46+
Button b3 = new Button("Button 3");
47+
Button b4 = new Button("Button 4");
48+
$(b1, b2, b3, b4).selectAllStyles().setBorder(RoundRectBorder.create().cornerRadius(2)).setBgColor(0x003399).setBgTransparency(0xff);
49+
50+
UIFragment frag;
51+
try {
52+
if (ta.getText().charAt(0) == '<') {
53+
frag = UIFragment.parseXML(ta.getText());
54+
} else {
55+
frag = UIFragment.parseJSON(ta.getText());
56+
}
57+
58+
f2.add(BorderLayout.CENTER,frag
59+
.set("button1", b1)
60+
.set("button2", b2)
61+
.set("button3", b3)
62+
.set("button4", b4)
63+
.getView()
64+
);
65+
f2.show();
66+
compiled[0] = true;
67+
} catch (Exception ex) {
68+
fail("Parsing failed: " + ex.getMessage());
69+
}
70+
});
71+
ta.setRows(5);
72+
73+
f.addAll(cb, ta, b);
74+
f.show();
75+
waitForForm(f);
76+
77+
// Trigger compile with XML
78+
b.pressed();
79+
b.released();
80+
assertTrue(compiled[0], "Should have compiled XML fragment");
81+
82+
// Reset and try JSON
83+
compiled[0] = false;
84+
ta.setText("{centerAbs:$button1, south:{xng:[$button2, $button3, $button4]}}");
85+
b.pressed();
86+
b.released();
87+
assertTrue(compiled[0], "Should have compiled JSON fragment");
88+
}
89+
90+
private void waitForForm(Form form) {
91+
long start = System.currentTimeMillis();
92+
while (System.currentTimeMillis() - start < 3000) {
93+
if (Display.getInstance().getCurrent() == form) {
94+
return;
95+
}
96+
try {
97+
Thread.sleep(20);
98+
} catch (InterruptedException e) {
99+
// Ignore
100+
}
101+
}
102+
fail("Form did not become current in time");
103+
}
104+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.codename1.samples;
2+
3+
import com.codename1.components.SpanLabel;
4+
import com.codename1.ui.Display;
5+
import com.codename1.ui.Font;
6+
import com.codename1.ui.Form;
7+
import com.codename1.ui.Label;
8+
import com.codename1.junit.UITestBase;
9+
import com.codename1.junit.FormTest;
10+
import com.codename1.ui.layouts.BoxLayout;
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
public class UnicodeFontsSampleTest extends UITestBase {
14+
15+
@FormTest
16+
public void testUnicodeFontsSample() {
17+
Form hi = new Form("Hi World", BoxLayout.y());
18+
Label l = new Label("重新开始重新开始");
19+
l.getStyle().setFont(Font.createTrueTypeFont(Font.NATIVE_MAIN_REGULAR, 5f));
20+
hi.add(l);
21+
22+
String emoji = "Here is an 重新开始重新开始 alien: \uD83D\uDC7D! ";
23+
StringBuilder s = new StringBuilder();
24+
for(int i = 0 ; i < 30 ; i++) {
25+
s.append(emoji);
26+
}
27+
SpanLabel sl = new SpanLabel(s.toString());
28+
sl.getTextAllStyles().setFont(Font.createTrueTypeFont(Font.NATIVE_MAIN_REGULAR, 3f));
29+
hi.add(sl);
30+
hi.show();
31+
waitForForm(hi);
32+
33+
assertNotNull(l.getStyle().getFont(), "Label font should be set");
34+
// We verify that the form is shown and components are added without crashing.
35+
assertEquals(2, hi.getComponentCount(), "Form should have 2 components");
36+
}
37+
38+
private void waitForForm(Form form) {
39+
long start = System.currentTimeMillis();
40+
while (System.currentTimeMillis() - start < 3000) {
41+
if (Display.getInstance().getCurrent() == form) {
42+
return;
43+
}
44+
try {
45+
Thread.sleep(20);
46+
} catch (InterruptedException e) {
47+
// Ignore
48+
}
49+
}
50+
fail("Form did not become current in time");
51+
}
52+
}

0 commit comments

Comments
 (0)