Skip to content

Commit 3b81831

Browse files
Improve core unit tests coverage (#4257)
Added tests for: - RangeCategorySeries - SignatureComponent$SignatureDialogBody (via reflection) - WebBrowser$Loading - WebServiceProxyCall (various types) - UiBinding$RadioListAdapter - PushAction - Tree$StringArrayTreeModel - Node$RenderableContainer (via reflection) Addressed coverage for SQLMap$SelectBuilder by relying on existing crash test as class is unusable in current state. Attempted coverage for SideMenuBar$CommandWrapper$ShowWaiter but encountered environment resource issues in headless mode. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 1d4aaf2 commit 3b81831

File tree

10 files changed

+362
-0
lines changed

10 files changed

+362
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.codename1.charts.models;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import org.junit.jupiter.api.Assertions;
6+
7+
public class RangeCategorySeriesTest extends UITestBase {
8+
9+
@FormTest
10+
public void testRangeCategorySeries() {
11+
RangeCategorySeries series = new RangeCategorySeries("Test Series");
12+
Assertions.assertEquals("Test Series", series.getTitle());
13+
14+
series.add(10.0, 20.0);
15+
series.add("Category1", 5.0, 15.0);
16+
17+
Assertions.assertEquals(2, series.getItemCount());
18+
19+
Assertions.assertEquals(10.0, series.getMinimumValue(0), 0.001);
20+
Assertions.assertEquals(20.0, series.getMaximumValue(0), 0.001);
21+
22+
Assertions.assertEquals(5.0, series.getMinimumValue(1), 0.001);
23+
Assertions.assertEquals(15.0, series.getMaximumValue(1), 0.001);
24+
25+
series.remove(0);
26+
Assertions.assertEquals(1, series.getItemCount());
27+
Assertions.assertEquals(5.0, series.getMinimumValue(0), 0.001);
28+
29+
series.clear();
30+
Assertions.assertEquals(0, series.getItemCount());
31+
}
32+
33+
@FormTest
34+
public void testToXYSeries() {
35+
RangeCategorySeries series = new RangeCategorySeries("Test Series");
36+
series.add(10.0, 20.0);
37+
series.add(30.0, 40.0);
38+
39+
XYSeries xy = series.toXYSeries();
40+
Assertions.assertEquals("Test Series", xy.getTitle());
41+
// Each add in RangeCategorySeries adds 2 points in XYSeries (min and max)
42+
Assertions.assertEquals(4, xy.getItemCount());
43+
44+
// Check values (implementation uses hack k+1 and k+1.000001)
45+
Assertions.assertEquals(1.0, xy.getX(0), 0.001);
46+
Assertions.assertEquals(10.0, xy.getY(0), 0.001);
47+
48+
Assertions.assertEquals(1.000001, xy.getX(1), 0.001);
49+
Assertions.assertEquals(20.0, xy.getY(1), 0.001);
50+
51+
Assertions.assertEquals(2.0, xy.getX(2), 0.001);
52+
Assertions.assertEquals(30.0, xy.getY(2), 0.001);
53+
}
54+
}

maven/core-unittests/src/test/java/com/codename1/components/SignatureComponentTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.codename1.junit.UITestBase;
55
import com.codename1.ui.Button;
66
import com.codename1.ui.Component;
7+
import com.codename1.ui.Container;
8+
import com.codename1.ui.Form;
79
import com.codename1.ui.Dialog;
810
import com.codename1.ui.Display;
911
import com.codename1.ui.DisplayTest;
@@ -98,6 +100,61 @@ private Button getLeadButton(SignatureComponent component) throws Exception {
98100
return (Button) leadField.get(component);
99101
}
100102

103+
@FormTest
104+
public void testDialogInteraction() throws Exception {
105+
SignatureComponent cmp = new SignatureComponent();
106+
107+
// Use reflection to instantiate SignatureDialogBody directly, bypassing Dialog.show() issues
108+
Class<?>[] innerClasses = SignatureComponent.class.getDeclaredClasses();
109+
Class<?> dialogBodyClass = null;
110+
for (Class<?> cls : innerClasses) {
111+
if (cls.getSimpleName().equals("SignatureDialogBody")) {
112+
dialogBodyClass = cls;
113+
break;
114+
}
115+
}
116+
assertNotNull(dialogBodyClass, "SignatureDialogBody inner class not found");
117+
118+
java.lang.reflect.Constructor<?> ctor = dialogBodyClass.getDeclaredConstructor(SignatureComponent.class);
119+
ctor.setAccessible(true);
120+
Container body = (Container) ctor.newInstance(cmp);
121+
122+
Form f = new Form("Test Form", new com.codename1.ui.layouts.BorderLayout());
123+
f.addComponent(com.codename1.ui.layouts.BorderLayout.CENTER, body);
124+
f.show();
125+
126+
// Now find buttons in the body
127+
Button doneButton = findButtonByText(body, "Save");
128+
Button resetButton = findButtonByText(body, "Reset");
129+
Button cancelButton = findButtonByText(body, "Cancel");
130+
131+
assertNotNull(doneButton, "Save button not found");
132+
assertNotNull(resetButton, "Reset button not found");
133+
assertNotNull(cancelButton, "Cancel button not found");
134+
135+
// Test Reset
136+
resetButton.pointerReleased(0, 0);
137+
138+
// Test Cancel
139+
cancelButton.pointerReleased(0, 0);
140+
}
141+
142+
private Button findButtonByText(Container cnt, String text) {
143+
for(int i=0; i<cnt.getComponentCount(); i++) {
144+
Component c = cnt.getComponentAt(i);
145+
if(c instanceof Button) {
146+
if(((Button)c).getText().equals(text)) {
147+
return (Button)c;
148+
}
149+
}
150+
if(c instanceof Container) {
151+
Button b = findButtonByText((Container)c, text);
152+
if(b != null) return b;
153+
}
154+
}
155+
return null;
156+
}
157+
101158
private static class StubImage extends Image {
102159
private final int width;
103160
private final int height;

maven/core-unittests/src/test/java/com/codename1/components/WebBrowserTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@ void testPropertyTypes() {
3636
assertNotNull(types);
3737
assertEquals(browser.getPropertyNames().length, types.length);
3838
}
39+
40+
@FormTest
41+
void testLoadingInnerClass() {
42+
WebBrowser browser = new WebBrowser();
43+
com.codename1.ui.Form f = new com.codename1.ui.Form();
44+
WebBrowser.Loading loading = browser.new Loading(f);
45+
46+
assertTrue(loading.animate());
47+
48+
// Verify install/uninstall
49+
loading.install();
50+
assertTrue(f.getGlassPane() == loading);
51+
52+
loading.unInstall();
53+
assertNull(f.getGlassPane());
54+
}
3955
}

maven/core-unittests/src/test/java/com/codename1/io/WebServiceProxyCallTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,75 @@ public void testWSConnectionArrays() throws Exception {
119119
int[] result = (int[]) conn.returnValue;
120120
Assertions.assertArrayEquals(new int[]{10, 20}, result);
121121
}
122+
123+
@FormTest
124+
public void testWSConnectionMissingTypes() throws Exception {
125+
WebServiceProxyCall.WSDefinition def = new WebServiceProxyCall.WSDefinition();
126+
def.url = "http://example.com/missing";
127+
def.name = "missingMethod";
128+
def.returnType = WebServiceProxyCall.TYPE_LONG; // Just pick one for return
129+
def.arguments = new int[]{
130+
WebServiceProxyCall.TYPE_BYTE,
131+
WebServiceProxyCall.TYPE_CHAR,
132+
WebServiceProxyCall.TYPE_SHORT,
133+
WebServiceProxyCall.TYPE_LONG,
134+
WebServiceProxyCall.TYPE_DOUBLE,
135+
WebServiceProxyCall.TYPE_FLOAT,
136+
WebServiceProxyCall.TYPE_BYTE_OBJECT,
137+
WebServiceProxyCall.TYPE_CHARACTER_OBJECT,
138+
WebServiceProxyCall.TYPE_SHORT_OBJECT,
139+
WebServiceProxyCall.TYPE_LONG_OBJECT,
140+
WebServiceProxyCall.TYPE_DOUBLE_OBJECT,
141+
WebServiceProxyCall.TYPE_FLOAT_OBJECT
142+
};
143+
144+
WebServiceProxyCall.WSConnection conn = new WebServiceProxyCall.WSConnection(def, null,
145+
(byte)1, 'a', (short)2, 3L, 4.0, 5.0f,
146+
(Byte)(byte)6, (Character)'b', (Short)(short)7, 8L, 9.0, 10.0f
147+
);
148+
149+
ByteArrayOutputStream requestBody = new ByteArrayOutputStream();
150+
conn.buildRequestBody(requestBody);
151+
152+
ByteArrayInputStream requestInput = new ByteArrayInputStream(requestBody.toByteArray());
153+
java.io.DataInputStream dis = new java.io.DataInputStream(requestInput);
154+
Assertions.assertEquals("missingMethod", dis.readUTF());
155+
156+
// Byte
157+
Assertions.assertEquals((byte)1, dis.readByte());
158+
// Char
159+
Assertions.assertEquals('a', dis.readChar());
160+
// Short
161+
Assertions.assertEquals((short)2, dis.readShort());
162+
// Long
163+
Assertions.assertEquals(3L, dis.readLong());
164+
// Double
165+
Assertions.assertEquals(4.0, dis.readDouble(), 0.001);
166+
// Float
167+
Assertions.assertEquals(5.0f, dis.readFloat(), 0.001);
168+
169+
// Byte Object
170+
Assertions.assertTrue(dis.readBoolean());
171+
Assertions.assertEquals((byte)6, dis.readByte());
172+
173+
// Character Object
174+
Assertions.assertTrue(dis.readBoolean());
175+
Assertions.assertEquals('b', dis.readChar());
176+
177+
// Short Object
178+
Assertions.assertTrue(dis.readBoolean());
179+
Assertions.assertEquals((short)7, dis.readShort());
180+
181+
// Long Object
182+
Assertions.assertTrue(dis.readBoolean());
183+
Assertions.assertEquals(8L, dis.readLong());
184+
185+
// Double Object
186+
Assertions.assertTrue(dis.readBoolean());
187+
Assertions.assertEquals(9.0, dis.readDouble(), 0.001);
188+
189+
// Float Object
190+
Assertions.assertTrue(dis.readBoolean());
191+
Assertions.assertEquals(10.0f, dis.readFloat(), 0.001);
192+
}
122193
}

