Skip to content

Commit 6f8b335

Browse files
committed
Extract the Swing unit tests so that test classes don't have to be on the main compile classpath.
1 parent fab6b5b commit 6f8b335

File tree

8 files changed

+431
-342
lines changed

8 files changed

+431
-342
lines changed

rxjava-contrib/rxjava-swing/src/main/java/rx/schedulers/SwingScheduler.java

Lines changed: 7 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,20 @@
1515
*/
1616
package rx.schedulers;
1717

18-
import static org.junit.Assert.*;
19-
import static org.mockito.Mockito.*;
20-
21-
import java.awt.EventQueue;
22-
import java.awt.event.ActionEvent;
23-
import java.awt.event.ActionListener;
24-
import java.util.concurrent.CountDownLatch;
25-
import java.util.concurrent.TimeUnit;
26-
import java.util.concurrent.atomic.AtomicReference;
27-
28-
import javax.swing.SwingUtilities;
29-
import javax.swing.Timer;
30-
31-
import org.junit.Rule;
32-
import org.junit.Test;
33-
import org.junit.rules.ExpectedException;
34-
import org.mockito.InOrder;
35-
3618
import rx.Scheduler;
3719
import rx.Subscription;
3820
import rx.subscriptions.CompositeSubscription;
3921
import rx.subscriptions.Subscriptions;
4022
import rx.util.functions.Action0;
4123
import rx.util.functions.Func2;
4224

25+
import javax.swing.*;
26+
import java.awt.*;
27+
import java.awt.event.ActionEvent;
28+
import java.awt.event.ActionListener;
29+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.atomic.AtomicReference;
31+
4332
/**
4433
* Executes work on the Swing UI thread.
4534
* This scheduler should only be used with actions that execute quickly.
@@ -157,118 +146,4 @@ private static void assertThatTheDelayIsValidForTheSwingTimer(long delay) {
157146
}
158147
}
159148

160-
public static class UnitTest {
161-
@Rule
162-
public ExpectedException exception = ExpectedException.none();
163-
164-
@Test
165-
public void testInvalidDelayValues() {
166-
final SwingScheduler scheduler = new SwingScheduler();
167-
final Action0 action = mock(Action0.class);
168-
169-
exception.expect(IllegalArgumentException.class);
170-
scheduler.schedulePeriodically(action, -1L, 100L, TimeUnit.SECONDS);
171-
172-
exception.expect(IllegalArgumentException.class);
173-
scheduler.schedulePeriodically(action, 100L, -1L, TimeUnit.SECONDS);
174-
175-
exception.expect(IllegalArgumentException.class);
176-
scheduler.schedulePeriodically(action, 1L + Integer.MAX_VALUE, 100L, TimeUnit.MILLISECONDS);
177-
178-
exception.expect(IllegalArgumentException.class);
179-
scheduler.schedulePeriodically(action, 100L, 1L + Integer.MAX_VALUE / 1000, TimeUnit.SECONDS);
180-
}
181-
182-
@Test
183-
public void testPeriodicScheduling() throws Exception {
184-
final SwingScheduler scheduler = new SwingScheduler();
185-
186-
final CountDownLatch latch = new CountDownLatch(4);
187-
188-
final Action0 innerAction = mock(Action0.class);
189-
final Action0 action = new Action0() {
190-
@Override
191-
public void call() {
192-
try {
193-
innerAction.call();
194-
assertTrue(SwingUtilities.isEventDispatchThread());
195-
} finally {
196-
latch.countDown();
197-
}
198-
}
199-
};
200-
201-
Subscription sub = scheduler.schedulePeriodically(action, 50, 200, TimeUnit.MILLISECONDS);
202-
203-
if (!latch.await(5000, TimeUnit.MILLISECONDS)) {
204-
fail("timed out waiting for tasks to execute");
205-
}
206-
207-
sub.unsubscribe();
208-
waitForEmptyEventQueue();
209-
verify(innerAction, times(4)).call();
210-
}
211-
212-
@Test
213-
public void testNestedActions() throws Exception {
214-
final SwingScheduler scheduler = new SwingScheduler();
215-
216-
final Action0 firstStepStart = mock(Action0.class);
217-
final Action0 firstStepEnd = mock(Action0.class);
218-
219-
final Action0 secondStepStart = mock(Action0.class);
220-
final Action0 secondStepEnd = mock(Action0.class);
221-
222-
final Action0 thirdStepStart = mock(Action0.class);
223-
final Action0 thirdStepEnd = mock(Action0.class);
224-
225-
final Action0 firstAction = new Action0() {
226-
@Override
227-
public void call() {
228-
assertTrue(SwingUtilities.isEventDispatchThread());
229-
firstStepStart.call();
230-
firstStepEnd.call();
231-
}
232-
};
233-
final Action0 secondAction = new Action0() {
234-
@Override
235-
public void call() {
236-
assertTrue(SwingUtilities.isEventDispatchThread());
237-
secondStepStart.call();
238-
scheduler.schedule(firstAction);
239-
secondStepEnd.call();
240-
}
241-
};
242-
final Action0 thirdAction = new Action0() {
243-
@Override
244-
public void call() {
245-
assertTrue(SwingUtilities.isEventDispatchThread());
246-
thirdStepStart.call();
247-
scheduler.schedule(secondAction);
248-
thirdStepEnd.call();
249-
}
250-
};
251-
252-
InOrder inOrder = inOrder(firstStepStart, firstStepEnd, secondStepStart, secondStepEnd, thirdStepStart, thirdStepEnd);
253-
254-
scheduler.schedule(thirdAction);
255-
waitForEmptyEventQueue();
256-
257-
inOrder.verify(thirdStepStart, times(1)).call();
258-
inOrder.verify(thirdStepEnd, times(1)).call();
259-
inOrder.verify(secondStepStart, times(1)).call();
260-
inOrder.verify(secondStepEnd, times(1)).call();
261-
inOrder.verify(firstStepStart, times(1)).call();
262-
inOrder.verify(firstStepEnd, times(1)).call();
263-
}
264-
265-
private static void waitForEmptyEventQueue() throws Exception {
266-
EventQueue.invokeAndWait(new Runnable() {
267-
@Override
268-
public void run() {
269-
// nothing to do, we're just waiting here for the event queue to be emptied
270-
}
271-
});
272-
}
273-
}
274149
}

rxjava-contrib/rxjava-swing/src/main/java/rx/swing/sources/AbstractButtonSource.java

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,16 @@
1515
*/
1616
package rx.swing.sources;
1717

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-
2818
import rx.Observable;
2919
import rx.Observable.OnSubscribeFunc;
3020
import rx.Observer;
3121
import rx.Subscription;
3222
import rx.subscriptions.Subscriptions;
3323
import rx.util.functions.Action0;
34-
import rx.util.functions.Action1;
24+
25+
import javax.swing.*;
26+
import java.awt.event.ActionEvent;
27+
import java.awt.event.ActionListener;
3528

