B
    ˦Z^)                 @   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   F/work/yifan.wang/lscsoft/opt/python37/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	rtjd|||d ntd|| |rd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<
