Skip to content

Package: AbstractControl_PTest$TestObservableValue

AbstractControl_PTest$TestObservableValue

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2016 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 - adjustments for databinding tests
14: ******************************************************************************/
15: package org.eclipse.emfforms.swt.common.test;
16:
17: import static org.junit.Assert.assertEquals;
18: import static org.junit.Assert.assertTrue;
19: import static org.mockito.Matchers.any;
20: import static org.mockito.Mockito.mock;
21: import static org.mockito.Mockito.reset;
22: import static org.mockito.Mockito.when;
23:
24: import java.util.Map;
25:
26: import org.eclipse.core.databinding.observable.IObserving;
27: import org.eclipse.core.databinding.observable.Observables;
28: import org.eclipse.core.databinding.observable.value.IObservableValue;
29: import org.eclipse.core.databinding.property.value.IValueProperty;
30: import org.eclipse.emf.common.command.BasicCommandStack;
31: import org.eclipse.emf.common.util.URI;
32: import org.eclipse.emf.ecore.EObject;
33: import org.eclipse.emf.ecore.EStructuralFeature;
34: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
35: import org.eclipse.emf.ecore.EcorePackage;
36: import org.eclipse.emf.ecore.resource.Resource;
37: import org.eclipse.emf.ecore.resource.ResourceSet;
38: import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl;
39: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
40: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
41: import org.eclipse.emf.ecp.view.spi.core.swt.AbstractControlSWTRenderer;
42: import org.eclipse.emf.ecp.view.spi.model.LabelAlignment;
43: import org.eclipse.emf.ecp.view.spi.model.VControl;
44: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
45: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
46: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
47: import org.eclipse.emf.ecp.view.template.model.VTViewTemplateProvider;
48: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
49: import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
50: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
51: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
52: import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
53: import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
54: import org.eclipse.emfforms.spi.swt.core.layout.GridDescriptionFactory;
55: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
56: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
57: import org.eclipse.swt.SWT;
58: import org.eclipse.swt.widgets.Control;
59: import org.eclipse.swt.widgets.Display;
60: import org.eclipse.swt.widgets.Label;
61: import org.eclipse.swt.widgets.Shell;
62: import org.junit.Test;
63: import org.mockito.Mockito;
64:
65: public abstract class AbstractControl_PTest<C extends VControl> {
66:
67:         protected static final String CUSTOM_VARIANT = "org.eclipse.rap.rwt.customVariant"; //$NON-NLS-1$
68:
69:         protected static final String UUID = "org.eclipse.emfforms.swt.common.test.AbstractControl_PTest.UUID";
70:
71:         private EMFFormsDatabinding databindingService;
72:         private VTViewTemplateProvider templateProvider;
73:         private AbstractControlSWTRenderer<C> renderer;
74:         private EMFFormsLabelProvider labelProvider;
75:
76:         private EObject eObject;
77:
78:         protected Resource createResource() {
79:                 final Resource.Factory.Registry registry = Resource.Factory.Registry.INSTANCE;
80:                 final Map<String, Object> extToFactoryMap = registry
81:                         .getExtensionToFactoryMap();
82:                 extToFactoryMap.put(Resource.Factory.Registry.DEFAULT_EXTENSION,
83:                         new ResourceFactoryImpl());
84:                 final ResourceSet resourceSet = new ResourceSetImpl();
85:                 resourceSet.getPackageRegistry().put(EcorePackage.eNS_URI,
86:                         EcorePackage.eINSTANCE);
87:
88:                 final AdapterFactoryEditingDomain domain = new AdapterFactoryEditingDomain(
89:                         new ComposedAdapterFactory(
90:                                 ComposedAdapterFactory.Descriptor.Registry.INSTANCE),
91:                         new BasicCommandStack(), resourceSet);
92:                 resourceSet.eAdapters().add(
93:                         new AdapterFactoryEditingDomain.EditingDomainProvider(domain));
94:                 final Resource resource = resourceSet
95:                         .createResource(URI.createURI("VIRTUAL"));
96:                 return resource;
97:
98:         }
99:
100:         protected void mockControl(EObject eObject,
101:                 final EStructuralFeature eStructuralFeature) throws DatabindingFailedException {
102:                 this.eObject = eObject;
103:                 final VDomainModelReference domainModelReference = Mockito
104:                         .mock(VDomainModelReference.class);
105:                 final Setting setting = mock(Setting.class);
106:                 final Resource resource = createResource();
107:                 resource.getContents().add(eObject);
108:
109:                 when(setting.getEObject()).thenReturn(eObject);
110:                 when(setting.getEStructuralFeature()).thenReturn(eStructuralFeature);
111:
112:                 mockDatabindingIsSettableAndChangeable();
113:
114:                 Mockito.when(getvControl().getDomainModelReference()).thenReturn(
115:                         domainModelReference);
116:
117:                 Mockito.doReturn(UUID).when(getvControl()).getUuid();
118:         }
119:
120:         protected void mockDatabindingIsSettableAndChangeable() throws DatabindingFailedException {
121:                 final EStructuralFeature structuralFeature = mock(EStructuralFeature.class);
122:                 when(structuralFeature.isUnsettable()).thenReturn(false);
123:                 when(structuralFeature.isChangeable()).thenReturn(true);
124:                 final IValueProperty valueProperty = mock(IValueProperty.class);
125:                 when(valueProperty.getValueType()).thenReturn(structuralFeature);
126:                 when(getDatabindingService().getValueProperty(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
127:                         valueProperty);
128:         }
129:
130:         protected void setMockLabelAlignment(LabelAlignment labelAlignment) {
131:                 Mockito.when(getvControl().getLabelAlignment()).thenReturn(labelAlignment);
132:         }
133:
134:         private ViewModelContext context;
135:         private C vControl;
136:         private Shell shell;
137:
138:         @SuppressWarnings("unchecked")
139:         protected void setup() throws DatabindingFailedException {
140:                 setvControl((C) Mockito.mock(VControl.class));
141:                 mockControl();
142:                 setContext(Mockito.mock(ViewModelContext.class));
143:                 when(getContext().getDomainModel()).thenReturn(eObject);
144:                 setShell(new Shell(Display.getDefault(), SWT.NONE));
145:         }
146:
147:         protected void dispose() {
148:                 getShell().dispose();
149:         }
150:
151:         @Test
152:         public void testGridDescriptionLabelAlignmentNone() {
153:                 setMockLabelAlignment(LabelAlignment.NONE);
154:                 final SWTGridDescription gridDescription = getRenderer().getGridDescription(GridDescriptionFactory.INSTANCE
155:                         .createEmptyGridDescription());
156:                 assertEquals(2, gridDescription.getColumns());
157:                 assertEquals(1, gridDescription.getRows());
158:         }
159:
160:         @Test
161:         public void testGridDescriptionLabelAlignmentLeft() {
162:                 setMockLabelAlignment(LabelAlignment.LEFT);
163:                 final SWTGridDescription gridDescription = getRenderer().getGridDescription(GridDescriptionFactory.INSTANCE
164:                         .createEmptyGridDescription());
165:                 assertEquals(3, gridDescription.getColumns());
166:                 assertEquals(1, gridDescription.getRows());
167:         }
168:
169:         @Test
170:         public void renderValidationIconLabelAlignmentNone()
171:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
172:                 setMockLabelAlignment(LabelAlignment.NONE);
173:                 renderValidationIcon(new SWTGridCell(0, 0, getRenderer()));
174:         }
175:
176:         @Test
177:         public void renderValidationIconLabelAlignmentLeft()
178:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
179:                 setMockLabelAlignment(LabelAlignment.LEFT);
180:                 renderValidationIcon(new SWTGridCell(0, 1, getRenderer()));
181:         }
182:
183:         private void renderValidationIcon(SWTGridCell gridCell)
184:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
185:                 final Control render = getRenderer().render(gridCell, getShell());
186:                 assertTrue(Label.class.isInstance(render));
187:                 assertEquals("", Label.class.cast(render).getText());
188:         }
189:
190:         protected void renderLabel(String text) throws NoRendererFoundException,
191:                 NoPropertyDescriptorFoundExeption {
192:                 setMockLabelAlignment(LabelAlignment.LEFT);
193:                 final Control render = getRenderer().render(new SWTGridCell(0, 0, getRenderer()), getShell());
194:                 assertTrue(Label.class.isInstance(render));
195:                 assertEquals(text, Label.class.cast(render).getText());
196:         }
197:
198:         /**
199:          * Tests whether the {@link EMFFormsLabelProvider} is used to get the labels of a control.
200:          *
201:          * @throws NoRendererFoundException
202:          * @throws NoPropertyDescriptorFoundExeption
203:          * @throws NoLabelFoundException
204:          */
205:         protected void labelServiceUsage() throws NoRendererFoundException, NoPropertyDescriptorFoundExeption,
206:                 NoLabelFoundException {
207:                 reset(getLabelProvider());
208:                 final IObservableValue testDescription = Observables.constantObservableValue("test-description", String.class);
209:                 final IObservableValue testDisplayName = Observables.constantObservableValue("test-displayname", String.class);
210:                 when(getLabelProvider().getDescription(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
211:                         testDescription);
212:                 when(getLabelProvider().getDisplayName(any(VDomainModelReference.class), any(EObject.class))).thenReturn(
213:                         testDisplayName);
214:
215:                 setMockLabelAlignment(LabelAlignment.LEFT);
216:                 final Control renderControl = renderControl(new SWTGridCell(0, 0, getRenderer()));
217:                 assertTrue(Label.class.isInstance(renderControl));
218:
219:                 final Label label = (Label) renderControl;
220:                 assertEquals(testDescription.getValue(), label.getToolTipText());
221:                 assertEquals(testDisplayName.getValue(), label.getText());
222:         }
223:
224:         protected Control renderControl(SWTGridCell gridCell)
225:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
226:                 final Control render = getRenderer().render(gridCell, getShell());
227:                 return render;
228:         }
229:
230:         protected abstract void mockControl() throws DatabindingFailedException;
231:
232:         /**
233:          * @return the context
234:          */
235:         public ViewModelContext getContext() {
236:                 return context;
237:         }
238:
239:         /**
240:          * @param context the context to set
241:          */
242:         public void setContext(ViewModelContext context) {
243:                 this.context = context;
244:         }
245:
246:         /**
247:          * @return the VControl
248:          */
249:         public C getvControl() {
250:                 return vControl;
251:         }
252:
253:         /**
254:          * @param vControl the VControl to set
255:          */
256:         public void setvControl(C vControl) {
257:                 this.vControl = vControl;
258:         }
259:
260:         /**
261:          * @return the shell
262:          */
263:         public Shell getShell() {
264:                 return shell;
265:         }
266:
267:         /**
268:          * @param shell the shell to set
269:          */
270:         public void setShell(Shell shell) {
271:                 this.shell = shell;
272:         }
273:
274:         /**
275:          * @return the databindingService
276:          */
277:         public EMFFormsDatabinding getDatabindingService() {
278:                 return databindingService;
279:         }
280:
281:         /**
282:          * @param databindingService the databindingService to set
283:          */
284:         public void setDatabindingService(EMFFormsDatabinding databindingService) {
285:                 this.databindingService = databindingService;
286:         }
287:
288:         /**
289:          * @return the templateProvider
290:          */
291:         public VTViewTemplateProvider getTemplateProvider() {
292:                 return templateProvider;
293:         }
294:
295:         /**
296:          * @param templateProvider the templateProvider to set
297:          */
298:         public void setTemplateProvider(VTViewTemplateProvider templateProvider) {
299:                 this.templateProvider = templateProvider;
300:         }
301:
302:         /**
303:          * @return the renderer
304:          */
305:         public AbstractControlSWTRenderer<C> getRenderer() {
306:                 return renderer;
307:         }
308:
309:         /**
310:          * @param renderer the renderer to set
311:          */
312:         public void setRenderer(AbstractControlSWTRenderer<C> renderer) {
313:                 this.renderer = renderer;
314:         }
315:
316:         /**
317:          * @return the labelProvider
318:          */
319:         public EMFFormsLabelProvider getLabelProvider() {
320:                 return labelProvider;
321:         }
322:
323:         /**
324:          * @param labelProvider the labelProvider to set
325:          */
326:         public void setLabelProvider(EMFFormsLabelProvider labelProvider) {
327:                 this.labelProvider = labelProvider;
328:         }
329:
330:         /**
331:          * Helper Interface for mocking.
332:          *
333:          * @author Eugen Neufeld
334:          *
335:          */
336:         public interface TestObservableValue extends IObservableValue, IObserving {
337:         }
338: }