B
    u9aâ)  ã               @   sB  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ dZdZ	G dd„ dej
ƒZG dd	„ d	ƒZG d
d„ deejƒZG dd„ deejƒZG dd„ deejƒZG dd„ deejƒZG dd„ deejƒZG dd„ deejƒZG dd„ deejƒZG dd„ deejƒZG dd„ dejƒZG dd„ dejƒZedkr>e ¡  dS )é    N)Úmock)Úsupportzhttp://www.example.comÚtestc               @   s   e Zd Zdd„ Zddd„ZdS )Ú	PopenMockc             C   s   dS )Nr   © )Úselfr   r   ú%/usr/lib/python3.7/test_webbrowser.pyÚpoll   s    zPopenMock.pollNc             C   s   dS )Nr   r   )r   Zsecondsr   r   r   Úwait   s    zPopenMock.wait)N)Ú__name__Ú
__module__Ú__qualname__r	   r
   r   r   r   r   r      s   r   c               @   s   e Zd Zegi dœdd„ZdS )ÚCommandTestMixin)ÚargsÚkwc      
      C   s–   t ƒ }t | td|¡ | jtd}t||ƒ||Ž tjjd d }|  	|d t¡ | 
d¡ x(|D ] }	|  |	|¡ | 
| |	¡¡ qbW |  	||¡ dS )aj  Given a web browser instance method name along with arguments and
        keywords for same (which defaults to the single argument URL), creates
        a browser instance from the class pointed to by self.browser, calls the
        indicated instance method with the indicated arguments, and compares
        the resulting options and arguments passed to Popen by the browser
        instance against the 'options' and 'args' lists.  Options are compared
        in a position independent fashion, and the arguments are compared in
        sequence order to whatever is left over after removing the options.

        ÚPopen)Únamer   N)r   r   ZpatchÚ
subprocessÚbrowser_classÚCMD_NAMEÚgetattrr   Ú	call_argsÚassertEqualÚpopÚassertInÚindex)
r   Zmethr   r   ÚoptionsÚ	argumentsÚpopenÚbrowserZ
popen_argsÚoptionr   r   r   Ú_test   s    

zCommandTestMixin._testN)r   r   r   ÚURLr!   r   r   r   r   r      s   r   c               @   s   e Zd ZejZdd„ ZdS )ÚGenericBrowserCommandTestc             C   s   | j dg tgd d S )NÚopen)r   r   )r!   r"   )r   r   r   r   Ú	test_open5   s    z#GenericBrowserCommandTest.test_openN)r   r   r   Ú
webbrowserÚGenericBrowserr   r%   r   r   r   r   r#   1   s   r#   c               @   s   e Zd ZejZdd„ ZdS )ÚBackgroundBrowserCommandTestc             C   s   | j dg tgd d S )Nr$   )r   r   )r!   r"   )r   r   r   r   r%   ?   s    z&BackgroundBrowserCommandTest.test_openN)r   r   r   r&   ZBackgroundBrowserr   r%   r   r   r   r   r(   ;   s   r(   c               @   s2   e Zd ZejZdd„ Zdd„ Zdd„ Zdd„ Z	d	S )
ÚChromeCommandTestc             C   s   | j dg tgd d S )Nr$   )r   r   )r!   r"   )r   r   r   r   r%   I   s    zChromeCommandTest.test_openc             C   s   | j dtddg tgd d S )Nr$   F)Ú	autoraise)r   r   r   )r!   Údictr"   )r   r   r   r   Útest_open_with_autoraise_falseN   s    z0ChromeCommandTest.test_open_with_autoraise_falsec             C   s   | j ddgtgd d S )NÚopen_newz--new-window)r   r   )r!   r"   )r   r   r   r   Útest_open_newS   s    zChromeCommandTest.test_open_newc             C   s   | j dg tgd d S )NÚopen_new_tab)r   r   )r!   r"   )r   r   r   r   Útest_open_new_tabX   s    z#ChromeCommandTest.test_open_new_tabN)
r   r   r   r&   ZChromer   r%   r,   r.   r0   r   r   r   r   r)   E   s
   r)   c               @   s2   e Zd ZejZdd„ Zdd„ Zdd„ Zdd„ Z	d	S )
ÚMozillaCommandTestc             C   s   | j dg tgd d S )Nr$   )r   r   )r!   r"   )r   r   r   r   r%   b   s    zMozillaCommandTest.test_openc             C   s   | j dtddg tgd d S )Nr$   F)r*   )r   r   r   )r!   r+   r"   )r   r   r   r   r,   g   s    z1MozillaCommandTest.test_open_with_autoraise_falsec             C   s   | j dg dtgd d S )Nr-   z-new-window)r   r   )r!   r"   )r   r   r   r   r.   l   s    z MozillaCommandTest.test_open_newc             C   s   | j dg dtgd d S )Nr/   z-new-tab)r   r   )r!   r"   )r   r   r   r   r0   q   s    z$MozillaCommandTest.test_open_new_tabN)
r   r   r   r&   ZMozillar   r%   r,   r.   r0   r   r   r   r   r1   ^   s
   r1   c               @   s2   e Zd ZejZdd„ Zdd„ Zdd„ Zdd„ Z	d	S )
