Skip to content

Package: PreSetValidationListeners_PTest

PreSetValidationListeners_PTest

nameinstructionbranchcomplexitylinemethod
PreSetValidationListeners_PTest()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
assertError(VElement)
M: 0 C: 39
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
comboVerification()
M: 0 C: 37
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
comboVerification_noVElement()
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
createShell()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
destroyShell()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
domainModelChange()
M: 0 C: 62
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
lambda$0(InvocationOnMock)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$1(Map, InvocationOnMock)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$10(List, InvocationOnMock)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
lambda$11(List, InvocationOnMock)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
lambda$2(Map, InvocationOnMock)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$3(InvocationOnMock)
M: 1 C: 14
93%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$4(List, List, InvocationOnMock)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
lambda$8(List, InvocationOnMock)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$9(List, InvocationOnMock)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
mockContext()
M: 0 C: 99
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
mockValidation()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
multipleInstantiation()
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
singleInstantiation()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
textVerification()
M: 0 C: 38
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
textVerification_noVElement()
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 Christian W. Damus and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.edit.internal.swt.util;
15:
16: import static org.hamcrest.CoreMatchers.both;
17: import static org.hamcrest.CoreMatchers.instanceOf;
18: import static org.hamcrest.CoreMatchers.is;
19: import static org.hamcrest.CoreMatchers.not;
20: import static org.hamcrest.CoreMatchers.notNullValue;
21: import static org.hamcrest.CoreMatchers.sameInstance;
22: import static org.hamcrest.MatcherAssert.assertThat;
23: import static org.mockito.Matchers.any;
24: import static org.mockito.Matchers.anyString;
25: import static org.mockito.Mockito.doAnswer;
26: import static org.mockito.Mockito.mock;
27: import static org.mockito.Mockito.never;
28: import static org.mockito.Mockito.verify;
29: import static org.mockito.Mockito.when;
30:
31: import java.util.ArrayList;
32: import java.util.HashMap;
33: import java.util.List;
34: import java.util.Map;
35:
36: import org.eclipse.emf.common.util.BasicDiagnostic;
37: import org.eclipse.emf.common.util.Diagnostic;
38: import org.eclipse.emf.ecore.EStructuralFeature;
39: import org.eclipse.emf.ecore.EcoreFactory;
40: import org.eclipse.emf.ecore.EcorePackage;
41: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
42: import org.eclipse.emf.ecp.view.spi.model.VControl;
43: import org.eclipse.emf.ecp.view.spi.model.VDiagnostic;
44: import org.eclipse.emf.ecp.view.spi.model.VElement;
45: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
46: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
47: import org.eclipse.emfforms.spi.common.validation.PreSetValidationService;
48: import org.eclipse.emfforms.spi.core.services.view.EMFFormsContextListener;
49: import org.eclipse.emfforms.spi.core.services.view.RootDomainModelChangeListener;
50: import org.eclipse.swt.SWT;
51: import org.eclipse.swt.widgets.Combo;
52: import org.eclipse.swt.widgets.Shell;
53: import org.eclipse.swt.widgets.Text;
54: import org.eclipse.swtbot.swt.finder.SWTBot;
55: import org.junit.After;
56: import org.junit.Before;
57: import org.junit.Test;
58: import org.junit.runner.RunWith;
59: import org.mockito.Mock;
60: import org.mockito.runners.MockitoJUnitRunner;
61:
62: /**
63: * Tests for the {@link PreSetValidationListeners} class.
64: */
65: @SuppressWarnings("nls")
66: @RunWith(MockitoJUnitRunner.class)
67: public class PreSetValidationListeners_PTest {
68:
69:         @Mock
70:         private PreSetValidationService service;
71:
72:         private Shell shell;
73:         private SWTBot bot;
74:
75:         /**
76:          * Initializes me.
77:          */
78:         public PreSetValidationListeners_PTest() {
79:                 super();
80:         }
81:
82:         /**
83:          * Verify management of instances of the class: a single instance for any view-model context.
84:          */
85:         @Test
86:         public void singleInstantiation() {
87:                 final ViewModelContext ctx = mockContext();
88:                 final PreSetValidationListeners l1 = PreSetValidationListeners.create(ctx);
89:
90:                 final PreSetValidationListeners l2 = PreSetValidationListeners.create(ctx);
91:                 assertThat(l2, sameInstance(l1));
92:
93:                 final PreSetValidationListeners l3 = PreSetValidationListeners.create(ctx);
94:                 assertThat(l3, sameInstance(l1));
95:         }
96:
97:         /**
98:          * Verify management of instances of the class: a different instance for different view-model contexts.
99:          */
100:         @Test
101:         public void multipleInstantiation() {
102:                 final ViewModelContext ctx1 = mockContext();
103:                 final PreSetValidationListeners l1 = PreSetValidationListeners.create(ctx1);
104:
105:                 final ViewModelContext ctx2 = mockContext();
106:                 final PreSetValidationListeners l2 = PreSetValidationListeners.create(ctx2);
107:                 assertThat(l2, not(sameInstance(l1)));
108:
109:                 final ViewModelContext ctx3 = mockContext();
110:                 final PreSetValidationListeners l3 = PreSetValidationListeners.create(ctx3);
111:                 assertThat(l3, both(not(sameInstance(l1))).and(not(sameInstance(l2))));
112:         }
113:
114:         /**
115:          * Verify the support for verify listener on a text field.
116:          */
117:         @Test
118:         public void textVerification() {
119:                 final Text text = new Text(shell, SWT.BORDER);
120:
121:                 final VControl control = VViewFactory.eINSTANCE.createControl();
122:                 final EStructuralFeature feature = EcorePackage.Literals.ENAMED_ELEMENT__NAME;
123:                 PreSetValidationListeners.create(mockContext()).verify(text, feature, control);
124:                 SWTTestUtil.waitForUIThread();
125:
126:                 bot.text().setText("foo");
127:
128:                 verify(service).validate(feature, "foo");
129:                 assertError(control);
130:         }
131:
132:         /**
133:          * Verify the support for verify listener on a combo field.
134:          */
135:         @Test
136:         public void comboVerification() {
137:                 final Combo combo = new Combo(shell, SWT.DROP_DOWN);
138:
139:                 final VControl control = VViewFactory.eINSTANCE.createControl();
140:                 final EStructuralFeature feature = EcorePackage.Literals.ENAMED_ELEMENT__NAME;
141:                 PreSetValidationListeners.create(mockContext()).verify(combo, feature, control);
142:                 SWTTestUtil.waitForUIThread();
143:
144:                 bot.comboBox().setText("foo");
145:
146:                 verify(service).validate(feature, "foo");
147:                 assertError(control);
148:         }
149:
150:         /**
151:          * Verify the support for verify listener on a text field that is not a rendering of a view
152:          * model element.
153:          */
154:         @Test
155:         public void textVerification_noVElement() {
156:                 final Text text = new Text(shell, SWT.BORDER);
157:
158:                 final EStructuralFeature feature = EcorePackage.Literals.ENAMED_ELEMENT__NAME;
159:                 PreSetValidationListeners.create(mockContext()).verify(text, feature);
160:                 SWTTestUtil.waitForUIThread();
161:
162:                 bot.text().setText("foo");
163:
164:                 verify(service).validateLoose(feature, "foo");
165:         }
166:
167:         /**
168:          * Verify the support for verify listener on a combo field that is not a rendering of a view
169:          * model element.
170:          */
171:         @Test
172:         public void comboVerification_noVElement() {
173:                 final Combo combo = new Combo(shell, SWT.DROP_DOWN);
174:
175:                 final EStructuralFeature feature = EcorePackage.Literals.ENAMED_ELEMENT__NAME;
176:                 PreSetValidationListeners.create(mockContext()).verify(combo, feature);
177:                 SWTTestUtil.waitForUIThread();
178:
179:                 bot.comboBox().setText("foo");
180:
181:                 verify(service).validateLoose(feature, "foo");
182:         }
183:
184:         /**
185:          * Verify that the pre-set validation does not happen during root domain model change
186:          * in the view model context.
187:          */
188:         @Test
189:         public void domainModelChange() {
190:                 final Text text = new Text(shell, SWT.BORDER);
191:
192:                 final VControl control = VViewFactory.eINSTANCE.createControl();
193:                 final EStructuralFeature feature = EcorePackage.Literals.ENAMED_ELEMENT__NAME;
194:                 final ViewModelContext ctx = mockContext();
195:                 PreSetValidationListeners.create(ctx).verify(text, feature, control);
196:                 SWTTestUtil.waitForUIThread();
197:
198:                 // We should pause listening while the domain model is changed
199:
200:                 ctx.changeDomainModel(EcoreFactory.eINSTANCE.createEObject());
201:                 bot.text().setText("loaded from model");
202:                 SWTTestUtil.waitForUIThread();
203:
204:                 verify(service, never()).validate(any(), any());
205:
206:                 // After domain model change is processed, we resume listening
207:
208:                 bot.text().setText("foo");
209:                 SWTTestUtil.waitForUIThread();
210:
211:                 verify(service).validate(feature, "foo");
212:                 assertError(control);
213:         }
214:
215:         //
216:         // Test framework
217:         //
218:
219:         @Before
220:         public void createShell() {
221:                 shell = new Shell();
222:                 shell.open();
223:                 bot = new SWTBot(shell);
224:         }
225:
226:         @Before
227:         public void mockValidation() {
228:                 when(service.validateLoose(any(), any())).thenReturn(Diagnostic.OK_INSTANCE);
229:                 when(service.validate(any(), any()))
230:                         .then(invocation -> new BasicDiagnostic(Diagnostic.ERROR, "test", 0, "Test error", null));
231:         }
232:
233:         @After
234:         public void destroyShell() {
235:                 shell.dispose();
236:                 shell = null;
237:         }
238:
239:         ViewModelContext mockContext() {
240:                 final ViewModelContext result = mock(ViewModelContext.class);
241:
242:                 // Mock management of context values
243:                 final Map<String, Object> values = new HashMap<>();
244:                 when(result.getContextValue(anyString())).then(invocation -> values.get(invocation.getArguments()[0]));
245:                 doAnswer(invocation -> values.put((String) invocation.getArguments()[0], invocation.getArguments()[1]))
246:                         .when(result).putContextValue(anyString(), any());
247:
248:                 // Mock access to certain critical services
249:                 when(result.getService(PreSetValidationService.class)).thenReturn(service);
250:•                when(result.hasService(any())).thenAnswer(invocation -> ((ViewModelContext) invocation.getMock())
251:                         .getService((Class<?>) invocation.getArguments()[0]) != null);
252:
253:                 // Mock support for root domain model change
254:                 final List<EMFFormsContextListener> listeners = new ArrayList<>();
255:                 final List<RootDomainModelChangeListener> domainModelListeners = new ArrayList<>();
256:                 doAnswer(invocation -> {
257:                         listeners.forEach(EMFFormsContextListener::contextDispose);
258:                         domainModelListeners.forEach(RootDomainModelChangeListener::notifyChange);
259:                         listeners.forEach(EMFFormsContextListener::contextInitialised);
260:                         return null;
261:                 }).when(result).changeDomainModel(any());
262:                 doAnswer(invocation -> {
263:                         listeners.add((EMFFormsContextListener) invocation.getArguments()[0]);
264:                         return null;
265:                 }).when(result).registerEMFFormsContextListener(any());
266:                 doAnswer(invocation -> {
267:                         listeners.remove(invocation.getArguments()[0]);
268:                         return null;
269:                 }).when(result).unregisterEMFFormsContextListener(any());
270:                 doAnswer(invocation -> {
271:                         domainModelListeners.add((RootDomainModelChangeListener) invocation.getArguments()[0]);
272:                         return null;
273:                 }).when(result).registerRootDomainModelChangeListener(any());
274:                 doAnswer(invocation -> {
275:                         domainModelListeners.remove(invocation.getArguments()[0]);
276:                         return null;
277:                 }).when(result).unregisterRootDomainModelChangeListener(any());
278:
279:                 return result;
280:         }
281:
282:         void assertError(VElement viewModel) {
283:                 final VDiagnostic vDiagnostic = viewModel.getDiagnostic();
284:                 assertThat("No diagnostic attached to view model", vDiagnostic, notNullValue());
285:                 assertThat("No diagnostics", vDiagnostic.getDiagnostics().isEmpty(), is(false));
286:
287:                 final Object first = vDiagnostic.getDiagnostics().get(0);
288:                 assertThat("Not a diagnostic", first, instanceOf(Diagnostic.class));
289:                 assertThat("Not an error", ((Diagnostic) vDiagnostic.getDiagnostics().get(0)).getSeverity(),
290:                         is(Diagnostic.ERROR));
291:         }
292:
293: }