#!/usr/bin/env python3 """Run with: dbus-run-session -- python3 tests/mpris_test.py [path/to/v]. Uses a private bus, the real MPRIS worker and libdbus, without a player window. """ import os from pathlib import Path import select import shutil import subprocess import sys import tempfile import time ROOT = Path(__file__).resolve().parents[1] DEST = "org.mpris.MediaPlayer2.vendetta" PATH = "/org/mpris/MediaPlayer2" PLAYER = "org.mpris.MediaPlayer2.Player" def call(method, *args, path=PATH): return subprocess.run( ["dbus-send", "--session", "--print-reply", "--reply-timeout=1000", f"--dest={DEST}", path, method, *args], capture_output=True, text=True, timeout=3, ) def get_all(): return call("org.freedesktop.DBus.Properties.GetAll", f"string:{PLAYER}") def eventually(predicate): deadline = time.monotonic() + 8 while time.monotonic() < deadline: if predicate(): return time.sleep(0.05) raise AssertionError("Timed out waiting for MPRIS state") def line(process): assert select.select([process.stdout], [], [], 8)[0], "Host stopped responding" result = process.stdout.readline().strip() assert result, f"Host exited: {process.poll()}" return result def control(process, command): process.stdin.write(command + "\n") process.stdin.flush() assert line(process) == "done" def stop(process): process.stdin.write("quit\n") process.stdin.flush() assert line(process) == "stopped" assert process.wait(timeout=5) == 0 def main(): assert os.environ.get("DBUS_SESSION_BUS_ADDRESS"), "Run inside dbus-run-session" with tempfile.TemporaryDirectory(prefix="vendetta-mpris-") as directory: directory = Path(directory) shutil.copy(ROOT / "src/mpris_d_mpris.v", directory) shutil.copy(ROOT / "tests/mpris_host.v", directory / "main.v") executable = directory / "host" subprocess.run( [sys.argv[1] if len(sys.argv) > 1 else "v", "-d", "mpris", "-d", "ui2_custom_rendering", "-path", f"{ROOT}|@vlib|@vmodules", "-o", str(executable), str(directory)], check=True, ) hosts = [] try: def start(): host = subprocess.Popen([str(executable)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, bufsize=1) hosts.append(host) assert line(host) == "ready" return host first = start() eventually(lambda: get_all().returncode == 0) result = get_all() assert 'string "Test track"' in result.stdout, result.stdout assert 'string "None"' in result.stdout, result.stdout result = call("org.freedesktop.DBus.Introspectable.Introspect") assert result.returncode == 0 and PLAYER in result.stdout, result.stderr # Signature checks must reject malformed calls without queuing them. for method, args in [ (f"{PLAYER}.Seek", ["string:bad"]), (f"{PLAYER}.SetPosition", ["string:bad", "int64:0"]), (f"{PLAYER}.Pause", ["int64:0"]), ("org.freedesktop.DBus.Properties.Get", [f"string:{PLAYER}"]), ("org.freedesktop.DBus.Properties.Set", [f"string:{PLAYER}", "string:Rate", "variant:double:nan"]), ("org.freedesktop.DBus.Properties.Set", [f"string:{PLAYER}", "string:Volume", "variant:double:inf"]), ]: result = call(method, *args) assert "InvalidArgs" in result.stderr, result result = call("org.freedesktop.DBus.Properties.GetAll", f"string:{PLAYER}", path="/wrong") assert result.returncode != 0 # Fill the UI queue, then ensure the bus still answers reads and quit. control(first, "fill") result = call(f"{PLAYER}.Pause") assert "LimitsExceeded" in result.stderr, result assert get_all().returncode == 0 monitor = subprocess.Popen( ["dbus-monitor", "--session", "type='signal',interface='org.mpris.MediaPlayer2.Player',member='Seeked'"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, ) try: time.sleep(0.2) control(first, "seek") time.sleep(0.2) monitor.terminate() output, _ = monitor.communicate(timeout=3) assert "int64 1250000" in output and "variant" not in output, output finally: if monitor.poll() is None: monitor.kill() monitor.wait() for _ in range(3): control(first, "off") eventually(lambda: get_all().returncode != 0) control(first, "on") eventually(lambda: get_all().returncode == 0) # A second instance must recover when the first releases the name. second = start() control(first, "off") eventually(lambda: get_all().returncode == 0) stop(first) assert get_all().returncode == 0 stop(second) eventually(lambda: get_all().returncode != 0) # Restart a bus at the same address while the worker remains alive. address = f"unix:path={directory / 'bus'}" original_address = os.environ["DBUS_SESSION_BUS_ADDRESS"] os.environ["DBUS_SESSION_BUS_ADDRESS"] = address daemon = None try: def start_bus(): return subprocess.Popen( ["dbus-daemon", "--session", "--nofork", "--nopidfile", f"--address={address}"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) daemon = start_bus() third = start() eventually(lambda: get_all().returncode == 0) daemon.terminate() daemon.wait(timeout=5) eventually(lambda: get_all().returncode != 0) assert third.poll() is None, "Bus loss terminated the worker host" daemon = start_bus() eventually(lambda: get_all().returncode == 0) assert 'string "Test track"' in get_all().stdout stop(third) finally: if daemon is not None and daemon.poll() is None: daemon.terminate() daemon.wait(timeout=5) os.environ["DBUS_SESSION_BUS_ADDRESS"] = original_address print("MPRIS checks passed: properties, signatures, overload, Seeked, toggles, ownership, bus recovery, shutdown") finally: for host in hosts: if host.poll() is None: host.kill() host.wait(timeout=5) if __name__ == "__main__": main()