diff --git a/nix/tests/vm/lib/komorebi_vm/core/platform.py b/nix/tests/vm/lib/komorebi_vm/core/platform.py index 9309d6a8..366e66c4 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/platform.py +++ b/nix/tests/vm/lib/komorebi_vm/core/platform.py @@ -191,7 +191,13 @@ class Platform(Protocol): ... def work_area(self) -> Rect: - """The area of the first display that windows may take.""" + """The area of the first display that windows may take, in the coordinates of the + desktop: the display less what the operating system keeps for itself (a panel, a dock, the + taskbar), as the operating system says and not the daemon. + + It is the whole display only where there is nothing of the kind. A scenario that asks + whether a window is "inside the work area" asks this, so a platform that answers with the + display where the system has a bar makes that check weaker than it says.""" ... def activate(self, client: Client) -> None: diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/maximizing.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/maximizing.py index 66106a09..1f7c6fb9 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/scenarios/maximizing.py +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/maximizing.py @@ -33,6 +33,11 @@ def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: f"after 'toggle-maximize' {target.title} fills the work area", platform.wait_for(fills, timeout=20), ) + window = platform.os_window(target) + checks.check( + "... without going past the work area", + window is not None and window.frame.inside(area), + ) checks.check( "... and the daemon has it as the maximized window", platform.wait_for(lambda: maximized() == target.title, timeout=20), diff --git a/nix/tests/vm/lib/komorebi_vm/core/scenarios/monocle.py b/nix/tests/vm/lib/komorebi_vm/core/scenarios/monocle.py index 4b3c3b65..0137b5d9 100644 --- a/nix/tests/vm/lib/komorebi_vm/core/scenarios/monocle.py +++ b/nix/tests/vm/lib/komorebi_vm/core/scenarios/monocle.py @@ -30,6 +30,11 @@ def run(platform: Platform, checks: Checks, clients: list[Client]) -> None: "in monocle the focused window fills the work area", platform.wait_for(fills_the_work_area, timeout=20), ) + window = platform.os_window(last) + checks.check( + "... without going past the work area", + window is not None and window.frame.inside(area), + ) checks.check( "... the daemon's state has it as the monocle container", platform.wait_for( diff --git a/nix/tests/vm/lib/komorebi_vm/kde.py b/nix/tests/vm/lib/komorebi_vm/kde.py index 38248154..4a13f08a 100644 --- a/nix/tests/vm/lib/komorebi_vm/kde.py +++ b/nix/tests/vm/lib/komorebi_vm/kde.py @@ -5,6 +5,7 @@ import re import shlex import time from collections.abc import Callable +from itertools import count from typing import Any from PIL import Image @@ -12,6 +13,9 @@ from PIL import Image from komorebi_vm.core.geometry import Rect from komorebi_vm.machine import Machine +# a KWin script is told apart by its name in the journal, so each probe has a name of its own +_PROBES = count(1) + class Outputs: """The outputs of a KWin session, as `kscreen-doctor` lists and changes them. @@ -698,6 +702,44 @@ class Kde: ) return [line for line in self.kwin_reports(name) if line != "done"] + def work_area(self) -> Rect | None: + """The area of the first output that windows may take, as KWin has it (its placement + area: the output less the panels), or `None` when KWin does not say. + + The first output is the leftmost one. It is the area of the desktop the output shows, as + the daemon's bridge reads it. A KWin script asks, because `kdotool` has no call for it. + """ + source = """ + var first = null; + for (var i = 0; i < workspace.screens.length; i++) { + var s = workspace.screens[i]; + if (first === null || s.geometry.x < first.geometry.x + || (s.geometry.x === first.geometry.x && s.geometry.y < first.geometry.y)) { + first = s; + } + } + if (first !== null) { + var a = workspace.clientArea( + KWin.PlacementArea, first, workspace.currentDesktopForScreen(first)); + report("area", Math.round(a.x), Math.round(a.y), Math.round(a.width), + Math.round(a.height)); + } + report("done"); + """ + try: + lines = self.kwin_script(f"work-area-{next(_PROBES)}", source) + except AssertionError: + return None + for line in lines: + words = line.split() + if words[:1] == ["area"] and len(words) == 5: + try: + x, y, w, h = (int(word) for word in words[1:]) + except ValueError: + return None + return Rect(x, y, w, h) if w > 0 and h > 0 else None + return None + def kwin_reports(self, name: str) -> list[str]: """Every line the KWin script `name` has reported so far ([`Kde.kwin_script`][]).""" log = self.machine.execute(f"journalctl _UID={self.uid} --no-pager -o cat")[1] diff --git a/nix/tests/vm/lib/komorebi_vm/kde_platform.py b/nix/tests/vm/lib/komorebi_vm/kde_platform.py index 868aa1c6..3382a878 100644 --- a/nix/tests/vm/lib/komorebi_vm/kde_platform.py +++ b/nix/tests/vm/lib/komorebi_vm/kde_platform.py @@ -180,7 +180,14 @@ class KdePlatform: self.kde.sh(f"kdotool windowmove {self.kde.wid(client.title)} {x} {y}") def work_area(self) -> Rect: - return Rect(0, 0, *self.size) + """What KWin says windows may take of the first output, which is the output less the + panels and docks the shell has made (a headless session has none, so it is all of it). + + Where KWin does not answer, the first output whole: a scenario then checks less, and does + not fail for it. + """ + area = self.kde.work_area() + return area if area is not None else Rect(0, 0, *self.size) def minimize(self, client: Client) -> None: self.kde.minimize(self.kde.wid(client.title)) diff --git a/nix/tests/vm/lib/tests/fake_platform.py b/nix/tests/vm/lib/tests/fake_platform.py index 98e26c5f..583b1472 100644 --- a/nix/tests/vm/lib/tests/fake_platform.py +++ b/nix/tests/vm/lib/tests/fake_platform.py @@ -14,7 +14,7 @@ from komorebi_vm.core.platform import Client, OsWindow from komorebi_vm.core.scenarios.commands import PRINTED from komorebi_vm.core.snapshot import Snapshot, parse -TILES = [Rect(0, 0, 640, 1080), Rect(640, 0, 640, 1080), Rect(1280, 0, 640, 1080)] +SCREEN = Rect(0, 0, 1920, 1080) def _place_index(place: str, count: int) -> int: @@ -60,8 +60,18 @@ class FakePlatform: follows_pointer: bool = True, reloads: bool = True, retiles_on_unlock: bool = True, + panel: int = 0, + ignores_panel: bool = False, + second: Rect | None = None, ) -> None: - self.tiles = list(TILES) + self.panel = panel + """The height of a panel along the bottom of the first display, which the work area leaves + out.""" + self.ignores_panel = ignores_panel + """A daemon that tiles on the whole display, under the panel.""" + self.second = second + """The rectangle of the second display, when it is not one of the first's size.""" + self.tiles = self._base() self.has_pointer = pointer self.follows_pointer = follows_pointer self.spaces: list[list[list[str]]] = [[], [], []] @@ -108,6 +118,21 @@ class FakePlatform: self.moves = moves self.focus_follows_move = focus_follows_move + @property + def area(self) -> Rect: + """What windows may take of the first display.""" + return Rect(0, 0, SCREEN.w, SCREEN.h - self.panel) + + @property + def tiled_area(self) -> Rect: + """What the daemon tiles on: the work area, or the display for a daemon that ignores the + panel.""" + return SCREEN if self.ignores_panel else self.area + + def _base(self) -> list[Rect]: + a = self.tiled_area + return [Rect(i * a.w // 3, 0, a.w // 3, a.h) for i in range(3)] + def has(self, feature: str) -> bool: return feature == "lock" or (feature == "pointer" and self.has_pointer) @@ -279,7 +304,17 @@ class FakePlatform: raise KeyError(title) def displays(self) -> list[Rect]: - return [Rect(i * 1920, 0, 1920, 1080) for i in range(self.display_count)] + return [SCREEN, *self._others()][: self.display_count] + + def _others(self) -> list[Rect]: + """The displays after the first, side by side to its right.""" + found = [] + x = SCREEN.w + for n in range(1, max(self.display_count, 1)): + size = self.second if self.second is not None and n == 1 else SCREEN + found.append(Rect(x, 0, size.w, size.h)) + x += size.w + return found def add_display(self) -> None: self.display_count += 1 @@ -312,19 +347,21 @@ class FakePlatform: def _relayout(self) -> None: """The tiles of each workspace share the work area in equal columns.""" - self.tiles = list(TILES) + base = self._base() + self.tiles = list(base) n = len(self.containers) if n: + area = self.tiled_area w, c = self.workspace_padding, self.container_padding - width = (1920 - 2 * w) // n + width = (area.w - 2 * w) // n self.tiles = [ - Rect(w + i * width + c, w + c, width - 2 * c, 1080 - 2 * w - 2 * c) + Rect(w + i * width + c, w + c, width - 2 * c, area.h - 2 * w - 2 * c) for i in range(n) - ] + list(TILES[n:]) + ] + list(base[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:] - ) + self.tiles = [ + Rect(area.w - t.x - t.w, t.y, t.w, t.h) for t in self.tiles[:n] + ] + list(base[n:]) def _container(self, title: str) -> int: return self._find(title)[1] @@ -339,7 +376,7 @@ class FakePlatform: return s != self.current or self.spaces[s][i][0] != client.title or away def work_area(self) -> Rect: - return Rect(0, 0, 1920, 1080) + return self.area def minimize(self, client: Client) -> None: self._find_remove(client.title) @@ -364,7 +401,7 @@ class FakePlatform: def os_window(self, client: Client) -> OsWindow | None: if client.title == self.maximized: - return OsWindow(client.title, Rect(0, 0, 1920, 1080), focused=True) + return OsWindow(client.title, self.tiled_area, 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: @@ -376,15 +413,19 @@ class FakePlatform: except KeyError: return None if self.monocle == client.title: - return OsWindow(client.title, Rect(0, 0, 1920, 1080), focused=True) + return OsWindow(client.title, self.tiled_area, focused=True) focused = ( s == self.current and i == self.focuses[s] and self.spaces[s][i][0] == client.title ) tile = self.tiles[i] - shift = 1920 * self.display_of.get(client.title, 0) + elsewhere = 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) + if elsewhere: + d = self.displays()[elsewhere] + frame = Rect(d.x + tile.x, d.y + tile.y, min(tile.w, d.w), min(tile.h, d.h)) + return OsWindow(client.title, frame, focused=focused) + return OsWindow(client.title, tile, focused=focused) def komorebic_status(self, *args: str) -> tuple[int, str]: """The fake's answer as a real `komorebic` gives it: exit 0, or 2 for what it lacks.""" @@ -556,7 +597,7 @@ class FakePlatform: floats = self.floats if current else [] monocle = None if current and self.monocle is not None: - whole = Rect(0, 0, 1920, 1080) + whole = self.tiled_area windows = {"elements": [window(self.monocle, whole)], "focused": 0} monocle = {"id": "m", "locked": False, "windows": windows} return { @@ -577,9 +618,7 @@ class FakePlatform: }, "monocle_container": monocle, "maximized_window": ( - window(self.maximized, Rect(0, 0, 1920, 1080)) - if current and self.maximized - else None + window(self.maximized, self.tiled_area) if current and self.maximized else None ), "floating_windows": { "elements": [window(t, Rect(300, 300, 600, 400)) for t in floats], @@ -594,7 +633,8 @@ class FakePlatform: monitor = { "id": 1, - "size": rect(Rect(0, 0, 1920, 1080)), + "size": rect(SCREEN), + "work_area_size": rect(self.area), "workspaces": { "elements": [ workspace(i, c, f, i == self.current) @@ -606,7 +646,7 @@ class FakePlatform: extra = [ { "id": n + 1, - "size": rect(Rect(n * 1920, 0, 1920, 1080)), + "size": rect(self._others()[n - 1]), "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/nix/tests/vm/lib/tests/test_focus_moving.py b/nix/tests/vm/lib/tests/test_focus_moving.py index f14fc691..1ace2536 100644 --- a/nix/tests/vm/lib/tests/test_focus_moving.py +++ b/nix/tests/vm/lib/tests/test_focus_moving.py @@ -781,3 +781,49 @@ def test_focus_follows_mouse_is_skipped_without_a_pointer() -> None: base = checks.passed focus_follows_mouse.run(platform, checks, clients[0], clients[1]) assert checks.failed == 0 and checks.passed == base + + +def test_a_daemon_that_tiles_inside_the_work_area_passes_with_a_panel() -> None: + platform = FakePlatform(panel=40) + checks = Checks() + clients = opened(platform, checks) + for scenario in (monocle, maximizing, layouts, floating, flipping, closing): + if scenario is closing: + closing.run(platform, checks, clients, clients[0]) + else: + scenario.run(platform, checks, clients) + assert platform.work_area().h == 1040 + assert checks.failed == 0, checks.failures + assert checks.passed > 20 + + +def test_a_daemon_that_tiles_under_the_panel_fails_tiling() -> None: + platform = FakePlatform(panel=40, ignores_panel=True) + checks = Checks() + opened(platform, checks) + assert any("inside the work area" in f for f in checks.failures) + + +def test_a_window_that_is_maximized_past_the_work_area_fails_maximizing() -> None: + platform = FakePlatform(panel=40, ignores_panel=True) + checks = Checks() + clients = [platform.spawn(t) for t in ("a", "b", "c")] + maximizing.run(platform, checks, clients) + assert any("without going past the work area" in f for f in checks.failures) + + +def test_a_window_in_monocle_past_the_work_area_fails_monocle() -> None: + platform = FakePlatform(panel=40, ignores_panel=True) + checks = Checks() + clients = [platform.spawn(t) for t in ("a", "b", "c")] + monocle.run(platform, checks, clients) + assert any("without going past the work area" in f for f in checks.failures) + + +def test_a_second_display_of_another_size_passes_hotplug() -> None: + platform = FakePlatform(second=Rect(0, 0, 1280, 800)) + checks = Checks() + clients = opened(platform, checks) + hotplug.run(platform, checks, clients) + assert platform.displays()[1] == Rect(1920, 0, 1280, 800) + assert checks.failed == 0, checks.failures diff --git a/nix/tests/vm/lib/tests/test_kde_platform.py b/nix/tests/vm/lib/tests/test_kde_platform.py new file mode 100644 index 00000000..e304a173 --- /dev/null +++ b/nix/tests/vm/lib/tests/test_kde_platform.py @@ -0,0 +1,54 @@ +"""KDE as a platform, against a machine that answers from a script.""" + +from itertools import count + +import pytest + +import komorebi_vm.kde as kde_module +from komorebi_vm import Kde, Rect +from komorebi_vm.kde_platform import KdePlatform + +from .conftest import FakeMachine + + +@pytest.fixture(autouse=True) +def probes(monkeypatch: pytest.MonkeyPatch) -> None: + """The first probe a test makes is `work-area-1`.""" + monkeypatch.setattr(kde_module, "_PROBES", count(1)) + + +def kde_with_area(machine: FakeMachine, area: str) -> KdePlatform: + machine.reply("loadScript", output="i 7\n") + machine.reply("journalctl", output=f"{area}KS:work-area-1 done\n") + return KdePlatform(Kde(machine, "main", scale=0.01), (1920, 1080)) + + +def test_the_work_area_is_what_kwin_says_and_not_the_whole_output(machine: FakeMachine) -> None: + platform = kde_with_area(machine, "KS:work-area-1 area 0 0 1920 1040\n") + + assert platform.work_area() == Rect(0, 0, 1920, 1040) + + +def test_the_work_area_is_asked_of_the_first_output_less_its_panels(machine: FakeMachine) -> None: + platform = kde_with_area(machine, "KS:work-area-1 area 0 0 1920 1040\n") + + platform.work_area() + + scripts = [c for c in machine.commands if "KWin.PlacementArea" in c] + assert scripts, "the script that asks KWin for its placement area was never written" + assert "currentDesktopForScreen" in scripts[0] + + +def test_the_work_area_is_the_whole_output_where_kwin_does_not_answer( + machine: FakeMachine, +) -> None: + machine.reply("loadScript", output="i 7\n") + platform = KdePlatform(Kde(machine, "main", scale=0.01), (1920, 1080)) + + assert platform.work_area() == Rect(0, 0, 1920, 1080) + + +def test_the_work_area_is_the_whole_output_where_kwin_says_nonsense(machine: FakeMachine) -> None: + platform = kde_with_area(machine, "KS:work-area-1 area 0 0 many 1040\n") + + assert platform.work_area() == Rect(0, 0, 1920, 1080)