diff --git a/docs/dev/vm-tests.md b/docs/dev/vm-tests.md index e3148741..d5a6f8df 100644 --- a/docs/dev/vm-tests.md +++ b/docs/dev/vm-tests.md @@ -153,7 +153,8 @@ the run. daemon, run `komorebic`, read the `Snapshot`, open a window, 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` is the first. A method is added with the first scenario + against it: `tiling`, `focus` and `moving`, 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 sections. diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/focus.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/focus.py new file mode 100644 index 00000000..bef1b7d0 --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/focus.py @@ -0,0 +1,50 @@ +"""The focus moves where komorebic says, and the operating system and the daemon agree on it.""" + +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, positions + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Moves the focus among `clients`, which are tiled windows of the first workspace, each in a + container of its own, with `focus` and `cycle-focus`.""" + first = clients[0].title + before = positions(platform, clients) + + platform.activate(clients[0]) + checks.check( + f"{first} has the focus, and the daemon agrees", + platform.wait_for(lambda: focus_agrees(platform, clients, first)), + ) + + platform.komorebic("focus", "right") + checks.check( + "after 'focus right' the operating system has given the focus to another window", + platform.wait_for(lambda: os_focused(platform, clients) not in ("", first)), + ) + moved_to = os_focused(platform, clients) + checks.check( + "... and the daemon's state focuses the same one", + platform.wait_for(lambda: focus_agrees(platform, clients, moved_to)), + ) + + platform.komorebic("focus", "left") + checks.check( + "'focus left' gives the focus back", + platform.wait_for(lambda: focus_agrees(platform, clients, first)), + ) + + # the next container is whichever follows in the daemon's own order + order = [titles[0] for titles in platform.state().containers() if titles] + after = order[(order.index(first) + 1) % len(order)] + platform.komorebic("cycle-focus", "next") + checks.check( + f"'cycle-focus next' focuses {after}, the next container in the daemon's order", + platform.wait_for(lambda: focus_agrees(platform, clients, after)), + ) + platform.komorebic("cycle-focus", "previous") + checks.check( + "'cycle-focus previous' goes back", + platform.wait_for(lambda: focus_agrees(platform, clients, first)), + ) + checks.check("moving the focus moved no window", positions(platform, clients) == before) diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/moving.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/moving.py new file mode 100644 index 00000000..a2c1bd73 --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/moving.py @@ -0,0 +1,45 @@ +"""Windows swap tiles on `move` and `cycle-move`, and the focus stays with the window.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.support import focus_agrees, positions + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Moves the first of `clients`, which are tiled windows of the first workspace, each in a + container of its own, to the tile on its right and back.""" + first = clients[0].title + platform.activate(clients[0]) + platform.wait_for(lambda: focus_agrees(platform, clients, first)) + before = positions(platform, clients) + + def swapped_with_another() -> bool: + now = positions(platform, clients) + return any( + title != first + and now[first] == before[title] + and now[title] == before[first] + and now[first] != before[first] + for title in before + ) + + platform.komorebic("move", "right") + checks.check( + f"after 'move right' {first} and a neighbor have swapped tiles", + platform.wait_for(swapped_with_another, timeout=20), + ) + checks.check( + f"... and {first} keeps the focus", + platform.wait_for(lambda: focus_agrees(platform, clients, first)), + ) + others = {t: p for t, p in positions(platform, clients).items() if t != first} + checks.check( + "... and the other windows are on tiles of their own", + len(set(others.values()) | {positions(platform, clients)[first]}) == len(clients), + ) + + platform.komorebic("cycle-move", "previous") + checks.check( + "'cycle-move previous' puts every window back", + platform.wait_for(lambda: positions(platform, clients) == before, timeout=20), + ) diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/support.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/support.py new file mode 100644 index 00000000..fe0c0e19 --- /dev/null +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/support.py @@ -0,0 +1,34 @@ +"""What more than one scenario needs of a platform.""" + +from komorebi_vm.core.platform import Client, Platform + + +def os_focused(platform: Platform, clients: list[Client]) -> str: + """The title of the window the operating system says has the focus, among `clients`. + + The empty string when none of them does. + """ + for client in clients: + window = platform.os_window(client) + if window is not None and window.focused: + return window.title + return "" + + +def positions(platform: Platform, clients: list[Client]) -> dict[str, tuple[int, int]]: + """Where the operating system has each window, by title. + + Only the position: a client may round its size to what suits it (a terminal's cells), and + a window that moved is told by where it is. + """ + found = {} + for client in clients: + window = platform.os_window(client) + if window is not None: + found[client.title] = (window.frame.x, window.frame.y) + return found + + +def focus_agrees(platform: Platform, clients: list[Client], title: str) -> bool: + """The operating system and the daemon's state both have `title` focused.""" + return os_focused(platform, clients) == title and platform.state().focused_title() == title diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/tiling.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/tiling.py index bb4111ed..0a9f871a 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/scenarios/tiling.py +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/tiling.py @@ -2,7 +2,7 @@ from komorebi_vm.core.checks import Checks from komorebi_vm.core.geometry import Rect -from komorebi_vm.core.platform import Platform +from komorebi_vm.core.platform import Client, Platform def _where_the_layout_says(frame: Rect, want: Rect, tolerance: int) -> bool: @@ -16,11 +16,12 @@ def _where_the_layout_says(frame: Rect, want: Rect, tolerance: int) -> bool: def run( platform: Platform, checks: Checks, titles: tuple[str, ...] = ("win1", "win2", "win3") -) -> None: +) -> list[Client]: """Opens a window for each title and checks, against the OS's own account of the frames, that the daemon has tiled them. - The daemon must be running, on a workspace with no window, with a tiling layout. + The daemon must be running, on a workspace with no window, with a tiling layout. Returns the + windows it opened, for a scenario that goes on from them. """ clients = [platform.spawn(title) for title in titles] area = platform.work_area() @@ -63,3 +64,4 @@ def run( "the windows share the work area: none takes all of its width", all(f.w < area.w - platform.tolerance for f in frames), ) + return clients diff --git a/nix/tests/vm/lib/tests/fake_platform.py b/nix/tests/vm/lib/tests/fake_platform.py new file mode 100644 index 00000000..b3826c1a --- /dev/null +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -0,0 +1,109 @@ +"""A desktop in memory with a daemon that follows the commands of the scenarios. + +Three tiles in a row, a window in each. `focus`, `cycle-focus`, `move` and `cycle-move` do what the +real daemon does with windows laid out left to right, so that a scenario can be run against a daemon +that is right, and against ones that are wrong in one way. +""" + +from typing import Any + +from PIL import Image + +from komorebi_vm.core.geometry import Rect +from komorebi_vm.core.platform import Client, OsWindow +from komorebi_vm.core.snapshot import Snapshot, parse + +TILES = [Rect(0, 0, 640, 1080), Rect(640, 0, 640, 1080), Rect(1280, 0, 640, 1080)] + + +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.focus = 0 + self.moves = moves + self.focus_follows_move = focus_follows_move + + def wait_for(self, predicate: Any, timeout: float = 10) -> bool: + return bool(predicate()) + + def start_komorebi(self) -> None: + pass + + def spawn(self, title: str) -> Client: + self.order.append(title) + return 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) + + 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) + + def komorebic(self, *args: str) -> str: + n = len(self.order) + match args: + case ("focus", "right"): + self.focus = min(self.focus + 1, n - 1) + case ("focus", "left"): + self.focus = max(self.focus - 1, 0) + case ("cycle-focus", "next"): + self.focus = (self.focus + 1) % n + case ("cycle-focus", "previous"): + self.focus = (self.focus - 1) % n + case ("move", "right"): + self._swap(min(self.focus + 1, n - 1)) + case ("cycle-move", "previous"): + self._swap((self.focus - 1) % n) + case _: + raise AssertionError(f"the fake does not know komorebic {args}") + return "" + + 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] + 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": ""}, + } + 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) + ], + "focused": self.focus, + }, + "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 new file mode 100644 index 00000000..57de96e5 --- /dev/null +++ b/nix/tests/vm/lib/tests/test_focus_moving.py @@ -0,0 +1,35 @@ +"""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 tests.fake_platform import FakePlatform + + +def opened(platform: FakePlatform, checks: Checks): # noqa: ANN201 + return tiling.run(platform, checks) + + +def test_a_daemon_that_is_right_passes_both() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + focus.run(platform, checks, clients) + moving.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 10 + + +def test_a_move_that_moves_nothing_fails() -> None: + platform = FakePlatform(moves=False) + checks = Checks() + clients = opened(platform, checks) + moving.run(platform, checks, clients) + assert any("swapped tiles" in f for f in checks.failures) + + +def test_a_move_that_loses_the_focus_fails() -> None: + platform = FakePlatform(focus_follows_move=False) + checks = Checks() + clients = opened(platform, checks) + moving.run(platform, checks, clients) + assert any("keeps the focus" in f for f in checks.failures) diff --git a/nix/tests/vm/suites/core-e2e/test.py b/nix/tests/vm/suites/core-e2e/test.py index 85e7b116..a2bc0871 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 tiling +from komorebi_vm.core.scenarios import focus, moving, tiling from komorebi_vm.kde_platform import KdePlatform W, H = 1920, 1080 @@ -348,12 +348,15 @@ checks.check( ) report(["afterwards"]) -print("== 12. the same tiling, through the neutral platform") -# the scenario of komorebi_vm.core.scenarios.tiling is what runs on Windows too: it asks the +print("== 12. tiling, focus and moving, 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")) checks.check("the window of the last section is closed", kde.wait_for(lambda: kde.managed() == 0)) -tiling.run(KdePlatform(kde, (W, H)), checks, titles=("neutral1", "neutral2", "neutral3")) +platform = KdePlatform(kde, (W, H)) +neutral = tiling.run(platform, checks, titles=("neutral1", "neutral2", "neutral3")) +focus.run(platform, checks, neutral) +moving.run(platform, checks, neutral) print("== 13. komorebic stop") kde.komorebic("stop")