From 49212bce0e78723df667ba544301a772343feaa5 Mon Sep 17 00:00:00 2001 From: Laurent Fasani Date: Fri, 4 Mar 2022 14:16:48 +0100 Subject: [PATCH] [781] Update the tests for multiline label Bug: https://github.com/eclipse-sirius/sirius-components/issues/781 Signed-off-by: Laurent Fasani --- .../layout/incremental/DiagramLayoutTest.java | 43 +++++++++++++++++-- .../NodeLabelPositionProviderTests.java | 4 +- .../services/TestDiagramCreationService.java | 4 ++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/incremental/DiagramLayoutTest.java b/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/incremental/DiagramLayoutTest.java index 7139202543e..af1cf8768b8 100644 --- a/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/incremental/DiagramLayoutTest.java +++ b/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/incremental/DiagramLayoutTest.java @@ -32,10 +32,11 @@ import org.eclipse.sirius.components.diagrams.description.DiagramDescription; import org.eclipse.sirius.components.diagrams.events.IDiagramEvent; import org.eclipse.sirius.components.diagrams.events.SinglePositionEvent; +import org.eclipse.sirius.components.diagrams.layout.ELKDiagramConverter; import org.eclipse.sirius.components.diagrams.layout.ELKLayoutedDiagramProvider; -import org.eclipse.sirius.components.diagrams.layout.IELKDiagramConverter; import org.eclipse.sirius.components.diagrams.layout.LayoutConfiguratorRegistry; import org.eclipse.sirius.components.diagrams.layout.LayoutService; +import org.eclipse.sirius.components.diagrams.layout.TextBoundsService; import org.eclipse.sirius.components.diagrams.layout.incremental.provider.ImageSizeProvider; import org.eclipse.sirius.components.diagrams.layout.incremental.provider.NodeSizeProvider; import org.eclipse.sirius.components.diagrams.layout.services.DefaultTestDiagramDescriptionProvider; @@ -90,8 +91,8 @@ public Optional findById(IEditingContext editingCont NodeSizeProvider nodeSizeProvider = new NodeSizeProvider(new ImageSizeProvider()); IncrementalLayoutEngine incrementalLayoutEngine = new IncrementalLayoutEngine(nodeSizeProvider); - LayoutService layoutService = new LayoutService(new IELKDiagramConverter.NoOp(), new IncrementalLayoutDiagramConverter(), new LayoutConfiguratorRegistry(List.of()), - new ELKLayoutedDiagramProvider(), new IncrementalLayoutedDiagramProvider(), representationDescriptionSearchService, incrementalLayoutEngine); + LayoutService layoutService = new LayoutService(new ELKDiagramConverter(new TextBoundsService(), new ImageSizeProvider()), new IncrementalLayoutDiagramConverter(), + new LayoutConfiguratorRegistry(List.of()), new ELKLayoutedDiagramProvider(), new IncrementalLayoutedDiagramProvider(), representationDescriptionSearchService, incrementalLayoutEngine); return new TestDiagramCreationService(this.objectService, representationDescriptionSearchService, layoutService); } @@ -159,4 +160,40 @@ public void testSimpleDiagramLayout() throws IOException { Node layoutedThirdParent = optionalLayoutedThirdParent.get(); assertThat(layoutedThirdParent.getPosition()).isEqualTo(Position.at(301, 101)); } + + @Test + public void testNodeLayoutWithMultilineLabel() throws IOException { + String nodeLabelWithMultiple = "First LineAAAAAAAA\nSecond LineBBBBBBBBB"; //$NON-NLS-1$ + String firstChildTargetObjectId = "First child"; //$NON-NLS-1$ + + // @formatter:off + Diagram diagram = TestLayoutDiagramBuilder.diagram("Root") //$NON-NLS-1$ + .nodes() + .rectangleNode(nodeLabelWithMultiple).at(10, 10).of(10, 10) + .childNodes() + .rectangleNode(firstChildTargetObjectId).at(10, 10).of(50, 50).and() + .and() + .and() + .and() + .build(); + // @formatter:on + + Path path = Paths.get("src", "test", "resources", "editing-contexts", "testSimpleDiagramLayout"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ //$NON-NLS-5$ + JsonBasedEditingContext editingContext = new JsonBasedEditingContext(path); + + TestDiagramCreationService diagramCreationService = this.createDiagramCreationService(diagram); + + Diagram layoutedDiagram = diagramCreationService.performElKLayout(editingContext, diagram); + + Optional optionalFirstParent = this.getNode(layoutedDiagram.getNodes(), nodeLabelWithMultiple); + assertThat(optionalFirstParent).isPresent(); + Node firstParent = optionalFirstParent.get(); + + // Check that the parent node and the label have the right size + assertThat(firstParent.getSize()).isEqualTo(Size.of(195.8818359375, 131.197265625)); + assertThat(firstParent.getLabel().getSize()).isEqualTo(Size.of(161.8818359375, 32.197265625)); + + // Check that the inner node is under the multi line label area + assertThat(firstParent.getChildNodes().get(0).getPosition()).isEqualTo(Position.at(12, 49.197265625)); + } } diff --git a/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/incremental/NodeLabelPositionProviderTests.java b/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/incremental/NodeLabelPositionProviderTests.java index 48145afe181..57f9432dc7a 100644 --- a/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/incremental/NodeLabelPositionProviderTests.java +++ b/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/incremental/NodeLabelPositionProviderTests.java @@ -62,7 +62,7 @@ public void testNodeImageLabelBoundsPosition() { LabelLayoutData labelLayoutData = this.createLabelLayoutData(); Position position = labelBoundsProvider.getPosition(nodeLayoutData, labelLayoutData); - assertThat(position).extracting(Position::getX).isEqualTo(Double.valueOf(42.5390625)); + assertThat(position).extracting(Position::getX).isEqualTo(Double.valueOf(DEFAULT_NODE_SIZE.getWidth() / 2)); assertThat(position).extracting(Position::getY).isEqualTo(Double.valueOf(-23.3984375)); } @@ -73,7 +73,7 @@ public void testNodeRectangleLabelBoundsPosition() { NodeLabelPositionProvider labelBoundsProvider = new NodeLabelPositionProvider(new LayoutConfiguratorRegistry(List.of()).getDefaultLayoutConfigurator()); LabelLayoutData labelLayoutData = this.createLabelLayoutData(); Position position = labelBoundsProvider.getPosition(nodeLayoutData, labelLayoutData); - assertThat(position).extracting(Position::getX).isEqualTo(Double.valueOf(42.5390625)); + assertThat(position).extracting(Position::getX).isEqualTo(Double.valueOf(DEFAULT_NODE_SIZE.getWidth() / 2)); assertThat(position).extracting(Position::getY).isEqualTo(Double.valueOf(5)); } diff --git a/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/services/TestDiagramCreationService.java b/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/services/TestDiagramCreationService.java index 3d1c6c3ae22..80f447f9bc3 100644 --- a/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/services/TestDiagramCreationService.java +++ b/backend/sirius-components-diagrams-layout/src/test/java/org/eclipse/sirius/components/diagrams/layout/services/TestDiagramCreationService.java @@ -94,4 +94,8 @@ public Diagram performLayout(IEditingContext editingContext, Diagram diagram, ID return this.layoutService.incrementalLayout(editingContext, diagram, Optional.of(diagramEvent)); } + public Diagram performElKLayout(IEditingContext editingContext, Diagram diagram) { + return this.layoutService.layout(editingContext, diagram); + } + }