test_gnome.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. """GNOME on Wayland via the Shell extension: desktop window, per monitor pause, desktop menu, extension removal."""
  2. import time
  3. from pathlib import Path
  4. import pytest
  5. from conftest import REPO, changed_fraction
  6. from desktops.runner import terminate
  7. UUID = "linux-wallpaperengine@wallpaperenginelover.github.io"
  8. WALLPAPER = "global.get_window_actors().map(a => a.meta_window).find(w => w.get_wm_class() === 'linux-wallpaperengine')"
  9. POINTER = ("const C = imports.gi.Clutter, G = imports.gi.GLib;"
  10. "if (!global._lweLabPointer) global._lweLabPointer = C.get_default_backend().get_default_seat()"
  11. ".create_virtual_device(C.InputDeviceType.POINTER_DEVICE);"
  12. "const pointer = global._lweLabPointer, now = () => G.get_monotonic_time();")
  13. CLICK_PROBE = REPO / "tests/desktops/fixtures/clickprobe"
  14. def _engine(desktop, engine, log, *extra, wallpaper=None):
  15. return desktop.client([engine["binary"], "--assets-dir", engine["assets"], "--screen-root",
  16. desktop.output_names()[0], "--silent", "--fps", "30", *extra,
  17. str(wallpaper or engine["wallpaper"])],
  18. log=log, env={"LWE_FRAME_STATS": "1", "LWE_SCRIPT_LOG": "1"})
  19. def _point(desktop, x, y):
  20. """moves the lab's virtual pointer, again until the Shell has it (it sometimes keeps an older position)"""
  21. # the hot corner at the start position opens the overview
  22. desktop.eval(POINTER + f"pointer.notify_absolute_motion(now(), {x}, {y}); 1")
  23. time.sleep(0.5)
  24. desktop.eval("Main.overview.hide(); 1")
  25. time.sleep(0.5)
  26. for _ in range(20):
  27. desktop.eval(POINTER + f"pointer.notify_absolute_motion(now(), {x}, {y}); 1")
  28. time.sleep(0.25)
  29. if desktop.eval("global.get_pointer().slice(0, 2).join(',')") == f'"{x},{y}"':
  30. return
  31. raise AssertionError(f"the virtual pointer never got to {x},{y}")
  32. def _click(desktop, button=1):
  33. desktop.eval(POINTER + f"pointer.notify_button(now(), {button}, C.ButtonState.PRESSED); 1")
  34. time.sleep(0.3)
  35. desktop.eval(POINTER + f"pointer.notify_button(now(), {button}, C.ButtonState.RELEASED); 1")
  36. def _frame_lines(log):
  37. return Path(log).read_text(errors="replace").count("FRAME-STATS")
  38. def _window(desktop, mode, name=None):
  39. """a GTK probe window, focused like a user would: Mutter maps it without the focus when the session never saw
  40. any input (focus stealing prevention), and WE counts only the focused window as fullscreen"""
  41. process = desktop.spawn(name or f"{mode}-probe", ["python3", str(REPO / "tools/desktops/wayland_window.py"), mode])
  42. title = f"lwe-{mode}-probe"
  43. # Mutter 17 (GNOME 49) drops an activation that comes before the window is shown, so ask until it has the focus
  44. for _ in range(40):
  45. time.sleep(0.25)
  46. if desktop.eval(f"const w = global.display.list_all_windows().find(w => w.get_title() === '{title}');"
  47. "if (w && !w.has_focus()) w.activate(global.display.get_current_time_roundtrip());"
  48. "!!w && w.has_focus()") == "true":
  49. break
  50. return process
  51. def _renders(log, seconds=5):
  52. before = _frame_lines(log)
  53. time.sleep(seconds)
  54. return _frame_lines(log) > before
  55. # 49/50: Meta.Window.set_type, 46-48: helper's Meta.WaylandClient, 45: bottom normal-layer window
  56. @pytest.fixture(params=["gnome", "gnome49", "gnome48", "gnome47", "gnome46", "gnome45"])
  57. def gnome(request, desktop_factory):
  58. return desktop_factory(request.param)
  59. def test_wallpaper_is_a_desktop_window(gnome, engine, tmp_path):
  60. process = _engine(gnome, engine, tmp_path / "engine.log")
  61. try:
  62. time.sleep(8)
  63. assert process.poll() is None, (tmp_path / "engine.log").read_text()[-1500:]
  64. state = gnome.eval(f"const w = {WALLPAPER}; const r = w.get_frame_rect();"
  65. "[w.get_window_type(), w.skip_taskbar, w.is_fullscreen(), r.width, r.height].join(',')")
  66. width, height = gnome.size
  67. if gnome.name == "gnome45":
  68. # 45: normal window hidden from the window lists, kept in the work area
  69. window_type, skip, fullscreen, frame_width, frame_height = state.strip('"').split(",")
  70. assert (window_type, skip, fullscreen, frame_width) == ("0", "true", "false", str(width))
  71. assert int(frame_height) >= height - 64
  72. else:
  73. assert state == f'"1,true,false,{width},{height}"'
  74. assert gnome.eval("global.display.get_monitor_in_fullscreen(0)") == "false"
  75. assert gnome.eval("Shell.AppSystem.get_default().get_running().length") == "0"
  76. finally:
  77. process.terminate()
  78. process.wait(10)
  79. @pytest.mark.parametrize("mode", ["maximized", "fullscreen"])
  80. def test_covering_window_pauses_rendering(gnome, engine, tmp_path, mode):
  81. log = tmp_path / "engine.log"
  82. process = _engine(gnome, engine, log)
  83. probe = None
  84. try:
  85. time.sleep(8)
  86. assert _renders(log), "the engine does not render on GNOME"
  87. probe = _window(gnome, mode)
  88. time.sleep(4)
  89. assert not _renders(log), f"still rendering under a {mode} window"
  90. terminate([probe.pid])
  91. probe = None
  92. time.sleep(4)
  93. assert _renders(log), f"rendering did not resume after the {mode} window"
  94. normal = _window(gnome, "normal")
  95. try:
  96. time.sleep(4)
  97. assert _renders(log), "a plain window paused the wallpaper"
  98. finally:
  99. terminate([normal.pid])
  100. finally:
  101. if probe is not None:
  102. terminate([probe.pid])
  103. process.terminate()
  104. process.wait(10)
  105. def test_desktop_menu_opens_over_the_wallpaper(gnome, engine, tmp_path):
  106. process = _engine(gnome, engine, tmp_path / "engine.log")
  107. try:
  108. width, height = gnome.size
  109. gnome.eval(POINTER + f"pointer.notify_absolute_motion(now(), {width // 2}, {height // 2}); 1")
  110. time.sleep(0.5)
  111. gnome.eval("Main.overview.hide(); 1")
  112. time.sleep(8)
  113. # the lab pointer sometimes lags, resend until the Shell has it
  114. target = f"{width // 2},{height // 2}"
  115. for _ in range(20):
  116. gnome.eval(POINTER + f"pointer.notify_absolute_motion(now(), {width // 2}, {height // 2}); 1")
  117. time.sleep(0.25)
  118. if gnome.eval("global.get_pointer().slice(0, 2).join(',')") == f'"{target}"':
  119. break
  120. gnome.shot(tmp_path / "before.png")
  121. gnome.eval(POINTER + "pointer.notify_button(now(), 3, C.ButtonState.PRESSED);"
  122. "pointer.notify_button(now() + 20000, 3, C.ButtonState.RELEASED); 1")
  123. time.sleep(2)
  124. opened = gnome.eval("Main.layoutManager._bgManagers.some(m => m.backgroundActor._backgroundMenu?.isOpen)")
  125. gnome.shot(tmp_path / "after.png")
  126. gnome.eval("Main.layoutManager._bgManagers.forEach(m => m.backgroundActor._backgroundMenu?.close()); 1")
  127. assert opened == "true", "a right click on the wallpaper did not reach GNOME's desktop menu"
  128. assert changed_fraction(tmp_path / "before.png", tmp_path / "after.png") > 0.01
  129. finally:
  130. process.terminate()
  131. process.wait(10)
  132. def test_engine_stops_without_the_extension(gnome, engine, tmp_path):
  133. log = tmp_path / "engine.log"
  134. process = _engine(gnome, engine, log)
  135. try:
  136. time.sleep(6)
  137. assert process.poll() is None
  138. gnome.run(["gnome-extensions", "disable", UUID])
  139. process.wait(10)
  140. assert "GNOME Shell extension went away" in log.read_text(errors="replace")
  141. deadline = time.monotonic() + 10
  142. while gnome.eval(f"!!{WALLPAPER}") != "false" and time.monotonic() < deadline:
  143. time.sleep(0.5)
  144. assert gnome.eval(f"!!{WALLPAPER}") == "false", "the wallpaper window outlived the extension"
  145. refused = _engine(gnome, engine, tmp_path / "refused.log")
  146. code = refused.wait(20)
  147. assert 0 < code < 128, f"the engine exited with {code} instead of an error"
  148. assert "without the GNOME Shell extension" in (tmp_path / "refused.log").read_text(errors="replace")
  149. finally:
  150. gnome.run(["gnome-extensions", "enable", UUID])
  151. if process.poll() is None:
  152. process.terminate()
  153. process.wait(10)
  154. def test_wallpaper_survives_a_close_request(gnome, engine, tmp_path):
  155. """Alt+F4 on a focused wallpaper (45 keeps it a normal window) must not remove it"""
  156. log = tmp_path / "engine.log"
  157. process = _engine(gnome, engine, log)
  158. try:
  159. time.sleep(8)
  160. assert _renders(log), "the engine does not render on GNOME"
  161. gnome.eval(f"{WALLPAPER}.delete(global.display.get_current_time_roundtrip()); 1")
  162. time.sleep(2)
  163. assert process.poll() is None, "the engine stopped on a close request"
  164. assert gnome.eval(f"!!{WALLPAPER}") == "true", "the wallpaper window is gone"
  165. assert _renders(log), "the wallpaper stopped rendering after a close request"
  166. finally:
  167. process.terminate()
  168. process.wait(10)
  169. def test_overview_shows_the_live_wallpaper(gnome, engine, tmp_path):
  170. log = tmp_path / "engine.log"
  171. gnome.eval("Main.overview.show(); 1")
  172. time.sleep(3)
  173. gnome.shot(tmp_path / "bare.png")
  174. gnome.eval("Main.overview.hide(); 1")
  175. time.sleep(2)
  176. process = _engine(gnome, engine, log)
  177. try:
  178. time.sleep(8)
  179. gnome.eval("Main.overview.show(); 1")
  180. time.sleep(3)
  181. gnome.shot(tmp_path / "overview.png")
  182. # only the clones keep frame callbacks coming in the overview
  183. assert _renders(log), "the wallpaper stopped rendering in the overview"
  184. assert changed_fraction(tmp_path / "bare.png", tmp_path / "overview.png") > 0.2, \
  185. "the overview still shows GNOME's own background"
  186. finally:
  187. gnome.eval("Main.overview.hide(); 1")
  188. process.terminate()
  189. process.wait(10)
  190. def test_focused_window_pauses_with_pause_on_focus(gnome, engine, tmp_path):
  191. log = tmp_path / "engine.log"
  192. process = _engine(gnome, engine, log, "--pause-on-focus")
  193. probe = None
  194. try:
  195. time.sleep(8)
  196. assert _renders(log), "the engine does not render on GNOME"
  197. probe = _window(gnome, "normal", "focus-probe")
  198. time.sleep(4)
  199. assert not _renders(log), "--pause-on-focus kept rendering while another application had the focus"
  200. terminate([probe.pid])
  201. probe = None
  202. time.sleep(4)
  203. assert _renders(log), "rendering did not resume once the focused window was gone"
  204. finally:
  205. if probe is not None:
  206. terminate([probe.pid])
  207. process.terminate()
  208. process.wait(10)
  209. # DING: own desktop window on GNOME 50, bottom-layer normal one on Ubuntu 46
  210. @pytest.fixture(params=["gnome-ding", "gnome49-ding", "gnome48-ding", "gnome47-ding", "gnome46-ding", "gnome45-ding"])
  211. def ding(request, desktop_factory):
  212. return desktop_factory(request.param)
  213. def test_wallpaper_stays_under_desktop_icons(ding, engine, tmp_path):
  214. log = tmp_path / "engine.log"
  215. process = _engine(ding, engine, log, "--pause-on-focus", wallpaper=CLICK_PROBE)
  216. try:
  217. time.sleep(8)
  218. order = ding.eval("global.display.sort_windows_by_stacking(global.display.list_all_windows())"
  219. ".map(w => w.get_gtk_application_id() || w.get_wm_class()).join('|')").strip('"').split("|")
  220. icons = order.index("com.rastersoft.ding")
  221. wallpaper = order.index("linux-wallpaperengine")
  222. assert wallpaper < icons, f"the wallpaper is above the desktop icons: {order}"
  223. # WE counts the icon view as the desktop: clicks reach the wallpaper, focusing it isn't another app
  224. width, height = ding.size
  225. _point(ding, width // 2, height // 2)
  226. _click(ding)
  227. time.sleep(1.5)
  228. text = log.read_text(errors="replace")
  229. assert "LWE-CLICK down" in text and "LWE-CLICK up" in text, "the click on the desktop never reached the wallpaper"
  230. assert ding.eval("global.display.focus_window?.get_gtk_application_id() ?? ''") == '"com.rastersoft.ding"'
  231. assert _renders(log), "focusing the desktop icons paused the wallpaper"
  232. finally:
  233. process.terminate()
  234. process.wait(10)
  235. def _mean_difference(a, b):
  236. import numpy
  237. from PIL import Image
  238. first = numpy.asarray(Image.open(a).convert("RGB"), dtype=numpy.float32)
  239. second = numpy.asarray(Image.open(b).convert("RGB"), dtype=numpy.float32)
  240. return float(numpy.abs(first - second).mean())
  241. @pytest.mark.parametrize("desktop", ["gnome-hdr"], indirect=True)
  242. def test_hdr_output(desktop, engine, tmp_path):
  243. """--hdr on a monitor Mutter runs in HDR: the surface is PQ and composites back to what SDR shows"""
  244. shots = {}
  245. for mode in ("sdr", "hdr"):
  246. log = tmp_path / f"{mode}.log"
  247. process = desktop.client([engine["binary"], "--assets-dir", engine["assets"], "--screen-root",
  248. desktop.output_names()[0], "--silent", "--fps", "30",
  249. *(["--hdr"] if mode == "hdr" else []), engine["wallpaper"]],
  250. log=log, env={"WAYLAND_DEBUG": "1"} if mode == "hdr" else None)
  251. try:
  252. time.sleep(10)
  253. assert process.poll() is None, log.read_text(errors="replace")[-1500:]
  254. shots[mode] = tmp_path / f"{mode}.png"
  255. desktop.shot(shots[mode])
  256. finally:
  257. process.terminate()
  258. process.wait(10)
  259. text = log.read_text(errors="replace")
  260. if mode == "hdr":
  261. assert "runs in HDR, drawing PQ" in text, text[-1500:]
  262. assert "wp_color_management_surface_v1" in text and ".set_image_description(" in text
  263. difference = _mean_difference(shots["sdr"], shots["hdr"])
  264. assert difference < 6, f"the PQ frame composites to a different picture than SDR (mean difference {difference:.1f})"