diff --git a/lib/solaar/ui/desktop_notifications.py b/lib/solaar/ui/desktop_notifications.py index 619f87da..67864d78 100644 --- a/lib/solaar/ui/desktop_notifications.py +++ b/lib/solaar/ui/desktop_notifications.py @@ -13,9 +13,9 @@ ## You should have received a copy of the GNU General Public License along ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import importlib # Optional desktop notifications. - import logging from solaar import NAME @@ -26,27 +26,35 @@ from . import icons logger = logging.getLogger(__name__) -try: - import gi +def notifications_available(): + """Checks if notification service is available.""" + notifications_supported = False + try: + import gi - gi.require_version("Notify", "0.7") - # this import is allowed to fail, in which case the entire feature is unavailable + gi.require_version("Notify", "0.7") + + importlib.util.find_spec("gi.repository.GLib") + importlib.util.find_spec("gi.repository.Notify") + + notifications_supported = True + except ValueError as e: + logger.warning(f"Notification service is not available: {e}") + return notifications_supported + + +available = notifications_available() + +if available: from gi.repository import GLib from gi.repository import Notify - # assumed to be working since the import succeeded - available = True - -except (ValueError, ImportError): - available = False - -if available: # cache references to shown notifications here, so if another status comes # while its notification is still visible we don't create another one _notifications = {} def init(): - """Init the notifications system.""" + """Initialize desktop notifications.""" global available if available: if not Notify.is_initted(): @@ -60,6 +68,7 @@ if available: return available and Notify.is_initted() def uninit(): + """Stop desktop notifications.""" if available and Notify.is_initted(): if logger.isEnabledFor(logging.INFO): logger.info("stopping desktop notifications") @@ -117,7 +126,7 @@ if available: try: n.show() except Exception: - logger.exception("showing %s", n) + logger.exception(f"showing {n}") else: diff --git a/tests/solaar/ui/test_desktop_notifications.py b/tests/solaar/ui/test_desktop_notifications.py new file mode 100644 index 00000000..f97c2153 --- /dev/null +++ b/tests/solaar/ui/test_desktop_notifications.py @@ -0,0 +1,28 @@ +from unittest import mock + +from solaar.ui import desktop_notifications + + +def test_notifications_available(): + result = desktop_notifications.notifications_available() + + assert not result + + +def test_init(): + assert not desktop_notifications.init() + + +def test_uninit(): + assert desktop_notifications.uninit() is None + + +def test_alert(): + reason = "unknown" + assert desktop_notifications.alert(reason) is None + + +def test_show(): + dev = mock.MagicMock() + reason = "unknown" + assert desktop_notifications.show(dev, reason) is None