o
    gb*                     @   s\  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 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re  dS dS )!    N)mock)support)import_helper)	os_helperzhttp://www.example.comtestc                   @   s   e Zd Zdd ZdddZdS )	PopenMockc                 C      dS Nr    selfr
   r
   J/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_webbrowser.pypoll      zPopenMock.pollNc                 C   r   r	   r
   )r   Zsecondsr
   r
   r   wait   r   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|||i | tjjd d }| 	|d t |
d |D ]}	| |	| |
||	 q2| 	|| 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   Z	call_argsassertEqualpopassertInindex)
r   methr   r   options	argumentspopenbrowserZ
popen_argsZoptionr
   r
   r   _test   s   
zCommandTestMixin._testN)r   r   r   URLr'   r
   r
   r
   r   r      s    r   c                   @      e Zd ZejZdd ZdS )GenericBrowserCommandTestc                 C      | j dg tgd d S Nopenr#   r$   r'   r(   r   r
   r
   r   	test_open7      
z#GenericBrowserCommandTest.test_openN)r   r   r   
webbrowserGenericBrowserr   r0   r
   r
   r
   r   r*   3       r*   c                   @   r)   )BackgroundBrowserCommandTestc                 C   r+   r,   r/   r   r
   r
   r   r0   A   r1   z&BackgroundBrowserCommandTest.test_openN)r   r   r   r2   ZBackgroundBrowserr   r0   r
   r
   r
   r   r5   =   r4   r5   c                   @   2   e Zd ZejZdd Zdd Zdd Zdd Z	d	S )
ChromeCommandTestc                 C   r+   r,   r/   r   r
   r
   r   r0   K   r1   zChromeCommandTest.test_openc                 C      | j dtddg tgd d S Nr-   FZ	autoraiser   r#   r$   r'   dictr(   r   r
   r
   r   test_open_with_autoraise_falseP      
z0ChromeCommandTest.test_open_with_autoraise_falsec                 C      | j ddgtgd d S Nopen_newz--new-windowr.   r/   r   r
   r
   r   test_open_newU      
zChromeCommandTest.test_open_newc                 C   r+   Nopen_new_tabr.   r/   r   r
   r
   r   test_open_new_tabZ   r1   z#ChromeCommandTest.test_open_new_tabN)
r   r   r   r2   ZChromer   r0   r>   rC   rG   r
   r
   r
   r   r7   G       r7   c                   @   r6   )
MozillaCommandTestc                 C   r+   r,   r/   r   r
   r
   r   r0   d   r1   zMozillaCommandTest.test_openc                 C   r8   r9   r<   r   r
   r
   r   r>   i   r?   z1MozillaCommandTest.test_open_with_autoraise_falsec                 C      | j dg dtgd d S )NrB   z-new-windowr.   r/   r   r
   r
   r   rC   n      
z MozillaCommandTest.test_open_newc                 C   rJ   )NrF   z-new-tabr.   r/   r   r
   r
   r   rG   s   rK   z$MozillaCommandTest.test_open_new_tabN)
