diff --git a/core/src/test/java/edu/umd/cs/piccolo/MockPInputEventListener.java b/core/src/test/java/edu/umd/cs/piccolo/MockPInputEventListener.java new file mode 100644 index 0000000..f1953b2 --- /dev/null +++ b/core/src/test/java/edu/umd/cs/piccolo/MockPInputEventListener.java @@ -0,0 +1,34 @@ +package edu.umd.cs.piccolo; + +import java.util.ArrayList; +import java.util.List; + +import edu.umd.cs.piccolo.event.PInputEvent; +import edu.umd.cs.piccolo.event.PInputEventListener; + +public class MockPInputEventListener implements PInputEventListener { + class Notification { + public PInputEvent event; + public int type; + + public Notification(PInputEvent event, int type) { + this.event = event; + this.type = type; + } + } + + private List notifications = new ArrayList(); + + public void processEvent(PInputEvent aEvent, int type) { + notifications.add(new Notification(aEvent, type)); + } + + public int getNotificationCount() { + return notifications.size(); + } + + public Notification getNotification(int index) { + return (Notification) notifications.get(index); + } + +} diff --git a/core/src/test/java/edu/umd/cs/piccolo/PInputManagerTest.java b/core/src/test/java/edu/umd/cs/piccolo/PInputManagerTest.java new file mode 100644 index 0000000..6484acd --- /dev/null +++ b/core/src/test/java/edu/umd/cs/piccolo/PInputManagerTest.java @@ -0,0 +1,108 @@ +package edu.umd.cs.piccolo; + +import java.awt.event.FocusEvent; +import java.awt.event.KeyEvent; +import java.awt.geom.Point2D; + +import junit.framework.TestCase; +import edu.umd.cs.piccolo.event.PInputEvent; +import edu.umd.cs.piccolo.event.PInputEventListener; +import edu.umd.cs.piccolo.util.PBounds; +import edu.umd.cs.piccolo.util.PPickPath; + +public class PInputManagerTest extends TestCase { + private PInputManager manager; + + public void setUp() { + manager = new PInputManager(); + } + + public void testGetKeyboardFocusNullByDefault() { + assertNull(manager.getKeyboardFocus()); + } + + public void testSetKeyboardFocusIsPersisted() { + PInputEventListener listener = new MockPInputEventListener(); + manager.setKeyboardFocus(listener); + assertEquals(listener, manager.getKeyboardFocus()); + } + + public void testSetKeyboardFocusDispatchesEventsAboutFocus() { + MockPInputEventListener oldListener = new MockPInputEventListener(); + manager.setKeyboardFocus(oldListener); + + assertEquals(1, oldListener.getNotificationCount()); + assertEquals(FocusEvent.FOCUS_GAINED, + oldListener.getNotification(0).type); + + MockPInputEventListener newListener = new MockPInputEventListener(); + manager.setKeyboardFocus(newListener); + + assertEquals(1, newListener.getNotificationCount()); + assertEquals(FocusEvent.FOCUS_GAINED, + newListener.getNotification(0).type); + assertEquals(2, oldListener.getNotificationCount()); + assertEquals(FocusEvent.FOCUS_LOST, oldListener.getNotification(1).type); + } + + public void testGetMouseFocusNullByDefault() { + assertNull(manager.getMouseFocus()); + } + + public void testSetMouseFocusPersists() { + PCamera camera = new PCamera(); + PPickPath path = new PPickPath(camera, new PBounds(0, 0, 10, 10)); + manager.setMouseFocus(path); + assertEquals(path, manager.getMouseFocus()); + } + + public void testGetMouseOverNullByDefault() { + assertNull(manager.getMouseOver()); + } + + public void testSetMouseOverPersists() { + PCamera camera = new PCamera(); + PPickPath path = new PPickPath(camera, new PBounds(0, 0, 10, 10)); + manager.setMouseOver(path); + assertEquals(path, manager.getMouseOver()); + } + + public void testGetCurrentCanvasPositionIsOriginByDefault() { + assertEquals(new Point2D.Double(0, 0), manager + .getCurrentCanvasPosition()); + } + + public void testGetLastCanvasPositionIsOriginByDefault() { + assertEquals(new Point2D.Double(0, 0), manager.getLastCanvasPosition()); + } + + public void testKeyPressedDispatchesToCurrentFocus() { + MockPInputEventListener mockListener = new MockPInputEventListener(); + manager.setKeyboardFocus(mockListener); + PInputEvent event = new PInputEvent(manager, null, null); + manager.keyPressed(event); + assertEquals(2, mockListener.getNotificationCount()); + assertEquals(KeyEvent.KEY_PRESSED, mockListener.getNotification(1).type); + } + public void testKeyReleasedDispatchesToCurrentFocus() { + MockPInputEventListener mockListener = new MockPInputEventListener(); + manager.setKeyboardFocus(mockListener); + PInputEvent event = new PInputEvent(manager, null, null); + manager.keyReleased(event); + assertEquals(2, mockListener.getNotificationCount()); + assertEquals(KeyEvent.KEY_RELEASED, mockListener.getNotification(1).type); + } + + public void testKeyTypedDispatchesToCurrentFocus() { + MockPInputEventListener mockListener = new MockPInputEventListener(); + manager.setKeyboardFocus(mockListener); + PInputEvent event = new PInputEvent(manager, null, null); + manager.keyTyped(event); + assertEquals(2, mockListener.getNotificationCount()); + assertEquals(KeyEvent.KEY_TYPED, mockListener.getNotification(1).type); + } + + public void testProcessInputMayBeCalledOnFreshManager() { + manager.processInput(); + } +}