some event tests

This commit is contained in:
James Turk 2011-03-27 22:09:39 -04:00
parent 5aba9f5cf4
commit ae0f2d4cab
3 changed files with 75 additions and 8 deletions

View File

@ -3,7 +3,7 @@ from .internal import _SDL, errcheck, Version
from enum import CEnum
# Constants
class INIT(CEnum):
class InitFlags(CEnum):
TIMER = 0x00000001
AUDIO = 0x00000010
VIDEO = 0x00000020

View File

@ -1,5 +1,5 @@
import ctypes
from .internal import _SDL, Version
from .internal import _SDL, Version, errcheck, SDLError
from .enum import CEnum
class EventType(CEnum):
@ -142,7 +142,7 @@ class MouseButtonEvent(ctypes.Structure):
('type', ctypes.c_uint32),
('window_id', ctypes.c_uint32),
('button', ctypes.c_uint8),
('istate', ctypes.c_uint8),
('state', ctypes.c_uint8),
('_padding1', ctypes.c_uint8),
('_padding2', ctypes.c_uint8),
('x', ctypes.c_int),
@ -213,7 +213,7 @@ class TouchFingerEvent(ctypes.Structure):
('pressure', ctypes.c_uint16),
)
class TouchFingerEvent(ctypes.Structure):
class TouchButtonEvent(ctypes.Structure):
_fields_ = (
('type', ctypes.c_uint32),
('window_id', ctypes.c_uint32),
@ -301,7 +301,7 @@ class Event(ctypes.Union):
('user', UserEvent),
('syswm', SysWMEvent),
('tfinger', TouchFingerEvent),
#('tbutton', TouchButtonEvent),
('tbutton', TouchButtonEvent),
('mgesture', MultiGestureEvent),
('dgesture', DollarGestureEvent),
('active', ActiveEvent),
@ -341,9 +341,9 @@ def wait_event(timeout=0):
event = Event()
if timeout:
retval = _SDL.SDL_WaitEvent(ctypes.byref(event))
else:
retval = _SDL.SDL_WaitEventTimeout(ctypes.byref(event), timeout)
else:
retval = _SDL.SDL_WaitEvent(ctypes.byref(event))
if retval:
return event
@ -355,4 +355,7 @@ def event_state(type, state):
errcheck(_SDL.SDL_EventState(type, state))
def register_events(num_events):
return _SDL.SDL_RegisterEvents(num_events)
registered = _SDL.SDL_RegisterEvents(num_events)
if registered < 0:
raise SDLError("out of available event ids for register_events")
return registered

64
csdl/tests/test_events.py Normal file
View File

@ -0,0 +1,64 @@
from nose.tools import with_setup, raises
from csdl import init, InitFlags
from csdl.internal import SDLError
from csdl.events import *
def get_mm_event():
mm_event = MouseMotionEvent(type=EventType.MOUSEMOTION,
window_id=1, state=1,
x=31, y=14, x_rel=1, y_rel=1)
return Event(type=EventType.MOUSEMOTION, motion=mm_event)
def setup_clean_queue():
init(InitFlags.VIDEO)
flush_events(EventType.FIRSTEVENT, EventType.LASTEVENT)
@with_setup(setup_clean_queue)
def test_has_events():
assert not has_events(EventType.FIRSTEVENT, EventType.LASTEVENT)
push_event(get_mm_event())
assert has_events(EventType.FIRSTEVENT, EventType.LASTEVENT)
@with_setup(setup_clean_queue)
def test_has_event():
assert not has_event(EventType.MOUSEMOTION)
push_event(get_mm_event())
assert has_event(EventType.MOUSEMOTION)
@with_setup(setup_clean_queue)
def test_poll_event():
# push 3 on
push_event(get_mm_event())
push_event(get_mm_event())
push_event(get_mm_event())
# pull 4 off, last should be None
first = poll_event()
second = poll_event()
third = poll_event()
too_many = poll_event()
assert first and second and third
assert not too_many
@with_setup(setup_clean_queue)
def test_wait_event_timeout():
# push one on
push_event(get_mm_event())
# pull two off, should timeout for second one
first = wait_event(1)
too_many = wait_event(1)
assert first
assert not too_many
def test_register_events():
first = register_events(100)
assert first == EventType.USEREVENT
next = register_events(1)
assert next == first+100
@raises(SDLError)
def test_register_events_exception():
too_many = register_events(100000)