test_fractional_scale.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import re
  2. import time
  3. from pathlib import Path
  4. import pytest
  5. from conftest import Unsupported
  6. POSITION_PROBE = Path(__file__).parent / "fixtures/posprobe"
  7. # WE 2.8.42 is per monitor DPI aware and sizes its swap chain from the client rect, so it draws every monitor at its
  8. # native pixel size whatever the scaling; on a 1.5x output that takes wp_fractional_scale_v1 + wp_viewporter
  9. SCALE = 1.5
  10. SESSIONS = ["sway", "labwc", "wayfire", "hyprland", "niri", "kde", "gnome"]
  11. # the ones with zwlr_virtual_pointer_manager_v1
  12. POINTER_SESSIONS = ["sway", "hyprland", "niri"]
  13. BUFFER = re.compile(r"create_immed\(new id wl_buffer#\d+, (\d+), (\d+),")
  14. DESTINATION = re.compile(r"wp_viewport#\d+\.set_destination\((\d+), (\d+)\)")
  15. def _set_scale(desktop):
  16. try:
  17. desktop.set_scale(SCALE)
  18. except Unsupported as error:
  19. pytest.skip(str(error))
  20. except Exception:
  21. # some outputs may have changed before the failure, the session is shared with the next tests
  22. desktop.set_scale(1)
  23. raise
  24. @pytest.mark.parametrize("desktop", SESSIONS, indirect=True)
  25. def test_buffer_has_the_output_pixel_size(desktop, engine, tmp_path):
  26. _set_scale(desktop)
  27. log = tmp_path / "engine.log"
  28. process = None
  29. try:
  30. process = desktop.client([engine["binary"], "--assets-dir", engine["assets"], "--screen-root",
  31. desktop.output_names()[0], "--silent", "--fps", "30", engine["wallpaper"]],
  32. log=log, env={"WAYLAND_DEBUG": "1"})
  33. width, height = desktop.size
  34. logical = (round(width / SCALE), round(height / SCALE))
  35. deadline = time.monotonic() + 40
  36. buffers, destinations = [], []
  37. while time.monotonic() < deadline and not (buffers and destinations):
  38. time.sleep(2)
  39. assert process.poll() is None, f"the engine exited with {process.returncode}:\n{log.read_text()[-1500:]}"
  40. text = log.read_text(errors="replace")
  41. buffers = [tuple(map(int, found)) for found in BUFFER.findall(text)]
  42. destinations = [tuple(map(int, found)) for found in DESTINATION.findall(text)]
  43. assert buffers, f"no buffer was attached on {desktop.name}"
  44. assert buffers[-1] == (width, height), f"{desktop.name} at {SCALE}x got {buffers[-1]} buffers"
  45. assert destinations and destinations[-1] == logical, f"viewport destination {destinations[-1:]}"
  46. finally:
  47. if process is not None:
  48. process.terminate()
  49. process.wait(10)
  50. desktop.set_scale(1)
  51. @pytest.mark.parametrize("desktop", POINTER_SESSIONS, indirect=True)
  52. def test_pointer_is_in_buffer_pixels(desktop, engine, tmp_path):
  53. """WE gives scripts the cursor in the window's physical client pixels"""
  54. _set_scale(desktop)
  55. log = tmp_path / "engine.log"
  56. pointer = process = None
  57. try:
  58. pointer = desktop.virtual_pointer()
  59. process = desktop.client([engine["binary"], "--assets-dir", engine["assets"], "--screen-root",
  60. desktop.output_names()[0], "--silent", "--fps", "30", str(POSITION_PROBE)],
  61. log=log, env={"LWE_SCRIPT_LOG": "1"})
  62. width, height = desktop.size
  63. logical = (round(width / SCALE), round(height / SCALE))
  64. expected = f"LWE-POS {width / 4:.1f} {height / 4:.1f}"
  65. deadline = time.monotonic() + 40
  66. seen = []
  67. while time.monotonic() < deadline and expected not in seen:
  68. assert process.poll() is None, f"the engine exited with {process.returncode}:\n{log.read_text()[-1500:]}"
  69. # a quarter of the logical output from the top left, nudged so every round is a motion
  70. for x in (logical[0] // 4 + 1, logical[0] // 4):
  71. pointer.stdin.write(f"abs {x} {logical[1] // 4} {logical[0]} {logical[1]}\n")
  72. pointer.stdin.flush()
  73. pointer.stdout.readline()
  74. time.sleep(0.3)
  75. seen = re.findall(r"LWE-POS [-\d.]+ [-\d.]+", log.read_text(errors="replace"))
  76. assert expected in seen, f"{desktop.name} at {SCALE}x: {seen[-3:]}"
  77. finally:
  78. if pointer is not None:
  79. pointer.stdin.close()
  80. pointer.wait(10)
  81. if process is not None:
  82. process.terminate()
  83. process.wait(10)
  84. desktop.set_scale(1)