maven/core-unittests/src/test/java/com/codename1/properties/SQLMapTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,5 @@ public void testSelectBuilder() throws Exception {
123123
map.selectBuild();
124124
});
125125
}
126+
126127
}

maven/core-unittests/src/test/java/com/codename1/properties/UiBindingTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,42 @@ public void actionPerformed(com.codename1.ui.events.ActionEvent evt) {
4141
// Picker.addActionListener is public.
4242
// We verified bindListener calls addActionListener.
4343
}
44+
45+
public static class BindableObject implements PropertyBusinessObject {
46+
public final Property<String, BindableObject> selection = new Property<>("selection", String.class);
47+
public final PropertyIndex idx = new PropertyIndex(this, "BindableObject", selection);
48+
@Override public PropertyIndex getPropertyIndex() { return idx; }
49+
}
50+
51+
@FormTest
52+
public void testRadioListAdapter() {
53+
BindableObject obj = new BindableObject();
54+
com.codename1.ui.RadioButton r1 = new com.codename1.ui.RadioButton("A");
55+
com.codename1.ui.RadioButton r2 = new com.codename1.ui.RadioButton("B");
56+
com.codename1.ui.ButtonGroup bg = new com.codename1.ui.ButtonGroup();
57+
bg.add(r1);
58+
bg.add(r2);
59+
60+
UiBinding binding = new UiBinding();
61+
binding.setAutoCommit(true);
62+
// bindGroup(prop, values, components)
63+
binding.bindGroup(obj.selection, new String[]{"A", "B"}, r1, r2);
64+
65+
// Test component -> property
66+
r1.setSelected(true);
67+
// Simulate user interaction to trigger listeners
68+
r1.pointerPressed(0, 0);
69+
r1.pointerReleased(0, 0);
70+
Assertions.assertEquals("A", obj.selection.get());
71+
72+
r2.setSelected(true);
73+
r2.pointerPressed(0, 0);
74+
r2.pointerReleased(0, 0);
75+
Assertions.assertEquals("B", obj.selection.get());
76+
77+
// Test property -> component
78+
obj.selection.set("A");
79+
Assertions.assertTrue(r1.isSelected());
80+
Assertions.assertFalse(r2.isSelected());
81+
}
4482
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codename1.push;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import org.junit.jupiter.api.Assertions;
6+
7+
public class PushActionTest extends UITestBase {
8+
9+
@FormTest
10+
public void testPushActionConstructorsAndGetters() {
11+
PushAction action1 = new PushAction("id1", "title1", "icon1");
12+
Assertions.assertEquals("id1", action1.getId());
13+
Assertions.assertEquals("title1", action1.getTitle());
14+
Assertions.assertEquals("icon1", action1.getIcon());
15+
Assertions.assertNull(action1.getTextInputPlaceholder());
16+
Assertions.assertNull(action1.getTextInputButtonText());
17+
18+
PushAction action2 = new PushAction("id2", "title2");
19+
Assertions.assertEquals("id2", action2.getId());
20+
Assertions.assertEquals("title2", action2.getTitle());
21+
Assertions.assertNull(action2.getIcon());
22+
23+
PushAction action3 = new PushAction("title3");
24+
Assertions.assertEquals("title3", action3.getId());
25+
Assertions.assertEquals("title3", action3.getTitle());
26+
27+
PushAction action4 = new PushAction("id4", "title4", "icon4", "placeholder", "buttonText");
28+
Assertions.assertEquals("id4", action4.getId());
29+
Assertions.assertEquals("title4", action4.getTitle());
30+
Assertions.assertEquals("icon4", action4.getIcon());
31+
Assertions.assertEquals("placeholder", action4.getTextInputPlaceholder());
32+
Assertions.assertEquals("buttonText", action4.getTextInputButtonText());
33+
}
34+
}

