113 lines
5.5 KiB
Python
113 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
# Copyright (c) 2026 Storm Dragon
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
"""Replay split dialog redraws through a real PTY and capture automatic SSIP.
|
|
|
|
Run: xvfb-run -a env FENRIR_PRIVATE_X11=1 python3 tests/native_dialogs.py
|
|
No installed I38, dialog, GNU Screen, live audio, or live keyboard is used.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
from native_terminal import KeyboardFixture, SpeechServer, wait_for
|
|
|
|
|
|
def main():
|
|
if not os.environ.get("DISPLAY") or os.environ.get("FENRIR_PRIVATE_X11") != "1":
|
|
raise SystemExit("Use xvfb-run -a env FENRIR_PRIVATE_X11=1 python3 tests/native_dialogs.py")
|
|
repository = Path(__file__).resolve().parents[1]
|
|
fixture = str(repository / "tests/native_terminal.py")
|
|
binary = os.environ.get("FENRIR_TEST_BINARY", str(repository / "fenrir"))
|
|
with tempfile.TemporaryDirectory(prefix="fenrir-dialogs-", dir="/tmp") as directory:
|
|
root = Path(directory)
|
|
(root / "home").mkdir()
|
|
(root / "keyboard").mkdir()
|
|
(root / "keyboard/custom.toml").write_bytes((repository / "config/keyboard/desktop.toml").read_bytes())
|
|
(root / "settings.toml").write_text(
|
|
'version=3\n[presentation.sound]\nenabled=false\n'
|
|
'[presentation.progress]\nenabled=false\n'
|
|
'[reading]\npunctuation_level="none"\nspellcheck=false\n'
|
|
f'[remote]\nsocket_file="{root / "remote.sock"}"\n')
|
|
shell = root / "shell"
|
|
shell.write_text(f'#!{sys.executable}\nimport runpy, sys\nsys.argv=[{fixture!r}, "--fixture-shell"]\nrunpy.run_path({fixture!r}, run_name="__main__")\n')
|
|
shell.chmod(0o700)
|
|
server = SpeechServer(root / "speech.sock")
|
|
keyboard = KeyboardFixture()
|
|
environment = dict(os.environ, FENRIR_TEST_BINARY=binary, FENRIR_FIXTURE_DIR=str(root),
|
|
HOME=str(root / "home"), TMPDIR=str(root), LOGNAME="Username", USER="Username",
|
|
SPEECHD_ADDRESS="unix_socket:" + str(root / "speech.sock"), LC_ALL="C", LANGUAGE="C")
|
|
terminal = subprocess.Popen(["xterm", "-geometry", "80x25", "-e", sys.executable,
|
|
fixture, "--outer", str(root)], env=environment, cwd=root)
|
|
|
|
def spoken(text, start):
|
|
wait_for(lambda: any(text in item for item in server.events[start:]),
|
|
f"automatic speech {text!r}; recent={server.events[-12:]}")
|
|
|
|
def emit_output(data):
|
|
start = len(server.events)
|
|
(root / "prompt-output").write_bytes(data)
|
|
os.kill(int((root / "shell-pid").read_text()), signal.SIGUSR1)
|
|
return start
|
|
|
|
try:
|
|
wait_for(lambda: (root / "started").exists(), "reader startup")
|
|
state = json.loads((root / "started").read_text())
|
|
subprocess.run(["xdotool", "windowfocus", "--sync", state["window"]], check=True)
|
|
spoken("prompt", 0)
|
|
for name, initial, redraw, expected in (
|
|
("saved preferences body",
|
|
b"\x1b[2J\x1b[9;1HI38\x1b[15;1H< OK >\x1b[20;1H",
|
|
b"\x1b[10;1HLoaded saved preferences\x1b[11;1HPreferences file loaded"
|
|
b"\x1b[13;1HMissing or invalid values will be prompted\x1b[15;3H",
|
|
["Loaded saved preferences", "Preferences file loaded", "Missing or invalid values will be prompted"]),
|
|
("last warning line",
|
|
b"\x1b[2J\x1b[1;1HI38\x1b[2;1HI38Lock is not available"
|
|
b"\x1b[3;1HRequires i38lock and a supported reader\x1b[23;1H< OK >\x1b[4;3H",
|
|
b"\x1b[4;1HPlease choose another screen lock method\x1b[23;3H",
|
|
["Please choose another screen lock method"]),
|
|
):
|
|
start = emit_output(initial)
|
|
spoken("OK", start) # The button is captured before the body.
|
|
start = emit_output(redraw)
|
|
for text in expected:
|
|
spoken(text, start)
|
|
spoken("OK", start)
|
|
utterances = [item for item in server.events[start:] if item != "CANCEL"]
|
|
joined = " ".join(utterances)
|
|
for text in expected:
|
|
assert joined.count(text) == 1, (name, text, utterances)
|
|
assert "OK" in utterances[-1], (name, utterances)
|
|
# No review key was used to obtain the missing text.
|
|
assert not (root / "input").exists() or not (root / "input").read_bytes()
|
|
print(f"PASS: {name}: automatic body speech followed by focused button", flush=True)
|
|
keyboard.key("Insert+q")
|
|
wait_for(lambda: (root / "finished").exists(), "reader shutdown")
|
|
result = json.loads((root / "finished").read_text())
|
|
assert result == {"status": 0, "restored": True, "flagsRestored": True}, result
|
|
assert not server.errors, server.errors
|
|
except BaseException:
|
|
print("SSIP:", server.events[-25:], file=sys.stderr)
|
|
for name in ("reader-errors", "timing.log"):
|
|
if (root / name).exists():
|
|
print(name + ":", (root / name).read_text()[-5000:], file=sys.stderr)
|
|
raise
|
|
finally:
|
|
terminal.terminate()
|
|
try:
|
|
terminal.wait(timeout=3)
|
|
except subprocess.TimeoutExpired:
|
|
terminal.kill()
|
|
terminal.wait()
|
|
keyboard.close()
|
|
server.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|