Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 78e7639ad100cb7f82a90d71bccd434fba515e63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
 * Copyright (c) 2010, 2018 THALES GLOBAL SERVICES.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.tests.unit.diagram.format.data;

import java.util.Collection;
import java.util.Map;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.formatdata.NodeFormatData;
import org.eclipse.sirius.diagram.ui.edit.api.part.IAbstractDiagramNodeEditPart;
import org.eclipse.sirius.diagram.ui.edit.api.part.IDiagramContainerEditPart;
import org.eclipse.sirius.diagram.ui.internal.edit.parts.AbstractDNodeContainerCompartmentEditPart;
import org.eclipse.sirius.diagram.ui.tools.api.format.semantic.SiriusFormatDataManagerForSemanticElements;
import org.eclipse.sirius.diagram.ui.tools.internal.editor.DDiagramEditorImpl;
import org.eclipse.sirius.diagram.ui.tools.internal.format.NodeFormatDataKey;
import org.eclipse.sirius.tests.SiriusTestsPlugin;
import org.eclipse.sirius.tests.support.api.SiriusDiagramTestCase;
import org.eclipse.sirius.tests.support.api.TestsUtil;
import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager;

import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.UnmodifiableIterator;

/**
 * Test class for {@link SiriusFormatDataManagerForSemanticElements}.
 * 
 * @author dlecan
 */
public class SiriusFormatDataManagerForDDiagramElementWithSameSemanticElementsTest extends SiriusDiagramTestCase {

    private static final int REF_SIZE = 25;

    private static final String WRONG_TESTS_DATA = "Wrong tests data.";

    private static final String PLUGIN_PATH = "/" + SiriusTestsPlugin.PLUGIN_ID + "/";

    private static final String DATA_PATH = "data/unit/layout/vp3089/";

    private static final String FULL_DATA_PATH = PLUGIN_PATH + DATA_PATH;

    private static final String SEMANTIC_MODEL_PATH = FULL_DATA_PATH + "vp-3089.ecore";

    private static final String MODELER_PATH = FULL_DATA_PATH + "vp-3089.odesign";

    private static final String REPRESENTATIONS_PATH = FULL_DATA_PATH + "vp-3089.aird";

    private static final String REPRESENTATION_DESC = "VP-3089";

    /**
     * {@inheritDoc}
     */
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        genericSetUp(SEMANTIC_MODEL_PATH, MODELER_PATH, REPRESENTATIONS_PATH);
    }

    /**
     * Test method.
     * 
     * @throws Exception
     *             Test error.
     */
    public void testHierarchyWithCommonSemanticTargetStoreFormat() throws Exception {
        DDiagram diagram = (DDiagram) getRepresentations(REPRESENTATION_DESC, semanticModel).toArray()[0];

        // check that all DDiagramElements have the same semantic target.
        EObject commonSemTgt = diagram.getOwnedDiagramElements().iterator().next().getTarget();
        UnmodifiableIterator<DDiagramElement> eAllDDiagramElements = Iterators.filter(diagram.eAllContents(), DDiagramElement.class);
        assertEquals(WRONG_TESTS_DATA, 4, Iterators.size(eAllDDiagramElements));
        for (DDiagramElement dde : Lists.newArrayList(eAllDDiagramElements)) {
            String message = "Wrong data for the current test : there should be on ecommon semantic target for all elements on the diagram.";
            assertEquals(message, commonSemTgt, dde.getTarget());
        }

        DDiagramEditorImpl editor = (DDiagramEditorImpl) DialectUIManager.INSTANCE.openEditor(session, diagram, new NullProgressMonitor());
        TestsUtil.synchronizationWithUIThread();

        // Check that there are 2 top level elements.
        Iterable<IAbstractDiagramNodeEditPart> firstLevelParts = getChildren(editor.getDiagramEditPart());
        assertEquals(WRONG_TESTS_DATA, 2, Iterables.size(firstLevelParts));

        // Check the SiriusFormatDataManagerForSemanticElements's behavior
        // with them. One is Node with a border node, the other is a container
        // with a child node.
        for (IAbstractDiagramNodeEditPart idep : firstLevelParts) {
            checkFormatManagerBehaviorWithCommonSemanticElement(idep);
        }
    }

    private void checkFormatManagerBehaviorWithCommonSemanticElement(IAbstractDiagramNodeEditPart idep) {
        Iterable<IAbstractDiagramNodeEditPart> children = getChildren(idep);

        // Each top level element has one child.
        assertEquals(WRONG_TESTS_DATA, 1, Iterables.size(children));

        // The top level part has "big" bounds.
        Rectangle l1Bounds = idep.getFigure().getBounds().getCopy();
        assertTrue(WRONG_TESTS_DATA, l1Bounds.height > REF_SIZE);
        assertTrue(WRONG_TESTS_DATA, l1Bounds.width > REF_SIZE);

        // The second level part has "small" bounds.
        Rectangle l2Bounds = children.iterator().next().getFigure().getBounds().getCopy();
        assertTrue(WRONG_TESTS_DATA, l2Bounds.height < REF_SIZE);
        assertTrue(WRONG_TESTS_DATA, l2Bounds.width < REF_SIZE);

        // Store the format data (copy format) and get the stored values.
        SiriusFormatDataManagerForSemanticElements mgr = new SiriusFormatDataManagerForSemanticElements();
        mgr.storeFormatData(idep);
        Map<? extends NodeFormatDataKey, Map<String, NodeFormatData>> data = mgr.getNodeFormatData();

        // Check the stored data
        assertEquals("There should be only one top level format data", 1, data.size());
        Collection<Map<String, NodeFormatData>> formatDataValues = data.values();
        assertEquals("There should be only one top level format data", 1, formatDataValues.size());

        NodeFormatDataKey formatDataKey = data.entrySet().iterator().next().getKey();
        assertEquals("There format data do not corresponds to the common semantic target", EcoreUtil.getURI(idep.resolveTargetSemanticElement()).fragment(), formatDataKey.getId());

        // With wrong behavior, the hierarchy was not respected, there was no
        // second level data and the recorded data was the small size.

        Map<String, NodeFormatData> formatDataMap = formatDataValues.iterator().next();
        NodeFormatData l1Data = formatDataMap.entrySet().iterator().next().getValue();
        assertEquals(WRONG_TESTS_DATA, l1Bounds.height, l1Data.getHeight());
        assertEquals(WRONG_TESTS_DATA, l1Bounds.width, l1Data.getWidth());

        assertTrue("There should be format data for the second level", l1Data.getChildren().size() == 1);
        NodeFormatData l2Data = l1Data.getChildren().iterator().next();
        assertEquals(WRONG_TESTS_DATA, l2Bounds.height, l2Data.getHeight());
        assertEquals(WRONG_TESTS_DATA, l2Bounds.width, l2Data.getWidth());
    }

    private Iterable<IAbstractDiagramNodeEditPart> getChildren(IGraphicalEditPart part) {
        if (part instanceof IDiagramContainerEditPart) {
            return getChildren(Iterables.filter(((IDiagramContainerEditPart) part).getChildren(), AbstractDNodeContainerCompartmentEditPart.class).iterator().next());
        }
        return Iterables.filter(part.getChildren(), IAbstractDiagramNodeEditPart.class);
    }
}

Back to the top