Question
My application has a map-like canvas where the user can move by dragging the map around. What I'm trying to accomplish is move the map to the right of 100px and then to the left of 100px and check if the position in the center is the same.
The code is as follows:
device.drag((640, 360), (640 - 100, 360))
device.drag((640, 360), (640 + 100, 360))
# check
This doesn't reliably bring the map to the same place every time. Sometimes the device hangs or is slow and it moves few extra pixels; some other times in the last move step it goes faster, giving it momentum.
Is there any way to precisely drag the screen of a certain amount of pixels? If the device hangs or is slow it doesn't matter, as long as the movement in the end is correct.
My attempts
I've tried to tweak the third (duration
) and fourth (steps
) argument to no avail.
I've also tried to implement my custom drag code with:
# Touch down screen
device.touch(100, 500, MonkeyDevice.DOWN)
# Move from 100, 500 to 200, 500
for i in range(1, 11):
device.touch(100 + 10 * i, 500, MonkeyDevice.MOVE)
time.sleep(0.1)
# Extra sleep to avoid momentum
time.sleep(0.2)
# Remove finger from screen
device.touch(200, 500, MonkeyDevice.UP)
and then to the other side:
# Touch down screen
device.touch(200, 500, MonkeyDevice.DOWN)
# Move from 200, 500 to 100, 500
for i in range(1, 11):
device.touch(200 - 10 * i, 500, MonkeyDevice.MOVE)
time.sleep(0.1)
# Extra sleep to avoid momentum
time.sleep(0.2)
# Remove finger from screen
device.touch(100, 500, MonkeyDevice.UP)
In a similar fashion I've also tried to test my game pad keys with:
for _ in range(0, 10):
device.press('KEYCODE_DPAD_RIGHT', MonkeyDevice.DOWN_AND_UP)
time.sleep(0.1)
for _ in range(0, 10):
device.press('KEYCODE_DPAD_LEFT', MonkeyDevice.DOWN_AND_UP)
time.sleep(0.1)
and even then sometimes monkeyrunner
will either skip events or not consider the up event and therefore causing a long press (which is equivalent to "keep moving on the map").