Skip to content

Commit e2d7994

Browse files
authored
[BugFix] Fix step distance scaling issue in walk_pointer_to_proportional() (#47)
Signed-off-by: Douglas Chiang <[email protected]>
1 parent 3007be5 commit e2d7994

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

yarf/rf_libraries/libraries/hid_base.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import asyncio
66
from abc import ABC, abstractmethod
7+
from math import sqrt
78
from typing import NamedTuple, Sequence
89

910
from robot.api.deco import keyword
@@ -202,15 +203,22 @@ async def walk_pointer_to_absolute(
202203
assert 0 <= y <= display_size.height, "Y coordinate outside of screen"
203204

204205
proportional = (x / display_size.width, y / display_size.height)
206+
proportional_step_distance = step_distance / sqrt(
207+
display_size.width * display_size.height
208+
)
205209
await self.walk_pointer_to_proportional(
206210
*proportional,
207-
step_distance,
211+
proportional_step_distance,
208212
delay,
209213
)
210214

211215
@keyword
212216
async def walk_pointer_to_proportional(
213-
self, x: float, y: float, step_distance: float, delay: float
217+
self,
218+
x: float,
219+
y: float,
220+
step_distance: float,
221+
delay: float,
214222
) -> None:
215223
"""
216224
Walk the virtual pointer to a position proportional to the size of the
@@ -220,14 +228,15 @@ async def walk_pointer_to_proportional(
220228
Args:
221229
x: horizontal coordinate, 0 <= x <= 1
222230
y: vertical coordinate, 0 <= y <= 1
223-
step_distance: maximum distance to move per step
231+
step_distance: maximum distance to move per step horizontally, 0 < step_distance <= 1
224232
delay: delay between steps in seconds
225233
226234
Raises:
227235
AssertionError: if coordinates are out of range
228236
"""
229237
assert 0 <= x <= 1, "x not in range 0..1"
230238
assert 0 <= y <= 1, "y not in range 0..1"
239+
assert 0 < step_distance <= 1, "step_distance_x not in range 0..1"
231240

232241
while self._pointer_position != (x, y):
233242
dist_x = x - self._pointer_position.x

yarf/rf_libraries/libraries/tests/test_hid_base.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from unittest.mock import AsyncMock, call, patch
1+
from unittest.mock import AsyncMock, MagicMock, call, patch
22

33
import pytest
44

@@ -112,24 +112,48 @@ async def test_walk_pointer_to_proportional(self, stub_hid, mock_sleep):
112112

113113
mock_sleep.assert_has_calls(7 * [call(0.2)])
114114

115+
@pytest.mark.asyncio
116+
@pytest.mark.parametrize(
117+
"x,y,step_distance",
118+
[
119+
(1.1, 0, 0),
120+
(0, 1.1, 0),
121+
(0, 0, 1.1),
122+
],
123+
)
124+
async def test_walk_pointer_to_proportional_raises(
125+
self,
126+
stub_hid: MagicMock,
127+
x: float,
128+
y: float,
129+
step_distance: float,
130+
):
131+
"""
132+
Test the function raises an exception if the target position or step
133+
distance are out of range.
134+
"""
135+
with pytest.raises(AssertionError):
136+
await stub_hid.walk_pointer_to_proportional(
137+
x, y, step_distance, 0.2
138+
)
139+
115140
@pytest.mark.asyncio
116141
async def test_walk_pointer_to_absolute(self, stub_hid, mock_sleep):
117142
"""
118143
Test the function moves the pointer by the requested step to the target
119144
position given in absolute coordinates.
120145
"""
121146
await stub_hid.move_pointer_to_proportional(0.15, 0.35)
122-
await stub_hid.walk_pointer_to_absolute(100, 200, 0.05, 0.2)
147+
await stub_hid.walk_pointer_to_absolute(100, 200, 100, 0.2)
123148

124149
stub_hid._move_pointer.assert_has_calls(
125150
(
126-
call(pytest.approx(0.1), pytest.approx(0.3)),
127151
call(pytest.approx(0.1), pytest.approx(0.25)),
128152
call(pytest.approx(0.1), pytest.approx(0.2)),
129153
)
130154
)
131155

132-
mock_sleep.assert_has_calls(3 * [call(0.2)])
156+
mock_sleep.assert_has_calls(2 * [call(0.2)])
133157

134158
@pytest.mark.asyncio
135159
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)