Skip to content

Package: MappedEClassControlSWTRenderer_PTest

MappedEClassControlSWTRenderer_PTest

nameinstructionbranchcomplexitylinemethod
MappedEClassControlSWTRenderer_PTest()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
disposeRealm()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
disposeShells()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$0()
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
lambda$1(Shell)
M: 5 C: 21
81%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 4
57%
M: 0 C: 1
100%
lambda$10(Shell)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$12(Shell)
M: 2 C: 4
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
lambda$14()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$16(Shell)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$18(Shell)
M: 2 C: 4
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
lambda$3(Shell)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$5()
M: 0 C: 25
100%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
lambda$6()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$8(Button)
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%
render()
M: 0 C: 102
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
render_cancelDialog()
M: 0 C: 75
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
setUp()
M: 2 C: 186
99%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 41
98%
M: 0 C: 1
100%
waitForShell(String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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: ******************************************************************************/
14: package org.eclipse.emfforms.internal.ide.view.mappingsegment;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertSame;
18: import static org.junit.Assert.assertTrue;
19: import static org.junit.Assert.fail;
20: import static org.mockito.Mockito.mock;
21: import static org.mockito.Mockito.when;
22:
23: import org.eclipse.emf.ecore.EClass;
24: import org.eclipse.emf.ecore.EPackage;
25: import org.eclipse.emf.ecore.EPackage.Registry;
26: import org.eclipse.emf.ecore.EReference;
27: import org.eclipse.emf.ecore.EcoreFactory;
28: import org.eclipse.emf.ecp.test.common.DefaultRealm;
29: import org.eclipse.emf.ecp.test.common.MultiTryTestRule;
30: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
31: import org.eclipse.emf.ecp.view.spi.editor.controls.SelectedFeatureViewService;
32: import org.eclipse.emf.ecp.view.spi.model.LabelAlignment;
33: import org.eclipse.emf.ecp.view.spi.model.VControl;
34: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
35: import org.eclipse.emf.ecp.view.spi.model.VFeatureDomainModelReferenceSegment;
36: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
37: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
38: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
39: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
40: import org.eclipse.emfforms.spi.common.report.ReportService;
41: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
42: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingDomainModelReferenceSegment;
43: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingsegmentFactory;
44: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingsegmentPackage;
45: import org.eclipse.swt.layout.FillLayout;
46: import org.eclipse.swt.widgets.Button;
47: import org.eclipse.swt.widgets.Composite;
48: import org.eclipse.swt.widgets.Control;
49: import org.eclipse.swt.widgets.Display;
50: import org.eclipse.swt.widgets.Shell;
51: import org.eclipse.swtbot.swt.finder.SWTBot;
52: import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
53: import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
54: import org.eclipse.swtbot.swt.finder.waits.Conditions;
55: import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
56: import org.junit.After;
57: import org.junit.Before;
58: import org.junit.Rule;
59: import org.junit.Test;
60: import org.junit.runner.RunWith;
61:
62: /**
63: * Test for {@link MappedEClassControlSWTRenderer}.
64: *
65: * @author Lucas Koehler
66: *
67: */
68: @RunWith(SWTBotJunit4ClassRunner.class)
69: public class MappedEClassControlSWTRenderer_PTest {
70:
71:         // Allow two tries because Jenkins CI might fail on the first run of the first test case
72:         @Rule
73:         public final MultiTryTestRule multiTryRule = new MultiTryTestRule(2);
74:
75:         private MappedEClassControlSWTRenderer renderer;
76:         private SelectedFeatureViewService featureService;
77:         private VMappingDomainModelReferenceSegment domainObject;
78:
79:         private EClass valueType;
80:
81:         private EClass subValueType;
82:
83:         private Control renderControl;
84:
85:         private DefaultRealm realm;
86:
87:         @Before
88:         public void setUp() {
89:                 realm = new DefaultRealm();
90:                 final VControl vControl = mock(VControl.class);
91:
92:                 // We need real objects because the MappedEClassControlSWTRenderer extends the
93:                 // EditableEReferenceLabelControlSWTRenderer which doesn't use proper DI. Consequently, we cannot mock the
94:                 // databinding.
95:                 domainObject = VMappingsegmentFactory.eINSTANCE.createMappingDomainModelReferenceSegment();
96:                 final VDomainModelReference dmr = VViewFactory.eINSTANCE.createDomainModelReference();
97:                 final VFeatureDomainModelReferenceSegment segment = VViewFactory.eINSTANCE
98:                         .createFeatureDomainModelReferenceSegment();
99:                 segment.setDomainModelFeature(
100:                         VMappingsegmentPackage.Literals.MAPPING_DOMAIN_MODEL_REFERENCE_SEGMENT__MAPPED_CLASS.getName());
101:                 dmr.getSegments().add(segment);
102:
103:                 when(vControl.getDomainModelReference()).thenReturn(dmr);
104:                 when(vControl.isVisible()).thenReturn(true);
105:                 when(vControl.isEffectivelyEnabled()).thenReturn(true);
106:                 when(vControl.isEffectivelyReadonly()).thenReturn(false);
107:                 when(vControl.getLabelAlignment()).thenReturn(LabelAlignment.DEFAULT);
108:                 final ViewModelContext viewContext = mock(ViewModelContext.class);
109:                 when(viewContext.getDomainModel()).thenReturn(domainObject);
110:                 final ReportService reportService = mock(ReportService.class);
111:                 featureService = mock(SelectedFeatureViewService.class);
112:                 when(viewContext.getService(SelectedFeatureViewService.class)).thenReturn(featureService);
113:
114:                 // Mock example package and classes
115:                 final EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
116:                 ePackage.setName("mappingSegmentTest"); //$NON-NLS-1$
117:                 ePackage.setNsPrefix("mappingSegmentTest"); //$NON-NLS-1$
118:                 ePackage.setNsURI("mappingSegmentTest"); //$NON-NLS-1$
119:                 valueType = EcoreFactory.eINSTANCE.createEClass();
120:                 valueType.setName("ValueType"); //$NON-NLS-1$
121:                 subValueType = EcoreFactory.eINSTANCE.createEClass();
122:                 subValueType.setName("SubValueType"); //$NON-NLS-1$
123:                 subValueType.getESuperTypes().add(valueType);
124:                 ePackage.getEClassifiers().add(valueType);
125:                 ePackage.getEClassifiers().add(subValueType);
126:                 Registry.INSTANCE.put("mappingSegmentTest", ePackage); //$NON-NLS-1$
127:
128:                 final EReference mappingERef = TestUtil.mockMapReference(valueType);
129:                 when(featureService.getFeature()).thenReturn(mappingERef);
130:
131:                 renderer = new MappedEClassControlSWTRenderer(vControl, viewContext, reportService);
132:
133:                 // Wait for the UI harness to be ready before starting the test execution
134:                 final Shell baseShell = UIThreadRunnable.syncExec(() -> {
135:                         final Shell s = new Shell(Display.getDefault());
136:                         s.setLayout(new FillLayout());
137:                         s.setSize(400, 400);
138:                         s.setLocation(100, 100);
139:                         s.open();
140:                         return s;
141:                 });
142:
143:                 // Render
144:                 renderControl = UIThreadRunnable.syncExec(() -> {
145:                         renderer.init();
146:                         try {
147:                                 final Control result = renderer.render(new SWTGridCell(0, 2, renderer), baseShell);
148:                                 renderer.finalizeRendering(baseShell);
149:                                 return result;
150:                         } catch (NoRendererFoundException | NoPropertyDescriptorFoundExeption ex) {
151:                                 ex.printStackTrace();
152:                                 return null;
153:                         }
154:                 });
155:
156:•                if (renderControl == null) {
157:                         fail("Rendering failed!"); //$NON-NLS-1$
158:                 }
159:
160:                 UIThreadRunnable.syncExec(SWTTestUtil::waitForUIThread);
161:                 UIThreadRunnable.syncExec(() -> baseShell.layout(true, true));
162:                 UIThreadRunnable.syncExec(SWTTestUtil::waitForUIThread);
163:         }
164:
165:         @After
166:         public void disposeRealm() {
167:                 realm.dispose();
168:         }
169:
170:         @After
171:         public void disposeShells() {
172:                 // Shell 0 and 1 must not be disposed. They belong to the UI harness around the tests
173:                 UIThreadRunnable.syncExec(Display.getDefault(), () -> {
174:                         final Shell[] shells = Display.getDefault().getShells();
175:•                        for (int i = 2; i < shells.length; i++) {
176:•                                if (shells[i] != null && !shells[i].isDisposed()) {
177:                                         shells[i].dispose();
178:                                 }
179:                         }
180:                 });
181:         }
182:
183:         @Test
184:         public void render() {
185:                 final Control c = UIThreadRunnable.syncExec(() -> {
186:                         final Composite renderComposite = Composite.class.cast(renderControl);
187:                         return renderComposite.getChildren()[1];
188:                 });
189:                 assertTrue("Must be a button: " + c, c instanceof Button); //$NON-NLS-1$
190:                 final Button linkButton = (Button) c;
191:                 assertEquals("Link Mapped Class", UIThreadRunnable.syncExec(linkButton::getText)); //$NON-NLS-1$
192:                 UIThreadRunnable.asyncExec(() -> {
193:                         SWTTestUtil.clickButton(linkButton);
194:                 });
195:
196:                 waitForShell("Select an EClass"); //$NON-NLS-1$
197:                 final Shell dialogShell = UIThreadRunnable.syncExec(Display.getDefault()::getActiveShell);
198:                 final SWTBot dialogBot = UIThreadRunnable.syncExec(() -> new SWTBot(dialogShell));
199:
200:                 // tree view should show ValueType as root node and SubValueType as its child
201:                 final SWTBotTreeItem[] treeItems = UIThreadRunnable.syncExec(dialogBot.tree()::getAllItems);
202:                 assertEquals(1, treeItems.length);
203:                 assertTrue(treeItems[0].getText().contains("ValueType")); //$NON-NLS-1$
204:
205:                 treeItems[0].expand();
206:                 final SWTBotTreeItem[] subItems = treeItems[0].getItems();
207:                 assertEquals(1, subItems.length);
208:
209:                 // Select SubValueType and click OK
210:                 subItems[0].select();
211:                 dialogBot.button("OK").click(); //$NON-NLS-1$
212:                 UIThreadRunnable.syncExec(() -> {
213:•                        if (!dialogShell.isDisposed()) {
214:                                 dialogShell.dispose();
215:                         }
216:                 });
217:
218:                 UIThreadRunnable.syncExec(SWTTestUtil::waitForUIThread);
219:                 assertSame(subValueType, domainObject.getMappedClass());
220:         }
221:
222:         @Test
223:         public void render_cancelDialog() {
224:                 UIThreadRunnable.asyncExec(() -> {
225:                         final Composite renderComposite = Composite.class.cast(renderControl);
226:                         // click 'Link Mapped Class' Button
227:                         SWTTestUtil.clickButton((Button) renderComposite.getChildren()[1]);
228:                 });
229:
230:                 waitForShell("Select an EClass"); //$NON-NLS-1$
231:                 final Shell dialogShell = UIThreadRunnable.syncExec(Display.getDefault()::getActiveShell);
232:                 final SWTBot dialogBot = UIThreadRunnable.syncExec(() -> new SWTBot(dialogShell));
233:
234:                 final SWTBotTreeItem[] treeItems = UIThreadRunnable.syncExec(dialogBot.tree()::getAllItems);
235:                 assertEquals(1, treeItems.length);
236:                 assertTrue(treeItems[0].getText().contains("ValueType")); //$NON-NLS-1$
237:
238:                 treeItems[0].expand();
239:                 final SWTBotTreeItem[] subItems = treeItems[0].getItems();
240:                 assertEquals(1, subItems.length);
241:
242:                 subItems[0].select();
243:                 dialogBot.button("Cancel").click(); //$NON-NLS-1$
244:                 UIThreadRunnable.syncExec(() -> {
245:•                        if (!dialogShell.isDisposed()) {
246:                                 dialogShell.dispose();
247:                         }
248:                 });
249:
250:                 UIThreadRunnable.syncExec(SWTTestUtil::waitForUIThread);
251:                 assertEquals(null, domainObject.getMappedClass());
252:         }
253:
254:         //
255:         // Test Framework
256:         //
257:
258:         private static void waitForShell(String title) {
259:                 new SWTBot().waitUntil(Conditions.shellIsActive(title), 10000, 100);
260:         }
261: }