fixes to be able to import window + use runTouchApp

This commit is contained in:
Mathieu Virbel 2010-11-03 17:37:48 -04:00
parent 1f2fb6eb41
commit 4107ea1f0b
20 changed files with 59 additions and 688 deletions

View File

@ -3,7 +3,7 @@ Base: Main event loop, provider creation, window management...
'''
__all__ = (
'EventLoop', 'Window',
'EventLoop',
'runTouchApp', 'stopTouchApp',
'getCurrentTouches',
)
@ -19,7 +19,6 @@ from kivy.input import TouchFactory, kivy_postproc_modules
# private vars
EventLoop = None
Window = None
def getCurrentTouches():
'''Return the list of all current touches
@ -37,6 +36,12 @@ class EventLoopBase(object):
self.status = 'idle'
self.input_providers = []
self.event_listeners = []
self.window = None
def set_window(self, window):
'''Set the window used for event loop
'''
self.window = window
def add_input_provider(self, provider):
'''Add a new input provider to listen for touch event
@ -110,11 +115,11 @@ class EventLoopBase(object):
if not touch.grab_exclusive_class:
for listener in self.event_listeners:
if event == 'down':
listener.dispatch_event('on_touch_down', touch)
listener.dispatch('on_touch_down', touch)
elif event == 'move':
listener.dispatch_event('on_touch_move', touch)
listener.dispatch('on_touch_move', touch)
elif event == 'up':
listener.dispatch_event('on_touch_up', touch)
listener.dispatch('on_touch_up', touch)
# dispatch grabbed touch
touch.grab_state = True
@ -153,12 +158,12 @@ class EventLoopBase(object):
if event == 'down':
# don't dispatch again touch in on_touch_down
# a down event are nearly uniq here.
# wid.dispatch_event('on_touch_down', touch)
# wid.dispatch('on_touch_down', touch)
pass
elif event == 'move':
wid.dispatch_event('on_touch_move', touch)
wid.dispatch('on_touch_move', touch)
elif event == 'up':
wid.dispatch_event('on_touch_up', touch)
wid.dispatch('on_touch_up', touch)
touch.grab_current = None
@ -202,11 +207,10 @@ class EventLoopBase(object):
# read and dispatch input from providers
self.dispatch_input()
if kivy_window:
kivy_window.dispatch_events()
kivy_window.dispatch_event('on_update')
kivy_window.dispatch_event('on_draw')
kivy_window.dispatch_event('on_flip')
window = self.window
if window:
window.dispatch('on_draw')
window.dispatch('on_flip')
# don't loop if we don't have listeners !
if len(self.event_listeners) == 0:
@ -224,8 +228,8 @@ class EventLoopBase(object):
def exit(self):
'''Close the main loop, and close the window'''
self.close()
if kivy_window:
kivy_window.close()
if self.window:
self.window.close()
#: EventLoop instance
EventLoop = EventLoopBase()
@ -275,11 +279,6 @@ def runTouchApp(widget=None, slave=False):
# Ok, we got one widget, and we are not in slave mode
# so, user don't create the window, let's create it for him !
### Not needed, since we always create window ?!
#if not slave and widget:
# global kivy_window
# from ui.window import MTWindow
# kivy_window = MTWindow()
# Instance all configured input
for key, value in Config.items('input'):
@ -306,8 +305,8 @@ def runTouchApp(widget=None, slave=False):
EventLoop.add_postproc_module(mod)
# add main widget
if widget and getWindow():
getWindow().add_widget(widget)
if widget and EventLoop.window:
EventLoop.window.add_widget(widget)
# start event loop
Logger.info('Base: Start application main loop')
@ -329,10 +328,10 @@ def runTouchApp(widget=None, slave=False):
# ourself (previous behavior.)
#
try:
if kivy_window is None:
if EventLoop.window is None:
_run_mainloop()
else:
kivy_window.mainloop()
EventLoop.window.mainloop()
finally:
stopTouchApp()

View File

@ -103,9 +103,9 @@ class Sound(EventDispatcher):
return
self._status = x
if x == 'stop':
self.dispatch_event('on_stop')
self.dispatch('on_stop')
elif x == 'play':
self.dispatch_event('on_play')
self.dispatch('on_play')
else:
assert('unknown status %s' % x)
status = property(_get_status,

View File

@ -17,7 +17,7 @@ if gl_check.lower() in ['0', 'false', 'no']:
# To be able to use our GL provider, we must have a window
# Automaticly import window auto to ensure the default window creation
import kivy.core.window.default
import kivy.core.window
# Display the current OpenGL version
version = glGetString(GL_VERSION)

View File

@ -48,7 +48,7 @@ class VideoGStreamer(VideoBase):
# reset to start for next play
self._pipeline.seek_simple(
gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, 0)
self.dispatch_event('on_eos')
self.dispatch('on_eos')
super(VideoGStreamer, self)._do_eos()
def stop(self):

View File

@ -79,7 +79,6 @@ class WindowBase(EventDispatcher):
self.register_event_type('on_flip')
self.register_event_type('on_rotate')
self.register_event_type('on_draw')
self.register_event_type('on_update')
self.register_event_type('on_resize')
self.register_event_type('on_close')
self.register_event_type('on_touch_down')
@ -92,8 +91,8 @@ class WindowBase(EventDispatcher):
self.register_event_type('on_key_down')
self.register_event_type('on_key_up')
#self.children = SafeList()
#self.parent = self
self.children = []
self.parent = self
#self.visible = True
# add view
@ -160,6 +159,7 @@ class WindowBase(EventDispatcher):
# attach modules + listener event
Modules.register_window(self)
EventLoop.set_window(self)
EventLoop.add_event_listener(self)
# mark as initialized
@ -185,10 +185,6 @@ class WindowBase(EventDispatcher):
'''Flip between buffers'''
pass
def dispatch_events(self):
'''Dispatch all events from windows'''
pass
def _get_modifiers(self):
return self._modifiers
modifiers = property(_get_modifiers)
@ -202,7 +198,7 @@ class WindowBase(EventDispatcher):
def _set_size(self, size):
if super(WindowBase, self)._set_size(size):
Logger.debug('Window: Resize window to %s' % str(self.size))
self.dispatch_event('on_resize', *size)
self.dispatch('on_resize', *size)
return True
return False
size = property(_get_size, _set_size,
@ -264,13 +260,6 @@ class WindowBase(EventDispatcher):
def get_parent_layout(self):
return None
def on_update(self):
'''Event called when window are update the widget tree.
(Usually before on_draw call.)
'''
for w in self.children[:]:
w.dispatch_event('on_update')
def on_draw(self):
'''Event called when window we are drawing window.
This function are cleaning the buffer with bg-color css,
@ -281,14 +270,14 @@ class WindowBase(EventDispatcher):
# then, draw childrens
for w in self.children[:]:
w.dispatch_event('on_draw')
w.dispatch('on_draw')
def on_touch_down(self, touch):
'''Event called when a touch is down'''
w, h = self.system_size
touch.scale_for_screen(w, h, rotation=self._rotation)
for w in reversed(self.children[:]):
if w.dispatch_event('on_touch_down', touch):
if w.dispatch('on_touch_down', touch):
return True
def on_touch_move(self, touch):
@ -296,7 +285,7 @@ class WindowBase(EventDispatcher):
w, h = self.system_size
touch.scale_for_screen(w, h, rotation=self._rotation)
for w in reversed(self.children[:]):
if w.dispatch_event('on_touch_move', touch):
if w.dispatch('on_touch_move', touch):
return True
def on_touch_up(self, touch):
@ -304,7 +293,7 @@ class WindowBase(EventDispatcher):
w, h = self.system_size
touch.scale_for_screen(w, h, rotation=self._rotation)
for w in reversed(self.children[:]):
if w.dispatch_event('on_touch_up', touch):
if w.dispatch('on_touch_up', touch):
return True
def on_resize(self, width, height):
@ -312,6 +301,9 @@ class WindowBase(EventDispatcher):
self.update_viewport()
def update_viewport(self):
# XXX FIXME
from kivy.core.gl import *
width, height = self.system_size
w2 = width / 2.
h2 = height / 2.
@ -357,8 +349,8 @@ class WindowBase(EventDispatcher):
if x not in (0, 90, 180, 270):
raise ValueError('can rotate only 0,90,180,270 degrees')
self._rotation = x
self.dispatch_event('on_resize', *self.size)
self.dispatch_event('on_rotate', x)
self.dispatch('on_resize', *self.size)
self.dispatch('on_rotate', x)
rotation = property(_get_rotation, _set_rotation,
'Get/set the window content rotation. Can be one of '
'0, 90, 180, 270 degrees.')
@ -411,5 +403,5 @@ class WindowBase(EventDispatcher):
# Load the appropriate provider
Window = core_select_lib('window', (
('pygame', 'window_pygame', 'WindowPygame'),
))
))()

