| 1 | import os |
| 2 | from pathlib import Path |
| 3 | import select |
| 4 | import signal |
| 5 | import subprocess |
| 6 | import time |
| 7 | |
| 8 | |
| 9 | ROOT = Path(__file__).resolve().parents[2] |
| 10 | OUTPUT = ROOT / "tests/rust-app-ui" / ("screens-" + str(time.time_ns())) |
| 11 | |
| 12 | |
| 13 | def main(): |
| 14 | OUTPUT.mkdir(exist_ok=True) |
| 15 | display = subprocess.Popen( |
| 16 | ["Xvfb", "-displayfd", "1", "-screen", "0", "1280x900x24", "-nolisten", "tcp"], |
| 17 | stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True, |
| 18 | ) |
| 19 | app = None |
| 20 | try: |
| 21 | if not select.select([display.stdout], [], [], 15)[0]: |
| 22 | raise RuntimeError("Xvfb 준비 시간 초과") |
| 23 | number = display.stdout.readline().decode().strip() |
| 24 | if not number.isdecimal(): |
| 25 | raise RuntimeError("Xvfb display 응답 오류") |
| 26 | env = {**os.environ, "DISPLAY": ":" + number, "WINIT_UNIX_BACKEND": "x11"} |
| 27 | env.pop("WAYLAND_DISPLAY", None) |
| 28 | with (OUTPUT / "app.stdout").open("wb") as stdout, (OUTPUT / "app.stderr").open("wb") as stderr: |
| 29 | app = subprocess.Popen( |
| 30 | [str(ROOT / "tests/ci/target/debug/zipelo-ui-memory-app"), "--ui-fixture", "/synthetic/example.zip"], |
| 31 | env=env, stdout=stdout, stderr=stderr, start_new_session=True, |
| 32 | ) |
| 33 | deadline = time.monotonic() + 30 |
| 34 | while True: |
| 35 | if app.poll() is not None: |
| 36 | raise RuntimeError(f"화면 조기 종료: {app.returncode}") |
| 37 | window = subprocess.run(["xdotool", "search", "--all", "--pid", str(app.pid), "--name", "Zipelo"], env=env, timeout=5, capture_output=True) |
| 38 | if window.returncode == 0: |
| 39 | break |
| 40 | if time.monotonic() >= deadline: |
| 41 | raise RuntimeError("소유 창 표시 시간 초과") |
| 42 | time.sleep(0.1) |
| 43 | time.sleep(1) |
| 44 | subprocess.run(["import", "-window", "root", str(OUTPUT / "list.png")], env=env, timeout=10, check=True) |
| 45 | subprocess.run(["xdotool", "search", "--pid", str(app.pid), "--name", "Zipelo"], env=env, timeout=10, check=True) |
| 46 | print(str(OUTPUT / "list.png")) |
| 47 | finally: |
| 48 | for process in [app, display]: |
| 49 | if process is not None: |
| 50 | if process.poll() is None: |
| 51 | os.killpg(process.pid, signal.SIGTERM) |
| 52 | try: |
| 53 | process.wait(timeout=5) |
| 54 | except subprocess.TimeoutExpired: |
| 55 | os.killpg(process.pid, signal.SIGKILL) |
| 56 | process.wait(timeout=5) |
| 57 | if display.stdout: |
| 58 | display.stdout.close() |
| 59 | if display.stderr: |
| 60 | display.stderr.close() |
| 61 | |
| 62 | |
| 63 | if __name__ == "__main__": |
| 64 | main() |