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()