Skip to content

Commit

Permalink
Merge pull request #6 from eddieburns55/mousewheel-events
Browse files Browse the repository at this point in the history
add mousewheel event support and extra unit tests
  • Loading branch information
benjchristensen committed Feb 3, 2015
2 parents 1e41f78 + 2a97b90 commit 1b63cbb
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 17 deletions.
17 changes: 12 additions & 5 deletions src/main/java/rx/observables/SwingObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.FocusEvent;
import java.awt.event.*;
import java.util.Set;

import javax.swing.AbstractButton;
Expand Down Expand Up @@ -121,6 +117,17 @@ public static Observable<MouseEvent> fromMouseMotionEvents(Component component)
public static Observable<Point> fromRelativeMouseMotion(Component component) {
return MouseEventSource.fromRelativeMouseMotion(component);
}

/**
* Creates an observable corresponding to raw mouse wheel events.
*
* @param component
* The component to register the observable for.
* @return The component to register the observable for.
*/
public static Observable<MouseWheelEvent> fromMouseWheelEvents(Component component) {
return MouseEventSource.fromMouseWheelEvents(component);
}

/**
* Creates an observable corresponding to raw component events.
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/rx/swing/sources/MouseEventSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.*;

import rx.Observable;
import rx.Observable.OnSubscribe;
Expand Down Expand Up @@ -109,6 +107,28 @@ public void call() {
});
}

public static Observable<MouseWheelEvent> fromMouseWheelEvents(final Component component){
return Observable.create(new OnSubscribe<MouseWheelEvent>() {
@Override
public void call(final Subscriber<? super MouseWheelEvent> subscriber) {
final MouseWheelListener listener = new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent event) {
subscriber.onNext(event);
}
};
component.addMouseWheelListener(listener);

subscriber.add(SwingSubscriptions.unsubscribeInEventDispatchThread(new Action0() {
@Override
public void call() {
component.removeMouseWheelListener(listener);
}
}));
}
});
}

/**
* @see rx.observables.SwingObservable#fromRelativeMouseMotion
*/
Expand Down
128 changes: 119 additions & 9 deletions src/test/java/rx/swing/sources/MouseEventSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.*;


import javax.swing.JPanel;

Expand Down Expand Up @@ -60,17 +60,17 @@ public void call() {
verify(error, never()).call(Matchers.<Exception> any());
verify(complete, never()).call();

fireMouseEvent(mouseEvent(0, 0));
fireMouseMotionEvent(mouseEvent(0, 0, MouseEvent.MOUSE_MOVED));
verify(action, never()).call(Matchers.<Point> any());

fireMouseEvent(mouseEvent(10, -5));
fireMouseMotionEvent(mouseEvent(10, -5, MouseEvent.MOUSE_MOVED));
inOrder.verify(action, times(1)).call(new Point(10, -5));

fireMouseEvent(mouseEvent(6, 10));
fireMouseMotionEvent(mouseEvent(6, 10, MouseEvent.MOUSE_MOVED));
inOrder.verify(action, times(1)).call(new Point(-4, 15));

sub.unsubscribe();
fireMouseEvent(mouseEvent(0, 0));
fireMouseMotionEvent(mouseEvent(0, 0, MouseEvent.MOUSE_MOVED));
inOrder.verify(action, never()).call(Matchers.<Point> any());
verify(error, never()).call(Matchers.<Exception> any());
verify(complete, never()).call();
Expand All @@ -79,14 +79,124 @@ public void call() {
}).awaitTerminal();
}

