0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-13 11:11:49 +02:00

fix(device browser): mocks and utils for tests

This commit is contained in:
2025-05-13 16:12:26 +02:00
committed by David Perl
parent 92d1d6435d
commit e0e26c205b
4 changed files with 58 additions and 17 deletions

View File

@ -184,8 +184,8 @@ class FakePositioner(BECPositioner):
class Positioner(FakePositioner): class Positioner(FakePositioner):
"""just placeholder for testing embedded isinstance check in DeviceCombobox""" """just placeholder for testing embedded isinstance check in DeviceCombobox"""
def __init__(self, name="test", limits=None, read_value=1.0): def __init__(self, name="test", limits=None, read_value=1.0, enabled=True):
super().__init__(name, limits, read_value) super().__init__(name, limits=limits, read_value=read_value, enabled=enabled)
class Device(FakeDevice): class Device(FakeDevice):

View File

@ -20,7 +20,9 @@ class ExpandableGroupFrame(QFrame):
EXPANDED_ICON_NAME: str = "collapse_all" EXPANDED_ICON_NAME: str = "collapse_all"
COLLAPSED_ICON_NAME: str = "expand_all" COLLAPSED_ICON_NAME: str = "expand_all"
def __init__(self, title: str, parent: QWidget | None = None, expanded: bool = True) -> None: def __init__(
self, title: str, parent: QWidget | None = None, expanded: bool = True, icon: str = ""
) -> None:
super().__init__(parent=parent) super().__init__(parent=parent)
self._expanded = expanded self._expanded = expanded
@ -29,13 +31,21 @@ class ExpandableGroupFrame(QFrame):
self._layout = QVBoxLayout() self._layout = QVBoxLayout()
self._layout.setContentsMargins(0, 0, 0, 0) self._layout.setContentsMargins(0, 0, 0, 0)
self.setLayout(self._layout) self.setLayout(self._layout)
self._title_layout = QHBoxLayout() self._title_layout = QHBoxLayout()
self._layout.addLayout(self._title_layout) self._layout.addLayout(self._title_layout)
self._expansion_button = QToolButton()
self._update_icon()
self._title = QLabel(f"<b>{title}</b>") self._title = QLabel(f"<b>{title}</b>")
self._title_layout.addWidget(self._expansion_button) self._title_icon = QLabel()
self._title_layout.addWidget(self._title_icon)
self._title_layout.addWidget(self._title) self._title_layout.addWidget(self._title)
self.icon_name = icon
self._title_layout.addStretch(1)
self._expansion_button = QToolButton()
self._update_expansion_icon()
self._title_layout.addWidget(self._expansion_button, stretch=1)
self._contents = QWidget(self) self._contents = QWidget(self)
self._layout.addWidget(self._contents) self._layout.addWidget(self._contents)
@ -50,7 +60,7 @@ class ExpandableGroupFrame(QFrame):
@SafeSlot() @SafeSlot()
def switch_expanded_state(self): def switch_expanded_state(self):
self.expanded = not self.expanded # type: ignore self.expanded = not self.expanded # type: ignore
self._update_icon() self._update_expansion_icon()
@SafeProperty(bool) @SafeProperty(bool)
def expanded(self): # type: ignore def expanded(self): # type: ignore
@ -62,7 +72,7 @@ class ExpandableGroupFrame(QFrame):
self._contents.setVisible(expanded) self._contents.setVisible(expanded)
self.updateGeometry() self.updateGeometry()
def _update_icon(self): def _update_expansion_icon(self):
self._expansion_button.setIcon( self._expansion_button.setIcon(
material_icon(icon_name=self.EXPANDED_ICON_NAME, size=(10, 10), convert_to_pixmap=False) material_icon(icon_name=self.EXPANDED_ICON_NAME, size=(10, 10), convert_to_pixmap=False)
if self.expanded if self.expanded
@ -70,3 +80,21 @@ class ExpandableGroupFrame(QFrame):
icon_name=self.COLLAPSED_ICON_NAME, size=(10, 10), convert_to_pixmap=False icon_name=self.COLLAPSED_ICON_NAME, size=(10, 10), convert_to_pixmap=False
) )
) )
@SafeProperty(str)
def icon_name(self): # type: ignore
return self._title_icon_name
@icon_name.setter
def icon_name(self, icon_name: str):
self._title_icon_name = icon_name
self._set_title_icon(self._title_icon_name)
def _set_title_icon(self, icon_name: str):
if icon_name:
self._title_icon.setVisible(True)
self._title_icon.setPixmap(
material_icon(icon_name=icon_name, size=(20, 20), convert_to_pixmap=True)
)
else:
self._title_icon.setVisible(False)

View File

