Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6dac2a4
Add comprehensive geometry and graphics unit tests
shai-almog Oct 23, 2025
5145d1d
Adjust geometry unit tests for library behaviors
shai-almog Oct 23, 2025
2b3da87
Adapt GeneralPath tests for headless transforms
shai-almog Oct 23, 2025
04b38e0
Fix GeneralPath tests for headless environment
shai-almog Oct 23, 2025
7cbc9e8
Adjust GeneralPath tests for runtime behavior
shai-almog Oct 23, 2025
47ad477
Expand graphics and geometry test coverage
shai-almog Oct 23, 2025
6d16967
Augment geometry and path iterator unit coverage
shai-almog Oct 24, 2025
19297c7
Ensure graphics tests flush EDT queue
shai-almog Oct 24, 2025
d1b4ee9
Add transform coverage to GeneralPath tests
shai-almog Oct 24, 2025
e8361c9
Adjust GraphicsTest to verify double paint coordinates
shai-almog Oct 24, 2025
3317b19
Handle TestRunnerComponent exceptions during test runs
shai-almog Oct 24, 2025
996d245
Revert TestRunnerComponent changes
shai-almog Oct 24, 2025
f6fad17
Align ChartComponent shape conversion test with coordinate helpers
shai-almog Oct 24, 2025
c50b32f
Adjust ChartComponent shape conversion round-trip assertions
shai-almog Oct 24, 2025
23ead62
Refine ChartComponent shape conversion assertions
shai-almog Oct 24, 2025
3307773
Adjust ChartComponent shape conversion round-trip expectations
shai-almog Oct 24, 2025
e3a75a3
Adjust shape conversion test expectations to use coordinate helpers
shai-almog Oct 24, 2025
c6bb139
Refine ChartComponent shape conversion tests
shai-almog Oct 24, 2025
97159c1
Align ChartComponent shape conversion expectations
shai-almog Oct 24, 2025
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 @@ -59,18 +59,58 @@ 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();

Point expectedChartTopLeft = component.screenToChartCoord(screenRect.getX(), screenRect.getY());
Point expectedChartBottomRight = component.screenToChartCoord(
screenRect.getX() + screenRect.getSize().getWidth(),
screenRect.getY() + screenRect.getSize().getHeight()
);

assertEquals(Math.round(expectedChartTopLeft.getX()), chartBounds.getX());
assertEquals(Math.round(expectedChartTopLeft.getY()), chartBounds.getY());
assertEquals(Math.round(expectedChartBottomRight.getX() - expectedChartTopLeft.getX()), chartBounds.getSize().getWidth());
assertEquals(Math.round(expectedChartBottomRight.getY() - expectedChartTopLeft.getY()), chartBounds.getSize().getHeight());

Rectangle roundTrippedScreen = component.chartToScreenShape(chartShape).getBounds();
Point expectedScreenTopLeft = component.chartToScreenCoord(chartBounds.getX(), chartBounds.getY());
Point expectedScreenBottomRight = component.chartToScreenCoord(
chartBounds.getX() + chartBounds.getSize().getWidth(),
chartBounds.getY() + chartBounds.getSize().getHeight()
);

assertEquals(Math.round(expectedScreenTopLeft.getX()), roundTrippedScreen.getX());
assertEquals(Math.round(expectedScreenTopLeft.getY()), roundTrippedScreen.getY());
assertEquals(Math.round(expectedScreenBottomRight.getX() - expectedScreenTopLeft.getX()), roundTrippedScreen.getSize().getWidth());
assertEquals(Math.round(expectedScreenBottomRight.getY() - expectedScreenTopLeft.getY()), roundTrippedScreen.getSize().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());
Shape screenShape = component.chartToScreenShape(chartRect);
Rectangle screenBounds = screenShape.getBounds();

Point expectedScreenFromChartTopLeft = component.chartToScreenCoord(chartRect.getX(), chartRect.getY());
Point expectedScreenFromChartBottomRight = component.chartToScreenCoord(
chartRect.getX() + chartRect.getSize().getWidth(),
chartRect.getY() + chartRect.getSize().getHeight()
);

