Пример #1
0
    def generate_region_by_markers(top_left_marker_img=None,
                                   bottom_right_marker_img=None):
        """Generate a region starting from 2 markers.

        :param top_left_marker_img: Top left pattern used to generate the region.
        :param bottom_right_marker_img: Bottom right pattern used to generate the region.
        :return: Screen region generated.
        """
        try:
            wait(top_left_marker_img, 10)
            exists(bottom_right_marker_img, 10)
        except FindError:
            raise FindError("Unable to find page markers.")

        top_left_pos = find(top_left_marker_img)
        bottom_right_pos = find(bottom_right_marker_img)

        marker_width, marker_height = bottom_right_marker_img.get_size()

        return Region(
            top_left_pos.x,
            top_left_pos.y,
            (bottom_right_pos.x + marker_width),
            bottom_right_pos.y - top_left_pos.y + marker_height,
        )
Пример #2
0
    def find(self, ps=None):
        """Look for a single match of a Pattern or image.

        :param ps: Pattern or String.
        :return: Call the find() method.
        """
        return find(ps, self._area)
Пример #3
0
    def create_region_from_patterns(
        top=None,
        bottom=None,
        left=None,
        right=None,
        padding_top=None,
        padding_bottom=None,
        padding_left=None,
        padding_right=None,
    ):
        """Returns a region created from combined area of one or more patterns. Argument names are just for convenience
        and don't influence outcome.

        :param top: Top pattern used to generate the region.
        :param bottom: Bottom pattern used to generate the region.
        :param left: Left pattern used to generate the region.
        :param right: Right pattern used to generate the region.
        :param padding_top: Padding to be added to the pattern's top.
        :param padding_bottom: Padding to be added to the pattern's bottom.
        :param padding_left: Padding to be added to the pattern's left.
        :param padding_right: Padding to be added to the pattern's right.
        :return: region created from combined area of one or more patterns.
        """

        patterns = []
        if top:
            patterns.append(top)
        if bottom:
            patterns.append(bottom)
        if left:
            patterns.append(left)
        if right:
            patterns.append(right)

        if len(patterns) == 0:
            raise ValueError("One or more patterns required.")

        logger.debug("Creating region from %s pattern(s)." % len(patterns))

        a, b = (Screen().width, Screen().height)
        p1 = Location(a, b)
        p2 = Location(0, 0)

        for pattern in patterns:
            if exists(pattern, 5):
                current_pattern = find(pattern)
                if current_pattern.x < p1.x:
                    p1.x = current_pattern.x
                if current_pattern.y < p1.y:
                    p1.y = current_pattern.y

                w, h = pattern.get_size()

                if current_pattern.x + w > p2.x:
                    p2.x = current_pattern.x + w
                if current_pattern.y + h > p2.y:
                    p2.y = current_pattern.y + h
            else:
                raise FindError("Pattern not found: %s " % pattern)

        found_region = Region(p1.x, p1.y, p2.x - p1.x, p2.y - p1.y)

        if padding_top or padding_bottom or padding_left or padding_right:
            logger.debug("Adding padding to region.")

        if padding_top:
            found_region.y -= padding_top
            found_region.height += padding_top

        if padding_bottom:
            found_region.height += padding_bottom

        if padding_left:
            found_region.x -= padding_left
            found_region.width += padding_left

        if padding_right:
            found_region.width += padding_right

        return found_region
Пример #4
0
def cancel_in_progress_downloads_from_the_library(private_window=False):
    # Open the 'Show Downloads' window and cancel all 'in progress' downloads.
    global cancel_downloads
    if private_window:
        steps = show_all_downloads_from_library_menu_private_window()
        logger.debug('Creating a region for Private Library window.')
        try:
            find_back_button = find(NavBar.BACK_BUTTON)
        except FindError:
            raise FindError('Could not get the coordinates of the nav bar back button.')

        try:
            find_hamburger_menu = find(NavBar.HAMBURGER_MENU)
        except FindError:
            raise FindError('Could not get the coordinates of the hamburger menu.')

        region = Region(find_back_button.x - 10, find_back_button.y,
                        Screen.SCREEN_WIDTH, Screen.SCREEN_HEIGHT)
    else:
        steps = open_show_all_downloads_window_from_library_menu()
        logger.debug('Creating a region for Non-private Library window.')
        expected = exists(Library.TITLE, 10)
        assert expected is True, 'Library successfully opened.'

        try:
            find_library = find(Library.TITLE)
        except FindError:
            raise FindError('Could not get the x-coordinate of the library window title.')

        try:
            find_clear_downloads = find(Library.CLEAR_DOWNLOADS)
        except FindError:
            raise FindError('Could not get the x-coordinate of the clear_downloads button.')

        clear_downloads_width, clear_downloads_height = Library.CLEAR_DOWNLOADS.get_size()
        region = Region(find_library.x - 10, find_library.y,
                        (find_clear_downloads.x + clear_downloads_width + 20) - find_library.x, 500)

    # Cancel all 'in progress' downloads.
    expected = region.exists(DownloadManager.DownloadsPanel.DOWNLOAD_CANCEL, 5)
    expected_highlighted = region.exists(DownloadManager.DownloadsPanel.DOWNLOAD_CANCEL_HIGHLIGHTED)
    if expected or expected_highlighted:
        steps.append(Step(expected, 'The Cancel Download button is displayed properly.'))
        cancel_downloads = True
        expected_cancel = True
    else:
        steps.append(Step(True, 'There are no downloads to be cancelled.'))
        cancel_downloads = False

    cancel_pattern = DownloadManager.DownloadsPanel.DOWNLOAD_CANCEL if expected \
        else DownloadManager.DownloadsPanel.DOWNLOAD_CANCEL_HIGHLIGHTED

    if cancel_downloads:
        while expected_cancel:
            expected_cancel = region.exists(cancel_pattern, 10)
            if expected_cancel:
                click(cancel_pattern)
                if not private_window:
                    hover(Library.TITLE)
        steps.append(Step(True, 'All downloads were cancelled.'))

    if private_window:
        close_tab()
    else:
        click_window_control('close')

    return steps