3629
public enum AbstractButtonSource { ; // no instances
3730

@@ -60,42 +53,4 @@ public void call() {
6053
});
6154
}
6255

63-
public static class UnitTest {
64-
@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);
78-
}
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();
99-
}
100-
}
10156
}

rxjava-contrib/rxjava-swing/src/main/java/rx/swing/sources/KeyEventSource.java

Lines changed: 7 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,22 @@
1515
*/
1616
package rx.swing.sources;
1717

18-
import static java.util.Arrays.*;
19-
import static org.mockito.Mockito.*;
20-
21-
import java.awt.Component;
22-
import java.awt.event.KeyEvent;
23-
import java.awt.event.KeyListener;
24-
import java.util.Collections;
25-
import java.util.HashSet;
26-
import java.util.Set;
27-
28-
import javax.swing.JPanel;
29-
30-
import org.junit.Test;
31-
import org.mockito.InOrder;
32-
import org.mockito.Matchers;
33-
3418
import rx.Observable;
3519
import rx.Observable.OnSubscribeFunc;
3620
import rx.Observer;
3721
import rx.Subscription;
3822
import rx.subscriptions.Subscriptions;
3923
import rx.util.functions.Action0;
40-
import rx.util.functions.Action1;
4124
import rx.util.functions.Func1;
4225
import rx.util.functions.Func2;
4326

