|
15 | 15 | */
|
16 | 16 | package rx.swing.sources;
|
17 | 17 |
|
18 |
| -import static org.mockito.Mockito.*; |
| 18 | +import static org.mockito.Mockito.mock; |
| 19 | +import static org.mockito.Mockito.never; |
| 20 | +import static org.mockito.Mockito.times; |
| 21 | +import static org.mockito.Mockito.verify; |
19 | 22 |
|
20 | 23 | import java.awt.event.ActionEvent;
|
21 | 24 | import java.awt.event.ActionListener;
|
|
26 | 29 | import org.mockito.Matchers;
|
27 | 30 |
|
28 | 31 | import rx.Observable;
|
29 |
| -import rx.Observable.OnSubscribeFunc; |
30 |
| -import rx.Observer; |
| 32 | +import rx.Observable.OnSubscribe; |
| 33 | +import rx.Subscriber; |
31 | 34 | import rx.Subscription;
|
32 | 35 | import rx.functions.Action0;
|
33 | 36 | import rx.functions.Action1;
|
34 |
| -import rx.subscriptions.Subscriptions; |
| 37 | +import rx.observables.SwingObservable; |
| 38 | +import rx.subscriptions.SwingSubscriptions; |
35 | 39 |
|
36 | 40 | public enum AbstractButtonSource { ; // no instances
|
37 | 41 |
|
38 | 42 | /**
|
39 | 43 | * @see rx.observables.SwingObservable#fromButtonAction
|
40 | 44 | */
|
41 | 45 | public static Observable<ActionEvent> fromActionOf(final AbstractButton button) {
|
42 |
| - return Observable.create(new OnSubscribeFunc<ActionEvent>() { |
| 46 | + return Observable.create(new OnSubscribe<ActionEvent>() { |
43 | 47 | @Override
|
44 |
| - public Subscription onSubscribe(final Observer<? super ActionEvent> observer) { |
| 48 | + public void call(final Subscriber<? super ActionEvent> subscriber) { |
| 49 | + SwingObservable.assertEventDispatchThread(); |
45 | 50 | final ActionListener listener = new ActionListener() {
|
46 | 51 | @Override
|
47 | 52 | public void actionPerformed(ActionEvent e) {
|
48 |
| - observer.onNext(e); |
| 53 | + subscriber.onNext(e); |
49 | 54 | }
|
50 | 55 | };
|
51 | 56 | button.addActionListener(listener);
|
52 |
| - |
53 |
| - return Subscriptions.create(new Action0() { |
| 57 | + subscriber.add(SwingSubscriptions.unsubscribeInEventDispatchThread(new Action0() { |
54 | 58 | @Override
|
55 | 59 | public void call() {
|
56 | 60 | button.removeActionListener(listener);
|
57 | 61 | }
|
58 |
| - }); |
| 62 | + })); |
59 | 63 | }
|
60 | 64 | });
|
61 | 65 | }
|
62 | 66 |
|
63 | 67 | public static class UnitTest {
|
64 | 68 | @Test
|
65 |
| - public void testObservingActionEvents() { |
66 |
| - @SuppressWarnings("unchecked") |
67 |
| - Action1<ActionEvent> action = mock(Action1.class); |
68 |
| - @SuppressWarnings("unchecked") |
69 |
| - Action1<Throwable> error = mock(Action1.class); |
70 |
| - Action0 complete = mock(Action0.class); |
71 |
| - |
72 |
| - final ActionEvent event = new ActionEvent(this, 1, "command"); |
73 |
| - |
74 |
| - @SuppressWarnings("serial") |
75 |
| - class TestButton extends AbstractButton { |
76 |
| - void testAction() { |
77 |
| - fireActionPerformed(event); |
| 69 | + public void testObservingActionEvents() throws Throwable { |
| 70 | + SwingTestHelper.create().runInEventDispatchThread(new Action0() { |
| 71 | + |
| 72 | + @Override |
| 73 | + public void call() { |
| 74 | + @SuppressWarnings("unchecked") |
| 75 | + Action1<ActionEvent> action = mock(Action1.class); |
| 76 | + @SuppressWarnings("unchecked") |
| 77 | + Action1<Throwable> error = mock(Action1.class); |
| 78 | + Action0 complete = mock(Action0.class); |
| 79 | + |
| 80 | + final ActionEvent event = new ActionEvent(this, 1, "command"); |
| 81 | + |
| 82 | + @SuppressWarnings("serial") |
| 83 | + class TestButton extends AbstractButton { |
| 84 | + void testAction() { |
| 85 | + fireActionPerformed(event); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + TestButton button = new TestButton(); |
| 90 | + Subscription sub = fromActionOf(button).subscribe(action, error, complete); |
| 91 | + |
| 92 | + verify(action, never()).call(Matchers.<ActionEvent> any()); |
| 93 | + verify(error, never()).call(Matchers.<Throwable> any()); |
| 94 | + verify(complete, never()).call(); |
| 95 | + |
| 96 | + button.testAction(); |
| 97 | + verify(action, times(1)).call(Matchers.<ActionEvent> any()); |
| 98 | + |
| 99 | + button.testAction(); |
| 100 | + verify(action, times(2)).call(Matchers.<ActionEvent> any()); |
| 101 | + |
| 102 | + sub.unsubscribe(); |
| 103 | + button.testAction(); |
| 104 | + verify(action, times(2)).call(Matchers.<ActionEvent> any()); |
| 105 | + verify(error, never()).call(Matchers.<Throwable> any()); |
| 106 | + verify(complete, never()).call(); |
78 | 107 | }
|
79 |
| - } |
80 |
| - |
81 |
| - TestButton button = new TestButton(); |
82 |
| - Subscription sub = fromActionOf(button).subscribe(action, error, complete); |
83 |
| - |
84 |
| - verify(action, never()).call(Matchers.<ActionEvent>any()); |
85 |
| - verify(error, never()).call(Matchers.<Throwable>any()); |
86 |
| - verify(complete, never()).call(); |
87 |
| - |
88 |
| - button.testAction(); |
89 |
| - verify(action, times(1)).call(Matchers.<ActionEvent>any()); |
90 |
| - |
91 |
| - button.testAction(); |
92 |
| - verify(action, times(2)).call(Matchers.<ActionEvent>any()); |
93 |
| - |
94 |
| - sub.unsubscribe(); |
95 |
| - button.testAction(); |
96 |
| - verify(action, times(2)).call(Matchers.<ActionEvent>any()); |
97 |
| - verify(error, never()).call(Matchers.<Throwable>any()); |
98 |
| - verify(complete, never()).call(); |
| 108 | + |
| 109 | + }).awaitTerminal(); |
99 | 110 | }
|
100 | 111 | }
|
101 | 112 | }
|
0 commit comments