Skip to content

Commit 824ac42

Browse files
Add unit tests for various core classes to improve coverage (#4250)
Added unit tests for: - com.codename1.io.WebServiceProxyCall$WSConnection - com.codename1.properties.SQLMap - com.codename1.ui.animations.BubbleTransition (disabled due to headless env issues) - com.codename1.ui.animations.AnimationObject - com.codename1.ui.CustomFont - com.codename1.ui.animations.Timeline - com.codename1.ui.SideMenuBar$MenuTransition (disabled due to missing resources) - com.codename1.properties.InstantUI - com.codename1.ui.BrowserComponent$JSProxy - com.codename1.charts.views.DialChart Refactored to avoid reflection and respect constraints. Tests for BubbleTransition and SideMenuBar are disabled but present for future enablement. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 256e155 commit 824ac42

File tree

10 files changed

+561
-0
lines changed

10 files changed

+561
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codename1.charts.views;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.charts.models.CategorySeries;
6+
import com.codename1.charts.renderers.DialRenderer;
7+
import com.codename1.charts.compat.Canvas;
8+
import com.codename1.charts.compat.Paint;
9+
import com.codename1.ui.Image;
10+
import com.codename1.ui.Graphics;
11+
import org.junit.jupiter.api.Assertions;
12+
13+
public class DialChartTest extends UITestBase {
14+
15+
@FormTest
16+
public void testDialChart() {
17+
CategorySeries dataset = new CategorySeries("Dial");
18+
dataset.add("Speed", 80);
19+
20+
DialRenderer renderer = new DialRenderer();
21+
DialChart chart = new DialChart(dataset, renderer);
22+
23+
Image img = Image.createImage(200, 200, 0xFFFFFFFF);
24+
Graphics g = img.getGraphics();
25+
Canvas canvas = new Canvas();
26+
canvas.g = g;
27+
Paint paint = new Paint();
28+
29+
chart.draw(canvas, 0, 0, 200, 200, paint);
30+
}
31+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.codename1.io;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.util.Callback;
6+
import org.junit.jupiter.api.Assertions;
7+
import java.io.ByteArrayInputStream;
8+
import java.io.ByteArrayOutputStream;
9+
import java.io.DataOutputStream;
10+
import java.io.IOException;
11+
12+
public class WebServiceProxyCallTest extends UITestBase {
13+
14+
@FormTest
15+
public void testWSConnectionVoid() throws Exception {
16+
WebServiceProxyCall.WSDefinition def = new WebServiceProxyCall.WSDefinition();
17+
def.url = "http://example.com/void";
18+
def.name = "voidMethod";
19+
def.returnType = WebServiceProxyCall.TYPE_VOID;
20+
def.arguments = new int[]{};
21+
22+
WebServiceProxyCall.WSConnection conn = new WebServiceProxyCall.WSConnection(def, null);
23+
24+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
25+
// Void returns nothing
26+
27+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
28+
conn.readResponse(bais);
29+
}
30+
31+
@FormTest
32+
public void testWSConnectionInt() throws Exception {
33+
WebServiceProxyCall.WSDefinition def = new WebServiceProxyCall.WSDefinition();
34+
def.url = "http://example.com/int";
35+
def.name = "intMethod";
36+
def.returnType = WebServiceProxyCall.TYPE_INT;
37+
def.arguments = new int[]{WebServiceProxyCall.TYPE_INT};
38+
39+
WebServiceProxyCall.WSConnection conn = new WebServiceProxyCall.WSConnection(def, null, 123);
40+
41+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
42+
DataOutputStream dos = new DataOutputStream(baos);
43+
dos.writeInt(456);
44+
dos.flush();
45+
46+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
47+
conn.readResponse(bais);
48+
49+
Assertions.assertEquals(456, conn.returnValue);
50+
51+
ByteArrayOutputStream requestBody = new ByteArrayOutputStream();
52+
conn.buildRequestBody(requestBody);
53+
54+
ByteArrayInputStream requestInput = new ByteArrayInputStream(requestBody.toByteArray());
55+
java.io.DataInputStream dis = new java.io.DataInputStream(requestInput);
56+
Assertions.assertEquals("intMethod", dis.readUTF());
57+
Assertions.assertEquals(123, dis.readInt());
58+
}
59+
60+
@FormTest
61+
public void testWSConnectionVariousTypes() throws Exception {
62+
WebServiceProxyCall.WSDefinition def = new WebServiceProxyCall.WSDefinition();
63+
def.url = "http://example.com/all";
64+
def.name = "allMethod";
65+
def.returnType = WebServiceProxyCall.TYPE_STRING;
66+
def.arguments = new int[]{
67+
WebServiceProxyCall.TYPE_BOOLEAN,
68+
WebServiceProxyCall.TYPE_STRING,
69+
WebServiceProxyCall.TYPE_STRING_ARRAY
70+
};
71+
72+
WebServiceProxyCall.WSConnection conn = new WebServiceProxyCall.WSConnection(def, null, true, "hello", new String[]{"a", "b"});
73+
74+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
75+
DataOutputStream dos = new DataOutputStream(baos);
76+
dos.writeBoolean(true); // String presence
77+
dos.writeUTF("result");
78+
dos.flush();
79+
80+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
81+
conn.readResponse(bais);
82+
83+
Assertions.assertEquals("result", conn.returnValue);
84+
85+
ByteArrayOutputStream requestBody = new ByteArrayOutputStream();
86+
conn.buildRequestBody(requestBody);
87+
88+
ByteArrayInputStream requestInput = new ByteArrayInputStream(requestBody.toByteArray());
89+
java.io.DataInputStream dis = new java.io.DataInputStream(requestInput);
90+
Assertions.assertEquals("allMethod", dis.readUTF());
91+
Assertions.assertTrue(dis.readBoolean()); // boolean arg
92+
Assertions.assertTrue(dis.readBoolean()); // string arg present
93+
Assertions.assertEquals("hello", dis.readUTF());
94+
Assertions.assertEquals(2, dis.readInt()); // string array length
95+
Assertions.assertEquals("a", dis.readUTF());
96+
Assertions.assertEquals("b", dis.readUTF());
97+
}
98+
99+
@FormTest
100+
public void testWSConnectionArrays() throws Exception {
101+
WebServiceProxyCall.WSDefinition def = new WebServiceProxyCall.WSDefinition();
102+
def.url = "http://example.com/arrays";
103+
def.name = "arraysMethod";
104+
def.returnType = WebServiceProxyCall.TYPE_INT_ARRAY;
105+
def.arguments = new int[]{};
106+
107+
WebServiceProxyCall.WSConnection conn = new WebServiceProxyCall.WSConnection(def, null);
108+
109+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
110+
DataOutputStream dos = new DataOutputStream(baos);
111+
dos.writeInt(2); // Array length
112+
dos.writeInt(10);
113+
dos.writeInt(20);
114+
dos.flush();
115+
116+
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
117+
conn.readResponse(bais);
118+
119+
int[] result = (int[]) conn.returnValue;
120+
Assertions.assertArrayEquals(new int[]{10, 20}, result);
121+
}
122+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.codename1.properties;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.ui.Container;
6+
import org.junit.jupiter.api.Assertions;
7+
8+
public class InstantUITest extends UITestBase {
9+
10+
public static class MyData implements PropertyBusinessObject {
11+
public final Property<String, MyData> name = new Property<>("name", String.class);
12+
public final Property<Boolean, MyData> active = new Property<>("active", Boolean.class);
13+
public final PropertyIndex idx = new PropertyIndex(this, "MyData", name, active);
14+
15+
@Override
16+
public PropertyIndex getPropertyIndex() {
17+
return idx;
18+
}
19+
}
20+
21+
@FormTest
22+
public void testCreateEditUI() {
23+
MyData data = new MyData();
24+
data.name.set("Test");
25+
data.active.set(true);
26+
27+
InstantUI iui = new InstantUI();
28+
Container ui = iui.createEditUI(data, true);
29+
30+
Assertions.assertNotNull(ui);
31+
Assertions.assertTrue(ui.getComponentCount() > 0);
32+
33+
UiBinding.Binding binding = iui.getBindings(ui);
34+
Assertions.assertNotNull(binding);
35+
}
36+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.codename1.properties;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.testing.TestCodenameOneImplementation;
6+
import com.codename1.db.Database;
7+
import org.junit.jupiter.api.Assertions;
8+
import java.util.List;
9+
10+
public class SQLMapTest extends UITestBase {
11+
12+
public static class MyData implements PropertyBusinessObject {
13+
public final Property<String, MyData> name = new Property<>("name", String.class);
14+
public final Property<Integer, MyData> age = new Property<>("age", Integer.class);
15+
public final PropertyIndex idx = new PropertyIndex(this, "MyData", name, age);
16+
17+
@Override
18+
public PropertyIndex getPropertyIndex() {
19+
return idx;
20+
}
21+
}
22+
23+
@FormTest
24+
public void testCreateTable() throws Exception {
25+
Database db = TestCodenameOneImplementation.getInstance().openOrCreateDB("test.db");
26+
SQLMap map = SQLMap.create(db);
27+
MyData data = new MyData();
28+
29+
map.setPrimaryKey(data, data.name);
30+
31+
// Mock query result for table existence check (returns false initially)
32+
((TestCodenameOneImplementation.TestDatabase)db).setQueryResult(new String[]{"type", "name"}, new Object[][]{});
33+
34+
boolean created = map.createTable(data);
35+
Assertions.assertTrue(created);
36+
37+
List<String> statements = ((TestCodenameOneImplementation.TestDatabase)db).getExecutedStatements();
38+
boolean foundCreate = false;
39+
for(String s : statements) {
40+
if (s.contains("CREATE TABLE")) {
41+
foundCreate = true;
42+
break;
43+
}
44+
}
45+
Assertions.assertTrue(foundCreate);
46+
}
47+
48+
@FormTest
49+
public void testInsert() throws Exception {
50+
Database db = TestCodenameOneImplementation.getInstance().openOrCreateDB("test.db");
51+
SQLMap map = SQLMap.create(db);
52+
MyData data = new MyData();
53+
data.name.set("John");
54+
data.age.set(30);
55+
56+
map.insert(data);
57+
58+
List<String> statements = ((TestCodenameOneImplementation.TestDatabase)db).getExecutedStatements();
59+
boolean foundInsert = false;
60+
for(String s : statements) {
61+
if (s.contains("INSERT INTO")) {
62+
foundInsert = true;
63+
break;
64+
}
65+
}
66+
Assertions.assertTrue(foundInsert);
67+
68+
List<String[]> params = ((TestCodenameOneImplementation.TestDatabase)db).getExecutedParameters();
69+
}
70+
71+
@FormTest
72+
public void testSelect() throws Exception {
73+
Database db = TestCodenameOneImplementation.getInstance().openOrCreateDB("test.db");
74+
SQLMap map = SQLMap.create(db);
75+
MyData data = new MyData();
76+
77+
// Mock result
78+
((TestCodenameOneImplementation.TestDatabase)db).setQueryResult(
79+
new String[]{"name", "age"},
80+
new Object[][]{
81+
{"John", 30},
82+
{"Jane", 25}
83+
}
84+
);
85+
86+
List<PropertyBusinessObject> results = map.select(data, null, true, 0, 0);
87+
Assertions.assertEquals(2, results.size());
88+
MyData res1 = (MyData) results.get(0);
89+
Assertions.assertEquals("John", res1.name.get());
90+
Assertions.assertEquals(30, res1.age.get().intValue());
91+
}
92+
93+
@FormTest
94+
public void testDelete() throws Exception {
95+
Database db = TestCodenameOneImplementation.getInstance().openOrCreateDB("test.db");
96+
SQLMap map = SQLMap.create(db);
97+
MyData data = new MyData();
98+
map.setPrimaryKey(data, data.name);
99+
data.name.set("John");
100+
101+
map.delete(data);
102+
103+
List<String> statements = ((TestCodenameOneImplementation.TestDatabase)db).getExecutedStatements();
104+
boolean foundDelete = false;
105+
for(String s : statements) {
106+
if (s.contains("DELETE FROM")) {
107+
foundDelete = true;
108+
break;
109+
}
110+
}
111+
Assertions.assertTrue(foundDelete);
112+
}
113+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.codename1.ui;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import com.codename1.testing.TestCodenameOneImplementation;
6+
import org.junit.jupiter.api.Assertions;
7+
8+
public class BrowserComponentJSProxyTest extends UITestBase {
9+
10+
public static class TestPeer extends PeerComponent {
11+
public TestPeer() {
12+
super(new Object());
13+
}
14+
}
15+
16+
@FormTest
17+
public void testJSProxy() {
18+
implementation.setBrowserComponent(new TestPeer());
19+
BrowserComponent bc = new BrowserComponent();
20+
BrowserComponent.JSProxy proxy = bc.createJSProxy("window");
21+
22+
proxy.call("alert", new Object[]{"Hello"}, null);
23+
24+
flushSerialCalls();
25+
26+
java.util.List<String> executed = implementation.getBrowserExecuted();
27+
Assertions.assertFalse(executed.isEmpty());
28+
String lastScript = executed.get(executed.size() - 1);
29+
Assertions.assertTrue(lastScript.contains("window.alert"));
30+
Assertions.assertTrue(lastScript.contains("Hello"));
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.codename1.ui;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import org.junit.jupiter.api.Assertions;
6+
7+
public class CustomFontTest extends UITestBase {
8+
9+
@FormTest
10+
public void testCustomFont() {
11+
// Create a dummy bitmap for the font
12+
// Assume 2 chars 'A' and 'B', each 10x10 pixels
13+
Image bitmap = Image.createImage(20, 10, 0xFF000000);
14+
int[] cutOffsets = new int[]{0, 10};
15+
int[] charWidth = new int[]{10, 10};
16+
String charsets = "AB";
17+
18+
CustomFont font = new CustomFont(bitmap, cutOffsets, charWidth, charsets);
19+
20+
Assertions.assertEquals(10, font.charWidth('A'));
21+
Assertions.assertEquals(10, font.charWidth('B'));
22+
Assertions.assertEquals(0, font.charWidth('C')); // Not in charset
23+
24+
Assertions.assertEquals(10, font.getHeight());
25+
Assertions.assertEquals("AB", font.getCharset());
26+
27+
Assertions.assertEquals(20, font.stringWidth("AB"));
28+
Assertions.assertEquals(10, font.substringWidth("ABC", 0, 1));
29+
}
30+
}

0 commit comments

Comments
 (0)