Skip to content

Package: RendererSupplier_PTest$MyRenderer

RendererSupplier_PTest$MyRenderer

nameinstructionbranchcomplexitylinemethod
RendererSupplier_PTest.MyRenderer(VControl, ViewModelContext)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getGridDescription(SWTGridDescription)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
renderControl(SWTGridCell, Composite)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2019 Christian W. Damus 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: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.swt.core.di;
15:
16: import static org.hamcrest.CoreMatchers.is;
17: import static org.hamcrest.MatcherAssert.assertThat;
18: import static org.junit.Assert.fail;
19: import static org.mockito.Mockito.mock;
20: import static org.mockito.Mockito.when;
21:
22: import java.util.function.Supplier;
23:
24: import org.eclipse.e4.core.contexts.ContextInjectionFactory;
25: import org.eclipse.e4.core.contexts.EclipseContextFactory;
26: import org.eclipse.e4.core.contexts.IEclipseContext;
27: import org.eclipse.e4.core.di.InjectionException;
28: import org.eclipse.e4.core.di.annotations.Execute;
29: import org.eclipse.emf.common.notify.impl.BasicNotifierImpl.EAdapterList;
30: import org.eclipse.emf.ecp.view.model.common.di.annotations.Renderer;
31: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
32: import org.eclipse.emf.ecp.view.spi.model.VControl;
33: import org.eclipse.emf.ecp.view.spi.model.VElement;
34: import org.eclipse.emf.ecp.view.spi.renderer.NoPropertyDescriptorFoundExeption;
35: import org.eclipse.emf.ecp.view.spi.renderer.NoRendererFoundException;
36: import org.eclipse.emfforms.internal.swt.core.di.RendererSupplier;
37: import org.eclipse.emfforms.spi.common.report.ReportService;
38: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewContext;
39: import org.eclipse.emfforms.spi.swt.core.AbstractSWTRenderer;
40: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridCell;
41: import org.eclipse.emfforms.spi.swt.core.layout.SWTGridDescription;
42: import org.eclipse.swt.widgets.Composite;
43: import org.eclipse.swt.widgets.Control;
44: import org.junit.After;
45: import org.junit.Before;
46: import org.junit.Test;
47: import org.osgi.framework.Bundle;
48: import org.osgi.framework.FrameworkUtil;
49:
50: /**
51: * Test cases for the {@link RendererSupplier}.
52: */
53: @SuppressWarnings({ "restriction", "nls" })
54: public class RendererSupplier_PTest {
55:
56:         private IEclipseContext e4Context;
57:
58:         /**
59:          * Initializes me.
60:          */
61:         public RendererSupplier_PTest() {
62:                 super();
63:         }
64:
65:         @Test
66:         public void injection() {
67:                 final ViewModelContext viewContext = mock(ViewModelContext.class);
68:                 final ReportService reportService = mock(ReportService.class);
69:                 when(viewContext.getService(ReportService.class)).thenReturn(reportService);
70:                 final VControl control = mock(VControl.class);
71:                 when(control.eAdapters()).thenReturn(new EAdapterList<>(control));
72:
73:                 final AbstractSWTRenderer<VControl> renderer = new MyRenderer(control, viewContext);
74:
75:                 final InjectMe injectMe = new InjectMe();
76:
77:                 try {
78:                         ContextInjectionFactory.invoke(injectMe, Execute.class, e4Context);
79:                         fail("Should have failed to inject");
80:                 } catch (final InjectionException e) {
81:                         // Success
82:                 }
83:
84:                 // Now put the prerequisites into the context
85:                 e4Context.set(VElement.class, control);
86:                 // Note that this one requires the context function that casts to ViewModelContext type
87:                 e4Context.set(EMFFormsViewContext.class, viewContext);
88:
89:                 try {
90:                         final String returnResult = (String) ContextInjectionFactory.invoke(injectMe, Execute.class, e4Context);
91:                         assertThat(returnResult, is("success"));
92:                         assertThat(injectMe.get(), is(renderer));
93:                 } catch (final InjectionException e) {
94:                         e.printStackTrace();
95:                         fail("Failed to inject: " + e.getMessage());
96:                 }
97:         }
98:
99:         //
100:         // Test framework
101:         //
102:
103:         @Before
104:         public void createContext() {
105:                 final Bundle self = FrameworkUtil.getBundle(RendererSupplier_PTest.class);
106:                 e4Context = EclipseContextFactory.createServiceContext(self.getBundleContext());
107:         }
108:
109:         @After
110:         public void destroyContext() {
111:                 e4Context.dispose();
112:         }
113:
114:         //
115:         // Nested types
116:         //
117:
118:         static class InjectMe implements Supplier<Object> {
119:                 private Object injected;
120:
121:                 @Execute
122:                 public String call(@Renderer MyRenderer renderer) {
123:                         injected = renderer;
124:                         return "success";
125:                 }
126:
127:                 @Override
128:                 public Object get() {
129:                         return injected;
130:                 }
131:         }
132:
133:         static class MyRenderer extends AbstractSWTRenderer<VControl> {
134:
135:                 MyRenderer(VControl control, ViewModelContext viewContext) {
136:                         super(control, viewContext, viewContext.getService(ReportService.class));
137:                 }
138:
139:                 @Override
140:                 protected Control renderControl(SWTGridCell cell, Composite parent)
141:                         throws NoRendererFoundException, NoPropertyDescriptorFoundExeption {
142:                         return null;
143:                 }
144:
145:                 @Override
146:                 public SWTGridDescription getGridDescription(SWTGridDescription gridDescription) {
147:                         return null;
148:                 }
149:
150:         }
151:
152: }