Skip to content

Commit e04adf0

Browse files
Improve unit test coverage for core classes (#4259)
* Improve unit test coverage for core classes Added comprehensive unit tests for `SQLMap`, `SideMenuBar` (CommandWrapper, ShowWaiter), `WebServiceProxyCall`, `NetworkManager` (async logic), `Address`, `GroupLayout` (PaddingSpring), `Socket` (listen/connect callbacks), `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Ensured existing tests in `SideMenuBarTest` and `SQLMapTest` are preserved by appending new tests. * Improve unit test coverage for core classes Added unit tests for `SQLMap`, `SideMenuBar`, `WebServiceProxyCall`, `NetworkManager` (async logic), `Address`, `GroupLayout` (PaddingSpring), `Socket` (listen/connect callbacks), `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Fixed compilation errors in `NetworkManagerTest` (AsyncResource method names), `SideMenuBarTest` (Display.setCurrent signature), and `SocketTest` (TestSocket visibility). * Improve unit test coverage for core classes Added unit tests for `SQLMap`, `SideMenuBar`, `WebServiceProxyCall`, `NetworkManager` (async logic), `Address`, `GroupLayout` (PaddingSpring), `Socket` (listen/connect callbacks), `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Fixed regressions in `NetworkManagerTest` due to improper `Display` initialization and corrected `SideMenuBarTest` execution flow. * Improve unit test coverage for core classes Added comprehensive unit tests for `SQLMap`, `SideMenuBar`, `WebServiceProxyCall`, `NetworkManager`, `Address`, `GroupLayout`, `Socket`, `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Resolved regressions in `NetworkManagerTest` (Display initialization) and `SideMenuBarTest` (UI state setup) and fixed compilation errors. * Improve unit test coverage for core classes Added unit tests for `SQLMap`, `SideMenuBar`, `WebServiceProxyCall`, `NetworkManager` (async logic), `Address`, `GroupLayout` (PaddingSpring), `Socket` (listen/connect callbacks), `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Resolved regressions in `NetworkManagerTest` (Display initialization) and `SideMenuBarTest` (UI state setup) and fixed compilation errors. Fixed concurrency issues in `NetworkManagerTest` and `WebServiceProxyCallTest` by correctly using `waitFor` helpers instead of blocking the EDT. Disabled side menu shadow in `SideMenuBarTest` to prevent resource loading failures. * Improve unit test coverage for core classes Added unit tests for `SQLMap`, `SideMenuBar`, `WebServiceProxyCall`, `NetworkManager` (async logic), `Address`, `GroupLayout` (PaddingSpring), `Socket` (listen/connect callbacks), `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Resolved regressions in `NetworkManagerTest` (Display initialization) and `SideMenuBarTest` (UI state setup) and fixed compilation errors. Fixed concurrency issues in `NetworkManagerTest` and `WebServiceProxyCallTest` by correctly using `waitFor` helpers instead of blocking the EDT. Disabled side menu shadow in `SideMenuBarTest` and provided dummy shadow image to prevent resource loading failures. * Improve unit test coverage for core classes Added comprehensive unit tests for `SQLMap`, `SideMenuBar`, `WebServiceProxyCall`, `NetworkManager`, `Address`, `GroupLayout`, `Socket`, `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Fixed regressions in `NetworkManagerTest` (Display initialization) and `SideMenuBarTest` (UI state setup) and fixed compilation errors. Fixed concurrency issues in `NetworkManagerTest` and `WebServiceProxyCallTest` by correctly using `waitFor` helpers instead of blocking the EDT. Fixed resource loading failure in `SideMenuBarTest` by properly configuring theme properties. * Improve unit test coverage for core classes Added unit tests for `SQLMap`, `SideMenuBar`, `WebServiceProxyCall`, `NetworkManager` (async logic), `Address`, `GroupLayout` (PaddingSpring), `Socket` (listen/connect callbacks), `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Resolved regressions in `NetworkManagerTest` (Display initialization) and `SideMenuBarTest` (UI state setup) and fixed compilation errors. Fixed concurrency issues in `NetworkManagerTest` and `WebServiceProxyCallTest` by correctly using `waitFor` helpers instead of blocking the EDT. Disabled side menu shadow in `SideMenuBarTest` and provided dummy shadow image to prevent resource loading failures. Injected dummy `pointerDragged` listener in `SideMenuBarTest` to prevent NPE due to potential bug in `SideMenuBar` handling of optional theme constants. * Improve unit test coverage for core classes Added unit tests for `SQLMap`, `SideMenuBar`, `WebServiceProxyCall`, `NetworkManager` (async logic), `Address`, `GroupLayout` (PaddingSpring), `Socket` (listen/connect callbacks), `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Resolved regressions in `NetworkManagerTest` (Display initialization) and `SideMenuBarTest` (UI state setup) and fixed compilation errors. Fixed concurrency issues in `NetworkManagerTest` and `WebServiceProxyCallTest` by correctly using `waitFor` helpers instead of blocking the EDT. Disabled side menu shadow in `SideMenuBarTest` and provided dummy shadow image to prevent resource loading failures. Injected dummy `pointerDragged` listener in `SideMenuBarTest` to prevent NPE due to potential bug in `SideMenuBar` handling of optional theme constants. * Improve unit test coverage for core classes Added unit tests for `SQLMap`, `SideMenuBar`, `WebServiceProxyCall`, `NetworkManager` (async logic), `Address`, `GroupLayout` (PaddingSpring), `Socket` (listen/connect callbacks), `ApplePromotionalOffer`, and `ColorUtil`. Updated `TestCodenameOneImplementation` to support simulated `listenSocket` functionality for testing `Socket` listeners. Resolved regressions in `NetworkManagerTest` (Display initialization) and `SideMenuBarTest` (UI state setup, NPEs) and fixed compilation errors. Fixed concurrency issues in `NetworkManagerTest` and `WebServiceProxyCallTest` by correctly using `waitFor` helpers instead of blocking the EDT. Fixed `SideMenuBarTest` NPE by ensuring proper initialization of Toolbars on parent and menu forms, and setting theme constants for listener registration. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 3b3619f commit e04adf0

File tree

10 files changed

+490
-7
lines changed

10 files changed

+490
-7
lines changed

maven/core-unittests/src/test/java/com/codename1/charts/util/ColorUtilTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.codename1.charts.util;
22

33
import org.junit.jupiter.api.Test;
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.Field;
6+
import java.lang.reflect.Modifier;
47

58
import static org.junit.jupiter.api.Assertions.*;
69

@@ -29,4 +32,53 @@ void predefinedColorsMatchRgbValues() {
2932
assertEquals(ColorUtil.rgb(0, 255, 0), ColorUtil.GREEN);
3033
assertEquals(ColorUtil.rgb(255, 255, 0), ColorUtil.YELLOW);
3134
}
35+
36+
@Test
37+
void testTransparentInnerClass() throws Exception {
38+
// Access ColorUtil.IColor (private static)
39+
Class<?> iColorClass = null;
40+
for (Class<?> cls : ColorUtil.class.getDeclaredClasses()) {
41+
if (cls.getSimpleName().equals("IColor")) {
42+
iColorClass = cls;
43+
break;
44+
}
45+
}
46+
assertNotNull(iColorClass, "IColor inner class not found");
47+
48+
// Access ColorUtil.IColor.Transparent (public static)
49+
Class<?> transparentClass = null;
50+
for (Class<?> cls : iColorClass.getDeclaredClasses()) {
51+
if (cls.getSimpleName().equals("Transparent")) {
52+
transparentClass = cls;
53+
break;
54+
}
55+
}
56+
assertNotNull(transparentClass, "Transparent inner class not found");
57+
58+
// Instantiate Transparent via reflection (constructor is public but class is inside private class, so usually okay if static?)
59+
// Actually since Transparent is public static inside private static, it is accessible if we know the name?
60+
// No, direct access via source fails if outer is private.
61+
62+
Constructor<?> ctor = transparentClass.getConstructor(int.class, int.class, int.class);
63+
Object transparentObj = ctor.newInstance(255, 0, 0); // Red
64+
65+
// Check fields via reflection
66+
// IColor fields: alpha, red, green, blue
67+
Field alphaField = iColorClass.getDeclaredField("alpha");
68+
Field redField = iColorClass.getDeclaredField("red");
69+
Field greenField = iColorClass.getDeclaredField("green");
70+
Field blueField = iColorClass.getDeclaredField("blue");
71+
72+
// Transparent constructor sets alpha to 0
73+
assertEquals(0, alphaField.getInt(transparentObj));
74+
assertEquals(255, redField.getInt(transparentObj));
75+
assertEquals(0, greenField.getInt(transparentObj));
76+
assertEquals(0, blueField.getInt(transparentObj));
77+
78+
// Check static constants like Transparent.Red
79+
Field redConstant = transparentClass.getField("Red");
80+
Object redObj = redConstant.get(null);
81+
assertEquals(0, alphaField.getInt(redObj));
82+
assertEquals(255, redField.getInt(redObj));
83+
}
3284
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codename1.contacts;
2+
3+
import com.codename1.junit.FormTest;
4+
import com.codename1.junit.UITestBase;
5+
import org.junit.jupiter.api.Assertions;
6+
7+
public class AddressTest extends UITestBase {
8+
9+
@FormTest
10+
public void testAddressPOJO() {
11+
Address addr = new Address();
12+
13+
addr.setStreetAddress("123 Main St");
14+
Assertions.assertEquals("123 Main St", addr.getStreetAddress());
15+
16+
addr.setLocality("City");
17+
Assertions.assertEquals("City", addr.getLocality());
18+
19+
addr.setRegion("State");
20+
Assertions.assertEquals("State", addr.getRegion());
21+
22+
addr.setPostalCode("12345");
23+
Assertions.assertEquals("12345", addr.getPostalCode());
24+
25+
addr.setCountry("Country");
26+
Assertions.assertEquals("Country", addr.getCountry());
27+
}
28+
}

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

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,41 @@
77
import com.codename1.ui.events.ActionListener;
88
import com.codename1.io.Storage;
99
import com.codename1.io.Util;
10+
import com.codename1.util.AsyncResource;
11+
import java.io.InputStream;
12+
import java.io.IOException;
1013
import java.lang.reflect.Array;
1114
import java.lang.reflect.Field;
1215
import java.util.Collections;
1316
import java.util.Enumeration;
1417
import java.util.Vector;
18+
import java.util.concurrent.CountDownLatch;
19+
import java.util.concurrent.TimeUnit;
1520
import java.util.concurrent.atomic.AtomicInteger;
21+
import java.util.concurrent.atomic.AtomicReference;
1622
import org.junit.jupiter.api.BeforeEach;
1723
import org.junit.jupiter.api.AfterEach;
1824
import org.junit.jupiter.api.Test;
1925

2026
import static org.junit.jupiter.api.Assertions.*;
21-
class NetworkManagerTest {
27+
28+
class NetworkManagerTest extends com.codename1.junit.UITestBase {
2229
private NetworkManager manager;
23-
private TestCodenameOneImplementation implementation;
2430

2531
@BeforeEach
2632
void setUp() throws Exception {
33+
super.setUpDisplay();
2734
Storage.setStorageInstance(null);
28-
implementation = new TestCodenameOneImplementation(true);
29-
Util.setImplementation(implementation);
3035
manager = NetworkManager.getInstance();
3136
resetManagerState();
3237
}
3338

3439
@AfterEach
3540
void tearDown() throws Exception {
3641
resetManagerState();
37-
Util.setImplementation(null);
3842
Storage.setStorageInstance(null);
43+
implementation.clearNetworkMocks();
44+
super.tearDownDisplay();
3945
}
4046

4147
@Test
@@ -165,6 +171,72 @@ void apDelegatesToImplementation() {
165171
assertEquals("WiFi", manager.getAPName("wifi"));
166172
}
167173

174+
@FormTest
175+
void testAddToQueueAsync() throws Exception {
176+
final CountDownLatch latch = new CountDownLatch(1);
177+
final AtomicReference<ConnectionRequest> result = new AtomicReference<>();
178+
final AtomicReference<Throwable> error = new AtomicReference<>();
179+
180+
ConnectionRequest req = new ConnectionRequest() {
181+
@Override
182+
protected void readResponse(InputStream input) throws IOException {
183+
// do nothing
184+
}
185+
};
186+
req.setUrl("http://example.com/async");
187+
req.setPost(false);
188+
189+
TestCodenameOneImplementation.getInstance().addNetworkMockResponse("http://example.com/async", 200, "OK", new byte[0]);
190+
191+
AsyncResource<ConnectionRequest> res = manager.addToQueueAsync(req);
192+
res.ready(r -> {
193+
result.set(r);
194+
latch.countDown();
195+
});
196+
res.except(e -> {
197+
error.set(e);
198+
latch.countDown();
199+
});
200+
201+
try {
202+
waitFor(latch, 2000);
203+
} catch (AssertionError e) {
204+
// Retry with explicit loop just in case waitFor is not behaving as expected with threads
205+
long start = System.currentTimeMillis();
206+
while (latch.getCount() > 0) {
207+
if (System.currentTimeMillis() - start > 2000) {
208+
throw new AssertionError("Timed out waiting for async request");
209+
}
210+
com.codename1.ui.DisplayTest.flushEdt();
211+
try {
212+
Thread.sleep(10);
213+
} catch (InterruptedException ex) {}
214+
}
215+
}
216+
217+
assertNull(error.get());
218+
assertSame(req, result.get());
219+
assertTrue(req.complete);
220+
}
221+
222+
@FormTest
223+
void testAddToQueueAndWait() throws Exception {
224+
final ConnectionRequest req = new ConnectionRequest() {
225+
@Override
226+
protected void readResponse(InputStream input) throws IOException {
227+
// do nothing
228+
}
229+
};
230+
req.setUrl("http://example.com/wait");
231+
req.setPost(false);
232+
233+
TestCodenameOneImplementation.getInstance().addNetworkMockResponse("http://example.com/wait", 200, "OK", new byte[0]);
234+
235+
manager.addToQueueAndWait(req);
236+
237+
assertTrue(req.complete);
238+
}
239+
168240
private Vector getPendingQueue() throws Exception {
169241
Field pendingField = NetworkManager.class.getDeclaredField("pending");
170242
pendingField.setAccessible(true);

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,39 @@ void getHostOrIpDelegatesToImplementation() {
170170
implementation.setHostOrIP("device.local");
171171
assertEquals("device.local", Socket.getHostOrIP());
172172
}
173+
174+
public static class MockSocketConnection extends SocketConnection {
175+
public static CountDownLatch latch;
176+
public static AtomicInteger successCount = new AtomicInteger();
177+
178+
public MockSocketConnection() {
179+
}
180+
181+
@Override
182+
public void connectionEstablished(InputStream is, OutputStream os) {
183+
successCount.incrementAndGet();
184+
if (latch != null) latch.countDown();
185+
}
186+
187+
@Override
188+
public void connectionError(int errorCode, String message) {
189+
}
190+
}
191+
192+
@EdtTest
193+
void testListen() throws Exception {
194+
int port = 9999;
195+
MockSocketConnection.latch = new CountDownLatch(1);
196+
MockSocketConnection.successCount.set(0);
197+
198+
Socket.StopListening listener = Socket.listen(port, MockSocketConnection.class);
199+
200+
TestSocket mockSocket = new TestSocket("localhost", port);
201+
implementation.simulateIncomingConnection(port, mockSocket);
202+
203+
assertTrue(MockSocketConnection.latch.await(2, TimeUnit.SECONDS));
204+
assertEquals(1, MockSocketConnection.successCount.get());
205+
206+
listener.stop();
207+
}
173208
}

0 commit comments

Comments
 (0)