o
    ¯bt  ã                   @   sæ   d Z ddlZddlmZ zddlmZ ddlmZ W n e	y'   dZdZ
Y nw eZzddlm
Z W n e	y=   dZ
Y n	w eZ
ddlmZ ddlmZ dd	lmZ dd
lmZmZ edu r`dZG dd„ deeƒZG dd„ deƒZdS )z
GI/GTK3 reactor tests.
é    N)ÚskipIf)ÚGio)Ú	gireactor)Úgtk3reactor)ÚGtk)ÚReactorAlreadyRunning)ÚReactorBuilder)ÚSkipTestÚTestCasezgtk3/gi not importablec                   @   sV   e Zd ZdZdd„ Zdd„ Zeedu dƒdd	„ ƒZd
d„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚGApplicationRegistrationTestsa  
    GtkApplication and GApplication are supported by
    L{twisted.internet.gtk3reactor} and L{twisted.internet.gireactor}.

    We inherit from L{ReactorBuilder} in order to use some of its
    reactor-running infrastructure, but don't need its test-creation
    functionality.
    c                    sr   t |dƒs	tdƒ‚g ‰‡ ‡fdd„‰‡ ‡‡fdd„}| d|¡ | ¡  ˆ  |¡ t | ˆ ¡ |  ˆdd	g¡ d
S )z˜
        Register the app, run the reactor, make sure app was activated, and
        that reactor was running, and that reactor can be stopped.
        Úquitz Version of PyGObject is too old.c                      s   ˆ  d¡ ˆ  ¡  d S )NÚstopped)ÚappendÚstop© )ÚreactorÚresultr   úF/usr/lib/python3/dist-packages/twisted/internet/test/test_gireactor.pyr   <   s   
z6GApplicationRegistrationTests.runReactor.<locals>.stopc                    s   ˆ  d¡ ˆ  dˆ¡ d S )NÚ	activatedr   )r   Ú	callLater)Úwidget©r   r   r   r   r   Úactivate@   s   
z:GApplicationRegistrationTests.runReactor.<locals>.activater   r   r   N)Úhasattrr	   ÚconnectÚholdÚregisterGApplicationr   Ú
runReactorÚassertEqual)ÚselfÚappr   r   r   r   r   r   2   s   

z(GApplicationRegistrationTests.runReactorc                 C   s<   t jdd�}|  | j|¡ tjdtjjd�}|  ||¡ dS )zR
        L{Gio.Application} instances can be registered with a gireactor.
        F©ÚuseGtkú!com.twistedmatrix.trial.gireactor©Úapplication_idÚflagsN)	r   Ú	GIReactorÚ
addCleanupÚunbuildReactorr   ÚApplicationÚApplicationFlagsÚ
FLAGS_NONEr   ©r   r   r    r   r   r   Útest_gApplicationActivateP   s   þz7GApplicationRegistrationTests.test_gApplicationActivateNz>Gtk unavailable (may require running with X11 DISPLAY env set)c                 C   s8   t  ¡ }|  | j|¡ tjdtjjd�}|  	||¡ dS )zT
        L{Gtk.Application} instances can be registered with a gtk3reactor.
        z#com.twistedmatrix.trial.gtk3reactorr$   N)
r   ÚGtk3Reactorr(   r)   r   r*   r   r+   r,   r   r-   r   r   r   Útest_gtkApplicationActivate]   s   þz9GApplicationRegistrationTests.test_gtkApplicationActivatec                 C   s<   t  ¡ }|  | j|¡ tjdtjjd�}|  t	|j
|¡ dS )zo
        L{gireactor.PortableGIReactor} doesn't support application
        registration at this time.
        r#   r$   N)r   ÚPortableGIReactorr(   r)   r   r*   r+   r,   ÚassertRaisesÚNotImplementedErrorr   r-   r   r   r   Útest_portablen   s   þz+GApplicationRegistrationTests.test_portablec                 C   sJ   t jdd�}|  | j|¡ tƒ }|  t|j|¡}|  |j	d  
d¡¡ dS )zp
        Older versions of PyGObject lack C{Application.quit}, and so won't
        allow registration.
        Fr!   r   zApplication registration is notN)r   r'   r(   r)   Úobjectr2   ÚRuntimeErrorr   Ú
assertTrueÚargsÚ
startswith)r   r   r    Úexcr   r   r   Útest_noQuit{   s
   z)GApplicationRegistrationTests.test_noQuitc                    sX   t jdd�‰ˆ ˆjˆ¡ tjdtjjd�‰ ‡ ‡‡fdd„}ˆ d|¡ t	 
ˆˆ¡ dS )	zp
        It is not possible to register a C{Application} after the reactor has
        already started.
        Fr!   r#   r$   c                     s.   ˆ  tˆjˆ ¡} ˆ | jd d¡ ˆ ¡  d S )Nr   z5Can't register application after reactor was started.)r2   r   r   r   r8   r   )r:   ©r    r   r   r   r   ÚtryRegister“   s   ÿ
ÿzLGApplicationRegistrationTests.test_cantRegisterAfterRun.<locals>.tryRegisterr   N)r   r'   r(   r)   r   r*   r+   r,   r   r   r   )r   r=   r   r<   r   Útest_cantRegisterAfterRun‡   s   þ	z7GApplicationRegistrationTests.test_cantRegisterAfterRunc                 C   sn   t jdd�}|  | j|¡ tjdtjjd�}| |¡ tjdtjjd�}|  	t
|j|¡}|  |jd d¡ dS )	zN
        It is not possible to register more than one C{Application}.
        Fr!   r#   r$   z"com.twistedmatrix.trial.gireactor2r   z2Can't register more than one application instance.N)r   r'   r(   r)   r   r*   r+   r,   r   r2   r6   r   r8   )r   r   r    Úapp2r:   r   r   r   Útest_cantRegisterTwiceŸ   s   þ
þ
ÿz4GApplicationRegistrationTests.test_cantRegisterTwice)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r.   r   r   r0   r4   r;   r>   r@   r   r   r   r   r   (   s    	þ
r   c                   @   s   e Zd ZdZdd„ ZdS )ÚPygtkCompatibilityTestsz_
    pygtk imports are either prevented, or a compatibility layer is used if
    possible.
    c                 C   s0   dt jvr	tdƒ‚ddl}|  |j d¡¡ dS )zo
        If compatibility layer is present, importing gobject uses
        the gi compatibility layer.
        zgi.pygtkcompatz/This version of gi doesn't include pygtkcompat.r   Nzgi.)ÚsysÚmodulesr	   Úgobjectr7   rA   r9   )r   rH   r   r   r   Útest_compatibilityLayerº   s   
z/PygtkCompatibilityTests.test_compatibilityLayerN)rA   rB   rC   rD   rI   r   r   r   r   rE   ´   s    rE   )rD   rF   Úunittestr   Úgi.repositoryr   Útwisted.internetr   Ú
_gireactorÚImportErrorr   Ú_gtk3reactorr   Útwisted.internet.errorr   Ú#twisted.internet.test.reactormixinsr   Útwisted.trial.unittestr	   r
   Úskipr   rE   r   r   r   r   Ú<module>   s4   þÿ 