
    ^j]/                     |    d dl Z d dlmZmZmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ  G d d	      Zy)
    N)AnyDictListOptionalTupleUnion)encode_file_to_base64)FlutterFinder)ScrollDirection)	WebDriver)
WebElementc                      e Zd ZdeddfdZ	 d!deeef   dee	   ddfdZ
	 d!deeef   dee	   ddfdZd!d	ed
eeeef      ddfdZd!d	ed
eeeef      ddfdZdededdfdZej$                  fdedededefdZdedefdZdeddfdZ	 	 	 d"dee   dee   dee   deee      fdZdededefdZdeedf   deeeeef   f   fd Zy)#FlutterCommanddriverreturnNc                     || _         y N)r   )selfr   s     t/home/ubuntu/.local/lib/python3.12/site-packages/appium/webdriver/extensions/flutter_integration/flutter_commands.py__init__zFlutterCommand.__init__   s	        locatortimeoutc                 X    | j                  |      }|||d<   | j                  d|       y)a[  
        Waits for a element to become visible.

        Args:
            locator (Union[WebElement, FlutterFinder]): The element to wait for; can be a WebElement or a FlutterFinder.
            timeout (Optional[float]): Maximum wait time in seconds. Defaults to a predefined timeout if not specified.

        Returns:
            None
        Nr   waitForVisible$_FlutterCommand__get_locator_optionsexecute_flutter_commandr   r   r   optss       r   wait_for_visiblezFlutterCommand.wait_for_visible   s4      $99'B%DO$$%5t<r   c                 X    | j                  |      }|||d<   | j                  d|       y)a^  
        Waits for a element to become invisible.

        Args:
            locator (Union[WebElement, FlutterFinder]): The element to wait for; can be a WebElement or a FlutterFinder.
            timeout (Optional[float]): Maximum wait time in seconds. Defaults to a predefined timeout if not specified.

        Returns:
            None:
        Nr   waitForAbsentr   r   s       r   wait_for_invisiblez!FlutterCommand.wait_for_invisible4   s3      $99'B%DO$$_d;r   elementoffsetc                 P    d|i}||d   |d   d|d<   | j                  d|       y)a  
        Performs a double-click on the given element, with an optional offset.

        Args:
            element (WebElement): The element to double-click on. This parameter is required.
            offset (Optional[Tuple[int, int]]): The x and y offsets from the element to click at. If not specified, the click is performed at the element's center.

        Returns:
            None:
        originNr      xyr&   doubleClickr   r   r%   r&   r    s       r   perform_double_clickz#FlutterCommand.perform_double_clickK   s;     ?G=P#)!96!9=DN$$]D9r   c                 P    d|i}||d   |d   d|d<   | j                  d|       y)a  
        Performs a long press on the given element, with an optional offset.

        Args:
            element (WebElement): The element to perform the long press on. This parameter is required.
            offset (Optional[Tuple[int, int]]): The x and y offsets from the element to perform the long press at. If not specified, the long press is performed at the element's center.

        Returns:
            None:
        r(   Nr   r)   r*   r&   	longPressr.   r/   s       r   perform_long_pressz!FlutterCommand.perform_long_press[   s;     ?G=P#)!96!9=DN$$[$7r   sourcetargetc                 .    | j                  d||d       y)a  
        Performs a drag-and-drop operation from a source element to a target element.

        Args:
            source (WebElement): The element to drag from.
            target (WebElement): The element to drop onto.

        Returns:
            None:
        dragAndDrop)r4   r5   Nr.   )r   r4   r5   s      r   perform_drag_and_dropz$FlutterCommand.perform_drag_and_dropk   s     	$$]vQW4XYr   	scroll_toscroll_directionr    c                 j    |j                         |d<   |j                  |d<   | j                  d|      S )a%  
        Scrolls until the specified element becomes visible.

        Args:
            scroll_to (FlutterFinder): The Flutter element to scroll to.
            scroll_direction (ScrollDirection): The direction to scroll up or down. Defaults to `ScrollDirection.DOWN`.

        KeywordArgs:
                scrollView (str): The view of the scroll. Default value is 'Scrollable'
                delta (int): delta for the scroll. Default value is 64
                maxScrolls (int): Max times to scroll. Default value is 15
                settleBetweenScrollsTimeout (float): settle timeout in milliseconds. Default value is 5000
                dragDuration (float): time gap between each scroll in milliseconds. Default value is 100

        Returns:
            Webelement: scrolled element
        finderscrollDirectionscrollTillVisible)to_dictvaluer   )r   r9   r:   r    s       r   scroll_till_visiblez"FlutterCommand.scroll_till_visiblex   s;    . #**,X"2"8"8++,?FFr   r@   c                     t         j                  j                  |      rt        |      }n|}| j	                  dd|i      S )a  
        Injects a mock image to the device. The input can be a file path or a base64-encoded string.

        Args:
            value (str): The file path of the image or a base64-encoded string.

        Returns:
            str: Image ID of the injected image.
        injectImagebase64Image)ospathisfiler	   r   )r   r@   base64_encoded_images      r   inject_mock_imagez FlutterCommand.inject_mock_image   s>     77>>% #8#? #( ++MMK_;`aar   image_idc                 ,    | j                  dd|i       y)z
        Activates an injected image with image ID.

        Args:
            image_id (str): The ID of the injected image to activate.

        Returns:
            None:
        activateInjectedImageimageIdNr.   )r   rJ   s     r   activate_injected_imagez&FlutterCommand.activate_injected_image   s     	$$%<y(>STr   widget_typekeytextc                 T    i }|||d<   |||d<   |||d<   | j                  d|      S )a  
        Returns the render tree of the root widget.

        Args:
            widget_type (Optional[str]): The type of the widget to primary filter by.
            key (Optional[str]): The key of the widget to filter by.
            text (Optional[str]): The text of the widget to filter by.

        Returns:
            List[Optional[Dict]]: A list of dictionaries or None values representing the render tree.

            The result is a nested list of dictionaries representing each widget and its properties,
            such as type, key, size, attribute, state, visual information, and hierarchy.

            The example widget includes the following code, which is rendered as part of the widget tree:

            .. code-block:: dart

                Semantics(
                    key: const Key('add_activity_semantics'),
                    label: 'add_activity_button',
                    button: true,
                    child: FloatingActionButton.small(
                        key: const Key('add_activity_button'),
                        tooltip: 'add_activity_button',
                        heroTag: 'add',
                        backgroundColor: const Color(0xFF2E2E3A),
                        onPressed: null,
                        child: Icon(
                        Icons.add,
                        size: 16,
                        color: Colors.amber.shade200.withOpacity(0.5),
                        semanticLabel: 'Add icon',
                        ),
                    ),
                )

        Example execute command:

            .. code-block:: python

                >>> flutter_command = FlutterCommand(driver)  # noqa
                >>> flutter_command.get_render_tree(widget_type='Semantics', key='add_activity_semantics')
                output >> [
                            {
                                "type": "Semantics",
                                "elementType": "SingleChildRenderObjectElement",
                                "description": "Semantics-[<'add_activity_semantics'>]",
                                "depth": 0,
                                "key": "[<'add_activity_semantics'>]",
                                "attributes": {
                                    "semanticsLabel": "add_activity_button"
                                },
                                "visual": {},
                                "state": {},
                                "rect": {
                                    "x": 0,
                                    "y": 0,
                                    "width": 48,
                                    "height": 48
                                },
                                "children": [
                                    {
                                        "type": "FloatingActionButton",
                                        "elementType": "StatelessElement",
                                        "description": "FloatingActionButton-[<'add_activity_button'>]",
                                        "depth": 1,
                                        "key": "[<'add_activity_button'>]",
                                        "attributes": {},
                                        "visual": {},
                                        "state": {},
                                        "rect": {
                                            "x": 0,
                                            "y": 0,
                                            "width": 48,
                                            "height": 48
                                        },
                                        "children": [
                                            {...},
                                                "children": [...]
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
        
widgetTyperP   rQ   
renderTreer.   )r   rO   rP   rQ   r    s        r   get_render_treezFlutterCommand.get_render_tree   sJ    z "!,D?DKDL++L$??r   
scriptNameparamsc                 @    | j                   j                  d| |      S )a  
        Executes a Flutter command by sending a script and parameters to the flutter integration driver.

        Args:
            scriptName (str): The name of the Flutter command to execute.
                This will be prefixed with 'flutter:' when passed to the driver.
            params (dict): A dictionary of parameters to be passed along with the Flutter command.

        Returns:
            Any: The result of the command execution. The return value depends on the
            specific Flutter command being executed.
        z	flutter: )r   execute_script)r   rV   rW   s      r   r   z&FlutterCommand.execute_flutter_command  s"     {{))Ij\*BFKKr   r
   c                 N    t        |t              rd|iS d|j                         iS )Nr%   r   )
isinstancer   r?   )r   r   s     r   __get_locator_optionsz$FlutterCommand.__get_locator_options%  s)    gz*w''7??,--r   r   )NNN)__name__
__module____qualname__r   r   r   r   r
   r   floatr!   r$   r   intr0   r3   r8   r   DOWNr   rA   strrI   rN   r   r   rU   dictr   r    r   r   r   r      s   y T  $(=z=01= %= 
	=0 $(<z=01< %< 
	<.:J :sTWx@Y :ei : 8* 8huSRUX>W 8cg 8 ZJ Z
 Zt Z  -<,@,@G G *G 	G
 
G6bs bs b 
U 
U 
U &*!"	e@c]e@ c]e@ sm	e@
 
htn	e@NL# Lt L L.U:3N-O .TXY\^cdhjtdt^uYuTv .r   r   )rE   typingr   r   r   r   r   r   appium.common.helperr	   >appium.webdriver.extensions.flutter_integration.flutter_finderr
   Aappium.webdriver.extensions.flutter_integration.scroll_directionsr   appium.webdriver.webdriverr   appium.webdriver.webelementr   r   re   r   r   <module>rl      s+    
 : : 6 X ] 0 2O. O.r   