Package greenfoot.junitUtils
Class EventDispatch
- java.lang.Object
-
- greenfoot.junitUtils.EventDispatch
-
public class EventDispatch extends java.lang.Object
It simulates input events of keyboard or mouse for Test Driven Development (TDD) with JUnit. If we wants to verify the behaviour of any actor or world that receives any keyboard key or mouse click, the test can generate this event and after verify the result.- Author:
- Francisco Guerra (francisco.guerra@ulpgc.es)
-
-
Constructor Summary
Constructors Constructor Description EventDispatch()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
keyboardExited()
It removes all events of the keyboard buffer.static void
keyPressed(java.lang.String key)
It simulates a keyborad key that is pressed into the current world.static void
keyReleased(java.lang.String key)
It simulates a keyborad key that is released into the current world.static void
keyTyped(java.lang.String key)
It simulates a keyborad key that is typed into the current world.static void
mouseClicked(int x, int y)
It simulates that a mouse botton has been clicked into the current world.static void
mouseClicked(int x, int y, int button, int nClickes)
It simulates that a mouse botton has been clicked into the current world.static void
mouseDragged(int x, int y)
It simulates that a mouse button has been dragged over the current world.static void
mouseDragged(int x, int y, int button)
It simulates that a mouse button has been dragged over the current world.static void
mouseExited()
It removes all events of the mouse buffer.static void
mouseMoved(int x, int y)
It simulates that the mouse is moved around the current world.static void
mousePressed(int x, int y)
It simulates that a mouse botton has been pressed into the current world.static void
mousePressed(int x, int y, int button)
It simulates that a mouse botton has been pressed into the current world.static void
mouseReleased(int x, int y)
It simulates that a mouse botton is released into the current world.static void
mouseReleased(int x, int y, int button)
It simulates that a mouse botton is released into the current world.
-
-
-
Method Detail
-
keyTyped
public static void keyTyped(java.lang.String key) throws GreenfootRunnerExpected
It simulates a keyborad key that is typed into the current world.
For the next actor behaviour:public class ExampleActorKeyboard extends Actor { @Override public void act(){ if (Greenfoot.isKeyDown("g")){ setLocation(10, 20); } if (!Greenfoot.isKeyDown("g")){ setLocation(100, 20); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorKeyboardTest { @Test public void testKeyTyped() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorKeyboard actor = new ExampleActorKeyboard(); world.addObject(actor, 1, 1); // WHEN EventDispatch.keyTyped("g"); WorldCreator.runOnce(world); // THEN assertEquals(100, actor.getX()); } }
- Parameters:
key
- Any key of keyboard. The expected values are "up", "down", "left, "right" (cursor keys); "enter", "space", "tab", "escape", "backspace", "F1" ... "F12" and "a" ... "z".- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
keyPressed
public static void keyPressed(java.lang.String key) throws GreenfootRunnerExpected
It simulates a keyborad key that is pressed into the current world.
For the next actor behaviour:public class ExampleActorKeyboard extends Actor { @Override public void act(){ if (Greenfoot.isKeyDown("g")){ setLocation(10, 20); } if (!Greenfoot.isKeyDown("g")){ setLocation(100, 20); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorKeyboardTest { @Test public void testKeyPressed() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorKeyboard actor = new ExampleActorKeyboard(); world.addObject(actor, 1, 1); // WHEN EventDispatch.keyPressed("g"); WorldCreator.runOnce(world); // THEN assertEquals(10, actor.getX()); } }
- Parameters:
key
- Any key of keyboard. The expected values are "up", "down", "left, "right" (cursor keys); "enter", "space", "tab", "escape", "backspace", "F1" ... "F12" and "a" ... "z".- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
keyReleased
public static void keyReleased(java.lang.String key) throws GreenfootRunnerExpected
It simulates a keyborad key that is released into the current world.
For the next actor behaviour:public class ExampleActorKeyboard extends Actor { @Override public void act(){ if (Greenfoot.isKeyDown("g")){ setLocation(10, 20); } if (!Greenfoot.isKeyDown("g")){ setLocation(100, 20); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorKeyboardTest { @Test public void testKeyReleased() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorKeyboard actor = new ExampleActorKeyboard(); world.addObject(actor, 1, 1); // WHEN EventDispatch.keyPressed("g"); EventDispatch.keyReleased("g"); WorldCreator.runOnce(world); // THEN assertEquals(100, actor.getX()); } }
- Parameters:
key
- Any key of keyboard. The expected values are "up", "down", "left, "right" (cursor keys); "enter", "space", "tab", "escape", "backspace", "F1" ... "F12" and "a" ... "z".- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
keyboardExited
public static void keyboardExited() throws GreenfootRunnerExpected
It removes all events of the keyboard buffer. It not need be called when runOnce operation is used.- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mouseClicked
public static void mouseClicked(int x, int y) throws GreenfootRunnerExpected
It simulates that a mouse botton has been clicked into the current world.
For the next actor behaviour:public class ExampleActorMouse extends Actor { @Override public void act(){ if (Greenfoot.mouseClicked(this)){ move(6); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMouseTest { @Test public void testMouseClicked() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMouse actor = new ExampleActorMouse(); world.addObject(actor, 1, 1); // WHEN EventDispatch.mouseClicked(1,1); WorldCreator.runOnce(world); // THEN assertEquals(7, actor.getX()); } }
- Parameters:
x
- Location index on the x-axis where the even has triggered into the current worky
- Location index on the y-axis where the even has triggered into the current work- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mouseClicked
public static void mouseClicked(int x, int y, int button, int nClickes) throws GreenfootRunnerExpected
It simulates that a mouse botton has been clicked into the current world.
For the next actor behaviour:public class ExampleActorMouse extends Actor { @Override public void act(){ if (Greenfoot.mouseClicked(this)){ move(6); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMouseTest { @Test public void testMouseClicked() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMouse actor = new ExampleActorMouse(); world.addObject(actor, 1, 1); // WHEN EventDispatch.mouseClicked(1,1,1,1); WorldCreator.runOnce(world); // THEN assertEquals(7, actor.getX()); } }
- Parameters:
x
- Location index on the x-axis where the even has triggered into the current worky
- Location index on the y-axis where the even has triggered into the current workbutton
- clicked (0: NONE; 1: left button; 2: middle button; 3: right button)nClickes
- is the click count recorded.- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mousePressed
public static void mousePressed(int x, int y) throws GreenfootRunnerExpected
It simulates that a mouse botton has been pressed into the current world.
For the next actor behaviour:public class ExampleActorMouse extends Actor { @Override public void act(){ if (Greenfoot.mousePressed(this)){ setLocation(10, 20); } if (!Greenfoot.mousePressed(this)){ setLocation(100, 20); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMouseTest { @Test public void testMousePressed() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMouse actor = new ExampleActorMouse(); world.addObject(actor, 1, 1); // WHEN EventDispatch.mousePressed(1,1); WorldCreator.runOnce(world); // THEN assertEquals(10, actor.getX()); } }
- Parameters:
x
- Location index on the x-axis where the even has triggered into the current worky
- Location index on the y-axis where the even has triggered into the current work- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mousePressed
public static void mousePressed(int x, int y, int button) throws GreenfootRunnerExpected
It simulates that a mouse botton has been pressed into the current world.
For the next actor behaviour:public class ExampleActorMouse extends Actor { @Override public void act(){ if (Greenfoot.mousePressed(this)){ setLocation(10, 20); } if (!Greenfoot.mousePressed(this)){ setLocation(100, 20); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMouseTest { @Test public void testMousePressed() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMouse actor = new ExampleActorMouse(); world.addObject(actor, 1, 1); // WHEN EventDispatch.mousePressed(1,1,1); WorldCreator.runOnce(world); // THEN assertEquals(10, actor.getX()); } }
- Parameters:
x
- Location index on the x-axis where the even has triggered into the current worky
- Location index on the y-axis where the even has triggered into the current workbutton
- pressed (0: NONE; 1: left button; 2: middle button; 3: right button)- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mouseDragged
public static void mouseDragged(int x, int y) throws GreenfootRunnerExpected
It simulates that a mouse button has been dragged over the current world.
For the next actor behaviour:public class ExampleActorMouse extends Actor { @Override public void act(){ if (Greenfoot.mouseDragged(this){ move(6); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMouseTest { @Test public void testMouseDragged() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMouse actor = new ExampleActorMouse(); world.addObject(actor, 1, 1); // WHEN EventDispatch.mouseDragged(1,1); WorldCreator.runOnce(world); // THEN assertEquals(7, actor.getX()); } }
- Parameters:
x
- Location index on the x-axis where the even has triggered into the current worky
- Location index on the y-axis where the even has triggered into the current work- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mouseDragged
public static void mouseDragged(int x, int y, int button) throws GreenfootRunnerExpected
It simulates that a mouse button has been dragged over the current world.
For the next actor behaviour:public class ExampleActorMouse extends Actor { @Override public void act(){ if (Greenfoot.mouseDragged(this){ move(6); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMouseTest { @Test public void testMouseDragged() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMouse actor = new ExampleActorMouse(); world.addObject(actor, 1, 1); // WHEN EventDispatch.mouseDragged(1,1,1); WorldCreator.runOnce(world); // THEN assertEquals(7, actor.getX()); } }
- Parameters:
x
- Location index on the x-axis where the even has triggered into the current worky
- Location index on the y-axis where the even has triggered into the current workbutton
- dragged (0: NONE; 1: left button; 2: middle button; 3: right button)- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mouseReleased
public static void mouseReleased(int x, int y) throws GreenfootRunnerExpected
It simulates that a mouse botton is released into the current world.
For the next actor behaviour:public class ExampleActorMouse extends Actor { @Override public void act(){ if (Greenfoot.mouseDragged(this)){ setLocation(10, 20); } if (Greenfoot.mouseDragEnded(this)){ setLocation(100, 20); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMouseTest { @Test public void testMouseDragEnded() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMouse actor = new ExampleActorMouse(); world.addObject(actor, 1, 1); // WHEN EventDispatch.mouseDragged(1,1); EventDispatch.mouseReleased(1,1); WorldCreator.runOnce(world); // THEN assertEquals(100, actor.getX()); } }
- Parameters:
x
- Location index on the x-axis where the even has triggered into the current worky
- Location index on the y-axis where the even has triggered into the current work- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mouseReleased
public static void mouseReleased(int x, int y, int button) throws GreenfootRunnerExpected
It simulates that a mouse botton is released into the current world.
For the next actor behaviour:public class ExampleActorMouse extends Actor { @Override public void act(){ if (Greenfoot.mouseDragged(this)){ setLocation(10, 20); } if (Greenfoot.mouseDragEnded(this)){ setLocation(100, 20); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMouseTest { @Test public void testMouseDragEnded() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMouse actor = new ExampleActorMouse(); world.addObject(actor, 1, 1); // WHEN EventDispatch.mouseDragged(1,1,1); EventDispatch.mouseReleased(1,1,1); WorldCreator.runOnce(world); // THEN assertEquals(100, actor.getX()); } }
- Parameters:
x
- Location index on the x-axis where the even has triggered into the current work.y
- Location index on the y-axis where the even has triggered into the current work.button
- released (0: NONE; 1: left button; 2: middle button; 3: right button)- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mouseMoved
public static void mouseMoved(int x, int y) throws GreenfootRunnerExpected
It simulates that the mouse is moved around the current world.
For the next actor behaviour:public class ExampleActorMouse extends Actor { @Override public void act(){ if (Greenfoot.mouseMoved(this){ move(6); } } }
import ... import greenfoot.TestRunnerState.EventDispatch; import greenfoot.TestRunnerState.WorldCreator; @RunWith(GreenfootRunner.class) public class ExampleActorMouseTest { @Test public void testMouseMoved() throws Exception { // GIVEN World world = WorldCreator.getWorld(400, 300, 1); ExampleActorMouse actor = new ExampleActorMouse(); world.addObject(actor, 1, 1); // WHEN EventDispatch.mouseMoved(1,1); WorldCreator.runOnce(world); // THEN assertEquals(7, actor.getX()); } }
- Parameters:
x
- Location index on the x-axis where the even has triggered into the current worky
- Location index on the y-axis where the even has triggered into the current work- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
mouseExited
public static void mouseExited() throws GreenfootRunnerExpected
It removes all events of the mouse buffer. It not need be called when runOnce operation is used.- Throws:
GreenfootRunnerExpected
- is raised when @RunWith(GreenfootRunner.class) or @ExtendWith(GreenfootRunner.class) is not found.
-
-