86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
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: Borderlands 2",
|
|
)
|
|
|
|
testScript._flushSteamPendingNotification(fromTimer=True)
|
|
|
|
testScript._presentSteamNotificationTextNow.assert_called_once_with(
|
|
"username Playing: Borderlands 2",
|
|
notification,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|