Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 80ed236cf4c0c706404400919402dba18480005a (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
/*****************************************************************************
 * Copyright (c) 2012, 2014 CEA LIST and others.
 *
 * 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
 *  Christian W. Damus (CEA) - bug 422257
 *  
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.tests.tests;

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

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.GradientStyle;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.papyrus.infra.emf.appearance.helper.AppearanceHelper;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.engine.WorkspaceCSSEngine;
import org.eclipse.papyrus.infra.gmfdiag.css.helper.CSSHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSDiagram;
import org.eclipse.papyrus.infra.gmfdiag.css.preferences.ThemePreferences;
import org.eclipse.papyrus.infra.gmfdiag.css.tests.Activator;
import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
import org.eclipse.papyrus.junit.utils.tests.AbstractPapyrusTest;
import org.eclipse.uml2.uml.NamedElement;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;


public class CSSStylesheetTest extends AbstractPapyrusTest {

	@Rule
	public final HouseKeeper houseKeeper = new HouseKeeper();
	
	private CSSDiagram diagram;

	@BeforeClass
	public static void initCSSTheme() {
		IPreferenceStore cssThemePreferences = org.eclipse.papyrus.infra.gmfdiag.css.Activator.getDefault().getPreferenceStore();
		cssThemePreferences.setValue(ThemePreferences.CURRENT_THEME, "org.eclipse.papyrus.css.papyrus_theme");
		WorkspaceCSSEngine.instance.reset();
	}

	@Before
	public void init() {
		ResourceSet resourceSet = houseKeeper.createResourceSet();
		CSSHelper.installCSSSupport(resourceSet);

		URI uri = URI.createPlatformPluginURI(Activator.PLUGIN_ID + "/resources/model/stylesheetTest/model.notation", true);
		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
	public void testClassStyle() {
		Shape classView = findShape("Class1");
		if(classView == null) {
			return;
		}

		Assert.assertEquals("Invalid color", rgb(255, 0, 0), classView.getFillColor()); //Red = #FF0000
		Assert.assertNull("Invalid gradient", classView.getGradient());
		Assert.assertTrue("The element icon should be displayed", AppearanceHelper.showElementIcon(classView));
	}

	@Test
	public void testInterfaceStyle() {
		Shape interfaceView = findShape("Interface1");

		Assert.assertNotNull("The interface should have a gradient", interfaceView.getGradient());
		Assert.assertEquals("The fill color should be yellow", rgb(255, 255, 0), interfaceView.getFillColor()); //Yellow = #FFFF00
		Assert.assertEquals("The gradient should be red", rgb(255, 0, 0), interfaceView.getGradient().getGradientColor1()); //Red = #FF0000
		Assert.assertEquals("The gradient should be vertical", GradientStyle.VERTICAL, interfaceView.getGradient().getGradientStyle());
		Assert.assertTrue("The element icon should be displayed", AppearanceHelper.showElementIcon(interfaceView));
	}

	@Test
	public void testCSSClassStyle() {
		Shape interfaceView = findShape("Interface2");
		Assert.assertNotNull("The interface should have a gradient", interfaceView.getGradient());
		Assert.assertEquals("The fill color should be light green (#90EE90)", rgb(144, 238, 144), interfaceView.getFillColor()); //Lightgreen = #90EE90

		//Case insensitive color name
		Assert.assertEquals(rgb(0, 191, 255), interfaceView.getGradient().getGradientColor1()); //DeepSkyBlue = #00BFFF

		Assert.assertEquals(GradientStyle.HORIZONTAL, interfaceView.getGradient().getGradientStyle());
		Assert.assertFalse(AppearanceHelper.showElementIcon(interfaceView));

		//TODO: Font names should be case-insensitive
		Assert.assertEquals("KaiTi", interfaceView.getFontName());
	}

	@Test
	public void testPackage() {
		Shape packageView = findShape("Package1");
		Assert.assertNotNull(packageView.getGradient());

		//Inherited style (From *)
		Assert.assertEquals(rgb(255, 0, 0), packageView.getGradient().getGradientColor1()); //Red = #FF0000
	}

	@Test
	public void testPriorities() {
		//Tests the priorities between styles:
		//.myClass : Low priority
		//.myClass.myOtherClass : Highest priority
		//[isAbstract=true] : Medium priority
		//Styles should also be merged when they aren't conflicting (ie. [isAbstract] + .myClass -> fontName = Tunga)
		Shape class3 = findShape("Class3");
		Shape class5 = findShape("Class5");

		testBlueStyle(class3); //isAbstract
		testBlueStyle(class5); //isAbstract

		Shape class4 = findShape("Class4");

		testWhiteStyle(class4); //.myClass.myOtherClass (Overrides isAbstract)
		testFontName(class4); //.myClass (Doesn't conflict with isAbstract)

		Shape class6 = findShape("Class6");

		testWhiteStyle(class6); //.myClass
		testFontName(class6); //.myClass

		Shape class7 = findShape("Class7");

		testBlueStyle(class7); //isAbstract
		testFontName(class7); //.myClass (Doesn't conflict with isAbstract)
	}

	private void testBlueStyle(Shape shape) {
		Assert.assertEquals(rgb(195, 205, 255), shape.getFillColor()); //rgb syntax
		Assert.assertTrue(shape.isBold());
		Assert.assertNotNull(shape.getGradient());
	}

	private void testWhiteStyle(Shape shape) {
		Assert.assertEquals(rgb(255, 255, 255), shape.getFillColor()); //White = #FFFFFF (Hexa syntax)
		Assert.assertFalse(shape.isBold());
		Assert.assertNull(shape.getGradient());
	}

	private void testFontName(Shape shape) {
		Assert.assertEquals("Tunga", shape.getFontName());
	}

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

	private int rgb(int red, int green, int blue) {
		return red | green << 8 | blue << 16;
	}

	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