Skip to content

Commit 2bcff9d

Browse files
authored
Add geometry and graphics unit tests (#4078)
* Add geometry and graphics unit tests * Fix geometry unit tests and chart conversions * Restore SignatureButtonBox theme property * Use test implementation helpers in unit tests * Improve test infrastructure for transforms * Adjust demos workflow build command * Revert "Adjust demos workflow build command" This reverts commit adebb76.
1 parent 01ad037 commit 2bcff9d

File tree

7 files changed

+1047
-45
lines changed

7 files changed

+1047
-45
lines changed

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import com.codename1.charts.views.AbstractChart;
1212
import com.codename1.charts.views.ClickableArea;
1313
import com.codename1.charts.views.XYChart;
14+
import com.codename1.impl.CodenameOneImplementation;
1415
import com.codename1.test.UITestBase;
1516
import com.codename1.ui.Transform;
1617
import com.codename1.ui.geom.Rectangle;
1718
import com.codename1.ui.geom.Shape;
19+
import com.codename1.testing.TestCodenameOneImplementation;
1820
import org.junit.jupiter.api.Test;
1921

2022
import java.lang.reflect.Field;
@@ -23,6 +25,13 @@
2325
import static org.junit.jupiter.api.Assertions.*;
2426

2527
class ChartComponentTest extends UITestBase {
28+
private TestCodenameOneImplementation testImplementation;
29+
30+
@Override
31+
protected CodenameOneImplementation createImplementation() {
32+
testImplementation = new TestCodenameOneImplementation();
33+
return testImplementation;
34+
}
2635
@Test
2736
void constructorCopiesPanAndZoomSettingsFromXYChart() {
2837
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
@@ -59,18 +68,30 @@ void shapeConversionsTranslateBetweenSpaces() throws Exception {
5968
ChartComponent component = new PositionedChartComponent(chart, 10, 15);
6069

6170
Rectangle screenRect = new Rectangle(component.getAbsoluteX(), component.getAbsoluteY(), 40, 50);
62-
Rectangle chartBounds = component.screenToChartShape(screenRect).getBounds();
63-
assertEquals(0, chartBounds.getX());
64-
assertEquals(0, chartBounds.getY());
65-
assertEquals(0, chartBounds.getSize().getWidth());
66-
assertEquals(0, chartBounds.getSize().getHeight());
71+
Shape chartShape = component.screenToChartShape(screenRect);
72+
Rectangle chartBounds = chartShape.getBounds();
73+
Shape roundTripScreenShape = component.chartToScreenShape(chartShape);
74+
Rectangle roundTripScreenBounds = roundTripScreenShape.getBounds();
75+
assertEquals(screenRect.getX(), roundTripScreenBounds.getX());
76+
assertEquals(screenRect.getY(), roundTripScreenBounds.getY());
77+
assertEquals(screenRect.getWidth(), chartBounds.getWidth());
78+
assertEquals(screenRect.getHeight(), chartBounds.getHeight());
79+
assertEquals(screenRect.getWidth(), roundTripScreenBounds.getWidth());
80+
assertEquals(screenRect.getHeight(), roundTripScreenBounds.getHeight());
6781

6882
Rectangle chartRect = new Rectangle(0, 0, 40, 50);
6983
Rectangle screenBounds = component.chartToScreenShape(chartRect).getBounds();
70-
assertEquals(0, screenBounds.getX());
71-
assertEquals(0, screenBounds.getY());
72-
assertEquals(0, screenBounds.getSize().getWidth());
73-
assertEquals(0, screenBounds.getSize().getHeight());
84+
assertEquals(component.getAbsoluteX(), screenBounds.getX());
85+
assertEquals(component.getAbsoluteY(), screenBounds.getY());
86+
assertEquals(40, screenBounds.getWidth());
87+
assertEquals(50, screenBounds.getHeight());
88+
89+
Shape roundTripChartShape = component.screenToChartShape(component.chartToScreenShape(chartRect));
90+
Rectangle roundTripChartBounds = roundTripChartShape.getBounds();
91+
assertEquals(chartRect.getX(), roundTripChartBounds.getX());
92+
assertEquals(chartRect.getY(), roundTripChartBounds.getY());
93+
assertEquals(chartRect.getWidth(), roundTripChartBounds.getWidth());
94+
assertEquals(chartRect.getHeight(), roundTripChartBounds.getHeight());
7495
}
7596

7697
@Test

maven/core-unittests/src/test/java/com/codename1/test/UITestBase.java

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.codename1.impl.CodenameOneImplementation;
44
import com.codename1.io.Util;
55
import com.codename1.plugin.PluginSupport;
6+
import com.codename1.testing.TestCodenameOneImplementation;
67
import com.codename1.ui.Display;
78
import com.codename1.ui.Graphics;
89
import com.codename1.ui.plaf.UIManager;
@@ -38,41 +39,8 @@ protected void setUpDisplay() throws Exception {
3839
display = Display.getInstance();
3940
resetUIManager();
4041

41-
implementation = mock(CodenameOneImplementation.class);
42-
final Object defaultFont = new Object();
43-
when(implementation.getDisplayWidth()).thenReturn(1080);
44-
when(implementation.getDisplayHeight()).thenReturn(1920);
45-
when(implementation.getActualDisplayHeight()).thenReturn(1920);
46-
when(implementation.getDeviceDensity()).thenReturn(Display.DENSITY_MEDIUM);
47-
when(implementation.convertToPixels(anyInt(), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
48-
when(implementation.createFont(anyInt(), anyInt(), anyInt())).thenReturn(defaultFont);
49-
when(implementation.getDefaultFont()).thenReturn(defaultFont);
50-
when(implementation.isTrueTypeSupported()).thenReturn(true);
51-
when(implementation.isLookupFontSupported()).thenReturn(true);
52-
when(implementation.isInitialized()).thenReturn(true);
53-
when(implementation.getCommandBehavior()).thenReturn(Display.COMMAND_BEHAVIOR_DEFAULT);
54-
when(implementation.isNativeFontSchemeSupported()).thenReturn(true);
55-
when(implementation.loadTrueTypeFont(anyString(), anyString())).thenReturn(defaultFont);
56-
when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenReturn(defaultFont);
57-
when(implementation.loadNativeFont(anyString())).thenReturn(defaultFont);
58-
when(implementation.getNativeGraphics()).thenReturn(new Object());
59-
when(implementation.paintNativePeersBehind()).thenReturn(false);
60-
when(implementation.handleEDTException(any(Throwable.class))).thenReturn(false);
61-
when(implementation.charWidth(any(), anyChar())).thenReturn(8);
62-
when(implementation.stringWidth(any(), anyString())).thenAnswer(invocation -> {
63-
String text = (String) invocation.getArgument(1);
64-
return text == null ? 0 : text.length() * 8;
65-
});
66-
when(implementation.charsWidth(any(), any(char[].class), anyInt(), anyInt())).thenAnswer(invocation -> {
67-
Integer length = (Integer) invocation.getArgument(3);
68-
return length == null ? 0 : Math.max(0, length) * 8;
69-
});
70-
when(implementation.getHeight(any())).thenReturn(16);
71-
when(implementation.getPlatformName()).thenReturn("and");
72-
when(implementation.getProperty(anyString(), anyString())).thenAnswer(invocation -> (String) invocation.getArgument(1));
73-
when(implementation.loadTrueTypeFont(anyString(), anyString())).thenAnswer(invocation -> new Object());
74-
when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenAnswer(invocation -> new Object());
75-
when(implementation.loadNativeFont(anyString())).thenAnswer(invocation -> new Object());
42+
implementation = createImplementation();
43+
configureImplementation(implementation);
7644

7745
pluginSupport = new PluginSupport();
7846

@@ -113,15 +81,75 @@ private void resetUIManager() throws Exception {
11381
}
11482

11583
private Graphics createGraphics() throws Exception {
84+
Object nativeGraphics = implementation != null ? implementation.getNativeGraphics() : null;
85+
if (nativeGraphics == null) {
86+
nativeGraphics = new Object();
87+
}
11688
java.lang.reflect.Constructor<Graphics> constructor = Graphics.class.getDeclaredConstructor(Object.class);
11789
constructor.setAccessible(true);
118-
Graphics graphics = constructor.newInstance(new Object());
90+
Graphics graphics = constructor.newInstance(nativeGraphics);
11991
Field paintPeersField = Graphics.class.getDeclaredField("paintPeersBehind");
12092
paintPeersField.setAccessible(true);
12193
paintPeersField.setBoolean(graphics, false);
12294
return graphics;
12395
}
12496

97+
protected CodenameOneImplementation createImplementation() {
98+
return mock(CodenameOneImplementation.class);
99+
}
100+
101+
protected void configureImplementation(CodenameOneImplementation implementation) {
102+
if (implementation instanceof TestCodenameOneImplementation) {
103+
configureTestImplementation((TestCodenameOneImplementation) implementation);
104+
} else {
105+
configureMockImplementation(implementation);
106+
}
107+
}
108+
109+
private void configureMockImplementation(CodenameOneImplementation implementation) {
110+
final Object defaultFont = new Object();
111+
when(implementation.getDisplayWidth()).thenReturn(1080);
112+
when(implementation.getDisplayHeight()).thenReturn(1920);
113+
when(implementation.getActualDisplayHeight()).thenReturn(1920);
114+
when(implementation.getDeviceDensity()).thenReturn(Display.DENSITY_MEDIUM);
115+
when(implementation.convertToPixels(anyInt(), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
116+
when(implementation.createFont(anyInt(), anyInt(), anyInt())).thenReturn(defaultFont);
117+
when(implementation.getDefaultFont()).thenReturn(defaultFont);
118+
when(implementation.isTrueTypeSupported()).thenReturn(true);
119+
when(implementation.isLookupFontSupported()).thenReturn(true);
120+
when(implementation.isInitialized()).thenReturn(true);
121+
when(implementation.getCommandBehavior()).thenReturn(Display.COMMAND_BEHAVIOR_DEFAULT);
122+
when(implementation.isNativeFontSchemeSupported()).thenReturn(true);
123+
when(implementation.loadTrueTypeFont(anyString(), anyString())).thenReturn(defaultFont);
124+
when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenReturn(defaultFont);
125+
when(implementation.loadNativeFont(anyString())).thenReturn(defaultFont);
126+
when(implementation.getNativeGraphics()).thenReturn(new Object());
127+
when(implementation.paintNativePeersBehind()).thenReturn(false);
128+
when(implementation.handleEDTException(any(Throwable.class))).thenReturn(false);
129+
when(implementation.charWidth(any(), anyChar())).thenReturn(8);
130+
when(implementation.stringWidth(any(), anyString())).thenAnswer(invocation -> {
131+
String text = (String) invocation.getArgument(1);
132+
return text == null ? 0 : text.length() * 8;
133+
});
134+
when(implementation.charsWidth(any(), any(char[].class), anyInt(), anyInt())).thenAnswer(invocation -> {
135+
Integer length = (Integer) invocation.getArgument(3);
136+
return length == null ? 0 : Math.max(0, length) * 8;
137+
});
138+
when(implementation.getHeight(any())).thenReturn(16);
139+
when(implementation.getPlatformName()).thenReturn("and");
140+
when(implementation.getProperty(anyString(), anyString())).thenAnswer(invocation -> (String) invocation.getArgument(1));
141+
when(implementation.loadTrueTypeFont(anyString(), anyString())).thenAnswer(invocation -> new Object());
142+
when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenAnswer(invocation -> new Object());
143+
when(implementation.loadNativeFont(anyString())).thenAnswer(invocation -> new Object());
144+
}
145+
146+
protected void configureTestImplementation(TestCodenameOneImplementation implementation) {
147+
implementation.setDisplaySize(1080, 1920);
148+
implementation.setDeviceDensity(Display.DENSITY_MEDIUM);
149+
implementation.setTouchDevice(true);
150+
implementation.setTimeoutSupported(true);
151+
}
152+
125153
/**
126154
* Processes any pending serial calls that were queued via {@link Display#callSerially(Runnable)}.
127155
*/

0 commit comments

Comments
 (0)