r   r   r   r2   ZMozillar   r0   r>   rC   rG   r
   r
   r
   r   rI   `   rH   rI   c                   @   r6   )
NetscapeCommandTestc                 C       | j dddgdtgd d S )Nr-   -raise-remoteopenURL({})r.   r'   formatr(   r   r
   r
   r   r0   }      

zNetscapeCommandTest.test_openc                 C   s(   | j dtddddgdtgd d S )Nr-   Fr:   -noraiserO   rP   r;   )r'   r=   rR   r(   r   r
   r
   r   r>      s   

z2NetscapeCommandTest.test_open_with_autoraise_falsec                 C   rM   )NrB   rN   rO   openURL({},new-window)r.   rQ   r   r
   r
   r   rC      rS   z!NetscapeCommandTest.test_open_newc                 C   rM   )NrF   rN   rO   openURL({},new-tab)r.   rQ   r   r
   r
   r   rG      rS   z%NetscapeCommandTest.test_open_new_tabN)
r   r   r   r2   ZNetscaper   r0   r>   rC   rG   r
   r
   r
   r   rL   y   rH   rL   c                   @   r6   )
GaleonCommandTestc                 C   r@   )Nr-   -nr.   r/   r   r
   r
   r   r0      rD   zGaleonCommandTest.test_openc                 C   s"   | j dtddddgtgd d S )Nr-   Fr:   rT   rX   r;   r<   r   r
   r
   r   r>      s   
z0GaleonCommandTest.test_open_with_autoraise_falsec                 C   r@   )NrB   -wr.   r/   r   r
   r
   r   rC      rD   zGaleonCommandTest.test_open_newc                 C   r@   )NrF   rY   r.   r/   r   r
   r
   r   rG      rD   z#GaleonCommandTest.test_open_new_tabN)
r   r   r   r2   ZGaleonr   r0   r>   rC   rG   r
   r
   r
   r   rW      rH   rW   c                   @   r6   )
OperaCommandTestc                 C   r+   r,   r/   r   r
   r
   r   r0      r1   zOperaCommandTest.test_openc                 C   r8   r9   r<   r   r
   r
   r   r>      r?   z/OperaCommandTest.test_open_with_autoraise_falsec                 C   r@   rA   r/   r   r
   r
   r   rC      rD   zOperaCommandTest.test_open_newc                 C   r+   rE   r/   r   r
   r
   r   rG      r1   z"OperaCommandTest.test_open_new_tabN)
r   r   r   r2   ZOperar   r0   r>   rC   rG   r
   r
   r
   r   rZ      rH   rZ   c                   @   r6   )
ELinksCommandTestc                 C      | j ddgdtgd d S Nr-   rO   rP   r.   rQ   r   r
   r
   r   r0      s   


zELinksCommandTest.test_openc                 C   r\   r]   rQ   r   r
   r
   r   r>         

z0ELinksCommandTest.test_open_with_autoraise_falsec                 C   r\   )NrB   rO   rU   r.   rQ   r   r
   r
   r   rC      r^   zELinksCommandTest.test_open_newc                 C   r\   )NrF   rO   rV   r.   rQ   r   r
   r
   r   rG      r^   z#ELinksCommandTest.test_open_new_tabN)
r   r   r   r2   ZElinksr   r0   r>   rC   rG   r
   r
   r
   r   r[      s    r[   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 r   )r2   	_tryorder_saved_tryorder	_browsers_saved_browsersr   r
   r
   r   setUp   s   
zBrowserRegistrationTest.setUpc                 C   s   | j t_| jt_d S r   )ra   r2   r`   rc   rb   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 urGtjd|||d ntd|| |rUddg}nddg}||g|d< |  tj| |  tj| d S )Nc                   @      e Zd ZdS )zCBrowserRegistrationTest._check_registration.<locals>.ExampleBrowserNr   r   r   r
   r
   r
   r   ExampleBrowser       rh   Example1example1Zexample2	preferred)r   r2   r`   rb   register)r   rm   rh   Zexpected_tryorderZexpected_browsersinstancer
   r
   r   _check_registration   s,   

z+BrowserRegistrationTest._check_registrationc                 C      | j dd d S )NFrl   rp   r   r
   r
   r   test_register     z%BrowserRegistrationTest.test_registerc                 C   s   | j d d d S )Nrl   rr   r   r
   r
   r   test_register_default
  rt   z-BrowserRegistrationTest.test_register_defaultc                 C   rq   )NTrl   rr   r   r
   r
   r   test_register_preferred  rt   z/BrowserRegistrationTest.test_register_preferredN)	r   r   r   rd   re   rp   rs   ru   rv   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 )Nr2   c                   @   rf   )z0ImportTest.test_register.<locals>.ExampleBrowserNrg   r
   r
   r
   r   rh     ri   rh   rj   rk   )
r   import_fresh_moduleassertIsNoner`   assertFalserb   rn   Z
assertTruer   r    )r   r2   rh   r
   r
   r   rs     s   
zImportTest.test_registerc                 C   sh   t d}| |j | |j | |j |d W d    n1 s'w   Y  | 	|j d S )Nr2   Zfakebrowser)
r   ry   rz   r`   r{   rb   ZassertRaisesErrorgetZassertIsNotNone)r   r2   r
   r
   r   test_get   s   
zImportTest.test_getc                 C   s@   t d}tjtj }||d |	| |
tj d S )Nr2   )r   ry   ospathbasenamesys
executablelowerrn   r3   r}   )r   r2   r   r
   r
   r   test_synthesize)  s   
zImportTest.test_synthesizec              
   C   s   t d}z| j}W n |jtfy( } z| t| W Y d }~nd }~ww t	 }||d< t d}|  W d    d S 1 sFw   Y  d S )Nr2   BROWSER)
r   ry   r}   r   r|   AttributeErrorskipTeststrr   EnvironmentVarGuard)r   r2   r&   errenvr
   r
   r   test_environment/  s   



"zImportTest.test_environmentc              
   C   s  t d}z|  ||jd j}W n |jttfy1 } z| t	| W Y d }~nd }~ww t
 }||d< t d}| | j| W d    n1 sSw   Y  t
 }tj|d< t d}| | jtj W d    d S 1 s|w   Y  d S )Nr2   rx   r   )r   ry   r}   r`   r   r|   r   
IndexErrorr   r   r   r   r   r   r   )r   r2   Zleast_preferred_browserr   r   r
   r
   r   test_environment_preferred:  s$   





"z%ImportTest.test_environment_preferredN)r   r   r   rs   r~   r   r   r   r
   r
   r
   r   rw     s    	rw   __main__)r2   Zunittestr   r   r   r   r   r   Ztest.supportr   r   r(   r   Z	MagicMockr   r   ZTestCaser*   r5   r7   rI   rL   rW   rZ   r[   r_   rw   r   mainr
   r
   r
   r   <module>   s4    	

5<