Skip to content

Package: KeyBindingManager_PTest

KeyBindingManager_PTest

nameinstructionbranchcomplexitylinemethod
KeyBindingManager_PTest()
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%
lambda$0(Composite, Action)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$1(Composite, Action)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setUp()
M: 0 C: 121
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
tearDown()
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%
testKeyBindingManager()
M: 0 C: 181
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 29
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 EclipseSource Muenchen GmbH 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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.swt.table.action;
15:
16: import static org.mockito.Mockito.mock;
17: import static org.mockito.Mockito.never;
18: import static org.mockito.Mockito.times;
19: import static org.mockito.Mockito.verify;
20: import static org.mockito.Mockito.when;
21:
22: import org.eclipse.emf.ecp.view.test.common.swt.spi.SWTTestUtil;
23: import org.eclipse.emfforms.spi.swt.table.action.Action;
24: import org.eclipse.emfforms.spi.swt.table.action.ActionConfiguration;
25: import org.eclipse.emfforms.spi.swt.table.action.ActionConfigurationBuilder;
26: import org.eclipse.emfforms.spi.swt.table.action.ActionControlCreator;
27: import org.eclipse.jface.viewers.TableViewer;
28: import org.eclipse.swt.SWT;
29: import org.eclipse.swt.layout.FillLayout;
30: import org.eclipse.swt.widgets.Button;
31: import org.eclipse.swt.widgets.Display;
32: import org.eclipse.swt.widgets.Label;
33: import org.eclipse.swt.widgets.Shell;
34: import org.junit.After;
35: import org.junit.Before;
36: import org.junit.Test;
37:
38: /**
39: * Unit tests for the {@link KeyBindingManager}.
40: *
41: * @author Lucas Koehler
42: *
43: */
44: public class KeyBindingManager_PTest {
45:         private ActionConfiguration actionConfiguration;
46:         private Shell shell;
47:         private Action action1;
48:         private Action action2;
49:         private KeyBindingManager keyBindingManager;
50:
51:         @Before
52:         public void setUp() {
53:                 final ActionConfigurationBuilder configurationBuilder = ActionConfigurationBuilder.usingDefaults();
54:                 action1 = mock(Action.class);
55:                 when(action1.getId()).thenReturn("action1"); //$NON-NLS-1$
56:                 when(action1.canExecute()).thenReturn(true);
57:                 action2 = mock(Action.class);
58:                 when(action2.getId()).thenReturn("action2"); //$NON-NLS-1$
59:                 when(action2.canExecute()).thenReturn(false);
60:
61:                 final ActionControlCreator<Button> creator1 = (composite, action) -> new Button(composite, SWT.PUSH);
62:                 final ActionControlCreator<Label> creator2 = (composite, action) -> new Label(composite, SWT.PUSH);
63:                 configurationBuilder.addAction(action1);
64:                 configurationBuilder.addAction(action2);
65:                 configurationBuilder.addControlFor(action1, creator1);
66:                 configurationBuilder.addControlFor(action2, creator2);
67:                 configurationBuilder.addKeySequenceFor(action1, "M1+a", "M2+b"); //$NON-NLS-1$ //$NON-NLS-2$
68:                 configurationBuilder.addKeySequenceFor(action2, "M1+f"); //$NON-NLS-1$
69:
70:                 actionConfiguration = configurationBuilder.build();
71:
72:                 shell = new Shell(Display.getDefault());
73:                 shell.setLayout(new FillLayout());
74:                 shell.open();
75:
76:                 keyBindingManager = new KeyBindingManager();
77:         }
78:
79:         @After
80:         public void tearDown() {
81:                 shell.dispose();
82:         }
83:
84:         @Test
85:         public void testKeyBindingManager() {
86:                 keyBindingManager.applyActionConfiguration(actionConfiguration);
87:                 final TableViewer viewer = new TableViewer(shell);
88:                 keyBindingManager.bindToViewer(viewer);
89:
90:                 SWTTestUtil.pressAndReleaseKey(viewer.getControl(), SWT.MOD1, 'a');
91:                 verify(action1, times(1)).canExecute();
92:                 verify(action1, times(1)).execute();
93:                 SWTTestUtil.pressAndReleaseKey(viewer.getControl(), SWT.MOD2, 'b');
94:                 verify(action1, times(2)).canExecute();
95:                 verify(action1, times(2)).execute();
96:
97:                 // The former keybindings should not invoke unrelated actions
98:                 verify(action2, never()).canExecute();
99:                 verify(action2, never()).execute();
100:
101:                 SWTTestUtil.pressAndReleaseKey(viewer.getControl(), SWT.MOD1, 'f');
102:                 verify(action2, times(1)).canExecute();
103:                 // execute must not be invoked yet because canExecute returns false
104:                 verify(action2, never()).execute();
105:                 when(action2.canExecute()).thenReturn(true);
106:                 SWTTestUtil.pressAndReleaseKey(viewer.getControl(), SWT.MOD1, 'f');
107:                 verify(action2, times(2)).canExecute();
108:                 // now execute must also be invoked
109:                 verify(action2, times(1)).execute();
110:
111:                 // Verify that only shortcuts used on the viewer trigger the actions
112:                 SWTTestUtil.pressAndReleaseKey(shell, SWT.MOD1, 'a');
113:                 verify(action1, times(2)).canExecute();
114:                 verify(action1, times(2)).execute();
115:
116:                 // Verify that unbinding the key binding manager works by checking that the actions are no longer invoked when
117:                 // their combinations are pressed
118:                 keyBindingManager.unbindFromViewer(viewer);
119:                 SWTTestUtil.pressAndReleaseKey(viewer.getControl(), SWT.MOD1, 'a');
120:                 SWTTestUtil.pressAndReleaseKey(viewer.getControl(), SWT.MOD1, 'f');
121:                 verify(action1, times(2)).canExecute();
122:                 verify(action1, times(2)).execute();
123:                 verify(action2, times(2)).canExecute();
124:                 verify(action2, times(1)).execute();
125:         }
126: }