From ae0f2d4cab29e2aa1e10dbe8683aca3041f1bfeb Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 27 Mar 2011 22:09:39 -0400 Subject: [PATCH] some event tests --- csdl/__init__.py | 2 +- csdl/events.py | 17 ++++++----- csdl/tests/test_events.py | 64 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 csdl/tests/test_events.py diff --git a/csdl/__init__.py b/csdl/__init__.py index 1e003b0..99cbd9e 100644 --- a/csdl/__init__.py +++ b/csdl/__init__.py @@ -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 diff --git a/csdl/events.py b/csdl/events.py index 026961b..582d37b 100644 --- a/csdl/events.py +++ b/csdl/events.py @@ -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 diff --git a/csdl/tests/test_events.py b/csdl/tests/test_events.py new file mode 100644 index 0000000..3da5801 --- /dev/null +++ b/csdl/tests/test_events.py @@ -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)