diff --git a/pom.xml b/pom.xml index 84c3922..fb626d5 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,18 @@ 4.12 test + + org.powermock + powermock-module-junit4 + 1.7.4 + test + + + org.powermock + powermock-api-mockito + 1.7.4 + test + diff --git a/src/main/java/com/github/joonasvali/naturalmouse/support/ScreenAdjustedNature.java b/src/main/java/com/github/joonasvali/naturalmouse/support/ScreenAdjustedNature.java index 115d856..cdddc72 100644 --- a/src/main/java/com/github/joonasvali/naturalmouse/support/ScreenAdjustedNature.java +++ b/src/main/java/com/github/joonasvali/naturalmouse/support/ScreenAdjustedNature.java @@ -23,9 +23,6 @@ public class ScreenAdjustedNature extends DefaultMouseMotionNature { public ScreenAdjustedNature(Dimension screenSize, Point mouseOffset) { this.screenSize = screenSize; this.offset = mouseOffset; - // Initial wrapping: - setMouseInfo(super.getMouseInfo()); - setSystemCalls(super.getSystemCalls()); } @Override diff --git a/src/test/java/com/github/joonavali/naturalmouse/MouseMotionTestBase.java b/src/test/java/com/github/joonavali/naturalmouse/MouseMotionTestBase.java index 6090cce..55e4bec 100644 --- a/src/test/java/com/github/joonavali/naturalmouse/MouseMotionTestBase.java +++ b/src/test/java/com/github/joonavali/naturalmouse/MouseMotionTestBase.java @@ -6,6 +6,8 @@ import com.github.joonasvali.naturalmouse.api.MouseMotionFactory; import com.github.joonasvali.naturalmouse.api.NoiseProvider; import com.github.joonasvali.naturalmouse.api.SpeedManager; import com.github.joonasvali.naturalmouse.api.SystemCalls; +import com.github.joonasvali.naturalmouse.support.DefaultMouseInfoAccessor; +import com.github.joonasvali.naturalmouse.support.DefaultSystemCalls; import com.github.joonasvali.naturalmouse.support.DoublePoint; import com.github.joonasvali.naturalmouse.support.Flow; import com.github.joonasvali.naturalmouse.util.Pair; @@ -53,10 +55,11 @@ public class MouseMotionTestBase { Assert.assertEquals(y, pos.getY(), SMALL_DELTA); } - protected static class MockSystemCalls implements SystemCalls { + protected static class MockSystemCalls extends DefaultSystemCalls { private final MockMouse mockMouse; public MockSystemCalls(MockMouse mockMouse) { + super(null); this.mockMouse = mockMouse; } @@ -109,7 +112,7 @@ public class MouseMotionTestBase { } } - protected static class MockMouse implements MouseInfoAccessor { + protected static class MockMouse extends DefaultMouseInfoAccessor { private final ArrayList mouseMovements = new ArrayList<>(); MockMouse() { diff --git a/src/test/java/com/github/joonavali/naturalmouse/ScreenAdjustedNatureDefaultsTest.java b/src/test/java/com/github/joonavali/naturalmouse/ScreenAdjustedNatureDefaultsTest.java new file mode 100644 index 0000000..fe9454a --- /dev/null +++ b/src/test/java/com/github/joonavali/naturalmouse/ScreenAdjustedNatureDefaultsTest.java @@ -0,0 +1,93 @@ +package com.github.joonavali.naturalmouse; + +import com.github.joonasvali.naturalmouse.api.MouseMotionFactory; +import com.github.joonasvali.naturalmouse.api.SystemCalls; +import com.github.joonasvali.naturalmouse.support.DefaultMouseInfoAccessor; +import com.github.joonasvali.naturalmouse.support.DefaultMouseMotionNature; +import com.github.joonasvali.naturalmouse.support.DefaultOvershootManager; +import com.github.joonasvali.naturalmouse.support.DefaultSystemCalls; +import com.github.joonasvali.naturalmouse.support.ScreenAdjustedNature; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.awt.*; +import java.util.ArrayList; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.whenNew; + +@RunWith(PowerMockRunner.class) +@PrepareForTest(DefaultMouseMotionNature.class) +/** + * This should run same tests as ScreenAdjustedNatureTest with difference in setup. + * These tests verify that the offsets and dimensions are properly set when user does not explicitly + * set MouseInfoAccessor and SystemCalls, but rely on the default version in DefaultMouseMotionNature + */ +public class ScreenAdjustedNatureDefaultsTest { + private MouseMotionFactory factory; + private MouseMotionTestBase.MockMouse mouse; + + @Before + public void setup() throws Exception { + mouse = new MouseMotionTestBase.MockMouse(60, 60); + DefaultSystemCalls mockSystemCalls = new MouseMotionTestBase.MockSystemCalls(mouse); + whenNew(DefaultSystemCalls.class).withAnyArguments().thenReturn(mockSystemCalls); + whenNew(DefaultMouseInfoAccessor.class).withAnyArguments().thenReturn(mouse); + + // Mockito inserts the systemCalls and Mouse + factory = new MouseMotionFactory(); + factory.setNature(new ScreenAdjustedNature(new Dimension(100, 100), new Point(50, 50))); + ((DefaultOvershootManager)factory.getOvershootManager()).setOvershoots(0); + factory.setNoiseProvider(new MouseMotionTestBase.MockNoiseProvider()); + factory.setDeviationProvider(new MouseMotionTestBase.MockDeviationProvider()); + factory.setSpeedManager(new MouseMotionTestBase.MockSpeedManager()); + factory.setRandom(new MouseMotionTestBase.MockRandom()); + + } + + @Test + public void testOffsetAppliesToMouseMovement() throws InterruptedException { + factory.move(50, 50); + + ArrayList moves = mouse.getMouseMovements(); + Assert.assertEquals(new Point(60, 60), moves.get(0)); + Assert.assertEquals(new Point(100, 100), moves.get(moves.size() - 1)); + Point lastPos = new Point(0, 0); + for (Point p : moves) { + Assert.assertTrue(lastPos.x + " vs " + p.x, lastPos.x < p.x); + Assert.assertTrue(lastPos.y + " vs " + p.y,lastPos.y < p.y); + lastPos = p; + } + } + + @Test + public void testDimensionsLimitScreenOnLargeSide() throws InterruptedException { + // Arbitrary large movement attempt: (60, 60) -> (1060, 1060) + factory.move(1000, 1000); + + ArrayList moves = mouse.getMouseMovements(); + Assert.assertEquals(new Point(60, 60), moves.get(0)); + // Expect the screen size to be only 100x100px, so it gets capped on 150, 150. + // But NaturalMouseMotion allows to move to screen length - 1, so it's [149, 149] + Assert.assertEquals(new Point(149, 149), moves.get(moves.size() - 1)); + } + + @Test + public void testOffsetLimitScreenOnSmallSide() throws InterruptedException { + // Try to move out of the specified screen + factory.move(-1, -1); + + ArrayList moves = mouse.getMouseMovements(); + Assert.assertEquals(new Point(60, 60), moves.get(0)); + // Expect the offset to limit the mouse movement to 50, 50 + Assert.assertEquals(new Point(50, 50), moves.get(moves.size() - 1)); + } + +} diff --git a/src/test/java/com/github/joonavali/naturalmouse/ScreenAdjustedNatureTest.java b/src/test/java/com/github/joonavali/naturalmouse/ScreenAdjustedNatureTest.java index 7e5c120..1153498 100644 --- a/src/test/java/com/github/joonavali/naturalmouse/ScreenAdjustedNatureTest.java +++ b/src/test/java/com/github/joonavali/naturalmouse/ScreenAdjustedNatureTest.java @@ -37,7 +37,6 @@ public class ScreenAdjustedNatureTest { Assert.assertEquals(new Point(100, 100), moves.get(moves.size() - 1)); Point lastPos = new Point(0, 0); for (Point p : moves) { - System.out.println(p); Assert.assertTrue(lastPos.x + " vs " + p.x, lastPos.x < p.x); Assert.assertTrue(lastPos.y + " vs " + p.y,lastPos.y < p.y); lastPos = p;