diff --git a/docs/dev/vm-tests.md b/docs/dev/vm-tests.md index 6569597c..ae1e90e4 100644 --- a/docs/dev/vm-tests.md +++ b/docs/dev/vm-tests.md @@ -154,7 +154,7 @@ the run. daemon put it away (`hidden`), ask the operating system for its frame (`os_window`, the oracle: the daemon's state is the thing under test), and take a screenshot. `core/scenarios` has the scenarios written against it: `tiling`, - `focus`, `moving` and `stacks`, which `core-e2e` runs on KDE and which a suite + `focus`, `moving`, `stacks` and `workspaces`, which `core-e2e` runs on KDE and which a suite of kfw can run unchanged. A method is added with the first scenario that needs it. `KdePlatform` (`kde_platform.py`) is a `Kde` session as a `Platform`, and the `core-e2e` suite runs the scenarios through it after its own sections. @@ -350,7 +350,7 @@ platform, so it follows four rules: suits it. - It is checked against a fake first. `tests/fake_platform.py` is a daemon in memory that follows `focus`, `cycle-focus`, `move`, `cycle-move`, `stack`, - `cycle-stack` and `unstack` over three tiles, with switches to make it wrong in + `cycle-stack`, `unstack`, `send-to-workspace` and `focus-workspace` over three tiles, with switches to make it wrong in one way; a scenario's unit test runs it against the right daemon and against wrong ones, so that a check that cannot fail is found without a VM. diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/workspaces.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/workspaces.py new file mode 100644 index 00000000..249a61da --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/workspaces.py @@ -0,0 +1,59 @@ +"""A window sent to another workspace is put away, and switching to it brings it and takes the +others away.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.support import focus_agrees, os_focused + + +def _in_view(platform: Platform, clients: list[Client]) -> list[str]: + return sorted(c.title for c in clients if not platform.hidden(c)) + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Sends the focused one of `clients` to workspace 1, switches to it and back. + + `clients` are tiled windows of workspace 0, each in a container of its own, with more than one + of them. Workspace 1 is empty. + """ + last = clients[-1] + rest = sorted(c.title for c in clients if c != last) + platform.activate(last) + platform.wait_for(lambda: focus_agrees(platform, clients, last.title)) + checks.check( + "every window is in view", _in_view(platform, clients) == sorted(c.title for c in clients) + ) + + platform.komorebic("send-to-workspace", "1") + checks.check( + f"after 'send-to-workspace 1' {last.title} is put away", + platform.wait_for(lambda: platform.hidden(last)), + ) + checks.check( + "... the daemon has it on workspace 1", + platform.wait_for(lambda: platform.state().containers(1) == [[last.title]]), + ) + checks.check( + "... and the others are still in view", + platform.wait_for(lambda: _in_view(platform, clients) == rest), + ) + + platform.komorebic("focus-workspace", "1") + checks.check( + "after 'focus-workspace 1' only the window sent there is in view", + platform.wait_for(lambda: _in_view(platform, clients) == [last.title]), + ) + checks.check( + "... it has the focus, and the daemon agrees", + platform.wait_for(lambda: focus_agrees(platform, clients, last.title)), + ) + + platform.komorebic("focus-workspace", "0") + checks.check( + "after 'focus-workspace 0' the others are in view and the one sent away is put away", + platform.wait_for(lambda: _in_view(platform, clients) == rest), + ) + checks.check( + "... and one of them has the focus", + platform.wait_for(lambda: os_focused(platform, clients) in rest), + ) diff --git a/nix/tests/vm/lib/komorebi_vm/core/snapshot.py b/nix/tests/vm/lib/komorebi_vm/core/snapshot.py index 3c2102ac..ef5d2a7f 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/snapshot.py +++ b/nix/tests/vm/lib/komorebi_vm/core/snapshot.py @@ -80,11 +80,12 @@ class Snapshot: extra: dict[str, Any] = field(default_factory=dict, compare=False) """What only this port's state has, such as `has_pending_raise_op` on kfw.""" - def workspace(self, workspace: int = 0, monitor: int = 0) -> Workspace: - """A workspace of a monitor, by position.""" - return self.monitors[monitor].workspaces[workspace] + def workspace(self, workspace: int | None = None, monitor: int = 0) -> Workspace: + """A workspace of a monitor, by position; the focused one when none is given.""" + m = self.monitors[monitor] + return m.workspaces[m.focused_workspace if workspace is None else workspace] - def containers(self, workspace: int = 0, monitor: int = 0) -> list[list[str]]: + def containers(self, workspace: int | None = None, monitor: int = 0) -> list[list[str]]: """The titles of the windows of each tiled container of a workspace.""" return [[w.title for w in c] for c in self.workspace(workspace, monitor).containers] @@ -92,9 +93,9 @@ class Snapshot: """The number of windows the daemon has, on every workspace of every monitor.""" return sum(len(ws.windows()) for m in self.monitors for ws in m.workspaces) - def focused_title(self, workspace: int = 0, monitor: int = 0) -> str: - """The title of the focused window of a workspace: the focused window of its focused - container. Empty if the workspace has no container.""" + def focused_title(self, workspace: int | None = None, monitor: int = 0) -> str: + """The title of the focused window of a workspace (the focused one when none is given): the + focused window of its focused container. Empty if the workspace has no container.""" ws = self.workspace(workspace, monitor) if not ws.containers: return "" diff --git a/nix/tests/vm/lib/komorebi_vm/kde_platform.py b/nix/tests/vm/lib/komorebi_vm/kde_platform.py index bd2c946b..805d38a2 100644 --- a/nix/tests/vm/lib/komorebi_vm/kde_platform.py +++ b/nix/tests/vm/lib/komorebi_vm/kde_platform.py @@ -53,11 +53,19 @@ class KdePlatform: return OsWindow(client.title, self.kde.rect(wid), self.kde.active() == wid) def hidden(self, client: Client) -> bool: - names = self.kde.desktop_names() + """The window is on the parking desktop, or on a desktop that is not the one in view. + + KDE keeps the windows of a workspace on a virtual desktop of its own and parks the ones a + stack or a monocle puts away, so both are out of sight. + """ wid = self.kde.wid(client.title) - if PARK not in names or wid == "": + if wid == "": return False - return self.kde.desk(wid) == names.index(PARK) + 1 + desk = self.kde.desk(wid) + names = self.kde.desktop_names() + if PARK in names and desk == names.index(PARK) + 1: + return True + return desk != 0 and desk != self.kde.shown() def work_area(self) -> Rect: return Rect(0, 0, *self.size) diff --git a/nix/tests/vm/lib/tests/fake_platform.py b/nix/tests/vm/lib/tests/fake_platform.py index 6bda5c67..5f408bb1 100644 --- a/nix/tests/vm/lib/tests/fake_platform.py +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -20,9 +20,11 @@ class FakePlatform: tolerance = 24 def __init__(self, *, moves: bool = True, focus_follows_move: bool = True) -> None: - self.containers: list[list[str]] = [] - """The stack of each container, in the order of the tiles; the first is in view.""" - self.focus = 0 + self.spaces: list[list[list[str]]] = [[], []] + """The two workspaces: for each, the stack of each container, in the order of the tiles, + and the first window of a stack is the one in view.""" + self.focuses = [0, 0] + self.current = 0 self.moves = moves self.focus_follows_move = focus_follows_move @@ -32,6 +34,18 @@ class FakePlatform: def start_komorebi(self) -> None: pass + @property + def containers(self) -> list[list[str]]: + return self.spaces[self.current] + + @property + def focus(self) -> int: + return self.focuses[self.current] + + @focus.setter + def focus(self, value: int) -> None: + self.focuses[self.current] = value + @property def order(self) -> list[str]: """The titles in view, in the order of the tiles.""" @@ -41,18 +55,25 @@ class FakePlatform: self.containers.append([title]) return Client(title) + def _find(self, title: str) -> tuple[int, int]: + for s, containers in enumerate(self.spaces): + for i, stack in enumerate(containers): + if title in stack: + return s, i + raise AssertionError(f"no window {title}") + def _container(self, title: str) -> int: - return next(i for i, stack in enumerate(self.containers) if title in stack) + return self._find(title)[1] def hidden(self, client: Client) -> bool: - stack = self.containers[self._container(client.title)] - return stack[0] != client.title + s, i = self._find(client.title) + return s != self.current or self.spaces[s][i][0] != client.title def work_area(self) -> Rect: return Rect(0, 0, 1920, 1080) def activate(self, client: Client) -> None: - self.focus = self._container(client.title) + self.current, self.focus = self._find(client.title) stack = self.containers[self.focus] stack.insert(0, stack.pop(stack.index(client.title))) @@ -90,6 +111,15 @@ class FakePlatform: case ("cycle-stack", "next" | "previous" as way): stack = self.containers[self.focus] stack.append(stack.pop(0)) if way == "next" else stack.insert(0, stack.pop()) + case ("send-to-workspace", number): + stack = self.containers[self.focus] + window = stack.pop(0) + if not stack: + self.containers.pop(self.focus) + self.focus = max(0, min(self.focus, len(self.containers) - 1)) + self.spaces[int(number)].append([window]) + case ("focus-workspace", number): + self.current = int(number) case ("unstack",): stack = self.containers.pop(self.focus) self.containers[self.focus : self.focus] = [[t] for t in stack] @@ -115,28 +145,42 @@ class FakePlatform: "details": {"title": title, "exe": "", "class": "", "path": ""}, } - workspace = { - "name": None, - "containers": { + def rect(r: Rect) -> dict[str, int]: + return {"left": r.x, "top": r.y, "right": r.w, "bottom": r.h} + + def workspace(containers: list[list[str]], focus: int) -> dict[str, Any]: + return { + "name": None, + "containers": { + "elements": [ + { + "id": str(i), + "locked": False, + "windows": { + "elements": [window(t, TILES[i]) for t in stack], + "focused": 0, + }, + } + for i, stack in enumerate(containers) + ], + "focused": focus, + }, + "monocle_container": None, + "maximized_window": None, + "floating_windows": {"elements": [], "focused": 0}, + "layout": {"Default": "Columns"}, + "tile": True, + "latest_layout": [rect(r) for r in TILES[: len(containers)]], + } + + monitor = { + "id": 1, + "size": rect(Rect(0, 0, 1920, 1080)), + "workspaces": { "elements": [ - { - "id": str(i), - "locked": False, - "windows": {"elements": [window(t, TILES[i]) for t in stack], "focused": 0}, - } - for i, stack in enumerate(self.containers) + workspace(c, f) for c, f in zip(self.spaces, self.focuses, strict=True) ], - "focused": self.focus, + "focused": self.current, }, - "monocle_container": None, - "maximized_window": None, - "floating_windows": {"elements": [], "focused": 0}, - "layout": {"Default": "Columns"}, - "tile": True, - "latest_layout": [ - {"left": r.x, "top": r.y, "right": r.w, "bottom": r.h} for r in TILES - ], } - size = {"left": 0, "top": 0, "right": 1920, "bottom": 1080} - monitor = {"id": 1, "size": size, "workspaces": {"elements": [workspace], "focused": 0}} return parse({"monitors": {"elements": [monitor], "focused": 0}}) diff --git a/nix/tests/vm/lib/tests/test_focus_moving.py b/nix/tests/vm/lib/tests/test_focus_moving.py index d30b7e79..a207304d 100644 --- a/nix/tests/vm/lib/tests/test_focus_moving.py +++ b/nix/tests/vm/lib/tests/test_focus_moving.py @@ -1,7 +1,7 @@ """The focus and moving scenarios against a daemon that is right, and against ones that are not.""" from komorebi_vm.core.checks import Checks -from komorebi_vm.core.scenarios import focus, moving, stacks, tiling +from komorebi_vm.core.scenarios import focus, moving, stacks, tiling, workspaces from tests.fake_platform import FakePlatform @@ -42,3 +42,12 @@ def test_a_daemon_that_is_right_passes_stacks() -> None: stacks.run(platform, checks, clients) assert checks.failed == 0, checks.failures assert checks.passed > 10 + + +def test_a_daemon_that_is_right_passes_workspaces() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + workspaces.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 7 diff --git a/nix/tests/vm/suites/core-e2e/test.py b/nix/tests/vm/suites/core-e2e/test.py index 9a28fb7e..5dee1fa1 100644 --- a/nix/tests/vm/suites/core-e2e/test.py +++ b/nix/tests/vm/suites/core-e2e/test.py @@ -13,7 +13,7 @@ import json from komorebi_vm import Checks, Kde, Rect, daemon -from komorebi_vm.core.scenarios import focus, moving, stacks, tiling +from komorebi_vm.core.scenarios import focus, moving, stacks, tiling, workspaces from komorebi_vm.kde_platform import KdePlatform W, H = 1920, 1080 @@ -348,7 +348,7 @@ checks.check( ) report(["afterwards"]) -print("== 12. tiling, focus, moving and stacks, through the neutral platform") +print("== 12. tiling, focus, moving, stacks and workspaces, through the neutral platform") # the scenarios of komorebi_vm.core.scenarios are what runs on Windows too: it asks the # operating system where the windows are, and not the daemon's log kde.close(kde.wid("afterwards")) @@ -358,6 +358,7 @@ neutral = tiling.run(platform, checks, titles=("neutral1", "neutral2", "neutral3 focus.run(platform, checks, neutral) moving.run(platform, checks, neutral) stacks.run(platform, checks, neutral) +workspaces.run(platform, checks, neutral) print("== 13. komorebic stop") kde.komorebic("stop")