3
8ßL]´  ã               @   sà   d Z ddlZddlZddlZy&ddlmZmZ ddlmZ eƒ Z	W n e
k
rZ   dZY nX dZddlmZ ddlmZmZmZmZ G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deejƒZG dd„ deejƒZdS )a   Tests for dynamic notifiers with `dispatch='ui'`.

Dynamic notifiers created with the `dispatch='ui'` option dispatch event
notifications on the UI thread. The class handling the dispatch,
`FastUITraitChangeNotifyWrapper`, is a subclass of `TraitChangeNotifyWrapper`.
Most of the functionality of the class is thus already covered by the
`TestDynamicNotifiers` test case, and we only need to test that the
notification really occurs on the UI thread.

At present, `dispatch='ui'` and `dispatch='fast_ui'` have the same effect.

é    N)Úget_app_qt4Ústart_event_loop_qt4)ÚtoolkitFT)Útrait_notifiers)ÚCallableÚFloatÚ	HasTraitsÚon_trait_changec               @   s   e Zd ZeZdS )ÚCalledAsMethodN)Ú__name__Ú
__module__Ú__qualname__r   Úfoo© r   r   úB/tmp/pip-install-5sebvq2j/traits/traits/tests/test_ui_notifiers.pyr
   (   s   r
   c               @   s(   e Zd ZeZeZeddd�dd„ ƒZdS )ÚCalledAsDecoratorr   Úui)Údispatchc             C   s   | j ||||ƒ d S )N)Úcallback)ÚselfÚobjÚnameÚoldÚnewr   r   r   Úon_foo_change1   s    zCalledAsDecorator.on_foo_changeN)	r   r   r   r   r   r   r   r	   r   r   r   r   r   r   ,   s   r   c               @   sT   e Zd ZdZdd„ Zdd„ Zdd„ Zeje	 dƒd	d
„ ƒZ
eje	 dƒdd„ ƒZdS )ÚBaseTestUINotifiersz7 Tests for dynamic notifiers with `dispatch='ui'`.
    c             C   s
   g | _ d S )N)Únotifications)r   r   r   r   ÚsetUp<   s    zBaseTestUINotifiers.setUpc             C   s   t jƒ  t jƒ  dS )z! Post and process the Qt events. N)Úqt4_appZsendPostedEventsZprocessEvents)r   r   r   r   Úflush_event_loopA   s    z$BaseTestUINotifiers.flush_event_loopc             C   s*   t jƒ j}|||||ff}| jj|ƒ d S )N)Ú	threadingÚcurrent_threadÚidentr   Úappend)r   r   r   r   r   Ú	thread_idÚeventr   r   r   Úon_foo_notificationsF   s    
z(BaseTestUINotifiers.on_foo_notificationsz2Qt event loop not found, UI dispatch not possible.c             C   sb   | j ƒ }d|_| jƒ  | j}| jt|ƒdƒ |d \}}| j||dddfƒ tj}| j||ƒ d S )Né   é   r   r   )Úobj_factoryr   r   r   ÚassertEqualÚlenr   Ú	ui_thread)r   r   r   r$   r%   r,   r   r   r   Ú"test_notification_from_main_threadM   s    z6BaseTestUINotifiers.test_notification_from_main_threadc             C   s‚   | j ƒ }dd„ }tj||fd�jƒ  tjdƒ | jƒ  | j}| jt	|ƒdƒ |d \}}| j||dddfƒ t
j}| j||ƒ d S )	Nc             S   s
   d| _ d S )Nr'   )r   )r   r   r   r   Úset_foo_to_3h   s    zPBaseTestUINotifiers.test_notification_from_separate_thread.<locals>.set_foo_to_3)ÚtargetÚargsgš™™™™™¹?r(   r   r   r'   )r)   r    ÚThreadÚstartÚtimeÚsleepr   r   r*   r+   r   r,   )r   r   r.   r   r$   r%   r,   r   r   r   Ú&test_notification_from_separate_thread`   s    
z:BaseTestUINotifiers.test_notification_from_separate_threadN)r   r   r   Ú__doc__r   r   r&   ÚunittestZskipIfÚQT_FOUNDr-   r5   r   r   r   r   r   6   s   r   c               @   s   e Zd ZdZdd„ ZdS )ÚTestMethodUINotifierszJ Tests for dynamic notifiers with `dispatch='ui'` set by method call.
    c             C   s   t ƒ }|j| jddd� |S )Nr   r   )r   )r
   r	   r&   )r   r   r   r   r   r)   €   s    z!TestMethodUINotifiers.obj_factoryN)r   r   r   r6   r)   r   r   r   r   r9   |   s   r9   c               @   s   e Zd ZdZdd„ ZdS )ÚTestDecoratorUINotifierszD Tests for dynamic notifiers with `dispatch='ui'` set by decorator. c             C   s   t | jd�S )N)r   )r   r&   )r   r   r   r   r)   ‰   s    z$TestDecoratorUINotifiers.obj_factoryN)r   r   r   r6   r)   r   r   r   r   r:   †   s   r:   )r6   r    r3   r7   Zpyface.util.guisupportr   r   Ztraitsui.qt4r   r   Ú	Exceptionr8   Ztraitsr   Z
traits.apir   r   r   r	   r
   r   Úobjectr   ZTestCaser9   r:   r   r   r   r   Ú<module>   s"   


F
