Skip to content

Instantly share code, notes, and snippets.

@tito
Last active August 29, 2015 14:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tito/f111b6916aa6a4ed0851 to your computer and use it in GitHub Desktop.
Save tito/f111b6916aa6a4ed0851 to your computer and use it in GitHub Desktop.
Example of mock-MotionEvent for unittest
# subclass for touch event in unit test
class UTMotionEvent(MotionEvent):
def depack(self, args):
self.is_touch = True
self.sx = args['x']
self.sy = args['y']
self.profile = ['pos']
super(UTMotionEvent, self).depack(args)
# then create a touch
touch = UTMotionEvent("unittest", 1, {"x": 0, "y": 0})
# dispatch it manually into kivy
# using post_dispatch_input doesn't pass the touch to any input post-proc
# we don't have the capability to dispatch manually a touch at the beginning
# of the input pipeline, except if you create a touch provider and emit them.
from kivy.base import EventLoop
EventLoop.post_dispatch_input("begin", touch)
# reminder, sx/sy must be size agnostic. So must be between 0-1 range.
touch.move({"x": 0.1, "y": 0})
EventLoop.post_dispatch_input("update", touch)
EventLoop.post_dispatch_input("end", touch)
@harmo
Copy link

harmo commented Mar 1, 2015

Very usefull, thanks !

Just a few questions :

  • What are all the event types we can pass in post_dispatch_input function ?
  • How can I do if I want to mouse_touch_down at given coordinates ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment