Make ui/desktop_notifications testable
Introduce unit tests. Related #2273
This commit is contained in:
parent
e8ef262433
commit
d5af19be8a
|
@ -13,9 +13,9 @@
|
||||||
## You should have received a copy of the GNU General Public License along
|
## 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.,
|
## with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
import importlib
|
||||||
|
|
||||||
# Optional desktop notifications.
|
# Optional desktop notifications.
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from solaar import NAME
|
from solaar import NAME
|
||||||
|
@ -26,27 +26,35 @@ from . import icons
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
try:
|
def notifications_available():
|
||||||
|
"""Checks if notification service is available."""
|
||||||
|
notifications_supported = False
|
||||||
|
try:
|
||||||
import gi
|
import gi
|
||||||
|
|
||||||
gi.require_version("Notify", "0.7")
|
gi.require_version("Notify", "0.7")
|
||||||
# this import is allowed to fail, in which case the entire feature is unavailable
|
|
||||||
|
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 GLib
|
||||||
from gi.repository import Notify
|
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
|
# cache references to shown notifications here, so if another status comes
|
||||||
# while its notification is still visible we don't create another one
|
# while its notification is still visible we don't create another one
|
||||||
_notifications = {}
|
_notifications = {}
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
"""Init the notifications system."""
|
"""Initialize desktop notifications."""
|
||||||
global available
|
global available
|
||||||
if available:
|
if available:
|
||||||
if not Notify.is_initted():
|
if not Notify.is_initted():
|
||||||
|
@ -60,6 +68,7 @@ if available:
|
||||||
return available and Notify.is_initted()
|
return available and Notify.is_initted()
|
||||||
|
|
||||||
def uninit():
|
def uninit():
|
||||||
|
"""Stop desktop notifications."""
|
||||||
if available and Notify.is_initted():
|
if available and Notify.is_initted():
|
||||||
if logger.isEnabledFor(logging.INFO):
|
if logger.isEnabledFor(logging.INFO):
|
||||||
logger.info("stopping desktop notifications")
|
logger.info("stopping desktop notifications")
|
||||||
|
@ -117,7 +126,7 @@ if available:
|
||||||
try:
|
try:
|
||||||
n.show()
|
n.show()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("showing %s", n)
|
logger.exception(f"showing {n}")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue