Files
OrcaSlicer/tools/mcp/test_speed_dial_open.py
T
SoftFever 6cd2ce1a2e Check every area of the MCP sweep with Python tests that read each step back
The sweep's objects, presets, filaments, settings, project files, slicing, gizmos and GUI tools are tested by Python
tests that compare the app's state after every step with the values their entries write out, in place of the
replays, and the existing tests' checks that could not fail now can. The calls these tests found wrong now behave
as described: moving one instance no longer sinks the others, a toolpath export writes the layers shown, an
auto-answered dialog no longer reports its call blocked, and a web dialog that has just opened takes requests.
2026-11-19 13:49:20 +08:00

222 lines
11 KiB
Python

#!/usr/bin/env python3
"""Checks that Space opens the Speed Dial right after a switch to each page with a 3D view, and that KWin draws
no title bar on the dial.
tools/mcp/orca_mcp.py launch [--cad]
tools/mcp/test_speed_dial_open.py
Each page is reached from Home by a click on its tab, as a person switches pages: Prepare first, while the
plater still shows the 3D view it started with, then Design when the Design tab is on, and Preview. The page's
3D view must then hold the keyboard focus, as a focused sidebar control keeps Space for itself, and Space sent
to the focused window must open the dial. The view also takes the focus when the pointer enters it, so before
each tab click the test moves the system pointer to the primary display's top-left corner, (0, 0), and reads
it back there: the seeded main window opens at the work area's top left, so that corner is its top bar or lies
outside it, never in the 3D view below the tab bar. A pointer that does not read back at (0, 0) fails the
page's focus check. On a native Wayland display, where an app can neither move the pointer nor read where it
is, the focus checks are reported SKIP. Where KWin answers on the display's D-Bus (the private one of a
`--headless` launch, else the caller's), the frame KWin reports for the dial must be the dial's own size: a
title bar of KWin's makes the frame taller. Run it on a freshly launched app. Exits 0 when every check passes.
"""
import ctypes
import ctypes.util
import os
import re
import subprocess
import sys
import time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import orca_mcp # noqa: E402
def park_pointer(display):
"""Moves the system pointer to (0, 0), the primary display's top-left corner, and returns where the system then
reads it, as (x, y), or a string saying why it could not be moved or read."""
if os.name == "nt":
from ctypes import wintypes
user32 = ctypes.windll.user32
if not user32.SetCursorPos(0, 0):
return f"SetCursorPos failed ({ctypes.GetLastError()})"
point = wintypes.POINT()
if not user32.GetCursorPos(ctypes.byref(point)):
return f"GetCursorPos failed ({ctypes.GetLastError()})"
return (point.x, point.y)
if sys.platform == "darwin":
class CGPoint(ctypes.Structure):
_fields_ = [("x", ctypes.c_double), ("y", ctypes.c_double)]
graphics = ctypes.CDLL(ctypes.util.find_library("CoreGraphics")
or "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics")
foundation = ctypes.CDLL(ctypes.util.find_library("CoreFoundation"))
graphics.CGWarpMouseCursorPosition.argtypes = [CGPoint]
graphics.CGEventCreate.restype = ctypes.c_void_p
graphics.CGEventCreate.argtypes = [ctypes.c_void_p]
graphics.CGEventGetLocation.restype = CGPoint
graphics.CGEventGetLocation.argtypes = [ctypes.c_void_p]
foundation.CFRelease.argtypes = [ctypes.c_void_p]
if graphics.CGWarpMouseCursorPosition(CGPoint(0, 0)) != 0:
return "CGWarpMouseCursorPosition failed"
event = graphics.CGEventCreate(None)
if not event:
return "CGEventCreate failed"
location = graphics.CGEventGetLocation(event)
foundation.CFRelease(event)
return (round(location.x), round(location.y))
path = ctypes.util.find_library("X11")
if not path or not display:
return "libX11 or the X display is not found"
x = ctypes.CDLL(path)
x.XOpenDisplay.restype = ctypes.c_void_p
x.XOpenDisplay.argtypes = [ctypes.c_char_p]
x.XDefaultRootWindow.restype = ctypes.c_ulong
x.XDefaultRootWindow.argtypes = [ctypes.c_void_p]
x.XWarpPointer.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_int, ctypes.c_int,
ctypes.c_uint, ctypes.c_uint, ctypes.c_int, ctypes.c_int]
x.XSync.argtypes = [ctypes.c_void_p, ctypes.c_int]
ulong, cint, cuint = ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_uint)
x.XQueryPointer.argtypes = [ctypes.c_void_p, ctypes.c_ulong, ulong, ulong, cint, cint, cint, cint, cuint]
x.XCloseDisplay.argtypes = [ctypes.c_void_p]
dpy = x.XOpenDisplay(display.encode())
if not dpy:
return f"the X display {display} does not open"
try:
root = x.XDefaultRootWindow(dpy)
x.XWarpPointer(dpy, 0, root, 0, 0, 0, 0, 0, 0)
x.XSync(dpy, 0)
root_ret, child = ctypes.c_ulong(), ctypes.c_ulong()
rx, ry, wx, wy, mask = ctypes.c_int(), ctypes.c_int(), ctypes.c_int(), ctypes.c_int(), ctypes.c_uint()
if not x.XQueryPointer(dpy, root, ctypes.byref(root_ret), ctypes.byref(child), ctypes.byref(rx), ctypes.byref(ry),
ctypes.byref(wx), ctypes.byref(wy), ctypes.byref(mask)):
return "XQueryPointer found the pointer on another screen"
return (rx.value, ry.value)
finally:
x.XCloseDisplay(dpy)
def kwin_frames(session):
"""[(caption, width, height)] of the app's windows as KWin frames them, or None when no KWin answers."""
env = dict(os.environ)
bus = (session.get("display_env") or {}).get("DBUS_SESSION_BUS_ADDRESS")
if bus:
env["DBUS_SESSION_BUS_ADDRESS"] = bus
def gdbus(path, method, *args):
try:
r = subprocess.run(["gdbus", "call", "--session", "--dest", "org.kde.KWin", "--object-path", path,
"--method", method, *args], env=env, capture_output=True, text=True, timeout=10)
except (OSError, subprocess.TimeoutExpired):
return None
return r.stdout if r.returncode == 0 else None
# KWin's window runner finds windows by caption or application, the dial (which has no caption) by the latter.
found = gdbus("/WindowsRunner", "org.kde.krunner1.Match", "orca")
if found is None:
return None
frames = []
for uuid in re.findall(r"'0_(\{[^}]+\})'", found):
info = gdbus("/KWin", "org.kde.KWin.getWindowInfo", uuid) or ""
pid = re.search(r"'pid': <(\d+)>", info)
if pid and int(pid.group(1)) == session["pid"]:
caption = re.search(r"'caption': <'(.*?)'>", info)
size = [float(re.search(rf"'{k}': <([\d.]+)>", info).group(1)) for k in ("width", "height")]
frames.append((caption.group(1) if caption else "", *size))
return frames
def main():
client, session = orca_mcp.session_client()
checks = []
skipped = []
wayland = client.call("app_state").get("display_backend") == "wayland"
display = session.get("display") or os.environ.get("DISPLAY")
parked = [] # where the pointer read back after each move before a tab click of the current switch
def check(name, ok, detail=""):
checks.append((name, bool(ok), detail))
def park():
"""Moves the pointer off the 3D view, to (0, 0), before a tab click, and keeps where it read back."""
if not wayland:
parked.append(park_pointer(display))
def wait_for(predicate, timeout):
deadline = time.time() + timeout
while time.time() < deadline:
value = predicate()
if value:
return value
time.sleep(0.25)
return predicate()
def tree():
_, texts = client.call_with_text("widget_list", max_nodes=3000)
return "\n".join(texts)
def switch_to(label, tab):
"""Clicks Home's tab (the first, with no label), then the tab `label`, and waits for app_state's tab."""
tabs = tree()
home = re.search(r"\[(w\d+)\] tab \[", tabs)
page = re.search(rf'\[(w\d+)\] tab "{re.escape(label)}"', tabs)
parked.clear()
park()
client.call("widget_click", target=home.group(1))
wait_for(lambda: client.call("app_state")["tab"] == "home", 10)
park()
client.call("widget_click", target=page.group(1))
return wait_for(lambda: client.call("app_state")["tab"] == tab, 10)
def dial():
"""The Speed Dial as window_list gives it: the shown web dialog with no title."""
return next((w for w in client.call("window_list")["items"]
if w.get("kind") == "dialog" and w.get("web") and not w.get("title")), None)
pages = [("Prepare", "prepare")]
if client.call("app_state").get("design_tab"):
pages.append(("Design", "design"))
pages.append(("Preview", "preview"))
frame_checked = False
for label, tab in pages:
check(f"{label}: the tab is shown", switch_to(label, tab))
focused = [line.strip() for line in tree().splitlines() if "[focused]" in line]
if wayland:
skipped.append((f"{label}: its 3D view holds the keyboard focus",
"on a native Wayland display an app can neither move the pointer off the view nor read "
"where it is"))
else:
check(f"{label}: its 3D view holds the keyboard focus",
all(p == (0, 0) for p in parked) and focused and "name=OrcaSceneCanvas" in focused[0],
f"the pointer read back at {parked} after each move to (0, 0); focused: {focused}")
client.call("keyboard_press", keys="Space")
opened = wait_for(dial, 10)
check(f"{label}: Space opens the Speed Dial", opened)
if not opened:
continue
if not frame_checked:
frame_checked = True
frames = kwin_frames(session)
frame = next((f for f in frames or [] if f[0] == ""), None)
if frames is None:
print("skip KWin's frame of the dial: no KWin answers on the display's D-Bus")
elif frame is None:
# On X11 the dial skips the taskbar, and the runner leaves such windows out.
print("skip KWin's frame of the dial: KWin's window runner does not list it")
else:
# A title bar adds a few dozen pixels; the margin covers rounding at a fractional scale.
check("KWin draws no title bar on the dial", frame[2] <= opened["height"] + 2,
f"KWin's frame {frame}, the dial {opened['width']}x{opened['height']}")
client.call("keyboard_press", keys="Escape", target=opened["ref"])
check(f"{label}: Escape closes the Speed Dial", wait_for(lambda: dial() is None, 10))
failed = [c for c in checks if not c[1]]
for name, ok, detail in checks:
print(f"{'ok ' if ok else 'FAIL'} {name}" + ("" if ok or not detail else f"\n {detail}"))
for name, why in skipped:
print(f"SKIP {name}: {why}")
print(f"{len(checks) - len(failed)} of {len(checks)} checks passed" + (f", {len(skipped)} skipped" if skipped else ""))
return 1 if failed else 0
if __name__ == "__main__":
sys.exit(main())