| 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 | environment = dict(environment) |
| 33 | if state in {"dialog-unavailable", "create-dialog-unavailable"}: |
| 34 | endpoint = ROOT / "tests/ui-redesign/runs/no-portal-bus" |
| 35 | if endpoint.exists(): |
| 36 | raise RuntimeError("실패 시험용 bus 경로가 이미 존재함") |
| 37 | environment["DBUS_SESSION_BUS_ADDRESS"] = f"unix:path={endpoint}" |
| 38 | environment.pop("DBUS_STARTER_ADDRESS", None) |
| 39 | mode = { |
| 40 | "explorer-selected": "toolbar-folder", |
| 41 | "explorer-marquee": "toolbar-folder", |
| 42 | "picker": "opened", |
| 43 | "selected-empty": "opened", |
| 44 | "dialog-unavailable": "toolbar-root", |
| 45 | "create-dialog-unavailable": "toolbar-create", |
| 46 | }.get(state, state) |
| 47 | log = ROOT / "tests/ui-redesign/runs" / f"{variant}-{state}.log" |
| 48 | with log.open("w") as stream: |
| 49 | app = subprocess.Popen([str(binary), mode], cwd=ROOT, env=environment, stdout=stream, stderr=stream) |
| 50 | try: |
| 51 | deadline = time.monotonic() + 15 |
| 52 | window = None |
| 53 | while time.monotonic() < deadline and app.poll() is None: |
| 54 | found = subprocess.run( |
| 55 | ["xdotool", "search", "--onlyvisible", "--name", "Zipelo"], |
| 56 | env=environment, capture_output=True, text=True, |
| 57 | ) |
| 58 | if found.returncode == 0: |
| 59 | window = found.stdout.splitlines()[0] |
| 60 | break |
| 61 | time.sleep(0.1) |
| 62 | if window is None: |
| 63 | raise RuntimeError(f"창 열기 실패: {log}") |
| 64 | command(["xdotool", "windowsize", window, str(width), str(height)], environment) |
| 65 | command(["xdotool", "windowfocus", "--sync", window], environment) |
| 66 | time.sleep(0.7) |
| 67 | |
| 68 | def click(x, y): |
| 69 | command(["xdotool", "mousemove", "--window", window, str(x), str(y), "click", "1"], environment) |
| 70 | time.sleep(0.4) |
| 71 | |
| 72 | if state == "dialog-unavailable": |
| 73 | click(70, 45) |
| 74 | elif state == "create-dialog-unavailable": |
| 75 | click(width // 2 - 250, height // 2 + 54) |
| 76 | if state == "create" or (state == "create-filled" and variant == "before"): |
| 77 | click(180 if variant == "before" else width - 90, 18 if variant == "before" else 40) |
| 78 | if state == "create-filled" and variant == "before": |
| 79 | base = ROOT / "tests/ui-redesign/runs/fixtures" |
| 80 | click(100, 105) |
| 81 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", |
| 82 | str(ROOT / "tests/ui-redesign/README.md")], environment) |
| 83 | command(["xdotool", "key", "Return"], environment) |
| 84 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", str(base)], environment) |
| 85 | click(100, 200) |
| 86 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", str(base / "archive.zip")], environment) |
| 87 | elif state == "picker": |
| 88 | click(97 if variant == "before" else width - 90, 18 if variant == "before" else height - 80) |
| 89 | elif state == "selected-empty": |
| 90 | click(16 if variant == "before" else 24, 126 if variant == "before" else height - 36) |
| 91 | elif state == "cancelling": |
| 92 | click(260 if variant == "before" else width - 60, 18 if variant == "before" else height - 36) |
| 93 | if state == "password-create" and variant == "after": |
| 94 | click(width // 2 - 56, height // 2 - 12) |
| 95 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", "fixture-password"], environment) |
| 96 | command(["xdotool", "key", "Tab", "Tab"], environment) |
| 97 | command(["xdotool", "type", "--clearmodifiers", "--delay", "0", "different"], environment) |
| 98 | if state == "explorer-selected": |
| 99 | click(150, 235) |
| 100 | command(["xdotool", "keydown", "ctrl"], environment) |
| 101 | click(150, 323) |
| 102 | command(["xdotool", "keyup", "ctrl"], environment) |
| 103 | if state == "explorer-marquee": |
| 104 | command(["xdotool", "mousemove", "--window", window, str(width - 20), str(height - 80), "mousedown", "1"], environment) |
| 105 | time.sleep(0.2) |
| 106 | command(["xdotool", "mousemove", "--window", window, "250", "300"], environment) |
| 107 | else: |
| 108 | command(["xdotool", "mousemove", "--window", window, "2", "2"], environment) |
| 109 | time.sleep(0.3) |
| 110 | destination = output / f"{state}.png" |
| 111 | command(["import", "-window", window, str(destination)], environment) |
| 112 | return {"state": state, "path": str(destination.relative_to(ROOT)), "width": width, "height": height} |
| 113 | finally: |
| 114 | subprocess.run(["xdotool", "mouseup", "1"], env=environment, check=False) |
| 115 | stop(app) |
| 116 | |
| 117 | |
| 118 | def main(): |
| 119 | parser = argparse.ArgumentParser(description=__doc__) |
| 120 | parser.add_argument("--binary", type=Path, required=True) |
| 121 | parser.add_argument("--variant", choices=["before", "after"], required=True) |
| 122 | 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"]) |
| 123 | parser.add_argument("--width", type=int, default=1060) |
| 124 | parser.add_argument("--height", type=int, default=720) |
| 125 | parser.add_argument("--output", type=Path) |
| 126 | args = parser.parse_args() |
| 127 | output = (args.output or ROOT / "tests/ui-redesign/screenshots" / args.variant).resolve() |
| 128 | if not output.is_relative_to(ROOT / "tests"): |
| 129 | raise ValueError("촬영 결과는 tests/ 안에만 저장함") |
| 130 | output.mkdir(parents=True, exist_ok=True) |
| 131 | read_fd, write_fd = os.pipe() |
| 132 | server = subprocess.Popen( |
| 133 | ["Xvfb", "-displayfd", str(write_fd), "-screen", "0", f"{max(1280, args.width)}x{max(900, args.height)}x24", "-nolisten", "tcp"], |
| 134 | pass_fds=(write_fd,), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, |
| 135 | ) |
| 136 | os.close(write_fd) |
| 137 | try: |
| 138 | with os.fdopen(read_fd) as stream: |
| 139 | display = stream.readline().strip() |
| 140 | if not display: |
| 141 | raise RuntimeError("Xvfb를 시작하지 못함") |
| 142 | environment = dict(os.environ, DISPLAY=f":{display}", LIBGL_ALWAYS_SOFTWARE="1", WINIT_UNIX_BACKEND="x11", XDG_SESSION_TYPE="x11") |
| 143 | environment.pop("WAYLAND_DISPLAY", None) |
| 144 | results = [ |
| 145 | capture(args.binary.resolve(), args.variant, state, output, environment, args.width, args.height) |
| 146 | for state in args.states |
| 147 | ] |
| 148 | metadata = output / "capture.json" |
| 149 | existing = json.loads(metadata.read_text()) if metadata.exists() else [] |
| 150 | merged = {row["state"]: row for row in existing} |
| 151 | merged.update({row["state"]: row for row in results}) |
| 152 | metadata.write_text(json.dumps(list(merged.values()), ensure_ascii=False, indent=4) + "\n") |
| 153 | print(json.dumps(results, ensure_ascii=False)) |
| 154 | finally: |
| 155 | stop(server) |
| 156 | |
| 157 | |
| 158 | if __name__ == "__main__": |
| 159 | main() |