This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
KeyShortcutPresenterTest.java
192 lines (159 loc) · 8.18 KB
/
KeyShortcutPresenterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright 2012, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.webtrans.client.presenter;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import net.customware.gwt.presenter.client.EventBus;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.webtrans.client.keys.EventWrapper;
import org.zanata.webtrans.client.keys.KeyShortcut;
import org.zanata.webtrans.client.keys.Keys;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.view.KeyShortcutDisplay;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.view.client.ListDataProvider;
/**
* @author David Mason, <a href="mailto:damason@redhat.com">damason@redhat.com</a>
*
*/
@Test(groups = { "unit-tests" })
public class KeyShortcutPresenterTest
{
static final String TEST_MESSAGE_CLOSE_SHORTCUT_VIEW = "close shortcut view";
static final String TEST_MESSAGE_SHOW_AVAILABLE_KEY_SHORTCUTS = "show available key shortcuts";
// These could be any ints for the purpose of this test, as long as key codes don't over
static final int KEY_DOWN_TYPE = 0x80;
static final int KEY_UP_TYPE = 0x200;
static final int KEY_CODE_Y = 'Y';
static final String TEST_MESSAGE_APPLICATION_SCOPE = "Application";
//object under test
KeyShortcutPresenter keyShortcutPresenter;
@Mock
private KeyShortcutDisplay mockDisplay;
@Mock
private EventWrapper mockEventWrapper;
@Mock
private EventBus mockEventBus;
@Mock
private WebTransMessages mockMessages;
@Captor
private ArgumentCaptor<NativePreviewHandler> capturedNativePreviewHandler;
@Mock
private HandlerRegistration handlerRegistration;
@BeforeMethod
public void beforeMethod()
{
MockitoAnnotations.initMocks(this);
keyShortcutPresenter = new KeyShortcutPresenter(mockDisplay, mockEventBus, mockMessages, mockEventWrapper);
when(mockMessages.closeShortcutView()).thenReturn(TEST_MESSAGE_CLOSE_SHORTCUT_VIEW);
when(mockMessages.showAvailableKeyShortcuts()).thenReturn(TEST_MESSAGE_SHOW_AVAILABLE_KEY_SHORTCUTS);
when(mockMessages.applicationScope()).thenReturn(TEST_MESSAGE_APPLICATION_SCOPE);
when(mockEventWrapper.addNativePreviewHandler(capturedNativePreviewHandler.capture())).thenReturn(handlerRegistration);
when(mockEventWrapper.keyDownEvent()).thenReturn(KEY_DOWN_TYPE);
when(mockEventWrapper.keyUpEvent()).thenReturn(KEY_UP_TYPE);
}
@Test
public void testExpectedActionsOnBind()
{
keyShortcutPresenter.bind();
verify(mockDisplay).setListener(keyShortcutPresenter);
verify(mockEventWrapper).addNativePreviewHandler(capturedNativePreviewHandler.capture());
}
@Test
public void displaysRegisteredShortcutsInOrder()
{
@SuppressWarnings("unchecked")
ListDataProvider<KeyShortcut> mockDataProvider = mock(ListDataProvider.class);
List<KeyShortcut> shortcutList = new ArrayList<KeyShortcut>();
when(mockDataProvider.getList()).thenReturn(shortcutList);
mockDisplay.clearPanel();
when(mockDisplay.addContext(TEST_MESSAGE_APPLICATION_SCOPE)).thenReturn(mockDataProvider);
mockDisplay.showPanel();
keyShortcutPresenter.bind();
keyShortcutPresenter.showShortcuts();
//Shortcut list should contain Alt+Y and Esc shortcuts
assertThat("KeyShortcutPresenter should register 2 global shortcuts", shortcutList.size(), is(2));
// esc should be first as it has no modifiers
Set<Keys> firstShortcutKeys = shortcutList.get(0).getAllKeys();
String firstShortcut = "first shortcut should be Esc with no modifiers and no aliases";
assertThat(firstShortcut, firstShortcutKeys.size(), is(1));
Keys firstShortcutFirstKeys = firstShortcutKeys.iterator().next();
assertThat(firstShortcut, firstShortcutFirstKeys.getModifiers(), is(0));
assertThat(firstShortcut, firstShortcutFirstKeys.getKeyCode(), is(KeyCodes.KEY_ESCAPE));
// Alt+Y should be the other
Set<Keys> secondShortcutKeys = shortcutList.get(1).getAllKeys();
String secondShortcut = "second shortcut should be Alt+Y with no aliases";
assertThat(secondShortcut, secondShortcutKeys.size(), is(1));
Keys secondShortcutFirstKeys = secondShortcutKeys.iterator().next();
assertThat(secondShortcut, secondShortcutFirstKeys.getModifiers(), is(Keys.ALT_KEY));
assertThat(secondShortcut, secondShortcutFirstKeys.getKeyCode(), is((int) 'Y'));
}
@Test
public void testRespondsToAltY()
{
NativePreviewEvent mockNativePreviewEvent = mock(NativePreviewEvent.class);
NativeEvent mockNativeEvent = mock(NativeEvent.class);
when(mockEventWrapper.getTypeInt(mockNativePreviewEvent)).thenReturn(KEY_DOWN_TYPE);
when(mockNativePreviewEvent.getNativeEvent()).thenReturn(mockNativeEvent);
when(mockEventWrapper.createKeys(mockNativeEvent)).thenReturn(new Keys(Keys.ALT_KEY, KEY_CODE_Y));
when(mockEventWrapper.getType(mockNativeEvent)).thenReturn(KeyShortcut.KeyEvent.KEY_DOWN.nativeEventType);
@SuppressWarnings("unchecked")
ListDataProvider<KeyShortcut> mockDataProvider = mock(ListDataProvider.class);
List<KeyShortcut> shortcutList = new ArrayList<KeyShortcut>();
when(mockDataProvider.getList()).thenReturn(shortcutList);
mockDisplay.clearPanel();
when(mockDisplay.addContext(TEST_MESSAGE_APPLICATION_SCOPE)).thenReturn(mockDataProvider);
mockDisplay.showPanel();
keyShortcutPresenter.bind();
capturedNativePreviewHandler.getValue().onPreviewNativeEvent(mockNativePreviewEvent);
//Shortcut list should contain Alt+Y and Esc shortcuts
assertThat("KeyShortcutPresenter should register 2 global shortcuts", shortcutList.size(), is(2));
// esc should be first as it has no modifiers
Set<Keys> firstShortcutKeys = shortcutList.get(0).getAllKeys();
String firstShortcut = "first shortcut should be Esc with no modifiers and no aliases";
assertThat(firstShortcut, firstShortcutKeys.size(), is(1));
Keys firstShortcutFirstKeys = firstShortcutKeys.iterator().next();
assertThat(firstShortcut, firstShortcutFirstKeys.getModifiers(), is(0));
assertThat(firstShortcut, firstShortcutFirstKeys.getKeyCode(), is(KeyCodes.KEY_ESCAPE));
// Alt+Y should be the other
Set<Keys> secondShortcutKeys = shortcutList.get(1).getAllKeys();
String secondShortcut = "second shortcut should be Alt+Y with no aliases";
assertThat(secondShortcut, secondShortcutKeys.size(), is(1));
Keys secondShortcutFirstKeys = secondShortcutKeys.iterator().next();
assertThat(secondShortcut, secondShortcutFirstKeys.getModifiers(), is(Keys.ALT_KEY));
assertThat(secondShortcut, secondShortcutFirstKeys.getKeyCode(), is((int) 'Y'));
}
}