135 lines
4.5 KiB
Python
135 lines
4.5 KiB
Python
from __future__ import annotations
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
|
|
PNG_BYTES = b"\x89PNG\r\n\x1a\n" + (b"x" * 2048) + b"IEND"
|
|
|
|
|
|
def test_capture_screenshot_direct_invalid_fallback_png_returns_false(tmp_path):
|
|
from douyin.core.underlying_operations_pkg.screenshot_direct import (
|
|
capture_screenshot_direct,
|
|
)
|
|
|
|
fake_self = SimpleNamespace(
|
|
_adb_path=lambda: "adb",
|
|
get_screenshot=lambda *_a, **_k: False,
|
|
)
|
|
|
|
pic_dir = tmp_path / "pic"
|
|
pic_dir.mkdir()
|
|
|
|
with patch(
|
|
"douyin.core.underlying_operations_pkg.screenshot_direct.path_config.get_pic_dir",
|
|
return_value=str(pic_dir),
|
|
), patch(
|
|
"douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.run",
|
|
return_value=SimpleNamespace(returncode=0),
|
|
), patch(
|
|
"douyin.core.underlying_operations_pkg.screenshot_direct._valid_png",
|
|
return_value=False,
|
|
):
|
|
result = capture_screenshot_direct(fake_self, "MOCK", "pic")
|
|
|
|
assert result is False
|
|
|
|
|
|
def test_capture_screenshot_direct_uses_shell_stdout_fallback_when_execout_and_pull_fail(tmp_path):
|
|
from douyin.core.underlying_operations_pkg.screenshot_direct import (
|
|
capture_screenshot_direct,
|
|
)
|
|
|
|
fake_self = SimpleNamespace(
|
|
_adb_path=lambda: "adb",
|
|
get_screenshot=lambda *_a, **_k: False,
|
|
)
|
|
|
|
pic_dir = tmp_path / "pic"
|
|
pic_dir.mkdir()
|
|
png_bytes = PNG_BYTES
|
|
calls: list[list[str]] = []
|
|
|
|
class FakePopen:
|
|
returncode = 1
|
|
|
|
def __init__(self, cmd, **_kwargs):
|
|
calls.append(list(cmd))
|
|
|
|
def communicate(self, timeout=None): # noqa: ARG002
|
|
return b"", None
|
|
|
|
def fake_run(cmd, **_kwargs):
|
|
calls.append(list(cmd))
|
|
if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and "-p" in cmd and len(cmd) == 6:
|
|
return SimpleNamespace(returncode=0, stdout=png_bytes, stderr=b"")
|
|
raise RuntimeError("file fallback unavailable")
|
|
|
|
with patch(
|
|
"douyin.core.underlying_operations_pkg.screenshot_direct.path_config.get_pic_dir",
|
|
return_value=str(pic_dir),
|
|
), patch(
|
|
"douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.Popen",
|
|
FakePopen,
|
|
), patch(
|
|
"douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.run",
|
|
side_effect=fake_run,
|
|
):
|
|
result = capture_screenshot_direct(fake_self, "MOCK", "pic")
|
|
|
|
assert result is True
|
|
assert (pic_dir / "screenshot.png").exists()
|
|
assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls)
|
|
|
|
|
|
def test_capture_screenshot_direct_tries_stdout_after_invalid_pull_file(tmp_path):
|
|
from douyin.core.underlying_operations_pkg.screenshot_direct import (
|
|
capture_screenshot_direct,
|
|
)
|
|
|
|
fake_self = SimpleNamespace(
|
|
_adb_path=lambda: "adb",
|
|
get_screenshot=lambda *_a, **_k: False,
|
|
)
|
|
|
|
pic_dir = tmp_path / "pic"
|
|
pic_dir.mkdir()
|
|
calls: list[list[str]] = []
|
|
|
|
class FakePopen:
|
|
returncode = 4294967295
|
|
|
|
def __init__(self, cmd, **_kwargs):
|
|
calls.append(list(cmd))
|
|
|
|
def communicate(self, timeout=None): # noqa: ARG002
|
|
return b"broken", None
|
|
|
|
def fake_run(cmd, **kwargs):
|
|
calls.append(list(cmd))
|
|
if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 7:
|
|
return SimpleNamespace(returncode=0, stdout=b"", stderr=b"")
|
|
if cmd[:4] == ["adb", "-s", "MOCK", "pull"]:
|
|
with open(cmd[4], "wb") as f:
|
|
f.write(b"not a png")
|
|
return SimpleNamespace(returncode=0, stdout=b"", stderr=b"")
|
|
if cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6:
|
|
return SimpleNamespace(returncode=0, stdout=PNG_BYTES.replace(b"\r\n", b"\r\r\n"), stderr=b"")
|
|
raise RuntimeError(f"unexpected command: {cmd}")
|
|
|
|
with patch(
|
|
"douyin.core.underlying_operations_pkg.screenshot_direct.path_config.get_pic_dir",
|
|
return_value=str(pic_dir),
|
|
), patch(
|
|
"douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.Popen",
|
|
FakePopen,
|
|
), patch(
|
|
"douyin.core.underlying_operations_pkg.screenshot_direct.subprocess.run",
|
|
side_effect=fake_run,
|
|
):
|
|
result = capture_screenshot_direct(fake_self, "MOCK", "pic")
|
|
|
|
assert result is True
|
|
assert (pic_dir / "screenshot.png").read_bytes().startswith(b"\x89PNG")
|
|
assert any(cmd[:5] == ["adb", "-s", "MOCK", "shell", "screencap"] and len(cmd) == 6 for cmd in calls)
|