Skip to content

Package: SWTViewTestHelper$1

SWTViewTestHelper$1

nameinstructionbranchcomplexitylinemethod
getControl()
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%
getRenderer()
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%
{...}
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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: * Jonas Helming - initial API and implementation
13: * Christian W. Damus - bug 548592
14: */
15: package org.eclipse.emf.ecp.view.test.common.swt.spi;
16:
17: import static org.junit.Assert.fail;
18:
19: import java.util.ArrayList;
20: import java.util.List;
21:
22: import org.eclipse.emf.ecore.EObject;
23: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
24: import org.eclipse.emf.ecp.view.spi.context.ViewModelContextFactory;
25: import org.eclipse.emf.ecp.view.spi.model.VElement;
26: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
27: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
28: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
29: import org.eclipse.emfforms.common.Optional;
30: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
31: import org.eclipse.emfforms.spi.swt.core.EMFFormsNoRendererException;
32: import org.eclipse.emfforms.spi.swt.core.EMFFormsRendererFactory;
33: import org.eclipse.emfforms.spi.swt.core.layout.GridDescriptionFactory;
34: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
35: import org.eclipse.swt.layout.FillLayout;
36: import org.eclipse.swt.layout.GridData;
37: import org.eclipse.swt.layout.GridLayout;
38: import org.eclipse.swt.widgets.Composite;
39: import org.eclipse.swt.widgets.Control;
40: import org.eclipse.swt.widgets.Display;
41: import org.eclipse.swt.widgets.Shell;
42: import org.eclipse.swt.widgets.Text;
43: import org.osgi.framework.BundleContext;
44: import org.osgi.framework.FrameworkUtil;
45: import org.osgi.framework.ServiceReference;
46:
47: /**
48: * Helper Class for SWT Tests.
49: *
50: * @author Jonas Helming
51: *
52: */
53: public final class SWTViewTestHelper {
54:         // private static SWTRendererFactory factory = new SWTRendererFactoryImpl();
55:         private static EMFFormsRendererFactory factory;
56:
57:         /**
58:          * Helper interface to return the renderer along with the rendered control.
59:          */
60:         public interface RendererResult {
61:
62:                 /**
63:                  * Get the renderer which was used to render the control.
64:                  *
65:                  * @return the renderer
66:                  */
67:                 AbstractSWTRenderer<VElement> getRenderer();
68:
69:                 /**
70:                  * Get the control which was rendered (if any).
71:                  *
72:                  * @return the control
73:                  */
74:                 Optional<Control> getControl();
75:
76:         }
77:
78:         static {
79:                 final BundleContext bundleContext = FrameworkUtil.getBundle(SWTViewTestHelper.class).getBundleContext();
80:                 final ServiceReference<EMFFormsRendererFactory> serviceReference = bundleContext
81:                         .getServiceReference(EMFFormsRendererFactory.class);
82:                 factory = bundleContext.getService(serviceReference);
83:         }
84:
85:         private SWTViewTestHelper() {
86:
87:         }
88:
89:         /**
90:          *
91:          * @return a new {@link Shell} with a {@link FillLayout}
92:          */
93:         public static Shell createShell() {
94:                 final Display display = Display.getDefault();
95:                 final Shell shell = new Shell(display);
96:                 shell.setLayout(new FillLayout());
97:                 return shell;
98:         }
99:
100:         /**
101:          * Renders the given {@link VElement} on the given {@link Shell}. The method will create a dummy domain model object
102:          * as an input.
103:          *
104:          * @param renderable the {@link VElement} to be rendered
105:          * @param shell The {@link Shell} to render on
106:          * @return the rendered {@link Control}
107:          * @throws NoRendererFoundException If a required sub renderer is not found
108:          * @throws NoPropertyDescriptorFoundExeption If no PropertyDescriptor was found for the domain model instance
109:          * @throws EMFFormsNoRendererException If the renderer for the given {@link VElement} is not found
110:          */
111:         public static Control render(VElement renderable, Shell shell) throws NoRendererFoundException,
112:                 NoPropertyDescriptorFoundExeption, EMFFormsNoRendererException {
113:                 return render(renderable, VViewFactory.eINSTANCE.createView(), shell);
114:         }
115:
116:         /**
117:          * Renders the given {@link VElement} on the given {@link Shell} and uses the given {@link EObject} as an input.
118:          *
119:          * @param renderable the {@link VElement} to be rendered
120:          * @param input The input {@link EObject} (domain model instance)
121:          * @param shell The {@link Shell} to render on
122:          * @return the rendered {@link Control}
123:          * @throws NoRendererFoundException If a required sub renderer is not found
124:          * @throws NoPropertyDescriptorFoundExeption If no PropertyDescriptor was found for the domain model instance
125:          * @throws EMFFormsNoRendererException If the renderer for the given {@link VElement} is not found
126:          */
127:         public static Control render(VElement renderable, EObject input, Shell shell) throws NoRendererFoundException,
128:                 NoPropertyDescriptorFoundExeption, EMFFormsNoRendererException {
129:
130:                 final RendererResult result = renderControl(renderable, input, shell);
131:                 if (result.getControl().isPresent()) {
132:                         return result.getControl().get();
133:                 }
134:                 return null;
135:         }
136:
137:         /**
138:          * Renders the given {@link VElement} on the given {@link Shell} and uses the given {@link EObject} as an input.
139:          *
140:          * @param renderable the {@link VElement} to be rendered
141:          * @param input The input {@link EObject} (domain model instance)
142:          * @param shell The {@link Shell} to render on
143:          * @return a {@link RendererResult}
144:          * @throws NoRendererFoundException If a required sub renderer is not found
145:          * @throws NoPropertyDescriptorFoundExeption If no PropertyDescriptor was found for the domain model instance
146:          * @throws EMFFormsNoRendererException If the renderer for the given {@link VElement} is not found
147:          */
148:         public static RendererResult renderControl(VElement renderable, EObject input, Shell shell)
149:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption, EMFFormsNoRendererException {
150:
151:                 final ViewModelContext viewContext = ViewModelContextFactory.INSTANCE.createViewModelContext(renderable, input);
152:                 return renderControl(viewContext, shell);
153:         }
154:
155:         /**
156:          * Render the given {@code context} on the given {@code composite}.
157:          *
158:          * @param context view-model context to render
159:          * @param composite the composite to render on
160:          * @return a {@link RendererResult}
161:          *
162:          * @throws NoRendererFoundException If a required sub renderer is not found
163:          * @throws NoPropertyDescriptorFoundExeption If no PropertyDescriptor was found for the domain model instance
164:          * @throws EMFFormsNoRendererException If the renderer for the given {@link VElement} is not found
165:          *
166:          * @since 1.22
167:          */
168:         public static RendererResult renderControl(ViewModelContext context, Composite composite)
169:                 throws NoRendererFoundException, NoPropertyDescriptorFoundExeption, EMFFormsNoRendererException {
170:
171:                 final AbstractSWTRenderer<VElement> renderer = factory
172:                         .getRendererInstance(context.getViewModel(), context);
173:                 final SWTGridDescription gridDescription = renderer.getGridDescription(GridDescriptionFactory.INSTANCE
174:                         .createEmptyGridDescription());
175:                 final Control control = renderer.render(gridDescription.getGrid().get(gridDescription.getColumns() - 1),
176:                         composite);
177:                 renderer.finalizeRendering(composite);
178:
179:                 return new RendererResult() {
180:                         @Override
181:                         public AbstractSWTRenderer<VElement> getRenderer() {
182:                                 return renderer;
183:                         }
184:
185:                         @Override
186:                         public Optional<Control> getControl() {
187:                                 return Optional.ofNullable(control);
188:                         }
189:                 };
190:         }
191:
192:         /**
193:          * Render the given {@code context} on the given {@code composite}.
194:          *
195:          * @param context view-model context to render
196:          * @param composite the composite to render on
197:          * @return the rendered SWT control
198:          *
199:          * @since 1.22
200:          */
201:         public static Control render(ViewModelContext context, Composite composite) {
202:                 try {
203:                         final Optional<Control> result = renderControl(context, composite).getControl();
204:                         return result.isPresent()
205:                                 ? result.get()
206:                                 : null;
207:                 } catch (NoRendererFoundException | NoPropertyDescriptorFoundExeption | EMFFormsNoRendererException e) {
208:                         e.printStackTrace();
209:                         fail("Failed to render: " + e.getMessage());
210:                         return null; // Unreachable
211:                 }
212:         }
213:
214:         /**
215:          *
216:          * @param composite a {@link Composite} with a {@link GridLayout}
217:          * @return the number of columns
218:          */
219:         public static int getNumberofColumns(Composite composite) {
220:                 final GridLayout gridLayout = (GridLayout) composite.getLayout();
221:                 return gridLayout.numColumns;
222:         }
223:
224:         /**
225:          *
226:          * @param control a control with a Horitontal Span specified
227:          * @return the horizontal span
228:          */
229:         public static int getHorizontalSpan(Control control) {
230:                 final GridData gridData = (GridData) control.getLayoutData();
231:                 return gridData.horizontalSpan;
232:         }
233:
234:         /**
235:          * Checks whether there is a text control.
236:          *
237:          * @param control the control to check its children
238:          * @return if there is a text control
239:          */
240:         public static boolean checkIfThereIsATextControlWithLabel(Control control) {
241:                 if (!(control instanceof Composite)) {
242:                         return false;
243:                 }
244:                 final Composite controlComposite = (Composite) control;
245:
246:                 return checkIfThereIsATextControl(controlComposite.getChildren()[2]);
247:         }
248:
249:         /**
250:          * Checks whether there is at least one text control in the children of a control (recursivly). The method will only
251:          * check the first child on every level.
252:          *
253:          * @param control control the control to check its children
254:          * @return if there is a text control
255:          */
256:         public static boolean checkIfThereIsATextControl(Control control) {
257:                 if (Text.class.isInstance(control)) {
258:                         return true;
259:                 } else if (Composite.class.isInstance(control)) {
260:                         return checkIfThereIsATextControl(Composite.class.cast(control).getChildren()[0]);
261:                 }
262:                 return false;
263:         }
264:
265:         /**
266:          * Retrieve all text controls, which are children on a given control.
267:          *
268:          * @param control the control to check its children
269:          * @return a {@link List} of {@link Text}
270:          */
271:         public static List<Text> getAllTextControls(Control control) {
272:                 final Composite controlComposite = (Composite) control;
273:                 final List<Text> textFields = new ArrayList<Text>();
274:                 for (final Control textControl : controlComposite.getChildren()) {
275:                         if (textControl instanceof Text) {
276:                                 textFields.add((Text) textControl);
277:                         } else if (textControl instanceof Composite) {
278:                                 textFields.addAll(getAllTextControls(textControl));
279:                         }
280:                 }
281:                 return textFields;
282:         }
283: }