Files
summit/engine/tests/test_window_portal.py

131 lines
5.4 KiB
Python

"""Wayland window capture (framegen/window_portal.py, framegen/kwin.py): the pure parts. The portal, PipeWire and
KWin themselves were run on a Plasma 6.8 desktop (docs/WAYLAND_REPORT.md), not here."""
import json
import os
import pathlib
import sys
import pytest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from framegen import kwin
from framegen import window_portal as wp
from framegen.window_x11 import XWindow, pick
def test_kwin_report_parses_and_skips_broken_entries():
text = json.dumps([
{"pid": 42, "caption": "My Game", "cls": "MyGame", "x": 1920, "y": 251, "w": 1288, "h": 749, "active": True,
"x11": False, "minimized": False, "output": "DP-1", "id": "{abc}"},
{"pid": 7, "caption": "broken"},
])
ws = kwin.parse(text)
assert len(ws) == 1 and ws[0].cls == "mygame" and ws[0].output == "DP-1" and not ws[0].x11
def test_kwin_windows_are_picked_by_the_same_rules_as_x11_ones():
ws = kwin.parse(json.dumps([
{"pid": 10, "caption": "Editor", "cls": "kate", "x": 0, "y": 0, "w": 800, "h": 600, "id": "a"},
{"pid": 42, "caption": "Voidrunner", "cls": "game", "x": 0, "y": 0, "w": 1280, "h": 720, "id": "b"},
]))
xs = kwin.as_xwindows(ws)
assert ws[pick("pid:42", xs, family=lambda p: {p}).xid].id == "b"
assert ws[pick("class:kate", xs).xid].id == "a"
assert ws[pick("void", xs).xid].id == "b"
assert pick("nothing", xs) is None
def test_kwin_only_on_kde_wayland():
assert kwin.available({"XDG_CURRENT_DESKTOP": "KDE", "WAYLAND_DISPLAY": "wayland-1"})
assert not kwin.available({"XDG_CURRENT_DESKTOP": "GNOME", "WAYLAND_DISPLAY": "wayland-0"})
assert not kwin.available({"XDG_CURRENT_DESKTOP": "KDE"})
def test_select_options_ask_for_one_window_kept_until_revoked():
o = wp.select_options("", 7, "h1")
assert o["types"] == ("u", 2) and o["multiple"] == ("b", False) and o["persist_mode"] == ("u", 2)
assert o["cursor_mode"] == ("u", 1) and "restore_token" not in o
o = wp.select_options("tok", 2, "h2") # a portal that can't hide the pointer: don't ask it to
assert o["restore_token"] == ("s", "tok") and "cursor_mode" not in o
def test_gst_command_reads_the_portal_stream_as_raw_bgr():
a = wp.gst_argv("gst-launch-1.0", 9, 161, 1288, 748)
assert a[a.index("pipewiresrc") + 1:a.index("pipewiresrc") + 3] == ["fd=9", "path=161"]
assert "video/x-raw,format=BGR,width=1288,height=748" in a and a[-2:] == ["fd=1", "sync=false"]
def test_restore_token_is_kept_private_under_summit_config(tmp_path):
assert wp.token_path({"XDG_CONFIG_HOME": str(tmp_path)}) == str(tmp_path / "summit" / "screencast-token")
p = str(tmp_path / "summit" / "screencast-token")
assert wp.load_token(p) == ""
wp.save_token("abc-123", p)
assert wp.load_token(p) == "abc-123"
assert (pathlib.Path(p).stat().st_mode & 0o777) == 0o600
# ---- XWayland or native? (KWin 6.8 gives no flag: found on the owner's desktop, where Voidrunner, an XWayland game,
# ---- was taken for a native Wayland window and shown the share dialog)
class FakeX:
def __init__(self, windows):
self.windows = windows # xid -> XWindow
def client_ids(self):
return list(self.windows)
def describe(self, xid):
return self.windows.get(xid)
def close(self):
pass
class FakeKWin:
def __init__(self, windows):
self._w = windows
def windows(self):
return self._w
def close(self):
pass
def _kw(pid, caption="Voidrunner", w=1288, h=749):
return kwin.KWindow(pid, caption, "game", 0, 0, w, h, False, False, False, "HDMI-A-1", f"{{{pid}}}")
def _wire(monkeypatch, xwins, kwins):
from framegen import window_x11 as wx
monkeypatch.setattr(wx, "session_problem", lambda env: None)
monkeypatch.setattr(wx, "X", lambda: FakeX(xwins))
monkeypatch.setattr(wx, "find_windows", lambda x=None: [w for w in xwins.values() if w.title and w.w >= 100 and w.h >= 100])
monkeypatch.setattr(kwin, "available", lambda env=None: True)
monkeypatch.setattr(kwin, "KWin", lambda: FakeKWin(kwins))
monkeypatch.setattr(wp, "PortalWindowSource", lambda w, kw: ("portal", w.caption))
def test_native_candidates_leave_out_windows_the_x_server_has():
ks = [_kw(42), _kw(43, "native"), _kw(os.getpid(), "me")]
assert [w.pid for w in wp.native_candidates(ks, {42}, os.getpid())] == [43]
def test_a_tiny_untitled_xwayland_window_is_not_taken_for_a_native_one(monkeypatch):
# The X client list has pid 42 as a 1x1 window with no title (the game is still starting): find_windows skips it,
# but KWin already lists it. It must not be offered to the portal.
_wire(monkeypatch, {1: XWindow(1, "", "game", 42, 1, 1)}, [_kw(42)])
with pytest.raises(RuntimeError, match="no window matches"):
wp.open_window("pid:42", wait_s=0.0, sleep=lambda s: None, env={})
def test_a_window_only_kwin_knows_goes_to_the_portal(monkeypatch):
_wire(monkeypatch, {1: XWindow(1, "Editor", "kate", 10, 800, 600)}, [_kw(42)])
assert wp.open_window("pid:42", wait_s=0.0, sleep=lambda s: None, env={}) == ("portal", "Voidrunner")
def test_an_xwayland_game_is_captured_through_x(monkeypatch):
from framegen import window_x11 as wx
_wire(monkeypatch, {1: XWindow(1, "Voidrunner", "game", 42, 1280, 720)}, [_kw(42)])
monkeypatch.setattr(wx, "X11WindowSource", lambda target, x: ("x11", target.title))
assert wp.open_window("pid:42", wait_s=0.0, sleep=lambda s: None, env={}) == ("x11", "Voidrunner")