U
    
W[R#                     @   sX  d Z ddlmZmZmZ ddlZddlZzddlmZ ddl	m
Z
 W n ek
r`   dZdZY n8X zddlmZ W n ek
r   dZY nX ddl	mZ ddlmZ dd	lmZ dd
lmZ ddl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" edkr"dZ#G dd deeZ$G dd deZ%G dd deZ&dS )z
GI/GTK3 reactor tests.
    )divisionabsolute_importprint_functionN)	gireactor)Gio)gtk3reactor)Gtk)FilePath)platform)Deferred)ReactorAlreadyRunning)ProcessProtocol)TestCaseSkipTest)ReactorBuilder)SetAsideModule)IReactorProcess)_PY3zgtk3/gi not importablec                   @   sV   e Zd ZdZdd Zdd Zdd Zedkr2d	e_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)Zwidgetr   r   r   r   r   activateA   s    
z:GApplicationRegistrationTests.runReactor.<locals>.activater    r   r   N)hasattrr   ZconnectZholdregisterGApplicationr   
runReactorassertEqual)selfappr   r    r   r   r   r#   5   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ZuseGtk!com.twistedmatrix.trial.gireactorZapplication_idflagsN)	r   	GIReactor
addCleanupunbuildReactorr   ApplicationApplicationFlags
FLAGS_NONEr#   r%   r   r&   r   r   r   test_gApplicationActivateQ   s    z7GApplicationRegistrationTests.test_gApplicationActivatec                 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   ZGtk3Reactorr,   r-   r   r.   r   r/   r0   r#   r1   r   r   r   test_gtkApplicationActivate^   s    z9GApplicationRegistrationTests.test_gtkApplicationActivateNz>Gtk unavailable (may require running with X11 DISPLAY env set)c                 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   ZPortableGIReactorr,   r-   r   r.   r/   r0   assertRaisesNotImplementedErrorr"   r1   r   r   r   test_portableo   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-   objectr4   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.)r4   r   r"   r$   r:   r   )r<   r&   r   r%   r   r   tryRegister   s     zLGApplicationRegistrationTests.test_cantRegisterAfterRun.<locals>.tryRegisterr   N)r   r+   r,   r-   r   r.   r/   r0   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/   r0   r"   r4   r8   r$   r:   )r%   r   r&   Zapp2r<   r   r   r   test_cantRegisterTwice   s$    
 z4GApplicationRegistrationTests.test_cantRegisterTwice)__name__
__module____qualname____doc__r#   r2   r3   r   skipr6   r=   r@   rA   r   r   r   r   r   ,   s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )PygtkCompatibilityTestsz_
    pygtk imports are either prevented, or a compatibility layer is used if
    possible.
    c                    s   t rtdddlm} t|s*tdt  G  fdddt}tt	
dj}ttj }|j| |||gdd	tjid
  | jd  S )z
        If no compatibility layer is present, imports of gobject and friends
        are disallowed.

        We do this by running a process where we make sure gi.pygtkcompat
        isn't present.
        z+Python3 always has the compatibility layer.r   )r   z-No process support available in this reactor.c                       s,   e Zd ZdZdd Zdd Z fddZdS )	zAPygtkCompatibilityTests.test_noCompatibilityLayer.<locals>.Stdout    c                 S   s   t | d S N)print)r%   errr   r   r   errReceived   s    zMPygtkCompatibilityTests.test_noCompatibilityLayer.<locals>.Stdout.errReceivedc                 S   s   |  j |7  _ d S rI   )data)r%   rM   r   r   r   outReceived   s    zMPygtkCompatibilityTests.test_noCompatibilityLayer.<locals>.Stdout.outReceivedc                    s     | j d S rI   )callbackrM   )r%   reasonr   r   r   processExited   s    zOPygtkCompatibilityTests.test_noCompatibilityLayer.<locals>.Stdout.processExitedN)rB   rC   rD   rM   rL   rN   rR   r   rQ   r   r   Stdout   s   rS   s   process_gireactornocompat.py
PYTHONPATH:)envs   success)r   r   twisted.internetr   r   Z
providedByr   r   r	   __file__Zsiblingpathsys
executableZ_asBytesPathZspawnProcessjoinZaddCallbackr$   )r%   r   rS   rY   ZpyExer   rQ   r   test_noCompatibilityLayer   s    
z1PygtkCompatibilityTests.test_noCompatibilityLayerc                 C   s0   dt jk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.)rZ   modulesr   gobjectr9   rB   r;   )r%   r_   r   r   r   test_compatibilityLayer   s    
z/PygtkCompatibilityTests.test_compatibilityLayerN)rB   rC   rD   rE   r]   r`   r   r   r   r   rG      s   &rG   c                   @   s2   e Zd ZdZdd Ze dks(e r.de_dS )Gtk3ReactorTestsz#
    Tests for L{gtk3reactor}.
    c              	   C   sh   t jdd}|dk	r0| t jjd| t jd= td& | ttd}| 	|j
d d W 5 Q R X dS )zs
        On X11, L{gtk3reactor} is unimportable if the C{DISPLAY} environment
        variable is not set.
        ZDISPLAYNztwisted.internet.gtk3reactorr   z=Gtk3 requires X11, and no DISPLAY environment variable is set)osenvirongetr,   __setitem__r   r4   ImportError
__import__r$   r:   )r%   Zdisplayr<   r   r   r   test_requiresDISPLAY   s    
 z%Gtk3ReactorTests.test_requiresDISPLAYposixz)This test is only relevant when using X11N)	rB   rC   rD   rE   rh   r
   ZgetTypeZisMacOSXrF   r   r   r   r   ra      s   ra   )'rE   Z
__future__r   r   r   rZ   rb   rW   r   Zgi.repositoryr   rf   r   r   Ztwisted.python.filepathr	   Ztwisted.python.runtimer
   Ztwisted.internet.deferr   Ztwisted.internet.errorr   Ztwisted.internet.protocolr   Ztwisted.trial.unittestr   r   Z#twisted.internet.test.reactormixinsr   Ztwisted.test.test_twistedr   Ztwisted.internet.interfacesr   Ztwisted.python.compatr   rF   r   rG   ra   r   r   r   r   <module>   s:   


 	7