Skip to content

Package: ViewTemplateProviderImpl_Test

ViewTemplateProviderImpl_Test

nameinstructionbranchcomplexitylinemethod
ViewTemplateProviderImpl_Test()
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%
getStyleProperties()
M: 7 C: 181
96%
M: 5 C: 3
38%
M: 4 C: 1
20%
M: 0 C: 39
100%
M: 0 C: 1
100%
getStyleProperties_ControlValidationConfiguration()
M: 0 C: 112
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 26
100%
M: 0 C: 1
100%
getViewTemplate()
M: 0 C: 126
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 32
100%
M: 0 C: 1
100%
getViewTemplate_NoSuppliedTemplates()
M: 0 C: 36
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
getViewTemplate_NoSuppliers()
M: 0 C: 32
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
hasControlValidationTemplate_False()
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
hasControlValidationTemplate_True()
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 36
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 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.template.service;
15:
16: import static org.junit.Assert.assertEquals;
17: import static org.junit.Assert.assertFalse;
18: import static org.junit.Assert.assertNotNull;
19: import static org.junit.Assert.assertNotSame;
20: import static org.junit.Assert.assertNull;
21: import static org.junit.Assert.assertSame;
22: import static org.junit.Assert.assertTrue;
23: import static org.mockito.Matchers.any;
24: import static org.mockito.Mockito.mock;
25: import static org.mockito.Mockito.verify;
26: import static org.mockito.Mockito.when;
27:
28: import java.util.Collections;
29: import java.util.HashMap;
30: import java.util.Iterator;
31: import java.util.LinkedHashMap;
32: import java.util.Map;
33: import java.util.Set;
34:
35: import org.eclipse.emf.ecp.spi.view.template.service.ViewTemplateSupplier;
36: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
37: import org.eclipse.emf.ecp.view.spi.model.VElement;
38: import org.eclipse.emf.ecp.view.template.model.VTControlValidationTemplate;
39: import org.eclipse.emf.ecp.view.template.model.VTStyle;
40: import org.eclipse.emf.ecp.view.template.model.VTStyleProperty;
41: import org.eclipse.emf.ecp.view.template.model.VTTemplateFactory;
42: import org.eclipse.emf.ecp.view.template.model.VTViewTemplate;
43: import org.eclipse.emf.ecp.view.template.style.alignment.model.AlignmentType;
44: import org.eclipse.emf.ecp.view.template.style.alignment.model.VTAlignmentFactory;
45: import org.eclipse.emf.ecp.view.template.style.alignment.model.VTAlignmentStyleProperty;
46: import org.eclipse.emf.ecp.view.template.style.background.model.VTBackgroundFactory;
47: import org.eclipse.emf.ecp.view.template.style.background.model.VTBackgroundStyleProperty;
48: import org.eclipse.emf.ecp.view.template.style.mandatory.model.VTMandatoryFactory;
49: import org.eclipse.emf.ecp.view.template.style.mandatory.model.VTMandatoryStyleProperty;
50: import org.eclipse.emf.ecp.view.template.style.validation.model.VTValidationFactory;
51: import org.eclipse.emf.ecp.view.template.style.validation.model.VTValidationPackage;
52: import org.eclipse.emf.ecp.view.template.style.validation.model.VTValidationStyleProperty;
53: import org.eclipse.emfforms.spi.common.report.AbstractReport;
54: import org.eclipse.emfforms.spi.common.report.ReportService;
55: import org.junit.Before;
56: import org.junit.Test;
57:
58: /**
59: * Unit tests for {@link ViewTemplateProviderImpl}.
60: *
61: * @author Lucas Koehler
62: *
63: */
64: public class ViewTemplateProviderImpl_Test {
65:
66:         private ViewTemplateProviderImpl provider;
67:         private ViewTemplateSupplier supplier1;
68:         private ViewTemplateSupplier supplier2;
69:         private ReportService reportService;
70:
71:         /**
72:          * Set up the {@link ViewTemplateProviderImpl} with two basic suppliers for every test.
73:          */
74:         @Before
75:         public void setUp() {
76:                 provider = new ViewTemplateProviderImpl();
77:                 reportService = mock(ReportService.class);
78:                 provider.setReportService(reportService);
79:                 supplier1 = mock(ViewTemplateSupplier.class);
80:                 supplier2 = mock(ViewTemplateSupplier.class);
81:                 provider.addViewTemplateSupplier(supplier1);
82:                 provider.addViewTemplateSupplier(supplier2);
83:         }
84:
85:         @SuppressWarnings("deprecation")
86:         @Test
87:         public void hasControlValidationTemplate_True() {
88:                 final VTViewTemplate viewTemplate1 = VTTemplateFactory.eINSTANCE.createViewTemplate();
89:                 final VTViewTemplate viewTemplate2 = VTTemplateFactory.eINSTANCE.createViewTemplate();
90:                 viewTemplate2.setControlValidationConfiguration(VTTemplateFactory.eINSTANCE.createControlValidationTemplate());
91:                 when(supplier1.getViewTemplates()).thenReturn(Collections.singleton(viewTemplate1));
92:                 when(supplier2.getViewTemplates()).thenReturn(Collections.singleton(viewTemplate2));
93:
94:                 assertTrue(provider.hasControlValidationTemplate());
95:         }
96:
97:         @SuppressWarnings("deprecation")
98:         @Test
99:         public void hasControlValidationTemplate_False() {
100:                 final VTViewTemplate viewTemplate1 = VTTemplateFactory.eINSTANCE.createViewTemplate();
101:                 final VTViewTemplate viewTemplate2 = VTTemplateFactory.eINSTANCE.createViewTemplate();
102:                 when(supplier1.getViewTemplates()).thenReturn(Collections.singleton(viewTemplate1));
103:                 when(supplier2.getViewTemplates()).thenReturn(Collections.singleton(viewTemplate2));
104:
105:                 assertFalse(provider.hasControlValidationTemplate());
106:         }
107:
108:         /**
109:          * Test that the {@link ViewTemplateProviderImpl} returns a merged view template containing all styles and
110:          * referenced ecores from all view templates provided by its suppliers.
111:          */
112:         @SuppressWarnings("deprecation")
113:         @Test
114:         public void getViewTemplate() {
115:                 final VTViewTemplate viewTemplate1 = VTTemplateFactory.eINSTANCE.createViewTemplate();
116:                 final VTViewTemplate viewTemplate2 = VTTemplateFactory.eINSTANCE.createViewTemplate();
117:                 when(supplier1.getViewTemplates()).thenReturn(Collections.singleton(viewTemplate1));
118:                 when(supplier2.getViewTemplates()).thenReturn(Collections.singleton(viewTemplate2));
119:
120:                 // Have one referenced ecore used by both to verify that the reference is not duplicated in the result
121:                 viewTemplate1.getReferencedEcores().add("http://test/one"); //$NON-NLS-1$
122:                 viewTemplate2.getReferencedEcores().add("http://test/one"); //$NON-NLS-1$
123:                 viewTemplate2.getReferencedEcores().add("http://test/two"); //$NON-NLS-1$
124:
125:                 final VTControlValidationTemplate validationTemplate = VTTemplateFactory.eINSTANCE
126:                         .createControlValidationTemplate();
127:                 viewTemplate2.setControlValidationConfiguration(validationTemplate);
128:
129:                 // Cannot mock styles because otherwise the style cannot be added to the new view template in the tested method
130:                 final VTStyle style1 = VTTemplateFactory.eINSTANCE.createStyle();
131:                 // Cannot mock because otherwise adding it to the style will fail
132:                 final VTAlignmentStyleProperty styleProperty1 = VTAlignmentFactory.eINSTANCE.createAlignmentStyleProperty();
133:                 styleProperty1.setType(AlignmentType.LEFT);
134:                 style1.getProperties().add(styleProperty1);
135:
136:                 final VTStyle style2 = VTTemplateFactory.eINSTANCE.createStyle();
137:                 final VTAlignmentStyleProperty styleProperty2 = VTAlignmentFactory.eINSTANCE.createAlignmentStyleProperty();
138:                 styleProperty2.setType(AlignmentType.RIGHT);
139:                 style2.getProperties().add(styleProperty2);
140:
141:                 viewTemplate1.getStyles().add(style1);
142:                 viewTemplate2.getStyles().add(style2);
143:
144:                 final VTViewTemplate result = provider.getViewTemplate();
145:
146:                 assertNotNull(result);
147:                 assertEquals("The merged view template should all styles of the supplied view templates.", 2, //$NON-NLS-1$
148:                         result.getStyles().size());
149:                 assertTrue(result.getStyles().contains(style1));
150:                 assertTrue(result.getStyles().contains(style2));
151:                 assertEquals(validationTemplate, result.getControlValidationConfiguration());
152:                 assertEquals("The merged view template should not contain duplicated referenced ecores.", 2, //$NON-NLS-1$
153:                         result.getReferencedEcores().size());
154:                 assertTrue(result.getReferencedEcores().contains("http://test/one")); //$NON-NLS-1$
155:                 assertTrue(result.getReferencedEcores().contains("http://test/two")); //$NON-NLS-1$
156:         }
157:
158:         @SuppressWarnings("deprecation")
159:         @Test
160:         public void getViewTemplate_NoSuppliers() {
161:                 provider.removeViewTemplateSupplier(supplier1);
162:                 provider.removeViewTemplateSupplier(supplier2);
163:
164:                 final VTViewTemplate viewTemplate = provider.getViewTemplate();
165:                 assertNotNull("The ViewTemplateProviderImpl should never " //$NON-NLS-1$
166:                         + "return null as a view template", viewTemplate); //$NON-NLS-1$
167:                 assertTrue("The View Template should be empty", viewTemplate.getStyles().isEmpty()); //$NON-NLS-1$
168:                 assertNull("The View Template should be empty", viewTemplate.getControlValidationConfiguration()); //$NON-NLS-1$
169:                 assertTrue("The View Template should be empty", viewTemplate.getReferencedEcores().isEmpty()); //$NON-NLS-1$
170:         }
171:
172:         @SuppressWarnings("deprecation")
173:         @Test
174:         public void getViewTemplate_NoSuppliedTemplates() {
175:                 when(supplier1.getViewTemplates()).thenReturn(Collections.<VTViewTemplate> emptySet());
176:                 when(supplier2.getViewTemplates()).thenReturn(Collections.<VTViewTemplate> emptySet());
177:
178:                 final VTViewTemplate viewTemplate = provider.getViewTemplate();
179:                 assertNotNull("The ViewTemplateProviderImpl should never " //$NON-NLS-1$
180:                         + "return null as a view template", viewTemplate); //$NON-NLS-1$
181:                 assertTrue("The View Template should be empty", viewTemplate.getStyles().isEmpty()); //$NON-NLS-1$
182:                 assertNull("The View Template should be empty", viewTemplate.getControlValidationConfiguration()); //$NON-NLS-1$
183:                 assertTrue("The View Template should be empty", viewTemplate.getReferencedEcores().isEmpty()); //$NON-NLS-1$
184:         }
185:
186:         /**
187:          * Test that a legacy {@link VTControlValidationTemplate} is properly converted to a global
188:          * {@link VTValidationStyleProperty}.
189:          */
190:         @SuppressWarnings("deprecation")
191:         @Test
192:         public void getStyleProperties_ControlValidationConfiguration() {
193:                 final VTViewTemplate viewTemplate1 = VTTemplateFactory.eINSTANCE.createViewTemplate();
194:                 when(supplier1.getViewTemplates()).thenReturn(Collections.singleton(viewTemplate1));
195:                 when(supplier1.getStyleProperties(any(VElement.class), any(ViewModelContext.class)))
196:                         .thenReturn(new LinkedHashMap<VTStyleProperty, Double>());
197:                 final VTControlValidationTemplate validationTemplate1 = VTTemplateFactory.eINSTANCE
198:                         .createControlValidationTemplate();
199:                 viewTemplate1.setControlValidationConfiguration(validationTemplate1);
200:                 validationTemplate1.setCancelColorHEX("#abcdef"); //$NON-NLS-1$
201:
202:                 final VTValidationStyleProperty validationProperty = VTValidationFactory.eINSTANCE
203:                         .createValidationStyleProperty();
204:                 when(supplier2.getViewTemplates()).thenReturn(Collections.<VTViewTemplate> emptySet());
205:                 final HashMap<VTStyleProperty, Double> styleMap = new HashMap<VTStyleProperty, Double>();
206:                 styleMap.put(validationProperty, 5d); // 5 equals the priority of a View Model Element selector
207:                 when(supplier2.getStyleProperties(any(VElement.class), any(ViewModelContext.class))).thenReturn(styleMap);
208:
209:                 final Set<VTStyleProperty> result = provider.getStyleProperties(mock(VElement.class),
210:                         mock(ViewModelContext.class));
211:
212:                 assertEquals(2, result.size());
213:                 final Iterator<VTStyleProperty> iterator = result.iterator();
214:                 // The configured validation style property must be returned before the generated one because generated
215:                 // properties should be less applicable than style properties defined in a view template
216:                 assertEquals(iterator.next(), validationProperty);
217:                 final VTStyleProperty generatedProperty = iterator.next();
218:                 assertEquals(VTValidationPackage.Literals.VALIDATION_STYLE_PROPERTY, generatedProperty.eClass());
219:                 final VTValidationStyleProperty generatedValidationProperty = (VTValidationStyleProperty) generatedProperty;
220:                 assertEquals("#abcdef", generatedValidationProperty.getCancelColorHEX()); //$NON-NLS-1$
221:                 // Sample check that the generated property's values are not set to something "random"
222:                 assertNull(generatedValidationProperty.getCancelForegroundColorHEX());
223:                 assertNull(generatedValidationProperty.getErrorColorHEX());
224:         }
225:
226:         /**
227:          * Tests various aspects of the getStyleProperties method:
228:          * <ul>
229:          * <li>Style Properties from all Suppliers are collected</li>
230:          * <li>For two applicable, <strong>equal</strong> styles with different specificities, the highest specificity is
231:          * used</li>
232:          * <li>For two applicable, <strong>unequal</strong> styles of the same type with equal specificities, a warning is
233:          * logged because there is no specified behaviour which one of the styles is ranked higher.</li>
234:          * <li>The result set is ordered by specificity in descending order</li>
235:          * </ul>
236:          */
237:         @Test
238:         public void getStyleProperties() {
239:                 final VTViewTemplate viewTemplate1 = VTTemplateFactory.eINSTANCE.createViewTemplate();
240:                 when(supplier1.getViewTemplates()).thenReturn(Collections.singleton(viewTemplate1));
241:                 final VTViewTemplate viewTemplate2 = VTTemplateFactory.eINSTANCE.createViewTemplate();
242:                 when(supplier2.getViewTemplates()).thenReturn(Collections.singleton(viewTemplate2));
243:
244:                 final Map<VTStyleProperty, Double> propertiesMap1 = new LinkedHashMap<VTStyleProperty, Double>();
245:                 when(supplier1.getStyleProperties(any(VElement.class), any(ViewModelContext.class))).thenReturn(propertiesMap1);
246:                 final Map<VTStyleProperty, Double> propertiesMap2 = new LinkedHashMap<VTStyleProperty, Double>();
247:                 when(supplier2.getStyleProperties(any(VElement.class), any(ViewModelContext.class))).thenReturn(propertiesMap2);
248:
249:                 final VTAlignmentStyleProperty alignmentLeft = VTAlignmentFactory.eINSTANCE.createAlignmentStyleProperty();
250:                 alignmentLeft.setType(AlignmentType.LEFT);
251:                 final VTValidationStyleProperty validationHigh = VTValidationFactory.eINSTANCE.createValidationStyleProperty();
252:                 propertiesMap1.put(alignmentLeft, 5d);
253:                 propertiesMap1.put(validationHigh, 20d);
254:
255:                 final VTValidationStyleProperty validationLow = VTValidationFactory.eINSTANCE.createValidationStyleProperty();
256:                 final VTMandatoryStyleProperty priorityIntermediate = VTMandatoryFactory.eINSTANCE
257:                         .createMandatoryStyleProperty();
258:                 final VTBackgroundStyleProperty priorityLowest = VTBackgroundFactory.eINSTANCE.createBackgroundStyleProperty();
259:                 final VTAlignmentStyleProperty alignmentRight = VTAlignmentFactory.eINSTANCE.createAlignmentStyleProperty();
260:                 alignmentRight.setType(AlignmentType.RIGHT);
261:                 propertiesMap2.put(validationLow, 2d);
262:                 propertiesMap2.put(priorityIntermediate, 7d);
263:                 propertiesMap2.put(alignmentRight, 5d);
264:                 propertiesMap2.put(priorityLowest, 0.1d);
265:
266:                 final Set<VTStyleProperty> result = provider.getStyleProperties(mock(VElement.class),
267:                         mock(ViewModelContext.class));
268:
269:                 assertEquals(5, result.size());
270:
271:                 final Iterator<VTStyleProperty> iterator = result.iterator();
272:                 assertSame("The result set must be sorted in descending order according to the properties' specificities.", //$NON-NLS-1$
273:                         validationHigh, iterator.next());
274:                 assertSame("The result set must be sorted in descending order according to the properties' specificities.", //$NON-NLS-1$
275:                         priorityIntermediate, iterator.next());
276:
277:                 // The conflicting properties are both present according to their specificities compared to the other
278:                 // properties. However, their order is not specified. Therefore, a warning is logged additionally.
279:                 final VTStyleProperty third = iterator.next();
280:                 final VTStyleProperty forth = iterator.next();
281:•                assertTrue(third == alignmentRight || third == alignmentLeft);
282:•                assertTrue(forth == alignmentRight || forth == alignmentLeft);
283:                 assertNotSame(third, forth);
284:                 verify(reportService).report(any(AbstractReport.class));
285:
286:                 assertSame(priorityLowest, iterator.next());
287:         }
288: }