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

feat(theme): added theme handler to bec widget base class; added tests

This commit is contained in:
2024-08-31 14:32:10 +02:00
parent 08c3d7d175
commit 7fb938a850
8 changed files with 143 additions and 40 deletions

View File

@ -14,6 +14,8 @@ def qapplication(qtbot): # pylint: disable=unused-argument
qapp = QApplication.instance()
# qapp.quit()
qapp.processEvents()
if hasattr(qapp, "os_listener") and qapp.os_listener:
qapp.removeEventFilter(qapp.os_listener)
try:
qtbot.waitUntil(lambda: qapp.topLevelWidgets() == [])
except QtBotTimeoutError as exc:

View File

@ -2,6 +2,7 @@ from unittest import mock
import pytest
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QApplication
from bec_widgets.utils.colors import set_theme
from bec_widgets.widgets.dark_mode_button.dark_mode_button import DarkModeButton
@ -70,3 +71,16 @@ def test_dark_mode_button_changes_theme(dark_mode_button):
dark_mode_button.toggle_dark_mode()
mocked_set_theme.assert_called_with("light")
def test_dark_mode_button_changes_on_os_theme_change(qtbot, dark_mode_button):
"""
Test that the dark mode button changes the theme correctly when the OS theme changes.
"""
qapp = QApplication.instance()
assert dark_mode_button.dark_mode_enabled is False
assert dark_mode_button.mode_button.toolTip() == "Set Dark Mode"
qapp.theme_signal.theme_updated.emit("dark")
qtbot.wait(100)
assert dark_mode_button.dark_mode_enabled is True
assert dark_mode_button.mode_button.toolTip() == "Set Light Mode"

View File

@ -61,7 +61,7 @@ def test_vscode_cleanup(qtbot, patched_vscode_process):
vscode_patched, mock_killpg = patched_vscode_process
vscode_patched.process.pid = 123
vscode_patched.process.poll.return_value = None
vscode_patched.cleanup()
vscode_patched.cleanup_vscode()
mock_killpg.assert_called_once_with(123, 15)
vscode_patched.process.wait.assert_called_once()
@ -70,6 +70,6 @@ def test_close_event_on_terminated_code(qtbot, patched_vscode_process):
vscode_patched, mock_killpg = patched_vscode_process
vscode_patched.process.pid = 123
vscode_patched.process.poll.return_value = 0
vscode_patched.cleanup()
vscode_patched.cleanup_vscode()
mock_killpg.assert_not_called()
vscode_patched.process.wait.assert_not_called()

View File

@ -2,8 +2,11 @@ from unittest.mock import MagicMock, patch
import pyqtgraph as pg
import pytest
from qtpy.QtGui import QColor
from qtpy.QtWidgets import QApplication
from bec_widgets.qt_utils.settings_dialog import SettingsDialog
from bec_widgets.utils.colors import apply_theme, get_theme_palette, set_theme
from bec_widgets.widgets.figure.plots.axis_settings import AxisSettings
from bec_widgets.widgets.waveform.waveform_popups.curve_dialog.curve_dialog import CurveSettings
from bec_widgets.widgets.waveform.waveform_widget import BECWaveformWidget
@ -460,3 +463,42 @@ def test_axis_dialog_set_properties(qtbot, waveform_widget):
assert waveform_widget._config_dict["axis"]["y_scale"] == "linear"
assert waveform_widget._config_dict["axis"]["x_lim"] == (5, 15)
assert waveform_widget._config_dict["axis"]["y_lim"] == (5, 15)
def test_waveform_widget_theme_update(qtbot, waveform_widget):
"""Test theme update for waveform widget."""
qapp = QApplication.instance()
# Set the theme directly; equivalent to clicking the dark mode button
# The background color should be black and the axis color should be white
set_theme("dark")
palette = get_theme_palette()
waveform_color_dark = waveform_widget.waveform.plot_item.getAxis("left").pen().color()
bg_color = waveform_widget.fig.backgroundBrush().color()
assert bg_color == QColor("black")
assert waveform_color_dark == palette.text().color()
# Set the theme to light; equivalent to clicking the light mode button
# The background color should be white and the axis color should be black
set_theme("light")
palette = get_theme_palette()
waveform_color_light = waveform_widget.waveform.plot_item.getAxis("left").pen().color()
bg_color = waveform_widget.fig.backgroundBrush().color()
assert bg_color == QColor("white")
assert waveform_color_light == palette.text().color()
assert waveform_color_dark != waveform_color_light
# Set the theme to auto; equivalent starting the application with no theme set
set_theme("auto")
# Simulate that the OS theme changes to dark
qapp.theme_signal.theme_updated.emit("dark")
apply_theme("dark")
# The background color should be black and the axis color should be white
# As we don't have access to the listener here, we can't test the palette change. Instead,
# we compare the waveform color to the dark theme color
waveform_color = waveform_widget.waveform.plot_item.getAxis("left").pen().color()
bg_color = waveform_widget.fig.backgroundBrush().color()
assert bg_color == QColor("black")
assert waveform_color == waveform_color_dark