B
    18™\â)  ã               @   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   úX/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/test_webbrowser.pyÚpoll   s    zPopenMock.pollNc             C   s   dS )Nr   r   )r   Ú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   ÚpatchÚ
subprocessÚbrowser_classÚCMD_NAMEÚgetattrr   Ú	call_argsÚassertEqualÚpopÚassertInÚindex)
r   Ú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/   r1   r3   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 )Nr0   z-new-window)r   r    )r$   r%   )r   r   r   r   r1   l   s    z MozillaCommandTest.test_open_newc             C   s   | j dg dtgd d S )Nr2   z-new-tab)r   r    )r$   r%   )r   r   r   r   r3   q   s    z$MozillaCommandTest.test_open_new_tabN)
r   r   r   r)   ZMozillar   r(   r/   r1   r3   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 )
Ú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.   r6   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 )Nr0   z-raisez-remotezopenURL({},new-window))r   r    )r$   r6   r%   )r   r   r   r   r1   …   s    z!NetscapeCommandTest.test_open_newc             C   s    | j dddgd t¡gd d S )Nr2   z-raisez-remotezopenURL({},new-tab))r   r    )r$   r6   r%   )r   r   r   r   r3   Š   s    z%NetscapeCommandTest.test_open_new_tabN)
r   r   r   r)   ZNetscaper   r(   r/   r1   r3   r   r   r   r   r5   w   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 )
Ú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 )Nr0   z-w)r   r    )r$   r%   )r   r   r   r   r1   ž   s    zGaleonCommandTest.test_open_newc             C   s   | j ddgtgd d S )Nr2   z-w)r   r    )r$   r%   )r   r   r   r   r3   £   s    z#GaleonCommandTest.test_open_new_tabN)
r   r   r   r)   ZGaleonr   r(   r/   r1   r3   r   r   r   r   r7      s
   r7   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 )Nr0   z--new-window)r   r    )r$   r%   )r   r   r   r   r1   ·   s    zOperaCommandTest.test_open_newc             C   s   | j dg tgd d S )Nr2   )r   r    )r$   r%   )r   r   r   r   r3   ¼   s    z"OperaCommandTest.test_open_new_tabN)
r   r   r   r)   ZOperar   r(   r/   r1   r3   r   r   r   r   r8   ©   s
   r8   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$   r6   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$   r6   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 )Nr0   z-remotezopenURL({},new-window))r   r    )r$   r6   r%   )r   r   r   r   r1   Ï   s    zELinksCommandTest.test_open_newc             C   s   | j ddgd t¡gd d S )Nr2   z-remotezopenURL({},new-tab))r   r    )r$   r6   r%   )r   r   r   r   r3   Ô   s    z#ELinksCommandTest.test_open_new_tabN)
r   r   r   r)   ZElinksr   r(   r/   r1   r3   r   r   r   r   r9   Â   s
   r9   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)r<   r)   r;   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   rA   ÚExample1Úexample1Zexample2)Ú	preferred)r   r)   r;   r=   Úregister)r   rD   rA   Zexpected_tryorderZexpected_browsersÚinstancer   r   r   Ú_check_registrationç   s*    


z+BrowserRegistrationTest._check_registrationc             C   s   | j dd d S )NF)rD   )rG   )r   r   r   r   Útest_register  s    z%BrowserRegistrationTest.test_registerc             C   s   | j d d d S )N)rD   )rG   )r   r   r   r   Útest_register_default  s    z-BrowserRegistrationTest.test_register_defaultc             C   s   | j dd d S )NT)rD   )rG   )r   r   r   r   Útest_register_preferred  s    z/BrowserRegistrationTest.test_register_preferredN)	r   r   r   r?   r@   rG   rH   rI   rJ   r   r   r   r   r:   Ú   s   r:   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   rA     s   rA   rB   éÿÿÿÿrC   )
r   Úimport_fresh_moduleÚassertIsNoner;   ÚassertFalser=   rE   Ú
assertTruer   r   )r   r)   rA   r   r   r   rH     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   rM   rN   r;   rO   r=   ÚassertRaisesÚErrorÚgetÚ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   rM   ÚosÚpathÚbasenameÚsysÚ
executableÚlowerrE   r*   rS   )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   rM   rS   r   rR   Ú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)   rL   r]   )r   rM   rS   r;   r   rR   r^   Ú
IndexErrorr_   r`   ra   r   rY   rZ   )r   r)   Zleast_preferred_browserrb   rc   r   r   r   Útest_environment_preferred8  s    
 




z%ImportTest.test_environment_preferredN)r   r   r   rH   rU   r\   rd   rf   r   r   r   r   rK     s
   	rK   Ú__main__)r)   ÚunittestrV   rY   r   r   r   r   r%   r   Z	MagicMockr   r   ÚTestCaser&   r+   r,   r4   r5   r7   r8   r9   r:   rK   r   Úmainr   r   r   r   Ú<module>   s,   	

5<
