Skip to content

Package: TableDetailRevealProvider_PTest

TableDetailRevealProvider_PTest

nameinstructionbranchcomplexitylinemethod
TableDetailRevealProvider_PTest()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
createDomainModel()
M: 0 C: 210
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 33
100%
M: 0 C: 1
100%
createShell()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createViewModel()
M: 0 C: 54
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
destroyShell()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$0(InvocationOnMock)
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
mockChildContexts()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
render()
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%
revealInTableInTableDetail()
M: 0 C: 61
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 12
100%
M: 0 C: 1
100%

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.emf.ecp.view.internal.table.swt;
15:
16: import static org.eclipse.emf.ecp.view.test.common.spi.EMFMocking.eSetContainer;
17: import static org.hamcrest.CoreMatchers.is;
18: import static org.junit.Assert.assertThat;
19: import static org.mockito.Matchers.any;
20: import static org.mockito.Matchers.anyBoolean;
21: import static org.mockito.Matchers.anyVararg;
22: import static org.mockito.Mockito.mock;
23: import static org.mockito.Mockito.verify;
24: import static org.mockito.Mockito.when;
25:
26: import org.eclipse.emf.common.util.ECollections;
27: import org.eclipse.emf.common.util.EList;
28: import org.eclipse.emf.ecore.EAttribute;
29: import org.eclipse.emf.ecore.EClass;
30: import org.eclipse.emf.ecore.EClassifier;
31: import org.eclipse.emf.ecore.EObject;
32: import org.eclipse.emf.ecore.EPackage;
33: import org.eclipse.emf.ecore.EReference;
34: import org.eclipse.emf.ecore.EStructuralFeature;
35: import org.eclipse.emf.ecore.EcorePackage;
36: import org.eclipse.emf.ecore.InternalEObject;
37: import org.eclipse.emf.ecp.test.common.DefaultRealm;
38: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
39: import org.eclipse.emf.ecp.view.spi.context.ViewModelService;
40: import org.eclipse.emf.ecp.view.spi.model.VElement;
41: import org.eclipse.emf.ecp.view.spi.model.VView;
42: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
43: import org.eclipse.emf.ecp.view.spi.table.model.DetailEditing;
44: import org.eclipse.emf.ecp.view.spi.table.model.VTableControl;
45: import org.eclipse.emf.ecp.view.table.test.common.TableControlHandle;
46: import org.eclipse.emf.ecp.view.table.test.common.TableTestUtil;
47: import org.eclipse.emf.ecp.view.test.common.spi.EMFFormsRevealServiceFixture;
48: import org.eclipse.emf.ecp.view.test.common.spi.EMFFormsViewContextFixture.DomainModel;
49: import org.eclipse.emf.ecp.view.test.common.spi.EMFFormsViewContextFixture.ViewModel;
50: import org.eclipse.emf.ecp.view.test.common.spi.EMFMockingRunner;
51: import org.eclipse.emf.ecp.view.test.common.spi.EMock;
52: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
53: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTViewTestHelper;
54: import org.eclipse.emfforms.bazaar.Bid;
55: import org.eclipse.emfforms.bazaar.Create;
56: import org.eclipse.emfforms.spi.core.services.reveal.EMFFormsRevealProvider;
57: import org.eclipse.emfforms.spi.core.services.reveal.Reveal;
58: import org.eclipse.emfforms.spi.core.services.reveal.RevealHelper;
59: import org.eclipse.emfforms.spi.core.services.reveal.RevealStep;
60: import org.eclipse.swt.widgets.Shell;
61: import org.eclipse.swt.widgets.Table;
62: import org.junit.After;
63: import org.junit.Before;
64: import org.junit.Rule;
65: import org.junit.Test;
66: import org.junit.rules.TestRule;
67: import org.junit.runner.RunWith;
68:
69: /**
70: * Tests covering the {@link TableDetailRevealProvider} class.
71: */
72: @RunWith(EMFMockingRunner.class)
73: public class TableDetailRevealProvider_PTest {
74:
75:         @ViewModel
76:         private final VView viewModel = VViewFactory.eINSTANCE.createView();
77:
78:         @EMock
79:         @DomainModel
80:         private EPackage rootObject;
81:
82:         @EMock
83:         private EClass class1;
84:
85:         @EMock
86:         private EClass class2;
87:
88:         @EMock
89:         private EReference obj1;
90:
91:         @EMock
92:         private EAttribute obj2;
93:
94:         private VTableControl masterTable;
95:
96:         private final VView detailView = VViewFactory.eINSTANCE.createView();
97:
98:         private VTableControl detailTable;
99:
100:         @Rule
101:         public final TestRule realm = DefaultRealm.rule();
102:
103:         @Rule
104:         public final EMFFormsRevealServiceFixture<ViewModelContext> fixture = EMFFormsRevealServiceFixture.create(
105:                 ViewModelContext.class, this);
106:
107:         private Shell shell;
108:
109:         /**
110:          * Initializes me.
111:          */
112:         public TableDetailRevealProvider_PTest() {
113:                 super();
114:         }
115:
116:         @Test
117:         public void revealInTableInTableDetail() {
118:                 final Runnable masterReveal = mock(Runnable.class);
119:                 final Runnable detailReveal = mock(Runnable.class);
120:
121:                 fixture.addRevealProvider(new ViewRevealer(viewModel, masterReveal));
122:                 fixture.addRevealProvider(new ViewRevealer(detailView, detailReveal));
123:
124:                 render();
125:
126:                 fixture.reveal(obj2);
127:
128:                 SWTTestUtil.waitForUIThread();
129:
130:                 // We revealed the root view and the detail view
131:                 verify(masterReveal).run();
132:                 verify(detailReveal).run();
133:
134:                 // The second table is the detail
135:                 final Table table = SWTTestUtil.findControl(shell, 1, Table.class);
136:                 assertThat("Table selection incorrect", table.getSelectionIndex(), is(1));
137:         }
138:
139:         //
140:         // Test framework
141:         //
142:
143:         @Before
144:         public void createViewModel() {
145:                 final TableControlHandle masterHandle = TableTestUtil
146:                         .createInitializedTableWithoutTableColumns(EcorePackage.Literals.EPACKAGE__ECLASSIFIERS);
147:
148:                 masterHandle.addFirstTableColumn(TableTestUtil.createTableColumn(EcorePackage.Literals.ENAMED_ELEMENT__NAME));
149:                 masterHandle.addSecondTableColumn(
150:                         TableTestUtil.createTableColumn(EcorePackage.Literals.ECLASSIFIER__INSTANCE_CLASS_NAME));
151:
152:                 masterTable = masterHandle.getTableControl();
153:                 masterTable.setDetailEditing(DetailEditing.WITH_PANEL);
154:                 viewModel.getChildren().add(masterTable);
155:
156:                 final TableControlHandle detailHandle = TableTestUtil
157:                         .createInitializedTableWithoutTableColumns(EcorePackage.Literals.ECLASS__ESTRUCTURAL_FEATURES);
158:
159:                 detailHandle.addFirstTableColumn(TableTestUtil.createTableColumn(EcorePackage.Literals.ENAMED_ELEMENT__NAME));
160:                 detailHandle.addSecondTableColumn(TableTestUtil.createTableColumn(EcorePackage.Literals.ETYPED_ELEMENT__MANY));
161:
162:                 detailTable = detailHandle.getTableControl();
163:                 detailView.getChildren().add(detailTable);
164:
165:                 masterTable.setDetailView(detailView);
166:         }
167:
168:         @Before
169:         public void createDomainModel() {
170:                 final EList<EClassifier> classifiers = ECollections.asEList(class1, class2);
171:                 when(rootObject.getEClassifiers()).thenReturn(classifiers);
172:                 when(rootObject.eGet(EcorePackage.Literals.EPACKAGE__ECLASSIFIERS)).thenReturn(classifiers);
173:
174:                 when(class1.getName()).thenReturn("Class1");
175:                 eSetContainer(class1, rootObject);
176:                 when(class2.getName()).thenReturn("Class2");
177:                 eSetContainer(class2, rootObject);
178:                 final EList<EStructuralFeature> features = ECollections.asEList(obj1, obj2);
179:                 when(class2.getEStructuralFeatures()).thenReturn(features);
180:                 when(class2.eGet(EcorePackage.Literals.ECLASS__ESTRUCTURAL_FEATURES)).thenReturn(features);
181:                 when(obj1.getName()).thenReturn("ref1");
182:                 eSetContainer(obj1, class2);
183:                 when(obj2.getName()).thenReturn("att1");
184:                 eSetContainer(obj2, class2);
185:
186:                 // These are needed by the data binding service
187:                 EStructuralFeature.Setting setting = mock(EStructuralFeature.Setting.class);
188:                 when(setting.getEObject()).thenReturn(rootObject);
189:                 when(setting.getEStructuralFeature()).thenReturn(EcorePackage.Literals.EPACKAGE__ECLASSIFIERS);
190:                 when(setting.get(anyBoolean())).thenReturn(classifiers);
191:                 when(((InternalEObject) rootObject).eSetting(EcorePackage.Literals.EPACKAGE__ECLASSIFIERS))
192:                         .thenReturn(setting);
193:                 setting = mock(EStructuralFeature.Setting.class);
194:                 when(setting.getEObject()).thenReturn(class1);
195:                 when(setting.getEStructuralFeature()).thenReturn(EcorePackage.Literals.ECLASS__ESTRUCTURAL_FEATURES);
196:                 when(setting.get(anyBoolean())).thenReturn(ECollections.emptyEList());
197:                 when(((InternalEObject) class1).eSetting(EcorePackage.Literals.ECLASS__ESTRUCTURAL_FEATURES))
198:                         .thenReturn(setting);
199:                 setting = mock(EStructuralFeature.Setting.class);
200:                 when(setting.getEObject()).thenReturn(class2);
201:                 when(setting.getEStructuralFeature()).thenReturn(EcorePackage.Literals.ECLASS__ESTRUCTURAL_FEATURES);
202:                 when(setting.get(anyBoolean())).thenReturn(features);
203:                 when(((InternalEObject) class2).eSetting(EcorePackage.Literals.ECLASS__ESTRUCTURAL_FEATURES))
204:                         .thenReturn(setting);
205:         }
206:
207:         @Before
208:         public void mockChildContexts() {
209:                 when(fixture.getViewContext().getChildContext(any(), any(), any(), (ViewModelService[]) anyVararg()))
210:                         .then(invocation -> {
211:                                 final ViewModelContext result = fixture.createChildContext((VElement) invocation.getArguments()[1],
212:                                         "child",
213:                                         (VView) invocation.getArguments()[2],
214:                                         (EObject) invocation.getArguments()[0]);
215:                                 when(result.getParentContext()).thenReturn((ViewModelContext) invocation.getMock());
216:                                 return result;
217:                         });
218:         }
219:
220:         @Before
221:         public void createShell() {
222:                 shell = new Shell();
223:         }
224:
225:         @After
226:         public void destroyShell() {
227:                 shell.dispose();
228:                 shell = null;
229:         }
230:
231:         void render() {
232:                 SWTViewTestHelper.render(fixture.getViewContext(), shell);
233:         }
234:
235:         //
236:         // Nested types
237:         //
238:
239:         /**
240:          * A high-bidding reveal provider to make sure that we drill into the view
241:          * to find the table, regardless of other possible contributions in the
242:          * current configuration.
243:          */
244:         private final class ViewRevealer implements EMFFormsRevealProvider {
245:
246:                 private final VView view;
247:                 private final Runnable reveal;
248:
249:                 ViewRevealer(VView view, Runnable reveal) {
250:                         super();
251:
252:                         this.view = view;
253:                         this.reveal = reveal;
254:                 }
255:
256:                 @Bid
257:                 public Double bid(VView view, EObject model) {
258:                         return view == this.view && model == obj2 ? Double.MAX_VALUE : null;
259:                 }
260:
261:                 @Create
262:                 public RevealStep create(VView view, EObject model, RevealHelper helper) {
263:                         return view == this.view && model == obj2
264:                                 ? helper.drillDown(this)
265:                                 : RevealStep.fail();
266:                 }
267:
268:                 @Reveal
269:                 private RevealStep drillDown(VView view, EObject model) {
270:                         return RevealStep.reveal(view, model, reveal);
271:                 }
272:         }
273:
274: }