ÚNetscapeCommandTestc             C   s    | j dddgd t¡gd d S )Nr$   z-raisez-remotezopenURL({}))r   r   )r!   Úformatr"   )r   r   r   r   r%   {   s    zNetscapeCommandTest.test_openc             C   s(   | j dtddddgd t¡gd d S )Nr$   F)r*   z-noraisez-remotezopenURL({}))r   r   r   )r!   r+   r3   r"   )r   r   r   r   r,   €   s    z2NetscapeCommandTest.test_open_with_autoraise_falsec             C   s    | j dddgd t¡gd d S )Nr-   z-raisez-remotezopenURL({},new-window))r   r   )r!   r3   r"   )r   r   r   r   r.   …   s    z!NetscapeCommandTest.test_open_newc             C   s    | j dddgd t¡gd d S )Nr/   z-raisez-remotezopenURL({},new-tab))r   r   )r!   r3   r"   )r   r   r   r   r0   Š   s    z%NetscapeCommandTest.test_open_new_tabN)
r   r   r   r&   ZNetscaper   r%   r,   r.   r0   r   r   r   r   r2   w   s
   r2   c               @   s2   e Zd ZejZdd„ Zdd„ Zdd„ Zdd„ Z	d	S )
ÚGaleonCommandTestc             C   s   | j ddgtgd d S )Nr$   z-n)r   r   )r!   r"   )r   r   r   r   r%   ”   s    zGaleonCommandTest.test_openc             C   s"   | j dtddddgtgd d S )Nr$   F)r*   z-noraisez-n)r   r   r   )r!   r+   r"   )r   r   r   r   r,   ™   s    z0GaleonCommandTest.test_open_with_autoraise_falsec             C   s   | j ddgtgd d S )Nr-   z-w)r   r   )r!   r"   )r   r   r   r   r.   ž   s    zGaleonCommandTest.test_open_newc             C   s   | j ddgtgd d S )Nr/   z-w)r   r   )r!   r"   )r   r   r   r   r0   £   s    z#GaleonCommandTest.test_open_new_tabN)
r   r   r   r&   ZGaleonr   r%   r,   r.   r0   r   r   r   r   r4      s
   r4   c               @   s2   e Zd ZejZdd„ Zdd„ Zdd„ Zdd„ Z	d	S )
ÚOperaCommandTestc             C   s   | j dg tgd d S )Nr$   )r   r   )r!   r"   )r   r   r   r   r%   ­   s    zOperaCommandTest.test_openc             C   s   | j dtddg tgd d S )Nr$   F)r*   )r   r   r   )r!   r+   r"   )r   r   r   r   r,   ²   s    z/OperaCommandTest.test_open_with_autoraise_falsec             C   s   | j ddgtgd d S )Nr-   z--new-window)r   r   )r!   r"   )r   r   r   r   r.   ·   s    zOperaCommandTest.test_open_newc             C   s   | j dg tgd d S )Nr/   )r   r   )r!   r"   )r   r   r   r   r0   ¼   s    z"OperaCommandTest.test_open_new_tabN)
r   r   r   r&   ZOperar   r%   r,   r.   r0   r   r   r   r   r5   ©   s
   r5   c               @   s2   e Zd ZejZdd„ Zdd„ Zdd„ Zdd„ Z	d	S )
