U
    n5{iµ  ã                   @  s6  d Z ddlmZ ddlmZ zddlmZ W n ek
rH   dZdZ	Y nxX dZddl
mZ dd	lmZmZ dd
lmZ dddœdd„Zd dd„ e dd¡ d¡D ƒ¡ZedƒZedk	r¼eneZ	ddlmZ ddlmZ ddlmZmZ esðdZe	dkpþe	dkZde e	ƒ› �Z!e�s ddlm"Z" G dd„ deeƒZ#dS )z\
GObject Introspection reactor tests; i.e. `gireactor` module for gio/glib/gtk
integration.
é    )Úannotations)ÚskipIf)ÚGioFNT)Úenviron)Úget_required_versionÚrequire_version)Ú	gireactorÚstr)ÚsomeVersionÚreturnc              
   C  sF   zt d| ƒ W n. tk
r< } zt|ƒ W Y ¢S d }~X Y nX dS d S )NÚGtkÚ )r   Ú
ValueErrorr	   )r
   Úve© r   úH/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/internet/test/test_gireactor.pyÚrequireEach   s
    r   z, c                 c  s   | ]}t |ƒV  qd S )N)r   )Ú.0Úversionr   r   r   Ú	<genexpr>"   s   ÿr   ZTWISTED_TEST_GTK_VERSIONz4.0,3.0ú,r   )ÚReactorAlreadyRunning)ÚReactorBuilder)ÚSkipTestÚTestCasez0GObject Introspection `gi` module not importable)z3.0z4.0zUnknown GTK version: )r   c                   @  s˜   e Zd ZdZddddœdd„Zddœd	d
„Zeeeƒddœdd„ƒZ	eeeƒddœdd„ƒZ
ddœdd„Zddœdd„Zddœdd„Zddœ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.
    zGio.Applicationzgireactor.GIReactorÚNone)ÚappÚreactorr   c                   s€   t |dƒstdƒ‚g ‰ddœ‡ ‡fdd„‰d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.r   ©r   c                     s   ˆ  d¡ ˆ  ¡  d S )NÚstopped)ÚappendÚstopr   )r   Úresultr   r   r#   R   s    
z6GApplicationRegistrationTests.runReactor.<locals>.stopÚobject)Úwidgetr   c                   s   ˆ  d¡ ˆ  dˆ¡ d S )NÚ	activatedr   )r"   Ú	callLater)r&   ©r   r$   r#   r   r   ÚactivateV   s    
z:GApplicationRegistrationTests.runReactor.<locals>.activater*   r'   r!   N)Úhasattrr   ÚconnectZholdÚregisterGApplicationr   Ú
runReactorÚassertEqual)Úselfr   r   r*   r   r)   r   r.   D   s    	

z(GApplicationRegistrationTests.runReactorr    c                 C  s4   dd„ | _ |  ¡ }tjdtjjd�}|  ||¡ dS )zR
        L{Gio.Application} instances can be registered with a gireactor.
        c                   S  s   t jdd�S ©NF)ZuseGtk©r   Ú	GIReactorr   r   r   r   Ú<lambda>j   ó    zIGApplicationRegistrationTests.test_gApplicationActivate.<locals>.<lambda>ú!com.twistedmatrix.trial.gireactor©Zapplication_idÚflagsN)ÚreactorFactoryÚbuildReactorr   ÚApplicationÚApplicationFlagsÚ
FLAGS_NONEr.   ©r0   r   r   r   r   r   Útest_gApplicationActivatef   s    
þz7GApplicationRegistrationTests.test_gApplicationActivatec                 C  sn   ddl m}m}m} |  |tj¡ |  |tj¡ |  |tj¡ |  ¡ }|  	t
|ƒd¡ |  d|d d ¡ dS )z‡
        L{twisted.internet.gtk3reactor} is now just a set of compatibility
        aliases for L{twisted.internet.GIReactor}.
        r   )ÚGtk3ReactorÚPortableGtk3ReactorÚinstallé   z+twisted.internet.gtk3reactor was deprecatedÚmessageN)Ztwisted.internet.gtk3reactorr@   rA   rB   ZassertIsr   r3   ÚPortableGIReactorZflushWarningsr/   ÚlenZassertIn)r0   r@   rA   rB   Úwarningsr   r   r   Útest_gtkAliasess   s     