assertEquals(Math.round(expectedScreenFromChartTopLeft.getX()), screenBounds.getX());
assertEquals(Math.round(expectedScreenFromChartTopLeft.getY()), screenBounds.getY());
assertEquals(Math.round(expectedScreenFromChartBottomRight.getX() - expectedScreenFromChartTopLeft.getX()), screenBounds.getSize().getWidth());
assertEquals(Math.round(expectedScreenFromChartBottomRight.getY() - expectedScreenFromChartTopLeft.getY()), screenBounds.getSize().getHeight());

Rectangle roundTrippedChart = component.screenToChartShape(screenShape).getBounds();
Point expectedChartFromScreenTopLeft = component.screenToChartCoord(screenBounds.getX(), screenBounds.getY());
Point expectedChartFromScreenBottomRight = component.screenToChartCoord(
screenBounds.getX() + screenBounds.getSize().getWidth(),
screenBounds.getY() + screenBounds.getSize().getHeight()
);

assertEquals(Math.round(expectedChartFromScreenTopLeft.getX()), roundTrippedChart.getX());
assertEquals(Math.round(expectedChartFromScreenTopLeft.getY()), roundTrippedChart.getY());
assertEquals(Math.round(expectedChartFromScreenBottomRight.getX() - expectedChartFromScreenTopLeft.getX()), roundTrippedChart.getSize().getWidth());
assertEquals(Math.round(expectedChartFromScreenBottomRight.getY() - expectedChartFromScreenTopLeft.getY()), roundTrippedChart.getSize().getHeight());
}

@Test
Expand Down
170 changes: 170 additions & 0 deletions maven/core-unittests/src/test/java/com/codename1/ui/GraphicsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package com.codename1.ui;