27+
import java.awt.*;
28+
import java.awt.event.KeyEvent;
29+
import java.awt.event.KeyListener;
30+
import java.util.Collections;
31+
import java.util.HashSet;
32+
import java.util.Set;
33+
4434
public enum KeyEventSource { ; // no instances
4535

4636
/**
@@ -110,88 +100,5 @@ public Boolean call(KeyEvent event) {
110100

111101
return filteredKeyEvents.scan(Collections.<Integer>emptySet(), new CollectKeys());
112102
}
113-
114-
public static class UnitTest {
115-
private Component comp = new JPanel();
116-
117-
@Test
118-
public void testObservingKeyEvents() {
119-
@SuppressWarnings("unchecked")
120-
Action1<KeyEvent> action = mock(Action1.class);
121-
@SuppressWarnings("unchecked")
122-
Action1<Throwable> error = mock(Action1.class);
123-
Action0 complete = mock(Action0.class);
124-
125-
final KeyEvent event = mock(KeyEvent.class);
126-
127-
Subscription sub = fromKeyEventsOf(comp).subscribe(action, error, complete);
128-
129-
verify(action, never()).call(Matchers.<KeyEvent>any());
130-
verify(error, never()).call(Matchers.<Throwable>any());
131-
verify(complete, never()).call();
132-
133-
fireKeyEvent(event);
134-
verify(action, times(1)).call(Matchers.<KeyEvent>any());
135-
136-
fireKeyEvent(event);
137-
verify(action, times(2)).call(Matchers.<KeyEvent>any());
138-
139-
sub.unsubscribe();
140-
fireKeyEvent(event);
141-
verify(action, times(2)).call(Matchers.<KeyEvent>any());
142-
verify(error, never()).call(Matchers.<Throwable>any());
143-
verify(complete, never()).call();
144-
}
145-
146-
@Test
147-
public void testObservingPressedKeys() {
148-
@SuppressWarnings("unchecked")
149-
Action1<Set<Integer>> action = mock(Action1.class);
150-
@SuppressWarnings("unchecked")
151-
Action1<Throwable> error = mock(Action1.class);
152-
Action0 complete = mock(Action0.class);
153-
154-
Subscription sub = currentlyPressedKeysOf(comp).subscribe(action, error, complete);
155-
156-
InOrder inOrder = inOrder(action);
157-
inOrder.verify(action, times(1)).call(Collections.<Integer>emptySet());
158-
verify(error, never()).call(Matchers.<Throwable>any());
159-
verify(complete, never()).call();
160-
161-
fireKeyEvent(keyEvent(1, KeyEvent.KEY_PRESSED));
162-
inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1)));
163-
verify(error, never()).call(Matchers.<Throwable>any());
164-
verify(complete, never()).call();
165-
166-
fireKeyEvent(keyEvent(2, KeyEvent.KEY_PRESSED));
167-
fireKeyEvent(keyEvent(KeyEvent.VK_UNDEFINED, KeyEvent.KEY_TYPED));
168-
inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1, 2)));
169-
170-
fireKeyEvent(keyEvent(2, KeyEvent.KEY_RELEASED));
171-
inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1)));
172-
173-
fireKeyEvent(keyEvent(3, KeyEvent.KEY_RELEASED));
174-
inOrder.verify(action, times(1)).call(new HashSet<Integer>(asList(1)));
175-
176-
fireKeyEvent(keyEvent(1, KeyEvent.KEY_RELEASED));
177-
inOrder.verify(action, times(1)).call(Collections.<Integer>emptySet());
178-
179-
sub.unsubscribe();
180103

181-
fireKeyEvent(keyEvent(1, KeyEvent.KEY_PRESSED));
182-
inOrder.verify(action, never()).call(Matchers.<Set<Integer>>any());
183-
verify(error, never()).call(Matchers.<Throwable>any());
184-
verify(complete, never()).call();
185-
}
186-
187-
private KeyEvent keyEvent(int keyCode, int id) {
188-
return new KeyEvent(comp, id, -1L, 0, keyCode, ' ');
189-
}
190-
191-
private void fireKeyEvent(KeyEvent event) {
192-
for (KeyListener listener: comp.getKeyListeners()) {
193-
listener.keyTyped(event);
194-
}
195-
}
196-
}
197104
}

0 commit comments

Comments
 (0)