Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions tests/test_linux_wallpaperengine_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

from waypaper.changer import change_with_linux_wallpaperengine


class LinuxWallpaperengineNotificationTests(unittest.TestCase):
def make_config(self) -> SimpleNamespace:
return SimpleNamespace(
fill_option="fill",
linux_wallpaperengine_silent=True,
linux_wallpaperengine_noautomute=True,
linux_wallpaperengine_no_audio_processing=False,
linux_wallpaperengine_no_fullscreen_pause=False,
linux_wallpaperengine_fullscreen_pause_only_active=False,
linux_wallpaperengine_disable_particles=True,
linux_wallpaperengine_disable_mouse=False,
linux_wallpaperengine_disable_parallax=False,
linux_wallpaperengine_clamp="none",
linux_wallpaperengine_volume=15,
linux_wallpaperengine_fps=30,
)

def make_preview_path(self, tmp_dir: str) -> Path:
wallpaper_dir = Path(tmp_dir) / "wallpaper"
wallpaper_dir.mkdir()
preview_path = wallpaper_dir / "preview.jpg"
preview_path.write_text("preview", encoding="utf-8")
return preview_path

def test_running_process_does_not_notify(self):
with tempfile.TemporaryDirectory() as tmp_dir:
process = MagicMock()
process.poll.return_value = None

with patch("waypaper.changer.seek_and_destroy"), patch(
"waypaper.changer.subprocess.Popen", return_value=process
) as popen_mock, patch("waypaper.changer.time.sleep"), patch(
"waypaper.changer.notify_waypaper_issue"
) as notify_mock:
change_with_linux_wallpaperengine(
self.make_preview_path(tmp_dir),
self.make_config(),
"DP-1",
)

command = popen_mock.call_args.args[0]
self.assertNotIn("&", command)
self.assertTrue(popen_mock.call_args.kwargs["start_new_session"])
notify_mock.assert_not_called()

def test_immediate_exit_notifies_user(self):
with tempfile.TemporaryDirectory() as tmp_dir:
process = MagicMock()
process.poll.return_value = 7

with patch("waypaper.changer.seek_and_destroy"), patch(
"waypaper.changer.subprocess.Popen", return_value=process
), patch("waypaper.changer.time.sleep"), patch(
"waypaper.changer.notify_waypaper_issue"
) as notify_mock:
change_with_linux_wallpaperengine(
self.make_preview_path(tmp_dir),
self.make_config(),
"DP-1",
)

notify_mock.assert_called_once()
summary, message = notify_mock.call_args.args
self.assertEqual(summary, "Waypaper launch failed")
self.assertIn("linux-wallpaperengine exited immediately with code 7", message)


if __name__ == "__main__":
unittest.main()
28 changes: 26 additions & 2 deletions waypaper/changer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ def seek_and_destroy(process: str, monitor: str = "All"):
pass


def notify_waypaper_issue(summary: str, body: str) -> None:
"""Show a desktop notification when a wallpaper backend fails."""
try:
subprocess.Popen(
["notify-send", summary, body],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except OSError:
pass


def change_with_swaybg(image_path: Path, cf: Config, monitor: str):
"""Change wallpaper with swaybg backend"""

Expand Down Expand Up @@ -438,9 +451,20 @@ def change_with_linux_wallpaperengine(image_path: Path, cf: Config, monitor: str
command.extend(["--scaling", fill])

command.append(str(image_path.parent))
command.append("&")
print(f"{command=}")
subprocess.Popen(" ".join(command), shell=True, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
process = subprocess.Popen(
command,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
start_new_session=True,
)
time.sleep(0.5)
exit_code = process.poll()
if exit_code is not None:
message = f"linux-wallpaperengine exited immediately with code {exit_code}."
print(message)
notify_waypaper_issue("Waypaper launch failed", message)

def change_wallpaper(image_path: Path, cf: Config, monitor: str):
"""Run system commands to change the wallpaper depending on the backend"""
Expand Down