private MouseEvent mouseEvent(int x, int y) {
return new MouseEvent(comp, MouseEvent.MOUSE_MOVED, 1L, 0, x, y, 0,
@Test
public void testMouseEvents() throws Throwable {
SwingTestHelper.create().runInEventDispatchThread(new Action0() {

@Override
public void call() {
@SuppressWarnings("unchecked")
Action1<MouseEvent> action = mock(Action1.class);
@SuppressWarnings("unchecked")
Action1<Throwable> error = mock(Action1.class);
Action0 complete = mock(Action0.class);

Subscription sub = MouseEventSource.fromMouseEventsOf(comp)
.subscribe(action, error, complete);

InOrder inOrder = inOrder(action);

verify(action, never()).call(Matchers.<MouseEvent> any());
verify(error, never()).call(Matchers.<Exception> any());
verify(complete, never()).call();

MouseEvent mouseEvent =
mouseEvent(0, 0, MouseEvent.MOUSE_CLICKED);
fireMouseClickEvent(mouseEvent);
inOrder.verify(action, times(1)).call(mouseEvent);

mouseEvent = mouseEvent(300, 200, MouseEvent.MOUSE_CLICKED);
fireMouseClickEvent(mouseEvent);
inOrder.verify(action, times(1)).call(mouseEvent);

mouseEvent = mouseEvent(0, 0, MouseEvent.MOUSE_CLICKED);
fireMouseClickEvent(mouseEvent);
inOrder.verify(action, times(1)).call(mouseEvent);

sub.unsubscribe();
fireMouseClickEvent(mouseEvent(0, 0, MouseEvent.MOUSE_CLICKED));
inOrder.verify(action, never()).call(Matchers.<MouseEvent> any());
verify(error, never()).call(Matchers.<Exception> any());
verify(complete, never()).call();
}

}).awaitTerminal();
}

@Test
public void testMouseWheelEvents() throws Throwable {
SwingTestHelper.create().runInEventDispatchThread(new Action0() {

@Override
public void call() {
@SuppressWarnings("unchecked")
Action1<MouseEvent> action = mock(Action1.class);
@SuppressWarnings("unchecked")
Action1<Throwable> error = mock(Action1.class);
Action0 complete = mock(Action0.class);

Subscription sub = MouseEventSource.fromMouseWheelEvents(comp)
.subscribe(action, error, complete);

InOrder inOrder = inOrder(action);

verify(action, never()).call(Matchers.<MouseEvent> any());
verify(error, never()).call(Matchers.<Exception> any());
verify(complete, never()).call();

MouseWheelEvent mouseEvent = mouseWheelEvent(0);
fireMouseWheelEvent(mouseEvent);
inOrder.verify(action, times(1)).call(mouseEvent);

mouseEvent = mouseWheelEvent(3);
fireMouseWheelEvent(mouseEvent);
inOrder.verify(action, times(1)).call(mouseEvent);

mouseEvent = mouseWheelEvent(5);
fireMouseWheelEvent(mouseEvent);
inOrder.verify(action, times(1)).call(mouseEvent);

mouseEvent = mouseWheelEvent(1);
fireMouseWheelEvent(mouseEvent);
inOrder.verify(action, times(1)).call(mouseEvent);

sub.unsubscribe();
fireMouseClickEvent(mouseEvent(0, 0, MouseEvent.MOUSE_CLICKED));
inOrder.verify(action, never()).call(Matchers.<MouseEvent> any());
verify(error, never()).call(Matchers.<Exception> any());
verify(complete, never()).call();
}

}).awaitTerminal();
}

private MouseEvent mouseEvent(int x, int y, int mouseEventType) {
return new MouseEvent(comp, mouseEventType, 1L, 0, x, y, 0,
false);
}

private void fireMouseEvent(MouseEvent event) {
private void fireMouseMotionEvent(MouseEvent event) {
for (MouseMotionListener listener : comp.getMouseMotionListeners()) {
listener.mouseMoved(event);
}
}

private void fireMouseClickEvent(MouseEvent event) {
for (MouseListener listener : comp.getMouseListeners()) {
listener.mouseClicked(event);
}
}

private MouseWheelEvent mouseWheelEvent(int wheelRotationClicks) {
int mouseEventType = MouseEvent.MOUSE_WHEEL;
return new MouseWheelEvent(comp, mouseEventType, 1L, 0, 0, 0, 0,
false, MouseWheelEvent.WHEEL_BLOCK_SCROLL, 0,
wheelRotationClicks);
}

private void fireMouseWheelEvent(MouseWheelEvent event) {
for (MouseWheelListener listener : comp.getMouseWheelListeners()) {
listener.mouseWheelMoved(event);
}
}
}

0 comments on commit 1b63cbb

Please sign in to comment.