Skip to content

Commit

Permalink
Merge pull request #5368 from kivy/unit_features
Browse files Browse the repository at this point in the history
Unittesting features
  • Loading branch information
KeyWeeUsr committed Sep 6, 2017
2 parents 78a4206 + baf30ac commit 3052803
Showing 1 changed file with 85 additions and 4 deletions.
89 changes: 85 additions & 4 deletions kivy/tests/common.py
Expand Up @@ -15,12 +15,15 @@
import logging
import os
from kivy.graphics.cgl import cgl_get_backend_name
from kivy.input.motionevent import MotionEvent
log = logging.getLogger('unittest')

_base = object
if 'mock' != cgl_get_backend_name():
_base = unittest.TestCase

make_screenshots = os.environ.get('KIVY_UNITTEST_SCREENSHOTS')


class GraphicUnitTest(_base):

Expand Down Expand Up @@ -100,10 +103,8 @@ def on_window_flip(self, window):
if self.framecount > 0:
return

# don't create screenshots if a specific var is in env
ignore = ['TRAVIS_OS_NAME', 'APPVEYOR_BUILD_FOLDER']
from os import environ
if any(i in environ for i in ignore):
# don't create screenshots if not requested manually
if not make_screenshots:
EventLoop.stop()
return

Expand Down Expand Up @@ -283,3 +284,83 @@ def do_yes():
root.mainloop()

return self.retval

def advance_frames(self, count):
'''Render the new frames and:
* tick the Clock
* dispatch input from all registered providers
* flush all the canvas operations
* redraw Window canvas if necessary
'''
from kivy.base import EventLoop
for i in range(count):
EventLoop.idle()


class UnitTestTouch(MotionEvent):
'''Custom MotionEvent representing a single touch. Similar to `on_touch_*`
methods from the Widget class, this one introduces:
* touch_down
* touch_move
* touch_up
Create a new touch with::
touch = UnitTestTouch(x, y)
then you press it on the default position with::
touch.touch_down()
or move it or even release with these simple calls::
touch.touch_move(new_x, new_y)
touch.touch_up()
'''

def __init__(self, x, y):
'''Create a MotionEvent instance with X and Y of the first
position a touch is at.
'''

from kivy.base import EventLoop
self.eventloop = EventLoop
win = EventLoop.window

super(UnitTestTouch, self).__init__(
# device, (tuio) id, args
"UnitTestTouch", 99, {
"x": x / float(win.width),
"y": y / float(win.height),
}
)

def touch_down(self, *args):
self.eventloop.post_dispatch_input("begin", self)

def touch_move(self, x, y):
win = self.eventloop.window
self.move({
"x": x / float(win.width),
"y": y / float(win.height)
})
self.eventloop.post_dispatch_input("update", self)

def touch_up(self, *args):
self.eventloop.post_dispatch_input("end", self)

def depack(self, args):
# set MotionEvent to touch
self.is_touch = True

# set sx/sy properties to ratio (e.g. X / win.width)
self.sx = args['x']
self.sy = args['y']

# set profile to accept x, y and pos properties
self.profile = ['pos']

# run depack after we set the values
super(UnitTestTouch, self).depack(args)

0 comments on commit 3052803

Please sign in to comment.