@ -45,7 +45,11 @@ class DictBackedTableModel(QAbstractTableModel):
def data(self, index, role=Qt.ItemDataRole): def data(self, index, role=Qt.ItemDataRole):
if index.isValid(): if index.isValid():
if role == Qt.ItemDataRole.DisplayRole or role == Qt.ItemDataRole.EditRole: if role in [
Qt.ItemDataRole.DisplayRole,
Qt.ItemDataRole.EditRole,
Qt.ItemDataRole.ToolTipRole,
]:
return str(self._data[index.row()][index.column()]) return str(self._data[index.row()][index.column()])
def setData(self, index, value, role): def setData(self, index, value, role):
@ -132,7 +136,7 @@ class DictBackedTable(QWidget):
self._table_view = QTreeView() self._table_view = QTreeView()
self._table_view.setModel(self._table_model) self._table_view.setModel(self._table_model)
self._table_view.setSizePolicy( self._table_view.setSizePolicy(
QSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Minimum) QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
) )
self._table_view.setAlternatingRowColors(True) self._table_view.setAlternatingRowColors(True)
self._layout.addWidget(self._table_view) self._layout.addWidget(self._table_view)

View File

@ -11,7 +11,13 @@ from .client_mocks import mocked_client
if TYPE_CHECKING: # pragma: no cover if TYPE_CHECKING: # pragma: no cover
from qtpy.QtWidgets import QListWidgetItem from qtpy.QtWidgets import QListWidgetItem
from bec_widgets.widgets.services.device_browser import DeviceItem from bec_widgets.widgets.services.device_browser.device_item import DeviceItem
# pylint: disable=no-member
# pylint: disable=missing-function-docstring
# pylint: disable=redefined-outer-name
# pylint: disable=protected-access
@pytest.fixture @pytest.fixture
@ -34,18 +40,21 @@ def test_device_browser_filtering(qtbot, device_browser):
""" """
Test that the device browser is able to filter the device list. Test that the device browser is able to filter the device list.
""" """
device_list = device_browser.ui.device_list
def num_visible(item_dict):
return len(list(filter(lambda i: not i.isHidden(), item_dict.values())))
device_browser.ui.filter_input.setText("sam") device_browser.ui.filter_input.setText("sam")
qtbot.wait(1000) qtbot.wait(1000)
assert device_list.count() == 3 assert num_visible(device_browser._device_items) == 3
device_browser.ui.filter_input.setText("nonexistent") device_browser.ui.filter_input.setText("nonexistent")
qtbot.wait(1000) qtbot.wait(1000)
assert device_list.count() == 0 assert num_visible(device_browser._device_items) == 0
device_browser.ui.filter_input.setText("") device_browser.ui.filter_input.setText("")
qtbot.wait(1000) qtbot.wait(1000)
assert device_list.count() == len(device_browser.dev) assert num_visible(device_browser._device_items) == len(device_browser.dev)
def test_device_item_mouse_press_event(device_browser, qtbot): def test_device_item_mouse_press_event(device_browser, qtbot):
@ -55,7 +64,7 @@ def test_device_item_mouse_press_event(device_browser, qtbot):
# Simulate a left mouse press event on the device item # Simulate a left mouse press event on the device item
device_item: QListWidgetItem = device_browser.ui.device_list.itemAt(0, 0) device_item: QListWidgetItem = device_browser.ui.device_list.itemAt(0, 0)
widget: DeviceItem = device_browser.ui.device_list.itemWidget(device_item) widget: DeviceItem = device_browser.ui.device_list.itemWidget(device_item)
qtbot.mouseClick(widget.label, Qt.MouseButton.LeftButton) qtbot.mouseClick(widget._title, Qt.MouseButton.LeftButton)
def test_device_item_mouse_press_and_move_events_creates_drag(device_browser, qtbot): def test_device_item_mouse_press_and_move_events_creates_drag(device_browser, qtbot):
@ -67,7 +76,7 @@ def test_device_item_mouse_press_and_move_events_creates_drag(device_browser, qt
device_name = widget.device device_name = widget.device
with mock.patch("qtpy.QtGui.QDrag.exec_") as mock_exec: with mock.patch("qtpy.QtGui.QDrag.exec_") as mock_exec:
with mock.patch("qtpy.QtGui.QDrag.setMimeData") as mock_set_mimedata: with mock.patch("qtpy.QtGui.QDrag.setMimeData") as mock_set_mimedata:
qtbot.mousePress(widget.label, Qt.MouseButton.LeftButton, pos=QPoint(0, 0)) qtbot.mousePress(widget._title, Qt.MouseButton.LeftButton, pos=QPoint(0, 0))
qtbot.mouseMove(widget, pos=QPoint(10, 10)) qtbot.mouseMove(widget, pos=QPoint(10, 10))
qtbot.mouseRelease(widget, Qt.MouseButton.LeftButton) qtbot.mouseRelease(widget, Qt.MouseButton.LeftButton)
mock_set_mimedata.assert_called_once() mock_set_mimedata.assert_called_once()