Files
slic/tests/test_utils_dbusnotify.py
T
tligui_y 3cdff137bc
Run CI Tests / test (push) Successful in 2m29s
Update tests/test_utils_dbusnotify.py
2025-08-29 10:49:44 +02:00

90 lines
3.3 KiB
Python

import os
import time
import subprocess
import pytest
import dbus
from dbusmock import DBusTestCase, MOCK_IFACE
@pytest.fixture(scope="session")
def notifier():
# Import DBusNotify with an active SessionBus (dunst -print).
import slic.utils.dbusnotify as dbn
return dbn.DBusNotify()
# Test creation of a simple notification
def test_notify_create(notifier):
from slic.utils.dbusnotify import Notification
notification = notifier.notify("Test Notification", "This is a test notification.")
assert notification.nid is not None
assert isinstance(notification, Notification)
assert notification.summary == "Test Notification"
assert notification.body == "This is a test notification."
# Test updating an existing notification
def test_notify_update(notifier):
notification = notifier.notify("Test Notification", "This is a test notification.")
notification.update(summary="Updated Test", body="This notification has been updated.")
assert notification.summary == "Updated Test"
assert notification.body == "This notification has been updated."
# Test retrieving DBus server information
def test_get_server_info(notifier):
server_info = notifier.get_server_info()
assert isinstance(server_info, dict)
assert "name" in server_info and server_info["name"], "Server name is empty"
assert "vendor" in server_info and server_info["vendor"], "Server vendor is empty"
assert "version" in server_info and server_info["version"], "Server version is empty"
assert "spec" in server_info and server_info["spec"], "Server spec is empty"
# Test retrieving server capabilities
def test_get_capabilities(notifier):
capabilities = [str(x) for x in list(notifier.get_capabilities())]
assert isinstance(capabilities, list), "Capabilities should be a list."
assert len(capabilities) > 0, "Capabilities list is empty."
# dunst typically exposes "body", "actions", "icon", "sound"
expected = {"actions", "body", "icon", "sound"}
assert expected.intersection(capabilities), "Capabilities don't contain expected values"
for cap in capabilities:
assert isinstance(cap, str), f"Capability should be str, got {type(cap)}"
# Test sending and closing a notification
def test_notify_and_close(notifier):
notification = notifier.notify("Test Close", "This notification will be closed.")
time.sleep(0.2) # small margin
try:
notification.close()
except Exception as e:
pytest.fail(f"close() raised an exception: {e}")
# Test that an invalid body type raises an exception
def test_notify_invalid_value(notifier):
with pytest.raises(Exception):
notifier.notify("Invalid Test", 1234)
# Test conversion of dbus.String
def test_convert_dbus_strings():
from slic.utils.dbusnotify import convert_dbus_strings
test_sequence = [dbus.String('Test String 1'), 42, dbus.String('Test String 2'), 3.14]
converted = convert_dbus_strings(test_sequence)
assert isinstance(converted[0], str), f"Expected str, got {type(converted[0])}"
assert isinstance(converted[2], str), f"Expected str, got {type(converted[2])}"
assert isinstance(converted[1], int), f"Expected int, got {type(converted[1])}"
assert isinstance(converted[3], float), f"Expected float, got {type(converted[3])}"
assert converted[0] == 'Test String 1'
assert converted[2] == 'Test String 2'