WEFT_OS/tests/frame/check_frame.py
Marco Allegretti ae176aae93
test: verify native pointer and keyboard input through weft-compositor
Nothing checked that input reaches a page through the compositor; the
Servo hosts' input forwarding had only been compiled.

tests/frame/check_input.py runs weft-servo-shell with
tests/frame/input.html on the nested desktop and injects real X input
with xdotool into weft-compositor's window. The compositor's winit backend
routes it through its seat to the Wayland client, which has no X display.
The page must react in its presented pixels: clicks 6 px either side of
the boundary between its halves change only the half under the pointer,
and the a and Shift+a key presses arrive with the right key value and
modifier. The harness gives the compositor's X window input focus, as a
window manager would; the client's keyboard focus comes from the
compositor's click-to-focus.

The nested desktop setup shared with check_frame.py moves to
tests/frame/session.py. Xvfb now reports the display it picked through
-displayfd, so concurrent runs cannot share an X server, and a run clears
only the logs and capture it writes.

Validation:
- check_input.py: 3 of 3 runs pass; two concurrent runs pass
- negative pages fail: no script; left click handled by the right half;
  an element extending the left half 20 px across the boundary; a key
  handler ignoring Shift
- check_frame.py --host system, --host app, --host app --slow-style 3 pass
  after the refactor
- unverified: key release and repeat, pointer motion and button release
  events, wheel, focus loss, non-US layouts, non-ASCII text, scale changes,
  weft-app-shell input
2026-10-09 01:18:53 +00:00

221 lines
8.9 KiB
Python

