Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 767be6c9ac103b0b1668f8d28d16a128071a9543 (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
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * 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
 *******************************************************************************/

package org.eclipse.etrice.ui.behavior.commands;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.net.URL;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.ChoicePoint;
import org.eclipse.etrice.core.room.EntryPoint;
import org.eclipse.etrice.core.room.ExitPoint;
import org.eclipse.etrice.core.room.InitialTransition;
import org.eclipse.etrice.core.room.State;
import org.eclipse.etrice.core.room.StateGraph;
import org.eclipse.etrice.core.room.StateGraphItem;
import org.eclipse.etrice.core.room.TrPoint;
import org.eclipse.etrice.core.room.Transition;
import org.eclipse.etrice.core.room.TransitionPoint;
import org.eclipse.etrice.tests.base.TestBase;
import org.eclipse.etrice.ui.behavior.BehaviorTestActivator;
import org.eclipse.etrice.ui.behavior.support.StateSupport;
import org.eclipse.etrice.ui.behavior.support.TrPointSupport;
import org.eclipse.graphiti.mm.algorithms.Ellipse;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Rectangle;
import org.eclipse.graphiti.mm.algorithms.RoundedRectangle;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.services.Graphiti;

/**
 * base class for state machine tests
 * 
 * @author Henrik Rentz-Reichert - API and initial contribution
 */
public abstract class AbstractStateMachineTest extends TestBase {

	/**
	 * test general conditions for state graphs
	 * @param diagram the diagram
	 * @param sg the state graph to test
	 */
	protected void testStateGraph(Diagram diagram, StateGraph sg) {
		EObject bo = Graphiti.getLinkService().getBusinessObjectForLinkedPictogramElement(diagram);
		assertTrue("diagram bo is actor class", bo instanceof ActorClass);

		ActorClass ac = (ActorClass) bo;
		
		boolean hasInitialTransition = false;
		for (Transition trans : sg.getTransitions()) {
			if (trans instanceof InitialTransition) {
				hasInitialTransition = true;
				break;
			}
		}
		List<PictogramElement> elements = Graphiti.getLinkService().getPictogramElements(diagram, sg);
		assertEquals("PEs for our state graph: sg and initial point", hasInitialTransition? 2:1, elements.size());
		
		for (State s : sg.getStates()) {
			elements = Graphiti.getLinkService().getPictogramElements(diagram, s);
			assertEquals("PEs for our state", 1, elements.size());
			assertTrue("PE is shape", elements.get(0) instanceof Shape);
			checkStateGAs(ac, s, (Shape) elements.get(0));
		}
		
		for (ChoicePoint cp : sg.getChPoints()) {
			elements = Graphiti.getLinkService().getPictogramElements(diagram, cp);
			assertEquals("PEs for our cp", 1, elements.size());
			assertTrue("PE is shape", elements.get(0) instanceof Shape);
		}
		
		for (TrPoint tp : sg.getTrPoints()) {
			elements = Graphiti.getLinkService().getPictogramElements(diagram, tp);
			assertEquals("PEs for our tp", (tp instanceof TransitionPoint)? 1:2, elements.size());
			assertTrue("PE is shape", elements.get(0) instanceof Shape);
			checkTrpGAs(ac, tp, (Shape) elements.get(0));
		}
		
		for (Transition trans : sg.getTransitions()) {
			elements = Graphiti.getLinkService().getPictogramElements(diagram, trans);
			assertEquals("PEs for our transition", 1, elements.size());
		}
	}

	/**
	 * test general conditions for states
	 * 
	 * @param ac actor class
	 * @param s the state to test
	 * @param shape the state's shape
	 */
	private void checkStateGAs(ActorClass ac, State s, Shape shape) {
		assertNotNull("ga is there (invisible rectangle)", shape.getGraphicsAlgorithm());
		assertTrue("ga is rounded rectangle", shape.getGraphicsAlgorithm() instanceof Rectangle);
		assertFalse("ga is invisible", shape.getGraphicsAlgorithm().getFilled());
		assertFalse("ga is invisible", shape.getGraphicsAlgorithm().getLineVisible());
		assertEquals("border rect", 1, shape.getGraphicsAlgorithm().getGraphicsAlgorithmChildren().size());
		GraphicsAlgorithm borderRect = shape.getGraphicsAlgorithm().getGraphicsAlgorithmChildren().get(0);
		assertTrue("border rect is rounded rectangle", borderRect instanceof RoundedRectangle);
		if (isInherited(ac,s))
			assertTrue("border rect background", isEqual(borderRect.getForeground(), StateSupport.INHERITED_COLOR));
		else
			assertTrue("border rect background", isEqual(borderRect.getForeground(), StateSupport.LINE_COLOR));
	}

	/**
	 * test general conditions for transition points
	 * 
	 * @param ac actor class
	 * @param tp the transition point to test
	 * @param shape the state's shape
	 */
	private void checkTrpGAs(ActorClass ac, TrPoint tp, Shape shape) {
		assertNotNull("ga is there (invisible rectangle)", shape.getGraphicsAlgorithm());
		assertTrue("ga is ellipse", shape.getGraphicsAlgorithm() instanceof Rectangle);
		assertFalse("ga is invisible", shape.getGraphicsAlgorithm().getFilled());
		assertFalse("ga is invisible", shape.getGraphicsAlgorithm().getLineVisible());
		int nga = 1;
		if (tp instanceof EntryPoint)
			nga = 3;
		else if (tp instanceof ExitPoint)
			nga = 2;
		assertEquals("border rect", nga, shape.getGraphicsAlgorithm().getGraphicsAlgorithmChildren().size());
		GraphicsAlgorithm borderRect = shape.getGraphicsAlgorithm().getGraphicsAlgorithmChildren().get(0);
		assertTrue("border rect is rounded rectangle", borderRect instanceof Ellipse);
		if (isInherited(ac,tp))
			assertTrue("border rect background", isEqual(borderRect.getForeground(), TrPointSupport.INHERITED_COLOR));
		else
			assertTrue("border rect background", isEqual(borderRect.getForeground(), TrPointSupport.DARK_COLOR));
	}

	/**
	 * compute inheritance
	 * 
	 * @param ac actor class
	 * @param item state graph item
	 * @return true if state graph item is not owned by the given actor class
	 */
	private boolean isInherited(ActorClass ac, StateGraphItem item) {
		EObject owner = item.eContainer();
		while (owner!=null) {
			if (owner instanceof ActorClass)
				break;
			owner = owner.eContainer();
		}
		return ac!=owner;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.etrice.tests.base.TestBase#getModelsDirectoy()
	 */
	@Override
	protected URL getModelsDirectoy() {
		return BehaviorTestActivator.getDefault().getBundle().getEntry("models");
	}
}

Back to the top