diff --git a/docs/dev/vm-tests.md b/docs/dev/vm-tests.md index d5a6f8df..c656d69b 100644 --- a/docs/dev/vm-tests.md +++ b/docs/dev/vm-tests.md @@ -150,10 +150,10 @@ the run. ports' serializers; recorded states replace them once a run on each port exists. - `Platform` (`core/platform.py`) is what a scenario needs of a platform: start the - daemon, run `komorebic`, read the `Snapshot`, open a window, ask the operating + daemon, run `komorebic`, read the `Snapshot`, open a window, ask whether the 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` and `moving`, which `core-e2e` runs on KDE and which + against it: `tiling`, `focus`, `moving` and `stacks`, which `core-e2e` runs on KDE and which scenarios of kfw's suite 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 `tiling` through it after its own diff --git a/nix/tests/vm/lib/komorebi_vm/core/platform.py b/nix/tests/vm/lib/komorebi_vm/core/platform.py index 0f40c9a9..134918fb 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/platform.py +++ b/nix/tests/vm/lib/komorebi_vm/core/platform.py @@ -73,6 +73,15 @@ class Platform(Protocol): """What the operating system says about the window, or `None` if it has none.""" ... + def hidden(self, client: Client) -> bool: + """The window exists but is out of sight because the daemon put it away: the other windows + of a stack, the windows of another workspace, those behind a monocle. + + That is a window parked on a desktop of its own on KDE, and cloaked on Windows. A window + the user minimized is not hidden in this sense. + """ + ... + def work_area(self) -> Rect: """The area of the first display that windows may take.""" ... diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/stacks.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/stacks.py new file mode 100644 index 00000000..53e87690 --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/stacks.py @@ -0,0 +1,83 @@ +"""A stack shows one window and puts the others away, and the window in view has the focus.""" + +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 _sizes(platform: Platform) -> list[int]: + return sorted(len(titles) for titles in platform.state().containers()) + + +def _away(platform: Platform, clients: list[Client]) -> list[str]: + return [c.title for c in clients if platform.hidden(c)] + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Stacks the focused one of `clients` onto its neighbor, cycles the stack and takes it apart. + + `clients` are three tiled windows of the first workspace, each in a container of its own, + and the last of them has the focus. + """ + last = clients[-1] + platform.activate(last) + platform.wait_for(lambda: focus_agrees(platform, clients, last.title)) + checks.check("there are three tiles", _sizes(platform) == [1, 1, 1]) + checks.check("nothing is put away yet", _away(platform, clients) == []) + + platform.komorebic("stack", "left") + checks.check( + "two containers, one of them a stack of two", + platform.wait_for(lambda: _sizes(platform) == [1, 2]), + ) + checks.check( + "exactly one window is put away", + platform.wait_for(lambda: len(_away(platform, clients)) == 1), + ) + shown = os_focused(platform, clients) + checks.check( + "the window in view has the focus", shown != "" and shown not in _away(platform, clients) + ) + checks.check( + "... and the daemon's state agrees", + platform.wait_for(lambda: focus_agrees(platform, clients, shown)), + ) + frame = platform.os_window(Client(shown)) + + platform.komorebic("cycle-stack", "next") + checks.check( + "after 'cycle-stack next' the other window of the stack is in view and the first is away", + platform.wait_for( + lambda: ( + os_focused(platform, clients) not in ("", shown) + and _away(platform, clients) == [shown] + ) + ), + ) + other = os_focused(platform, clients) + checks.check( + "... with the focus agreed", + platform.wait_for(lambda: focus_agrees(platform, clients, other)), + ) + now = platform.os_window(Client(other)) + checks.check( + "... and it fills the tile the first one had", + frame is not None and now is not None and now.frame == frame.frame, + ) + platform.komorebic("cycle-stack", "previous") + checks.check( + "cycling back, the first is in view again", + platform.wait_for( + lambda: os_focused(platform, clients) == shown and len(_away(platform, clients)) == 1 + ), + ) + + platform.komorebic("unstack") + checks.check("three tiles again", platform.wait_for(lambda: _sizes(platform) == [1, 1, 1])) + checks.check("nothing is put away", platform.wait_for(lambda: _away(platform, clients) == [])) + frames = [w.frame for c in clients if (w := platform.os_window(c)) is not None] + checks.check( + "and they tile, not on each other", + len(frames) == len(clients) + and not any(a.overlaps(b) for i, a in enumerate(frames) for b in frames[i + 1 :]), + ) diff --git a/nix/tests/vm/lib/komorebi_vm/core/snapshot.py b/nix/tests/vm/lib/komorebi_vm/core/snapshot.py index d3a88b10..3c2102ac 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/snapshot.py +++ b/nix/tests/vm/lib/komorebi_vm/core/snapshot.py @@ -41,6 +41,8 @@ class Workspace: containers: list[list[Window]] """The tiled containers, in order, each with its stack of windows.""" focused_container: int + focused_windows: list[int] + """For each container, the place in it of the window that has the focus (a stack's top).""" monocle: list[Window] | None """The windows of the monocle container, or `None` when the workspace is not in monocle.""" maximized: Window | None @@ -96,8 +98,11 @@ class Snapshot: ws = self.workspace(workspace, monitor) if not ws.containers: return "" - container = ws.containers[min(ws.focused_container, len(ws.containers) - 1)] - return container[0].title if container else "" + index = min(ws.focused_container, len(ws.containers) - 1) + container = ws.containers[index] + if not container: + return "" + return container[min(ws.focused_windows[index], len(container) - 1)].title def _ring(value: dict[str, Any]) -> list[Any]: @@ -144,6 +149,7 @@ def _workspace(value: dict[str, Any]) -> Workspace: tile=bool(value.get("tile", True)), containers=containers, focused_container=_focused(value["containers"]), + focused_windows=[_focused(c["windows"]) for c in _ring(value["containers"])], monocle=[_window(w) for w in _ring(monocle["windows"])] if monocle else None, maximized=_window(maximized) if maximized else None, floating=[_window(w) for w in _ring(value["floating_windows"])], diff --git a/nix/tests/vm/lib/komorebi_vm/kde_platform.py b/nix/tests/vm/lib/komorebi_vm/kde_platform.py index 3fde4a4a..bd2c946b 100644 --- a/nix/tests/vm/lib/komorebi_vm/kde_platform.py +++ b/nix/tests/vm/lib/komorebi_vm/kde_platform.py @@ -11,6 +11,8 @@ from komorebi_vm.kde import Kde # foot sizes itself to whole cells of its font, so its frame can be short of the one asked for CELL = 24 +# the desktop that komorebi puts the windows it hides on, which is the last one +PARK = "komorebi-park" class KdePlatform: @@ -50,6 +52,13 @@ class KdePlatform: return None return OsWindow(client.title, self.kde.rect(wid), self.kde.active() == wid) + def hidden(self, client: Client) -> bool: + names = self.kde.desktop_names() + wid = self.kde.wid(client.title) + if PARK not in names or wid == "": + return False + return self.kde.desk(wid) == names.index(PARK) + 1 + 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 b3826c1a..6bda5c67 100644 --- a/nix/tests/vm/lib/tests/fake_platform.py +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -20,8 +20,8 @@ class FakePlatform: tolerance = 24 def __init__(self, *, moves: bool = True, focus_follows_move: bool = True) -> None: - self.order: list[str] = [] - """The titles in the order of the tiles.""" + 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.moves = moves self.focus_follows_move = focus_follows_move @@ -32,27 +32,43 @@ class FakePlatform: def start_komorebi(self) -> None: pass + @property + def order(self) -> list[str]: + """The titles in view, in the order of the tiles.""" + return [stack[0] for stack in self.containers] + def spawn(self, title: str) -> Client: - self.order.append(title) + self.containers.append([title]) return Client(title) + def _container(self, title: str) -> int: + return next(i for i, stack in enumerate(self.containers) if title in stack) + + def hidden(self, client: Client) -> bool: + stack = self.containers[self._container(client.title)] + return stack[0] != client.title + def work_area(self) -> Rect: return Rect(0, 0, 1920, 1080) def activate(self, client: Client) -> None: - self.focus = self.order.index(client.title) + self.focus = self._container(client.title) + stack = self.containers[self.focus] + stack.insert(0, stack.pop(stack.index(client.title))) def screenshot(self, name: str) -> Image.Image: return Image.new("RGB", (4, 4)) def os_window(self, client: Client) -> OsWindow | None: - if client.title not in self.order: - return None - i = self.order.index(client.title) - return OsWindow(client.title, TILES[i], focused=i == self.focus) + i = self._container(client.title) + return OsWindow( + client.title, + TILES[i], + focused=i == self.focus and self.containers[i][0] == client.title, + ) def komorebic(self, *args: str) -> str: - n = len(self.order) + n = len(self.containers) match args: case ("focus", "right"): self.focus = min(self.focus + 1, n - 1) @@ -66,6 +82,17 @@ class FakePlatform: self._swap(min(self.focus + 1, n - 1)) case ("cycle-move", "previous"): self._swap((self.focus - 1) % n) + case ("stack", "left"): + if self.focus > 0: + stack = self.containers.pop(self.focus) + self.focus -= 1 + self.containers[self.focus][:0] = stack + 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 ("unstack",): + stack = self.containers.pop(self.focus) + self.containers[self.focus : self.focus] = [[t] for t in stack] case _: raise AssertionError(f"the fake does not know komorebic {args}") return "" @@ -73,25 +100,31 @@ class FakePlatform: def _swap(self, other: int) -> None: if not self.moves: return - self.order[self.focus], self.order[other] = self.order[other], self.order[self.focus] + self.containers[self.focus], self.containers[other] = ( + self.containers[other], + self.containers[self.focus], + ) if self.focus_follows_move: self.focus = other def state(self) -> Snapshot: - windows = [ - { - "id": i + 1, - "rect": {"left": r.x, "top": r.y, "right": r.w, "bottom": r.h}, - "details": {"title": t, "exe": "", "class": "", "path": ""}, + def window(title: str, tile: Rect) -> dict[str, Any]: + return { + "id": hash(title) % 1000, + "rect": {"left": tile.x, "top": tile.y, "right": tile.w, "bottom": tile.h}, + "details": {"title": title, "exe": "", "class": "", "path": ""}, } - for i, (t, r) in enumerate(zip(self.order, TILES, strict=False)) - ] + workspace = { "name": None, "containers": { "elements": [ - {"id": str(i), "locked": False, "windows": {"elements": [w], "focused": 0}} - for i, w in enumerate(windows) + { + "id": str(i), + "locked": False, + "windows": {"elements": [window(t, TILES[i]) for t in stack], "focused": 0}, + } + for i, stack in enumerate(self.containers) ], "focused": self.focus, }, diff --git a/nix/tests/vm/lib/tests/test_focus_moving.py b/nix/tests/vm/lib/tests/test_focus_moving.py index 57de96e5..d30b7e79 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, tiling +from komorebi_vm.core.scenarios import focus, moving, stacks, tiling from tests.fake_platform import FakePlatform @@ -33,3 +33,12 @@ def test_a_move_that_loses_the_focus_fails() -> None: clients = opened(platform, checks) moving.run(platform, checks, clients) assert any("keeps the focus" in f for f in checks.failures) + + +def test_a_daemon_that_is_right_passes_stacks() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + stacks.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 10 diff --git a/nix/tests/vm/lib/tests/test_snapshot.py b/nix/tests/vm/lib/tests/test_snapshot.py index 893af625..6d6d8aff 100644 --- a/nix/tests/vm/lib/tests/test_snapshot.py +++ b/nix/tests/vm/lib/tests/test_snapshot.py @@ -97,3 +97,12 @@ def test_a_monocle_container_lists_its_windows() -> None: "windows": ring(kfde_window(1, "win1", rect(0, 0, 1920, 1040))), } assert [w.title for w in parse(state).workspace().monocle or []] == ["win1"] + + +def test_the_focused_window_of_a_stack_is_the_one_the_ring_names() -> None: + state = scene(kfde_window, {}, {}) + workspace = state["monitors"]["elements"][0]["workspaces"]["elements"][0] + stack = workspace["containers"]["elements"][1]["windows"] + stack["elements"].append(kfde_window(4, "under", rect(960, 0, 960, 1080))) + stack["focused"] = 1 + assert parse(state).focused_title() == "under" diff --git a/nix/tests/vm/lib/tests/test_tiling.py b/nix/tests/vm/lib/tests/test_tiling.py index 917aa7f1..a5cd2328 100644 --- a/nix/tests/vm/lib/tests/test_tiling.py +++ b/nix/tests/vm/lib/tests/test_tiling.py @@ -32,6 +32,9 @@ class FakePlatform: i = self.opened.index(client.title) return OsWindow(client.title, self.frames[i], focused=False) + def hidden(self, client: Client) -> bool: + return False + def work_area(self) -> Rect: return Rect(0, 0, 1920, 1080) diff --git a/nix/tests/vm/suites/core-e2e/test.py b/nix/tests/vm/suites/core-e2e/test.py index a2bc0871..9a28fb7e 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, tiling +from komorebi_vm.core.scenarios import focus, moving, stacks, tiling from komorebi_vm.kde_platform import KdePlatform W, H = 1920, 1080 @@ -348,7 +348,7 @@ checks.check( ) report(["afterwards"]) -print("== 12. tiling, focus and moving, through the neutral platform") +print("== 12. tiling, focus, moving and stacks, 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")) @@ -357,6 +357,7 @@ platform = KdePlatform(kde, (W, H)) neutral = tiling.run(platform, checks, titles=("neutral1", "neutral2", "neutral3")) focus.run(platform, checks, neutral) moving.run(platform, checks, neutral) +stacks.run(platform, checks, neutral) print("== 13. komorebic stop") kde.komorebic("stop")