Tightened up Steam notifications. Fixed a system notifications regression.?

This commit is contained in:
Storm Dragon
2026-04-04 19:04:45 -04:00
parent 518d2b3bb6
commit f4af54228a
4 changed files with 204 additions and 8 deletions

View File

@@ -0,0 +1,58 @@
import importlib
import sys
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from cthulhu import messages
notification_script = importlib.import_module("cthulhu.scripts.apps.notification-daemon.script")
class NotificationDaemonTests(unittest.TestCase):
def test_window_created_ignores_empty_labels(self):
testScript = notification_script.Script.__new__(notification_script.Script)
labelEmpty = object()
labelText = object()
eventSource = object()
event = mock.Mock(source=eventSource)
testScript.utilities = mock.Mock()
testScript.utilities.displayedText.side_effect = lambda obj: {
labelEmpty: None,
labelText: "this is a test",
}.get(obj)
testScript.speechGenerator = mock.Mock()
testScript.speechGenerator.voice.return_value = object()
testScript.speakMessage = mock.Mock()
testScript.displayBrailleMessage = mock.Mock()
testScript.notificationPresenter = mock.Mock()
with (
mock.patch.object(
notification_script.AXUtilities,
"find_all_labels",
return_value=[labelEmpty, labelText],
),
mock.patch.object(
notification_script.AXObject,
"get_name",
side_effect=lambda obj: {
labelEmpty: "",
labelText: "",
}.get(obj, ""),
),
):
testScript.onWindowCreated(event)
expectedText = f"{messages.NOTIFICATION} this is a test"
testScript.speechGenerator.voice.assert_called_once_with(obj=eventSource, string=expectedText)
testScript.speakMessage.assert_called_once_with(expectedText, voice=testScript.speechGenerator.voice.return_value)
testScript.displayBrailleMessage.assert_called_once()
testScript.notificationPresenter.save_notification.assert_called_once_with(expectedText)
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,85 @@
import re
import sys
import unittest
from pathlib import Path
from unittest import mock
import gi
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
soundGeneratorModule = sys.modules.get("cthulhu.sound_generator")
if soundGeneratorModule is not None and not hasattr(soundGeneratorModule, "SoundGenerator"):
class _StubSoundGenerator:
pass
soundGeneratorModule.SoundGenerator = _StubSoundGenerator
from cthulhu.ax_object import AXObject
from cthulhu.ax_utilities import AXUtilities
from cthulhu.scripts.apps.steamwebhelper import script as steam_script
class SteamNotificationRootTests(unittest.TestCase):
def test_prefers_notification_ancestor_over_live_region_descendant(self):
testScript = steam_script.Script.__new__(steam_script.Script)
fragment = object()
notification = object()
def get_attribute(obj, name):
if obj is fragment and name == "container-live":
return "assertive"
return None
def find_ancestor(obj, predicate):
if obj is fragment and predicate(notification):
return notification
return None
with (
mock.patch.object(AXUtilities, "is_notification", side_effect=lambda obj: obj is notification),
mock.patch.object(AXUtilities, "is_alert", return_value=False),
mock.patch.object(AXObject, "get_attribute", side_effect=get_attribute),
mock.patch.object(AXObject, "find_ancestor", side_effect=find_ancestor),
):
result = testScript._findSteamNotificationRoot(fragment)
self.assertIs(result, notification)
class SteamNotificationQueueTests(unittest.TestCase):
def test_merges_non_overlapping_fragments_for_same_notification(self):
testScript = steam_script.Script.__new__(steam_script.Script)
notification = object()
testScript._lastSteamNotification = ("", 0.0)
testScript._steamPendingNotification = None
testScript._steamRelativeTimePattern = re.compile(
r"^(?:just now|now|\d+\s+(?:second|minute|hour|day|week|month|year)s?\s+ago)$",
re.IGNORECASE,
)
testScript._resetSteamPendingTimer = mock.Mock()
testScript._presentSteamNotificationTextNow = mock.Mock()
testScript._queueSteamNotification("username", notification)
testScript._queueSteamNotification("Playing: Borderlands 2", notification)
testScript._presentSteamNotificationTextNow.assert_not_called()
self.assertEqual(
testScript._steamPendingNotification["text"],
"username Playing: Game Title",
)
testScript._flushSteamPendingNotification(fromTimer=True)
testScript._presentSteamNotificationTextNow.assert_called_once_with(
"username Playing: Game Title",
notification,
)
if __name__ == "__main__":
unittest.main()