Skip to content

Package: SWTTableDatabindingLabel_PTest

SWTTableDatabindingLabel_PTest

nameinstructionbranchcomplexitylinemethod
SWTTableDatabindingLabel_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%
assertDatabinding(WritableList, Table)
M: 0 C: 28
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createInitialList()
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
createTableDomainModelReference(EStructuralFeature)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
getTable(Control)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 183
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 35
100%
M: 0 C: 1
100%
setUpDatabindingTests(IObservableList, EClass, EClass)
M: 0 C: 108
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
testCreateLabelStyleBits()
M: 0 C: 61
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
testDatabindingServiceUsageAddToModel()
M: 0 C: 49
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
testDatabindingServiceUsageChangeModel()
M: 0 C: 45
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testDatabindingServiceUsageInitialBinding()
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%
testDatabindingServiceUsageRemoveFromModel()
M: 0 C: 43
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testLabelServiceUsage()
M: 0 C: 165
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 29
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.table.swt;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertNotEquals;
18: import static org.junit.Assert.assertTrue;
19: import static org.mockito.Matchers.any;
20: import static org.mockito.Matchers.same;
21: import static org.mockito.Mockito.mock;
22: import static org.mockito.Mockito.when;
23:
24: import java.util.LinkedHashSet;
25: import java.util.LinkedList;
26: import java.util.List;
27: import java.util.Set;
28:
29: import org.eclipse.core.databinding.observable.Observables;
30: import org.eclipse.core.databinding.observable.list.IObservableList;
31: import org.eclipse.core.databinding.observable.list.WritableList;
32: import org.eclipse.core.databinding.observable.value.IObservableValue;
33: import org.eclipse.core.databinding.property.value.IValueProperty;
34: import org.eclipse.emf.databinding.IEMFValueProperty;
35: import org.eclipse.emf.databinding.internal.EMFValueProperty;
36: import org.eclipse.emf.databinding.internal.EMFValuePropertyDecorator;
37: import org.eclipse.emf.ecore.EClass;
38: import org.eclipse.emf.ecore.EObject;
39: import org.eclipse.emf.ecore.EReference;
40: import org.eclipse.emf.ecore.EStructuralFeature;
41: import org.eclipse.emf.ecore.EcoreFactory;
42: import org.eclipse.emf.ecore.EcorePackage;
43: import org.eclipse.emf.ecore.InternalEObject;
44: import org.eclipse.emf.ecp.view.internal.context.ViewModelContextImpl;
45: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
46: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
47: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
48: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
49: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
50: import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
51: import org.eclipse.emf.ecp.view.spi.table.model.VTableDomainModelReference;
52: import org.eclipse.emf.ecp.view.spi.table.model.VTableFactory;
53: import org.eclipse.emf.ecp.view.spi.util.swt.ImageRegistryService;
54: import org.eclipse.emf.ecp.view.template.model.VTStyleProperty;
55: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
56: import org.eclipse.emf.ecp.view.template.style.alignment.model.AlignmentType;
57: import org.eclipse.emf.ecp.view.template.style.alignment.model.VTAlignmentFactory;
58: import org.eclipse.emf.ecp.view.template.style.alignment.model.VTControlLabelAlignmentStyleProperty;
59: import org.eclipse.emf.ecp.view.template.style.wrap.model.VTLabelWrapStyleProperty;
60: import org.eclipse.emf.ecp.view.template.style.wrap.model.VTWrapFactory;
61: import org.eclipse.emf.ecp.view.test.common.swt.spi.DatabindingClassRunner;
62: import org.eclipse.emf.edit.domain.EditingDomain;
63: import org.eclipse.emfforms.spi.common.report.ReportService;
64: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
65: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
66: import org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport;
67: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
68: import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
69: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
70: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
71: import org.eclipse.swt.SWT;
72: import org.eclipse.swt.widgets.Composite;
73: import org.eclipse.swt.widgets.Control;
74: import org.eclipse.swt.widgets.Label;
75: import org.eclipse.swt.widgets.Shell;
76: import org.eclipse.swt.widgets.Table;
77: import org.eclipse.swt.widgets.TableColumn;
78: import org.junit.Before;
79: import org.junit.Test;
80: import org.junit.runner.RunWith;
81: import org.mockito.Matchers;
82: import org.mockito.Mockito;
83:
84: /**
85: * JUnit tests for {@link TableControlSWTRenderer} testing the data binding of the table.
86: *
87: * @author Lucas Koehler
88: *
89: */
90: @SuppressWarnings("restriction")
91: @RunWith(DatabindingClassRunner.class)
92: public class SWTTableDatabindingLabel_PTest {
93:         private static final IObservableValue DISPLAYNAME = Observables.constantObservableValue("displayname");
94:         private static final IObservableValue DISPLAYNAME_COLUMNS = Observables
95:                 .constantObservableValue("displayname-columns");
96:         private static final IObservableValue DESCRIPTION = Observables.constantObservableValue("description");
97:         private static final IObservableValue DESCRIPTION_COLUMNS = Observables
98:                 .constantObservableValue("description-columns");
99:         private EMFFormsDatabindingEMF databindingService;
100:         private TableControlSWTRenderer renderer;
101:         private Shell shell;
102:         private EClass domainModel;
103:         private VTableControl vTableControl;
104:         private EMFFormsLabelProvider labelProvider;
105:         private VTViewTemplateProvider vtViewTemplateProvider;
106:         private ViewModelContextImpl viewContext;
107:
108:         /**
109:          * Set up executed before every test.
110:          * Mocks and registers the databinding and label service.
111:          * Creates a new {@link TableControlSWTRenderer} to be tested. Mocks needed parameters and contents (e.g.
112:          * VControl, ViewModelContext).
113:          *
114:          * @throws DatabindingFailedException
115:          * @throws NoLabelFoundException
116:          */
117:         @Before
118:         public void setUp() throws DatabindingFailedException, NoLabelFoundException {
119:                 databindingService = mock(EMFFormsDatabindingEMF.class);
120:                 labelProvider = mock(EMFFormsLabelProvider.class);
121:
122:                 when(labelProvider.getDescription(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
123:                         DESCRIPTION);
124:                 when(labelProvider.getDescription(any(VDomainModelReference.class), any(EClass.class)))
125:                         .thenReturn(DESCRIPTION_COLUMNS);
126:                 when(labelProvider.getDisplayName(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
127:                         DISPLAYNAME);
128:                 when(labelProvider.getDisplayName(any(VDomainModelReference.class), any(EClass.class)))
129:                         .thenReturn(DISPLAYNAME_COLUMNS);
130:
131:                 shell = new Shell();
132:
133:                 domainModel = EcoreFactory.eINSTANCE.createEClass();
134:                 final EStructuralFeature eStructuralFeature = EcorePackage.eINSTANCE.getEClass_ESuperTypes();
135:
136:                 final VTableDomainModelReference tableDomainModelReference = createTableDomainModelReference(
137:                         eStructuralFeature);
138:                 vTableControl = VTableFactory.eINSTANCE.createTableControl();
139:                 vTableControl.setDomainModelReference(tableDomainModelReference);
140:
141:                 final IValueProperty delegate = new EMFValueProperty(EcorePackage.eINSTANCE.getEClass_ESuperTypes());
142:                 final EMFValuePropertyDecorator valueProperty = new EMFValuePropertyDecorator(delegate,
143:                         EcorePackage.eINSTANCE.getEClass_ESuperTypes());
144:                 final IObservableValue observableValue = valueProperty.observe(domainModel);
145:                 when(databindingService.getValueProperty(vTableControl.getDomainModelReference(), (EObject) domainModel))
146:                         .thenReturn(valueProperty);
147:                 when(databindingService.getObservableValue(vTableControl.getDomainModelReference(), domainModel)).thenReturn(
148:                         observableValue);
149:
150:                 final ReportService reportservice = mock(ReportService.class);
151:                 vtViewTemplateProvider = mock(VTViewTemplateProvider.class);
152:                 final ImageRegistryService imageRegistryService = mock(ImageRegistryService.class);
153:                 final EMFFormsEditSupport emfFormsEditSupport = mock(EMFFormsEditSupport.class);
154:                 viewContext = new ViewModelContextImpl(vTableControl, domainModel);
155:
156:                 renderer = new TableControlSWTRenderer(vTableControl, viewContext, reportservice, databindingService,
157:                         labelProvider, vtViewTemplateProvider, imageRegistryService, emfFormsEditSupport);
158:                 renderer.init();
159:                 renderer.getGridDescription(new SWTGridDescription());
160:         }
161:
162:         private VTableDomainModelReference createTableDomainModelReference(EStructuralFeature eStructuralFeature) {
163:                 final VTableDomainModelReference tableDomainModelReference = VTableFactory.eINSTANCE
164:                         .createTableDomainModelReference();
165:                 tableDomainModelReference.setDomainModelEFeature(eStructuralFeature);
166:
167:                 final VFeaturePathDomainModelReference columnReference1 = VViewFactory.eINSTANCE
168:                         .createFeaturePathDomainModelReference();
169:                 columnReference1.setDomainModelEFeature(EcorePackage.eINSTANCE.getEClass_Abstract());
170:
171:                 tableDomainModelReference.getColumnDomainModelReferences().add(columnReference1);
172:
173:                 return tableDomainModelReference;
174:         }
175:
176:         @Test
177:         public void testLabelServiceUsage() throws NoRendererFoundException, NoPropertyDescriptorFoundExeption,
178:                 DatabindingFailedException {
179:                 final IEMFValueProperty columnValueProperty = new EMFValuePropertyDecorator(
180:                         new EMFValueProperty(EcorePackage.eINSTANCE.getEClass_Abstract()),
181:                         EcorePackage.eINSTANCE.getEClass_Abstract());
182:                 final VDomainModelReference columnDMR = ((VTableDomainModelReference) vTableControl.getDomainModelReference())
183:                         .getColumnDomainModelReferences().get(0);
184:                 when(databindingService.getValueProperty(same(columnDMR), same(domainModel.eClass()), any(EditingDomain.class)))
185:                         .thenReturn(columnValueProperty);
186:                 when(databindingService.getValueProperty(columnDMR, (EObject) domainModel)).thenReturn(columnValueProperty);
187:
188:                 final EReference eReference = mock(EReference.class);
189:                 final EClass eClass = EcorePackage.eINSTANCE.getEClass();
190:                 when(eReference.getEReferenceType()).thenReturn(eClass);
191:
192:                 final WritableList mockedObservableList = new WritableList(createInitialList(), eReference);
193:
194:                 when(databindingService.getObservableList(Matchers.eq(vTableControl.getDomainModelReference()),
195:                         any(EObject.class)))
196:                                 .thenReturn(mockedObservableList);
197:                 when(databindingService.getSetting(vTableControl.getDomainModelReference(), domainModel)).thenReturn(
198:                         InternalEObject.class.cast(domainModel).eSetting(EcorePackage.eINSTANCE.getEClass_ESuperTypes()));
199:
200:                 final Composite renderedControl = (Composite) renderer.render(new SWTGridCell(0, 0, renderer), shell);
201:                 final Composite composite = (Composite) renderedControl.getChildren()[0];
202:                 final Composite titleComposite = (Composite) composite.getChildren()[0];
203:                 final Label titleLabel = (Label) titleComposite.getChildren()[0];
204:
205:                 assertEquals(DISPLAYNAME.getValue(), titleLabel.getText());
206:
207:                 final Control tableControl = getTable(renderedControl);
208:                 assertTrue(Table.class.isInstance(tableControl));
209:                 final Table table = (Table) tableControl;
210:                 final TableColumn column = table.getColumn(1);
211:                 assertEquals(DISPLAYNAME_COLUMNS.getValue(), column.getText());
212:                 assertEquals(DESCRIPTION_COLUMNS.getValue(), column.getToolTipText());
213:         }
214:
215:         /**
216:          * Tests the initial binding from the model to the table.
217:          *
218:          * @throws NoRendererFoundException Renderer could not be found
219:          * @throws NoPropertyDescriptorFoundExeption Property descriptor could not be found
220:          * @throws DatabindingFailedException if the databinidng failed
221:          */
222:         @Test
223:         public void testDatabindingServiceUsageInitialBinding() throws NoRendererFoundException,
224:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
225:                 final List<EClass> initialList = createInitialList();
226:
227:                 final EReference eReference = mock(EReference.class);
228:                 final EClass eClass = EcorePackage.eINSTANCE.getEClass();
229:                 when(eReference.getEReferenceType()).thenReturn(eClass);
230:
231:                 final WritableList mockedObservableList = new WritableList(initialList, eReference);
232:
233:                 final Table table = setUpDatabindingTests(mockedObservableList, initialList.get(0), initialList.get(1));
234:
235:                 assertDatabinding(mockedObservableList, table);
236:         }
237:
238:         /**
239:          * Tests the binding from the model to the table for the addition of a new element to the model.
240:          *
241:          * @throws NoRendererFoundException Renderer could not be found
242:          * @throws NoPropertyDescriptorFoundExeption Property descriptor could not be found
243:          * @throws DatabindingFailedException if the databinidng failed
244:          */
245:         @Test
246:         public void testDatabindingServiceUsageAddToModel() throws NoRendererFoundException,
247:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
248:                 final List<EClass> initialList = createInitialList();
249:
250:                 final EReference eReference = mock(EReference.class);
251:                 final EClass eClass = EcorePackage.eINSTANCE.getEClass();
252:                 when(eReference.getEReferenceType()).thenReturn(eClass);
253:
254:                 final WritableList mockedObservableList = new WritableList(initialList, eReference);
255:
256:                 final Table table = setUpDatabindingTests(mockedObservableList, initialList.get(0), initialList.get(1));
257:
258:                 final EClass class3 = EcoreFactory.eINSTANCE.createEClass();
259:                 class3.setAbstract(true);
260:                 mockedObservableList.add(class3);
261:
262:                 assertDatabinding(mockedObservableList, table);
263:         }
264:
265:         /**
266:          * Tests the binding from the model to the table for the removal of a element from the model .
267:          *
268:          * @throws NoRendererFoundException Renderer could not be found
269:          * @throws NoPropertyDescriptorFoundExeption Property descriptor could not be found
270:          * @throws DatabindingFailedException if the databinidng failed
271:          */
272:         @Test
273:         public void testDatabindingServiceUsageRemoveFromModel() throws NoRendererFoundException,
274:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
275:                 final List<EClass> initialList = createInitialList();
276:                 final EReference eReference = mock(EReference.class);
277:                 final EClass eClass = EcorePackage.eINSTANCE.getEClass();
278:                 when(eReference.getEReferenceType()).thenReturn(eClass);
279:                 final WritableList mockedObservableList = new WritableList(initialList, eReference);
280:
281:                 final Table table = setUpDatabindingTests(mockedObservableList, initialList.get(0), initialList.get(1));
282:
283:                 mockedObservableList.remove(1);
284:
285:                 assertDatabinding(mockedObservableList, table);
286:         }
287:
288:         /**
289:          * Tests the binding from the model to the table for the change of an element of the model.
290:          *
291:          * @throws NoRendererFoundException Renderer could not be found
292:          * @throws NoPropertyDescriptorFoundExeption Property descriptor could not be found
293:          * @throws DatabindingFailedException if the databinidng failed
294:          */
295:         @Test
296:         public void testDatabindingServiceUsageChangeModel() throws NoRendererFoundException,
297:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
298:                 final List<EClass> initialList = createInitialList();
299:                 final EReference eReference = mock(EReference.class);
300:                 final EClass eClass = EcorePackage.eINSTANCE.getEClass();
301:                 when(eReference.getEReferenceType()).thenReturn(eClass);
302:                 final WritableList mockedObservableList = new WritableList(initialList, eReference);
303:
304:                 final Table table = setUpDatabindingTests(mockedObservableList, initialList.get(0), initialList.get(1));
305:
306:                 ((EClass) mockedObservableList.get(0)).setAbstract(true);
307:
308:                 assertDatabinding(mockedObservableList, table);
309:         }
310:
311:         @Test
312:         public void testCreateLabelStyleBits() {
313:                 /* setup */
314:                 final VTControlLabelAlignmentStyleProperty alignProperty = VTAlignmentFactory.eINSTANCE
315:                         .createControlLabelAlignmentStyleProperty();
316:                 alignProperty.setType(AlignmentType.RIGHT);
317:
318:                 final VTLabelWrapStyleProperty labelWrapStyleProperty = VTWrapFactory.eINSTANCE.createLabelWrapStyleProperty();
319:                 labelWrapStyleProperty.setWrapLabel(true);
320:
321:                 final Set<VTStyleProperty> properties = new LinkedHashSet<VTStyleProperty>();
322:                 properties.add(alignProperty);
323:                 properties.add(labelWrapStyleProperty);
324:                 Mockito.when(vtViewTemplateProvider.getStyleProperties(vTableControl, viewContext)).thenReturn(properties);
325:
326:                 /* act */
327:                 final Control render = renderer.createLabel(shell);
328:
329:                 /* assert */
330:                 assertTrue(Label.class.isInstance(render));
331:                 assertNotEquals(0, render.getStyle() & SWT.WRAP);
332:                 assertEquals(SWT.RIGHT, Label.class.cast(render).getAlignment());
333:         }
334:
335:         private void assertDatabinding(final WritableList mockedObservableList, final Table table) {
336:                 assertEquals(mockedObservableList.size(), table.getItemCount());
337:•                for (int i = 0; i < mockedObservableList.size(); i++) {
338:                         assertEquals(Boolean.toString(((EClass) mockedObservableList.get(i)).isAbstract()), table.getItem(i)
339:                                 .getText(1));
340:                 }
341:         }
342:
343:         /**
344:          * @return The initial list for data binding tests containing two {@link EClass} objects.
345:          */
346:         private List<EClass> createInitialList() {
347:                 final List<EClass> initialList = new LinkedList<EClass>();
348:                 final EClass class1 = EcoreFactory.eINSTANCE.createEClass();
349:                 class1.setAbstract(false);
350:                 final EClass class2 = EcoreFactory.eINSTANCE.createEClass();
351:                 class2.setAbstract(true);
352:                 initialList.add(class1);
353:                 initialList.add(class2);
354:                 return initialList;
355:         }
356:
357:         private Table setUpDatabindingTests(IObservableList mockedObservableList, EClass class1, EClass class2)
358:                 throws NoRendererFoundException,
359:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
360:
361:                 final IEMFValueProperty columnValueProperty = new EMFValuePropertyDecorator(
362:                         new EMFValueProperty(EcorePackage.eINSTANCE.getEClass_Abstract()),
363:                         EcorePackage.eINSTANCE.getEClass_Abstract());
364:                 final VDomainModelReference columnDMR = ((VTableDomainModelReference) vTableControl.getDomainModelReference())
365:                         .getColumnDomainModelReferences().get(0);
366:                 when(databindingService.getValueProperty(same(columnDMR), same(domainModel.eClass()), any(EditingDomain.class)))
367:                         .thenReturn(columnValueProperty);
368:                 when(databindingService.getValueProperty(columnDMR, (EObject) domainModel)).thenReturn(columnValueProperty);
369:
370:                 when(databindingService.getSetting(vTableControl.getDomainModelReference(), domainModel)).thenReturn(
371:                         InternalEObject.class.cast(domainModel).eSetting(EcorePackage.eINSTANCE.getEClass_ESuperTypes()));
372:
373:                 when(databindingService.getObservableList(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
374:                         mockedObservableList);
375:
376:                 final Control renderedControl = renderer.render(new SWTGridCell(0, 0, renderer), shell);
377:                 assertTrue(Composite.class.isInstance(renderedControl));
378:                 final Control tableControl = getTable(renderedControl);
379:                 assertTrue(Table.class.isInstance(tableControl));
380:                 return (Table) tableControl;
381:         }
382:
383:         private Control getTable(Control render) {
384:                 Composite composite = (Composite) render;
385:                 composite = (Composite) composite.getChildren()[0];
386:                 composite = (Composite) composite.getChildren()[1];
387:                 return composite.getChildren()[0];
388:         }
389: }