maven/core-unittests/src/test/java/com/codename1/ui/SideMenuBarTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.codename1.ui.Command;
77
import com.codename1.ui.layouts.BorderLayout;
88
import com.codename1.ui.events.ActionEvent;
9+
import com.codename1.ui.plaf.UIManager;
910
import org.junit.jupiter.api.AfterEach;
1011
import org.junit.jupiter.api.BeforeEach;
1112

@@ -64,6 +65,8 @@ void addingCommandsIncrementsCount() {
6465
}
6566
}
6667

68+
69+
6770
@FormTest
6871
void closeCurrentMenuRunsCallbackWhenNoMenuShowing() {
6972
TestLogger.install();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.codename1.ui.scene;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.Graphics;
6+
import com.codename1.ui.Image;
7+
import org.junit.jupiter.api.Assertions;
8+
9+
public class NodeTest extends UITestBase {
10+
11+
@FormTest
12+
public void testNodeProperties() {
13+
Node node = new Node();
14+
Assertions.assertEquals(1.0, node.scaleX.get(), 0.001);
15+
Assertions.assertEquals(0.0, node.translateX.get(), 0.001);
16+
17+
node.translateX.set(10.0);
18+
Assertions.assertEquals(10.0, node.translateX.get(), 0.001);
19+
20+
node.addTags("tag1", "tag2");
21+
Assertions.assertTrue(node.hasTag("tag1"));
22+
23+
node.removeTags("tag1");
24+
Assertions.assertFalse(node.hasTag("tag1"));
25+
Assertions.assertTrue(node.hasTag("tag2"));
26+
}
27+
28+
@FormTest
29+
public void testRenderableContainerCoverage() throws Exception {
30+
// Access private inner class RenderableContainer to improve coverage as requested
31+
Class<?>[] declaredClasses = Node.class.getDeclaredClasses();
32+
for (Class<?> cls : declaredClasses) {
33+
if (cls.getSimpleName().equals("RenderableContainer")) {
34+
java.lang.reflect.Constructor<?> ctor = cls.getDeclaredConstructor(Node.class);
35+
ctor.setAccessible(true);
36+
Node node = new Node();
37+
Object instance = ctor.newInstance(node); // inner class needs outer instance
38+
39+
// Invoke render method
40+
java.lang.reflect.Method render = cls.getDeclaredMethod("render", Graphics.class);
41+
render.setAccessible(true);
42+
43+
Image img = Image.createImage(100, 100, 0);
44+
render.invoke(instance, img.getGraphics());
45+
46+
// Also verify it is a Container
47+
Assertions.assertTrue(instance instanceof com.codename1.ui.Container);
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)