|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2019 Kiel University and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + *******************************************************************************/ |
| 10 | +package org.eclipse.elk.alg.common.issues; |
| 11 | + |
| 12 | +import static org.junit.Assert.assertEquals; |
| 13 | +import static org.junit.Assert.assertTrue; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import org.eclipse.elk.alg.test.framework.LayoutTestRunner; |
| 18 | +import org.eclipse.elk.alg.test.framework.annotations.Algorithm; |
| 19 | +import org.eclipse.elk.alg.test.framework.annotations.Configurator; |
| 20 | +import org.eclipse.elk.alg.test.framework.annotations.DefaultConfiguration; |
| 21 | +import org.eclipse.elk.alg.test.framework.annotations.GraphResourceProvider; |
| 22 | +import org.eclipse.elk.alg.test.framework.io.AbstractResourcePath; |
| 23 | +import org.eclipse.elk.alg.test.framework.io.ModelResourcePath; |
| 24 | +import org.eclipse.elk.core.options.FixedLayouterOptions; |
| 25 | +import org.eclipse.elk.graph.ElkNode; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | + |
| 29 | +import com.google.common.collect.Lists; |
| 30 | + |
| 31 | +/** |
| 32 | + * Test for issue 475. |
| 33 | + */ |
| 34 | +@RunWith(LayoutTestRunner.class) |
| 35 | +@Algorithm(FixedLayouterOptions.ALGORITHM_ID) |
| 36 | +@DefaultConfiguration |
| 37 | +public class Issue475Test { |
| 38 | + |
| 39 | + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 40 | + // Sources |
| 41 | + |
| 42 | + @GraphResourceProvider |
| 43 | + public List<AbstractResourcePath> testGraphs() { |
| 44 | + return Lists.newArrayList(new ModelResourcePath("tickets/core/475_fixedLayoutWithLongLabels.elkt")); |
| 45 | + } |
| 46 | + |
| 47 | + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 48 | + // Configuration |
| 49 | + |
| 50 | + /** Default compound node size. */ |
| 51 | + private static final double SIZE = 20; |
| 52 | + |
| 53 | + @Configurator |
| 54 | + public void configureFixedGraphSize(final ElkNode graph) { |
| 55 | + for (ElkNode child : graph.getChildren()) { |
| 56 | + // Make sure we know the size the node shall have |
| 57 | + child.setProperty(FixedLayouterOptions.NODE_SIZE_FIXED_GRAPH_SIZE, true); |
| 58 | + child.setHeight(SIZE); |
| 59 | + child.setWidth(SIZE); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Configurator |
| 64 | + public void configureNoFixedGraphSize(final ElkNode graph) { |
| 65 | + graph.getChildren().stream() |
| 66 | + .forEach(node -> node.setProperty(FixedLayouterOptions.NODE_SIZE_FIXED_GRAPH_SIZE, false)); |
| 67 | + } |
| 68 | + |
| 69 | + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 70 | + // Tests |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testCompoundNodeSize(final ElkNode graph) { |
| 74 | + graph.getChildren().stream().forEach(node -> doTestCompoundNodeSize(node)); |
| 75 | + } |
| 76 | + |
| 77 | + public void doTestCompoundNodeSize(final ElkNode node) { |
| 78 | + if (node.getProperty(FixedLayouterOptions.NODE_SIZE_FIXED_GRAPH_SIZE)) { |
| 79 | + // The node's size should not have changed |
| 80 | + assertEquals(SIZE, node.getWidth(), 0); |
| 81 | + assertEquals(SIZE, node.getHeight(), 0); |
| 82 | + |
| 83 | + } else { |
| 84 | + double maxX = 0; |
| 85 | + double maxY = 0; |
| 86 | + |
| 87 | + for (ElkNode child : node.getChildren()) { |
| 88 | + maxX = Math.max(maxX, child.getX() + child.getWidth()); |
| 89 | + maxY = Math.max(maxY, child.getY() + child.getHeight()); |
| 90 | + } |
| 91 | + |
| 92 | + assertTrue(node.getWidth() >= maxX); |
| 93 | + assertTrue(node.getHeight() >= maxY); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments