a
    §ˆc’"  ã                   @   sp  d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ G d	d
„ d
e jƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zej d¡Zdd„ Zdd„ Zd:dd„Z dd„ Z!d d!„ Z"d"d#„ Z#d$d%„ Z$d&d'„ Z%d(d)„ Z&d;d*d+„Z'd,d-„ Z(d.d/„ Z)d0d1„ Z*d2d3„ Z+ed4ƒZ,e,j-d5kZ.e.�r^G d6d7„ d7e/ƒZ,G d8d9„ d9ƒZ0dS )<é    N)ÚskipIfÚ	TestSuite)Útoolkit_object)Úpop_exception_handlerÚpush_exception_handler)Ú	ETSConfig©Úreraise_exceptions)Úprocess_cascade_events)ÚUITesterc                   @   s   e Zd ZdZdZdZdS )ÚToolkitNameÚwxÚqtÚnullN)Ú__name__Ú
__module__Ú__qualname__r   r   r   © r   r   úX/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/tests/_tools.pyr   "   s   r   c                   C   s   t jtjjkS )z)Return true if the toolkit backend is wx.)r   Útoolkitr   r   Únamer   r   r   r   Úis_wx(   s    r   c                   C   s   t j tjj¡S )zRReturn true if the toolkit backend is Qt
    (that includes Qt4 or Qt5, etc.)
    )r   r   Ú
startswithr   r   r   r   r   r   r   Úis_qt-   s    r   c                  C   s   t ƒ s
dS ddl} | jjS )z*Return true if the toolkit backend is Qt5.Fr   N)r   Ú	pyface.qtr   Úis_qt5©Úpyfacer   r   r   r   4   s    r   c                  C   s   t ƒ s
dS ddl} | jjS )z*Return true if the toolkit backend is Qt6.Fr   N)r   r   r   Úis_qt6r   r   r   r   r   @   s    r   c                   C   s   t jtjjkS )z+Return true if the toolkit backend is null.)r   r   r   r   r   r   r   r   r   Úis_nullL   s    r   c                    s:   t jtt jtt jti‰ tt‡ fdd„| D ƒƒ d 	| ¡ƒS )zñDecorator factory for skipping tests if the current toolkit is not
    one of the given values.

    Parameters
    ----------
    toolkits : iterable of members of ToolkitName
        e.g. ``list(ToolkitName)`` to include all toolkits.
    c                 3   s   | ]}ˆ | ƒ V  qd S ©Nr   )Ú.0r   ©Úmappingr   r   Ú	<genexpr>`   ó    z#requires_toolkit.<locals>.<genexpr>z'Test requires one of these toolkits: {})
r   r   r   r   r   r   r   r   ÚanyÚformat)Ztoolkitsr   r"   r   Úrequires_toolkitQ   s    
ýþr(   Údarwinc                    s   ‡ ‡fdd„‰dˆ_ ˆS )zrDecorator that stores the number of times a function is called.

    The counter is stored in func._n_counts.
    c                     s   ˆ j d7  _ ˆ | i |¤ŽS )Né   ©Z_n_calls)ÚargsÚkwargs©ÚfuncÚwrappedr   r   r0   o   s    zcount_calls.<locals>.wrappedr   r+   )r/   r   r.   r   Úcount_callsi   s    r1   c                    sv   t ƒ }| D ]f‰ tˆ t ƒr.tˆ |ƒ}| |¡ q
t |ˆ  ¡ ¡}|d urfd |¡‰tˆ d‡ ‡fdd„ƒ | ˆ ¡ q
|S )NzTest excluded via pattern '{}'ÚsetUpc                      s
   ˆ   ˆ¡S r    )ZskipTestr   ©ÚitemÚskip_msgr   r   Ú<lambda>„   r%   zfilter_tests.<locals>.<lambda>)	r   Ú
isinstanceÚfilter_testsZaddTestÚreÚsearchÚidr'   Úsetattr)Z
test_suiteZexclusion_patternZfiltered_test_suiteÚfilteredÚmatchr   r3   r   r8   x   s    

ÿr8   c                 C   s   t ƒ j| |d�S )aT  Context manager for creating a UI and then dispose it when exiting
    the context.

    Parameters
    ----------
    object : HasTraits
        An object from which ``edit_traits`` can be called to create a UI
    ui_kwargs : dict or None
        Keyword arguments to be provided to ``edit_traits``.

    Yields
    ------
    ui: UI
    ©ÚobjectZ	ui_kwargs)r   Ú	create_uir?   r   r   r   rA   ‰   s    rA   c                 C   s   t ƒ r|  ¡ S |  ¡ S d S r    )r   ÚGetChildrenÚchildren©Únoder   r   r   Úget_childrenž   s    rF   c                 C   sj   t ƒ r<ddl}| j d| j¡}| |j| ¡ ¡}| |¡ n*tƒ rfddl	m
} | j |jj¡}| ¡  dS )z)Press the OK button in a wx or qt dialog.r   NÚbutton)r   )r   r   ÚcontrolZFindWindowByNameÚCommandEventZwxEVT_COMMAND_BUTTON_CLICKEDÚGetIdZProcessEventr   r   r   Z	findChildZQtGuiZQPushButtonÚclick)Zuir   Z	ok_buttonZclick_eventr   r   r   r   Úpress_ok_button¥   s    
ÿrL   c                 C   sT   t ƒ r:ddl}| |jj|  ¡ ¡}| | ¡ | | |¡ ntƒ rJ|  	¡  nt
ƒ ‚dS )z#Click the button given its control.r   N)r   r   rI   Z
EVT_BUTTONZtypeIdrJ   ZSetEventObjectZ	PostEventr   rK   ÚNotImplementedError)rG   r   Úeventr   r   r   Úclick_button¹   s    

rO   c                 C   s&   t ƒ r|  ¡ S tƒ r|  ¡ S tƒ ‚dS )z.Return if the given control is enabled or not.N)r   Z	IsEnabledr   Z	isEnabledrM   )rH   r   r   r   Úis_control_enabledÊ   s
    rP   c                 C   s0   t ƒ r|  ¡ S tƒ r,|  ¡  ¡ |  ¡  ¡ fS dS )z¡Return the size of the dialog.

    Return a tuple (width, height) with the size of the dialog in pixels.
    E.g.:

        >>> get_dialog_size(ui.control)
    N)r   ZGetSizer   ÚsizeÚwidthÚheight)Z
ui_controlr   r   r   Úget_dialog_size×   s    	rT   c                 C   s†   g }t ƒ r>|  ¡  ¡ D ]$}| ¡ }|jdkr| | ¡ ¡ qnDtƒ r||  ¡ }t	| 
¡ ƒD ] }| |¡ ¡ }| | ¡ ¡ qXntƒ ‚|S )z�Get status of all 2-state (wx) or checkable (qt) buttons under given
    control.

    Assumes all sizer children (wx) or layout items (qt) are buttons.
    Ú )r   ZGetSizerrB   Z	GetWindowÚvalueÚappendZGetValuer   ÚlayoutÚrangeÚcountZitemAtZwidgetZ	isCheckedrM   )rH   Zbutton_statusr4   rG   rX   Úir   r   r   Úget_all_button_statusç   s    
r\   c                 C   sR   t d| t|ƒ ƒ t d| t| |ƒƒ d ƒ t|ƒD ]}t| ||d ƒ q8dS )zCPrint the result of applying a function on `node` and its children.ú-ú Ú
r*   N)ÚprintÚstrrF   Úapply_on_children)r/   rE   Ú_levelÚchildr   r   r   rb     s    rb   c                 C   s   t dd„ | ƒ dS )z�Print the name and id of `node` and its children.

    Use as::

        >>> ui = xxx.edit_traits()
        >>> wx_print_names(ui.control)
    c                 S   s   |   ¡ |  ¡ fS r    )ÚGetNamerJ   ©Únr   r   r   r6     r%   z wx_print_names.<locals>.<lambda>N©rb   rD   r   r   r   Úwx_print_names  s    ri   c                 C   s   t dd„ | ƒ dS )zˆPrint the name of `node` and its children.

    Use as::

        >>> ui = xxx.edit_traits()
        >>> qt_print_names(ui.control)
    c                 S   s   |   ¡ S r    )Z
objectNamerf   r   r   r   r6      r%   z qt_print_names.<locals>.<lambda>Nrh   rD   r   r   r   Úqt_print_names  s    rj   c                    s(   ˆj ‰ ‡ ‡fdd„}|ˆ_ d ˆ ¡ ¡S )z¦Prints a message when `node` is destroyed.

    Use as:

        >>> ui = xxx.edit_traits()
        >>> apply_on_children(wx_announce_when_destroyed, ui.control)
    c                      s   t dˆƒ ˆ ƒ  t dˆƒ d S )NzDestroying:z
Destroyed:)r`   r   ©Z_destroy_methodrE   r   r   Údestroy_wrapped.  s    
z3wx_announce_when_destroyed.<locals>.destroy_wrappedzNode {} decorated)ZDestroyr'   re   )rE   rl   r   rk   r   Úwx_announce_when_destroyed#  s    	rm   c                    s$   ddl ‰‡ ‡fdd„tˆƒD ƒ}|S )zÇFind all wx event names that correspond to a certain event number.

    Example:

        >>> wx_find_event_by_number(10010)
        ['wxEVT_COMMAND_MENU_SELECTED', 'wxEVT_COMMAND_TOOL_CLICKED']
    r   Nc                    s(   g | ] }|  d ¡rtˆ|ƒˆ kr|‘qS )ZwxEVT)r   Úgetattr)r!   Úattr©Úevt_numr   r   r   Ú
<listcomp>D  s   þz+wx_find_event_by_number.<locals>.<listcomp>)r   Údir)rq   Úpossibler   rp   r   Úwx_find_event_by_number9  s
    	þru   z(util.gui_test_assistant:GuiTestAssistantZUnimplementedc                   @   s   e Zd ZdS )ÚGuiTestAssistantN)r   r   r   r   r   r   r   rv   R  s   rv   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚBaseTestMixinz…This is a mixin class for all test cases in TraitsUI, regardless of
    whether GUI is involved.

    Not to be used externally.
    c                 C   s   t dd� d S )NTr   )r   ©Úselfr   r   r   r2   ]  s    zBaseTestMixin.setUpc                 C   s
   t ƒ  d S r    )r   rx   r   r   r   ÚtearDown`  s    zBaseTestMixin.tearDownN)r   r   r   Ú__doc__r2   rz   r   r   r   r   rw   V  s   rw   )N)r   )1Úenumr9   ÚsysZunittestr   r   Zpyface.toolkitr   Z
traits.apir   r   Ztraits.etsconfig.apir   Z$traitsui.testing._exception_handlingr	   Ztraitsui.testing._guir
   Ztraitsui.testing.apir   ÚEnumr   r   r   r   r   r   r(   Úplatformr   Z	is_mac_osr1   r8   rA   rF   rL   rO   rP   rT   r\   rb   ri   rj   rm   ru   rv   r   Zno_gui_test_assistantr@   rw   r   r   r   r   Ú<module>   sH   


