Files
pyflowx/tests/cli/test_screenshot.py
T

140 lines
5.7 KiB
Python

"""Tests for cli.screenshot module."""
from __future__ import annotations
from pathlib import Path
from unittest.mock import MagicMock, patch
from pyflowx.conditions import Constants
from pyflowx.ops.files import screenshot as media
# ---------------------------------------------------------------------- #
# get_screenshot_path
# ---------------------------------------------------------------------- #
class TestGetScreenshotPath:
"""Test get_screenshot_path function."""
def test_get_screenshot_path_with_filename(self, tmp_path: Path) -> None:
"""Should get screenshot path with filename."""
with patch.object(Path, "home", return_value=tmp_path):
result = media.get_screenshot_path("test.png")
assert result.name == "test.png"
def test_get_screenshot_path_without_filename(self, tmp_path: Path) -> None:
"""Should get screenshot path without filename."""
with patch.object(Path, "home", return_value=tmp_path):
result = media.get_screenshot_path()
assert "screenshot_" in result.name
assert result.suffix == ".png"
# ---------------------------------------------------------------------- #
# take_screenshot_full
# ---------------------------------------------------------------------- #
class TestTakeScreenshotFull:
"""Test take_screenshot_full function."""
def test_take_screenshot_full_windows(self, tmp_path: Path) -> None:
"""Should take full screenshot on Windows."""
with (
patch.object(Constants, "IS_WINDOWS", True),
patch.object(Constants, "IS_MACOS", False),
patch.object(Path, "home", return_value=tmp_path),
patch("subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
media.take_screenshot_full()
assert mock_run.called
def test_take_screenshot_full_macos(self, tmp_path: Path) -> None:
"""Should take full screenshot on macOS."""
with (
patch.object(Constants, "IS_WINDOWS", False),
patch.object(Constants, "IS_MACOS", True),
patch.object(Path, "home", return_value=tmp_path),
patch("subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
media.take_screenshot_full()
assert mock_run.called
def test_take_screenshot_full_linux(self, tmp_path: Path) -> None:
"""Should take full screenshot on Linux."""
with (
patch.object(Constants, "IS_WINDOWS", False),
patch.object(Constants, "IS_MACOS", False),
patch.object(Path, "home", return_value=tmp_path),
patch("subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
media.take_screenshot_full()
assert mock_run.called
def test_take_screenshot_full_linux_scrot_fallback(self, tmp_path: Path) -> None:
"""Should fallback to scrot when gnome-screenshot not found."""
with (
patch.object(Constants, "IS_WINDOWS", False),
patch.object(Constants, "IS_MACOS", False),
patch.object(Path, "home", return_value=tmp_path),
patch("subprocess.run") as mock_run,
):
mock_run.side_effect = [FileNotFoundError(), MagicMock(returncode=0)]
media.take_screenshot_full()
assert mock_run.call_count == 2
# ---------------------------------------------------------------------- #
# take_screenshot_area
# ---------------------------------------------------------------------- #
class TestTakeScreenshotArea:
"""Test take_screenshot_area function."""
def test_take_screenshot_area_windows(self, tmp_path: Path) -> None:
"""Should take area screenshot on Windows."""
with (
patch.object(Constants, "IS_WINDOWS", True),
patch.object(Constants, "IS_MACOS", False),
patch.object(Path, "home", return_value=tmp_path),
patch("subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
media.take_screenshot_area()
assert mock_run.called
def test_take_screenshot_area_macos(self, tmp_path: Path) -> None:
"""Should take area screenshot on macOS."""
with (
patch.object(Constants, "IS_WINDOWS", False),
patch.object(Constants, "IS_MACOS", True),
patch.object(Path, "home", return_value=tmp_path),
patch("subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
media.take_screenshot_area()
assert mock_run.called
def test_take_screenshot_area_linux(self, tmp_path: Path) -> None:
"""Should take area screenshot on Linux."""
with (
patch.object(Constants, "IS_WINDOWS", False),
patch.object(Constants, "IS_MACOS", False),
patch.object(Path, "home", return_value=tmp_path),
patch("subprocess.run") as mock_run,
):
mock_run.return_value = MagicMock(returncode=0)
media.take_screenshot_area()
assert mock_run.called
def test_take_screenshot_area_linux_scrot_fallback(self, tmp_path: Path) -> None:
"""Should fallback to scrot -s when gnome-screenshot not found."""
with (
patch.object(Constants, "IS_WINDOWS", False),
patch.object(Constants, "IS_MACOS", False),
patch.object(Path, "home", return_value=tmp_path),
patch("subprocess.run") as mock_run,
):
mock_run.side_effect = [FileNotFoundError(), MagicMock(returncode=0)]
media.take_screenshot_area()
assert mock_run.call_count == 2