test_fractional_scale.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. POINTER_SESSIONS = ["sway", "hyprland", "niri"]
  12. BUFFER = re.compile(r"create_immed\(new id wl_buffer#\d+, (\d+), (\d+),")
  13. DESTINATION = re.compile(r"wp_viewport#\d+\.set_destination\((\d+), (\d+)\)")
  14. def _set_scale(desktop):
  15. try:
  16. desktop.set_scale(SCALE)
  17. except Unsupported as error:
  18. pytest.skip(str(error))
  19. except Exception:
  20. # some outputs may have changed before the failure, the session is shared with the next tests
  21. desktop.set_scale(1)
  22. raise
  23. @pytest.mark.parametrize("desktop", SESSIONS, indirect=True)
  24. def test_buffer_has_the_output_pixel_size(desktop, engine, tmp_path):
  25. _set_scale(desktop)
  26. log = tmp_path / "engine.log"
  27. process = None
  28. try:
  29. process = desktop.client([engine["binary"], "--assets-dir", engine["assets"], "--screen-root",
  30. desktop.output_names()[0], "--silent", "--fps", "30", engine["wallpaper"]],
  31. log=log, env={"WAYLAND_DEBUG": "1"})
  32. width, height = desktop.size
  33. logical = (round(width / SCALE), round(height / SCALE))
  34. deadline = time.monotonic() + 40
  35. buffers, destinations = [], []
  36. while time.monotonic() < deadline and not (buffers and destinations):
  37. time.sleep(2)
  38. assert process.poll() is None, f"the engine exited with {process.returncode}:\n{log.read_text()[-1500:]}"
  39. text = log.read_text(errors="replace")
  40. buffers = [tuple(map(int, found)) for found in BUFFER.findall(text)]
  41. destinations = [tuple(map(int, found)) for found in DESTINATION.findall(text)]
  42. assert buffers, f"no buffer was attached on {desktop.name}"
  43. assert buffers[-1] == (width, height), f"{desktop.name} at {SCALE}x got {buffers[-1]} buffers"
  44. assert destinations and destinations[-1] == logical, f"viewport destination {destinations[-1:]}"
  45. finally:
  46. if process is not None:
  47. process.terminate()
  48. process.wait(10)
  49. desktop.set_scale(1)
  50. @pytest.mark.parametrize("desktop", POINTER_SESSIONS, indirect=True)
  51. def test_pointer_is_in_buffer_pixels(desktop, engine, tmp_path):
  52. """WE gives scripts the cursor in the window's physical client pixels"""
  53. _set_scale(desktop)
  54. log = tmp_path / "engine.log"
  55. pointer = process = None
  56. try:
  57. pointer = desktop.virtual_pointer()
  58. process = desktop.client([engine["binary"], "--assets-dir", engine["assets"], "--screen-root",
  59. desktop.output_names()[0], "--silent", "--fps", "30", str(POSITION_PROBE)],
  60. log=log, env={"LWE_SCRIPT_LOG": "1"})
  61. width, height = desktop.size
  62. logical = (round(width / SCALE), round(height / SCALE))
  63. expected = f"LWE-POS {width / 4:.1f} {height / 4:.1f}"
  64. deadline = time.monotonic() + 40
  65. seen = []
  66. while time.monotonic() < deadline and expected not in seen:
  67. assert process.poll() is None, f"the engine exited with {process.returncode}:\n{log.read_text()[-1500:]}"
  68. # a quarter of the logical output from the top left, nudged so every round is a motion
  69. for x in (logical[0] // 4 + 1, logical[0] // 4):
  70. pointer.stdin.write(f"abs {x} {logical[1] // 4} {logical[0]} {logical[1]}\n")
  71. pointer.stdin.flush()
  72. pointer.stdout.readline()
  73. time.sleep(0.3)
  74. seen = re.findall(r"LWE-POS [-\d.]+ [-\d.]+", log.read_text(errors="replace"))
  75. assert expected in seen, f"{desktop.name} at {SCALE}x: {seen[-3:]}"
  76. finally:
  77. if pointer is not None:
  78. pointer.stdin.close()
  79. pointer.wait(10)
  80. if process is not None:
  81. process.terminate()
  82. process.wait(10)
  83. desktop.set_scale(1)
  84. @pytest.mark.parametrize("desktop", ["sway", "labwc", "niri"], indirect=True)
  85. def test_render_scale_shrinks_the_buffer_only(desktop, engine, tmp_path):
  86. """--render-scale draws below the output's pixels, the viewport destination stays the logical size"""
  87. log = tmp_path / "engine.log"
  88. process = desktop.client([engine["binary"], "--assets-dir", engine["assets"], "--screen-root",
  89. desktop.output_names()[0], "--render-scale", "0.5", "--silent", "--fps", "30",
  90. engine["wallpaper"]], log=log, env={"WAYLAND_DEBUG": "1"})
  91. try:
  92. width, height = desktop.size
  93. deadline = time.monotonic() + 40
  94. buffers, destinations = [], []
  95. while time.monotonic() < deadline and not (buffers and destinations):
  96. time.sleep(2)
  97. assert process.poll() is None, f"the engine exited with {process.returncode}:\n{log.read_text()[-1500:]}"
  98. text = log.read_text(errors="replace")
  99. buffers = [tuple(map(int, found)) for found in BUFFER.findall(text)]
  100. destinations = [tuple(map(int, found)) for found in DESTINATION.findall(text)]
  101. assert buffers and destinations, f"nothing was attached on {desktop.name}"
  102. assert buffers[-1] == (round(width / 2), round(height / 2)), f"buffer {buffers[-1]}"
  103. assert destinations[-1] == (width, height), f"viewport destination {destinations[-1]}"
  104. finally:
  105. process.terminate()
  106. process.wait(10)