test_span.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """--screen-span on two outputs: WE makes a stretch group one monitor with the bounding box of its monitors
  2. (wallpaper64.exe 2.8.42 sub_140061B50), so the cursor is in that box's pixels wherever it is (sub_140110630)."""
  3. import json
  4. import os
  5. import signal
  6. import time
  7. import pytest
  8. from test_fractional_scale import POSITION_PROBE
  9. LAYOUTS = {
  10. # name: (outputs as (x, y, scale), pointer positions in the layout)
  11. "side": (((0, 0, 1), (1280, 0, 1)), ((100, 100), (1290, 100), (2550, 710))),
  12. "gap": (((0, 0, 1), (1480, 200, 1)), ((100, 100), (1490, 300), (2750, 910))),
  13. "mixed": (((0, 0, 1), (1280, 0, 2)), ((100, 100), (1290, 100), (1910, 350))),
  14. }
  15. @pytest.fixture
  16. def sway(desktop_factory):
  17. return desktop_factory("sway-dual")
  18. def _swaymsg(sway, *command):
  19. result = sway.run(["swaymsg", *command], sway.extra)
  20. assert result.returncode == 0, result.stderr or result.stdout
  21. return result.stdout
  22. def _layout(sway, outputs):
  23. for index, (x, y, scale) in enumerate(outputs, 1):
  24. _swaymsg(sway, f"output HEADLESS-{index} enable mode 1280x720 pos {x} {y} scale {scale}")
  25. def _engine(sway, engine, log, wallpaper, *extra):
  26. return sway.client([engine["binary"], "--assets-dir", engine["assets"], "--silent", "--fps", "30", *extra,
  27. "--screen-span", "HEADLESS-1,HEADLESS-2", "--bg", str(wallpaper)],
  28. log=log, env={"LWE_SCRIPT_LOG": "1"})
  29. @pytest.mark.parametrize("layout", LAYOUTS)
  30. def test_cursor_is_in_group_pixels(sway, engine, tmp_path, layout):
  31. outputs, points = LAYOUTS[layout]
  32. log = tmp_path / "engine.log"
  33. pointer = sway.virtual_pointer()
  34. process = None
  35. try:
  36. _layout(sway, outputs)
  37. rects = [output["rect"] for output in json.loads(_swaymsg(sway, "-t", "get_outputs", "-r"))]
  38. left, top = min(rect["x"] for rect in rects), min(rect["y"] for rect in rects)
  39. width = max(rect["x"] + rect["width"] for rect in rects)
  40. height = max(rect["y"] + rect["height"] for rect in rects)
  41. scale = max(scale for _, _, scale in outputs)
  42. process = _engine(sway, engine, log, POSITION_PROBE)
  43. time.sleep(8)
  44. for x, y in points:
  45. expected = f"LWE-POS {(x - left) * scale:.1f} {(y - top) * scale:.1f}"
  46. seen = None
  47. for _ in range(10):
  48. assert process.poll() is None, f"the engine exited with {process.returncode}"
  49. for dx in (1, 0):
  50. pointer.stdin.write(f"abs {x + dx} {y} {width} {height}\n")
  51. pointer.stdin.flush()
  52. pointer.stdout.readline()
  53. time.sleep(0.3)
  54. lines = [line for line in log.read_text(errors="replace").splitlines() if "LWE-POS" in line]
  55. seen = lines[-1].strip() if lines else None
  56. if seen == expected:
  57. break
  58. assert seen == expected, f"pointer at {x},{y} in the {layout} layout"
  59. finally:
  60. pointer.stdin.write("quit\n")
  61. pointer.stdin.flush()
  62. if process is not None:
  63. process.terminate()
  64. process.wait(10)
  65. _layout(sway, LAYOUTS["side"][0])
  66. def test_group_survives_hotswap_resize_and_unplug(sway, engine, tmp_path):
  67. _layout(sway, LAYOUTS["side"][0])
  68. log = tmp_path / "engine.log"
  69. control = tmp_path / "control"
  70. process = _engine(sway, engine, log, engine["wallpaper"], "--control-file", str(control))
  71. def spans():
  72. return [line for line in log.read_text(errors="replace").splitlines() if line.startswith("Span ")]
  73. try:
  74. time.sleep(8)
  75. steps = [
  76. ("output HEADLESS-2 mode 1920x1080", "3200x1080 at 0,0, rendered at 1x"),
  77. ("output HEADLESS-2 mode 1280x720 scale 2", "1920x720 at 0,0, rendered at 2x"),
  78. ("output HEADLESS-2 scale 1", "2560x720 at 0,0, rendered at 1x"),
  79. ("output HEADLESS-1 disable", "1280x720 at 1280,0, rendered at 1x"),
  80. ("output HEADLESS-1 enable", "2560x720 at 0,0, rendered at 1x"),
  81. ]
  82. for command, expected in steps:
  83. _swaymsg(sway, command)
  84. time.sleep(4)
  85. assert process.poll() is None, f"the engine exited with {process.returncode} after {command}"
  86. assert spans() and spans()[-1].endswith(expected), f"after {command}: {spans()[-1:]}"
  87. # the group's one wallpaper is replaced on both outputs, the old project must not be read afterwards
  88. control.write_text(f"path={POSITION_PROBE}\n")
  89. os.kill(process.pid, signal.SIGUSR1)
  90. time.sleep(6)
  91. assert process.poll() is None, f"the engine exited with {process.returncode} after the hotswap"
  92. finally:
  93. process.terminate()
  94. process.wait(10)
  95. _layout(sway, LAYOUTS["side"][0])