Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions tests/unit/test_screenshot_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Unit tests for tests/shared/screenshot_steps.py.

screenshot_steps.py registers two behave @step handlers that wrap
take_screenshot / take_app_screenshot and assert the returned path is
not None. These tests verify both the happy path and the assertion
failure path without requiring a live display or VM.
"""

import sys
import types
from unittest.mock import MagicMock, patch

import pytest


# ---------------------------------------------------------------------------
# Import helper: stub out behave before importing the module under test
# ---------------------------------------------------------------------------

def _import_screenshot_steps():
"""Import screenshot_steps with behave.step replaced by a no-op decorator."""
behave_stub = types.ModuleType("behave")
behave_stub.step = lambda *_args, **_kwargs: (lambda f: f)
sys.modules.setdefault("behave", behave_stub)

# Clear cached module so the stub is picked up on re-import.
for key in list(sys.modules):
if "screenshot_steps" in key and "test_" not in key:
del sys.modules[key]

import tests.shared.screenshot_steps as m # noqa: PLC0415
return m


_mod = _import_screenshot_steps()


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _ctx():
return MagicMock()


# ---------------------------------------------------------------------------
# step_take_screenshot_labeled
# ---------------------------------------------------------------------------

def test_take_screenshot_labeled_success():
"""Step succeeds when take_screenshot returns a path."""
with patch.object(_mod, "take_screenshot", return_value="/tmp/shot.png"):
_mod.step_take_screenshot_labeled(_ctx(), "my-label")


def test_take_screenshot_labeled_passes_label():
"""Step forwards the label argument to take_screenshot."""
with patch.object(_mod, "take_screenshot", return_value="/tmp/shot.png") as mock_ts:
_mod.step_take_screenshot_labeled(_ctx(), "smoke")
mock_ts.assert_called_once_with("smoke")


def test_take_screenshot_labeled_raises_on_none():
"""Step raises AssertionError when take_screenshot returns None."""
with patch.object(_mod, "take_screenshot", return_value=None):
with pytest.raises(AssertionError, match="Screenshot failed for label 'fail-label'"):
_mod.step_take_screenshot_labeled(_ctx(), "fail-label")


# ---------------------------------------------------------------------------
# step_launch_and_screenshot
# ---------------------------------------------------------------------------

def test_launch_and_screenshot_success():
"""Step succeeds when take_app_screenshot returns a path."""
with patch.object(_mod, "take_app_screenshot", return_value="/tmp/app.png"):
_mod.step_launch_and_screenshot(_ctx(), "org.gnome.Calculator")


def test_launch_and_screenshot_passes_app_id():
"""Step forwards the app_id argument to take_app_screenshot."""
with patch.object(_mod, "take_app_screenshot", return_value="/tmp/app.png") as mock_tas:
_mod.step_launch_and_screenshot(_ctx(), "org.gnome.Clocks")
mock_tas.assert_called_once_with("org.gnome.Clocks")


def test_launch_and_screenshot_raises_on_none():
"""Step raises AssertionError when take_app_screenshot returns None."""
with patch.object(_mod, "take_app_screenshot", return_value=None):
with pytest.raises(AssertionError, match="Screenshot failed for app 'org.gnome.BadApp'"):
_mod.step_launch_and_screenshot(_ctx(), "org.gnome.BadApp")
Loading