Skip to content

Package: EnumComboViewerRenderer_PTest

EnumComboViewerRenderer_PTest

nameinstructionbranchcomplexitylinemethod
EnumComboViewerRenderer_PTest()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
availableEnumValues()
M: 0 C: 40
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
before()
M: 0 C: 75
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 18
100%
M: 0 C: 1
100%
lambda$0()
M: 3 C: 105
97%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 1 C: 20
95%
M: 0 C: 1
100%
lambda$1(TestEnum)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$2(List, Throwable)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
mockControl()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setUpDatabindingTest(IObservableValue)
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
sneakyThrow(Exception)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
testDatabindingServiceUsageChangeControl()
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
testDatabindingServiceUsageChangeObservable()
M: 0 C: 55
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
testDatabindingServiceUsageInitialBinding()
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%
testEffectivelyReadOnlyDeactivatesControl()
M: 0 C: 62
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
testLabelServiceUsage()
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%
testRootDomainModelChanged()
M: 0 C: 80
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
testTearDown()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
updateUIFromModelChange()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH 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: * Lucas Koehler - initial API and implementation
13: * Christian W. Damus - bug 547422
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.internal.core.swt.renderer;
16:
17: import static org.hamcrest.CoreMatchers.anything;
18: import static org.hamcrest.CoreMatchers.hasItem;
19: import static org.hamcrest.CoreMatchers.not;
20: import static org.junit.Assert.assertArrayEquals;
21: import static org.junit.Assert.assertEquals;
22: import static org.junit.Assert.assertFalse;
23: import static org.junit.Assert.assertSame;
24: import static org.junit.Assert.assertThat;
25: import static org.mockito.Matchers.any;
26: import static org.mockito.Mockito.mock;
27: import static org.mockito.Mockito.times;
28: import static org.mockito.Mockito.verify;
29: import static org.mockito.Mockito.when;
30:
31: import java.util.ArrayList;
32: import java.util.Collection;
33: import java.util.Iterator;
34: import java.util.List;
35: import java.util.concurrent.CompletableFuture;
36:
37: import org.eclipse.core.databinding.observable.IChangeListener;
38: import org.eclipse.core.databinding.observable.Realm;
39: import org.eclipse.core.databinding.observable.value.IObservableValue;
40: import org.eclipse.core.databinding.property.Properties;
41: import org.eclipse.emf.databinding.EMFProperties;
42: import org.eclipse.emf.databinding.edit.EMFEditProperties;
43: import org.eclipse.emf.ecore.EObject;
44: import org.eclipse.emf.ecore.EStructuralFeature;
45: import org.eclipse.emf.ecp.test.common.DefaultRealm;
46: import org.eclipse.emf.ecp.view.core.swt.test.model.SimpleTestObject;
47: import org.eclipse.emf.ecp.view.core.swt.test.model.TestEnum;
48: import org.eclipse.emf.ecp.view.core.swt.test.model.TestFactory;
49: import org.eclipse.emf.ecp.view.core.swt.test.model.TestPackage;
50: import org.eclipse.emf.ecp.view.spi.model.VControl;
51: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
52: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
53: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
54: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
55: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
56: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
57: import org.eclipse.emf.edit.domain.EditingDomain;
58: import org.eclipse.emf.emfstore.bowling.BowlingFactory;
59: import org.eclipse.emf.emfstore.bowling.BowlingPackage;
60: import org.eclipse.emf.emfstore.bowling.Gender;
61: import org.eclipse.emf.emfstore.bowling.Player;
62: import org.eclipse.emfforms.spi.common.report.ReportService;
63: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
64: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
65: import org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport;
66: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
67: import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
68: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
69: import org.eclipse.emfforms.swt.common.test.AbstractControl_PTest;
70: import org.eclipse.jface.databinding.swt.DisplayRealm;
71: import org.eclipse.swt.SWT;
72: import org.eclipse.swt.widgets.Combo;
73: import org.eclipse.swt.widgets.Control;
74: import org.eclipse.swt.widgets.Display;
75: import org.eclipse.swt.widgets.Event;
76: import org.junit.After;
77: import org.junit.Before;
78: import org.junit.Test;
79: import org.mockito.Matchers;
80:
81: /**
82: * Plugin test for {@link EnumComboViewerSWTRenderer}.
83: *
84: * @author Lucas Koehler
85: *
86: */
87: @SuppressWarnings({ "rawtypes", "unchecked" })
88: public class EnumComboViewerRenderer_PTest extends AbstractControl_PTest<VControl> {
89:
90:         private EMFFormsEditSupport editSupport;
91:         private DefaultRealm realm;
92:         private SimpleTestObject domainObject;
93:         private IObservableValue observableValue;
94:
95:         @Before
96:         public void before() throws DatabindingFailedException {
97:                 realm = new DefaultRealm();
98:                 final ReportService reportService = mock(ReportService.class);
99:                 setDatabindingService(mock(EMFFormsDatabinding.class));
100:                 setLabelProvider(mock(EMFFormsLabelProvider.class));
101:                 setTemplateProvider(mock(VTViewTemplateProvider.class));
102:                 editSupport = mock(EMFFormsEditSupport.class);
103:
104:                 domainObject = TestFactory.eINSTANCE.createSimpleTestObject();
105:                 domainObject.setInner(TestFactory.eINSTANCE.createInnerObject());
106:
107:                 setup();
108:
109:                 final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(domainObject);
110:                 observableValue = EMFEditProperties
111:                         .value(editingDomain, TestPackage.Literals.INNER_OBJECT__MY_ENUM)
112:                         .observe(domainObject.getInner());
113:
114:                 setRenderer(new EnumComboViewerSWTRenderer(getvControl(), getContext(), reportService, getDatabindingService(),
115:                         getLabelProvider(),
116:                         getTemplateProvider(), editSupport));
117:                 getRenderer().init();
118:         }
119:
120:         @After
121:         public void testTearDown() {
122:                 realm.dispose();
123:                 dispose();
124:         }
125:
126:         @Override
127:         protected void mockControl() throws DatabindingFailedException {
128:                 super.mockControl(domainObject, TestPackage.eINSTANCE.getInnerObject_MyEnum());
129:         }
130:
131:         @Test
132:         public void testDatabindingServiceUsageInitialBinding() throws NoRendererFoundException,
133:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
134:                 final TestEnum initialValue = TestEnum.C;
135:                 observableValue.setValue(initialValue);
136:
137:                 when(
138:                         editSupport.getText(any(VDomainModelReference.class), any(EObject.class),
139:                                 Matchers.eq(observableValue.getValue())))
140:                                         .thenReturn(observableValue.getValue().toString());
141:
142:                 final Combo combo = setUpDatabindingTest(observableValue);
143:                 assertEquals(initialValue.getName(), combo.getText());
144:
145:         }
146:
147:         @Test
148:         public void testDatabindingServiceUsageChangeObservable() throws NoRendererFoundException,
149:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
150:                 final TestEnum initialValue = TestEnum.B;
151:                 final TestEnum changedValue = TestEnum.C;
152:                 observableValue.setValue(initialValue);
153:
154:                 when(
155:                         editSupport.getText(any(VDomainModelReference.class), any(EObject.class),
156:                                 Matchers.same(initialValue)))
157:                                         .thenReturn(initialValue.toString());
158:
159:                 when(
160:                         editSupport.getText(any(VDomainModelReference.class), any(EObject.class),
161:                                 Matchers.same(changedValue)))
162:                                         .thenReturn(changedValue.toString());
163:
164:                 final Combo combo = setUpDatabindingTest(observableValue);
165:                 observableValue.setValue(changedValue);
166:
167:                 assertEquals(changedValue.getName(), combo.getText());
168:
169:         }
170:
171:         @Test
172:         public void testDatabindingServiceUsageChangeControl() throws NoRendererFoundException,
173:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
174:                 final TestEnum changedValue = TestEnum.C;
175:
176:                 domainObject.getInner().setMyEnum(TestEnum.B);
177:                 final Combo combo = setUpDatabindingTest(observableValue);
178:                 combo.select(1); // TestEnum.C
179:                 combo.notifyListeners(SWT.Selection, new Event());
180:                 SWTTestUtil.waitForUIThread();
181:
182:                 assertEquals(changedValue.getName(), ((TestEnum) observableValue.getValue()).getName());
183:
184:         }
185:
186:         /**
187:          * Universal set up stuff for the data binding test cases.
188:          *
189:          * @param mockedObservable
190:          * @return
191:          * @throws NoRendererFoundException
192:          * @throws NoPropertyDescriptorFoundExeption
193:          * @throws DatabindingFailedException
194:          */
195:         private Combo setUpDatabindingTest(final IObservableValue<?> mockedObservable) throws NoRendererFoundException,
196:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
197:                 mockDatabindingIsSettableAndChangeable();
198:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
199:                         .thenReturn(
200:                                 mockedObservable);
201:                 when(getDatabindingService().getValueProperty(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
202:                         EMFProperties.value((EStructuralFeature) mockedObservable.getValueType()));
203:
204:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
205:
206:                 final Combo combo = (Combo) renderControl;
207:                 return combo;
208:         }
209:
210:         /**
211:          * Tests whether the {@link EMFFormsLabelProvider} is used to get the labels of the control.
212:          *
213:          * @throws NoRendererFoundException
214:          * @throws NoPropertyDescriptorFoundExeption
215:          * @throws DatabindingFailedException
216:          * @throws NoLabelFoundException
217:          */
218:         @Test
219:         public void testLabelServiceUsage() throws NoRendererFoundException, NoPropertyDescriptorFoundExeption,
220:                 DatabindingFailedException, NoLabelFoundException {
221:                 labelServiceUsage();
222:         }
223:
224:         @Test
225:         public void testEffectivelyReadOnlyDeactivatesControl()
226:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption, DatabindingFailedException {
227:                 observableValue.setValue(TestEnum.B);
228:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
229:                         .thenReturn(observableValue);
230:                 when(getDatabindingService().getValueProperty(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
231:                         Properties.selfValue(observableValue.getValueType()));
232:
233:                 when(getvControl().isEffectivelyReadonly()).thenReturn(true);
234:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
235:                 getRenderer().finalizeRendering(getShell());
236:                 assertFalse(renderControl.isEnabled());
237:         }
238:
239:         /**
240:          * Tests that the renderer only shows enum literals which fulfill both conditions:
241:          * <ul>
242:          * <li>they are returned by the item descriptor</li>
243:          * <li>they are not filtered out by the custom isInputtable annotation</li>
244:          * </ul>
245:          *
246:          * @throws DatabindingFailedException
247:          */
248:         @Test
249:         public void availableEnumValues() throws DatabindingFailedException {
250:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
251:                         .thenReturn(observableValue);
252:                 final EnumComboViewerSWTRenderer enumRenderer = (EnumComboViewerSWTRenderer) getRenderer();
253:                 final Collection<?> choices = enumRenderer.getAvailableChoicesValue().getValue();
254:
255:                 // TestEnum.A is filtered by annotation and TestEnum.D is filtered by the property descriptor
256:                 assertEquals(2, choices.size());
257:                 final Iterator<?> iterator = choices.iterator();
258:                 assertSame(TestEnum.B, iterator.next());
259:                 assertSame(TestEnum.C, iterator.next());
260:         }
261:
262:         /**
263:          * Verify that the renderer correctly updates the UI when the model is updated directly
264:          * as by updating with change sets from an EMFStore repository.
265:          */
266:         @SuppressWarnings("nls")
267:         @Test
268:         public void updateUIFromModelChange() {
269:                 // Have to run this test in a real SWT Display realm because the DefaultRealm for testing
270:                 // doesn't enforce the thread on which things happen in the observables
271:                 final Realm realRealm = DisplayRealm.getRealm(Display.getCurrent());
272:                 Realm.runWithDefault(realRealm, () -> {
273:                         try {
274:                                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
275:                                         .thenReturn(observableValue);
276:
277:                                 // A is filtered by annotation and D is filtered by the property descriptor,
278:                                 // so we can only use B and C for testing
279:                                 domainObject.getInner().setMyEnum(TestEnum.B);
280:                                 renderControl(new SWTGridCell(0, 2, getRenderer()));
281:
282:                                 final EnumComboViewerSWTRenderer enumRenderer = (EnumComboViewerSWTRenderer) getRenderer();
283:                                 final IObservableValue<?> availableChoices = enumRenderer.getAvailableChoicesValue();
284:
285:                                 final IChangeListener listener = mock(IChangeListener.class);
286:                                 availableChoices.addChangeListener(listener);
287:
288:                                 // Make changes to the model on background threads and verify that
289:                                 // the observable machinery works correctly
290:                                 final TestEnum[] valuesToSet = { TestEnum.C, TestEnum.B };
291:                                 final List<Throwable> thrown = new ArrayList<>(valuesToSet.length);
292:•                                for (final TestEnum valueToSet : valuesToSet) {
293:                                         final CompletableFuture<?> asyncUpdate = CompletableFuture.runAsync(
294:                                                 () -> domainObject.getInner().setMyEnum(valueToSet))
295:                                                 .exceptionally(x -> {
296:                                                         thrown.add(x);
297:                                                         return null;
298:                                                 });
299:
300:                                         do {
301:                                                 SWTTestUtil.waitForUIThread();
302:•                                        } while (!asyncUpdate.isDone());
303:                                 }
304:
305:                                 assertThat("Async update failed", thrown, not(hasItem(anything())));
306:
307:                                 // Got notified once for each async update
308:                                 verify(listener, times(valuesToSet.length)).handleChange(any());
309:                         } catch (DatabindingFailedException | NoRendererFoundException | NoPropertyDescriptorFoundExeption e) {
310:                                 sneakyThrow(e);
311:                         }
312:                 });
313:         }
314:
315:         private static <X extends Exception> void sneakyThrow(Exception x) throws X {
316:                 throw (X) x;
317:         }
318:
319:         @Test
320:         public void testRootDomainModelChanged()
321:                 throws DatabindingFailedException, NoRendererFoundException, NoPropertyDescriptorFoundExeption {
322:                 // assert we have the old values
323:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
324:                         .thenReturn(observableValue);
325:                 renderControl(new SWTGridCell(0, 2, getRenderer()));
326:                 final EnumComboViewerSWTRenderer enumRenderer = (EnumComboViewerSWTRenderer) getRenderer();
327:                 assertArrayEquals(new Object[] { TestEnum.B, TestEnum.C },
328:                         enumRenderer.getAvailableChoicesValue().getValue().toArray());
329:
330:                 // prepare a new domain model
331:                 final Player player = BowlingFactory.eINSTANCE.createPlayer();
332:                 final EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(player);
333:                 final IObservableValue playerGenderObservable = EMFEditProperties
334:                         .value(editingDomain, BowlingPackage.Literals.PLAYER__GENDER)
335:                         .observe(player);
336:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
337:                         .thenReturn(playerGenderObservable);
338:                 // this triggers the reevaluation of the modelObservable
339:                 enumRenderer.notifyChange();
340:                 // we should now have new values
341:                 assertArrayEquals(Gender.VALUES.toArray(), enumRenderer.getAvailableChoicesValue().getValue().toArray());
342:         }
343:
344: }