mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 22:38:59 +00:00
126 lines
4.8 KiB
Python
126 lines
4.8 KiB
Python
import json
|
|
import os
|
|
from pathlib import Path
|
|
import plistlib
|
|
import secrets
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
|
|
app = Path(sys.argv[1])
|
|
sender = sys.argv[2]
|
|
tag = os.environ["RELEASE_TAG"]
|
|
with (app / "Contents/Info.plist").open("rb") as info:
|
|
exe = app / "Contents/MacOS" / plistlib.load(info)["CFBundleExecutable"]
|
|
with socket.socket() as available:
|
|
available.bind(("127.0.0.1", 0))
|
|
port = available.getsockname()[1]
|
|
token = secrets.token_hex(32)
|
|
root = tempfile.mkdtemp()
|
|
env = dict(os.environ, VECTORCRAFT_CONFIG_DIR=root, VECTORCRAFT_CONTROL_TOKEN=token)
|
|
p = subprocess.Popen([str(exe), "--control", str(port), "--automation-read-root", root, "--automation-write-root", root], env=env)
|
|
try:
|
|
for _ in range(100):
|
|
try:
|
|
sock = socket.create_connection(("127.0.0.1", port), timeout=3)
|
|
break
|
|
except OSError:
|
|
if p.poll() is not None:
|
|
raise RuntimeError("Application closed during startup")
|
|
time.sleep(.4)
|
|
else:
|
|
raise RuntimeError("Timed out connecting to app")
|
|
sock.settimeout(25)
|
|
pipe = sock.makefile("rwb")
|
|
def call(method, params):
|
|
pipe.write((json.dumps(dict(id=1, method=method, params=params))+"\n").encode())
|
|
pipe.flush()
|
|
line=pipe.readline()
|
|
obj=json.loads(line)
|
|
if not obj.get("ok"):
|
|
raise RuntimeError((method,obj.get("error")))
|
|
return obj["result"]
|
|
def engine(command, **params):
|
|
return call("engine.execute", dict(command=command,params=params))
|
|
def send(mode):
|
|
subprocess.run([sender,str(p.pid),mode],check=True)
|
|
time.sleep(.6)
|
|
if tag != "v0.24.1":
|
|
call("auth",dict(token=token))
|
|
engine("file.new",width=600,height=450)
|
|
time.sleep(1)
|
|
state=call("ui.inspect",{})
|
|
x,y,w,h=state["canvasRect"]
|
|
call("ui.click",dict(x=x+w/2,y=y+h/2))
|
|
engine("tool.select",tool="selection")
|
|
send("tool")
|
|
tool=call("ui.inspect",{})["tool"]
|
|
print("Control key M selected:",tool,flush=True)
|
|
if tool != "rectangle":
|
|
raise RuntimeError("OS key input did not reach the app; no Undo result can be inferred")
|
|
call("ui.drag", dict(x=x+w*.35,y=y+h*.36,toX=x+w*.55,toY=y+h*.59,steps=9))
|
|
time.sleep(.5)
|
|
doc=engine("document.inspect")
|
|
children=[child for layer in doc["layers"] for child in layer.get("children",[])]
|
|
assert len(children)==1, ("GUI gesture did not draw exactly one rectangle",doc)
|
|
made=children[0]["id"]
|
|
def exists():
|
|
doc=engine("document.inspect")
|
|
return any(child["id"]==made for layer in doc["layers"] for child in layer.get("children",[]))
|
|
assert exists()
|
|
send("undo")
|
|
undone = not exists()
|
|
send("redo")
|
|
restored=exists()
|
|
print("Signed app",tag,"OS Cmd+Z removed rectangle:",undone,"Cmd+Shift+Z restored:",restored,flush=True)
|
|
assert undone and restored
|
|
if tag == "v0.25.1":
|
|
engine("tool.select",tool="selection")
|
|
item=engine("text.create",x=60,y=120,text="Validate font selection",font="Arial")
|
|
engine("select.set",ids=[item["id"]])
|
|
time.sleep(.5)
|
|
try:
|
|
combo=call("ui.find",dict(name="Font Family",index=0))
|
|
except RuntimeError:
|
|
combo=None
|
|
if combo:
|
|
x,y,w,h=combo["rect"]
|
|
call("ui.click",dict(x=x+w-7,y=y+h/2))
|
|
else:
|
|
print("Opening the Character panel font menu using Type > Highlight Font Name",flush=True)
|
|
engine("type.highlightFontName")
|
|
time.sleep(.5)
|
|
families=engine("text.fontList")["families"]
|
|
window_height=call("ui.inspect",{})["window"][1]
|
|
def visible_rows():
|
|
found=[]
|
|
for family in families[:55]:
|
|
try:
|
|
row=call("ui.find",dict(name=family,role="TreeItem",index=0))
|
|
except RuntimeError:
|
|
continue
|
|
x,y,w,h=row["rect"]
|
|
if w>30 and h>8 and 0 < y+h < window_height:
|
|
found.append(row)
|
|
if len(found)==5:
|
|
break
|
|
return found
|
|
rows=visible_rows()
|
|
print("Mac font rows visible at once:",[(row["name"],row["rect"]) for row in rows],flush=True)
|
|
assert len({row["rect"][1] for row in rows}) >= 4, "Font list is still cramped"
|
|
row=next(r for r in rows if r["name"]!="Arial")
|
|
x,y,w,h=row["rect"]
|
|
at=dict(x=x+w/2,y=y+h/2)
|
|
call("ui.move",at)
|
|
time.sleep(.3)
|
|
call("ui.click",at)
|
|
call("ui.move",dict(x=20,y=100))
|
|
time.sleep(.5)
|
|
fonts=engine("document.info",category="fonts")["fonts"]
|
|
print("Clicked font",row["name"],"persisted in the document as",fonts,flush=True)
|
|
assert any(f.startswith(row["name"]) for f in fonts)
|
|
finally:
|
|
p.terminate()
|