Skip to main content
summaryrefslogtreecommitdiffstats
blob: 96f7c5198cb4003163a636b4b2e730fa5df8f7a2 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST.
 *
 * 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.tests.tests;

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl;
import org.eclipse.gmf.runtime.notation.BasicCompartment;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.TitleStyle;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.databinding.custom.CustomStringStyleObservableList;
import org.eclipse.papyrus.infra.gmfdiag.css.helper.CSSHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.helper.ResetStyleHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSDiagram;
import org.eclipse.papyrus.infra.gmfdiag.css.tests.Activator;
import org.eclipse.uml2.uml.NamedElement;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Test case for Compartment-related styles and properties
 * See resources/model/compartmentsTest/model.di for details
 *
 * @author Camille Letavernier
 * 
 */
public class CSSCompartmentsTests {

	public static final String CLASS_ATTRIBUTE_COMPARTMENT_TYPE = "7017"; //$NON-NLS-1$

	public static final String CLASS_OPERATION_COMPARTMENT_TYPE = "7018"; //$NON-NLS-1$

	public static final String CLASS_CLASSIFIER_COMPARTMENT_TYPE = "7019"; //$NON-NLS-1$

	public static final String INTERFACE_ATTRIBUTE_COMPARTMENT_TYPE = "7006"; //$NON-NLS-1$

	public static final String INTERFACE_OPERATION_COMPARTMENT_TYPE = "7007"; //$NON-NLS-1$

	public static final String INTERFACE_INTERFACEIFIER_COMPARTMENT_TYPE = "7008"; //$NON-NLS-1$

	public static final String ENUMERATION_LITERAL_COMPARTMENT_TYPE = "7015"; //$NON-NLS-1$

	private CSSDiagram diagram;

	@Before
	public void init() {
		ResourceSet resourceSet = new ResourceSetImpl();
		CSSHelper.installCSSSupport(resourceSet);

		URI uri = URI.createPlatformPluginURI(Activator.PLUGIN_ID + "/resources/model/compartmentsTest/model.notation", true); //$NON-NLS-1$
		try {
			Diagram diagram = (Diagram)EMFHelper.loadEMFModel(resourceSet, uri);
			Assert.assertNotNull("Cannot find the model", diagram);
			Assert.assertTrue("CSS are not activated on this resource", diagram instanceof CSSDiagram);
			this.diagram = (CSSDiagram)diagram;
		} catch (IOException ex) {
			Activator.log.error(ex);
		}
	}

	/* Test Class1 and Class2 */
	@Test
	public void testAttributeCompartment() {
		Shape class1 = findShape("Class1");
		Shape class2 = findShape("Class2");

		BasicCompartment attributes1 = findCompartment(class1, CLASS_ATTRIBUTE_COMPARTMENT_TYPE);
		Assert.assertTrue("Attribute compartment should be collapsed, as it contains a P1 property", attributes1.isCollapsed());
		Assert.assertTrue("Attribute compartment should be visible", attributes1.isVisible());
		BasicCompartment operations1 = findCompartment(class1, CLASS_OPERATION_COMPARTMENT_TYPE);
		Assert.assertTrue("Operation compartment should be visible", operations1.isVisible());
		BasicCompartment classifiers1 = findCompartment(class1, CLASS_CLASSIFIER_COMPARTMENT_TYPE);
		Assert.assertFalse("Classifiers compartment should be hidden", classifiers1.isVisible());

		BasicCompartment attributes2 = findCompartment(class2, CLASS_ATTRIBUTE_COMPARTMENT_TYPE);
		Assert.assertFalse("Attribute compartment should not be collapsed, as it doesn't contain a P1 property", attributes2.isCollapsed());
		Assert.assertTrue("Attribute compartment should be visible", attributes2.isVisible());
		BasicCompartment operations2 = findCompartment(class2, CLASS_OPERATION_COMPARTMENT_TYPE);
		Assert.assertTrue("Operation compartment should be visible", operations2.isVisible());
		BasicCompartment classifiers2 = findCompartment(class2, CLASS_CLASSIFIER_COMPARTMENT_TYPE);
		Assert.assertFalse("Classifiers compartment should be hidden", classifiers2.isVisible());


	}

	/* Test Enumeration1 */
	@Test
	//Currently fails. The EnumerationLiteral compartment is not defined in the NotationTypesMap
	public void testCaseInsensitiveForEnumeration() {
		Shape enumeration = findShape("Enumeration1");
		BasicCompartment compartment = findCompartment(enumeration, ENUMERATION_LITERAL_COMPARTMENT_TYPE);

		Assert.assertFalse("EnumerationLiteral Compartment should not be collapsed", compartment.isCollapsed());
	}

