Skip to content

Package: MasterDetailSelectionProvider_Test

MasterDetailSelectionProvider_Test

nameinstructionbranchcomplexitylinemethod
MasterDetailSelectionProvider_Test()
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
changeDetail()
M: 0 C: 83
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 16
100%
M: 0 C: 1
100%
changedTo(Object)
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%
getSelection_detail()
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%
getSelection_detail2()
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%
getSelection_master()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
isEmpty()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
postSelectionChanged()
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%
selected(Object)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
selectionChanged()
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%
selectionChanged(ISelectionChangedListener)
M: 0 C: 119
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 19
100%
M: 0 C: 1
100%
setSelection_detail()
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
setSelection_master()
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
setup()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
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.ui.view.spi.swt.selection.test;
15:
16: import static org.hamcrest.CoreMatchers.is;
17: import static org.hamcrest.MatcherAssert.assertThat;
18: import static org.junit.Assume.assumeThat;
19: import static org.mockito.Matchers.argThat;
20: import static org.mockito.Mockito.verify;
21: import static org.mockito.Mockito.verifyNoMoreInteractions;
22:
23: import java.util.List;
24: import java.util.function.Supplier;
25: import java.util.stream.Collectors;
26: import java.util.stream.Stream;
27:
28: import org.eclipse.emf.ecp.view.spi.swt.selection.MasterDetailSelectionProvider;
29: import org.eclipse.jface.viewers.ISelection;
30: import org.eclipse.jface.viewers.ISelectionChangedListener;
31: import org.eclipse.jface.viewers.IStructuredSelection;
32: import org.eclipse.jface.viewers.SelectionChangedEvent;
33: import org.eclipse.jface.viewers.StructuredSelection;
34: import org.hamcrest.CustomTypeSafeMatcher;
35: import org.hamcrest.FeatureMatcher;
36: import org.hamcrest.Matcher;
37: import org.junit.Before;
38: import org.junit.Test;
39: import org.junit.runner.RunWith;
40: import org.mockito.Mock;
41: import org.mockito.runners.MockitoJUnitRunner;
42:
43: /**
44: * Test cases for the {@link MasterDetailSelectionProvider} class.
45: */
46: @RunWith(MockitoJUnitRunner.class)
47: public class MasterDetailSelectionProvider_Test {
48:
49:         private final SimplePostSelectionProvider master = new SimplePostSelectionProvider();
50:         private final DetailToggle detail = new DetailToggle();
51:
52:         private final MasterDetailSelectionProvider fixture = new MasterDetailSelectionProvider(master);
53:
54:         @Mock
55:         private ISelectionChangedListener listener;
56:
57:         @Mock
58:         private ISelectionChangedListener postListener;
59:
60:         /**
61:          * Initializes me.
62:          */
63:         public MasterDetailSelectionProvider_Test() {
64:                 super();
65:         }
66:
67:         @Test
68:         public void getSelection_master() {
69:                 master.select("a");
70:                 assertThat(fixture.getSelection(), selected("a"));
71:         }
72:
73:         @Test
74:         public void getSelection_detail() {
75:                 master.select("a");
76:                 detail.get().select("b");
77:                 fixture.setDetailSelectionProvider(detail.get());
78:
79:                 assertThat(fixture.getSelection(), selected("b"));
80:
81:                 fixture.setDetailSelectionProvider(null);
82:                 assertThat(fixture.getSelection(), selected("a"));
83:         }
84:
85:         @Test
86:         public void getSelection_detail2() {
87:                 fixture.setDetailSelectionProvider(detail.get());
88:
89:                 detail.get().select("b");
90:                 master.select("a");
91:
92:                 assertThat(fixture.getSelection(), selected("b"));
93:
94:                 fixture.setDetailSelectionProvider(null);
95:                 assertThat(fixture.getSelection(), selected("a"));
96:         }
97:
98:         @Test
99:         public void setSelection_master() {
100:                 fixture.setSelection(new StructuredSelection("a"));
101:
102:                 assertThat(master.getSelection(), selected("a"));
103:         }
104:
105:         @Test
106:         public void setSelection_detail() {
107:                 fixture.setDetailSelectionProvider(detail.get());
108:                 fixture.setSelection(new StructuredSelection("b"));
109:
110:                 assertThat(master.getSelection(), isEmpty());
111:                 assertThat(detail.get().getSelection(), selected("b"));
112:         }
113:
114:         @Test
115:         public void changeDetail() {
116:                 master.select("a");
117:                 detail.get().select("b");
118:                 detail.cycle().select("c");
119:                 detail.cycle().select("d");
120:
121:                 assumeThat(fixture.getSelection(), selected("a"));
122:
123:                 fixture.setDetailSelectionProvider(detail.reset());
124:                 assumeThat(fixture.getSelection(), selected("b"));
125:
126:                 fixture.setDetailSelectionProvider(detail.cycle());
127:                 assertThat(fixture.getSelection(), selected("c"));
128:
129:                 fixture.setDetailSelectionProvider(detail.cycle());
130:                 assertThat(fixture.getSelection(), selected("d"));
131:
132:                 fixture.setDetailSelectionProvider(detail.cycle());
133:                 assertThat(fixture.getSelection(), isEmpty());
134:
135:                 fixture.setDetailSelectionProvider(null);
136:                 assertThat(fixture.getSelection(), selected("a"));
137:         }
138:
139:         @Test
140:         public void selectionChanged() {
141:                 selectionChanged(listener);
142:         }
143:
144:         void selectionChanged(ISelectionChangedListener listener) {
145:                 master.select("a");
146:                 verify(listener).selectionChanged(argThat(changedTo("a")));
147:
148:                 // Focused on the master
149:                 detail.get().select("b");
150:                 verifyNoMoreInteractions(listener);
151:
152:                 // Change to the detail
153:                 fixture.setDetailSelectionProvider(detail.get());
154:                 verify(listener).selectionChanged(argThat(changedTo("b")));
155:
156:                 // Update the detail
157:                 detail.get().select("c");
158:                 verify(listener).selectionChanged(argThat(changedTo("c")));
159:
160:                 // Change a different detail
161:                 detail.cycle().select("d");
162:                 verifyNoMoreInteractions(listener);
163:
164:                 // Change to that detail
165:                 fixture.setDetailSelectionProvider(detail.get());
166:                 verify(listener).selectionChanged(argThat(changedTo("d")));
167:
168:                 // Idempotent
169:                 fixture.setDetailSelectionProvider(detail.get());
170:                 verifyNoMoreInteractions(listener);
171:
172:                 // Focused on the detail
173:                 master.select("z");
174:                 verifyNoMoreInteractions(listener);
175:
176:                 // Change back to the master
177:                 fixture.setDetailSelectionProvider(null);
178:                 verify(listener).selectionChanged(argThat(changedTo("z")));
179:         }
180:
181:         @Test
182:         public void postSelectionChanged() {
183:                 selectionChanged(postListener);
184:         }
185:
186:         //
187:         // Test framework
188:         //
189:
190:         @Before
191:         public void setup() {
192:                 assumeThat(fixture.getSelection(), isEmpty());
193:
194:                 fixture.addSelectionChangedListener(listener);
195:                 fixture.addPostSelectionChangedListener(postListener);
196:         }
197:
198:         static Matcher<ISelection> isEmpty() {
199:                 return new CustomTypeSafeMatcher<ISelection>("empty") {
200:                         @Override
201:                         protected boolean matchesSafely(ISelection item) {
202:                                 return item.isEmpty();
203:                         }
204:                 };
205:         }
206:
207:         static Matcher<ISelection> selected(Object selection) {
208:                 return new CustomTypeSafeMatcher<ISelection>("selected " + selection) {
209:                         @Override
210:                         protected boolean matchesSafely(ISelection item) {
211:                                 return item instanceof IStructuredSelection
212:                                         && ((IStructuredSelection) item).toList().contains(selection);
213:                         }
214:                 };
215:         }
216:
217:         Matcher<SelectionChangedEvent> changedTo(Object selection) {
218:                 return new FeatureMatcher<SelectionChangedEvent, ISelection>(selected(selection), "selection", "selection") {
219:                         @Override
220:                         protected ISelection featureValueOf(SelectionChangedEvent actual) {
221:                                 assertThat("Wrong event source", actual.getSelectionProvider(), is(fixture));
222:                                 return actual.getSelection();
223:                         }
224:                 };
225:         }
226:
227:         //
228:         // Nested types
229:         //
230:
231:         private final class DetailToggle implements Supplier<SimplePostSelectionProvider> {
232:                 private final List<SimplePostSelectionProvider> details;
233:
234:                 private int index;
235:
236:                 DetailToggle() {
237:                         super();
238:
239:                         details = Stream.generate(SimplePostSelectionProvider::new)
240:                                 .limit(10L).collect(Collectors.toList());
241:                 }
242:
243:                 SimplePostSelectionProvider cycle() {
244:                         return switchTo((index + 1) % details.size());
245:                 }
246:
247:                 SimplePostSelectionProvider reset() {
248:                         return switchTo(0);
249:                 }
250:
251:                 SimplePostSelectionProvider switchTo(int detail) {
252:                         index = detail;
253:                         return get();
254:                 }
255:
256:                 @Override
257:                 public SimplePostSelectionProvider get() {
258:                         return details.get(index);
259:                 }
260:         }
261:
262: }