| 1 | """격리한 Xvfb에서 상태별 실제 창을 촬영함.""" |
| 2 | |
| 3 | import argparse |
| 4 | import json |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import subprocess |
| 8 | import time |
| 9 | |
| 10 | ROOT = Path(__file__).resolve().parents[2] |
| 11 | |
| 12 | |
| 13 | def command(args, environment, **kwargs): |
| 14 | return subprocess.run(args, env=environment, check=True, **kwargs) |
| 15 | |
| 16 | |
| 17 | def stop(process): |
| 18 | process.terminate() |
| 19 | try: |
| 20 | process.wait(timeout=3) |
| 21 | except subprocess.TimeoutExpired: |
| 22 | process.kill() |
| 23 | process.wait() |
| 24 | |
| 25 | |
| 26 | def capture(binary, variant, state, output, environment, width, height): |
| 27 | automatic = ROOT / "tests/ui-redesign/runs/fixtures/여행" |
| 28 | try: |
| 29 | automatic.rmdir() |
| 30 | except OSError: |
| 31 | pass |
| 32 | mode = "opened" if state in {"picker", "selected-empty"} else state |
| 33 | log = ROOT / "tests/ui-redesign/runs" / f"{variant}-{state}.log" |
| 34 | with log.open("w") as stream: |
| 35 | app = subprocess.Popen([str(binary), mode], cwd=ROOT, env=environment, stdout=stream, stderr=stream) |
| 36 | try: |
| 37 | deadline = time.monotonic() + 15 |
| 38 | window = None |
| 39 | while time.monotonic() < deadline and app.poll() is None: |
| 40 | found = subprocess.run( |
| 41 | ["xdotool", "search", "--onlyvisible", "--name", "^Zipelo UI capture"], |
| 42 | env=environment, capture_output=True, text=True, |
| 43 | ) |
| 44 | if found.returncode == 0: |
| 45 | window = found.stdout.splitlines()[0] |
| 46 | break |
| 47 | time.sleep(0.1) |
| 48 | if window is None: |
| 49 | raise RuntimeError(f"창 열기 실패: {log}") |
| 50 | command(["xdotool", "windowsize", window, str(width), str(height)], environment) |
| 51 | command(["xdotool", "windowfocus", "--sync", window], environment) |
| 52 | time.sleep(0.7) |
| 53 | |
| 54 | def click(x, y): |
| 55 | command(["xdotool", "mousemove", "--window", window, str(x), str(y), "click", "1"], environment) |
| 56 | time.sleep(0.4) |
| 57 | |
| 58 | if state == "create" or (state == "create-filled" and variant == "before"): |
| 59 | click(180 if variant == "before" else width - 90, 18 if variant == "before" else 40) |
| 60 | if state == "create-filled" and variant == "before": |
| 61 | base = ROOT / "tests/ui-redesign/runs/fixtures" |
| 62 | click(100, 105) |
| 63 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", |
| 64 | str(ROOT / "tests/ui-redesign/README.md")], environment) |
| 65 | command(["xdotool", "key", "Return"], environment) |
| 66 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", str(base)], environment) |
| 67 | click(100, 200) |
| 68 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", str(base / "archive.zip")], environment) |
| 69 | elif state == "picker": |
| 70 | click(97 if variant == "before" else width - 90, 18 if variant == "before" else height - 80) |
| 71 | elif state == "selected-empty": |
| 72 | click(16 if variant == "before" else 24, 126 if variant == "before" else height - 36) |
| 73 | elif state == "cancelling": |
| 74 | click(260 if variant == "before" else width - 60, 18 if variant == "before" else height - 36) |
| 75 | if state == "password-create" and variant == "after": |
| 76 | click(width // 2 - 56, height // 2 - 12) |
| 77 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", "fixture-password"], environment) |
| 78 | command(["xdotool", "key", "Tab", "Tab"], environment) |
| 79 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", "different"], environment) |
| 80 | command(["xdotool", "mousemove", "--window", window, "2", "2"], environment) |
| 81 | time.sleep(0.3) |
| 82 | destination = output / f"{state}.png" |
| 83 | command(["import", "-window", window, str(destination)], environment) |
| 84 | return {"state": state, "path": str(destination.relative_to(ROOT)), "width": width, "height": height} |
| 85 | finally: |
| 86 | stop(app) |
| 87 | |
| 88 | |
| 89 | def main(): |
| 90 | parser = argparse.ArgumentParser(description=__doc__) |
| 91 | parser.add_argument("--binary", type=Path, required=True) |
| 92 | parser.add_argument("--variant", choices=["before", "after"], required=True) |
| 93 | parser.add_argument("--states", nargs="+", default=["empty", "hover", "opened", "selected-empty", "picker", "create", "create-filled", "password", "password-retry", "password-create", "progress", "progress-unknown", "cancelling", "success", "error"]) |
| 94 | parser.add_argument("--width", type=int, default=1060) |
| 95 | parser.add_argument("--height", type=int, default=720) |
| 96 | parser.add_argument("--output", type=Path) |
| 97 | args = parser.parse_args() |
| 98 | output = (args.output or ROOT / "tests/ui-redesign/screenshots" / args.variant).resolve() |
| 99 | if not output.is_relative_to(ROOT / "tests"): |
| 100 | raise ValueError("촬영 결과는 tests/ 안에만 저장함") |
| 101 | output.mkdir(parents=True, exist_ok=True) |
| 102 | read_fd, write_fd = os.pipe() |
| 103 | server = subprocess.Popen( |
| 104 | ["Xvfb", "-displayfd", str(write_fd), "-screen", "0", "1280x900x24", "-nolisten", "tcp"], |
| 105 | pass_fds=(write_fd,), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, |
| 106 | ) |
| 107 | os.close(write_fd) |
| 108 | try: |
| 109 | with os.fdopen(read_fd) as stream: |
| 110 | display = stream.readline().strip() |
| 111 | if not display: |
| 112 | raise RuntimeError("Xvfb를 시작하지 못함") |
| 113 | environment = dict(os.environ, DISPLAY=f":{display}", LIBGL_ALWAYS_SOFTWARE="1", WINIT_UNIX_BACKEND="x11", XDG_SESSION_TYPE="x11") |
| 114 | environment.pop("WAYLAND_DISPLAY", None) |
| 115 | results = [ |
| 116 | capture(args.binary.resolve(), args.variant, state, output, environment, args.width, args.height) |
| 117 | for state in args.states |
| 118 | ] |
| 119 | metadata = output / "capture.json" |
| 120 | existing = json.loads(metadata.read_text()) if metadata.exists() else [] |
| 121 | merged = {row["state"]: row for row in existing} |
| 122 | merged.update({row["state"]: row for row in results}) |
| 123 | metadata.write_text(json.dumps(list(merged.values()), ensure_ascii=False, indent=4) + "\n") |
| 124 | print(json.dumps(results, ensure_ascii=False)) |
| 125 | finally: |
| 126 | stop(server) |
| 127 | |
| 128 | |
| 129 | if __name__ == "__main__": |
| 130 | main() |