View File

@ -1,10 +0,0 @@
'''
Default: create the default window if not exist
'''
from kivy.core.window import Window
from kivy import kivy_configure
Window()
kivy_configure()

View File

@ -76,8 +76,9 @@ class WindowPygame(WindowBase):
pygame.key.set_repeat(repeat_delay, int(1000. / repeat_rate))
# set window icon before calling set_mode
icon = pygame.image.load(Config.get('graphics', 'window_icon'))
pygame.display.set_icon(icon)
# XXX FIXME
#icon = pygame.image.load(Config.get('graphics', 'window_icon'))
#pygame.display.set_icon(icon)
# init ourself size + setmode
# before calling on_resize
@ -171,7 +172,7 @@ class WindowPygame(WindowBase):
if event.buttons == (0, 0, 0):
continue
x, y = event.pos
self.dispatch_event('on_mouse_move', x, y, self.modifiers)
self.dispatch('on_mouse_move', x, y, self.modifiers)
# mouse action
elif event.type in (pygame.MOUSEBUTTONDOWN,
@ -186,22 +187,22 @@ class WindowPygame(WindowBase):
eventname = 'on_mouse_down'
if event.type == pygame.MOUSEBUTTONUP:
eventname = 'on_mouse_up'
self.dispatch_event(eventname, x, y, btn, self.modifiers)
self.dispatch(eventname, x, y, btn, self.modifiers)
# keyboard action
elif event.type in (pygame.KEYDOWN, pygame.KEYUP):
self._pygame_update_modifiers(event.mod)
# atm, don't handle keyup
if event.type == pygame.KEYUP:
self.dispatch_event('on_key_up', event.key,
self.dispatch('on_key_up', event.key,
event.scancode)
continue
# don't dispatch more key if down event is accepted
if self.dispatch_event('on_key_down', event.key,
if self.dispatch('on_key_down', event.key,
event.scancode, event.unicode):
continue
self.dispatch_event('on_keyboard', event.key,
self.dispatch('on_keyboard', event.key,
event.scancode, event.unicode)
# video resize
@ -219,7 +220,7 @@ class WindowPygame(WindowBase):
def mainloop(self):
# don't known why, but pygame required a resize event
# for opengl, before mainloop... window reinit ?
self.dispatch_event('on_resize', *self.size)
self.dispatch('on_resize', *self.size)
while not EventLoop.quit:
try:

View File

@ -9,5 +9,5 @@ Kivy. We hardly ask you to use theses class !
.. seealso:: Read the full documentation at :mod:`kivy.c_ext.c_graphics`
'''
from kivy.c_ext.graphics import Canvas
from kivy.c_ext.graphics import *

View File

@ -143,14 +143,15 @@ class MouseTouchProvider(TouchProvider):
def update(self, dispatch_fn):
'''Update the mouse provider (pop event from the queue)'''
if not self.window:
from kivy.base import getWindow
self.window = getWindow()
from kivy.core.window import Window
self.window = Window
if self.window:
self.window.push_handlers(
on_mouse_move=self.on_mouse_motion,
Window.bind(on_mouse_move=self.on_mouse_motion)
''',
on_mouse_down=self.on_mouse_press,
on_mouse_up=self.on_mouse_release
)
'''
if not self.window:
return
try:

View File

@ -193,7 +193,7 @@ class LoaderBase(object):
# got one client to update
client.image = image
client.loaded = True
client.dispatch_event('on_load')
client.dispatch('on_load')
self._client.remove((c_filename, client))
def image(self, filename, load_callback=None, post_callback=None):

View File

@ -39,7 +39,7 @@ class TouchInfos(MTWidget):
self.bubbles[uid] = bubble
bubble.pos = touch.pos
bubble.label = info(touch)
bubble.dispatch_event('on_draw')
bubble.dispatch('on_draw')
alive = [x.uid for x in current]
for uid in bubbles.keys()[:]:

View File

@ -58,7 +58,7 @@ class bench_widget_dispatch:
def run(self):
root = self.root
for x in xrange(1000):
root.dispatch_event('on_update')
root.dispatch('on_update')
class bench_graphx_line:
'''Graphx: draw lines (5000 x/y) 1000 times'''

Binary file not shown.

View File

@ -1,61 +0,0 @@
'''
BaseObject
'''
from init import test, import_kivy_no_window
def unittest_defaults():
import_kivy_no_window()
from kivy import BaseObject
a = BaseObject()
test(a.x == 0)
test(a.y == 0)
test(a.width == 0)
test(a.height == 0)
test(a.pos == (0, 0))
test(a.size == (0, 0))
# test every accessor
a.x = 2
test(a.x == 2)
test(a.pos == (2, 0))
test(a.center == (2, 0))
a.y = 2
test(a.y == 2)
test(a.pos == (2, 2))
test(a.center == (2, 2))
a.pos = (0, 0)
test(a.x == 0)
test(a.y == 0)
test(a.pos == (0, 0))
test(a.center == (0, 0))
a.width = 2
test(a.width == 2)
test(a.size == (2, 0))
test(a.center == (1, 0))
a.height = 2
test(a.height == 2)
test(a.size == (2, 2))
test(a.center == (1, 1))
a.size = (0, 0)
test(a.width == 0)
test(a.height == 0)
test(a.size == (0, 0))
test(a.center == (0, 0))
a.center = (5, 5)
test(a.x == 5)
test(a.y == 5)
test(a.pos == (5, 5))
test(a.width == 0)
test(a.height == 0)
test(a.size == (0, 0))
a.size = (20, 20)
test(a.center == (15, 15))

View File

@ -1,47 +0,0 @@
'''
Css styling basic tests
'''
from init import test, import_kivy_no_window
def unittest_css():
import_kivy_no_window()
from kivy import MTWidget, Label
from kivy import css_add_sheet
css_add_sheet('''
.style {
bg-color: rgba(255, 255, 255, 255);
}
#my { bg-color : rgba(255, 0, 255, 0);}
''')
w = MTWidget(cls='style')
x = MTWidget(id='my',cls='style')
test(w.style['bg-color'] == [1.0 ,1.0 ,1.0 ,1.0])
test(x.style['bg-color'] == [1.0 ,0.0 ,1.0 ,0.0])
x.style['bg-color'] = [0, 0, 0, 0]
test(x.style['bg-color'] == [0 ,0 ,0 ,0])
def unittest_css_label():
import_kivy_no_window()
from kivy import MTLabel, css_add_sheet
css_add_sheet('''
.style {
color: rgba(0, 255, 0, 255);
}
''')
l = MTLabel(label='test', cls='style')
test(l.style['color'] == [0.0, 1.0, 0.0, 1.0])
def unittest_css_multiclass():
import_kivy_no_window()
from kivy import MTLabel, css_add_sheet
css_add_sheet('''
.test1 {
font-color : rgba(255,255,255,255);
}
.test2 {
font-size: 24;
}
''')
l = MTLabel(label = 'test', cls=('test1', 'test2'))
test(l.style['font-size'] == 24)

View File

@ -1,129 +0,0 @@
'''
Layout
'''
from init import test, import_kivy_no_window
def unittest_boxlayout_horizontal():
_test_boxlayout('horizontal')
def unittest_boxlayout_vertical():
_test_boxlayout('vertical')
def _test_boxlayout(orientation):
import_kivy_no_window()
from kivy import MTBoxLayout, MTWidget
# note: this test act always if orientation
# is a horizontal one. use sw() around pos or size
# to ensure that the swap is done.
def sw(tpl):
tpl = tuple(map(int, tpl))
if orientation == 'vertical':
return tpl[1], tpl[0]
return tpl
# note: default spacing is 1
# default padding is 0
# default add
m = MTBoxLayout(orientation=orientation)
for x in xrange(10):
m.add_widget(MTWidget(size=(10,10)))
test(sw(m.size) == (109, 10))
#
# spacing to 10
#
m = MTBoxLayout(orientation=orientation, spacing=10)
for x in xrange(10):
m.add_widget(MTWidget(size=(10,10)))
test(sw(m.size) == (190, 10))
#
# padding to 10
#
m = MTBoxLayout(orientation=orientation, padding=10, spacing=0)
for x in xrange(10):
m.add_widget(MTWidget(size=(10,10)))
m.do_layout()
# size should be 10 (number of widget) * width (10) + 2 * padding
test(sw(m.size) == (120, 30))
for x in xrange(10):
if orientation == 'vertical':
test(sw(m.children[x].pos) == (10 + x * 10, 10))
else:
test(sw(m.children[x].pos) == (10 + (9 - x) * 10, 10))
#
# testing size_hint with padding
#
m = MTBoxLayout(orientation=orientation, padding=10, spacing=0,
size_hint=(None, None), size=(500, 500))
m.add_widget(MTWidget(size_hint=(1, 1)))
m.do_layout()
test(sw(m.size) == (500, 500))
test(sw(m.children[0].size) == (480, 480))
#
# testing size_hint with spacing
#
m = MTBoxLayout(orientation=orientation, spacing=10,
size_hint=(None, None), size=(500, 500))
m.add_widget(MTWidget(size_hint=(1, 1)))
m.do_layout()
# only one should have no impact
test(sw(m.size) == (500, 500))
test(sw(m.children[0].size) == (500, 500))
# add a second widget
m.add_widget(MTWidget(size_hint=(1, 1)))
m.do_layout()
# now, we should see difference
test(sw(m.size) == (500, 500))
test(sw(m.children[0].size) == (245, 500))
test(sw(m.children[1].size) == (245, 500))
#
# testing with padding + spacing
#
m = MTBoxLayout(orientation=orientation, spacing=10, padding=10)
for x in xrange(10):
m.add_widget(MTWidget(size=(10,10)))
m.do_layout()
test(sw(m.size) == (210, 30))
for x in xrange(10):
if orientation == 'vertical':
test(sw(m.children[x].pos) == (10 + x * 20, 10))
else:
test(sw(m.children[x].pos) == (10 + (9 - x) * 20, 10))
#
# testing with padding + spacing + size_hint
#
m = MTBoxLayout(orientation=orientation, spacing=10, padding=10,
size_hint=(None, None), size=(500, 500))
m.add_widget(MTWidget(size_hint=(1, 1)))
m.add_widget(MTWidget(size_hint=(1, 1)))
m.do_layout()
# now, we should see difference
test(sw(m.size) == (500, 500))
test(sw(m.children[0].size) == (235, 480))
test(sw(m.children[1].size) == (235, 480))
if orientation == 'vertical':
test(sw(m.children[0].pos) == (10, 10))
test(sw(m.children[1].pos) == (255, 10))
else:
test(sw(m.children[0].pos) == (255, 10))
test(sw(m.children[1].pos) == (10, 10))

View File

@ -1,85 +0,0 @@
'''
New properties
'''
from init import test, import_kivy_no_window
def unittest_property():
import_kivy_no_window()
from kivy.c_ext.c_properties import Property
a = Property(-1)
test(a.get() == -1)
a.set(0)
test(a.get() == 0)
a.set(1)
test(a.get() == 1)
def unittest_property_observer():
import_kivy_no_window()
from kivy.c_ext.c_properties import Property
a = Property(-1)
test(a.get() == -1)
global observe_called
observe_called = 0
def observe(value):
global observe_called
observe_called = 1
a.bind(observe)
a.set(0)
test(a.get() == 0)
test(observe_called == 1)
observe_called = 0
a.set(0)
test(a.get() == 0)
test(observe_called == 0)
a.set(1)
test(a.get() == 1)
test(observe_called == 1)
def unittest_property_stringcheck():
import_kivy_no_window()
from kivy.c_ext.c_properties import StringProperty
a = StringProperty('')
test(a.get() == '')
a.set('hello')
test(a.get() == 'hello')
try:
a.set(88) # number shouldn't be accepted
test('string accept number, fail.' == 0)
except ValueError:
test('string dont accept number')
def unittest_property_numericcheck():
import_kivy_no_window()
from kivy.c_ext.c_properties import NumericProperty
a = NumericProperty(0)
test(a.get() == 0)
a.set(99)
test(a.get() == 99)
try:
a.set('') # string shouldn't be accepted
test('number accept string, fail.' == 0)
except ValueError:
test('number dont accept string')
def unittest_property_propertynone():
import_kivy_no_window()
from kivy.c_ext.c_properties import NumericProperty
a = NumericProperty(0, allownone=True)
test(a.get() == 0)
try:
a.set(None)
test(a.get() == None)
except ValueError, e:
print e
test('none not accepted' == 0)
a.set(1)
test(a.get() == 1)

View File

@ -1,170 +0,0 @@
'''
Test usage of MTTextArea widget
'''
from init import test, import_kivy_window
def instance(**kwargs):
''' Individual test framework'''
import_kivy_window()
from kivy import MTTextArea
from kivy import css_add_sheet, css_reload
try:
return MTTextArea(**kwargs)
except:
return None
def unittest_mttextarea_cursor():
t = instance()
test(t is not None)
if t is None:
return
test(t.cursor == (0, 0))
test(t.cursor_index == 0)
t.value = 'abc\ndef\nghi'
test(len(t.value) == 11)
test(t.cursor == (3, 2))
test(t.cursor_index == 11)
# test some cursor position from text index
test(t.get_cursor_from_index(0) == (0, 0))
test(t.get_cursor_from_index(1) == (1, 0))
test(t.get_cursor_from_index(2) == (2, 0))
test(t.get_cursor_from_index(3) == (3, 0))
test(t.get_cursor_from_index(4) == (0, 1))
test(t.get_cursor_from_index(5) == (1, 1))
test(t.get_cursor_from_index(6) == (2, 1))
test(t.get_cursor_from_index(7) == (3, 1))
test(t.get_cursor_from_index(8) == (0, 2))
test(t.get_cursor_from_index(9) == (1, 2))
test(t.get_cursor_from_index(10) == (2, 2))
test(t.get_cursor_from_index(11) == (3, 2))
# now, set the cursor, and check the index
t.cursor = (0, 0)
test(t.cursor_index == 0)
t.cursor = (1, 0)
test(t.cursor_index == 1)
t.cursor = (2, 0)
test(t.cursor_index == 2)
t.cursor = (3, 0)
test(t.cursor_index == 3)
t.cursor = (0, 1)
test(t.cursor_index == 4)
t.cursor = (1, 1)
test(t.cursor_index == 5)
t.cursor = (2, 1)
test(t.cursor_index == 6)
t.cursor = (3, 1)
test(t.cursor_index == 7)
t.cursor = (0, 2)
test(t.cursor_index == 8)
t.cursor = (1, 2)
test(t.cursor_index == 9)
t.cursor = (2, 2)
test(t.cursor_index == 10)
t.cursor = (3, 2)
test(t.cursor_index == 11)
# test bounds
test(t.get_cursor_from_index(-1) == (0, 0))
test(t.get_cursor_from_index(-100) == (0, 0))
print t.get_cursor_from_index(12)
test(t.get_cursor_from_index(12) == (3, 2))
test(t.get_cursor_from_index(100) == (3, 2))
def unittest_mttextarea_basics():
'''Test driver'''
# Test defaults
t = instance()
test(t is not None)
if t is None: return
test(t.height == 100)
test(t.width == 100)
test(t.value == '')
test(len(t.lines) == 1)
# Test operations with a single line in widget
tline1 = 'This is a single line'
t.value = tline1
test(tline1 == t.value)
test(len(t.lines) == 3)
test(t.height == 100)
test(t.width == 100)
# Replace text with an empty string
t.value = ''
test(t.value == '')
test(len(t.lines) == 1)
test(t.height == 100)
test(t.width == 100)
# Now lets put in a string of 12 lines and see what happens
tline2 = ['', 'Line 1', ('Line 2 which is rather long and should overflow'
' horizontally ........................'),
'Line 3', 'Line 4', 'Line 5 ', 'Line 6', ' Line 7',
'Line 8', '', 'Line 10', '']
tline2b = '\n'.join(tline2)
t.value = tline2b
# Among other things, make sure leading and trailing white space
# are not lost
test(tline2b == t.value)
test(len(t.lines) == 21)
test(t.height == 100)
test(t.width == 100)
# Lets replace line 8
'''This is now deprecated.
lt1 = 'Replacement text for line 8'
tline2[7] = lt1
tline2c = '\n'.join(tline2)
t.set_line_text(7, lt1)
test(tline2c == t.value)
test(len(t.lines) == 12)
test(t.height == 100)
test(t.width == 100)
'''
# Test full auto-sizing
del t
t = instance(autosize=True)
test(t is not None)
if t is None: return
# Test defaults
test(int(t.height) == 25)
test(int(t.width) == 1)
test(t.value == '')
test(len(t._glyph_size) == 0)
test(len(t.lines) == 1)
# Test operations with a single line in widget
# This test assumes the default font and Pygame as text manager running
# on Ubuntu 10.04.
# Other text managers may give slightly different results for dimensions
# and different fonts could cause larger variations
t.value = tline1
test(tline1 == t.value)
test(len(t.lines) == 1)
test(int(t.height) == 25)
test(int(t.width) == 203)
# Replace text with an empty string
t.value = ''
test(t.value == '')
test(len(t.lines) == 1)
test(int(t.height) == 25)
test(int(t.width) == 1)
# Now lets put in a string of 12 lines and see what happens
t.value = tline2b
# Among other things, make sure leading and trailing white space
# are not lost
test(tline2b == t.value)
test(len(t.lines) == 12)
test(int(t.height) == 322)
test(int(t.width) == 809)

View File

@ -1,19 +0,0 @@
'''
test usage of MTTextInput widget
'''
from init import test, import_kivy_window
def instance():
import_kivy_window()
from kivy import MTTextInput
from kivy import css_add_sheet, css_reload
try:
t = MTTextInput()
return True
except:
return False
def unittest_mttextinput():
test(instance())

View File

@ -1,101 +0,0 @@
'''
Widgets
'''
from init import test, import_kivy_no_window
def unittest_defaults():
import_kivy_no_window()
from kivy import MTWidget
w = MTWidget()
test(w.x == 0)
test(w.y == 0)
test(w.width == 100)
test(w.height == 100)
test(w.visible == True)
test(w.draw_children == True)
test(w.cls == '')
def unittest_visible_methods():
import_kivy_no_window()
from kivy import MTWidget
w = MTWidget()
w.hide()
test(w.visible == False)
w.show()
test(w.visible == True)
def unittest_visible_events():
import_kivy_no_window()
from kivy import MTWidget
global on_update_called
on_update_called = 0
def on_update():
global on_update_called
on_update_called += 1
# by default, visible is True
w = MTWidget()
w.connect('on_draw', on_draw)
w.dispatch_event('on_draw')
test(on_draw_called == 1)
# make it invisible
w.visible = False
w.dispatch_event('on_draw')
test(on_draw_called == 1)
# make it visible
w.visible = True
w.dispatch_event('on_draw')
test(on_draw_called == 2)
# create a new widget, visible default to False
on_draw_called = 0
w = MTWidget(visible=False)
try:
# XXX FIXME unable to connect to default on_draw
# since it's not yet register.
w.connect('on_draw', on_draw)
except:
pass
w.dispatch_event('on_draw')
test(on_draw_called == 0)
w.visible = True
w.connect('on_draw', on_draw)
w.dispatch_event('on_draw')
test(on_draw_called == 1)
def unittest_coordinate_transform():
import_kivy_no_window()
from kivy import MTWidget
# child 2 inside child 1 inside child0
child0 = MTWidget(pos=(100, 100))
child1 = MTWidget(pos=(200, 200))
child2 = MTWidget(pos=(300, 300))
child0.add_widget(child1)
child1.add_widget(child2)
test(child0.pos == (100, 100))
test(child1.pos == (200, 200))
test(child2.pos == (300, 300))
# screen coordinate is default
test(child0.to_local(*child1.pos) == (200, 200))
# using the relative attribute,
# we should have relative coordinate
test(child0.to_local(*child1.pos, relative=True) == (100, 100))
test(child1.to_local(*child2.pos, relative=True) == (100, 100))
# screen coordinate 400,400 is 100,100 in relative coordinate from child2
test(child2.to_widget(400, 400, relative=True) == (100, 100))
# 100, 100 relative coordinate from child2 is 400, 400 in screen coordinate
test(child2.to_window(100, 100, relative=True) == (400, 400))