Skip to content

Package: LeafConditionSegmentControlRenderer_PTest

LeafConditionSegmentControlRenderer_PTest

nameinstructionbranchcomplexitylinemethod
LeafConditionSegmentControlRenderer_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%
onSelectButton()
M: 0 C: 97
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
onSelectButton_dmrDatabindingFailed()
M: 0 C: 44
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
onSelectButton_dmrNotPointingToEAttribute()
M: 0 C: 51
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
onSelectButton_noDmr()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 97
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 22
100%
M: 0 C: 1
100%
tearDown()
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%

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.emf.ecp.view.internal.editor.controls;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertNotNull;
18: import static org.junit.Assert.assertNull;
19: import static org.junit.Assert.assertSame;
20: import static org.junit.Assert.assertTrue;
21: import static org.mockito.Matchers.any;
22: import static org.mockito.Matchers.eq;
23: import static org.mockito.Matchers.same;
24: import static org.mockito.Mockito.mock;
25: import static org.mockito.Mockito.spy;
26: import static org.mockito.Mockito.verify;
27: import static org.mockito.Mockito.when;
28:
29: import org.eclipse.emf.common.command.CommandStack;
30: import org.eclipse.emf.databinding.IEMFValueProperty;
31: import org.eclipse.emf.ecore.EAttribute;
32: import org.eclipse.emf.ecore.EClass;
33: import org.eclipse.emf.ecore.EObject;
34: import org.eclipse.emf.ecore.EReference;
35: import org.eclipse.emf.ecore.EcorePackage;
36: import org.eclipse.emf.ecore.resource.Resource;
37: import org.eclipse.emf.ecp.test.common.TestUtil;
38: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
39: import org.eclipse.emf.ecp.view.spi.model.VControl;
40: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
41: import org.eclipse.emf.ecp.view.spi.model.VView;
42: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
43: import org.eclipse.emf.ecp.view.spi.rule.model.EnableRule;
44: import org.eclipse.emf.ecp.view.spi.rule.model.LeafCondition;
45: import org.eclipse.emf.ecp.view.spi.rule.model.RuleFactory;
46: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
47: import org.eclipse.emf.edit.command.SetCommand;
48: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
49: import org.eclipse.emf.edit.domain.EditingDomain;
50: import org.eclipse.emfforms.spi.common.report.ReportService;
51: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
52: import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
53: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
54: import org.eclipse.swt.SWT;
55: import org.eclipse.swt.widgets.Label;
56: import org.eclipse.swt.widgets.Shell;
57: import org.junit.After;
58: import org.junit.Before;
59: import org.junit.Test;
60:
61: /**
62: * Tests for {@link LeafConditionSegmentControlRenderer}.
63: *
64: * @author Lucas Koehler
65: *
66: */
67: public class LeafConditionSegmentControlRenderer_PTest {
68:
69:         private LeafCondition leafCondition;
70:         private Object selectedObject;
71:         private TestLeafConditionSegmentControlRenderer renderer;
72:         private EMFFormsDatabindingEMF databinding;
73:         private Shell shell;
74:         private Label label;
75:         private ReportService reportService;
76:
77:         @Before
78:         public void setUp() {
79:                 selectedObject = new Object();
80:                 leafCondition = RuleFactory.eINSTANCE.createLeafCondition();
81:                 final EnableRule rule = RuleFactory.eINSTANCE.createEnableRule();
82:                 rule.setCondition(leafCondition);
83:                 final VControl control = VViewFactory.eINSTANCE.createControl();
84:                 control.getAttachments().add(rule);
85:                 final VView view = VViewFactory.eINSTANCE.createView();
86:                 view.setRootEClass(EcorePackage.Literals.ECLASS);
87:                 view.getChildren().add(control);
88:                 // Add leaf condition to resource with editing domain
89:                 final Resource resource = TestUtil.createResourceWithEditingDomain();
90:                 resource.getContents().add(view);
91:
92:                 reportService = mock(ReportService.class);
93:                 final VControl vControl = VViewFactory.eINSTANCE.createControl();
94:                 final ViewModelContext viewContext = mock(ViewModelContext.class);
95:                 databinding = mock(EMFFormsDatabindingEMF.class);
96:                 final EMFFormsLabelProvider labelProvider = mock(EMFFormsLabelProvider.class);
97:                 final VTViewTemplateProvider templateProvider = mock(VTViewTemplateProvider.class);
98:
99:                 renderer = spy(new TestLeafConditionSegmentControlRenderer(vControl, viewContext, reportService, databinding,
100:                         labelProvider, templateProvider));
101:                 shell = new Shell();
102:                 label = new Label(shell, SWT.NONE);
103:         }
104:
105:         @After
106:         public void tearDown() {
107:                 shell.dispose();
108:         }
109:
110:         @Test
111:         public void onSelectButton_noDmr() {
112:                 renderer.onSelectButton(label);
113:
114:                 verify(renderer).showError(shell, "No Domain Model Reference found",
115:                         "A Domain Model Reference needs to be added to the condition first.");
116:                 assertNull(leafCondition.getExpectedValue());
117:         }
118:
119:         @Test
120:         public void onSelectButton_dmrDatabindingFailed() throws DatabindingFailedException {
121:                 final VDomainModelReference dmr = VViewFactory.eINSTANCE.createDomainModelReference();
122:                 leafCondition.setDomainModelReference(dmr);
123:                 when(databinding.getValueProperty(same(dmr), any(EClass.class)))
124:                         .thenThrow(mock(DatabindingFailedException.class));
125:
126:                 renderer.onSelectButton(label);
127:
128:                 verify(reportService).report(any());
129:                 assertNull(leafCondition.getExpectedValue());
130:         }
131:
132:         @Test
133:         public void onSelectButton_dmrNotPointingToEAttribute() throws DatabindingFailedException {
134:                 final VDomainModelReference dmr = VViewFactory.eINSTANCE.createDomainModelReference();
135:                 leafCondition.setDomainModelReference(dmr);
136:                 final IEMFValueProperty valueProperty = mock(IEMFValueProperty.class);
137:                 when(valueProperty.getStructuralFeature()).thenReturn(mock(EReference.class));
138:                 when(databinding.getValueProperty(same(dmr), any(EClass.class))).thenReturn(valueProperty);
139:
140:                 renderer.onSelectButton(label);
141:
142:                 verify(renderer).showError(shell, "No EAttribute selected", //$NON-NLS-1$
143:                         "The condition's domain model reference must point to an EAttribute."); //$NON-NLS-1$
144:                 assertNull(leafCondition.getExpectedValue());
145:         }
146:
147:         @Test
148:         public void onSelectButton() throws DatabindingFailedException {
149:                 final VDomainModelReference dmr = VViewFactory.eINSTANCE.createDomainModelReference();
150:                 leafCondition.setDomainModelReference(dmr);
151:                 final VDomainModelReference valueDmr = VViewFactory.eINSTANCE.createDomainModelReference();
152:                 leafCondition.setValueDomainModelReference(valueDmr);
153:                 final IEMFValueProperty valueProperty = mock(IEMFValueProperty.class);
154:                 final EAttribute eAttribute = mock(EAttribute.class);
155:                 when(valueProperty.getStructuralFeature()).thenReturn(eAttribute);
156:                 when(databinding.getValueProperty(same(dmr), any(EClass.class))).thenReturn(valueProperty);
157:
158:                 renderer.onSelectButton(label);
159:
160:                 verify(renderer).showInfo(same(shell), eq("Legacy Value DMR will be removed"), any());
161:                 assertNull(leafCondition.getValueDomainModelReference());
162:                 assertNotNull(leafCondition.getExpectedValue());
163:                 final EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(leafCondition);
164:                 final CommandStack commandStack = domain.getCommandStack();
165:                 assertTrue(commandStack.getMostRecentCommand() instanceof SetCommand);
166:                 final SetCommand setCommand = (SetCommand) commandStack.getMostRecentCommand();
167:                 assertSame("SetCommand should have selected object as value.", selectedObject, setCommand.getValue());
168:                 assertEquals(selectedObject.toString(), label.getText());
169:         }
170:
171:         // Must be public to allow spying/mocking with Mockito
172:         public class TestLeafConditionSegmentControlRenderer extends LeafConditionSegmentControlRenderer {
173:
174:                 /**
175:                  * @param vElement
176:                  * @param viewContext
177:                  * @param reportService
178:                  * @param databindingService
179:                  * @param labelProvider
180:                  * @param viewTemplateProvider
181:                  */
182:                 public TestLeafConditionSegmentControlRenderer(VControl vElement, ViewModelContext viewContext,
183:                         ReportService reportService, EMFFormsDatabindingEMF databindingService, EMFFormsLabelProvider labelProvider,
184:                         VTViewTemplateProvider viewTemplateProvider) {
185:                         super(vElement, viewContext, reportService, databindingService, labelProvider, viewTemplateProvider);
186:                 }
187:
188:                 @Override
189:                 protected void showError(Shell shell, String title, String description) {
190:                         // do nothing. Mockito is used to verify whether this is called
191:                 }
192:
193:                 @Override
194:                 protected void showInfo(Shell parent, String title, String message) {
195:                         // do nothing. Mockito is used to verify whether this is called
196:                 }
197:
198:                 @Override
199:                 protected EObject getObservedEObject() throws DatabindingFailedException {
200:                         return leafCondition;
201:                 }
202:
203:                 @Override
204:                 protected Object getSelectedObject(EAttribute attribute) {
205:                         return selectedObject;
206:                 }
207:         }
208: }