ÿz-GApplicationRegistrationTests.test_gtkAliasesc                 C  s2   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.gtk3reactorr7   N)
r   r3   r9   r:   r   r;   r   r<   r=   r.   r>   r   r   r   Útest_gtkApplicationActivateˆ   s    þz9GApplicationRegistrationTests.test_gtkApplicationActivatec                 C  s6   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.
        r6   r7   N)r   rE   r9   r:   r   r;   r<   r=   ÚassertRaisesÚNotImplementedErrorr-   r>   r   r   r   Útest_portable•   s    þz+GApplicationRegistrationTests.test_portablec                 C  sB   dd„ | _ |  ¡ }tƒ }|  t|j|¡}|  |jd  d¡¡ dS )zp
        Older versions of PyGObject lack C{Application.quit}, and so won't
        allow registration.
        c                   S  s   t jdd�S r1   r2   r   r   r   r   r4   §   r5   z;GApplicationRegistrationTests.test_noQuit.<locals>.<lambda>r   zApplication registration is notN)	r9   r:   r%   rJ   ÚRuntimeErrorr-   Ú
assertTrueÚargsÚ
startswith)r0   r   r   Úexcr   r   r   Útest_noQuit¢   s
    
z)GApplicationRegistrationTests.test_noQuitc                   sV   dd„ ˆ_ ˆ ¡ ‰tjdtjjd�‰ ddœ‡ ‡‡fdd„}ˆ d	|¡ t ˆˆ¡ d
S )zp
        It is not possible to register a C{Application} after the reactor has
        already started.
        c                   S  s   t jdd�S r1   r2   r   r   r   r   r4   ³   r5   zIGApplicationRegistrationTests.test_cantRegisterAfterRun.<locals>.<lambda>r6   r7   r   r    c                    s.   ˆ  tˆjˆ ¡} ˆ | jd d¡ ˆ ¡  d S )Nr   z5Can't register application after reactor was started.)rJ   r   r-   r/   rO   r#   )rQ   ©r   r   r0   r   r   ÚtryRegisterº   s      ÿ ÿzLGApplicationRegistrationTests.test_cantRegisterAfterRun.<locals>.tryRegisterr   N)	r9   r:   r   r;   r<   r=   r(   r   r.   )r0   rT   r   rS   r   Útest_cantRegisterAfterRun®   s    
þ	z7GApplicationRegistrationTests.test_cantRegisterAfterRunc                 C  sf   dd„ | _ |  ¡ }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}.
        c                   S  s   t jdd�S r1   r2   r   r   r   r   r4   Ê   r5   zFGApplicationRegistrationTests.test_cantRegisterTwice.<locals>.<lambda>r6   r7   z"com.twistedmatrix.trial.gireactor2r   z2Can't register more than one application instance.N)r9   r:   r   r;   r<   r=   r-   rJ   rM   r/   rO   )r0   r   r   Zapp2rQ   r   r   r   Útest_cantRegisterTwiceÆ   s     
þ
þ ÿz4GApplicationRegistrationTests.test_cantRegisterTwiceN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r.   r?   r   Ú	noGtkSkipÚnoGtkMessagerH   rI   rL   rR   rU   rV   r   r   r   r   r   :   s   	"r   )$rZ   Ú
__future__r   Zunittestr   Zgi.repositoryr   ÚImportErrorZ
giImportedZ
gtkVersionÚosr   Zgir   r   Ztwisted.internetr   r   ÚjoinÚgetÚsplitZerrorMessageZactualVersionZtwisted.internet.errorr   Z#twisted.internet.test.reactormixinsr   Ztwisted.trial.unittestr   r   Úskipr[   Úreprr\   r   r   r   r   r   r   Ú<module>   s6   

þ