Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 56fca716098dd7640312cc7411bfff1850232ef7 (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
/*******************************************************************************
 * Copyright (c) 2015, 2016 Obeo.
 * 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.eef.tests.internal.controllers;

import java.util.Map;

import org.eclipse.eef.EEFTextDescription;
import org.eclipse.eef.EefFactory;
import org.eclipse.eef.core.api.EEFExpressionUtils;
import org.eclipse.eef.core.api.controllers.IEEFTextController;
import org.eclipse.eef.core.internal.controllers.EEFTextController;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl;
import org.eclipse.sirius.common.interpreter.api.EvaluationResult;
import org.eclipse.sirius.common.interpreter.api.IInterpreter;
import org.eclipse.sirius.common.interpreter.api.IVariableManager;
import org.eclipse.sirius.common.interpreter.api.VariableManagerFactory;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

/**
 * Unit tests for the {@link IEEFTextController}.
 *
 * @author sbegaudeau
 */
@SuppressWarnings({ "checkstyle:javadocmethod" })
public class EEFTextControllerTests {
	/**
	 * The editing domain.
	 */
	private TransactionalEditingDomainImpl editingDomain;

	/**
	 * The description.
	 */
	private EEFTextDescription description;

	/**
	 * The variable manager.
	 */
	private IVariableManager variableManager;

	/**
	 * The resource set.
	 */
	private ResourceSetImpl resourceSet;

	@Before
	public void setUp() {
		AdapterFactory adapterFactory = new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
		this.resourceSet = new ResourceSetImpl();
		this.resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
		.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
		this.editingDomain = new TransactionalEditingDomainImpl(adapterFactory, this.resourceSet);
		this.variableManager = new VariableManagerFactory().createVariableManager();

		this.description = EefFactory.eINSTANCE.createEEFTextDescription();

		this.description.setLabelExpression("aql:'General'"); //$NON-NLS-1$
		this.description.setValueExpression("aql:self.name"); //$NON-NLS-1$
		this.description.setEditExpression("aql.self.eSet('name', newValue)"); //$NON-NLS-1$
	}

	@Test
	public void testUpdateValue() {
		final String newValue = "newValue"; //$NON-NLS-1$

		IInterpreter interpreter = (Map<String, Object> variables, String expressionBody) -> {
			return EvaluationResult.of(newValue);
		};

		IEEFTextController controller = new EEFTextController(description, variableManager, interpreter, editingDomain);
		controller.onNewValue((text) -> {
			assertThat(text, equalTo(newValue));
		});

		controller.onNewLabel(newLabel -> {
			// do nothing
		});

		controller.onNewHelp(newHelp -> {
			// nothing
		});

		controller.onValidation(result -> {
			// nothing
		});

		controller.refresh();
	}

	@Test
	public void testUpdateValueWithEObject() {
		String name = "TestEClass"; //$NON-NLS-1$

		EClass eClass = EcoreFactory.eINSTANCE.createEClass();
		eClass.setName(name);

		Resource resource = this.resourceSet.createResource(URI.createURI("test.ecore")); //$NON-NLS-1$
		Command command = new RecordingCommand(this.editingDomain) {
			@Override
			protected void doExecute() {
				resource.getContents().add(eClass);
			}
		};

		CommandStack commandStack = this.editingDomain.getCommandStack();
		commandStack.execute(command);

		this.variableManager.getVariables().put(EEFExpressionUtils.SELF, eClass);
	}

	@Test
	public void testUpdateLabel() {
		final String newLabel = "Label"; //$NON-NLS-1$

		IInterpreter interpreter = (Map<String, Object> variables, String expressionBody) -> {
			return EvaluationResult.of(newLabel);
		};

		IEEFTextController controller = new EEFTextController(description, variableManager, interpreter, editingDomain);
		controller.onNewLabel((label) -> {
			assertThat(label, equalTo(newLabel));
		});

		controller.onNewValue(newValue -> {
			// nothing
		});

		controller.onNewHelp(newHelp -> {
			// nothing
		});

		controller.onValidation(result -> {
			// nothing
		});

		controller.refresh();
	}
}

Back to the top