test_gnome.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. # Meta.WindowType.DESKTOP
  74. assert state == f'"1,true,false,{width},{height}"'
  75. assert gnome.eval("global.display.get_monitor_in_fullscreen(0)") == "false"
  76. assert gnome.eval("Shell.AppSystem.get_default().get_running().length") == "0"
  77. finally:
  78. process.terminate()
  79. process.wait(10)
  80. @pytest.mark.parametrize("mode", ["maximized", "fullscreen"])
  81. def test_covering_window_pauses_rendering(gnome, engine, tmp_path, mode):
  82. log = tmp_path / "engine.log"
  83. process = _engine(gnome, engine, log)
  84. probe = None
  85. try:
  86. time.sleep(8)
  87. assert _renders(log), "the engine does not render on GNOME"
  88. probe = _window(gnome, mode)
  89. time.sleep(4)
  90. assert not _renders(log), f"still rendering under a {mode} window"
  91. terminate([probe.pid])
  92. probe = None
  93. time.sleep(4)
  94. assert _renders(log), f"rendering did not resume after the {mode} window"
  95. normal = _window(gnome, "normal")
  96. try:
  97. time.sleep(4)
  98. assert _renders(log), "a plain window paused the wallpaper"
  99. finally:
  100. terminate([normal.pid])
  101. finally:
  102. if probe is not None:
  103. terminate([probe.pid])
  104. process.terminate()
  105. process.wait(10)
  106. def test_desktop_menu_opens_over_the_wallpaper(gnome, engine, tmp_path):
  107. process = _engine(gnome, engine, tmp_path / "engine.log")
  108. try:
  109. width, height = gnome.size
  110. gnome.eval(POINTER + f"pointer.notify_absolute_motion(now(), {width // 2}, {height // 2}); 1")
  111. time.sleep(0.5)
  112. gnome.eval("Main.overview.hide(); 1")
  113. time.sleep(8)
  114. # the lab pointer sometimes lags, resend until the Shell has it
  115. target = f"{width // 2},{height // 2}"
  116. for _ in range(20):
  117. gnome.eval(POINTER + f"pointer.notify_absolute_motion(now(), {width // 2}, {height // 2}); 1")
  118. time.sleep(0.25)
  119. if gnome.eval("global.get_pointer().slice(0, 2).join(',')") == f'"{target}"':
  120. break
  121. gnome.shot(tmp_path / "before.png")
  122. gnome.eval(POINTER + "pointer.notify_button(now(), 3, C.ButtonState.PRESSED);"
  123. "pointer.notify_button(now() + 20000, 3, C.ButtonState.RELEASED); 1")
  124. time.sleep(2)
  125. opened = gnome.eval("Main.layoutManager._bgManagers.some(m => m.backgroundActor._backgroundMenu?.isOpen)")
  126. gnome.shot(tmp_path / "after.png")
  127. gnome.eval("Main.layoutManager._bgManagers.forEach(m => m.backgroundActor._backgroundMenu?.close()); 1")
  128. assert opened == "true", "a right click on the wallpaper did not reach GNOME's desktop menu"
  129. assert changed_fraction(tmp_path / "before.png", tmp_path / "after.png") > 0.01
  130. finally:
  131. process.terminate()
  132. process.wait(10)
  133. def test_engine_stops_without_the_extension(gnome, engine, tmp_path):
  134. log = tmp_path / "engine.log"
  135. process = _engine(gnome, engine, log)
  136. try:
  137. time.sleep(6)
  138. assert process.poll() is None
  139. gnome.run(["gnome-extensions", "disable", UUID])
  140. process.wait(10)
  141. assert "GNOME Shell extension went away" in log.read_text(errors="replace")
  142. deadline = time.monotonic() + 10
  143. while gnome.eval(f"!!{WALLPAPER}") != "false" and time.monotonic() < deadline:
  144. time.sleep(0.5)
  145. assert gnome.eval(f"!!{WALLPAPER}") == "false", "the wallpaper window outlived the extension"
  146. refused = _engine(gnome, engine, tmp_path / "refused.log")
  147. code = refused.wait(20)
  148. # a signal exit (abort) fails too
  149. assert 0 < code < 128, f"the engine exited with {code} instead of an error"
  150. assert "without the GNOME Shell extension" in (tmp_path / "refused.log").read_text(errors="replace")
  151. finally:
  152. gnome.run(["gnome-extensions", "enable", UUID])
  153. if process.poll() is None:
  154. process.terminate()
  155. process.wait(10)
  156. def test_wallpaper_survives_a_close_request(gnome, engine, tmp_path):
  157. """Alt+F4 on a focused wallpaper (45 keeps it a normal window) must not remove it"""
  158. log = tmp_path / "engine.log"
  159. process = _engine(gnome, engine, log)
  160. try:
  161. time.sleep(8)
  162. assert _renders(log), "the engine does not render on GNOME"
  163. gnome.eval(f"{WALLPAPER}.delete(global.display.get_current_time_roundtrip()); 1")
  164. time.sleep(2)
  165. assert process.poll() is None, "the engine stopped on a close request"
  166. assert gnome.eval(f"!!{WALLPAPER}") == "true", "the wallpaper window is gone"
  167. assert _renders(log), "the wallpaper stopped rendering after a close request"
  168. finally:
  169. process.terminate()
  170. process.wait(10)
  171. def test_overview_shows_the_live_wallpaper(gnome, engine, tmp_path):
  172. log = tmp_path / "engine.log"
  173. gnome.eval("Main.overview.show(); 1")
  174. time.sleep(3)
  175. gnome.shot(tmp_path / "bare.png")
  176. gnome.eval("Main.overview.hide(); 1")
  177. time.sleep(2)
  178. process = _engine(gnome, engine, log)
  179. try:
  180. time.sleep(8)
  181. gnome.eval("Main.overview.show(); 1")
  182. time.sleep(3)
  183. gnome.shot(tmp_path / "overview.png")
  184. # only the clones keep frame callbacks coming in the overview
  185. assert _renders(log), "the wallpaper stopped rendering in the overview"
  186. assert changed_fraction(tmp_path / "bare.png", tmp_path / "overview.png") > 0.2, \
  187. "the overview still shows GNOME's own background"
  188. finally:
  189. gnome.eval("Main.overview.hide(); 1")
  190. process.terminate()
  191. process.wait(10)
  192. def test_focused_window_pauses_with_pause_on_focus(gnome, engine, tmp_path):
  193. log = tmp_path / "engine.log"
  194. process = _engine(gnome, engine, log, "--pause-on-focus")
  195. probe = None
  196. try:
  197. time.sleep(8)
  198. assert _renders(log), "the engine does not render on GNOME"
  199. probe = _window(gnome, "normal", "focus-probe")
  200. time.sleep(4)
  201. assert not _renders(log), "--pause-on-focus kept rendering while another application had the focus"
  202. terminate([probe.pid])
  203. probe = None
  204. time.sleep(4)
  205. assert _renders(log), "rendering did not resume once the focused window was gone"
  206. finally:
  207. if probe is not None:
  208. terminate([probe.pid])
  209. process.terminate()
  210. process.wait(10)
  211. # DING: own desktop window on GNOME 50, bottom-layer normal one on Ubuntu 46
  212. @pytest.fixture(params=["gnome-ding", "gnome46-ding", "gnome45-ding"])
  213. def ding(request, desktop_factory):
  214. return desktop_factory(request.param)
  215. def test_wallpaper_stays_under_desktop_icons(ding, engine, tmp_path):
  216. log = tmp_path / "engine.log"
  217. process = _engine(ding, engine, log, "--pause-on-focus", wallpaper=CLICK_PROBE)
  218. try:
  219. time.sleep(8)
  220. order = ding.eval("global.display.sort_windows_by_stacking(global.display.list_all_windows())"
  221. ".map(w => w.get_gtk_application_id() || w.get_wm_class()).join('|')").strip('"').split("|")
  222. icons = order.index("com.rastersoft.ding")
  223. wallpaper = order.index("linux-wallpaperengine")
  224. assert wallpaper < icons, f"the wallpaper is above the desktop icons: {order}"
  225. # WE counts the icon view as the desktop: clicks reach the wallpaper, focusing it isn't another app
  226. width, height = ding.size
  227. _point(ding, width // 2, height // 2)
  228. _click(ding)
  229. time.sleep(1.5)
  230. text = log.read_text(errors="replace")
  231. assert "LWE-CLICK down" in text and "LWE-CLICK up" in text, "the click on the desktop never reached the wallpaper"
  232. assert ding.eval("global.display.focus_window?.get_gtk_application_id() ?? ''") == '"com.rastersoft.ding"'
  233. assert _renders(log), "focusing the desktop icons paused the wallpaper"
  234. finally:
  235. process.terminate()
  236. process.wait(10)