From 5d1b2279cc8afe073227f6e36af867d6d14db126 Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Tue, 10 Oct 2023 15:00:02 -0400 Subject: [PATCH] test: add tests --- src/magicgui/backends/_ipynb/widgets.py | 3 ++- src/magicgui/backends/_qtpy/widgets.py | 5 +++++ tests/test_widgets.py | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/magicgui/backends/_ipynb/widgets.py b/src/magicgui/backends/_ipynb/widgets.py index 0aecfd8f1..81593d453 100644 --- a/src/magicgui/backends/_ipynb/widgets.py +++ b/src/magicgui/backends/_ipynb/widgets.py @@ -266,7 +266,7 @@ class _IPySupportsIcon(protocols.SupportsIcon): _ipywidget: ipywdg.Button - def _mgui_set_icon(self, value: str, color: str) -> None: + def _mgui_set_icon(self, value: str | None, color: str | None) -> None: """Set icon.""" # only ipywdg.Button actually supports icons. # but our button protocol allows it for all buttons subclasses @@ -277,6 +277,7 @@ def _mgui_set_icon(self, value: str, color: str) -> None: # which works for iconify icons served by qt, while still # allowing for bare "icon-name" syntax which works for ipywidgets. # note however... only fa4/5 icons will work for ipywidgets. + value = value or "" self._ipywidget.icon = value.replace("fa-", "").split(":", 1)[-1] self._ipywidget.style.text_color = color diff --git a/src/magicgui/backends/_qtpy/widgets.py b/src/magicgui/backends/_qtpy/widgets.py index 8aff10df7..e7f8259f1 100644 --- a/src/magicgui/backends/_qtpy/widgets.py +++ b/src/magicgui/backends/_qtpy/widgets.py @@ -457,6 +457,11 @@ def _update_icon(self) -> None: pal = self._qwidget.palette() color = pal.color(QPalette.ColorRole.WindowText).name() + if ":" not in value: + # for parity with the other backends, assume fontawesome + # if no prefix is given. + value = f"fa-regular:{value}" + try: self._qwidget.setIcon(superqt.QIconifyIcon(value, color=color)) except (OSError, ValueError) as e: diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 60abd1fc2..f2fc9c772 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -809,6 +809,17 @@ def test_pushbutton_click_signal(): mock2.assert_called_once() +def test_pushbutton_icon(backend: str): + use_app(backend) + btn = widgets.PushButton(icon="mdi:folder") + btn.set_icon("smile", "red") + btn.set_icon(None) + + if backend == "qt": + with pytest.warns(UserWarning, match="Could not set iconify icon"): + btn.set_icon("bad:key") + + def test_list_edit(): """Test ListEdit.""" from typing import List