Skip to content

Commit

Permalink
#98 Fix mouse moving when primary screen is not left.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Rösch authored and Christian Rösch committed Jun 8, 2015
1 parent 75afb8d commit 5af72f3
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 11 deletions.
Expand Up @@ -51,7 +51,7 @@ class RobotEventGenerator implements InputEventGenerator {

RobotEventGenerator(@Nonnull RobotFactory robotFactory, @Nonnull Settings settings) {
try {
robot = robotFactory.newRobotInPrimaryScreen();
robot = robotFactory.newRobotInLeftScreen();
if (isWindows() || isOSX()) {
pause(500);
}
Expand Down
Expand Up @@ -65,7 +65,7 @@ public ScreenshotTaker() {
ScreenshotTaker(@Nonnull ImageFileWriter writer, @Nonnull RobotFactory robotFactory) {
this.writer = writer;
try {
robot = robotFactory.newRobotInPrimaryScreen();
robot = robotFactory.newRobotInLeftScreen();
} catch (AWTException e) {
throw new ImageException("Unable to create AWT Robot", e);
}
Expand Down
Expand Up @@ -56,7 +56,7 @@ class WindowStatus {
this.windows = windows;
Robot r = null;
try {
r = robotFactory.newRobotInPrimaryScreen();
r = robotFactory.newRobotInLeftScreen();
} catch (AWTException ignored) {
logger.log(WARNING, "Error ocurred when creating a new Robot", ignored);
}
Expand Down
Expand Up @@ -13,6 +13,8 @@
package org.assertj.swing.util;

import java.awt.AWTException;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;

import javax.annotation.Nonnull;
Expand All @@ -34,4 +36,25 @@ public class RobotFactory {
public @Nonnull Robot newRobotInPrimaryScreen() throws AWTException {
return new Robot();
}

/**
* Creates a new AWT {@code Robot} object in the coordinate system of the left screen (in terms of coordinates).
*
* @return the created {@code Robot}.
* @throws AWTException if the platform configuration does not allow low-level input control. This exception is always
* thrown when {@code GraphicsEnvironment.isHeadless()} returns {@code true}.
* @throws SecurityException if {@code createRobot} permission is not granted.
*/
public @Nonnull
Robot newRobotInLeftScreen() throws AWTException {
int lowestX = Integer.MAX_VALUE;
GraphicsDevice lowestScreen = null;
for (GraphicsDevice screen : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
if (screen.getDefaultConfiguration().getBounds().x < lowestX) {
lowestX = screen.getDefaultConfiguration().getBounds().x;
lowestScreen = screen;
}
}
return new Robot(lowestScreen);
}
}
Expand Up @@ -35,7 +35,7 @@ public abstract class RobotEventGenerator_TestCase {
public final void setUp() throws Exception {
RobotFactory robotFactory = newRobotFactoryMock();
robot = mock(Robot.class);
when(robotFactory.newRobotInPrimaryScreen()).thenReturn(robot);
when(robotFactory.newRobotInLeftScreen()).thenReturn(robot);
eventGenerator = new RobotEventGenerator(robotFactory, new Settings());
}

Expand Down
Expand Up @@ -47,15 +47,15 @@ public void setUp() {
@Test
public void should_Use_RobotFactory_To_Create_AWTRobot() throws AWTException {
Robot robot = mock(Robot.class);
when(robotFactory.newRobotInPrimaryScreen()).thenReturn(robot);
when(robotFactory.newRobotInLeftScreen()).thenReturn(robot);
RobotEventGenerator eventGenerator = new RobotEventGenerator(robotFactory, new Settings());
assertThat(eventGenerator.robot()).isSameAs(robot);
}

@Test
public void should_Rethrow_Any_Error_From_RobotFactory() throws AWTException {
AWTException toThrow = new AWTException("Thrown on purpose");
when(robotFactory.newRobotInPrimaryScreen()).thenThrow(toThrow);
when(robotFactory.newRobotInLeftScreen()).thenThrow(toThrow);
thrown.expect(UnexpectedException.class);
try {
new RobotEventGenerator(robotFactory, new Settings());
Expand Down
Expand Up @@ -48,7 +48,7 @@ public void setUp() {

@Test
public void should_Throw_Wrapped_Exception_Thrown_When_Creating_Robot() throws AWTException {
when(robotFactory.newRobotInPrimaryScreen()).thenThrow(toThrow);
when(robotFactory.newRobotInLeftScreen()).thenThrow(toThrow);
thrown.expectWrappingException(ImageException.class, toThrow);
new ScreenshotTaker(writer, robotFactory);
}
Expand Down
Expand Up @@ -96,7 +96,7 @@ public boolean test() {
public void should_Not_Check_If_Frame_Is_Ready_If_Robot_Is_Null() throws AWTException {
final RobotFactory factory = mock(RobotFactory.class);
Point before = MouseInfo.getPointerInfo().getLocation();
when(factory.newRobotInPrimaryScreen()).thenReturn(null);
when(factory.newRobotInLeftScreen()).thenReturn(null);
status = new WindowStatus(windows, factory);
status.checkIfReady(window);
// mouse pointer should not have moved
Expand Down
Expand Up @@ -52,7 +52,7 @@ protected final void onTearDown() {
@Test
public void should_Not_Check_If_Frame_Is_Ready_If_Robot_Is_Null() throws AWTException {
Point before = MouseInfo.getPointerInfo().getLocation();
when(factory.newRobotInPrimaryScreen()).thenReturn(null);
when(factory.newRobotInLeftScreen()).thenReturn(null);
WindowStatus status = new WindowStatus(windows, factory);
status.checkIfReady(window);
// mouse pointer should not have moved
Expand Down
Expand Up @@ -40,7 +40,7 @@ public void setUp() {

@Test
public void should_Have_Null_Robot_If_Robot_Cannot_Be_Created() throws AWTException {
when(factory.newRobotInPrimaryScreen()).thenThrow(new AWTException("Thrown on purpose"));
when(factory.newRobotInLeftScreen()).thenThrow(new AWTException("Thrown on purpose"));
WindowStatus status = new WindowStatus(windows, factory);
assertThat(status.robot).isNull();
}
Expand Down
Expand Up @@ -37,7 +37,7 @@ public void setUp() {
public void shouldCreateNewRobot() throws AWTException {
Robot last = null;
for (int i = 0; i < 6; i++) {
Robot current = robotFactory.newRobotInPrimaryScreen();
Robot current = robotFactory.newRobotInLeftScreen();
assertThat(current).isNotNull().isNotSameAs(last);
last = current;
}
Expand Down

0 comments on commit 5af72f3

Please sign in to comment.