Skip to content

Commit 87b53fa

Browse files
author
jmhofer
committed
Refactored implementation out from SwingObservable, as there will
probably be lots more methods in the long run.
1 parent f81e592 commit 87b53fa

File tree

2 files changed

+88
-68
lines changed

2 files changed

+88
-68
lines changed

extensions/rxjava-swing/src/main/java/rx/SwingObservable.java

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,80 +15,15 @@
1515
*/
1616
package rx;
1717

18-
import static org.mockito.Mockito.*;
19-
2018
import java.awt.event.ActionEvent;
21-
import java.awt.event.ActionListener;
2219

2320
import javax.swing.AbstractButton;
2421

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;
22+
import rx.swing.sources.AbstractButtonSource;
3223

3324
public enum SwingObservable { ; // no instances
3425

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

0 commit comments

Comments
 (0)