Skip to content

Commit f81e592

Browse files
author
jmhofer
committed
Allow creating observables from AbstractButtons.
1 parent 7d39ba4 commit f81e592

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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;
17+
18+
import static org.mockito.Mockito.*;
19+
20+
import java.awt.event.ActionEvent;
21+
import java.awt.event.ActionListener;
22+
23+
import javax.swing.AbstractButton;
24+
25+
import org.junit.Test;
26+
import org.mockito.Matchers;
27+
28+
import rx.subscriptions.Subscriptions;
29+
import rx.util.functions.Action0;
30+
import rx.util.functions.Action1;
31+
import rx.util.functions.Func1;
32+
33+
public enum SwingObservable { ; // no instances
34+
35+
public static Observable<ActionEvent> fromButtonAction(final AbstractButton button) {
36+
return Observable.create(new Func1<Observer<ActionEvent>, Subscription>() {
37+
@Override
38+
public Subscription call(final Observer<ActionEvent> observer) {
39+
final ActionListener listener = new ActionListener() {
40+
@Override
41+
public void actionPerformed(ActionEvent e) {
42+
observer.onNext(e);
43+
}
44+
};
45+
button.addActionListener(listener);
46+
47+
return Subscriptions.create(new Action0() {
48+
@Override
49+
public void call() {
50+
button.removeActionListener(listener);
51+
}
52+
});
53+
}
54+
});
55+
}
56+
57+
public static class UnitTest {
58+
@Test
59+
public void testObservingActionEvents() {
60+
@SuppressWarnings("unchecked")
61+
Action1<ActionEvent> action = mock(Action1.class);
62+
@SuppressWarnings("unchecked")
63+
Action1<Exception> error = mock(Action1.class);
64+
Action0 complete = mock(Action0.class);
65+
66+
final ActionEvent event = new ActionEvent(this, 1, "command");
67+
68+
class TestButton extends AbstractButton {
69+
void testAction() {
70+
fireActionPerformed(event);
71+
}
72+
}
73+
74+
TestButton button = new TestButton();
75+
Subscription sub = fromButtonAction(button).subscribe(action, error, complete);
76+
77+
verify(action, never()).call(Matchers.<ActionEvent>any());
78+
verify(error, never()).call(Matchers.<Exception>any());
79+
verify(complete, never()).call();
80+
81+
button.testAction();
82+
verify(action, times(1)).call(Matchers.<ActionEvent>any());
83+
84+
button.testAction();
85+
verify(action, times(2)).call(Matchers.<ActionEvent>any());
86+
87+
sub.unsubscribe();
88+
button.testAction();
89+
verify(action, times(2)).call(Matchers.<ActionEvent>any());
90+
verify(error, never()).call(Matchers.<Exception>any());
91+
verify(complete, never()).call();
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)