diff --git a/e2e/lib/komorebi_vm/core/platform.py b/e2e/lib/komorebi_vm/core/platform.py index 976cf125..b25fafc8 100644 --- a/e2e/lib/komorebi_vm/core/platform.py +++ b/e2e/lib/komorebi_vm/core/platform.py @@ -68,8 +68,8 @@ class Platform(Protocol): """A machine with a desktop and a daemon to test.""" def has(self, feature: str) -> bool: - """Whether the platform can do what a scenario needs of it (`"pointer"`), so that a - scenario that needs it is skipped, with the reason, where it cannot.""" + """Whether the platform can do what a scenario needs of it (`"pointer"`, `"lock"`), so + that a scenario that needs it is skipped, with the reason, where it cannot.""" ... @property @@ -101,6 +101,12 @@ class Platform(Protocol): """ ... + def rewrite_config(self, config: dict[str, Any]) -> None: + """Writes `config` over the `komorebi.json` that `restart_with` gave the daemon, which + the daemon watches and reloads when it changes. It returns once the file is written; a + scenario waits for the state to follow.""" + ... + def komorebic(self, *args: str) -> str: """Runs `komorebic` with `args` and returns what it printed. Fails if it does.""" ... @@ -114,8 +120,16 @@ class Platform(Protocol): """The daemon's state.""" ... - def spawn(self, title: str) -> Client: - """Opens a window with this title and waits until the platform has it.""" + def spawn(self, title: str, app_id: str | None = None, exe: str | None = None) -> Client: + """Opens a window with this title and waits until the platform has it. + + `app_id` is the class of the window (its app id on Wayland, its window class on Windows) + for a rule that matches by it; the platform's own when there is none. + + `exe` is the name of the program that opens it, without an extension (`e2e-exe` is + `e2e-exe.exe` on Windows and `e2e-exe` on Linux), for a rule that matches by it. The + platform makes a program of that name; a rule's `id` is the same on every platform. + """ ... def os_window(self, client: Client) -> OsWindow | None: @@ -151,6 +165,26 @@ class Platform(Protocol): """Closes the window, as its close button would.""" ... + def minimize(self, client: Client) -> None: + """Minimizes the window, as its title bar button would. `activate` restores it.""" + ... + + def lock(self) -> None: + """Locks the screen, as the user's lock shortcut would, and returns when it is locked. + Only where `has("lock")`.""" + ... + + def unlock(self) -> None: + """Unlocks the screen, as the user's password would, and returns when it is unlocked. + Only where `has("lock")`.""" + ... + + def move_window(self, client: Client, x: int, y: int) -> None: + """Moves the window so that its top left corner is at (x, y) of the desktop, as the + compositor or the shell would, not as the user's pointer would. The window keeps its + size.""" + ... + def work_area(self) -> Rect: """The area of the first display that windows may take.""" ... diff --git a/e2e/lib/komorebi_vm/core/scenarios/config_reload.py b/e2e/lib/komorebi_vm/core/scenarios/config_reload.py new file mode 100644 index 00000000..d4274679 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/config_reload.py @@ -0,0 +1,76 @@ +"""The daemon reloads its `komorebi.json` when the file changes: names, layout, rules.""" + +from typing import Any + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Platform + + +def _config(names: list[str], layout: str, rule: str | None = None) -> dict[str, Any]: + workspaces: list[dict[str, Any]] = [{"name": name, "layout": layout} for name in names] + if rule is not None: + matcher = {"kind": "Title", "id": rule, "matching_strategy": "StartsWith"} + workspaces[1]["workspace_rules"] = [matcher] + return {"monitors": [{"workspaces": workspaces}]} + + +def _names(platform: Platform) -> list[str | None]: + return [w.name for w in platform.state().monitors[0].workspaces] + + +def run(platform: Platform, checks: Checks) -> None: + """Starts the daemon from a file with two workspaces, a window open, and rewrites the file + three times: with new names and a third workspace and another layout, with a workspace rule + that sends the windows of a title to the second workspace, and with the rule gone. + + The daemon is left running on that file, with the window open on the first workspace, which + is in view. The daemon must have been running, with no window but the one it is given. + """ + stay = platform.spawn("reload-stays") + platform.restart_with(_config(["one", "two"], "BSP")) + checks.check( + "the daemon starts with the workspaces of the file", + _names(platform)[:2] == ["one", "two"], + ) + checks.check( + "the window that was open is managed", + platform.wait_for(lambda: platform.state().workspace_of(stay.title) == 0, timeout=30), + ) + + platform.rewrite_config(_config(["uno", "dos", "tres"], "Columns")) + checks.check( + "a file with other names and one workspace more is loaded", + platform.wait_for(lambda: _names(platform) == ["uno", "dos", "tres"], timeout=30), + ) + checks.check( + "... with the layout the file gives the first workspace", + platform.wait_for(lambda: platform.state().workspace().layout == "Columns", timeout=30), + ) + checks.check( + "... and the window that was open stays where it was", + platform.state().workspace_of(stay.title) == 0 and not platform.hidden(stay), + ) + + platform.rewrite_config(_config(["uno", "dos", "tres"], "Columns", rule="reload-sent")) + sent = platform.spawn("reload-sent-1") + checks.check( + "a rule the file gained sends a window that opens to its workspace", + platform.wait_for(lambda: platform.state().workspace_of(sent.title) == 1, timeout=30), + ) + + platform.rewrite_config(_config(["uno", "dos", "tres"], "Columns")) + # the reload does not say when it is done: the next window shows whether the rule is + platform.wait_for(lambda: False, timeout=3) + later = platform.spawn("reload-sent-2") + checks.check( + "once the file loses the rule, a window of that title stays on the first workspace", + platform.wait_for(lambda: platform.state().workspace_of(later.title) == 0, timeout=30), + ) + + platform.komorebic("change-layout", "bsp") + for client in (sent, later): + platform.close(client) + checks.check( + "the two windows are closed", + platform.wait_for(lambda: platform.state().managed() == 1, timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/flipping.py b/e2e/lib/komorebi_vm/core/scenarios/flipping.py new file mode 100644 index 00000000..54345bf8 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/flipping.py @@ -0,0 +1,46 @@ +"""A flipped layout is the layout mirrored, and flipping it back gives the layout back.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Flips the layout of the first workspace, which has `clients` tiled in a container each, + horizontally and back.""" + area = platform.work_area() + + def centres() -> dict[str, int]: + found = {} + for client in clients: + window = platform.os_window(client) + if window is not None: + found[client.title] = window.frame.x + window.frame.w // 2 + return found + + before = centres() + + def mirrored() -> bool: + now = centres() + return len(now) == len(before) and all( + abs((2 * area.x + area.w - before[t]) - now[t]) <= platform.tolerance for t in before + ) + + platform.komorebic("flip-layout", "horizontal") + checks.check( + "after 'flip-layout horizontal' every window is where the mirror of its place is", + platform.wait_for(mirrored, timeout=20), + ) + checks.check( + "... and every window is where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + + platform.komorebic("flip-layout", "horizontal") + checks.check( + "flipped again, every window is where it was", + platform.wait_for( + lambda: all(abs(centres()[t] - before[t]) <= platform.tolerance for t in before), + timeout=20, + ), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/floating.py b/e2e/lib/komorebi_vm/core/scenarios/floating.py new file mode 100644 index 00000000..dd8d1676 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/floating.py @@ -0,0 +1,56 @@ +"""A window that floats leaves the tiling to the others, and goes back into it when it stops.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled +from komorebi_vm.core.scenarios.support import focus_agrees + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Floats the last of `clients`, which are tiled windows of the first workspace in a + container each, with more than one of them, and then tiles it again.""" + target = clients[-1] + others = clients[:-1] + platform.activate(target) + platform.wait_for(lambda: focus_agrees(platform, clients, target.title)) + + def area(c: Client) -> int: + window = platform.os_window(c) + return window.frame.w * window.frame.h if window is not None else 0 + + def floating() -> list[str]: + return [w.title for w in platform.state().workspace().floating] + + before = sum(area(c) for c in others) + managed = platform.state().managed() + + platform.komorebic("toggle-float") + checks.check( + f"after 'toggle-float' the daemon has {target.title} floating", + platform.wait_for(lambda: floating() == [target.title], timeout=20), + ) + checks.check("... and still manages every window", platform.state().managed() == managed) + checks.check( + "... the others take more room between them", + platform.wait_for(lambda: sum(area(c) for c in others) > before, timeout=20), + ) + frames = [w.frame for c in others if (w := platform.os_window(c)) is not None] + checks.check( + "... inside the work area and not on each other", + all(f.inside(platform.work_area()) for f in frames) + and not any(a.overlaps(b) for i, a in enumerate(frames) for b in frames[i + 1 :]), + ) + checks.check( + f"... {target.title} is still there, and not put away", + platform.os_window(target) is not None and not platform.hidden(target), + ) + + platform.komorebic("toggle-float") + checks.check( + f"after another 'toggle-float' {target.title} floats no more", + platform.wait_for(lambda: floating() == [], timeout=20), + ) + checks.check( + "... and every window is tiled where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/layouts.py b/e2e/lib/komorebi_vm/core/scenarios/layouts.py new file mode 100644 index 00000000..45d2043e --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/layouts.py @@ -0,0 +1,79 @@ +"""Each layout the daemon has tiles the windows it is given: where it says, none over another.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.tiling import _where_the_layout_says + +# the layouts both ports have, but for scrolling, which does not fit the work area +LAYOUTS = ( + "bsp", + "columns", + "rows", + "vertical-stack", + "horizontal-stack", + "ultrawide-vertical-stack", + "grid", + "right-main-vertical-stack", +) + + +def tiled(platform: Platform, clients: list[Client]) -> bool: + """Every window is where the daemon's own layout puts it, as the OS reports it.""" + workspace = platform.state().workspace() + layout = workspace.latest_layout + frames = [platform.os_window(c) for c in clients] + if len(layout) != len(clients) or any(f is None for f in frames): + return False + by_title = {w.title: layout[i] for i, c in enumerate(workspace.containers) for w in c[:1]} + return all( + f is not None + and f.title in by_title + and _where_the_layout_says(f.frame, by_title[f.title], platform.tolerance) + for f in frames + ) + + +def _check_layout(platform: Platform, checks: Checks, clients: list[Client], layout: str) -> None: + label = f"{layout}, {len(clients)} windows" + platform.komorebic("change-layout", layout) + checks.check( + f"{label}: every window is where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + area = platform.work_area() + frames = [w.frame for c in clients if (w := platform.os_window(c)) is not None] + checks.check(f"{label}: ... inside the work area", all(f.inside(area) for f in frames)) + checks.check( + f"{label}: ... and not on each other", + not any(a.overlaps(b) for i, a in enumerate(frames) for b in frames[i + 1 :]), + ) + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Changes the layout of the first workspace to each of `LAYOUTS` with `clients`, which are + tiled windows in a container each, and then with two windows more, which it closes. + + The layout is `bsp` again at the end. + """ + for layout in LAYOUTS: + _check_layout(platform, checks, clients, layout) + + more = [platform.spawn("layouts-extra1"), platform.spawn("layouts-extra2")] + everyone = clients + more + checks.check( + "two windows more are managed", + platform.wait_for(lambda: platform.state().managed() == len(everyone), timeout=20), + ) + for layout in LAYOUTS: + _check_layout(platform, checks, everyone, layout) + + platform.komorebic("change-layout", "bsp") + for client in more: + platform.close(client) + checks.check( + "the two windows are closed and the others tiled again", + platform.wait_for( + lambda: platform.state().managed() == len(clients) and tiled(platform, clients), + timeout=20, + ), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/lifecycle_hidden.py b/e2e/lib/komorebi_vm/core/scenarios/lifecycle_hidden.py new file mode 100644 index 00000000..57adc120 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/lifecycle_hidden.py @@ -0,0 +1,50 @@ +"""A window that was put away on another workspace is not lost when the daemon restarts.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.support import focus_agrees + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Sends the last of `clients`, which are tiled windows of workspace 0, to workspace 1, + stops the daemon and starts it again. + + Where the daemon puts the window afterwards is not asked for, only that it manages every + window again and that the ones in view are inside the work area and clear of each other. + """ + target = clients[-1] + platform.activate(target) + platform.wait_for(lambda: focus_agrees(platform, clients, target.title)) + platform.komorebic("send-to-workspace", "1") + checks.check( + f"{target.title} is put away on workspace 1", + platform.wait_for(lambda: platform.state().containers(1) == [[target.title]], timeout=20), + ) + + platform.komorebic("stop") + checks.check( + "the daemon stops answering", + platform.wait_for(lambda: platform.komorebic_status("state")[0] != 0, timeout=20), + ) + platform.start_komorebi() + checks.check( + "the daemon manages every window again, the one that was put away too", + platform.wait_for(lambda: platform.state().managed() >= len(clients), timeout=30), + ) + checks.check( + f"... and it has {target.title} on a workspace", + platform.wait_for(lambda: platform.state().workspace_of(target.title) is not None), + ) + area = platform.work_area() + frames = [ + w.frame + for c in clients + if not platform.hidden(c) and (w := platform.os_window(c)) is not None + ] + checks.check( + "... the windows in view are inside the work area", all(f.inside(area) for f in frames) + ) + checks.check( + "... and not on each other", + not any(a.overlaps(b) for i, a in enumerate(frames) for b in frames[i + 1 :]), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/locking.py b/e2e/lib/komorebi_vm/core/scenarios/locking.py new file mode 100644 index 00000000..b17d82f7 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/locking.py @@ -0,0 +1,55 @@ +"""The screen is locked and unlocked: the lock screen is not tiled, nothing the daemon does while +it is locked changes the layout, and the desktop is put right when it is unlocked.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled + + +def _focus_agrees(platform: Platform, clients: list[Client]) -> bool: + """The window the daemon says has the focus is the one the operating system says does.""" + workspace = platform.state().workspace() + if not workspace.containers: + return False + stack = workspace.containers[workspace.focused_container] + top = stack[workspace.focused_windows[workspace.focused_container]].title + return any( + (w := platform.os_window(c)) is not None and w.focused and w.title == top for c in clients + ) + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Locks the screen with `clients` tiled windows on the first workspace, moves the first + while it is locked, unlocks, and checks the windows are in their tiles and the focus is + agreed on. + + The daemon must be running, with only `clients`, tiled. Needs `has("lock")`. + """ + checks.check( + "the windows are tiled before the lock", + platform.wait_for(lambda: tiled(platform, clients), timeout=30), + ) + before = [w.frame for c in clients if (w := platform.os_window(c)) is not None] + + platform.lock() + checks.check( + "while the screen is locked the daemon has the same windows: the lock screen is not one", + platform.state().managed() == len(clients), + ) + platform.move_window(clients[0], 400, 300) + after = [platform.os_window(c) for c in clients] + checks.check( + "the others did not move for it", + all(w is not None and w.frame == f for w, f in zip(after[1:], before[1:], strict=True)), + ) + + platform.unlock() + checks.check( + "once it is unlocked the window that was moved is in its tile again", + platform.wait_for(lambda: tiled(platform, clients), timeout=30), + ) + checks.check("... with the windows the daemon had", platform.state().managed() == len(clients)) + checks.check( + "the daemon's idea of the focus is the operating system's", + platform.wait_for(lambda: _focus_agrees(platform, clients), timeout=30), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/maximizing.py b/e2e/lib/komorebi_vm/core/scenarios/maximizing.py new file mode 100644 index 00000000..66106a09 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/maximizing.py @@ -0,0 +1,53 @@ +"""A maximized window has the work area, with the others left where they are behind it.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled +from komorebi_vm.core.scenarios.support import focus_agrees, positions + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Toggles maximize on the last of `clients`, which are tiled windows of the first workspace, + each in a container of its own, and toggles it off again.""" + target = clients[-1] + others = [c for c in clients if c != target] + platform.activate(target) + platform.wait_for(lambda: focus_agrees(platform, clients, target.title)) + area = platform.work_area() + behind = positions(platform, others) + + def fills() -> bool: + window = platform.os_window(target) + return ( + window is not None + and window.frame.w >= area.w - platform.tolerance + and window.frame.h >= area.h - platform.tolerance + ) + + def maximized() -> str | None: + window = platform.state().workspace().maximized + return window.title if window else None + + platform.komorebic("toggle-maximize") + checks.check( + f"after 'toggle-maximize' {target.title} fills the work area", + platform.wait_for(fills, timeout=20), + ) + checks.check( + "... and the daemon has it as the maximized window", + platform.wait_for(lambda: maximized() == target.title, timeout=20), + ) + checks.check( + "... the others stay where they were", + positions(platform, others) == behind, + ) + + platform.komorebic("toggle-maximize") + checks.check( + "toggled again, the daemon has no maximized window", + platform.wait_for(lambda: maximized() is None, timeout=20), + ) + checks.check( + "... and every window is tiled where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/minimizing.py b/e2e/lib/komorebi_vm/core/scenarios/minimizing.py new file mode 100644 index 00000000..3a03895a --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/minimizing.py @@ -0,0 +1,40 @@ +"""A window that is minimized leaves its room to the others, and takes it back when restored.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Minimizes the first of `clients`, which are tiled windows of the first workspace in a + container each, with more than two of them, and restores it.""" + target = clients[0] + others = clients[1:] + + def area(c: Client) -> int: + window = platform.os_window(c) + return window.frame.w * window.frame.h if window is not None else 0 + + before = sum(area(c) for c in others) + platform.minimize(target) + checks.check( + f"with {target.title} minimized the others take more room between them", + platform.wait_for(lambda: sum(area(c) for c in others) > before, timeout=20), + ) + frames = [w.frame for c in others if (w := platform.os_window(c)) is not None] + checks.check( + "... inside the work area and not on each other", + all(f.inside(platform.work_area()) for f in frames) + and not any(a.overlaps(b) for i, a in enumerate(frames) for b in frames[i + 1 :]), + ) + + platform.activate(target) + checks.check( + f"restored, {target.title} and the others are tiled where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + frames = [w.frame for c in clients if (w := platform.os_window(c)) is not None] + checks.check( + "... and not on each other", + not any(a.overlaps(b) for i, a in enumerate(frames) for b in frames[i + 1 :]), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/padding.py b/e2e/lib/komorebi_vm/core/scenarios/padding.py new file mode 100644 index 00000000..94f249bb --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/padding.py @@ -0,0 +1,81 @@ +"""The padding of a workspace and of its containers moves the windows in from the edges and from +each other.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.geometry import Rect +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled + + +def run( + platform: Platform, + checks: Checks, + clients: list[Client], + configured: tuple[int, int] = (0, 0), +) -> None: + """Raises the workspace padding and then the container padding of the first workspace, which + has `clients` tiled in a container each, and sets them back to what the daemon says they were + (`configured` is the workspace and container padding for a daemon that does not say).""" + state = platform.state().workspace() + workspace = configured[0] if state.workspace_padding is None else state.workspace_padding + container = configured[1] if state.container_padding is None else state.container_padding + print(f" padding of the workspace {workspace}, of its containers {container}") + area = platform.work_area() + + def frames() -> list[Rect]: + return [w.frame for c in clients if (w := platform.os_window(c)) is not None] + + def margin() -> int: + """How far the windows are from the left edge of the work area: the padding of the + workspace and that of the first container.""" + return min(f.x for f in frames()) - area.x + + def room() -> int: + return sum(f.w * f.h for f in frames()) + + def near(value: int, want: int) -> bool: + return abs(value - want) <= 10 + + # what the windows are measured against is what these commands give, not what the config said + platform.komorebic("focused-workspace-padding", str(workspace)) + platform.komorebic("focused-workspace-container-padding", str(container)) + checks.check( + "the windows are inside the work area, tiled where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + start = margin() + print(f" margin {start} with the padding as it is") + base = room() + + platform.komorebic("focused-workspace-padding", str(workspace + 40)) + checks.check( + "a workspace padding 40 px more moves the windows 40 px in from the edge", + platform.wait_for(lambda: near(margin(), start + 40), timeout=20), + ) + checks.check( + "... and they stay where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + + platform.komorebic("focused-workspace-padding", str(workspace)) + platform.komorebic("focused-workspace-container-padding", str(container + 30)) + checks.check( + "a container padding 30 px more leaves the windows less room", + platform.wait_for(lambda: room() < base - 5000, timeout=20), + ) + checks.check( + "... and none is over another, or outside the work area", + all(f.inside(area) for f in frames()) + and not any(a.overlaps(b) for i, a in enumerate(frames()) for b in frames()[i + 1 :]), + ) + + platform.komorebic("focused-workspace-container-padding", str(container)) + checks.check( + "with the padding as it was, the windows are as far from the edge as they were", + platform.wait_for(lambda: near(margin(), start), timeout=20), + ) + print(f" margin {margin()} (was {start}), room {room()} (was {base})") + checks.check( + "... and have the room they had", + platform.wait_for(lambda: abs(room() - base) <= 2000, timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/pausing.py b/e2e/lib/komorebi_vm/core/scenarios/pausing.py new file mode 100644 index 00000000..3ea3c424 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/pausing.py @@ -0,0 +1,39 @@ +"""A paused daemon leaves new windows alone, and an unpaused one takes them up.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled + +# how long a daemon that is paused has to leave a window alone for it to count +QUIET = 3 + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Pauses the daemon, opens a window, unpauses it and opens another, with `clients` tiled + on the first workspace. Closes both.""" + managed = platform.state().managed() + + platform.komorebic("toggle-pause") + paused = platform.spawn("while-paused") + checks.check( + "a window that opens while the daemon is paused is not managed", + not platform.wait_for(lambda: platform.state().managed() > managed, timeout=QUIET), + ) + + platform.komorebic("toggle-pause") + after = platform.spawn("after-the-pause") + checks.check( + "after 'toggle-pause' again the next window is managed", + platform.wait_for(lambda: platform.state().managed() > managed, timeout=20), + ) + checks.check( + "... and every window it had is still tiled where the layout says", + platform.wait_for(lambda: tiled(platform, [*clients, after]), timeout=20), + ) + + platform.close(paused) + platform.close(after) + checks.check( + "the two windows are closed and the daemon has the ones it had", + platform.wait_for(lambda: platform.state().managed() == managed, timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/promoting.py b/e2e/lib/komorebi_vm/core/scenarios/promoting.py new file mode 100644 index 00000000..5058185e --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/promoting.py @@ -0,0 +1,35 @@ +"""A promoted window takes the first tile, and keeps the focus.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled +from komorebi_vm.core.scenarios.support import focus_agrees + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Promotes the last of `clients`, which are tiled windows of the first workspace in a + container each, with more than one of them.""" + target = clients[-1] + platform.activate(target) + platform.wait_for(lambda: focus_agrees(platform, clients, target.title)) + first = platform.state().workspace().latest_layout[0] + + platform.komorebic("promote") + checks.check( + f"after 'promote' the daemon has {target.title} in the first container", + platform.wait_for(lambda: platform.state().containers()[0] == [target.title], timeout=20), + ) + + def on_the_first_tile() -> bool: + window = platform.os_window(target) + return window is not None and (window.frame.x, window.frame.y) == (first.x, first.y) + + checks.check("... and the window is on the first tile", platform.wait_for(on_the_first_tile)) + checks.check( + "... every window is where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + checks.check( + "... and the window still has the focus", + platform.wait_for(lambda: focus_agrees(platform, clients, target.title), timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/resize_commands.py b/e2e/lib/komorebi_vm/core/scenarios/resize_commands.py new file mode 100644 index 00000000..ed1318d0 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/resize_commands.py @@ -0,0 +1,43 @@ +"""The resize commands move an edge of the focused window, with no pointer.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.support import focus_agrees + +# how much wider the window must get for the command to have done something +MIN_STEP = 10 + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Resizes the first of `clients`, which are tiled windows of the first workspace, with + `resize-axis horizontal increase` and `decrease`.""" + target = clients[0] + platform.activate(target) + platform.wait_for(lambda: focus_agrees(platform, clients, target.title)) + + def width() -> int: + window = platform.os_window(target) + return window.frame.w if window is not None else 0 + + def clear() -> bool: + frames = [w.frame for c in clients if (w := platform.os_window(c)) is not None] + area = platform.work_area() + return all(f.inside(area) for f in frames) and not any( + a.overlaps(b) for i, a in enumerate(frames) for b in frames[i + 1 :] + ) + + before = width() + platform.komorebic("resize-axis", "horizontal", "increase") + checks.check( + f"after 'resize-axis horizontal increase' {target.title} is wider", + platform.wait_for(lambda: width() >= before + MIN_STEP, timeout=20), + ) + print(f" width {before} -> {width()}") + checks.check("... and every window is inside the work area, none over another", clear()) + + platform.komorebic("resize-axis", "horizontal", "decrease") + checks.check( + "after 'decrease' it has the width it had", + platform.wait_for(lambda: abs(width() - before) <= platform.tolerance, timeout=20), + ) + checks.check("... and every window is inside the work area, none over another", clear()) diff --git a/e2e/lib/komorebi_vm/core/scenarios/rules_class.py b/e2e/lib/komorebi_vm/core/scenarios/rules_class.py new file mode 100644 index 00000000..bf3fb8c4 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/rules_class.py @@ -0,0 +1,49 @@ +"""Rules that match a window by its class (its app id on Wayland) decide where it goes, or that it +is left alone.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Platform + + +def run(platform: Platform, checks: Checks) -> None: + """Gives the daemon a workspace rule and an ignore rule by class, opens a window of each + class and one of neither, and checks where each went. + + The daemon must be running, on the first workspace, which is the one in view, with no window, + and with workspaces 0 and 1. The rules are cleared afterwards. + """ + platform.komorebic("ensure-workspaces", "0", "2") + platform.komorebic("workspace-rule", "class", "e2e-sent", "0", "1") + platform.komorebic("ignore-rule", "class", "e2e-left") + + sent = platform.spawn("by-class-sent", app_id="e2e-sent") + checks.check( + "a window a class rule matches goes to the workspace the rule names", + platform.wait_for(lambda: platform.state().workspace_of(sent.title) == 1, timeout=20), + ) + checks.check( + "... where it is put away while another workspace is in view", + platform.wait_for(lambda: platform.hidden(sent)), + ) + + left = platform.spawn("by-class-left", app_id="e2e-left") + checks.check( + "a window an ignore rule by class matches is not managed", + not platform.wait_for( + lambda: platform.state().workspace_of(left.title) is not None, timeout=3 + ), + ) + + plain = platform.spawn("by-class-plain", app_id="e2e-other") + checks.check( + "a window of another class stays on the first workspace", + platform.wait_for(lambda: platform.state().workspace_of(plain.title) == 0, timeout=20), + ) + + platform.komorebic("clear-all-workspace-rules") + for client in (sent, left, plain): + platform.close(client) + checks.check( + "the three windows are closed", + platform.wait_for(lambda: platform.state().managed() == 0, timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/rules_exe.py b/e2e/lib/komorebi_vm/core/scenarios/rules_exe.py new file mode 100644 index 00000000..666f372b --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/rules_exe.py @@ -0,0 +1,53 @@ +"""Rules that match a window by the program that opened it decide where it goes, or that it is +left alone. The program is `e2e-exe-sent.exe` on Windows and `e2e-exe-sent` on Linux, and the +rule is the same. + +The names differ from those of `rules_class`: `ignore-rule` drops a rule whose id an earlier +rule has, whatever its kind.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Platform + + +def run(platform: Platform, checks: Checks) -> None: + """Gives the daemon a workspace rule and an ignore rule by exe, opens a window of each + program and one of neither, and checks where each went. + + The daemon must be running, on the first workspace, which is the one in view, with no window, + and with workspaces 0 and 1. The rules are cleared afterwards. + """ + platform.komorebic("ensure-workspaces", "0", "2") + platform.komorebic("workspace-rule", "exe", "e2e-exe-sent", "0", "1") + platform.komorebic("ignore-rule", "exe", "e2e-exe-left") + + sent = platform.spawn("by-exe-sent", exe="e2e-exe-sent") + checks.check( + "a window an exe rule matches goes to the workspace the rule names", + platform.wait_for(lambda: platform.state().workspace_of(sent.title) == 1, timeout=20), + ) + checks.check( + "... where it is put away while another workspace is in view", + platform.wait_for(lambda: platform.hidden(sent)), + ) + + left = platform.spawn("by-exe-left", exe="e2e-exe-left") + checks.check( + "a window an ignore rule by exe matches is not managed", + not platform.wait_for( + lambda: platform.state().workspace_of(left.title) is not None, timeout=3 + ), + ) + + plain = platform.spawn("by-exe-plain", exe="e2e-exe-other") + checks.check( + "a window of another program stays on the first workspace", + platform.wait_for(lambda: platform.state().workspace_of(plain.title) == 0, timeout=20), + ) + + platform.komorebic("clear-all-workspace-rules") + for client in (sent, left, plain): + platform.close(client) + checks.check( + "the three windows are closed", + platform.wait_for(lambda: platform.state().managed() == 0, timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/scrolling.py b/e2e/lib/komorebi_vm/core/scenarios/scrolling.py new file mode 100644 index 00000000..f082748f --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/scrolling.py @@ -0,0 +1,33 @@ +"""In the scrolling layout the window that has the focus is in view, wherever the others are.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled +from komorebi_vm.core.scenarios.support import focus_agrees + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Changes the layout of the first workspace, which has `clients` tiled in a container each, + to scrolling, gives each of them the focus in turn, and changes it back to bsp.""" + area = platform.work_area() + + def in_view(client: Client) -> bool: + window = platform.os_window(client) + return window is not None and window.frame.inside(area) + + platform.komorebic("change-layout", "scrolling") + for client in clients: + platform.activate(client) + checks.check( + f"in scrolling, {client.title} has the focus and is inside the work area", + platform.wait_for( + lambda c=client: focus_agrees(platform, clients, c.title) and in_view(c), + timeout=20, + ), + ) + + platform.komorebic("change-layout", "bsp") + checks.check( + "back in bsp every window is where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/tiling_off.py b/e2e/lib/komorebi_vm/core/scenarios/tiling_off.py new file mode 100644 index 00000000..7517fc73 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/tiling_off.py @@ -0,0 +1,39 @@ +"""A workspace with tiling off leaves its windows where they are, and tiling it again tiles them.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled +from komorebi_vm.core.scenarios.support import positions + + +def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: + """Toggles tiling off on the first workspace, which has `clients` tiled in a container each, + and on again, with a layout change in between that must not move anything.""" + before = positions(platform, clients) + + platform.komorebic("toggle-tiling") + checks.check( + "after 'toggle-tiling' the daemon has the workspace untiled", + platform.wait_for(lambda: not platform.state().workspace().tile, timeout=20), + ) + checks.check("... and no window moved", positions(platform, clients) == before) + platform.komorebic("change-layout", "columns") + checks.check( + "... a layout change does not move them either", + not platform.wait_for(lambda: positions(platform, clients) != before, timeout=3), + ) + + platform.komorebic("toggle-tiling") + checks.check( + "'toggle-tiling' again has the workspace tiled", + platform.wait_for(lambda: platform.state().workspace().tile, timeout=20), + ) + checks.check( + "... and every window is where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + platform.komorebic("change-layout", "bsp") + checks.check( + "... and in bsp again", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) diff --git a/e2e/lib/komorebi_vm/core/scenarios/workspace_moves.py b/e2e/lib/komorebi_vm/core/scenarios/workspace_moves.py new file mode 100644 index 00000000..f63f95b2 --- /dev/null +++ b/e2e/lib/komorebi_vm/core/scenarios/workspace_moves.py @@ -0,0 +1,61 @@ +"""A window moved to a workspace takes the focus there, and the last workspace is one command +away.""" + +from komorebi_vm.core.checks import Checks +from komorebi_vm.core.platform import Client, Platform +from komorebi_vm.core.scenarios.layouts import tiled +from komorebi_vm.core.scenarios.support import focus_agrees + + +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: + """Moves the last of `clients`, which are tiled windows of workspace 0 in a container each, + to workspace 1 with `move-to-workspace`, goes back and forth with `focus-last-workspace`, and + moves it home. Workspace 1 is empty.""" + target = clients[-1] + rest = sorted(c.title for c in clients if c != target) + everyone = sorted(c.title for c in clients) + platform.activate(target) + platform.wait_for(lambda: focus_agrees(platform, clients, target.title)) + + platform.komorebic("move-to-workspace", "1") + checks.check( + "after 'move-to-workspace 1' the window is there, in view, and the others are put away", + platform.wait_for(lambda: _in_view(platform, clients) == [target.title], timeout=20), + ) + checks.check( + "... the daemon has it on workspace 1", + platform.wait_for(lambda: platform.state().containers(1) == [[target.title]]), + ) + checks.check( + "... and it has the focus", + platform.wait_for(lambda: focus_agrees(platform, clients, target.title)), + ) + + platform.komorebic("focus-last-workspace") + checks.check( + "'focus-last-workspace' brings the others back and puts the window away", + platform.wait_for(lambda: _in_view(platform, clients) == rest, timeout=20), + ) + platform.komorebic("focus-last-workspace") + checks.check( + "... and again, it goes back to the window", + platform.wait_for(lambda: _in_view(platform, clients) == [target.title], timeout=20), + ) + + platform.komorebic("move-to-workspace", "0") + checks.check( + "'move-to-workspace 0' has every window in view again", + platform.wait_for(lambda: _in_view(platform, clients) == everyone, timeout=20), + ) + checks.check( + "... tiled where the layout says", + platform.wait_for(lambda: tiled(platform, clients), timeout=20), + ) + checks.check( + "... and workspace 1 is empty", + platform.wait_for(lambda: platform.state().containers(1) == []), + ) diff --git a/e2e/lib/komorebi_vm/core/snapshot.py b/e2e/lib/komorebi_vm/core/snapshot.py index 0c104445..fa3512e0 100644 --- a/e2e/lib/komorebi_vm/core/snapshot.py +++ b/e2e/lib/komorebi_vm/core/snapshot.py @@ -49,6 +49,10 @@ class Workspace: floating: list[Window] latest_layout: list[Rect] """The rectangles the layout last computed, one for each container.""" + workspace_padding: int | None = None + """The padding of the workspace, when the daemon says.""" + container_padding: int | None = None + """The padding of each of its containers, when the daemon says.""" def windows(self) -> list[Window]: """Every window of the workspace: the tiled ones, the monocle ones, the maximized one and @@ -162,6 +166,8 @@ def _workspace(value: dict[str, Any]) -> Workspace: maximized=_window(maximized) if maximized else None, floating=[_window(w) for w in _ring(value["floating_windows"])], latest_layout=[r for r in map(_rect, value.get("latest_layout", [])) if r is not None], + workspace_padding=value.get("workspace_padding"), + container_padding=value.get("container_padding"), ) diff --git a/e2e/lib/tests/fake_platform.py b/e2e/lib/tests/fake_platform.py index d444b54c..88d6095f 100644 --- a/e2e/lib/tests/fake_platform.py +++ b/e2e/lib/tests/fake_platform.py @@ -58,6 +58,8 @@ class FakePlatform: focus_follows_move: bool = True, pointer: bool = True, follows_pointer: bool = True, + reloads: bool = True, + retiles_on_unlock: bool = True, ) -> None: self.tiles = list(TILES) self.has_pointer = pointer @@ -79,13 +81,46 @@ class FakePlatform: self.display_count = 2 self.display_of: dict[str, int] = {} self.forgets_displays = False + self.floats: list[str] = [] + self.class_rules: dict[str, int] = {} + self.ignored_classes: set[str] = set() + self.exe_rules: dict[str, int] = {} + self.ignored_exes: set[str] = set() + self.exes: dict[str, str] = {} + self.names: list[str | None] = [None, None, None] + self.layout_name = "Columns" + self.reloads = reloads + self.locked = False + self.moved: dict[str, Rect] = {} + self.retiles_on_unlock = retiles_on_unlock + self.tiling = True + self.paused = False + self.flipped = False + self.last = 0 + self.maximized: str | None = None + self.workspace_padding = 0 + self.container_padding = 0 + self.minimized: list[str] = [] self.running = True self.placement: Any = None self.moves = moves self.focus_follows_move = focus_follows_move def has(self, feature: str) -> bool: - return feature == "pointer" and self.has_pointer + return feature == "lock" or (feature == "pointer" and self.has_pointer) + + def lock(self) -> None: + self.locked = True + + def unlock(self) -> None: + self.locked = False + if self.retiles_on_unlock: + self.moved.clear() + + def move_window(self, client: Client, x: int, y: int) -> None: + here = self.os_window(client) + assert here is not None + self.moved[client.title] = Rect(x, y, here.frame.w, here.frame.h) @property def pointer(self) -> FakePointer: @@ -109,6 +144,8 @@ class FakePlatform: def start_komorebi(self) -> None: """A new daemon knows nothing: every window is tiled on the first workspace.""" everyone = [t for containers in self.spaces for stack in containers for t in stack] + everyone += self.floats + self.floats = [] self.spaces = [[[t] for t in everyone], [], []] self.focuses = [0, 0, 0] self.current = 0 @@ -133,7 +170,23 @@ class FakePlatform: """The titles in view, in the order of the tiles.""" return [stack[0] for stack in self.containers] - def spawn(self, title: str) -> Client: + def spawn(self, title: str, app_id: str | None = None, exe: str | None = None) -> Client: + self.exes[title] = exe or "" + if exe in self.ignored_exes: + self.unmanaged.append(title) + return Client(title) + if exe in self.exe_rules: + self.spaces[self.exe_rules[exe]].append([title]) + return Client(title) + if app_id in self.ignored_classes: + self.unmanaged.append(title) + return Client(title) + if app_id in self.class_rules: + self.spaces[self.class_rules[app_id]].append([title]) + return Client(title) + if self.paused: + self.unmanaged.append(title) + return Client(title) if title in self.ignored: self.unmanaged.append(title) elif title in self.initial_rules: @@ -174,16 +227,26 @@ class FakePlatform: return space return None - def restart_with(self, config: dict[str, Any]) -> None: - self.start_komorebi() + def rewrite_config(self, config: dict[str, Any]) -> None: + if self.reloads: + self._load(config) + + def _load(self, config: dict[str, Any]) -> None: workspaces = config["monitors"][0]["workspaces"] - self.placement = workspaces[0].get("initial_window_placement_rules") + self.names = [w.get("name") for w in workspaces] + [None] * (3 - len(workspaces)) + self.layout_name = workspaces[0]["layout"].title() self.prefix_rules = [ (rule["id"], index) for index, workspace in enumerate(workspaces) for rule in workspace.get("workspace_rules", []) if rule["matching_strategy"] == "StartsWith" ] + + def restart_with(self, config: dict[str, Any]) -> None: + self.start_komorebi() + self._load(config) + workspaces = config["monitors"][0]["workspaces"] + self.placement = workspaces[0].get("initial_window_placement_rules") for title in [t for stack in self.spaces[0] for t in stack]: target = self._by_prefix(title) if target not in (None, 0): @@ -216,6 +279,12 @@ class FakePlatform: self.display_of[title] = 0 def close(self, client: Client) -> None: + if client.title in self.floats: + self.floats.remove(client.title) + return + if client.title in self.minimized: + self.minimized.remove(client.title) + return if client.title in self.unmanaged: self.unmanaged.remove(client.title) return @@ -234,13 +303,23 @@ class FakePlatform: self.tiles = list(TILES) n = len(self.containers) if n: - width = 1920 // n - self.tiles = [Rect(i * width, 0, width, 1080) for i in range(n)] + list(TILES[n:]) + w, c = self.workspace_padding, self.container_padding + width = (1920 - 2 * w) // n + self.tiles = [ + Rect(w + i * width + c, w + c, width - 2 * c, 1080 - 2 * w - 2 * c) + for i in range(n) + ] + list(TILES[n:]) + if self.flipped: + self.tiles = [Rect(1920 - t.x - t.w, t.y, t.w, t.h) for t in self.tiles[:n]] + list( + TILES[n:] + ) def _container(self, title: str) -> int: return self._find(title)[1] def hidden(self, client: Client) -> bool: + if client.title in self.floats or client.title in self.minimized: + return False if client.title in self.unmanaged: return False s, i = self._find(client.title) @@ -250,7 +329,17 @@ class FakePlatform: def work_area(self) -> Rect: return Rect(0, 0, 1920, 1080) + def minimize(self, client: Client) -> None: + self._find_remove(client.title) + self.minimized.append(client.title) + self.focuses[self.current] = max(0, min(self.focus, len(self.containers) - 1)) + self._relayout() + def activate(self, client: Client) -> None: + if client.title in self.minimized: + self.minimized.remove(client.title) + self.containers.append([client.title]) + self._relayout() self.current, self.focus = self._find(client.title) stack = self.containers[self.focus] stack.insert(0, stack.pop(stack.index(client.title))) @@ -259,6 +348,12 @@ class FakePlatform: return Image.new("RGB", (4, 4)) def os_window(self, client: Client) -> OsWindow | None: + if client.title == self.maximized: + return OsWindow(client.title, Rect(0, 0, 1920, 1080), focused=True) + if client.title in self.minimized: + return OsWindow(client.title, Rect(0, 0, 0, 0), focused=False) + if client.title in self.floats: + return OsWindow(client.title, Rect(300, 300, 600, 400), focused=False) if client.title in self.unmanaged: return OsWindow(client.title, Rect(100, 100, 400, 300), focused=False) try: @@ -272,6 +367,8 @@ class FakePlatform: ) tile = self.tiles[i] shift = 1920 * self.display_of.get(client.title, 0) + if client.title in self.moved: + return OsWindow(client.title, self.moved[client.title], focused=focused) return OsWindow(client.title, Rect(tile.x + shift, tile.y, tile.w, tile.h), focused=focused) def komorebic_status(self, *args: str) -> tuple[int, str]: @@ -333,17 +430,28 @@ class FakePlatform: self.monocle = None if self.monocle else top case ("ensure-workspaces", _, _) | ("clear-all-workspace-rules",): if args[0] == "clear-all-workspace-rules": + self.class_rules.clear() + self.exe_rules.clear() self.rules.clear() self.initial_rules.clear() case ("initial-workspace-rule", "title", title, _, number): self.initial_rules[title] = int(number) case ("workspace-rule", "title", title, _, number): self.rules[title] = int(number) + case ("workspace-rule", "class", name, _, number): + self.class_rules[name] = int(number) + case ("workspace-rule", "exe", name, _, number): + self.exe_rules[name] = int(number) + case ("ignore-rule", "exe", name): + self.ignored_exes.add(name) + case ("ignore-rule", "class", name): + self.ignored_classes.add(name) case ("ignore-rule", "title", title): self.ignored.add(title) case ("move-to-workspace", number): self.komorebic("send-to-workspace", number) - self.current = int(number) + self.last, self.current = self.current, int(number) + self.focus = len(self.containers) - 1 case ("enforce-workspace-rules",): for title, number in self.rules.items(): for s, containers in enumerate(self.spaces): @@ -355,7 +463,43 @@ class FakePlatform: self.spaces[number].append([title]) break case ("focus-workspace", number): - self.current = int(number) + self.last, self.current = self.current, int(number) + case ("focus-last-workspace",): + self.last, self.current = self.current, self.last + case ("focused-workspace-padding", size): + self.workspace_padding = int(size) + self._relayout() + case ("focused-workspace-container-padding", size): + self.container_padding = int(size) + self._relayout() + case ("toggle-maximize",): + self.maximized = None if self.maximized else self.containers[self.focus][0] + case ("resize-axis", "horizontal", way): + d = 50 if way == "increase" else -50 + i = self.focus + if n > 1 and i < n - 1: + a, b = self.tiles[i], self.tiles[i + 1] + self.tiles[i] = Rect(a.x, a.y, a.w + d, a.h) + self.tiles[i + 1] = Rect(b.x + d, b.y, b.w - d, b.h) + case ("promote",): + self.containers.insert(0, self.containers.pop(self.focus)) + self.focus = 0 + case ("flip-layout", "horizontal"): + self.flipped = not self.flipped + self._relayout() + case ("toggle-pause",): + self.paused = not self.paused + case ("toggle-tiling",): + self.tiling = not self.tiling + case ("toggle-float",): + if self.floats: + self.containers.insert(self.focus, [self.floats.pop()]) + else: + title = self.containers[self.focus][0] + self._find_remove(title) + self.floats.append(title) + self.focuses[self.current] = max(0, min(self.focus, len(self.containers) - 1)) + self._relayout() case ("unstack",): stack = self.containers.pop(self.focus) self.containers[self.focus : self.focus] = [[t] for t in stack] @@ -378,20 +522,28 @@ class FakePlatform: return { "id": hash(title) % 1000, "rect": {"left": tile.x, "top": tile.y, "right": tile.w, "bottom": tile.h}, - "details": {"title": title, "exe": "", "class": "", "path": ""}, + "details": { + "title": title, + "exe": self.exes.get(title, ""), + "class": "", + "path": "", + }, } 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, current: bool) -> dict[str, Any]: + def workspace( + index: int, containers: list[list[str]], focus: int, current: bool + ) -> dict[str, Any]: + floats = self.floats if current else [] monocle = None if current and self.monocle is not None: whole = Rect(0, 0, 1920, 1080) windows = {"elements": [window(self.monocle, whole)], "focused": 0} monocle = {"id": "m", "locked": False, "windows": windows} return { - "name": None, + "name": self.names[index], "containers": { "elements": [ { @@ -407,10 +559,19 @@ class FakePlatform: "focused": focus, }, "monocle_container": monocle, - "maximized_window": None, - "floating_windows": {"elements": [], "focused": 0}, - "layout": {"Default": "Columns"}, - "tile": True, + "maximized_window": ( + window(self.maximized, Rect(0, 0, 1920, 1080)) + if current and self.maximized + else None + ), + "floating_windows": { + "elements": [window(t, Rect(300, 300, 600, 400)) for t in floats], + "focused": 0, + }, + "layout": {"Default": self.layout_name}, + "tile": self.tiling, + "workspace_padding": self.workspace_padding, + "container_padding": self.container_padding, "latest_layout": [rect(r) for r in self.tiles[: len(containers)]], } @@ -419,7 +580,7 @@ class FakePlatform: "size": rect(Rect(0, 0, 1920, 1080)), "workspaces": { "elements": [ - workspace(c, f, i == self.current) + workspace(i, c, f, i == self.current) for i, (c, f) in enumerate(zip(self.spaces, self.focuses, strict=True)) ], "focused": self.current, @@ -429,7 +590,7 @@ class FakePlatform: { "id": n + 1, "size": rect(Rect(n * 1920, 0, 1920, 1080)), - "workspaces": {"elements": [workspace([], 0, False)], "focused": 0}, + "workspaces": {"elements": [workspace(0, [], 0, False)], "focused": 0}, } for n in range(1, 1 if self.forgets_displays else self.display_count) ] diff --git a/e2e/lib/tests/test_focus_moving.py b/e2e/lib/tests/test_focus_moving.py index ffad76ed..ccb48b4f 100644 --- a/e2e/lib/tests/test_focus_moving.py +++ b/e2e/lib/tests/test_focus_moving.py @@ -1,21 +1,40 @@ """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.geometry import Rect +from komorebi_vm.core.platform import OsWindow from komorebi_vm.core.scenarios import ( closing, commands, + config_reload, + flipping, + floating, focus, focus_follows_mouse, hotplug, + layouts, lifecycle, + lifecycle_hidden, + locking, + maximizing, + minimizing, monocle, moving, + padding, + pausing, placement, + promoting, resize, + resize_commands, rules, + rules_class, + rules_exe, + scrolling, stacks, startup_rules, tiling, + tiling_off, + workspace_moves, workspaces, ) from tests.fake_platform import FakePlatform @@ -135,6 +154,359 @@ def test_a_daemon_that_does_not_take_its_windows_up_again_fails_lifecycle() -> N assert checks.failed > 0 +def test_a_daemon_that_is_right_passes_layouts() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + layouts.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > len(layouts.LAYOUTS) * 6 + + +def test_a_layout_that_puts_tiles_over_each_other_fails_layouts() -> None: + class Overlapping(FakePlatform): + def komorebic(self, *args: str) -> str: + out = super().komorebic(*args) + if args == ("change-layout", "grid"): + self.tiles = [Rect(0, 0, 1000, 1080)] * len(self.containers) + return out + + platform = Overlapping() + checks = Checks() + clients = opened(platform, checks) + layouts.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_floating() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + floating.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 6 + + +def test_a_float_that_stays_in_the_tiling_fails_floating() -> None: + class Stubborn(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if args == ("toggle-float",) else super().komorebic(*args) + + platform = Stubborn() + checks = Checks() + clients = opened(platform, checks) + floating.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_minimizing() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + minimizing.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 3 + + +def test_a_daemon_that_keeps_the_room_of_a_minimized_window_fails_minimizing() -> None: + class Keeping(FakePlatform): + def _relayout(self) -> None: + pass + + platform = Keeping() + checks = Checks() + clients = opened(platform, checks) + minimizing.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_padding() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + padding.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 5 + + +def test_a_daemon_that_ignores_the_padding_fails_padding() -> None: + class Ignoring(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if args[0].endswith("padding") else super().komorebic(*args) + + platform = Ignoring() + checks = Checks() + clients = opened(platform, checks) + padding.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_maximizing() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + maximizing.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 4 + + +def test_a_daemon_that_does_not_maximize_fails_maximizing() -> None: + class Stubborn(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if args == ("toggle-maximize",) else super().komorebic(*args) + + platform = Stubborn() + checks = Checks() + clients = opened(platform, checks) + maximizing.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_workspace_moves() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + workspace_moves.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 6 + + +def test_a_daemon_that_does_not_follow_the_window_fails_workspace_moves() -> None: + class Staying(FakePlatform): + def komorebic(self, *args: str) -> str: + if args[0] == "move-to-workspace": + return super().komorebic("send-to-workspace", args[1]) + return super().komorebic(*args) + + platform = Staying() + checks = Checks() + clients = opened(platform, checks) + workspace_moves.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_resize_commands() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + resize_commands.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 3 + + +def test_a_daemon_that_does_not_resize_fails_resize_commands() -> None: + class Stubborn(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if args[0] == "resize-axis" else super().komorebic(*args) + + platform = Stubborn() + checks = Checks() + clients = opened(platform, checks) + resize_commands.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_promoting() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + promoting.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 3 + + +def test_a_daemon_that_does_not_promote_fails_promoting() -> None: + class Stubborn(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if args == ("promote",) else super().komorebic(*args) + + platform = Stubborn() + checks = Checks() + clients = opened(platform, checks) + promoting.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_flipping() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + flipping.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 3 + + +def test_a_daemon_that_does_not_flip_fails_flipping() -> None: + class Stubborn(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if args[0] == "flip-layout" else super().komorebic(*args) + + platform = Stubborn() + checks = Checks() + clients = opened(platform, checks) + flipping.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_pausing() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + pausing.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 3 + + +def test_a_daemon_that_manages_while_paused_fails_pausing() -> None: + class Stubborn(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if args == ("toggle-pause",) else super().komorebic(*args) + + platform = Stubborn() + checks = Checks() + clients = opened(platform, checks) + pausing.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_tiling_off() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + tiling_off.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 4 + + +def test_a_daemon_that_ignores_toggle_tiling_fails_tiling_off() -> None: + class Stubborn(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if args == ("toggle-tiling",) else super().komorebic(*args) + + platform = Stubborn() + checks = Checks() + clients = opened(platform, checks) + tiling_off.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_lifecycle_hidden() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + lifecycle_hidden.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 4 + + +def test_a_daemon_that_loses_the_put_away_window_fails_lifecycle_hidden() -> None: + class Forgetful(FakePlatform): + def start_komorebi(self) -> None: + super().start_komorebi() + self.spaces = [[c for c in self.spaces[0] if c != ["win3"]], [], []] + self.unmanaged.append("win3") + + platform = Forgetful() + checks = Checks() + clients = opened(platform, checks) + lifecycle_hidden.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_scrolling() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + scrolling.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 3 + + +def test_a_daemon_that_loses_the_focused_window_off_screen_fails_scrolling() -> None: + class Off(FakePlatform): + def os_window(self, client): # noqa: ANN001, ANN201 + window = super().os_window(client) + if window is not None and window.focused: + return OsWindow(window.title, Rect(5000, 0, 100, 100), focused=True) + return window + + platform = Off() + checks = Checks() + clients = opened(platform, checks) + scrolling.run(platform, checks, clients) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_rules_class() -> None: + platform = FakePlatform() + checks = Checks() + rules_class.run(platform, checks) + assert checks.failed == 0, checks.failures + assert checks.passed > 4 + + +def test_a_daemon_that_ignores_class_rules_fails_rules_class() -> None: + class Deaf(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if "class" in args else super().komorebic(*args) + + platform = Deaf() + checks = Checks() + rules_class.run(platform, checks) + assert checks.failed > 0 + + +def test_a_daemon_that_is_right_passes_rules_exe() -> None: + platform = FakePlatform() + checks = Checks() + rules_exe.run(platform, checks) + assert checks.failed == 0, checks.failures + assert checks.passed > 4 + + +def test_a_daemon_that_ignores_exe_rules_fails_rules_exe() -> None: + class Deaf(FakePlatform): + def komorebic(self, *args: str) -> str: + return "" if "exe" in args else super().komorebic(*args) + + platform = Deaf() + checks = Checks() + rules_exe.run(platform, checks) + assert checks.failed > 0 + + +def test_a_daemon_that_reloads_the_file_passes_config_reload() -> None: + platform = FakePlatform() + checks = Checks() + config_reload.run(platform, checks) + assert checks.failed == 0, checks.failures + assert checks.passed > 6 + + +def test_a_daemon_that_does_not_reload_the_file_fails_config_reload() -> None: + platform = FakePlatform(reloads=False) + checks = Checks() + config_reload.run(platform, checks) + assert checks.failed > 0 + + +def test_a_daemon_that_tiles_again_on_unlock_passes_locking() -> None: + platform = FakePlatform() + checks = Checks() + clients = opened(platform, checks) + locking.run(platform, checks, clients) + assert checks.failed == 0, checks.failures + assert checks.passed > 4 + + +def test_a_daemon_that_does_not_tile_again_on_unlock_fails_locking() -> None: + platform = FakePlatform(retiles_on_unlock=False) + checks = Checks() + clients = opened(platform, checks) + locking.run(platform, checks, clients) + assert checks.failed > 0 + + def test_a_command_the_port_lacks_is_skipped_by_name() -> None: platform = FakePlatform() checks = Checks() diff --git a/e2e/lib/tests/test_tiling.py b/e2e/lib/tests/test_tiling.py index bacc2f74..007ccef2 100644 --- a/e2e/lib/tests/test_tiling.py +++ b/e2e/lib/tests/test_tiling.py @@ -21,7 +21,7 @@ class FakePlatform: def wait_for(self, predicate, timeout: float = 10) -> bool: # noqa: ANN001 return predicate() - def spawn(self, title: str) -> Client: + def spawn(self, title: str, app_id: str | None = None, exe: str | None = None) -> Client: self.opened.append(title) return Client(title) @@ -69,6 +69,21 @@ class FakePlatform: def komorebic(self, *args: str) -> str: return "" + def minimize(self, client: Client) -> None: + pass + + def rewrite_config(self, config: dict) -> None: + pass + + def lock(self) -> None: + pass + + def unlock(self) -> None: + pass + + def move_window(self, client: Client, x: int, y: int) -> None: + pass + def activate(self, client: Client) -> None: pass