|
| 1 | +/** |
| 2 | + * Copyright 2013 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.swing.sources; |
| 17 | + |
| 18 | +import static java.util.Arrays.asList; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import java.awt.event.KeyEvent; |
| 22 | +import java.awt.event.KeyListener; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import javax.swing.JComponent; |
| 28 | +import javax.swing.JPanel; |
| 29 | + |
| 30 | +import org.junit.Test; |
| 31 | +import org.mockito.InOrder; |
| 32 | +import org.mockito.Matchers; |
| 33 | + |
| 34 | +import rx.Observable; |
| 35 | +import rx.Observer; |
| 36 | +import rx.Subscription; |
| 37 | +import rx.subscriptions.Subscriptions; |
| 38 | +import rx.util.functions.Action0; |
| 39 | +import rx.util.functions.Action1; |
| 40 | +import rx.util.functions.Func1; |
| 41 | +import rx.util.functions.Func2; |
| 42 | + |
| 43 | +public enum KeyEventSource { ; // no instances |
| 44 | + |
| 45 | + public static Observable<KeyEvent> fromKeyEventsOf(final JComponent component) { |
| 46 | + return Observable.create(new Func1<Observer<KeyEvent>, Subscription>() { |
| 47 | + @Override |
| 48 | + public Subscription call(final Observer<KeyEvent> observer) { |
| 49 | + final KeyListener listener = new KeyListener() { |
| 50 | + @Override |
| 51 | + public void keyPressed(KeyEvent event) { |
| 52 | + observer.onNext(event); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void keyReleased(KeyEvent event) { |
| 57 | + observer.onNext(event); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void keyTyped(KeyEvent event) { |
| 62 | + observer.onNext(event); |
| 63 | + } |
| 64 | + }; |
| 65 | + component.addKeyListener(listener); |
| 66 | + |
| 67 | + return Subscriptions.create(new Action0() { |
| 68 | + @Override |
| 69 | + public void call() { |
| 70 | + component.removeKeyListener(listener); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + public static Observable<Set<Integer>> currentlyPressedKeysOf(JComponent component) { |
| 78 | + return Observable.<KeyEvent, Set<Integer>>scan(fromKeyEventsOf(component), new HashSet<Integer>(), new Func2<Set<Integer>, KeyEvent, Set<Integer>>() { |
| 79 | + @Override |
| 80 | + public Set<Integer> call(Set<Integer> pressedKeys, KeyEvent event) { |
| 81 | + Set<Integer> afterEvent = new HashSet<Integer>(pressedKeys); |
| 82 | + switch (event.getID()) { |
| 83 | + case KeyEvent.KEY_PRESSED: |
| 84 | + afterEvent.add(event.getKeyCode()); |
| 85 | + break; |
| 86 | + |
| 87 | + case KeyEvent.KEY_RELEASED: |
| 88 | + afterEvent.remove(event.getKeyCode()); |
| 89 | + break; |
| 90 | + |
| 91 | + default: // nothing to do |
| 92 | + } |
| 93 | + return afterEvent; |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + public static class UnitTest { |
| 99 | + private JComponent comp = new JPanel(); |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testObservingKeyEvents() { |
| 103 | + @SuppressWarnings("unchecked") |
| 104 | + Action1<KeyEvent> action = mock(Action1.class); |
| 105 | + @SuppressWarnings("unchecked") |
| 106 | + Action1<Exception> error = mock(Action1.class); |
| 107 | + Action0 complete = mock(Action0.class); |
| 108 | + |
| 109 | + final KeyEvent event = mock(KeyEvent.class); |
| 110 | + |
| 111 | + Subscription sub = fromKeyEventsOf(comp).subscribe(action, error, complete); |
| 112 | + |
| 113 | + verify(action, never()).call(Matchers.<KeyEvent>any()); |
| 114 | + verify(error, never()).call(Matchers.<Exception>any()); |
| 115 | + verify(complete, never()).call(); |
| 116 | + |
| 117 | + fireKeyEvent(event); |
| 118 | + verify(action, times(1)).call(Matchers.<KeyEvent>any()); |
| 119 | + |
| 120 | + fireKeyEvent(event); |
| 121 | + verify(action, times(2)).call(Matchers.<KeyEvent>any()); |
| 122 | + |
| 123 | + sub.unsubscribe(); |
| 124 | + fireKeyEvent(event); |
| 125 | + verify(action, times(2)).call(Matchers.<KeyEvent>any()); |
| 126 | + verify(error, never()).call(Matchers.<Exception>any()); |
| 127 | + verify(complete, never()).call(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testObservingPressedKeys() { |
| 132 | + @SuppressWarnings("unchecked") |
| 133 | + Action1<Set<Integer>> action = mock(Action1.class); |
| 134 | + @SuppressWarnings("unchecked") |
| 135 | + Action1<Exception> error = mock(Action1.class); |
| 136 | + Action0 complete = mock(Action0.class); |
| 137 | + |
| 138 | + Subscription sub = currentlyPressedKeysOf(comp).subscribe(action, error, complete); |
| 139 | + |
| 140 | + InOrder inOrder = inOrder(action); |
| 141 | + inOrder.verify(action, times(1)).call(Collections.<Integer>emptySet()); |
| 142 | + verify(error, never()).call(Matchers.<Exception>any()); |
| 143 | + verify(complete, never()).call(); |
| 144 | + |
| 145 | + fireKeyEvent(keyEvent(1, KeyEvent.KEY_PRESSED)); |
| 146 | + inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1))); |
| 147 | + verify(error, never()).call(Matchers.<Exception>any()); |
| 148 | + verify(complete, never()).call(); |
| 149 | + |
| 150 | + fireKeyEvent(keyEvent(2, KeyEvent.KEY_PRESSED)); |
| 151 | + inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1, 2))); |
| 152 | + |
| 153 | + fireKeyEvent(keyEvent(2, KeyEvent.KEY_RELEASED)); |
| 154 | + inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1))); |
| 155 | + |
| 156 | + fireKeyEvent(keyEvent(3, KeyEvent.KEY_RELEASED)); |
| 157 | + inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1))); |
| 158 | + |
| 159 | + fireKeyEvent(keyEvent(1, KeyEvent.KEY_RELEASED)); |
| 160 | + inOrder.verify(action, times(1)).call(Collections.<Integer>emptySet()); |
| 161 | + |
| 162 | + sub.unsubscribe(); |
| 163 | + |
| 164 | + fireKeyEvent(keyEvent(1, KeyEvent.KEY_PRESSED)); |
| 165 | + inOrder.verify(action, never()).call(Matchers.<Set<Integer>>any()); |
| 166 | + verify(error, never()).call(Matchers.<Exception>any()); |
| 167 | + verify(complete, never()).call(); |
| 168 | + } |
| 169 | + |
| 170 | + private KeyEvent keyEvent(int keyCode, int id) { |
| 171 | + return new KeyEvent(comp, id, -1L, 0, keyCode, ' '); |
| 172 | + } |
| 173 | + |
| 174 | + private void fireKeyEvent(KeyEvent event) { |
| 175 | + for (KeyListener listener: comp.getKeyListeners()) { |
| 176 | + listener.keyTyped(event); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments