Skip to content

Package: NumberControlRenderer_PTest

NumberControlRenderer_PTest

nameinstructionbranchcomplexitylinemethod
NumberControlRenderer_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%
assertControl(Control)
M: 0 C: 57
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
before()
M: 0 C: 83
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
getDecimalFormat(Class)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
mockControl()
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%
renderControlLabelAlignmentLeft()
M: 0 C: 104
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
renderControlLabelAlignmentNone()
M: 0 C: 104
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
setUpDatabindingTest(ObservingWritableValue)
M: 0 C: 60
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testControlTextUpdateWithPrevalidation()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
testControlTextUpdateWithoutPrevalidation_InputTooBig()
M: 0 C: 56
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testControlTextUpdateWithoutPrevalidation_InputTooSmall()
M: 0 C: 56
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testDatabindingServiceUsageChangeControl()
M: 0 C: 33
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
testDatabindingServiceUsageChangeControlToDoubleValue()
M: 0 C: 42
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
testDatabindingServiceUsageChangeObservable()
M: 0 C: 33
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: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
testEffectivelyReadOnlyDeactivatesControl()
M: 0 C: 66
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 11
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%
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%

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: * Eugen Neufeld - initial API and implementation
13: * Lucas Koehler - databinding tests
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.internal.core.swt.renderer;
16:
17: import static org.junit.Assert.assertEquals;
18: import static org.junit.Assert.assertFalse;
19: import static org.junit.Assert.assertTrue;
20: import static org.mockito.Matchers.any;
21: import static org.mockito.Mockito.mock;
22: import static org.mockito.Mockito.when;
23:
24: import java.text.DecimalFormat;
25: import java.util.Locale;
26:
27: import org.eclipse.core.databinding.property.Properties;
28: import org.eclipse.emf.ecore.EAttribute;
29: import org.eclipse.emf.ecore.EClassifier;
30: import org.eclipse.emf.ecore.EObject;
31: import org.eclipse.emf.ecore.EStructuralFeature;
32: import org.eclipse.emf.ecore.EcoreFactory;
33: import org.eclipse.emf.ecore.EcorePackage;
34: import org.eclipse.emf.ecp.edit.internal.swt.controls.NumericalHelper;
35: import org.eclipse.emf.ecp.test.common.DefaultRealm;
36: import org.eclipse.emf.ecp.view.core.swt.tests.ObservingWritableValue;
37: import org.eclipse.emf.ecp.view.internal.core.swt.MessageKeys;
38: import org.eclipse.emf.ecp.view.spi.model.LabelAlignment;
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.renderer.NoPropertyDescriptorFoundExeption;
42: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
43: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
44: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
45: import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleProvider;
46: import org.eclipse.emfforms.spi.common.report.ReportService;
47: import org.eclipse.emfforms.spi.common.validation.PreSetValidationService;
48: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
49: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
50: import org.eclipse.emfforms.spi.core.services.editsupport.EMFFormsEditSupport;
51: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
52: import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
53: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
54: import org.eclipse.emfforms.spi.swt.core.SWTDataElementIdHelper;
55: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
56: import org.eclipse.emfforms.swt.common.test.AbstractControl_PTest;
57: import org.eclipse.swt.SWT;
58: import org.eclipse.swt.widgets.Composite;
59: import org.eclipse.swt.widgets.Control;
60: import org.eclipse.swt.widgets.Text;
61: import org.junit.After;
62: import org.junit.Before;
63: import org.junit.Ignore;
64: import org.junit.Test;
65:
66: public class NumberControlRenderer_PTest extends AbstractControl_PTest<VControl> {
67:
68:         private DefaultRealm realm;
69:
70:         @Before
71:         public void before() throws DatabindingFailedException {
72:                 realm = new DefaultRealm();
73:                 final ReportService reportService = mock(ReportService.class);
74:                 setDatabindingService(mock(EMFFormsDatabinding.class));
75:                 setLabelProvider(mock(EMFFormsLabelProvider.class));
76:                 setTemplateProvider(mock(VTViewTemplateProvider.class));
77:                 final EMFFormsLocalizationService localizationService = mock(EMFFormsLocalizationService.class);
78:                 when(
79:                         localizationService.getString(NumberControlSWTRenderer.class, MessageKeys.NumericalControl_FormatNumerical))
80:                                 .thenReturn("#"); //$NON-NLS-1$
81:                 when(
82:                         localizationService.getString(NumberControlSWTRenderer.class,
83:                                 MessageKeys.NumericalControl_FormatNumericalDecimal)).thenReturn("#.#"); //$NON-NLS-1$
84:                 final EMFFormsLocaleProvider localeProvider = mock(EMFFormsLocaleProvider.class);
85:                 when(localeProvider.getLocale()).thenReturn(Locale.getDefault());
86:                 final EMFFormsEditSupport editSupport = mock(EMFFormsEditSupport.class);
87:                 setup();
88:                 setRenderer(new NumberControlSWTRenderer(getvControl(), getContext(), reportService, getDatabindingService(),
89:                         getLabelProvider(),
90:                         getTemplateProvider(), editSupport, localizationService, localeProvider));
91:                 getRenderer().init();
92:         }
93:
94:         @After
95:         public void testTearDown() {
96:                 realm.dispose();
97:                 dispose();
98:         }
99:
100:         @SuppressWarnings({ "rawtypes", "unchecked" })
101:         @Test
102:         public void renderControlLabelAlignmentNone()
103:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption, DatabindingFailedException {
104:                 setMockLabelAlignment(LabelAlignment.NONE);
105:                 final TestObservableValue mockedObservableValue = mock(TestObservableValue.class);
106:                 when(mockedObservableValue.getRealm()).thenReturn(realm);
107:                 final EObject mockedEObject = mock(EObject.class);
108:                 when(mockedEObject.eIsSet(any(EStructuralFeature.class))).thenReturn(true);
109:                 when(mockedObservableValue.getObserved()).thenReturn(mockedEObject);
110:                 final EStructuralFeature mockedEStructuralFeature = mock(EStructuralFeature.class);
111:                 final EClassifier mockedEClassifier = mock(EClassifier.class);
112:                 final Class clazz = Double.class;
113:                 when(mockedEClassifier.getInstanceClass()).thenReturn(clazz);
114:                 when(mockedEStructuralFeature.getEType()).thenReturn(mockedEClassifier);
115:                 when(mockedObservableValue.getValueType()).thenReturn(mockedEStructuralFeature);
116:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
117:                         .thenReturn(
118:                                 mockedObservableValue);
119:                 when(getDatabindingService().getValueProperty(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
120:                         Properties.selfValue(mockedEStructuralFeature));
121:                 final Control render = renderControl(new SWTGridCell(0, 1, getRenderer()));
122:                 assertControl(render);
123:         }
124:
125:         @SuppressWarnings({ "unchecked" })
126:         @Test
127:         public void renderControlLabelAlignmentLeft()
128:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption, DatabindingFailedException {
129:                 setMockLabelAlignment(LabelAlignment.LEFT);
130:                 final TestObservableValue mockedObservableValue = mock(TestObservableValue.class);
131:                 when(mockedObservableValue.getRealm()).thenReturn(realm);
132:                 final EObject mockedEObject = mock(EObject.class);
133:                 when(mockedEObject.eIsSet(any(EStructuralFeature.class))).thenReturn(true);
134:                 when(mockedObservableValue.getObserved()).thenReturn(mockedEObject);
135:                 final EStructuralFeature mockedEStructuralFeature = mock(EStructuralFeature.class);
136:                 final EClassifier mockedEClassifier = mock(EClassifier.class);
137:                 final Class clazz = Double.class;
138:                 when(mockedEClassifier.getInstanceClass()).thenReturn(clazz);
139:                 when(mockedEStructuralFeature.getEType()).thenReturn(mockedEClassifier);
140:                 when(mockedObservableValue.getValueType()).thenReturn(mockedEStructuralFeature);
141:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
142:                         .thenReturn(
143:                                 mockedObservableValue);
144:                 when(getDatabindingService().getValueProperty(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
145:                         Properties.selfValue(mockedEStructuralFeature));
146:
147:                 final Control render = renderControl(new SWTGridCell(0, 2, getRenderer()));
148:
149:                 assertControl(render);
150:         }
151:
152:         /**
153:          * Tests whether the {@link EMFFormsLabelProvider} is used to get the labels of the control.
154:          *
155:          * @throws NoRendererFoundException
156:          * @throws NoPropertyDescriptorFoundExeption
157:          * @throws DatabindingFailedException
158:          * @throws NoLabelFoundException
159:          */
160:         @Test
161:         public void testLabelServiceUsage() throws NoRendererFoundException, NoPropertyDescriptorFoundExeption,
162:                 DatabindingFailedException, NoLabelFoundException {
163:                 labelServiceUsage();
164:         }
165:
166:         private void assertControl(Control render) {
167:                 final Control textRender = Composite.class.cast(render).getChildren()[0];
168:                 assertTrue(Text.class.isInstance(textRender));
169:                 assertEquals(SWT.RIGHT, Text.class.cast(textRender).getStyle()
170:                         & SWT.RIGHT);
171:                 assertEquals(SWT.RIGHT, Text.class.cast(textRender).getStyle()
172:                         & SWT.RIGHT);
173:
174:                 assertEquals("org_eclipse_emf_ecp_control_numerical", Text.class.cast(textRender).getData(CUSTOM_VARIANT)); //$NON-NLS-1$
175:                 assertEquals(UUID + "#control", Text.class.cast(textRender).getData(SWTDataElementIdHelper.ELEMENT_ID_KEY)); //$NON-NLS-1$
176:                 assertEquals(UUID + "#control", Composite.class.cast(render).getData(SWTDataElementIdHelper.ELEMENT_ID_KEY)); //$NON-NLS-1$
177:         }
178:
179:         @Override
180:         protected void mockControl() throws DatabindingFailedException {
181:                 final EStructuralFeature eObject = EcoreFactory.eINSTANCE.createEAttribute();
182:                 final EStructuralFeature eStructuralFeature = EcorePackage.eINSTANCE.getETypedElement_LowerBound();
183:                 super.mockControl(eObject, eStructuralFeature);
184:         }
185:
186:         @Test
187:         public void testDatabindingServiceUsageInitialBinding() throws NoRendererFoundException,
188:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
189:                 final int initialValue = 13;
190:                 final ObservingWritableValue mockedObservable = new ObservingWritableValue(realm, initialValue,
191:                         EcorePackage.eINSTANCE.getETypedElement_LowerBound());
192:                 final Text text = setUpDatabindingTest(mockedObservable);
193:
194:                 final DecimalFormat format = getDecimalFormat(Integer.class);
195:
196:                 assertEquals(format.format(initialValue), text.getText());
197:
198:         }
199:
200:         @Test
201:         public void testDatabindingServiceUsageChangeObservable() throws NoRendererFoundException,
202:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
203:                 final int initialValue = 13;
204:                 final int changedValue = 42;
205:                 final ObservingWritableValue mockedObservable = new ObservingWritableValue(realm, initialValue,
206:                         EcorePackage.eINSTANCE.getETypedElement_LowerBound());
207:
208:                 final Text text = setUpDatabindingTest(mockedObservable);
209:                 mockedObservable.setValue(changedValue);
210:
211:                 final DecimalFormat format = getDecimalFormat(Integer.class);
212:
213:                 assertEquals(format.format(changedValue), text.getText());
214:
215:         }
216:
217:         @Test
218:         public void testDatabindingServiceUsageChangeControl() throws NoRendererFoundException,
219:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
220:                 final int initialValue = 13;
221:                 final int changedValue = 42;
222:                 final ObservingWritableValue mockedObservable = new ObservingWritableValue(realm, initialValue,
223:                         EcorePackage.eINSTANCE.getETypedElement_LowerBound());
224:
225:                 final Text text = setUpDatabindingTest(mockedObservable);
226:
227:                 final DecimalFormat format = getDecimalFormat(Integer.class);
228:                 SWTTestUtil.typeAndFocusOut(text, format.format(changedValue));
229:
230:                 assertEquals(changedValue, mockedObservable.getValue());
231:
232:         }
233:
234:         @Test
235:         public void testDatabindingServiceUsageChangeControlToDoubleValue() throws NoRendererFoundException,
236:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
237:                 final double initialValue = 13;
238:                 final double changedValue = 42.5;
239:                 final EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
240:                 eAttribute.setEType(EcorePackage.eINSTANCE.getEDouble());
241:                 eAttribute.setName("doubleAttr"); //$NON-NLS-1$
242:                 final ObservingWritableValue mockedObservable = new ObservingWritableValue(realm, initialValue,
243:                         eAttribute);
244:
245:                 final Text text = setUpDatabindingTest(mockedObservable);
246:
247:                 final DecimalFormat format = getDecimalFormat(Double.class);
248:                 SWTTestUtil.typeAndFocusOut(text, format.format(changedValue));
249:
250:                 assertEquals(changedValue, mockedObservable.getValue());
251:
252:         }
253:
254:         @Test
255:         public void testControlTextUpdateWithoutPrevalidation_InputTooBig()
256:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption, DatabindingFailedException {
257:                 when(getContext().hasService(PreSetValidationService.class)).thenReturn(false);
258:                 when(getContext().getService(PreSetValidationService.class)).thenReturn(null);
259:
260:                 final int initialValue = 0;
261:                 final String tooBig = "11111111111111111111111111111111"; //$NON-NLS-1$
262:                 final ObservingWritableValue mockedObservable = new ObservingWritableValue(realm, initialValue,
263:                         EcorePackage.eINSTANCE.getETypedElement_LowerBound());
264:
265:                 final Text text = setUpDatabindingTest(mockedObservable);
266:
267:                 SWTTestUtil.typeAndFocusOut(text, tooBig);
268:
269:                 assertEquals(Integer.MAX_VALUE, mockedObservable.getValue());
270:                 final DecimalFormat format = getDecimalFormat(Integer.class);
271:                 assertEquals(format.format(Integer.MAX_VALUE), text.getText());
272:         }
273:
274:         @Test
275:         public void testControlTextUpdateWithoutPrevalidation_InputTooSmall()
276:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption, DatabindingFailedException {
277:                 when(getContext().hasService(PreSetValidationService.class)).thenReturn(false);
278:                 when(getContext().getService(PreSetValidationService.class)).thenReturn(null);
279:
280:                 final int initialValue = 0;
281:                 final String tooSmall = "-11111111111111111111111111111111"; //$NON-NLS-1$
282:                 final ObservingWritableValue mockedObservable = new ObservingWritableValue(realm, initialValue,
283:                         EcorePackage.eINSTANCE.getETypedElement_LowerBound());
284:
285:                 final Text text = setUpDatabindingTest(mockedObservable);
286:
287:                 SWTTestUtil.typeAndFocusOut(text, tooSmall);
288:
289:                 assertEquals(Integer.MIN_VALUE, mockedObservable.getValue());
290:                 final DecimalFormat format = getDecimalFormat(Integer.class);
291:                 assertEquals(format.format(Integer.MIN_VALUE), text.getText());
292:         }
293:
294:         @Ignore
295:         @Test
296:         public void testControlTextUpdateWithPrevalidation() {
297:                 // TODO implement when PreSetValidationListeners does not use a static instance of PreSetValidationService
298:                 // anymore
299:         }
300:
301:         /**
302:          * Universal set up stuff for the data binding test cases.
303:          *
304:          * @param mockedObservable
305:          * @return
306:          * @throws NoRendererFoundException
307:          * @throws NoPropertyDescriptorFoundExeption
308:          * @throws DatabindingFailedException
309:          */
310:         private Text setUpDatabindingTest(final ObservingWritableValue mockedObservable) throws NoRendererFoundException,
311:                 NoPropertyDescriptorFoundExeption, DatabindingFailedException {
312:                 mockDatabindingIsSettableAndChangeable();
313:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
314:                         .thenReturn(
315:                                 mockedObservable, new ObservingWritableValue(mockedObservable));
316:                 when(getDatabindingService().getValueProperty(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
317:                         Properties.selfValue(mockedObservable.getValueType()));
318:
319:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
320:                 final Text text = (Text) Composite.class.cast(renderControl).getChildren()[0];
321:                 return text;
322:         }
323:
324:         private DecimalFormat getDecimalFormat(Class<?> instanceClass) {
325:                 return NumericalHelper.setupFormat(Locale.getDefault(), instanceClass);
326:         }
327:
328:         @Test
329:         public void testEffectivelyReadOnlyDeactivatesControl()
330:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption, DatabindingFailedException {
331:                 final ObservingWritableValue mockedObservable = new ObservingWritableValue(realm, 1,
332:                         EcorePackage.eINSTANCE.getETypedElement_LowerBound());
333:                 when(getDatabindingService().getObservableValue(any(VDomainModelReference.class), any(EObject.class)))
334:                         .thenReturn(mockedObservable);
335:                 when(getDatabindingService().getValueProperty(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
336:                         Properties.selfValue(mockedObservable.getValueType()));
337:
338:                 when(getvControl().isEffectivelyReadonly()).thenReturn(true);
339:                 final Control renderControl = renderControl(new SWTGridCell(0, 2, getRenderer()));
340:                 getRenderer().finalizeRendering(getShell());
341:                 assertFalse(renderControl.isEnabled());
342:         }
343: }