test_split.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. """--screen-slot: several wallpapers side by side on one output, each in its own rect with its own cursor and clicks"""
  2. import json
  3. import os
  4. import re
  5. import signal
  6. import time
  7. import pytest
  8. from PIL import Image
  9. RED, GREEN, BLUE = (255, 0, 0), (0, 255, 0), (0, 0, 255)
  10. @pytest.fixture
  11. def sway(desktop_factory):
  12. return desktop_factory("sway")
  13. def _probe(directory, tag, size, color):
  14. """a solid colour scene of the given projection size that logs its cursor and presses with a tag"""
  15. directory.mkdir(parents=True, exist_ok=True)
  16. width, height = size
  17. script = ("'use strict';\n"
  18. f"export function cursorMove(event) {{ console.log('LWE-POS-{tag} ' + input.cursorScreenPosition.x.toFixed(1)"
  19. " + ' ' + input.cursorScreenPosition.y.toFixed(1)); }\n"
  20. f"export function cursorDown(event) {{ console.log('LWE-DOWN-{tag}'); }}\n"
  21. "export function update(value) { return value; }\n")
  22. scene = {
  23. "version": 3,
  24. "camera": {"center": "0 0 -1", "eye": "0 0 0", "up": "0 1 0"},
  25. "general": {"orthogonalprojection": {"width": width, "height": height}, "clearcolor": "0 0 0",
  26. "bloom": False, "cameraparallax": False},
  27. "objects": [{"id": 1, "name": "probe", "image": "models/util/solidlayer.json", "size": f"{width} {height}",
  28. "origin": f"{width / 2} {height / 2} 0", "color": " ".join(str(c / 255) for c in color),
  29. "visible": True, "alpha": {"script": script, "value": 1.0}}],
  30. }
  31. (directory / "scene.json").write_text(json.dumps(scene))
  32. (directory / "project.json").write_text(json.dumps({"file": "scene.json", "type": "scene", "title": tag,
  33. "general": {"properties": {}}}))
  34. return directory
  35. def _engine(sway, engine, log, *slots, after=(), control=None):
  36. output = sway.output_names()[0]
  37. argv = [engine["binary"], "--assets-dir", engine["assets"], "--silent", "--fps", "30"]
  38. if control is not None:
  39. argv += ["--control-file", str(control)]
  40. for wallpaper in slots:
  41. argv += ["--screen-slot", output, "--bg", str(wallpaper)]
  42. argv += after
  43. return sway.client(argv, log=log, env={"LWE_SCRIPT_LOG": "1"})
  44. def _pixel(sway, tmp_path, x, y):
  45. path = tmp_path / "shot.png"
  46. sway.shot(path)
  47. return Image.open(path).convert("RGB").getpixel((x, y))
  48. def _wait_for_pixel(sway, tmp_path, process, x, y, color, log):
  49. seen = None
  50. for _ in range(20):
  51. assert process.poll() is None, f"the engine exited with {process.returncode}:\n{log.read_text()[-1500:]}"
  52. seen = _pixel(sway, tmp_path, x, y)
  53. if all(abs(a - b) < 8 for a, b in zip(seen, color)):
  54. return
  55. time.sleep(1)
  56. pytest.fail(f"{x},{y} is {seen}, expected {color}")
  57. def _stop(process):
  58. if process is not None:
  59. process.terminate()
  60. process.wait(10)
  61. def test_slots_are_sized_from_the_wallpapers(sway, engine, tmp_path):
  62. # 1080x1920 + 1920x1080 on 1920x1080: the first slot ends at 462
  63. portrait = _probe(tmp_path / "a", "A", (1080, 1920), RED)
  64. landscape = _probe(tmp_path / "b", "B", (1920, 1080), GREEN)
  65. log = tmp_path / "engine.log"
  66. process = _engine(sway, engine, log, portrait, landscape)
  67. try:
  68. _wait_for_pixel(sway, tmp_path, process, 455, 540, RED, log)
  69. _wait_for_pixel(sway, tmp_path, process, 470, 540, GREEN, log)
  70. _wait_for_pixel(sway, tmp_path, process, 1910, 1070, GREEN, log)
  71. finally:
  72. _stop(process)
  73. def test_center_layout_leaves_bars(sway, engine, tmp_path):
  74. first = _probe(tmp_path / "a", "A", (1080, 1920), RED)
  75. second = _probe(tmp_path / "b", "B", (1080, 1920), GREEN)
  76. log = tmp_path / "engine.log"
  77. process = _engine(sway, engine, log, first, second, after=("--split-layout", "center"))
  78. try:
  79. # two 607.5 px slots from 352.5 to 1567.5
  80. _wait_for_pixel(sway, tmp_path, process, 340, 540, (0, 0, 0), log)
  81. _wait_for_pixel(sway, tmp_path, process, 365, 540, RED, log)
  82. _wait_for_pixel(sway, tmp_path, process, 1555, 540, GREEN, log)
  83. _wait_for_pixel(sway, tmp_path, process, 1580, 540, (0, 0, 0), log)
  84. finally:
  85. _stop(process)
  86. def test_portrait_output_stacks_slots(sway, engine, tmp_path):
  87. output = sway.output_names()[0]
  88. first = _probe(tmp_path / "a", "A", (1920, 1080), RED)
  89. second = _probe(tmp_path / "b", "B", (1920, 1080), GREEN)
  90. log = tmp_path / "engine.log"
  91. process = None
  92. try:
  93. sway.run(["swaymsg", f"output {output} transform 90"], sway.extra)
  94. process = _engine(sway, engine, log, first, second)
  95. # 1080x1920 split in two 1080x960 rows, the first on top
  96. _wait_for_pixel(sway, tmp_path, process, 540, 940, RED, log)
  97. _wait_for_pixel(sway, tmp_path, process, 540, 980, GREEN, log)
  98. finally:
  99. _stop(process)
  100. sway.run(["swaymsg", f"output {output} transform normal"], sway.extra)
  101. def test_cursor_and_clicks_belong_to_the_slot_under_the_pointer(sway, engine, tmp_path):
  102. first = _probe(tmp_path / "a", "A", (1920, 1080), RED)
  103. second = _probe(tmp_path / "b", "B", (1920, 1080), GREEN)
  104. log = tmp_path / "engine.log"
  105. process = _engine(sway, engine, log, first, second)
  106. pointer = sway.virtual_pointer()
  107. width, height = sway.size
  108. def send(line):
  109. pointer.stdin.write(line + "\n")
  110. pointer.stdin.flush()
  111. pointer.stdout.readline()
  112. try:
  113. _wait_for_pixel(sway, tmp_path, process, 100, 100, RED, log)
  114. # equal 960 px slots, pointer logged in slot pixels, y from the top
  115. for x, y, tag, expected in ((300, 200, "A", (300, 200)), (1500, 700, "B", (540, 700))):
  116. seen = None
  117. for _ in range(10):
  118. for dx in (1, 0):
  119. send(f"abs {x + dx} {y} {width} {height}")
  120. time.sleep(0.3)
  121. lines = re.findall(rf"LWE-POS-{tag} [-\d.]+ [-\d.]+", log.read_text(errors="replace"))
  122. seen = lines[-1] if lines else None
  123. if seen == f"LWE-POS-{tag} {expected[0]:.1f} {expected[1]:.1f}":
  124. break
  125. assert seen == f"LWE-POS-{tag} {expected[0]:.1f} {expected[1]:.1f}"
  126. send(f"abs 1500 700 {width} {height}")
  127. time.sleep(0.5)
  128. send("button 272 1")
  129. time.sleep(0.5)
  130. send("button 272 0")
  131. time.sleep(1)
  132. text = log.read_text(errors="replace")
  133. assert "LWE-DOWN-B" in text
  134. assert "LWE-DOWN-A" not in text
  135. finally:
  136. pointer.stdin.write("quit\n")
  137. pointer.stdin.flush()
  138. pointer.wait(10)
  139. _stop(process)
  140. def test_hotswap_reaches_only_the_named_slot(sway, engine, tmp_path):
  141. output = sway.output_names()[0]
  142. first = _probe(tmp_path / "a", "A", (1920, 1080), RED)
  143. second = _probe(tmp_path / "b", "B", (1920, 1080), GREEN)
  144. third = _probe(tmp_path / "c", "C", (1920, 1080), BLUE)
  145. control = tmp_path / "control"
  146. log = tmp_path / "engine.log"
  147. process = _engine(sway, engine, log, first, second, control=control)
  148. try:
  149. _wait_for_pixel(sway, tmp_path, process, 1500, 540, GREEN, log)
  150. control.write_text(f"screen={output}#2\npath={third}\n")
  151. os.kill(process.pid, signal.SIGUSR1)
  152. _wait_for_pixel(sway, tmp_path, process, 1500, 540, BLUE, log)
  153. _wait_for_pixel(sway, tmp_path, process, 400, 540, RED, log)
  154. finally:
  155. _stop(process)