improved some docks, added default value for getAlmostPoint wiggle

This commit is contained in:
dtookey 2023-08-07 08:52:56 -04:00
parent 72346ee6e6
commit 99556be92f
3 changed files with 41 additions and 15 deletions

View File

@ -14,29 +14,56 @@ import kotlin.random.Random
* Classes that implement this can serve as desktop automation controllers. * Classes that implement this can serve as desktop automation controllers.
*/ */
interface DesktopController { interface DesktopController {
/** /**
* Gets the current pointer/mouse location on the desktop. * 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 { fun getPointerLocation(): Point {
return MouseInfo.getPointerInfo().location 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 * This generates a new point that is randomly offset from the given [point]
* point that is randomly offset from the target point based on the * by an amount controlled by the given [WiggleParams].
* wiggle parameters.
* *
* This is useful for adding variance to mouse movements. * Usage example:
* *
* @param point The target point to wiggle around * ```
* @param params The wiggle parameters * val base = Point(10, 20)
* @return A new [Point] randomly offset from the target point. * 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 xDel = Random.nextInt(0, params.xWiggle)
val yDel = Random.nextInt(0, params.yWiggle) val yDel = Random.nextInt(0, params.yWiggle)
val xDir = if (Random.nextDouble() > 0.5) { val xDir = if (Random.nextDouble() > 0.5) {

View File

@ -19,8 +19,7 @@ class AutomatonTest {
@Test @Test
fun `Automaton extends DesktopController`() { fun `Automaton extends DesktopController`() {
val automaton = mock(Automaton::class.java) val automaton = mock(Automaton::class.java)
verify(automaton).getAlmostPoint(Point(100, 100))
verify(automaton).getPointerLocation()
// Asserts Automaton extends DesktopController // Asserts Automaton extends DesktopController
} }

View File

@ -66,9 +66,9 @@ internal class TemporalControllerTest {
val end = System.currentTimeMillis() val end = System.currentTimeMillis()
val elapsed = end - start val elapsed = end - start
val lowerBound = duration - variance / 2 val lowerBound = (duration - variance / 2) - 1
val upperBound = duration + variance / 2 val upperBound = (duration + variance / 2) + 20
println("elapsed: $elapsed, [$lowerBound, $upperBound]")
assertTrue(elapsed >= lowerBound) assertTrue(elapsed >= lowerBound)
assertTrue(elapsed <= upperBound) assertTrue(elapsed <= upperBound)
} }