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    