Skip to main content
summaryrefslogtreecommitdiffstats
blob: 51eeb449754282f538fdcab78f7cdacc367bd911 (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
/*****************************************************************************
 * Copyright (c) 2012 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:
 *   CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.tests.bug;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequestFactory;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.papyrus.commands.ICreationCommand;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.ActionExecutionSpecificationEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.DurationConstraintEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.DurationConstraintLabelEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.LifelineEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.providers.UMLElementTypes;
import org.eclipse.papyrus.uml.diagram.sequence.tests.canonical.CreateSequenceDiagramCommand;
import org.eclipse.papyrus.uml.diagram.sequence.tests.canonical.TestTopNode;
import org.eclipse.papyrus.uml.tools.utils.ValueSpecificationUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.uml2.uml.ActionExecutionSpecification;
import org.eclipse.uml2.uml.DurationConstraint;
import org.eclipse.uml2.uml.Interval;
import org.eclipse.uml2.uml.OccurrenceSpecification;
import org.eclipse.uml2.uml.ValueSpecification;
import org.junit.Test;

/**
 * An embedded textuel editor shall allow editing duration constraints. This editor shall be accessible on the diagram.
 * When the two values of the interval are equal, the value shall be displayed only once.
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=384596
 * 
 */
public class TestDurationConstraints_384596 extends TestTopNode {

	private static final String SEP = "..";

	/** The occurrence specification(s) which are the nearest from a creation request */
	public static final String NEAREST_OCCURRENCE_SPECIFICATION = "Nearest occurrence specification"; //$NON-NLS-1$

	/** The occurrence specification(s) which are the nearest from a creation request (2nd end of creation) */
	public static final String NEAREST_OCCURRENCE_SPECIFICATION_2 = "Nearest occurrence specification (end)"; //$NON-NLS-1$

	private static final String EDIT = "Edit: ";

	protected ICreationCommand getDiagramCommandCreation() {
		return new CreateSequenceDiagramCommand();
	}

	@Test
	public void testDurationEdition() {
		createNode(UMLElementTypes.Lifeline_3001, getRootEditPart(), new Point(100, 100), new Dimension(62, 200), null);
		final LifelineEditPart lifeline1 = (LifelineEditPart)getRootEditPart().getChildren().get(0);
		waitForComplete();

		createNode(UMLElementTypes.ActionExecutionSpecification_3006, lifeline1, new Point(131, 200), new Dimension(20, 40), null);
		waitForComplete();

		ActionExecutionSpecificationEditPart esp = (ActionExecutionSpecificationEditPart)lifeline1.getChildren().get(1);
		createDuraitonConstraint(lifeline1, esp);

		DurationConstraintEditPart dp = (DurationConstraintEditPart)lifeline1.getChildren().get(2);

		// edit text
		WrappingLabel label = verifyEditText(dp, "1s", "2s");
		assertTrue(EDIT + TEST_THE_EXECUTION, label.getText().contains("1s..2s"));

		label = verifyEditText(dp, "d", "d+3");
		assertTrue(EDIT + TEST_THE_EXECUTION, label.getText().contains("d..d+3"));

		getEMFCommandStack().undo();
		assertTrue(EDIT + TEST_THE_UNDO, label.getText().contains("1s..2s"));

		label = verifyEditText(dp, "1", "1");
		assertTrue(EDIT + TEST_THE_EXECUTION, label.getText().contains("1") && !label.getText().contains(SEP));

	}

	protected WrappingLabel verifyEditText(DurationConstraintEditPart dp, String min, String max) {
		DurationConstraintLabelEditPart lp = (DurationConstraintLabelEditPart)dp.getChildren().get(0);
		WrappingLabel label = performEditRequest(lp);

		Composite composite = (Composite)lp.getViewer().getControl();
		Text editor = findEditor(composite, label);

		editor.setText(min + SEP + max);
		input(editor, SWT.CR);

		DurationConstraint constraint = (DurationConstraint)dp.resolveSemanticElement();
		ValueSpecification spec = constraint.getSpecification();
		assertTrue(EDIT + TEST_THE_EXECUTION, min.equals(ValueSpecificationUtil.getSpecificationValue(((Interval)spec).getMin())));
		assertTrue(EDIT + TEST_THE_EXECUTION, max.equals(ValueSpecificationUtil.getSpecificationValue(((Interval)spec).getMax())));

		waitForComplete();
		return label;
	}

	protected void createDuraitonConstraint(final LifelineEditPart lifeline1, ActionExecutionSpecificationEditPart esp) {
		ActionExecutionSpecification aes = (ActionExecutionSpecification)esp.resolveSemanticElement();

		Map<String, Object> extendedData = new HashMap<String, Object>();
		List<OccurrenceSpecification> events = new ArrayList<OccurrenceSpecification>(2);
		events.add(aes.getStart());
		extendedData.put(NEAREST_OCCURRENCE_SPECIFICATION, events);

		events = new ArrayList<OccurrenceSpecification>(2);
		events.add(aes.getFinish());
		extendedData.put(NEAREST_OCCURRENCE_SPECIFICATION_2, events);

		createNode(UMLElementTypes.DurationConstraint_3021, lifeline1, getAbsoluteBounds(esp).getBottom(), new Dimension(20, 40), extendedData);
		waitForComplete();
	}

	public void input(Widget widget, char... character) {
		if(widget.isDisposed())
			return;

		for(char c : character) {
			Event e = createKeyEvent(widget, 0, c);
			e.type = SWT.KeyDown;
			widget.notifyListeners(SWT.KeyDown, e);

			e = createKeyEvent(widget, 0, c);
			e.type = SWT.KeyUp;
			widget.notifyListeners(SWT.KeyUp, e);
		}
		waitForComplete();

		Event e = new Event();
		e.type = SWT.DefaultSelection;
		widget.notifyListeners(SWT.DefaultSelection, e);
	}

	private Event createKeyEvent(Widget widget, int keyCode, char character) {
		Event event = new Event();
		event.time = (int)System.currentTimeMillis();
		event.widget = widget;
		event.display = Display.getDefault();
		event.keyCode = keyCode;
		event.character = character;
		return event;
	}

	private Text findEditor(Composite composite, WrappingLabel label) {
		for(Control c : composite.getChildren()) {
			if(c instanceof Text) {
				if(label.getText().equals(((Text)c).getText()))
					return (Text)c;
			}
		}
		return null;
	}

	protected WrappingLabel performEditRequest(DurationConstraintLabelEditPart lp) {
		WrappingLabel label = (WrappingLabel)lp.getFigure();
		Rectangle b = label.getBounds().getCopy();
		label.translateToAbsolute(b);
		DirectEditRequest req = new DirectEditRequest();
		req.setLocation(b.getCenter());
		lp.performRequest(req);
		return label;
	}

	public void createNode(IElementType type, EditPart parentPart, Point location, Dimension size, Map<String, Object> extendedData) {
		//CREATION
		CreateViewRequest requestcreation = CreateViewRequestFactory.getCreateShapeRequest(type, getRootEditPart().getDiagramPreferencesHint());
		requestcreation.setLocation(location);
		requestcreation.setSize(size);
		if(extendedData != null)
			requestcreation.getExtendedData().putAll(extendedData);
		Command command = parentPart.getCommand(requestcreation);
		assertNotNull(CREATION + COMMAND_NULL, command);
		assertTrue(CREATION + TEST_IF_THE_COMMAND_IS_CREATED, command != UnexecutableCommand.INSTANCE);
		assertTrue(CREATION + TEST_IF_THE_COMMAND_CAN_BE_EXECUTED, command.canExecute() == true);

		getDiagramCommandStack().execute(command);
	}

}

Back to the top