test_kde.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. """KDE Plasma: the KWin window-state script pauses only the affected output."""
  2. import time
  3. from pathlib import Path
  4. import pytest
  5. from conftest import REPO
  6. from desktops.runner import terminate
  7. def _engine(desktop, engine, log, output, *extra):
  8. return desktop.client([engine["binary"], "--assets-dir", engine["assets"], "--screen-root", output, "--silent",
  9. "--fps", "30", *extra, engine["wallpaper"]], log=log, env={"LWE_FRAME_STATS": "1"})
  10. def _renders(log, seconds=5):
  11. before = Path(log).read_text(errors="replace").count("FRAME-STATS")
  12. time.sleep(seconds)
  13. return Path(log).read_text(errors="replace").count("FRAME-STATS") > before
  14. def _window(desktop, mode):
  15. return desktop.spawn(f"{mode}-probe", ["python3", str(REPO / "tools/desktops/wayland_window.py"), mode])
  16. @pytest.fixture
  17. def kde(desktop_factory):
  18. return desktop_factory("kde")
  19. @pytest.mark.parametrize("mode", ["maximized", "fullscreen"])
  20. def test_covering_window_pauses_rendering(kde, engine, tmp_path, mode):
  21. log = tmp_path / "engine.log"
  22. process = _engine(kde, engine, log, kde.output_names()[0])
  23. probe = None
  24. try:
  25. time.sleep(8)
  26. assert _renders(log), "the engine does not render on KDE"
  27. probe = _window(kde, mode)
  28. time.sleep(4)
  29. assert not _renders(log), f"still rendering under a {mode} window"
  30. terminate([probe.pid])
  31. probe = None
  32. time.sleep(4)
  33. assert _renders(log), f"rendering did not resume after the {mode} window"
  34. finally:
  35. if probe is not None:
  36. terminate([probe.pid])
  37. process.terminate()
  38. process.wait(10)
  39. @pytest.mark.parametrize("pause_on_focus", [False, True])
  40. def test_focused_window(kde, engine, tmp_path, pause_on_focus):
  41. log = tmp_path / "engine.log"
  42. process = _engine(kde, engine, log, kde.output_names()[0], *(["--pause-on-focus"] if pause_on_focus else []))
  43. probe = None
  44. try:
  45. time.sleep(8)
  46. probe = _window(kde, "normal")
  47. time.sleep(4)
  48. if pause_on_focus:
  49. assert not _renders(log), "--pause-on-focus kept rendering while another application had the focus"
  50. else:
  51. assert _renders(log), "a plain window paused the wallpaper"
  52. finally:
  53. if probe is not None:
  54. terminate([probe.pid])
  55. process.terminate()
  56. process.wait(10)
  57. def test_pause_is_per_output(desktop_factory, engine, tmp_path):
  58. kde = desktop_factory("kde-dual")
  59. outputs = kde.output_names()
  60. assert len(outputs) == 2, outputs
  61. logs = [tmp_path / f"engine-{output}.log" for output in outputs]
  62. processes = [_engine(kde, engine, log, output) for log, output in zip(logs, outputs)]
  63. probe = None
  64. try:
  65. time.sleep(8)
  66. assert all(_renders(log) for log in logs)
  67. probe = _window(kde, "maximized")
  68. time.sleep(4)
  69. rendering = [_renders(log) for log in logs]
  70. assert rendering.count(False) == 1, f"a maximized window should pause exactly its own output: {rendering}"
  71. finally:
  72. if probe is not None:
  73. terminate([probe.pid])
  74. for process in processes:
  75. process.terminate()
  76. process.wait(10)
  77. def test_plasma5(desktop_factory, engine, tmp_path):
  78. """KWin 5.27 runs both scripts (/<id>, not /Scripting/Script<id>); the window script says it needs Plasma 6"""
  79. kde5 = desktop_factory("kde5")
  80. log = tmp_path / "engine.log"
  81. process = _engine(kde5, engine, log, kde5.output_names()[0])
  82. probe = None
  83. try:
  84. time.sleep(8)
  85. assert _renders(log), "the engine does not render on Plasma 5"
  86. text = log.read_text(errors="replace")
  87. assert "could not load its KWin script" not in text
  88. assert "KDE window detection needs Plasma 6" in text
  89. probe = _window(kde5, "maximized")
  90. time.sleep(4)
  91. assert _renders(log), "a maximized window paused the wallpaper without window detection"
  92. finally:
  93. if probe is not None:
  94. terminate([probe.pid])
  95. process.terminate()
  96. process.wait(10)
  97. def test_script_ids_stay_unique_after_an_unload(kde, tmp_path):
  98. """KWin 6.4 numbers scripts by how many are loaded, after an unload the next one got the id of a live script
  99. (KWinScript works around it); this tells whether the KWin of this lab still does that"""
  100. scripts = []
  101. for name in ("a", "b", "c"):
  102. path = tmp_path / f"{name}.js"
  103. path.write_text("print('lwe-id-probe');\n")
  104. scripts.append(path)
  105. def call(method, *args):
  106. result = kde.run(["gdbus", "call", "--session", "--dest", "org.kde.KWin", "--object-path", "/Scripting",
  107. "--method", f"org.kde.kwin.Scripting.{method}", *args])
  108. assert result.returncode == 0, result.stderr
  109. return result.stdout.strip()
  110. def load(path, name):
  111. return int(call("loadScript", str(path), name).strip("(), \n"))
  112. first = load(scripts[0], "lwe-id-a")
  113. second = load(scripts[1], "lwe-id-b")
  114. call("unloadScript", "lwe-id-a")
  115. third = load(scripts[2], "lwe-id-c")
  116. for name in ("lwe-id-b", "lwe-id-c"):
  117. call("unloadScript", name)
  118. if third == second:
  119. pytest.xfail(f"this KWin hands the id {second} of a live script to the next one (ids {first}, {second}, {third})")
  120. def _cursor_scripts(desktop):
  121. return sorted(desktop.run_dir.glob("lwe-cursor-input-*.js"))
  122. def _watch_cursor_calls(desktop, name):
  123. log = desktop.dir / f"{name}.log"
  124. log.unlink(missing_ok=True)
  125. monitor = desktop.client(["dbus-monitor", "--session", "type='method_call',member='OnCursorMoved'"], log=log)
  126. time.sleep(1)
  127. return monitor, log
  128. def _calls(log):
  129. """(x, y) of every OnCursorMoved call dbus-monitor saw"""
  130. lines = Path(log).read_text(errors="replace").splitlines()
  131. calls = []
  132. for index, line in enumerate(lines):
  133. if "member=OnCursorMoved" in line:
  134. numbers = [int(part.split()[-1]) for part in lines[index + 1:index + 3] if part.strip().startswith("int32")]
  135. if len(numbers) == 2:
  136. calls.append(tuple(numbers))
  137. return calls
  138. def _wiggle(desktop, steps=12):
  139. for step in range(steps):
  140. desktop.pointer("move", 300 + step * 20, 300 + (step % 2) * 40)
  141. time.sleep(0.15)
  142. def test_cursor_script_follows_the_pause(kde, engine, tmp_path):
  143. """a long pause unloads the engine's cursor script so KWin sends nothing, resuming loads it again (off the render
  144. thread) and the pointer reaches the engine once more"""
  145. log = tmp_path / "engine.log"
  146. process = _engine(kde, engine, log, kde.output_names()[0])
  147. probe = None
  148. monitor = None
  149. try:
  150. time.sleep(8)
  151. assert len(_cursor_scripts(kde)) == 1, "the cursor script is not loaded on KDE"
  152. monitor, calls = _watch_cursor_calls(kde, "before")
  153. _wiggle(kde)
  154. assert _calls(calls), "the pointer does not reach the engine"
  155. terminate([monitor.pid])
  156. probe = _window(kde, "fullscreen")
  157. time.sleep(8)
  158. assert not _renders(log, 3), "still rendering under a fullscreen window"
  159. assert not _cursor_scripts(kde), "the cursor script is still loaded after a long pause"
  160. monitor, calls = _watch_cursor_calls(kde, "paused")
  161. _wiggle(kde)
  162. assert not _calls(calls), "KWin still sends cursor calls to a paused engine"
  163. terminate([monitor.pid])
  164. terminate([probe.pid])
  165. probe = None
  166. for _ in range(20):
  167. if _cursor_scripts(kde):
  168. break
  169. time.sleep(1)
  170. assert len(_cursor_scripts(kde)) == 1, "the cursor script did not come back after the pause"
  171. monitor, calls = _watch_cursor_calls(kde, "resumed")
  172. _wiggle(kde)
  173. assert _calls(calls), "the pointer does not reach the engine after the pause"
  174. assert "could not load its KWin script again" not in log.read_text(errors="replace")
  175. finally:
  176. for stray in (monitor, probe):
  177. if stray is not None:
  178. terminate([stray.pid])
  179. process.terminate()
  180. process.wait(10)
  181. def test_cursor_script_reports_where_the_pointer_stopped(kde, engine, tmp_path):
  182. """a fast flick ends on a call within a few ms of the last one, the final position has to be reported anyway"""
  183. log = tmp_path / "engine.log"
  184. process = _engine(kde, engine, log, kde.output_names()[0])
  185. monitor = None
  186. try:
  187. time.sleep(8)
  188. monitor, calls = _watch_cursor_calls(kde, "flick")
  189. for run in range(5):
  190. target = (900 + run * 37, 500 + run * 11)
  191. kde.pointer("move", 400, 300, "move", 500, 340, "move", 600, 380, "move", 700, 420, "move", *target)
  192. time.sleep(0.5)
  193. seen = _calls(calls)
  194. assert seen and seen[-1] == target, f"the engine ended at {seen[-1] if seen else None}, not at {target}"
  195. finally:
  196. if monitor is not None:
  197. terminate([monitor.pid])
  198. process.terminate()
  199. process.wait(10)
  200. def test_media_signals_while_paused(kde, engine, tmp_path):
  201. """MPRIS signals that pile up behind a pause are read and dropped, the engine keeps its media connection, renders
  202. again afterwards and does not crash"""
  203. log = tmp_path / "engine.log"
  204. process = _engine(kde, engine, log, kde.output_names()[0])
  205. probe = None
  206. try:
  207. time.sleep(8)
  208. probe = _window(kde, "fullscreen")
  209. time.sleep(5)
  210. assert not _renders(log, 3), "still rendering under a fullscreen window"
  211. flood = kde.run(["python3", str(REPO / "tools/desktops/mpris_flood.py"), "3000", "4096"], timeout=120)
  212. assert flood.returncode == 0, flood.stderr
  213. time.sleep(3)
  214. assert process.poll() is None, "the engine died while paused"
  215. terminate([probe.pid])
  216. probe = None
  217. time.sleep(6)
  218. assert _renders(log), "rendering did not resume after the flood"
  219. text = log.read_text(errors="replace")
  220. assert "the session bus dropped the connection" not in text
  221. assert "Media: reconnected" not in text
  222. finally:
  223. if probe is not None:
  224. terminate([probe.pid])
  225. process.terminate()
  226. process.wait(10)