Skip to content

Commit

Permalink
ui/common: Introduce tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MattHag committed Sep 28, 2024
1 parent aaa324d commit 353abb0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/solaar/ui/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import logging

from typing import Tuple

import gi

from solaar.i18n import _
Expand All @@ -28,45 +30,46 @@
logger = logging.getLogger(__name__)


def _error_dialog(reason, object):
logger.error("error: %s %s", reason, object)

def _create_error_text(reason: str, object_) -> Tuple[str, str]:
if reason == "permissions":
title = _("Permissions error")
text = (
_("Found a Logitech receiver or device (%s), but did not have permission to open it.") % object
_("Found a Logitech receiver or device (%s), but did not have permission to open it.") % object_
+ "\n\n"
+ _("If you've just installed Solaar, try disconnecting the receiver or device and then reconnecting it.")
)
elif reason == "nodevice":
title = _("Cannot connect to device error")
text = (
_("Found a Logitech receiver or device at %s, but encountered an error connecting to it.") % object
_("Found a Logitech receiver or device at %s, but encountered an error connecting to it.") % object_
+ "\n\n"
+ _("Try disconnecting the device and then reconnecting it or turning it off and then on.")
)
elif reason == "unpair":
title = _("Unpairing failed")
text = (
_("Failed to unpair %{device} from %{receiver}.").format(device=object.name, receiver=object.receiver.name)
_("Failed to unpair %{device} from %{receiver}.").format(device=object_.name, receiver=object_.receiver.name)
+ "\n\n"
+ _("The receiver returned an error, with no further details.")
)
else:
raise Exception("ui.error_dialog: don't know how to handle (%s, %s)", reason, object)
raise Exception("ui.error_dialog: don't know how to handle (%s, %s)", reason, object_)
return title, text


assert title
assert text
def _error_dialog(reason: str, object_):
logger.error("error: %s %s", reason, object_)
title, text = _create_error_text(reason, object_)

m = Gtk.MessageDialog(None, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, text)
m.set_title(title)
m.run()
m.destroy()


def error_dialog(reason, object):
def error_dialog(reason, object_):
assert reason is not None
GLib.idle_add(_error_dialog, reason, object)
GLib.idle_add(_error_dialog, reason, object_)


_task_runner = None
Expand Down
28 changes: 28 additions & 0 deletions tests/solaar/ui/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from unittest import mock
from unittest.mock import PropertyMock

import pytest

from solaar.ui import common


@pytest.mark.parametrize(
"reason, expected_in_title, expected_in_text",
[
(
"permissions",
"Permissions error",
"not have permission to open",
),
("nodevice", "connect to device error", "error connecting"),
("unpair", "Unpairing failed", "receiver returned an error"),
],
)
def test_create_error_text(reason, expected_in_title, expected_in_text):
obj = mock.Mock()
obj.name = PropertyMock(return_value="test")

title, text = common._create_error_text(reason, obj)

assert expected_in_title in title
assert expected_in_text in text

0 comments on commit 353abb0

Please sign in to comment.