|
15 | 15 | */ |
16 | 16 | package rx.swing.sources; |
17 | 17 |
|
18 | | -import static java.util.Arrays.asList; |
19 | | -import static org.mockito.Mockito.inOrder; |
20 | | -import static org.mockito.Mockito.mock; |
21 | | -import static org.mockito.Mockito.never; |
22 | | -import static org.mockito.Mockito.times; |
23 | | -import static org.mockito.Mockito.verify; |
24 | | - |
25 | 18 | import java.awt.Component; |
26 | 19 | import java.awt.event.KeyEvent; |
27 | 20 | import java.awt.event.KeyListener; |
28 | 21 | import java.util.Collections; |
29 | 22 | import java.util.HashSet; |
30 | 23 | import java.util.Set; |
31 | 24 |
|
32 | | -import javax.swing.JPanel; |
33 | | - |
34 | | -import org.junit.Test; |
35 | | -import org.mockito.InOrder; |
36 | | -import org.mockito.Matchers; |
37 | | - |
38 | 25 | import rx.Observable; |
39 | 26 | import rx.Observable.OnSubscribe; |
40 | 27 | import rx.Subscriber; |
41 | | -import rx.Subscription; |
42 | 28 | import rx.functions.Action0; |
43 | | -import rx.functions.Action1; |
44 | 29 | import rx.functions.Func1; |
45 | 30 | import rx.functions.Func2; |
46 | 31 | import rx.observables.SwingObservable; |
@@ -117,101 +102,4 @@ public Boolean call(KeyEvent event) { |
117 | 102 | return filteredKeyEvents.scan(Collections.<Integer>emptySet(), new CollectKeys()); |
118 | 103 | } |
119 | 104 |
|
120 | | - public static class UnitTest { |
121 | | - private Component comp = new JPanel(); |
122 | | - |
123 | | - @Test |
124 | | - public void testObservingKeyEvents() throws Throwable { |
125 | | - SwingTestHelper.create().runInEventDispatchThread(new Action0(){ |
126 | | - |
127 | | - @Override |
128 | | - public void call() { |
129 | | - @SuppressWarnings("unchecked") |
130 | | - Action1<KeyEvent> action = mock(Action1.class); |
131 | | - @SuppressWarnings("unchecked") |
132 | | - Action1<Throwable> error = mock(Action1.class); |
133 | | - Action0 complete = mock(Action0.class); |
134 | | - |
135 | | - final KeyEvent event = mock(KeyEvent.class); |
136 | | - |
137 | | - Subscription sub = fromKeyEventsOf(comp).subscribe(action, error, complete); |
138 | | - |
139 | | - verify(action, never()).call(Matchers.<KeyEvent> any()); |
140 | | - verify(error, never()).call(Matchers.<Throwable> any()); |
141 | | - verify(complete, never()).call(); |
142 | | - |
143 | | - fireKeyEvent(event); |
144 | | - verify(action, times(1)).call(Matchers.<KeyEvent> any()); |
145 | | - |
146 | | - fireKeyEvent(event); |
147 | | - verify(action, times(2)).call(Matchers.<KeyEvent> any()); |
148 | | - |
149 | | - sub.unsubscribe(); |
150 | | - fireKeyEvent(event); |
151 | | - verify(action, times(2)).call(Matchers.<KeyEvent> any()); |
152 | | - verify(error, never()).call(Matchers.<Throwable> any()); |
153 | | - verify(complete, never()).call(); |
154 | | - } |
155 | | - |
156 | | - }).awaitTerminal(); |
157 | | - } |
158 | | - |
159 | | - @Test |
160 | | - public void testObservingPressedKeys() throws Throwable { |
161 | | - SwingTestHelper.create().runInEventDispatchThread(new Action0() { |
162 | | - |
163 | | - @Override |
164 | | - public void call() { |
165 | | - @SuppressWarnings("unchecked") |
166 | | - Action1<Set<Integer>> action = mock(Action1.class); |
167 | | - @SuppressWarnings("unchecked") |
168 | | - Action1<Throwable> error = mock(Action1.class); |
169 | | - Action0 complete = mock(Action0.class); |
170 | | - |
171 | | - Subscription sub = currentlyPressedKeysOf(comp).subscribe(action, error, complete); |
172 | | - |
173 | | - InOrder inOrder = inOrder(action); |
174 | | - inOrder.verify(action, times(1)).call(Collections.<Integer> emptySet()); |
175 | | - verify(error, never()).call(Matchers.<Throwable> any()); |
176 | | - verify(complete, never()).call(); |
177 | | - |
178 | | - fireKeyEvent(keyEvent(1, KeyEvent.KEY_PRESSED)); |
179 | | - inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1))); |
180 | | - verify(error, never()).call(Matchers.<Throwable> any()); |
181 | | - verify(complete, never()).call(); |
182 | | - |
183 | | - fireKeyEvent(keyEvent(2, KeyEvent.KEY_PRESSED)); |
184 | | - fireKeyEvent(keyEvent(KeyEvent.VK_UNDEFINED, KeyEvent.KEY_TYPED)); |
185 | | - inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1, 2))); |
186 | | - |
187 | | - fireKeyEvent(keyEvent(2, KeyEvent.KEY_RELEASED)); |
188 | | - inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1))); |
189 | | - |
190 | | - fireKeyEvent(keyEvent(3, KeyEvent.KEY_RELEASED)); |
191 | | - inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1))); |
192 | | - |
193 | | - fireKeyEvent(keyEvent(1, KeyEvent.KEY_RELEASED)); |
194 | | - inOrder.verify(action, times(1)).call(Collections.<Integer> emptySet()); |
195 | | - |
196 | | - sub.unsubscribe(); |
197 | | - |
198 | | - fireKeyEvent(keyEvent(1, KeyEvent.KEY_PRESSED)); |
199 | | - inOrder.verify(action, never()).call(Matchers.<Set<Integer>> any()); |
200 | | - verify(error, never()).call(Matchers.<Throwable> any()); |
201 | | - verify(complete, never()).call(); |
202 | | - } |
203 | | - |
204 | | - }).awaitTerminal(); |
205 | | - } |
206 | | - |
207 | | - private KeyEvent keyEvent(int keyCode, int id) { |
208 | | - return new KeyEvent(comp, id, -1L, 0, keyCode, ' '); |
209 | | - } |
210 | | - |
211 | | - private void fireKeyEvent(KeyEvent event) { |
212 | | - for (KeyListener listener: comp.getKeyListeners()) { |
213 | | - listener.keyTyped(event); |
214 | | - } |
215 | | - } |
216 | | - } |
217 | 105 | } |
0 commit comments