Author: allain.lalonde
Date: Mon Jul 13 09:01:00 2009
New Revision: 452
Added:
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/MockPInputEventListener.java
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/PInputManagerTest.java
Log:
Added some tests for PInputManagerTest. Difficult to create a mock PickPath
so the mouse specific code is largely untested.
Added:
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/MockPInputEventListener.java
==============================================================================
--- (empty file)
+++
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/MockPInputEventListener.java
Mon Jul 13 09:01:00 2009
@@ -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);
+ }
+
+}
Added:
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/PInputManagerTest.java
==============================================================================
--- (empty file)
+++
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/PInputManagerTest.java
Mon Jul 13 09:01:00 2009
@@ -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();
+ }
+}
--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---