test_fractional_scale.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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)