"""Exercise the packaged GUI and save/reload entirely inside a temporary project.""" import json from pathlib import Path import tempfile import traceback import time import tkinter as tk from PIL import Image from obstacle_range_tool.main import RangeEditor def run(report_path): report_path = Path(report_path).resolve() report = {"ok": False} root = None try: with tempfile.TemporaryDirectory(prefix="obstacle_range_test_") as temporary: project = Path(temporary).resolve() configs = project / "game/frame/obstacle/test" images = project / "res/img/mapElem/test" configs.mkdir(parents=True) images.mkdir(parents=True) Image.new("RGBA", (200, 150), (80, 160, 110, 255)).save(images / "test.png") data = {"TYPE": "test", "WIDTH": 2, "HEIGHT": 1, "STAND": {"IMG": ["test.png", "test.png"], "CX": [-30, -40], "CY": [-70, -80]}, "KEEP": {"value": "原有配置 } [ 不变", "number": 1.25}} path = configs / "test.json" original = b"\xef\xbb\xbf" + (json.dumps(data, ensure_ascii=False, indent=4).replace("\n", "\r\n") + "\r\n").encode("utf-8") path.write_bytes(original) root = tk.Tk() app = RangeEditor(root) root.geometry("1180x760+5000+5000") app.open_path(path) root.update() assert app.canvas.winfo_width() > 400 assert app.listbox.winfo_width() > 150 assert app.document.project_root == project def event_point(point): x, y = app.world_to_canvas(*point) return dict(x=round(x-app.canvas.canvasx(0)), y=round(y-app.canvas.canvasy(0))) def drag(start, end): app.canvas.event_generate("", **event_point(start)) app.canvas.event_generate("", state=256, **event_point(end)) app.canvas.event_generate("", **event_point(end)) root.update() drag((-20, -40), (100, 80)) assert app.ranges == [dict(x1=-20, y1=-40, x2=100, y2=80)], app.ranges app.mode.set("移动 / 调整") drag((40, 20), (60, 50)) assert app.ranges == [dict(x1=0, y1=-10, x2=120, y2=110)], app.ranges drag((120, 110), (160, 150)) assert app.ranges[0] == dict(x1=0, y1=-10, x2=160, y2=150), app.ranges app.undo() assert app.ranges[0]["x2"] == 120 app.redo() assert app.ranges[0]["x2"] == 160 app.zoom.set("200%") app.change_zoom() root.update() app.mode.set("新建区域") drag((-40, -80), (-10, -20)) assert app.ranges[1] == dict(x1=-40, y1=-80, x2=-10, y2=-20), app.ranges app.frame_box.current(1) app.change_frame() assert app.frame["cx"] == -40 and len(app.ranges) == 2 app.simulate.set(True) app.pointer = (-30, -50) app.refresh_preview() assert app.preview_alpha == 255 app.preview_transition.advance(127, time.monotonic()*1000 + 180) app.refresh_preview() assert app.preview_alpha == 127 app.commit_ranges([dict(x1=-40, y1=-80, x2=40, y2=0)], 0) preview = app.preview_image() # Frame CY=-80, lowest y2=0 -> cutoff row 80. assert preview.getpixel((0, 79))[3] == 127 assert preview.getpixel((0, 80))[3] == 255 assert preview.crop((0, 80, 200, 150)).tobytes() == app.source.crop((0, 80, 200, 150)).tobytes() assert app.source.getpixel((0, 79))[3] == 255 app.pointer = (500, 500) app.refresh_preview() assert app.preview_alpha == 127 app.preview_transition.advance(255, time.monotonic()*1000 + 180) app.refresh_preview() assert app.preview_alpha == 255 app.copy_ranges() assert json.loads("{" + root.clipboard_get() + "}")["TRANSPARENT_RANGE"] == app.ranges app.top_update.set(True) assert app.is_dirty() app.save(silent=True) saved = path.read_bytes() assert saved.startswith(b"\xef\xbb\xbf") assert b"\r\n" in saved backups = list(configs.glob("test.json.*.bak")) assert len(backups) == 1 and backups[0].read_bytes() == original expected = dict(data, TRANSPARENT_RANGE=app.ranges, TOP_UPDATE=True) assert json.loads(saved.decode("utf-8-sig")) == expected app.open_path(path) assert app.ranges == expected["TRANSPARENT_RANGE"] assert app.top_update.get() and not app.is_dirty() app.clear_ranges() app.save(silent=True) assert json.loads(path.read_text(encoding="utf-8-sig"))["TRANSPARENT_RANGE"] == [] assert b'"KEEP": {\r\n "value": ' in path.read_bytes() report.update(ok=True, checks=["GUI layout", "negative pixel coordinates", "drag create/move/resize", "undo/redo", "zoom mapping", "frame CX/CY", "smooth alpha preview 127/255", "upper fades/base pixels preserved", "clipboard JSON", "save and reload", "TOP_UPDATE checkbox and persistence", "backup exact bytes", "preserve BOM/CRLF/other fields", "clear ranges"], frozen=bool(getattr(__import__("sys"), "frozen", False))) except Exception: report["error"] = traceback.format_exc() raise finally: if root is not None: root.destroy() report_path.parent.mkdir(parents=True, exist_ok=True) report_path.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")