Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import com.codename1.charts.views.AbstractChart;
import com.codename1.charts.views.ClickableArea;
import com.codename1.charts.views.XYChart;
import com.codename1.impl.CodenameOneImplementation;
import com.codename1.test.UITestBase;
import com.codename1.ui.Transform;
import com.codename1.ui.geom.Rectangle;
import com.codename1.ui.geom.Shape;
import com.codename1.testing.TestCodenameOneImplementation;
import org.junit.jupiter.api.Test;

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

class ChartComponentTest extends UITestBase {
private TestCodenameOneImplementation testImplementation;

@Override
protected CodenameOneImplementation createImplementation() {
testImplementation = new TestCodenameOneImplementation();
return testImplementation;
}
@Test
void constructorCopiesPanAndZoomSettingsFromXYChart() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
Expand Down Expand Up @@ -59,18 +68,30 @@ void shapeConversionsTranslateBetweenSpaces() throws Exception {
ChartComponent component = new PositionedChartComponent(chart, 10, 15);

Rectangle screenRect = new Rectangle(component.getAbsoluteX(), component.getAbsoluteY(), 40, 50);
Rectangle chartBounds = component.screenToChartShape(screenRect).getBounds();
assertEquals(0, chartBounds.getX());
assertEquals(0, chartBounds.getY());
assertEquals(0, chartBounds.getSize().getWidth());
assertEquals(0, chartBounds.getSize().getHeight());
Shape chartShape = component.screenToChartShape(screenRect);
Rectangle chartBounds = chartShape.getBounds();
Shape roundTripScreenShape = component.chartToScreenShape(chartShape);
Rectangle roundTripScreenBounds = roundTripScreenShape.getBounds();
assertEquals(screenRect.getX(), roundTripScreenBounds.getX());
assertEquals(screenRect.getY(), roundTripScreenBounds.getY());
assertEquals(screenRect.getWidth(), chartBounds.getWidth());
assertEquals(screenRect.getHeight(), chartBounds.getHeight());
assertEquals(screenRect.getWidth(), roundTripScreenBounds.getWidth());
assertEquals(screenRect.getHeight(), roundTripScreenBounds.getHeight());

Rectangle chartRect = new Rectangle(0, 0, 40, 50);
Rectangle screenBounds = component.chartToScreenShape(chartRect).getBounds();
assertEquals(0, screenBounds.getX());
assertEquals(0, screenBounds.getY());
assertEquals(0, screenBounds.getSize().getWidth());
assertEquals(0, screenBounds.getSize().getHeight());
assertEquals(component.getAbsoluteX(), screenBounds.getX());
assertEquals(component.getAbsoluteY(), screenBounds.getY());
assertEquals(40, screenBounds.getWidth());
assertEquals(50, screenBounds.getHeight());

Shape roundTripChartShape = component.screenToChartShape(component.chartToScreenShape(chartRect));
Rectangle roundTripChartBounds = roundTripChartShape.getBounds();
assertEquals(chartRect.getX(), roundTripChartBounds.getX());
assertEquals(chartRect.getY(), roundTripChartBounds.getY());
assertEquals(chartRect.getWidth(), roundTripChartBounds.getWidth());
assertEquals(chartRect.getHeight(), roundTripChartBounds.getHeight());
}

@Test
Expand Down
100 changes: 64 additions & 36 deletions maven/core-unittests/src/test/java/com/codename1/test/UITestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.codename1.impl.CodenameOneImplementation;
import com.codename1.io.Util;
import com.codename1.plugin.PluginSupport;
import com.codename1.testing.TestCodenameOneImplementation;
import com.codename1.ui.Display;
import com.codename1.ui.Graphics;
import com.codename1.ui.plaf.UIManager;
Expand Down Expand Up @@ -38,41 +39,8 @@ protected void setUpDisplay() throws Exception {
display = Display.getInstance();
resetUIManager();

implementation = mock(CodenameOneImplementation.class);
final Object defaultFont = new Object();
when(implementation.getDisplayWidth()).thenReturn(1080);
when(implementation.getDisplayHeight()).thenReturn(1920);
when(implementation.getActualDisplayHeight()).thenReturn(1920);
when(implementation.getDeviceDensity()).thenReturn(Display.DENSITY_MEDIUM);
when(implementation.convertToPixels(anyInt(), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(implementation.createFont(anyInt(), anyInt(), anyInt())).thenReturn(defaultFont);
when(implementation.getDefaultFont()).thenReturn(defaultFont);
when(implementation.isTrueTypeSupported()).thenReturn(true);
when(implementation.isLookupFontSupported()).thenReturn(true);
when(implementation.isInitialized()).thenReturn(true);
when(implementation.getCommandBehavior()).thenReturn(Display.COMMAND_BEHAVIOR_DEFAULT);
when(implementation.isNativeFontSchemeSupported()).thenReturn(true);
when(implementation.loadTrueTypeFont(anyString(), anyString())).thenReturn(defaultFont);
when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenReturn(defaultFont);
when(implementation.loadNativeFont(anyString())).thenReturn(defaultFont);
when(implementation.getNativeGraphics()).thenReturn(new Object());
when(implementation.paintNativePeersBehind()).thenReturn(false);
when(implementation.handleEDTException(any(Throwable.class))).thenReturn(false);
when(implementation.charWidth(any(), anyChar())).thenReturn(8);
when(implementation.stringWidth(any(), anyString())).thenAnswer(invocation -> {
String text = (String) invocation.getArgument(1);
return text == null ? 0 : text.length() * 8;
});
when(implementation.charsWidth(any(), any(char[].class), anyInt(), anyInt())).thenAnswer(invocation -> {
Integer length = (Integer) invocation.getArgument(3);
return length == null ? 0 : Math.max(0, length) * 8;
});
when(implementation.getHeight(any())).thenReturn(16);
when(implementation.getPlatformName()).thenReturn("and");
when(implementation.getProperty(anyString(), anyString())).thenAnswer(invocation -> (String) invocation.getArgument(1));
when(implementation.loadTrueTypeFont(anyString(), anyString())).thenAnswer(invocation -> new Object());
when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenAnswer(invocation -> new Object());
when(implementation.loadNativeFont(anyString())).thenAnswer(invocation -> new Object());
implementation = createImplementation();
configureImplementation(implementation);

pluginSupport = new PluginSupport();

Expand Down Expand Up @@ -113,15 +81,75 @@ private void resetUIManager() throws Exception {
}

private Graphics createGraphics() throws Exception {
Object nativeGraphics = implementation != null ? implementation.getNativeGraphics() : null;
if (nativeGraphics == null) {
nativeGraphics = new Object();
}
java.lang.reflect.Constructor<Graphics> constructor = Graphics.class.getDeclaredConstructor(Object.class);
constructor.setAccessible(true);
Graphics graphics = constructor.newInstance(new Object());
Graphics graphics = constructor.newInstance(nativeGraphics);
Field paintPeersField = Graphics.class.getDeclaredField("paintPeersBehind");
paintPeersField.setAccessible(true);
paintPeersField.setBoolean(graphics, false);
return graphics;
}

protected CodenameOneImplementation createImplementation() {
return mock(CodenameOneImplementation.class);
}

protected void configureImplementation(CodenameOneImplementation implementation) {
if (implementation instanceof TestCodenameOneImplementation) {
configureTestImplementation((TestCodenameOneImplementation) implementation);
} else {
configureMockImplementation(implementation);
}
}

private void configureMockImplementation(CodenameOneImplementation implementation) {
final Object defaultFont = new Object();
when(implementation.getDisplayWidth()).thenReturn(1080);
when(implementation.getDisplayHeight()).thenReturn(1920);
when(implementation.getActualDisplayHeight()).thenReturn(1920);
when(implementation.getDeviceDensity()).thenReturn(Display.DENSITY_MEDIUM);
when(implementation.convertToPixels(anyInt(), anyBoolean())).thenAnswer(invocation -> invocation.getArgument(0));
when(implementation.createFont(anyInt(), anyInt(), anyInt())).thenReturn(defaultFont);
when(implementation.getDefaultFont()).thenReturn(defaultFont);
when(implementation.isTrueTypeSupported()).thenReturn(true);
when(implementation.isLookupFontSupported()).thenReturn(true);
when(implementation.isInitialized()).thenReturn(true);
when(implementation.getCommandBehavior()).thenReturn(Display.COMMAND_BEHAVIOR_DEFAULT);
when(implementation.isNativeFontSchemeSupported()).thenReturn(true);
when(implementation.loadTrueTypeFont(anyString(), anyString())).thenReturn(defaultFont);
when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenReturn(defaultFont);
when(implementation.loadNativeFont(anyString())).thenReturn(defaultFont);
when(implementation.getNativeGraphics()).thenReturn(new Object());
when(implementation.paintNativePeersBehind()).thenReturn(false);
when(implementation.handleEDTException(any(Throwable.class))).thenReturn(false);
when(implementation.charWidth(any(), anyChar())).thenReturn(8);
when(implementation.stringWidth(any(), anyString())).thenAnswer(invocation -> {
String text = (String) invocation.getArgument(1);
return text == null ? 0 : text.length() * 8;
});
when(implementation.charsWidth(any(), any(char[].class), anyInt(), anyInt())).thenAnswer(invocation -> {
Integer length = (Integer) invocation.getArgument(3);
return length == null ? 0 : Math.max(0, length) * 8;
});
when(implementation.getHeight(any())).thenReturn(16);
when(implementation.getPlatformName()).thenReturn("and");
when(implementation.getProperty(anyString(), anyString())).thenAnswer(invocation -> (String) invocation.getArgument(1));
when(implementation.loadTrueTypeFont(anyString(), anyString())).thenAnswer(invocation -> new Object());
when(implementation.deriveTrueTypeFont(any(), anyFloat(), anyInt())).thenAnswer(invocation -> new Object());
when(implementation.loadNativeFont(anyString())).thenAnswer(invocation -> new Object());
}

protected void configureTestImplementation(TestCodenameOneImplementation implementation) {
implementation.setDisplaySize(1080, 1920);
implementation.setDeviceDensity(Display.DENSITY_MEDIUM);
implementation.setTouchDevice(true);
implementation.setTimeoutSupported(true);
}

/**
* Processes any pending serial calls that were queued via {@link Display#callSerially(Runnable)}.
*/
Expand Down
Loading