ÚELinksCommandTestc             C   s   | j ddgd t¡gd d S )Nr$   z-remotezopenURL({}))r   r   )r!   r3   r"   )r   r   r   r   r%   Æ   s    
zELinksCommandTest.test_openc             C   s   | j ddgd t¡gd d S )Nr$   z-remotezopenURL({}))r   r   )r!   r3   r"   )r   r   r   r   r,   Ê   s    z0ELinksCommandTest.test_open_with_autoraise_falsec             C   s   | j ddgd t¡gd d S )Nr-   z-remotezopenURL({},new-window))r   r   )r!   r3   r"   )r   r   r   r   r.   Ï   s    zELinksCommandTest.test_open_newc             C   s   | j ddgd t¡gd d S )Nr/   z-remotezopenURL({},new-tab))r   r   )r!   r3   r"   )r   r   r   r   r0   Ô   s    z#ELinksCommandTest.test_open_new_tabN)
r   r   r   r&   ZElinksr   r%   r,   r.   r0   r   r   r   r   r6   Â   s
   r6   c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚBrowserRegistrationTestc             C   s    t j| _g t _t j| _i t _d S )N)r&   Ú	_tryorderÚ_saved_tryorderÚ	_browsersÚ_saved_browsers)r   r   r   r   ÚsetUpÜ   s    zBrowserRegistrationTest.setUpc             C   s   | j t_| jt_d S )N)r9   r&   r8   r;   r:   )r   r   r   r   ÚtearDownã   s    z BrowserRegistrationTest.tearDownc             C   sÞ   G dd„ dƒ}g }i }|   tj|¡ |   tj|¡ t d|¡ dg}|d g|d< |   tj|¡ |   tj|¡ |ƒ }|d k	rŽtjd|||d nt d||¡ |rªddg}nddg}||g|d< |   tj|¡ |   tj|¡ d S )Nc               @   s   e Zd ZdS )zCBrowserRegistrationTest._check_registration.<locals>.ExampleBrowserN)r   r   r   r   r   r   r   ÚExampleBrowserè   s   r>   ÚExample1Úexample1Zexample2)Ú	preferred)r   r&   r8   r:   Úregister)r   rA   r>   Zexpected_tryorderZexpected_browsersÚinstancer   r   r   Ú_check_registrationç   s*    


z+BrowserRegistrationTest._check_registrationc             C   s   | j dd d S )NF)rA   )rD   )r   r   r   r   Útest_register  s    z%BrowserRegistrationTest.test_registerc             C   s   | j d d d S )N)rA   )rD   )r   r   r   r   Útest_register_default  s    z-BrowserRegistrationTest.test_register_defaultc             C   s   | j dd d S )NT)rA   )rD   )r   r   r   r   Útest_register_preferred  s    z/BrowserRegistrationTest.test_register_preferredN)	r   r   r   r<   r=   rD   rE   rF   rG   r   r   r   r   r7   Ú   s   r7   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú
ImportTestc             C   sŽ   t  d¡}|  |j¡ |  |j¡ G dd„ dƒ}| d|¡ |  |j¡ |  |jd d¡ |  |j¡ |  	d|j¡ |  |jd |d g¡ d S )Nr&   c               @   s   e Zd ZdS )z0ImportTest.test_register.<locals>.ExampleBrowserN)r   r   r   r   r   r   r   r>     s   r>   r?   éÿÿÿÿr@   )
r   Úimport_fresh_moduleÚassertIsNoner8   ÚassertFalser:   rB   Z
assertTruer   r   )r   r&   r>   r   r   r   rE     s    
zImportTest.test_registerc          	   C   sT   t  d¡}|  |j¡ |  |j¡ |  |j¡ | d¡ W d Q R X |  	|j¡ d S )Nr&   Zfakebrowser)
r   rJ   rK   r8   rL   r:   ZassertRaisesÚErrorÚgetZassertIsNotNone)r   r&   r   r   r   Útest_get  s    
zImportTest.test_getc             C   s@   t  d¡}tj tj¡ ¡ }| |d | 	|¡¡ | 
tj¡ d S )Nr&   )r   rJ   ÚosÚpathÚbasenameÚsysÚ
executableÚlowerrB   r'   rN   )r   r&   r   r   r   r   Útest_synthesize'  s    
zImportTest.test_synthesizec          
   C   s‚   t  d¡}y| ¡ j}W n6 |jtfk
rN } z|  t|ƒ¡ W d d }~X Y nX t  ¡  }||d< t  d¡}| ¡  W d Q R X d S )Nr&   ÚBROWSER)	r   rJ   rN   r   rM   ÚAttributeErrorÚskipTestÚstrÚEnvironmentVarGuard)r   r&   r   ÚerrÚenvr   r   r   Útest_environment-  s    
 

zImportTest.test_environmentc          
   C   sÚ   t  d¡}y| ¡  | |jd ¡j}W n8 |jttfk
r` } z|  t	|ƒ¡ W d d }~X Y nX t  
¡ *}||d< t  d¡}|  | ¡ j|¡ W d Q R X t  
¡ .}tj|d< t  d¡}|  | ¡ jtj¡ W d Q R X d S )Nr&   rI   rW   )r   rJ   rN   r8   r   rM   rX   Ú
IndexErrorrY   rZ   r[   r   rS   rT   )r   r&   Zleast_preferred_browserr\   r]   r   r   r   Útest_environment_preferred8  s    
 




z%ImportTest.test_environment_preferredN)r   r   r   rE   rO   rV   r^   r`   r   r   r   r   rH     s
   	rH   Ú__main__)r&   ZunittestrP   rS   r   r   r   r   r"   r   Z	MagicMockr   r   ZTestCaser#   r(   r)   r1   r2   r4   r5   r6   r7   rH   r   Úmainr   r   r   r   Ú<module>   s,   	

5<