"""Check that a Servo host paints and presents a known page through weft-compositor.
The harness starts Xvfb, runs weft-compositor nested on it with the winit
backend, launches a Servo host as a Wayland client showing `reference.html`,
captures the X screen and checks the presented pixels: four solid quadrants
(red, green, blue, white) in the page's arrangement and dark text pixels
inside the white quadrant. It exits non-zero when the frame never appears or
does not match.
`--host system` runs weft-servo-shell with the page as its system UI.
`--host app` installs the page as a package in a temporary app store and runs
weft-app-shell for it. The host must print READY, and the page must be on
screen within a short grace period after READY, because the compositor shows
a presented frame slightly later. READY printed within that grace before the
page is presented is therefore not detected. `--slow-style SECONDS` makes
early readiness observable: the page stays hidden until a stylesheet that a
local HTTP server delivers after the delay reveals it, so a host that reports
READY before the document has loaded shows no page within the grace period.
Requires Xvfb, xwd and ImageMagick's convert; see session.py.
"""
import argparse
import http.server
import shutil
import sys
import threading
import time
from pathlib import Path
sys.dont_write_bytecode = True
from session import ROOT, Desktop, SessionError, bounding_box, missing_tools, parse_ppm # noqa: E402
HERE = Path(__file__).resolve().parent
COLORS = {
"red": (255, 0, 0),
"green": (0, 255, 0),
"blue": (0, 0, 255),
"white": (255, 255, 255),
}
MIN_SIDE = 40
MIN_TEXT_PIXELS = 30
def dark_pixels(width, pixels, box):
x0, y0, x1, y1 = box
count = 0
for y in range(y0, y1 + 1):
for x in range(x0, x1 + 1):
offset = (y * width + x) * 3
if max(pixels[offset:offset + 3]) < 96:
count += 1
return count
def evaluate(width, height, pixels):
"""Return a list of problems with the captured frame; empty means it matches."""
boxes = {name: bounding_box(width, height, pixels, color) for name, color in COLORS.items()}
problems = [f"no {name} region" for name, box in boxes.items() if box is None]
if problems:
return problems
for name, (x0, y0, x1, y1) in boxes.items():
if x1 - x0 < MIN_SIDE or y1 - y0 < MIN_SIDE:
problems.append(f"{name} region too small: {x1 - x0 + 1}x{y1 - y0 + 1}")
red, green, blue, white = (boxes[n] for n in ("red", "green", "blue", "white"))
if not green[0] > red[2]:
problems.append("green is not right of red")
if not blue[1] > red[3]:
problems.append("blue is not below red")
if not (white[0] > blue[2] and white[1] > green[3]):
problems.append("white is not below green and right of blue")
if abs(red[1] - green[1]) > 4 or abs(red[0] - blue[0]) > 4:
problems.append("quadrants are not aligned")
text = dark_pixels(width, pixels, white)
if text < MIN_TEXT_PIXELS:
problems.append(f"no text in white region ({text} dark pixels)")
return problems
APP_ID = "org.weft.test.frame"
COMPOSITE_GRACE = 2.0
def install_app(store, page):
"""Install `page` as the UI entry of a minimal package and return the store root."""
package = store / APP_ID
(package / "ui").mkdir(parents=True)
shutil.copyfile(page, package / "ui" / "index.html")
(package / "wapp.toml").write_text(
"[package]\n"
f'id = "{APP_ID}"\n'
'name = "Frame reference"\n'
'version = "0.0.0"\n'
"\n[runtime]\n"
'module = "app.wasm"\n'
"\n[ui]\n"
'entry = "ui/index.html"\n',
encoding="utf-8",
)
return store
def slow_style_server(delay):
"""Serve a stylesheet that reveals the page after `delay` seconds."""
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
time.sleep(delay)
body = b"body { visibility: visible; }"
self.send_response(200)
self.send_header("Content-Type", "text/css")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)
threading.Thread(target=server.serve_forever, daemon=True).start()
return server
def hidden_until_styled(page, destination, port):
"""Copy `page`, hiding its body until the delayed stylesheet loads."""
html = page.read_text(encoding="utf-8")
link = (f'<style>body {{ visibility: hidden; }}</style>\n'
f'<link rel="stylesheet" href="http://127.0.0.1:{port}/reveal.css">\n</head>')
if "</head>" not in html:
raise ValueError(f"{page} has no </head>")
destination.write_text(html.replace("</head>", link, 1), encoding="utf-8")
return destination
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--compositor", type=Path,
default=ROOT / "target/debug/weft-compositor")
parser.add_argument("--host", choices=("system", "app"), default="system")
parser.add_argument("--shell", type=Path,
help="host binary (default: target/debug/weft-servo-shell or "
"target/debug/weft-app-shell)")
parser.add_argument("--page", type=Path, default=HERE / "reference.html")
parser.add_argument("--slow-style", type=float, metavar="SECONDS",
help="hide the page until a stylesheet delayed by SECONDS loads")
parser.add_argument("--timeout", type=float, default=120.0,
help="seconds to wait for a matching frame")
parser.add_argument("--output", type=Path, default=ROOT / "target/frame-check",
help="directory for logs and the captured screenshot")
args = parser.parse_args(argv)
if args.shell is None:
name = "weft-servo-shell" if args.host == "system" else "weft-app-shell"
args.shell = ROOT / "target/debug" / name
missing = missing_tools() + [str(b) for b in (args.compositor, args.shell) if not b.is_file()]
if missing:
print("missing: " + ", ".join(missing), file=sys.stderr)
return 2
style_server = None
try:
with Desktop(args.compositor, args.output, outputs=["frame.ppm"]) as desktop:
page = args.page
if args.slow_style:
style_server = slow_style_server(args.slow_style)
page = hidden_until_styled(args.page, desktop.runtime / "page.html",
style_server.server_address[1])
if args.host == "system":
shell = desktop.launch_client(
"shell", [args.shell], {"WEFT_SYSTEM_UI_HTML": str(page.resolve())})
else:
store = install_app(desktop.runtime / "store", page)
shell = desktop.launch_client(
"shell", [args.shell, APP_ID, "1"], {"WEFT_APP_STORE": str(store)})
problems, screenshot = watch(desktop, shell, args)
except SessionError as error:
print(error, file=sys.stderr)
return 1
finally:
if style_server is not None:
style_server.shutdown()
if screenshot is not None:
(args.output / "frame.ppm").write_bytes(screenshot)
if problems:
print("frame check failed: " + "; ".join(problems), file=sys.stderr)
print(f"logs and last capture in {args.output}", file=sys.stderr)
return 1
print(f"frame check passed; capture in {args.output / 'frame.ppm'}")
return 0
def watch(desktop, shell, args):
"""Capture until the frame matches, the host exits or the time runs out."""
def ready_printed():
return any(line.strip() == "READY" for line in desktop.log_text("shell").splitlines())
problems = ["no frame captured"]
screenshot = None
ready_at = None
deadline = time.monotonic() + args.timeout
while time.monotonic() < deadline:
if shell.poll() is not None:
return [f"shell exited with status {shell.returncode}"], screenshot
if args.host == "app" and ready_at is None and ready_printed():
ready_at = time.monotonic()
screenshot = desktop.capture()
problems = evaluate(*parse_ppm(screenshot))
if ready_at is not None and problems and time.monotonic() - ready_at > COMPOSITE_GRACE:
return [f"page not on screen {COMPOSITE_GRACE:g} s after READY"] + problems, screenshot
if not problems and (args.host == "system" or ready_at is not None):
return [], screenshot
if not problems:
problems = ["frame presented but READY not printed"]
time.sleep(1)
return problems, screenshot
if __name__ == "__main__":
raise SystemExit(main())