import com.codename1.test.UITestBase;
import com.codename1.ui.Paint;
import com.codename1.ui.Stroke;
import com.codename1.ui.geom.Rectangle;
import com.codename1.ui.geom.Shape;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.lang.reflect.Constructor;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.same;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class GraphicsTest extends UITestBase {

private Object nativeGraphics;

@BeforeEach
@Override
protected void setUpDisplay() throws Exception {
super.setUpDisplay();
nativeGraphics = new Object();
when(implementation.isTranslationSupported()).thenReturn(false);
when(implementation.getClipX(any())).thenReturn(0);
when(implementation.getClipY(any())).thenReturn(0);
when(implementation.getClipWidth(any())).thenReturn(100);
when(implementation.getClipHeight(any())).thenReturn(100);
when(implementation.isShapeSupported(any())).thenReturn(true);
when(implementation.isShapeClipSupported(any())).thenReturn(true);
}

@AfterEach
@Override
protected void tearDownDisplay() throws Exception {
flushSerialCalls();
super.tearDownDisplay();
}

private Graphics newGraphics() throws Exception {
Constructor<Graphics> constructor = Graphics.class.getDeclaredConstructor(Object.class);
constructor.setAccessible(true);
return constructor.newInstance(nativeGraphics);
}

@Test
void testTranslateDelegatesWhenSupported() throws Exception {
when(implementation.isTranslationSupported()).thenReturn(true);
when(implementation.getTranslateX(nativeGraphics)).thenReturn(3);
when(implementation.getTranslateY(nativeGraphics)).thenReturn(4);
Graphics graphics = newGraphics();
graphics.translate(5, 6);
verify(implementation).translate(nativeGraphics, 5, 6);
assertEquals(3, graphics.getTranslateX());
assertEquals(4, graphics.getTranslateY());
}

@Test
void testTranslateWithoutSupportAccumulates() throws Exception {
when(implementation.isTranslationSupported()).thenReturn(false);
Graphics graphics = newGraphics();
graphics.translate(2, 3);
graphics.translate(1, -1);
verify(implementation, never()).translate(any(), anyInt(), anyInt());
assertEquals(3, graphics.getTranslateX());
assertEquals(2, graphics.getTranslateY());
}

@Test
void testSetColorAndSetAndGetColor() throws Exception {
Graphics graphics = newGraphics();
graphics.setColor(0x123456);
verify(implementation).setColor(nativeGraphics, 0x123456);
int previous = graphics.setAndGetColor(0xABCDEF);
assertEquals(0x123456, previous);
verify(implementation).setColor(nativeGraphics, 0xABCDEF & 0xFFFFFF);
}

@Test
void testClipRectUsesTranslationOffsets() throws Exception {
Graphics graphics = newGraphics();
graphics.translate(5, 7);
graphics.clipRect(1, 2, 3, 4);
verify(implementation).clipRect(nativeGraphics, 6, 9, 3, 4);
}

@Test
void testSetClipShapeAppliesTranslation() throws Exception {
Graphics graphics = newGraphics();
graphics.translate(10, 5);
Rectangle shape = new Rectangle(0, 0, 10, 10);
graphics.setClip(shape);
ArgumentCaptor<Shape> shapeCaptor = ArgumentCaptor.forClass(Shape.class);
verify(implementation).setClip(eq(nativeGraphics), shapeCaptor.capture());
Rectangle bounds = shapeCaptor.getValue().getBounds();
assertEquals(10, bounds.getX());
assertEquals(5, bounds.getY());
}

@Test
void testDrawShapeTranslatesWhenNeeded() throws Exception {
Graphics graphics = newGraphics();
graphics.translate(3, 4);
Rectangle shape = new Rectangle(0, 0, 5, 5);
Stroke stroke = new Stroke(1, Stroke.CAP_BUTT, Stroke.JOIN_MITER, 1f);
graphics.drawShape(shape, stroke);
ArgumentCaptor<Shape> shapeCaptor = ArgumentCaptor.forClass(Shape.class);
verify(implementation).drawShape(eq(nativeGraphics), shapeCaptor.capture(), eq(stroke));
Rectangle bounds = shapeCaptor.getValue().getBounds();
assertEquals(3, bounds.getX());
assertEquals(4, bounds.getY());
}

@Test
void testFillShapeWithPaintUsesCustomPaint() throws Exception {
Graphics graphics = newGraphics();
Paint paint = mock(Paint.class);
graphics.setColor(paint);
Rectangle shape = new Rectangle(0, 0, 10, 10);
graphics.fillShape(shape);
verify(paint).paint(same(graphics), eq(0.0), eq(0.0), eq(10.0), eq(10.0));
verify(implementation).setClip(eq(nativeGraphics), any(Shape.class));
verify(implementation).clipRect(nativeGraphics, 0, 0, 100, 100);
verify(implementation).setClip(nativeGraphics, 0, 0, 100, 100);
}

@Test
void testPushAndPopClipDelegates() throws Exception {
Graphics graphics = newGraphics();
graphics.pushClip();
graphics.popClip();
verify(implementation).pushClip(nativeGraphics);
verify(implementation).popClip(nativeGraphics);
}

@Test
void testDrawLineAppliesTranslation() throws Exception {
Graphics graphics = newGraphics();
graphics.translate(2, 3);
graphics.drawLine(1, 1, 4, 4);
verify(implementation).drawLine(nativeGraphics, 3, 4, 6, 7);
}

@Test
void testGetClipReflectsCurrentTranslation() throws Exception {
when(implementation.getClipX(nativeGraphics)).thenReturn(50);
when(implementation.getClipY(nativeGraphics)).thenReturn(60);
Graphics graphics = newGraphics();
graphics.translate(5, 7);
assertEquals(45, graphics.getClipX());
assertEquals(53, graphics.getClipY());
}

@Test
void testSetAlphaDelegatesToImplementation() throws Exception {
Graphics graphics = newGraphics();
graphics.setAlpha(128);
verify(implementation).setAlpha(nativeGraphics, 128);
}
}
Loading
Loading