From 99556be92f28c31c6089778997555389aa5638ae Mon Sep 17 00:00:00 2001 From: dtookey Date: Mon, 7 Aug 2023 08:52:56 -0400 Subject: [PATCH] improved some docks, added default value for getAlmostPoint wiggle --- .../kotlin/controllers/DesktopController.kt | 47 +++++++++++++++---- src/test/kotlin/controllers/AutomatonTest.kt | 3 +- .../controllers/TemporalControllerTest.kt | 6 +-- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/controllers/DesktopController.kt b/src/main/kotlin/controllers/DesktopController.kt index 3155ed7..758d96c 100644 --- a/src/main/kotlin/controllers/DesktopController.kt +++ b/src/main/kotlin/controllers/DesktopController.kt @@ -14,29 +14,56 @@ import kotlin.random.Random * Classes that implement this can serve as desktop automation controllers. */ interface DesktopController { + /** * Gets the current pointer/mouse location on the desktop. * - * @return The current [Point] location of the mouse pointer. + * This returns a [Point] representing the x, y coordinates of the mouse pointer on the screen. + * + * For example: + * + * ``` + * val mouseLocation = getPointerLocation() + * + * println(mouseLocation) // Might print "Point[x=1920,y=1080]" + * ``` + * + * @return The current [Point] location of the mouse pointer on the screen. */ fun getPointerLocation(): Point { return MouseInfo.getPointerInfo().location } + /** - * Gets a "wiggly" point near the given point. + * Gets a point near the given [point], with some random wiggle. * - * This takes in a target [Point] and [WiggleParams] and returns a new - * point that is randomly offset from the target point based on the - * wiggle parameters. + * This generates a new point that is randomly offset from the given [point] + * by an amount controlled by the given [WiggleParams]. * - * This is useful for adding variance to mouse movements. + * Usage example: * - * @param point The target point to wiggle around - * @param params The wiggle parameters - * @return A new [Point] randomly offset from the target point. + * ``` + * val base = Point(10, 20) + * val params = WiggleParams(xWiggle = 5, yWiggle = 5) + * val randomPoint = getAlmostPoint(base, params) + * + * // randomPoint might be (8, 22) + * ``` + * + * Alternatively, params may be omitted to use the default WiggleParams values + * ``` + * val base = Point(10, 20) + * val randomPoint = getAlmostPoint(base) + * + * // randomPoint might be (8, 22) + * ``` + * + * @param point The base point to start from + * @param params The wiggle parameters that control the random offset amount + * @return A new [Point] near the given point with some random wiggle applied */ - fun getAlmostPoint(point: Point, params: WiggleParams): Point { + fun getAlmostPoint(point: Point, params: WiggleParams = WiggleParams()): Point { val xDel = Random.nextInt(0, params.xWiggle) val yDel = Random.nextInt(0, params.yWiggle) val xDir = if (Random.nextDouble() > 0.5) { diff --git a/src/test/kotlin/controllers/AutomatonTest.kt b/src/test/kotlin/controllers/AutomatonTest.kt index f887e16..07ab0e6 100644 --- a/src/test/kotlin/controllers/AutomatonTest.kt +++ b/src/test/kotlin/controllers/AutomatonTest.kt @@ -19,8 +19,7 @@ class AutomatonTest { @Test fun `Automaton extends DesktopController`() { val automaton = mock(Automaton::class.java) - - verify(automaton).getPointerLocation() + verify(automaton).getAlmostPoint(Point(100, 100)) // Asserts Automaton extends DesktopController } diff --git a/src/test/kotlin/controllers/TemporalControllerTest.kt b/src/test/kotlin/controllers/TemporalControllerTest.kt index 8112cc2..0ece881 100644 --- a/src/test/kotlin/controllers/TemporalControllerTest.kt +++ b/src/test/kotlin/controllers/TemporalControllerTest.kt @@ -66,9 +66,9 @@ internal class TemporalControllerTest { val end = System.currentTimeMillis() val elapsed = end - start - val lowerBound = duration - variance / 2 - val upperBound = duration + variance / 2 - + val lowerBound = (duration - variance / 2) - 1 + val upperBound = (duration + variance / 2) + 20 + println("elapsed: $elapsed, [$lowerBound, $upperBound]") assertTrue(elapsed >= lowerBound) assertTrue(elapsed <= upperBound) }