	/* Test Interface 1 and Interface 2 */
	//Currently fails. The interface compartments are not defined in the NotationTypesMap
	@Test
	public void testAttributeCompartmentForInterface() {
		Shape interface1 = findShape("Interface1");
		Shape interface2 = findShape("Interface2");

		BasicCompartment attributes1 = findCompartment(interface1, INTERFACE_ATTRIBUTE_COMPARTMENT_TYPE);
		Assert.assertTrue("Attribute compartment should be visible, as it contains a Property", attributes1.isVisible());
		BasicCompartment operations1 = findCompartment(interface1, INTERFACE_OPERATION_COMPARTMENT_TYPE);
		Assert.assertTrue("Operation compartment should be visible", operations1.isVisible());
		BasicCompartment classifiers1 = findCompartment(interface1, INTERFACE_INTERFACEIFIER_COMPARTMENT_TYPE);
		Assert.assertFalse("Classifiers compartment should be hidden", classifiers1.isVisible());

		BasicCompartment attributes2 = findCompartment(interface2, INTERFACE_ATTRIBUTE_COMPARTMENT_TYPE);
		Assert.assertFalse("Attribute compartment should not be visible, as it doesn't contain any property", attributes2.isVisible());
		BasicCompartment operations2 = findCompartment(interface2, INTERFACE_OPERATION_COMPARTMENT_TYPE);
		Assert.assertTrue("Operation compartment should be visible", operations2.isVisible());
		BasicCompartment classifiers2 = findCompartment(interface2, INTERFACE_INTERFACEIFIER_COMPARTMENT_TYPE);
		Assert.assertFalse("Classifiers compartment should be hidden", classifiers2.isVisible());
	}

	/* Test Class 3 */
	@Test
	public void testCompartmentForceValue() {
		//Test initial situation
		Shape class3 = findShape("Class3");

		//Check all compartments
		for(View childNode : (List<View>)class3.getChildren()) {
			if(childNode instanceof BasicCompartment) {
				Assert.assertFalse("All compartments from Class3 should be hidden. " + childNode.getType() + " is visible", childNode.isVisible());
			}
		}

		//We need an editingDomain for the ResetStyle operation
		TransactionalEditingDomain domain = new TransactionalEditingDomainImpl(new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE), diagram.eResource().getResourceSet());
		diagram.eResource().getResourceSet().eAdapters().add(new AdapterFactoryEditingDomain.EditingDomainProvider(domain));

		//Test resetStyle
		ResetStyleHelper.resetStyle(Collections.singleton(class3));

		//Check only some specific compartments. Some style rules still hide the nestedClassifier compartment. The visibility of e.g. the ShapeCompartment is undetermined.
		BasicCompartment attributesCompartment = findCompartment(class3, CLASS_ATTRIBUTE_COMPARTMENT_TYPE);
		BasicCompartment operationsCompartment = findCompartment(class3, CLASS_OPERATION_COMPARTMENT_TYPE);

		Assert.assertTrue("Attributes compartment should be visible after the reset style", attributesCompartment.isVisible());
		Assert.assertTrue("Operations compartment should be visible after the reset style", operationsCompartment.isVisible());
	}

	/* Test apply style */
	@Test
	//Currently fails. showTitle is not properly supported for BasicCompartments
	//Unlike Compartment, BasicCompartment doesn't implement TitleStyle. Instead,
	//it owns a specific instance of TitleStyle, which is not supported by the CSS Engine (?)
	public void testShowAllCompartmentsTitles() {
		//Initial state: title should be hidden
		for(View childNode : (List<View>)diagram.getChildren()) {
			for(View compartment : (List<View>)childNode.getChildren()) {
				if(compartment instanceof BasicCompartment) {
					TitleStyle titleStyle = (TitleStyle)compartment.getStyle(NotationPackage.eINSTANCE.getTitleStyle());
					Assert.assertFalse("Title should be hidden", titleStyle.isShowTitle());
				}
			}
		}

		//Apply style
		CustomStringStyleObservableList stylesList = new CustomStringStyleObservableList(diagram, null, "cssClass");
		stylesList.add("showTitleForAllCompartments");

		//Check that all titles are visible
		for(View childNode : (List<View>)diagram.getChildren()) {
			for(View compartment : (List<View>)childNode.getChildren()) {
				if(compartment instanceof BasicCompartment) {
					TitleStyle titleStyle = (TitleStyle)compartment.getStyle(NotationPackage.eINSTANCE.getTitleStyle());
					Assert.assertTrue("Title should be visible", titleStyle.isShowTitle());
				}
			}
		}
	}


	@After
	public void dispose() {
		Iterator<Resource> iterator = diagram.eResource().getResourceSet().getResources().iterator();
		while(iterator.hasNext()) {
			iterator.next().unload();
			iterator.remove();
		}
	}

	private BasicCompartment findCompartment(Shape element, String type) {
		for(View childNode : (List<View>)element.getChildren()) {
			if(type.equals(childNode.getType())) {
				return (BasicCompartment)childNode;
			}
		}

		return null;
	}

	private Shape findShape(String elementName) {
		for(Object viewObject : diagram.getChildren()) {
			View view = (View)viewObject;
			if(view instanceof Shape && view.getElement() instanceof NamedElement) {
				NamedElement element = (NamedElement)view.getElement();
				if(elementName.equals(element.getName())) {
					return (Shape)view;
				}
			}
		}

		Assert.fail("Cannot find the view associated to " + elementName);
		return null;
	}

}

Back to the top