diff --git a/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/workspace_rules.py b/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/workspace_rules.py new file mode 100644 index 00000000..c5fcea11 --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/platforms/kde/extras/workspace_rules.py @@ -0,0 +1,123 @@ +"""What only KDE can tell about workspace rules, beside the shared `rules` and `startup_rules`. + +A workspace is a virtual desktop of KWin, so a window that a rule sends to another workspace is on +that workspace's desktop, and out of sight on the parking desktop while the desktop in front is +showing. The shared scenarios check where the daemon puts the window and that it is put away; these +check it on KWin's desktops, the rules by the name of a workspace, and a rule that is applied at +the next event. + +The suite runs the shared scenarios, then `desktops`, `named_rule` and `cleared`. +""" + +import json + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client +from komorebi_vm.kde_platform import KdePlatform +from komorebi_vm.platforms.kde.support import parked, where_asked + + +def _on(platform: KdePlatform, title: str) -> int | None: + return platform.state().workspace_of(title) + + +def _shown_workspace(platform: KdePlatform) -> int: + return platform.state().monitors[0].focused_workspace + + +def desktops(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: + """An initial rule sends a window to workspace 1: it is on the parking desktop while desktop 1 + shows, on desktop 2, the desktop of its workspace, once that is shown, and tiled where the + daemon asked. + + The daemon must be running on the first workspace, with no window. It makes workspaces 0 to 2 + (`web`, `code`, `chat`) and leaves the rule, which `cleared` takes away. + """ + kde = platform.kde + kde.komorebic("ensure-named-workspaces", "0", "web", "code", "chat") + kde.komorebic("initial-workspace-rule", "title", "sent-once", "0", "1") + kde.komorebic("named-workspace-rule", "title", "sent-always", "chat") + + stay = platform.spawn("stays here") + once = platform.spawn("sent-once one") + kde.wait_for(lambda: _on(platform, once.title) == 1, timeout=30) + checks.check( + "the window the rule names is on the parking desktop while desktop 1 is showing", + kde.wait_for( + lambda: ( + parked(platform, once.title) + and kde.shown() == 1 + and _shown_workspace(platform) == 0 + ) + ), + ) + checks.check( + "the other window is on KWin's desktop 1, where it opened", + _on(platform, stay.title) == 0 and kde.desk(kde.wid(stay.title)) == 1, + ) + kde.komorebic("focus-workspace", "1") + checks.check("workspace 1 is shown as KWin's desktop 2", kde.wait_for(lambda: kde.shown() == 2)) + checks.check( + "and the window is on that desktop, not parked", + kde.wait_for( + lambda: kde.desk(kde.wid(once.title)) == 2 and not parked(platform, once.title) + ), + ) + checks.check( + "the window is tiled where the daemon asked", + kde.wait_for(lambda: where_asked(platform, once.title)), + ) + kde.komorebic("move-to-workspace", "0") + kde.wait_for(lambda: _on(platform, once.title) == 0 and kde.desk(kde.wid(once.title)) == 1) + kde.komorebic("focus-workspace", "0") + + +def named_rule(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: + """A rule by the name of a workspace (`chat`) sends a window to it, on desktop 3, and a window + that opens is an event that applies the rules again, so the window comes back after the user + moves it. Needs `desktops` to have run.""" + kde = platform.kde + always = platform.spawn("sent-always one") + checks.check( + "a window the rule by the name of a workspace names goes to chat, out of sight", + kde.wait_for(lambda: _on(platform, always.title) == 2 and parked(platform, always.title)), + ) + kde.komorebic("focus-workspace", "2") + checks.check( + "showing chat brings the window to KWin's desktop 3", + kde.wait_for( + lambda: kde.desk(kde.wid(always.title)) == 3 and not parked(platform, always.title) + ), + ) + kde.komorebic("move-to-workspace", "0") + # a new window is an event, and every event looks at the rules first + platform.spawn("poke one") + checks.check( + "the rule sends the window back at the next event", + kde.wait_for(lambda: _on(platform, always.title) == 2, timeout=30), + ) + kde.komorebic("focus-workspace", "0") + + +OPENED = ("stays here", "sent-once one", "sent-always one", "poke one", "sent-once two") + + +def cleared(platform: KdePlatform, checks: Checks, clients: list[Client]) -> None: + """Once every rule is cleared the daemon's global state has none, and a window that opens stays + on the desktop in front. The windows of these extras are closed after, and the workspace in + front is the first, so that the shared scenarios find the daemon as they expect.""" + kde = platform.kde + kde.komorebic("clear-all-workspace-rules") + rules = json.loads(kde.komorebic("global-state"))["workspace_rules"] + checks.check("no rule is left", rules == []) + later = platform.spawn("sent-once two") + checks.check( + "a window that opens now is on the desktop that is in front", + kde.wait_for(lambda: _on(platform, later.title) == _shown_workspace(platform), timeout=30) + and kde.desk(kde.wid(later.title)) == kde.shown(), + ) + for title in OPENED: + if kde.wid(title) != "": + platform.close(Client(title)) + kde.wait_for(lambda: kde.managed_all() == 0, timeout=20) + kde.komorebic("focus-workspace", "0") diff --git a/nix/tests/vm/suites/workspace-rules/test.py b/nix/tests/vm/suites/workspace-rules/test.py index 5d53a911..cebe72ea 100644 --- a/nix/tests/vm/suites/workspace-rules/test.py +++ b/nix/tests/vm/suites/workspace-rules/test.py @@ -1,15 +1,18 @@ # Workspace rules in a real KWin: a rule sends the windows of an application to a workspace, which -# is a virtual desktop, so that a window that opens on the desktop in front leaves the workspace -# in front and is hidden, on the parking desktop, until its workspace is shown. What only a real -# KWin can tell: that the window is out of sight on the parking desktop while the desktop in front -# stays showing, that it is on the desktop of the workspace the rule names and tiled when that -# workspace is shown, that an initial rule sends a window once and a permanent rule sends it back, -# and that the rules a `komorebi.json` gives apply to a window that was open before the daemon -# started. `komorebi/tests/workspace_rules.rs` checks the same against the fake. -import json -import shlex - -from komorebi_vm import Checks, Kde, daemon +# is a virtual desktop, so that a window that opens on the desktop in front leaves the workspace in +# front and is hidden, on the parking desktop, until its workspace is shown. The shared scenarios +# (`komorebi_vm.core.scenarios.rules` and `startup_rules`, which run on Windows too) check where the +# daemon puts the window, that an initial rule sends it once and a permanent rule sends it again, +# and the rules of a `komorebi.json` for a window that was open before the daemon. What only a real +# KWin can tell is in `komorebi_vm.platforms.kde.extras.workspace_rules`: that the window is on the +# desktop of its workspace and out of sight on the parking desktop while another desktop shows, +# the rules by the name of a workspace, and a rule applied at the next event. +# `komorebi/tests/workspace_rules.rs` checks the same against the fake. +from komorebi_vm import Checks, Kde +from komorebi_vm.core.scenarios import rules, startup_rules +from komorebi_vm.core.suite import platform_only, run_suite, shared +from komorebi_vm.kde_platform import KdePlatform +from komorebi_vm.platforms.kde.extras import workspace_rules as kde_rules start_all() machine.wait_for_unit("multi-user.target") @@ -17,174 +20,20 @@ machine.wait_for_unit("user@1000.service") checks = Checks() kde = Kde.headless(machine, "main", size=(1920, 1080), scale=SCALE) -# foot sizes itself to whole cells of its font, so its frame can be short of the one asked for -CELL = 24 +platform = KdePlatform(kde, (1920, 1080)) +platform.start_komorebi() - -def open_window(title: str) -> str: - kde.spawn(f"foot -T {shlex.quote(title)} sleep 3000") - kde.wait_for(lambda: kde.wid(title) != "", timeout=30) - return kde.wid(title) - - -def workspace_of(title: str) -> int | None: - """The workspace of the first monitor that has a window with the title in a container.""" - elements = kde.state()["monitors"]["elements"][0]["workspaces"]["elements"] - for index, workspace in enumerate(elements): - for container in workspace["containers"]["elements"]: - if any(w["details"]["title"] == title for w in container["windows"]["elements"]): - return index - return None - - -def shown_workspace() -> int: - return kde.state()["monitors"]["elements"][0]["workspaces"]["focused"] - - -def park_desktop() -> int: - """The number of the parking desktop, counted from 1 as kdotool counts, or 0 for none.""" - names = kde.desktop_names() - return names.index("komorebi-park") + 1 if "komorebi-park" in names else 0 - - -def parked(wid: str) -> bool: - """Whether the window is on the parking desktop, out of sight.""" - return park_desktop() != 0 and kde.desk(wid) == park_desktop() - - -print("== 1. a rule that was made while the daemon runs") -kde.start_komorebi() -checks.check( - "the daemon answers komorebic", - kde.wait_for(lambda: kde.run("komorebic state")[0] == 0, timeout=30), -) -kde.komorebic("ensure-named-workspaces", "0", "web", "code", "chat") -kde.komorebic("initial-workspace-rule", "title", "sent-once", "0", "1") -kde.komorebic("named-workspace-rule", "title", "sent-always", "chat") - -stay = open_window("stays here") -once = open_window("sent-once one") -checks.check( - "the window the rule names is on workspace 1, whichever way it got there", - kde.wait_for(lambda: workspace_of("sent-once one") == 1, timeout=30), -) -checks.check( - "... and it is out of sight on the parking desktop, while desktop 1 is still showing", - kde.wait_for(lambda: parked(once) and kde.shown() == 1 and shown_workspace() == 0), -) -checks.check( - "the other window is where it opened", - workspace_of("stays here") == 0 and kde.desk(stay) == 1, +args = (platform, checks, []) +run_suite( + checks, + platform_only("kde", kde_rules.desktops, *args), + platform_only("kde", kde_rules.named_rule, *args), + platform_only("kde", kde_rules.cleared, *args), + shared(rules.run, platform, checks), + shared(startup_rules.run, platform, checks), ) -kde.komorebic("focus-workspace", "1") -checks.check("workspace 1 is shown", kde.wait_for(lambda: kde.shown() == 2)) -checks.check( - "and the window is back, on KWin's desktop 2, the desktop of its workspace", - kde.wait_for(lambda: kde.desk(once) == 2 and not parked(once)), -) - - -def tiled(title: str) -> bool: - want, wid = daemon.asked(kde.komorebi_log(), title), kde.wid(title) - if want is None or wid == "": - return False - have = kde.rect(wid) - return (have.x, have.y) == (want.x, want.y) and 0 <= want.w - have.w < CELL - - -checks.check( - "the window is tiled where komorebi asked", - kde.wait_for(lambda: tiled("sent-once one")), -) - -print("== 2. an initial rule sends a window once, a permanent rule every time") -kde.komorebic("move-to-workspace", "0") -checks.check( - "the user takes the window back to workspace 0", - kde.wait_for(lambda: workspace_of("sent-once one") == 0 and kde.desk(once) == 1), -) -always = open_window("sent-always one") -checks.check( - "a window the permanent rule names goes to the workspace named chat (2), out of sight", - kde.wait_for(lambda: workspace_of("sent-always one") == 2 and parked(always)), -) -checks.check( - "the window the initial rule sent is where the user put it", - workspace_of("sent-once one") == 0, -) -kde.komorebic("focus-workspace", "2") -checks.check( - "showing chat brings the window back to KWin's desktop 3", - kde.wait_for(lambda: kde.desk(always) == 3 and not parked(always)), -) -kde.komorebic("move-to-workspace", "0") -# a new window is an event, and every event looks at the rules first -open_window("poke one") -checks.check( - "the permanent rule sends the window back at the next event", - kde.wait_for(lambda: workspace_of("sent-always one") == 2, timeout=30), -) -kde.komorebic("enforce-workspace-rules") -checks.check( - "enforce-workspace-rules sends the initial rule's window again", - kde.wait_for(lambda: workspace_of("sent-once one") == 1, timeout=30), -) - -print("== 3. the rules can all be taken away") -kde.komorebic("clear-all-workspace-rules") -state_rules = json.loads(kde.komorebic("global-state"))["workspace_rules"] -checks.check("no rule is left", state_rules == []) -later = open_window("sent-once two") -checks.check( - "a window that opens now stays on the workspace in front", - kde.wait_for(lambda: workspace_of("sent-once two") == shown_workspace(), timeout=30) - and kde.desk(later) == kde.shown(), -) - -print("== 4. the rules of komorebi.json, with a window that was open before the daemon") -kde.komorebic("stop") -kde.wait_for(lambda: not kde.komorebi_running(), timeout=30) -CONFIG = "/tmp/rules-config.json" -kde.sh( - "printf %s " - + shlex.quote( - json.dumps( - { - "monitors": [ - { - "workspaces": [ - {"name": "web", "layout": "BSP"}, - { - "name": "code", - "layout": "BSP", - "workspace_rules": [ - { - "kind": "Title", - "id": "stays here", - "matching_strategy": "StartsWith", - } - ], - }, - ] - } - ] - } - ) - ) - + f" > {CONFIG}" -) -kde.start_komorebi("--config", CONFIG) -checks.check( - "the daemon answers komorebic again", - kde.wait_for(lambda: kde.run("komorebic state")[0] == 0, timeout=30), -) -checks.check( - "the window that was open when it started is on the workspace the file names, out of sight", - kde.wait_for(lambda: workspace_of("stays here") == 1 and parked(stay), timeout=30), -) - -kde.komorebic("stop") -checks.check("a clean stop", kde.wait_for(lambda: not kde.komorebi_running())) +kde.stop_komorebi() +checks.check("a clean stop", not kde.komorebi_running()) kde.stop() checks.finish()