Skip to content

Package: EMFFormsContextTracker_Test

EMFFormsContextTracker_Test

nameinstructionbranchcomplexitylinemethod
EMFFormsContextTracker_Test()
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
addChild(EMFFormsViewContext, VElement, EMFFormsViewContext)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
changeDomainModel(EMFFormsViewContext)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
close()
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
dispose(EMFFormsViewContext)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
initialize(EMFFormsViewContext)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
interceptListenerRegistration(Class, Map)
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%
interceptListenerUnregistration(Class, Map)
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%
isRoot()
M: 0 C: 36
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
lambda$0(Class, Map, InvocationOnMock)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$1(Class, Map, InvocationOnMock)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
mockConsumer()
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%
mockContext(String)
M: 0 C: 55
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
mockTriConsumer()
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%
onChildContextAdded()
M: 0 C: 32
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
onChildContextRemoved()
M: 0 C: 37
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
onContextDisposed_child()
M: 0 C: 38
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
onContextDisposed_root()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
onContextInitialized_child()
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%
onDomainModelChanged_child()
M: 0 C: 39
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
onDomainModelChanged_root()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
open()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
removeChild(EMFFormsViewContext, EMFFormsViewContext)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
requireContextListener(EMFFormsViewContext)
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%
requireRootDomainListener(EMFFormsViewContext)
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%

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.core.services.view;
15:
16: import static org.hamcrest.CoreMatchers.anything;
17: import static org.hamcrest.CoreMatchers.hasItem;
18: import static org.hamcrest.CoreMatchers.is;
19: import static org.hamcrest.CoreMatchers.not;
20: import static org.hamcrest.CoreMatchers.notNullValue;
21: import static org.junit.Assert.assertThat;
22: import static org.mockito.Matchers.any;
23: import static org.mockito.Mockito.doAnswer;
24: import static org.mockito.Mockito.mock;
25: import static org.mockito.Mockito.never;
26: import static org.mockito.Mockito.verify;
27:
28: import java.util.HashMap;
29: import java.util.Map;
30: import java.util.function.Consumer;
31:
32: import org.eclipse.emf.ecp.view.spi.model.VElement;
33: import org.eclipse.emfforms.common.TriConsumer;
34: import org.eclipse.emfforms.spi.core.services.view.EMFFormsContextListener;
35: import org.eclipse.emfforms.spi.core.services.view.EMFFormsContextTracker;
36: import org.eclipse.emfforms.spi.core.services.view.EMFFormsViewContext;
37: import org.eclipse.emfforms.spi.core.services.view.RootDomainModelChangeListener;
38: import org.junit.After;
39: import org.junit.Before;
40: import org.junit.Test;
41: import org.mockito.stubbing.Answer;
42:
43: /**
44: * Test cases for the {@link EMFFormsContextTracker} class.
45: */
46: @SuppressWarnings("nls")
47: public class EMFFormsContextTracker_Test {
48:
49:         private final Map<EMFFormsViewContext, EMFFormsContextListener> contextListeners = new HashMap<>();
50:         private final Map<EMFFormsViewContext, RootDomainModelChangeListener> rootDomainListeners = new HashMap<>();
51:
52:         private final EMFFormsViewContext root = mockContext("root");
53:         private final EMFFormsContextTracker fixture = new EMFFormsContextTracker(root);
54:
55:         /**
56:          * Initializes me.
57:          */
58:         public EMFFormsContextTracker_Test() {
59:                 super();
60:         }
61:
62:         @Test
63:         public void onContextDisposed_root() {
64:                 final Consumer<EMFFormsViewContext> handler = mockConsumer();
65:
66:                 fixture.onContextDisposed(handler);
67:
68:                 dispose(root);
69:
70:                 verify(handler).accept(root);
71:         }
72:
73:         @Test
74:         public void onContextDisposed_child() {
75:                 final EMFFormsViewContext child = mockContext("child");
76:                 final VElement parentView = mock(VElement.class);
77:                 addChild(root, parentView, child);
78:
79:                 final Consumer<EMFFormsViewContext> handler = mockConsumer();
80:
81:                 fixture.onContextDisposed(handler);
82:
83:                 dispose(child);
84:
85:                 verify(handler).accept(child);
86:                 verify(handler, never()).accept(root);
87:         }
88:
89:         @Test
90:         public void isRoot() {
91:                 final EMFFormsViewContext child = mockContext("child");
92:                 final VElement parentView = mock(VElement.class);
93:                 addChild(root, parentView, child);
94:
95:                 assertThat("Root not root", fixture.isRoot(root), is(true));
96:                 assertThat("Child is root", fixture.isRoot(child), is(false));
97:         }
98:
99:         @Test
100:         public void onChildContextAdded() {
101:                 final EMFFormsViewContext child = mockContext("child");
102:                 final VElement parentView = mock(VElement.class, "parentView");
103:
104:                 final TriConsumer<EMFFormsViewContext, VElement, EMFFormsViewContext> handler = mockTriConsumer();
105:
106:                 fixture.onChildContextAdded(handler);
107:
108:                 addChild(root, parentView, child);
109:
110:                 verify(handler).accept(root, parentView, child);
111:         }
112:
113:         @Test
114:         public void onContextInitialized_child() {
115:                 final EMFFormsViewContext child = mockContext("child");
116:                 final VElement parentView = mock(VElement.class, "parentView");
117:
118:                 final Consumer<EMFFormsViewContext> handler = mockConsumer();
119:
120:                 fixture.onContextInitialized(handler);
121:
122:                 addChild(root, parentView, child);
123:
124:                 verify(handler).accept(child);
125:                 verify(handler, never()).accept(root);
126:         }
127:
128:         @Test
129:         public void onChildContextRemoved() {
130:                 final EMFFormsViewContext child = mockContext("child");
131:                 final VElement parentView = mock(VElement.class, "parentView");
132:                 addChild(root, parentView, child);
133:
134:                 final TriConsumer<EMFFormsViewContext, VElement, EMFFormsViewContext> handler = mockTriConsumer();
135:
136:                 fixture.onChildContextRemoved(handler);
137:
138:                 removeChild(root, child);
139:
140:                 verify(handler).accept(root, parentView, child);
141:         }
142:
143:         @Test
144:         public void onDomainModelChanged_root() {
145:                 final Consumer<EMFFormsViewContext> handler = mockConsumer();
146:
147:                 fixture.onDomainModelChanged(handler);
148:
149:                 changeDomainModel(root);
150:
151:                 verify(handler).accept(root);
152:         }
153:
154:         @Test
155:         public void onDomainModelChanged_child() {
156:                 final EMFFormsViewContext child = mockContext("child");
157:                 final VElement parentView = mock(VElement.class, "parentView");
158:                 addChild(root, parentView, child);
159:
160:                 final Consumer<EMFFormsViewContext> handler = mockConsumer();
161:
162:                 fixture.onDomainModelChanged(handler);
163:
164:                 changeDomainModel(child);
165:
166:                 verify(handler).accept(child);
167:                 verify(handler, never()).accept(root);
168:         }
169:
170:         //
171:         // Test framework
172:         //
173:
174:         @Before
175:         public void open() {
176:                 fixture.open();
177:         }
178:
179:         @After
180:         public void close() {
181:                 fixture.close();
182:
183:                 assertThat("Listeners not removed", contextListeners.keySet(), not(hasItem(anything())));
184:                 assertThat("Listeners not removed", rootDomainListeners.keySet(), not(hasItem(anything())));
185:         }
186:
187:         private EMFFormsViewContext mockContext(String name) {
188:                 final EMFFormsViewContext result = mock(EMFFormsViewContext.class, name);
189:
190:                 doAnswer(interceptListenerRegistration(EMFFormsContextListener.class, contextListeners))
191:                         .when(result).registerEMFFormsContextListener(any());
192:                 doAnswer(interceptListenerRegistration(RootDomainModelChangeListener.class, rootDomainListeners))
193:                         .when(result).registerRootDomainModelChangeListener(any());
194:
195:                 doAnswer(interceptListenerUnregistration(EMFFormsContextListener.class, contextListeners))
196:                         .when(result).unregisterEMFFormsContextListener(any());
197:                 doAnswer(interceptListenerUnregistration(RootDomainModelChangeListener.class, rootDomainListeners))
198:                         .when(result).unregisterRootDomainModelChangeListener(any());
199:
200:                 return result;
201:         }
202:
203:         private <T> Answer<T> interceptListenerRegistration(Class<T> type,
204:                 Map<? super EMFFormsViewContext, ? super T> map) {
205:                 return invocation -> {
206:                         final T listener = type.cast(invocation.getArguments()[0]);
207:                         map.put((EMFFormsViewContext) invocation.getMock(), listener);
208:                         return null;
209:                 };
210:         }
211:
212:         private <T> Answer<T> interceptListenerUnregistration(Class<T> type,
213:                 Map<? super EMFFormsViewContext, ? super T> map) {
214:                 return invocation -> {
215:                         final T listener = type.cast(invocation.getArguments()[0]);
216:                         map.remove(invocation.getMock(), listener);
217:                         return null;
218:                 };
219:         }
220:
221:         @SuppressWarnings("unchecked")
222:         <T> Consumer<T> mockConsumer() {
223:                 return mock(Consumer.class);
224:         }
225:
226:         @SuppressWarnings("unchecked")
227:         <T, U, V> TriConsumer<T, U, V> mockTriConsumer() {
228:                 return mock(TriConsumer.class);
229:         }
230:
231:         private EMFFormsContextListener requireContextListener(EMFFormsViewContext context) {
232:                 final EMFFormsContextListener result = contextListeners.get(context);
233:                 assertThat(String.format("No context listener for %s", context), result, notNullValue());
234:                 return result;
235:         }
236:
237:         private RootDomainModelChangeListener requireRootDomainListener(EMFFormsViewContext context) {
238:                 final RootDomainModelChangeListener result = rootDomainListeners.get(context);
239:                 assertThat(String.format("No root domain model listener for %s", context), result, notNullValue());
240:                 return result;
241:         }
242:
243:         void dispose(EMFFormsViewContext context) {
244:                 requireContextListener(context).contextDispose();
245:         }
246:
247:         void initialize(EMFFormsViewContext context) {
248:                 requireContextListener(context).contextInitialised();
249:         }
250:
251:         void addChild(EMFFormsViewContext parent, VElement parentView, EMFFormsViewContext child) {
252:                 requireContextListener(parent).childContextAdded(parentView, child);
253:                 initialize(child);
254:         }
255:
256:         void removeChild(EMFFormsViewContext parent, EMFFormsViewContext child) {
257:                 dispose(child);
258:                 requireContextListener(parent).childContextDisposed(child);
259:         }
260:
261:         void changeDomainModel(EMFFormsViewContext context) {
262:                 requireRootDomainListener(context).notifyChange();
263:         }
264:
265: }