Skip to content

Package: MasterDetailFocusAdapter_PTest

MasterDetailFocusAdapter_PTest

nameinstructionbranchcomplexitylinemethod
MasterDetailFocusAdapter_PTest()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
changedTo(Matcher)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createUI()
M: 0 C: 145
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
disposeUI()
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()
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%
switchFocus()
M: 0 C: 69
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 15
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.eclipse.emf.ecp.ui.view.spi.swt.selection.test.MasterDetailSelectionProvider_Test.isEmpty;
17: import static org.eclipse.emf.ecp.ui.view.spi.swt.selection.test.MasterDetailSelectionProvider_Test.selected;
18: import static org.hamcrest.MatcherAssert.assertThat;
19: import static org.mockito.Matchers.any;
20: import static org.mockito.Matchers.argThat;
21: import static org.mockito.Mockito.times;
22: import static org.mockito.Mockito.verify;
23: import static org.mockito.Mockito.when;
24:
25: import org.eclipse.emf.ecp.view.spi.swt.selection.IMasterDetailSelectionProvider;
26: import org.eclipse.emf.ecp.view.spi.swt.selection.MasterDetailFocusAdapter;
27: import org.eclipse.emf.ecp.view.spi.swt.selection.MasterDetailSelectionProvider;
28: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
29: import org.eclipse.jface.viewers.ISelection;
30: import org.eclipse.jface.viewers.ISelectionChangedListener;
31: import org.eclipse.jface.viewers.ITreeContentProvider;
32: import org.eclipse.jface.viewers.SelectionChangedEvent;
33: import org.eclipse.jface.viewers.TreeViewer;
34: import org.eclipse.jface.viewers.Viewer;
35: import org.eclipse.swt.SWT;
36: import org.eclipse.swt.custom.SashForm;
37: import org.eclipse.swt.layout.FillLayout;
38: import org.eclipse.swt.layout.GridLayout;
39: import org.eclipse.swt.widgets.Composite;
40: import org.eclipse.swt.widgets.Label;
41: import org.eclipse.swt.widgets.Shell;
42: import org.eclipse.swt.widgets.Text;
43: import org.eclipse.swt.widgets.Tree;
44: import org.hamcrest.FeatureMatcher;
45: import org.hamcrest.Matcher;
46: import org.junit.After;
47: import org.junit.Before;
48: import org.junit.Test;
49: import org.junit.runner.RunWith;
50: import org.mockito.Mock;
51: import org.mockito.runners.MockitoJUnitRunner;
52:
53: /**
54: * Test cases for the {@link MasterDetailFocusAdapter} class.
55: */
56: @RunWith(MockitoJUnitRunner.class)
57: public class MasterDetailFocusAdapter_PTest {
58:
59:         private Shell shell;
60:         private Tree tree;
61:         private Viewer viewer;
62:         private Composite detail;
63:         private Text textInDetail;
64:
65:         private IMasterDetailSelectionProvider mdSelectionProvider;
66:
67:         @Mock
68:         private ITreeContentProvider content;
69:
70:         @Mock
71:         private ISelectionChangedListener listener;
72:
73:         /**
74:          * Initializes me.
75:          */
76:         public MasterDetailFocusAdapter_PTest() {
77:                 super();
78:         }
79:
80:         @Test
81:         public void switchFocus() {
82:                 viewer.getControl().setFocus();
83:                 SWTTestUtil.waitForUIThread();
84:
85:                 assertThat(mdSelectionProvider.getSelection(), isEmpty());
86:
87:                 SWTTestUtil.selectTreeItem(tree, 1);
88:                 SWTTestUtil.waitForUIThread();
89:
90:                 assertThat(mdSelectionProvider.getSelection(), selected("b"));
91:
92:                 textInDetail.setFocus();
93:                 SWTTestUtil.waitForUIThread();
94:
95:                 assertThat(mdSelectionProvider.getSelection(), isEmpty());
96:
97:                 viewer.getControl().setFocus();
98:                 SWTTestUtil.waitForUIThread();
99:
100:                 assertThat(mdSelectionProvider.getSelection(), selected("b"));
101:
102:                 verify(listener, times(2)).selectionChanged(argThat(changedTo(selected("b"))));
103:                 verify(listener).selectionChanged(argThat(changedTo(isEmpty())));
104:         }
105:
106:         //
107:         // Test framework
108:         //
109:
110:         @Before
111:         public void createUI() {
112:                 shell = new Shell();
113:
114:                 final SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);
115:                 viewer = new TreeViewer(sashForm);
116:                 tree = ((TreeViewer) viewer).getTree();
117:                 detail = new Composite(sashForm, SWT.NONE);
118:                 detail.setLayout(new FillLayout());
119:                 final Composite detailContent = new Composite(detail, SWT.NONE);
120:                 detailContent.setLayout(new GridLayout(2, false));
121:                 new Label(detailContent, SWT.NONE).setText("Text:");
122:                 textInDetail = new Text(detailContent, SWT.BORDER | SWT.SINGLE);
123:
124:                 when(content.getElements(any())).thenReturn(new Object[] { "a", "b", "c", "d", "e" });
125:                 when(content.getChildren(any())).thenReturn(new Object[0]);
126:                 ((TreeViewer) viewer).setContentProvider(content);
127:                 viewer.setInput(new Object());
128:
129:                 mdSelectionProvider = new MasterDetailSelectionProvider(viewer);
130:                 mdSelectionProvider.addSelectionChangedListener(listener);
131:                 viewer.getControl().addFocusListener(new MasterDetailFocusAdapter(mdSelectionProvider, () -> detail));
132:
133:                 shell.pack();
134:                 shell.open();
135:         }
136:
137:         @After
138:         public void disposeUI() {
139:                 shell.close();
140:                 shell.dispose();
141:         }
142:
143:         Matcher<SelectionChangedEvent> changedTo(Matcher<? super ISelection> selectionMatcher) {
144:                 return new FeatureMatcher<SelectionChangedEvent, ISelection>(selectionMatcher, "selection", "selection") {
145:                         @Override
146:                         protected ISelection featureValueOf(SelectionChangedEvent actual) {
147:                                 return actual.getSelection();
148:                         }
149:                 };
150:         }
151:
152: }