#!/usr/bin/env python3 """End-to-end check of the helper's browser bridge. Runs inside live_session.sh's private session and display, with the Electron fixture already started in WINEPREFIX. Registers a fake Cthulhu reader on the session bus, starts wine-access.exe, and checks that the helper publishes the Chromium window, that legacy announcements stop for it, that a killed publisher is restarted, and that nothing is left behind when the helper exits. Usage: helper_integration.py BUILD_DIR """ import os import signal import subprocess import sys import threading import time from pathlib import Path import gi gi.require_version("Atspi", "2.0") from gi.repository import Atspi, Gio, GLib # noqa: E402 BUS_NAME = "org.stormux.Cthulhu1.Service" OBJECT_PATH = "/" + BUS_NAME.replace(".", "/") TITLE = "Wine Access Electron Test" failures = 0 presented = [] helperLines = [] def check(name, condition, detail=""): global failures if condition: print(f"PASS: {name}", flush=True) else: failures += 1 print(f"FAIL: {name} {detail}", flush=True) def pump(seconds): context = GLib.MainContext.default() deadline = time.monotonic() + seconds while time.monotonic() < deadline: while context.pending(): context.iteration(False) time.sleep(0.01) def wait_until(predicate, seconds): deadline = time.monotonic() + seconds while time.monotonic() < deadline: if predicate(): return True pump(0.1) return predicate() def register_reader(): xml = '' for index, kind in enumerate(["s"] * 7 + ["i", "i", "s"]): xml += f'' xml += '' for method, inputs in (("SpeakText", "sb"), ("CancelSpeech", "")): xml += '' xml += "".join('' for kind in inputs) xml += '' xml += "" def record(connection, sender, path, interface, method, parameters, invocation): if method == "PresentAccessibleEvent": presented.append(parameters.unpack()) invocation.return_value(GLib.Variant("(b)", (True,))) connection = Gio.bus_get_sync(Gio.BusType.SESSION, None) connection.register_object(OBJECT_PATH, Gio.DBusNodeInfo.new_for_xml(xml).interfaces[0], record, None, None) connection.call_sync("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "RequestName", GLib.Variant("(su)", (BUS_NAME, 0)), None, Gio.DBusCallFlags.NONE, 1000, None) return connection def find(accessible, role, name=None, depth=0): if accessible is None or depth > 60: return None try: if accessible.get_role_name() == role and (name is None or (accessible.get_name() or "").startswith(name)): return accessible count = accessible.get_child_count() except Exception: return None for index in range(count): try: found = find(accessible.get_child_at_index(index), role, name, depth + 1) except Exception: found = None if found is not None: return found return None def published_application(): desktop = Atspi.get_desktop(0) for index in range(desktop.get_child_count()): application = desktop.get_child_at_index(index) if application is not None and application.get_name() == "electron": return application return None def process_pids(matches): pids = [] for entry in Path("/proc").iterdir(): if not entry.name.isdigit(): continue try: commandLine = (entry / "cmdline").read_bytes().split(b"\0") except OSError: continue if commandLine and matches(commandLine): pids.append(int(entry.name)) return pids def publisher_pids(): return process_pids(lambda arguments: arguments[0].endswith(b"wine-access-browser-publisher")) def worker_pids(): return process_pids(lambda arguments: any(argument.lower().endswith(b"wine-access-browser-worker.exe") for argument in arguments)) def main(): build = Path(sys.argv[1]).resolve() readerConnection = register_reader() # releasing it would drop the reader's bus name helper = subprocess.Popen([str(build / "wine-access.exe"), "--no-controller"], stderr=subprocess.PIPE, env=os.environ.copy()) threading.Thread(target=lambda: [helperLines.append(line.decode(errors="replace").rstrip()) for line in helper.stderr], daemon=True).start() try: check("helper reports the browser bridge active", wait_until(lambda: any('"browser_bridge":true' in line for line in helperLines), 30), str(helperLines[-5:])) subprocess.run(["xdotool", "search", "--name", TITLE, "windowactivate", "--sync"], capture_output=True) published = wait_until(lambda: any("published; legacy announcements retired" in line for line in helperLines), 60) check("helper starts a publisher that publishes", published, str(helperLines[-5:])) application = None if published: wait_until(lambda: published_application() is not None, 20) application = published_application() check("electron application appears on the accessibility bus", application is not None) entry = find(application, "entry", "User name") if application else None check("the page's text field is published", entry is not None) if entry is not None: Atspi.Component.grab_focus(entry) pump(1.5) presented.clear() subprocess.run(["xdotool", "search", "--name", TITLE, "windowfocus", "--sync"], capture_output=True) subprocess.run(["xdotool", "type", "--delay", "100", "abc"], capture_output=True) pump(2.5) text = Atspi.Text.get_text(entry, 0, -1) check("typing reached the published field", text == "abc", repr(text)) check("no legacy announcements while typing in the published window", not presented, str(presented[:3])) before = publisher_pids() check("exactly one publisher is running", len(before) == 1, str(before)) if before: os.kill(before[0], signal.SIGKILL) restarted = wait_until(lambda: any(pid not in before for pid in publisher_pids()), 30) and \ wait_until(lambda: sum("published; legacy announcements retired" in line for line in helperLines) >= 2, 60) check("a killed publisher is restarted and publishes again", restarted, str(helperLines[-5:])) finally: helper.send_signal(signal.SIGTERM) try: helper.wait(timeout=15) except subprocess.TimeoutExpired: helper.kill() helper.wait() check("no publisher survives the helper", wait_until(lambda: not publisher_pids(), 10), str(publisher_pids())) check("no worker survives the helper", wait_until(lambda: not worker_pids(), 10), str(worker_pids())) readerConnection.close_sync(None) print("\n".join(line for line in helperLines if "browser" in line), flush=True) return 1 if failures else 0 if __name__ == "__main__": sys.exit(main())